我正在尝试创建一个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));
// ...
}
// ...
}
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);
也不要在堆上分配cursorX
和cursorY
。根本没有理由这样做:
double cursorX, cursorY;
glfwGetCursorPos(window, &cursorX, &cursorY);
controlPoints.push_back(glm::vec3(cursorX, cursorY, 0));