fixed code style

This commit is contained in:
2026-02-20 04:02:26 +03:00
parent a9bec00973
commit 97a3111791
2 changed files with 182 additions and 87 deletions

View File

@@ -1,13 +1,13 @@
#include <GL/glew.h> #include <GL/glew.h>
#include <GLFW/glfw3.h> #include <GLFW/glfw3.h>
#include <iostream>
#include <vector>
#include <cmath> #include <cmath>
#include <iostream>
#include <omath/omath.hpp> #include <omath/omath.hpp>
#include <vector>
using omath::Vector3;
using omath::Triangle;
using omath::Color; using omath::Color;
using omath::Triangle;
using omath::Vector3;
static const char* vertexShaderSource = R"( static const char* vertexShaderSource = R"(
#version 330 core #version 330 core
@@ -45,13 +45,15 @@ void main() {
} }
)"; )";
GLuint compileShader(GLenum type, const char* src) { GLuint compileShader(GLenum type, const char* src)
{
GLuint shader = glCreateShader(type); GLuint shader = glCreateShader(type);
glShaderSource(shader, 1, &src, nullptr); glShaderSource(shader, 1, &src, nullptr);
glCompileShader(shader); glCompileShader(shader);
GLint ok; GLint ok;
glGetShaderiv(shader, GL_COMPILE_STATUS, &ok); glGetShaderiv(shader, GL_COMPILE_STATUS, &ok);
if (!ok) { if (!ok)
{
char log[1024]; char log[1024];
glGetShaderInfoLog(shader, sizeof(log), nullptr, log); glGetShaderInfoLog(shader, sizeof(log), nullptr, log);
std::cerr << "Shader error: " << log << std::endl; std::cerr << "Shader error: " << log << std::endl;
@@ -59,47 +61,118 @@ GLuint compileShader(GLenum type, const char* src) {
return shader; return shader;
} }
void drawChar(char c, float x, float y, float scale, const Color& color, std::vector<float>& lines) { void drawChar(char c, float x, float y, float scale, const Color& color, std::vector<float>& lines)
{
float w = 0.5f * scale; float w = 0.5f * scale;
float h = 1.0f * scale; float h = 1.0f * scale;
auto add = [&](float x1, float y1, float x2, float y2) { auto add = [&](float x1, float y1, float x2, float y2)
lines.push_back(x + x1*w); lines.push_back(y + y1*h); lines.push_back(0.0f); {
lines.push_back(color.x); lines.push_back(color.y); lines.push_back(color.z); lines.push_back(x + x1 * w);
lines.push_back(y + y1 * h);
lines.push_back(0.0f);
lines.push_back(color.x);
lines.push_back(color.y);
lines.push_back(color.z);
lines.push_back(1.0f); // size lines.push_back(1.0f); // size
lines.push_back(1.0f); // isLine lines.push_back(1.0f); // isLine
lines.push_back(x + x2*w); lines.push_back(y + y2*h); lines.push_back(0.0f); lines.push_back(x + x2 * w);
lines.push_back(color.x); lines.push_back(color.y); lines.push_back(color.z); lines.push_back(y + y2 * h);
lines.push_back(0.0f);
lines.push_back(color.x);
lines.push_back(color.y);
lines.push_back(color.z);
lines.push_back(1.0f); // size lines.push_back(1.0f); // size
lines.push_back(1.0f); // isLine lines.push_back(1.0f); // isLine
}; };
switch(c) { switch (c)
case '0': add(0,0, 1,0); add(1,0, 1,1); add(1,1, 0,1); add(0,1, 0,0); break; {
case '1': add(0.5f,0, 0.5f,1); add(0.25f,0.75f, 0.5f,1); add(0.25f,0, 0.75f,0); break; case '0':
case '2': add(0,1, 1,1); add(1,1, 1,0.5f); add(1,0.5f, 0,0.5f); add(0,0.5f, 0,0); add(0,0, 1,0); break; add(0, 0, 1, 0);
case '3': add(0,1, 1,1); add(1,1, 1,0); add(1,0, 0,0); add(0,0.5f, 1,0.5f); break; add(1, 0, 1, 1);
case '4': add(0,1, 0,0.5f); add(0,0.5f, 1,0.5f); add(1,1, 1,0); break; add(1, 1, 0, 1);
case '5': add(1,1, 0,1); add(0,1, 0,0.5f); add(0,0.5f, 1,0.5f); add(1,0.5f, 1,0); add(1,0, 0,0); break; add(0, 1, 0, 0);
case '6': add(1,1, 0,1); add(0,1, 0,0); add(0,0, 1,0); add(1,0, 1,0.5f); add(1,0.5f, 0,0.5f); break; break;
case '7': add(0,1, 1,1); add(1,1, 0.5f,0); break; case '1':
case '8': add(0,0, 1,0); add(1,0, 1,1); add(1,1, 0,1); add(0,1, 0,0); add(0,0.5f, 1,0.5f); break; add(0.5f, 0, 0.5f, 1);
case '9': add(1,0.5f, 0,0.5f); add(0,0.5f, 0,1); add(0,1, 1,1); add(1,1, 1,0); add(1,0, 0,0); break; add(0.25f, 0.75f, 0.5f, 1);
case '.': add(0.4f,0, 0.6f,0); add(0.6f,0, 0.6f,0.2f); add(0.6f,0.2f, 0.4f,0.2f); add(0.4f,0.2f, 0.4f,0); break; add(0.25f, 0, 0.75f, 0);
break;
case '2':
add(0, 1, 1, 1);
add(1, 1, 1, 0.5f);
add(1, 0.5f, 0, 0.5f);
add(0, 0.5f, 0, 0);
add(0, 0, 1, 0);
break;
case '3':
add(0, 1, 1, 1);
add(1, 1, 1, 0);
add(1, 0, 0, 0);
add(0, 0.5f, 1, 0.5f);
break;
case '4':
add(0, 1, 0, 0.5f);
add(0, 0.5f, 1, 0.5f);
add(1, 1, 1, 0);
break;
case '5':
add(1, 1, 0, 1);
add(0, 1, 0, 0.5f);
add(0, 0.5f, 1, 0.5f);
add(1, 0.5f, 1, 0);
add(1, 0, 0, 0);
break;
case '6':
add(1, 1, 0, 1);
add(0, 1, 0, 0);
add(0, 0, 1, 0);
add(1, 0, 1, 0.5f);
add(1, 0.5f, 0, 0.5f);
break;
case '7':
add(0, 1, 1, 1);
add(1, 1, 0.5f, 0);
break;
case '8':
add(0, 0, 1, 0);
add(1, 0, 1, 1);
add(1, 1, 0, 1);
add(0, 1, 0, 0);
add(0, 0.5f, 1, 0.5f);
break;
case '9':
add(1, 0.5f, 0, 0.5f);
add(0, 0.5f, 0, 1);
add(0, 1, 1, 1);
add(1, 1, 1, 0);
add(1, 0, 0, 0);
break;
case '.':
add(0.4f, 0, 0.6f, 0);
add(0.6f, 0, 0.6f, 0.2f);
add(0.6f, 0.2f, 0.4f, 0.2f);
add(0.4f, 0.2f, 0.4f, 0);
break;
} }
} }
void drawText(const std::string& text, float x, float y, float scale, const Color& color, std::vector<float>& lines) { void drawText(const std::string& text, float x, float y, float scale, const Color& color, std::vector<float>& lines)
{
float cursor = x; float cursor = x;
for (char c : text) { for (char c : text)
{
drawChar(c, cursor, y, scale, color, lines); drawChar(c, cursor, y, scale, color, lines);
cursor += (c == '.' ? 0.3f : 0.7f) * scale; cursor += (c == '.' ? 0.3f : 0.7f) * scale;
} }
} }
GLFWwindow* initWindow(int width, int height, const char* title) { GLFWwindow* initWindow(int width, int height, const char* title)
if (!glfwInit()) { {
if (!glfwInit())
{
std::cerr << "Failed to initialize GLFW\n"; std::cerr << "Failed to initialize GLFW\n";
return nullptr; return nullptr;
} }
@@ -109,7 +182,8 @@ GLFWwindow* initWindow(int width, int height, const char* title) {
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
GLFWwindow* window = glfwCreateWindow(width, height, title, NULL, NULL); GLFWwindow* window = glfwCreateWindow(width, height, title, NULL, NULL);
if (!window) { if (!window)
{
std::cerr << "Failed to create GLFW window\n"; std::cerr << "Failed to create GLFW window\n";
glfwTerminate(); glfwTerminate();
return nullptr; return nullptr;
@@ -119,20 +193,27 @@ GLFWwindow* initWindow(int width, int height, const char* title) {
// Check if context is valid using standard GL // Check if context is valid using standard GL
const GLubyte* renderer = glGetString(GL_RENDERER); const GLubyte* renderer = glGetString(GL_RENDERER);
const GLubyte* version = glGetString(GL_VERSION); const GLubyte* version = glGetString(GL_VERSION);
if (renderer && version) { if (renderer && version)
{
std::cout << "Renderer: " << renderer << "\n"; std::cout << "Renderer: " << renderer << "\n";
std::cout << "OpenGL version supported: " << version << "\n"; std::cout << "OpenGL version supported: " << version << "\n";
} else { }
else
{
std::cerr << "Failed to get GL_RENDERER or GL_VERSION. Context might be invalid.\n"; std::cerr << "Failed to get GL_RENDERER or GL_VERSION. Context might be invalid.\n";
} }
glewExperimental = GL_TRUE; glewExperimental = GL_TRUE;
GLenum glewErr = glewInit(); GLenum glewErr = glewInit();
if (glewErr != GLEW_OK) { if (glewErr != GLEW_OK)
{
// Ignore GLEW_ERROR_NO_GLX_DISPLAY if we have a valid context (e.g. Wayland) // Ignore GLEW_ERROR_NO_GLX_DISPLAY if we have a valid context (e.g. Wayland)
if (glewErr == GLEW_ERROR_NO_GLX_DISPLAY && renderer) { if (glewErr == GLEW_ERROR_NO_GLX_DISPLAY && renderer)
{
std::cerr << "GLEW warning: " << glewGetErrorString(glewErr) << " (Ignored because context seems valid)\n"; std::cerr << "GLEW warning: " << glewGetErrorString(glewErr) << " (Ignored because context seems valid)\n";
} else { }
else
{
std::cerr << "Failed to initialize GLEW: " << glewGetErrorString(glewErr) << "\n"; std::cerr << "Failed to initialize GLEW: " << glewGetErrorString(glewErr) << "\n";
glfwTerminate(); glfwTerminate();
return nullptr; return nullptr;
@@ -141,7 +222,8 @@ GLFWwindow* initWindow(int width, int height, const char* title) {
return window; return window;
} }
GLuint createShaderProgram() { GLuint createShaderProgram()
{
GLuint vs = compileShader(GL_VERTEX_SHADER, vertexShaderSource); GLuint vs = compileShader(GL_VERTEX_SHADER, vertexShaderSource);
GLuint fs = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource); GLuint fs = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource);
GLuint shaderProgram = glCreateProgram(); GLuint shaderProgram = glCreateProgram();
@@ -151,18 +233,26 @@ GLuint createShaderProgram() {
return shaderProgram; return shaderProgram;
} }
void generatePointCloud(std::vector<float>& pointCloud, const Triangle<Vector3<float>>& triangle) { void generatePointCloud(std::vector<float>& pointCloud, const Triangle<Vector3<float>>& triangle)
{
const auto& A = triangle.m_vertex1; const auto& A = triangle.m_vertex1;
const auto& B = triangle.m_vertex2; const auto& B = triangle.m_vertex2;
const auto& C = triangle.m_vertex3; const auto& C = triangle.m_vertex3;
// Iterating over barycentric coordinates (u, v, w) from 0.0 to 1.0 // Iterating over barycentric coordinates (u, v, w) from 0.0 to 1.0
for (float u = 0.0f; u <= 1.0f; u += 0.015f) { for (float u = 0.0f; u <= 1.0f; u += 0.015f)
for (float v = 0.0f; v <= 1.0f - u; v += 0.015f) { {
for (float v = 0.0f; v <= 1.0f - u; v += 0.015f)
{
float w = 1.0f - u - v; float w = 1.0f - u - v;
if (w >= 0.0f && w <= 1.0f) { if (w >= 0.0f && w <= 1.0f)
{
Vector3<float> P = A * u + B * v + C * w; Vector3<float> P = A * u + B * v + C * w;
pointCloud.push_back(P.x); pointCloud.push_back(P.y); pointCloud.push_back(P.z); pointCloud.push_back(P.x);
pointCloud.push_back(u); pointCloud.push_back(v); pointCloud.push_back(w); pointCloud.push_back(P.y);
pointCloud.push_back(P.z);
pointCloud.push_back(u);
pointCloud.push_back(v);
pointCloud.push_back(w);
pointCloud.push_back(2.0f); // size pointCloud.push_back(2.0f); // size
pointCloud.push_back(0.0f); // isLine pointCloud.push_back(0.0f); // isLine
} }
@@ -170,7 +260,9 @@ void generatePointCloud(std::vector<float>& pointCloud, const Triangle<Vector3<f
} }
} }
void setupBuffers(GLuint& VAO_cloud, GLuint& VBO_cloud, const std::vector<float>& pointCloud, GLuint& VAO_dyn, GLuint& VBO_dyn) { void setupBuffers(GLuint& VAO_cloud, GLuint& VBO_cloud, const std::vector<float>& pointCloud, GLuint& VAO_dyn,
GLuint& VBO_dyn)
{
glGenVertexArrays(1, &VAO_cloud); glGenVertexArrays(1, &VAO_cloud);
glGenBuffers(1, &VBO_cloud); glGenBuffers(1, &VBO_cloud);
glBindVertexArray(VAO_cloud); glBindVertexArray(VAO_cloud);
@@ -200,7 +292,9 @@ void setupBuffers(GLuint& VAO_cloud, GLuint& VBO_cloud, const std::vector<float>
glEnableVertexAttribArray(3); glEnableVertexAttribArray(3);
} }
void updateDynamicData(std::vector<float>& dynData, float u, float v, float w, const Vector3<float>& P, const Triangle<Vector3<float>>& triangle) { void updateDynamicData(std::vector<float>& dynData, float u, float v, float w, const Vector3<float>& P,
const Triangle<Vector3<float>>& triangle)
{
const auto& A = triangle.m_vertex1; const auto& A = triangle.m_vertex1;
const auto& B = triangle.m_vertex2; const auto& B = triangle.m_vertex2;
const auto& C = triangle.m_vertex3; const auto& C = triangle.m_vertex3;
@@ -210,25 +304,19 @@ void updateDynamicData(std::vector<float>& dynData, float u, float v, float w, c
float sizeC = 10.0f + w * 30.0f; float sizeC = 10.0f + w * 30.0f;
float sizeP = 12.0f; float sizeP = 12.0f;
dynData = { dynData = {// Lines from P to A, B, C
// Lines from P to A, B, C P.x, P.y, P.z, u, v, w, 1.0f, 1.0f, A.x, A.y, A.z, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
P.x, P.y, P.z, u, v, w, 1.0f, 1.0f,
A.x, A.y, A.z, 1.0f, 0.0f, 0.0f, 1.0f, 1.0f,
P.x, P.y, P.z, u, v, w, 1.0f, 1.0f, P.x, P.y, P.z, u, v, w, 1.0f, 1.0f, B.x, B.y, B.z, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
B.x, B.y, B.z, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
P.x, P.y, P.z, u, v, w, 1.0f, 1.0f, P.x, P.y, P.z, u, v, w, 1.0f, 1.0f, C.x, C.y, C.z, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
C.x, C.y, C.z, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f,
// The animated dot itself (White) // The animated dot itself (White)
P.x, P.y, P.z, 1.0f, 1.0f, 1.0f, sizeP, 0.0f, P.x, P.y, P.z, 1.0f, 1.0f, 1.0f, sizeP, 0.0f,
// The 3 corner dots // The 3 corner dots
A.x, A.y, A.z, 1.0f, 0.0f, 0.0f, sizeA, 0.0f, A.x, A.y, A.z, 1.0f, 0.0f, 0.0f, sizeA, 0.0f, B.x, B.y, B.z, 0.0f, 1.0f, 0.0f, sizeB, 0.0f, C.x, C.y,
B.x, B.y, B.z, 0.0f, 1.0f, 0.0f, sizeB, 0.0f, C.z, 0.0f, 0.0f, 1.0f, sizeC, 0.0f};
C.x, C.y, C.z, 0.0f, 0.0f, 1.0f, sizeC, 0.0f
};
char bufA[16], bufB[16], bufC[16]; char bufA[16], bufB[16], bufC[16];
snprintf(bufA, sizeof(bufA), "%.2f", u); snprintf(bufA, sizeof(bufA), "%.2f", u);
@@ -240,23 +328,24 @@ void updateDynamicData(std::vector<float>& dynData, float u, float v, float w, c
float distB = 0.13f; float distB = 0.13f;
float distC = 0.13f; float distC = 0.13f;
drawText(bufA, A.x - 0.05f, A.y + distA, 0.1f, Color(1,0,0,1), dynData); drawText(bufA, A.x - 0.05f, A.y + distA, 0.1f, Color(1, 0, 0, 1), dynData);
drawText(bufB, B.x - 0.15f - distB, B.y - 0.05f - distB, 0.1f, Color(0,1,0,1), dynData); drawText(bufB, B.x - 0.15f - distB, B.y - 0.05f - distB, 0.1f, Color(0, 1, 0, 1), dynData);
drawText(bufC, C.x + 0.05f + distC, C.y - 0.05f - distC, 0.1f, Color(0,0,1,1), dynData); drawText(bufC, C.x + 0.05f + distC, C.y - 0.05f - distC, 0.1f, Color(0, 0, 1, 1), dynData);
} }
int main() { int main()
{
GLFWwindow* window = initWindow(800, 800, "Barycentric Coordinates"); GLFWwindow* window = initWindow(800, 800, "Barycentric Coordinates");
if (!window) return -1; if (!window)
return -1;
GLuint shaderProgram = createShaderProgram(); GLuint shaderProgram = createShaderProgram();
// Triangle vertices as shown in the picture (Red, Green, Blue) // Triangle vertices as shown in the picture (Red, Green, Blue)
// Scaled down slightly to leave room for text // Scaled down slightly to leave room for text
Triangle<Vector3<float>> triangle( Triangle<Vector3<float>> triangle(Vector3<float>(0.0f, 0.6f, 0.0f), // Red dot (top)
Vector3<float>(0.0f, 0.6f, 0.0f), // Red dot (top) Vector3<float>(-0.6f, -0.6f, 0.0f), // Green dot (bottom left)
Vector3<float>(-0.6f, -0.6f, 0.0f), // Green dot (bottom left) Vector3<float>(0.6f, -0.6f, 0.0f) // Blue dot (bottom right)
Vector3<float>(0.6f, -0.6f, 0.0f) // Blue dot (bottom right)
); );
std::vector<float> pointCloud; std::vector<float> pointCloud;
@@ -269,7 +358,8 @@ int main() {
glEnable(GL_BLEND); glEnable(GL_BLEND);
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
while (!glfwWindowShouldClose(window)) { while (!glfwWindowShouldClose(window))
{
glClearColor(0.02f, 0.02f, 0.02f, 1.0f); glClearColor(0.02f, 0.02f, 0.02f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
@@ -284,18 +374,22 @@ int main() {
float u = (std::sin(t * 1.5f) * 0.5f + 0.5f); float u = (std::sin(t * 1.5f) * 0.5f + 0.5f);
float v = (std::cos(t * 1.1f) * 0.5f + 0.5f); float v = (std::cos(t * 1.1f) * 0.5f + 0.5f);
if (u + v > 1.0f) { if (u + v > 1.0f)
{
u = 1.0f - u; u = 1.0f - u;
v = 1.0f - v; v = 1.0f - v;
} }
float w = 1.0f - u - v; float w = 1.0f - u - v;
if (w > 1.0f) { if (w > 1.0f)
{
float diff = w - 1.0f; float diff = w - 1.0f;
w = 1.0f; w = 1.0f;
u += diff / 2.0f; u += diff / 2.0f;
v += diff / 2.0f; v += diff / 2.0f;
} else if (w < 0.0f) { }
else if (w < 0.0f)
{
float diff = -w; float diff = -w;
w = 0.0f; w = 0.0f;
u -= diff / 2.0f; u -= diff / 2.0f;
@@ -316,7 +410,8 @@ int main() {
// Draw text lines // Draw text lines
int numTextVertices = (dynData.size() / 8) - 10; int numTextVertices = (dynData.size() / 8) - 10;
if (numTextVertices > 0) { if (numTextVertices > 0)
{
glDrawArrays(GL_LINES, 10, numTextVertices); glDrawArrays(GL_LINES, 10, numTextVertices);
} }

View File

@@ -342,7 +342,7 @@ int main()
old_mouse_time = glfwGetTime(); old_mouse_time = glfwGetTime();
mouse_capture = !mouse_capture; mouse_capture = !mouse_capture;
glfwSetInputMode(window, GLFW_CURSOR, mouse_capture ? GLFW_CURSOR_CAPTURED : GLFW_CURSOR_NORMAL); glfwSetInputMode(window, GLFW_CURSOR, mouse_capture ? GLFW_CURSOR_HIDDEN : GLFW_CURSOR_NORMAL);
} }
if (mouse_capture) if (mouse_capture)
{ {