我需要帮助使用给定的控制点绘制Catmull-Rom样条曲线

时间:2016-10-09 00:41:17

标签: opengl catmull-rom-curve

我正在尝试创建一个openGL程序,允许用户单击窗口上的任意位置,在每次单击的精确光标坐标中创建一个点,然后通过这些点绘制曲线。我确实设法实现了这一部分,并在每个点画一条直线,但现在我必须使用catmull-rom使该线弯曲。我是这个概念的新手,我确实找到了函数glm:catmullRom()如何工作的例子,但我找不到一个关于它如何与程序的其余部分一起绘制曲线的例子。我试图使用它,但现在我得到了一条从窗口切掉的直线。我需要知道如何正确处理此设置。基本上,这是我到目前为止所做的:

double* cursorX = new double;
double* cursorY = new double;
vector<GLfloat>controlPoints = {0.0, 0.0, 0.0}; // Default value (can't pass in an empty vector to VBO)
glm::vec3 point1;
glm::vec3 point2;
glm::vec3 point3;
glm::vec3 point4;
vector<glm::vec3>interpolatedPoints;

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods)
{
    if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS)
    {
        glfwGetCursorPos(window, cursorX, cursorY);
        controlPoints.push_back(*cursorX); // X-coordinate
        controlPoints.push_back(*cursorY); // Y-coordinate
        controlPoints.push_back(0);        // Z-coordinate (always fixed at 0)
    }
}

void key_callback(GLFWwindow* window, int key, int scancode, int action, int mode)
{

    if (key == GLFW_KEY_ENTER && action == GLFW_PRESS)
    {
        // Test with 8 points, each point having X,Y, and Z, meaning 24 indices in total!
        point1 = { controlPoints[1], controlPoints[2], controlPoints[3] };
        point2 = { controlPoints[4], controlPoints[5], controlPoints[6] };
        point3 = { controlPoints[7], controlPoints[8], controlPoints[9] };
        point4 = { controlPoints[10], controlPoints[11], controlPoints[12] };
        interpolatedPoints.push_back(glm::catmullRom(point1, point2, point3, point4, 0));

        point1 = { controlPoints[13], controlPoints[14], controlPoints[15] };
        point2 = { controlPoints[16], controlPoints[17], controlPoints[18] };
        point3 = { controlPoints[19], controlPoints[20], controlPoints[21] };
        point4 = { controlPoints[22], controlPoints[23], controlPoints[24] };
        interpolatedPoints.push_back(glm::catmullRom(point1, point2, point3, point4, 0.5));
    }
}

// Initialize VAO and VBO
GLuint VAO, VBO;
glGenVertexArrays(1, &VAO);
glGenBuffers(1, &VBO);

// Bind VAO
glBindVertexArray(VAO);

// Bind and implement VBO
glBindBuffer(GL_ARRAY_BUFFER, VBO);

glBufferData(GL_ARRAY_BUFFER, controlPoints.size() * sizeof(GLfloat), &controlPoints.front(), GL_STATIC_DRAW);

// Connecting coordinates to shader
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);

    while (!glfwWindowShouldClose(window))
    {
        // Check if any events have been activated (key pressed, mouse moved etc.) and call corresponding response functions
        glfwPollEvents();

        // Render
        // Clear the colorbuffer
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        glClear(GL_COLOR_BUFFER_BIT);

        glBindVertexArray(VAO);

        // Update VBO
        glBufferData(GL_ARRAY_BUFFER, interpolatedPoints.size() * sizeof(glm::vec3), &interpolatedPoints.front(), GL_STATIC_DRAW);

        glDrawArrays(GL_LINE_STRIP, 0, interpolatedPoints.size() * sizeof(glm::vec3));

    // ...
    }
    // ...
}

1 个答案:

答案 0 :(得分:0)

std::vector索引从0开始,但您的代码从1开始索引。因此请相应地修复它。同样由于某种原因,你将一个虚拟点推入controlPoints,这意味着它实际上从3开始:

    point1 = { controlPoints[3], controlPoints[4], controlPoints[5] };
    point2 = { controlPoints[6], controlPoints[7], controlPoints[8] };
    // ...

更好的解决方案是将其更改为glm::vec3的向量并删除该虚拟元素。你这样做:

vector<glm::vec3> controlPoints;

controlPoints.push_back(glm::vec3(*cursorX, *cursorY, 0));

interpolatedPoints.push_back(glm::catmullRom(controlPoints[0], 
    controlPoints[1], controlPoints[2], controlPoints[3], 0));

glBufferData(GL_ARRAY_BUFFER, controlPoints.size() * sizeof(glm::vec3), controlPoints.data(), GL_STATIC_DRAW);

也不要在堆上分配cursorXcursorY。根本没有理由这样做:

double cursorX, cursorY;
glfwGetCursorPos(window, &cursorX, &cursorY);
controlPoints.push_back(glm::vec3(cursorX, cursorY, 0));