From b8323d3bc0951a058922405dd7ac7c171d3363d5 Mon Sep 17 00:00:00 2001 From: Saikari Date: Fri, 20 Feb 2026 02:56:27 +0300 Subject: [PATCH] simplify complexity --- examples/example_barycentric.cpp | 128 ++++++++++++++++++------------- 1 file changed, 75 insertions(+), 53 deletions(-) diff --git a/examples/example_barycentric.cpp b/examples/example_barycentric.cpp index 4df4995..f4fb6ab 100644 --- a/examples/example_barycentric.cpp +++ b/examples/example_barycentric.cpp @@ -98,21 +98,21 @@ void drawText(const std::string& text, float x, float y, float scale, const Vect } } -int main() { +GLFWwindow* initWindow(int width, int height, const char* title) { if (!glfwInit()) { std::cerr << "Failed to initialize GLFW\n"; - return -1; + return nullptr; } glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3); glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3); glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE); - GLFWwindow* window = glfwCreateWindow(800, 800, "Barycentric Coordinates", NULL, NULL); + GLFWwindow* window = glfwCreateWindow(width, height, title, NULL, NULL); if (!window) { std::cerr << "Failed to create GLFW window\n"; glfwTerminate(); - return -1; + return nullptr; } glfwMakeContextCurrent(window); @@ -135,25 +135,23 @@ int main() { } else { std::cerr << "Failed to initialize GLEW: " << glewGetErrorString(glewErr) << "\n"; glfwTerminate(); - return -1; + return nullptr; } } + return window; +} +GLuint createShaderProgram() { GLuint vs = compileShader(GL_VERTEX_SHADER, vertexShaderSource); GLuint fs = compileShader(GL_FRAGMENT_SHADER, fragmentShaderSource); GLuint shaderProgram = glCreateProgram(); glAttachShader(shaderProgram, vs); glAttachShader(shaderProgram, fs); glLinkProgram(shaderProgram); + return shaderProgram; +} - // Triangle vertices as shown in the picture (Red, Green, Blue) - // Scaled down slightly to leave room for text - Vector3 A(0.0f, 0.6f, 0.0f); // Red dot (top) - Vector3 B(-0.6f, -0.6f, 0.0f); // Green dot (bottom left) - Vector3 C(0.6f, -0.6f, 0.0f); // Blue dot (bottom right) - - std::vector pointCloud; - +void generatePointCloud(std::vector& pointCloud, const Vector3& A, const Vector3& B, const Vector3& C) { // 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 v = 0.0f; v <= 1.0f - u; v += 0.015f) { @@ -167,8 +165,9 @@ int main() { } } } +} - GLuint VAO_cloud, VBO_cloud; +void setupBuffers(GLuint& VAO_cloud, GLuint& VBO_cloud, const std::vector& pointCloud, GLuint& VAO_dyn, GLuint& VBO_dyn) { glGenVertexArrays(1, &VAO_cloud); glGenBuffers(1, &VBO_cloud); glBindVertexArray(VAO_cloud); @@ -183,7 +182,6 @@ int main() { glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(7 * sizeof(float))); glEnableVertexAttribArray(3); - GLuint VAO_dyn, VBO_dyn; glGenVertexArrays(1, &VAO_dyn); glGenBuffers(1, &VBO_dyn); glBindVertexArray(VAO_dyn); @@ -197,6 +195,66 @@ int main() { glEnableVertexAttribArray(2); glVertexAttribPointer(3, 1, GL_FLOAT, GL_FALSE, 8 * sizeof(float), (void*)(7 * sizeof(float))); glEnableVertexAttribArray(3); +} + +void updateDynamicData(std::vector& dynData, float u, float v, float w, const Vector3& P, const Vector3& A, const Vector3& B, const Vector3& C) { + float sizeA = 10.0f + u * 30.0f; + float sizeB = 10.0f + v * 30.0f; + float sizeC = 10.0f + w * 30.0f; + float sizeP = 12.0f; + + dynData = { + // 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, + 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, + C.x, C.y, C.z, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, + + // The animated dot itself (White) + P.x, P.y, P.z, 1.0f, 1.0f, 1.0f, sizeP, 0.0f, + + // The 3 corner dots + 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, C.z, 0.0f, 0.0f, 1.0f, sizeC, 0.0f + }; + + char bufA[16], bufB[16], bufC[16]; + snprintf(bufA, sizeof(bufA), "%.2f", u); + snprintf(bufB, sizeof(bufB), "%.2f", v); + snprintf(bufC, sizeof(bufC), "%.2f", w); + + // Keep text at a fixed distance from the dots + float distA = 0.13f; + float distB = 0.13f; + float distC = 0.13f; + + drawText(bufA, A.x - 0.05f, A.y + distA, 0.1f, Vector3(1,0,0), dynData); + drawText(bufB, B.x - 0.15f - distB, B.y - 0.05f - distB, 0.1f, Vector3(0,1,0), dynData); + drawText(bufC, C.x + 0.05f + distC, C.y - 0.05f - distC, 0.1f, Vector3(0,0,1), dynData); +} + +int main() { + GLFWwindow* window = initWindow(800, 800, "Barycentric Coordinates"); + if (!window) return -1; + + GLuint shaderProgram = createShaderProgram(); + + // Triangle vertices as shown in the picture (Red, Green, Blue) + // Scaled down slightly to leave room for text + Vector3 A(0.0f, 0.6f, 0.0f); // Red dot (top) + Vector3 B(-0.6f, -0.6f, 0.0f); // Green dot (bottom left) + Vector3 C(0.6f, -0.6f, 0.0f); // Blue dot (bottom right) + + std::vector pointCloud; + generatePointCloud(pointCloud, A, B, C); + + GLuint VAO_cloud, VBO_cloud, VAO_dyn, VBO_dyn; + setupBuffers(VAO_cloud, VBO_cloud, pointCloud, VAO_dyn, VBO_dyn); glEnable(GL_PROGRAM_POINT_SIZE); glEnable(GL_BLEND); @@ -237,44 +295,8 @@ int main() { Vector3 P = A * u + B * v + C * w; - float sizeA = 10.0f + u * 30.0f; - float sizeB = 10.0f + v * 30.0f; - float sizeC = 10.0f + w * 30.0f; - float sizeP = 12.0f; - - std::vector dynData = { - // 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, - 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, - C.x, C.y, C.z, 0.0f, 0.0f, 1.0f, 1.0f, 1.0f, - - // The animated dot itself (White) - P.x, P.y, P.z, 1.0f, 1.0f, 1.0f, sizeP, 0.0f, - - // The 3 corner dots - 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, C.z, 0.0f, 0.0f, 1.0f, sizeC, 0.0f - }; - - char bufA[16], bufB[16], bufC[16]; - snprintf(bufA, sizeof(bufA), "%.2f", u); - snprintf(bufB, sizeof(bufB), "%.2f", v); - snprintf(bufC, sizeof(bufC), "%.2f", w); - - // Keep text at a fixed distance from the dots - float distA = 0.13f; - float distB = 0.13f; - float distC = 0.13f; - - drawText(bufA, A.x - 0.05f, A.y + distA, 0.1f, Vector3(1,0,0), dynData); - drawText(bufB, B.x - 0.15f - distB, B.y - 0.05f - distB, 0.1f, Vector3(0,1,0), dynData); - drawText(bufC, C.x + 0.05f + distC, C.y - 0.05f - distC, 0.1f, Vector3(0,0,1), dynData); + std::vector dynData; + updateDynamicData(dynData, u, v, w, P, A, B, C); glBindVertexArray(VAO_dyn); glBindBuffer(GL_ARRAY_BUFFER, VBO_dyn);