我正在尝试创建一个 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));
// ...
}
// ...
}