如何在Opengl中使对象沿着用户定义的线移动
How to make an object move along a user-defined line in Opengl
我的程序允许用户单击窗口上的任何位置,当按Enter键时,它沿着这些点跟踪线。另外,当按空格键时,它会在第一个点处画一个三角形,我希望它沿着整条线移动到终点,然后回到起点,然后重复。
我得到了线条跟踪和在起点上绘制三角形的工作,我甚至得到了平移运动的工作。我唯一的问题是,不管线是怎样的,它总是从右下角移动。一定是我找不到方向的问题,但我不知道哪里出了问题。
首先,我是这样画三角形的。用户定义的点定义在一个名为controlPoints的glm::vec3变量中。所以我在第一个点上创建三角形(然后它被传递到VBO,绑定到VAO,等等[triangleVertices也是一个glm::vec3变量]):
// Lower-left vertex
triangleVertices.push_back(glm::vec3(controlPoints[0].x - 30.0f, controlPoints[0].y + 30.0f, controlPoints[0].z));
// Lower-right vertex
triangleVertices.push_back(glm::vec3(controlPoints[0].x + 30.0f, controlPoints[0].y + 30.0f, controlPoints[0].z));
// Upper-center vertex
triangleVertices.push_back(glm::vec3(controlPoints[0].x, controlPoints[0].y, controlPoints[0].z));
// Bind VAO for the triangle
glBindVertexArray(VAOTriangle);
// Bind and implement VBO for the triangle
glBindBuffer(GL_ARRAY_BUFFER, VBOTriangle);
glBufferData(GL_ARRAY_BUFFER, triangleVertices.size() * sizeof(glm::vec3), triangleVertices.data(), GL_STATIC_DRAW);
// Connecting coordinates to shader
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3 * sizeof(GLfloat), (GLvoid*)0);
glEnableVertexAttribArray(0);
// Unbind VAO
glBindVertexArray(0);
然后在我的游戏循环中,我是这样做转换的(模型矩阵已经被乘以到着色器中的位置,对象已经被绑定):
glBindVertexArray(VAOTriangle);
float inputX = 0.0;
float inputY = 0.0;
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
GLfloat speed = 5.0f * deltaTime;
glm::vec2 direction;
for (int i = 0; i < controlPoints.size(); i++)
{
inputX = controlPoints[i].x;
inputY = controlPoints[i].y;
direction.x = inputX * float((speed * deltaTime) / 1000.0f);
direction.y = inputY * float((speed * deltaTime) / 1000.0f);
model_matrix = glm::translate(model_matrix, glm::vec3(direction, 0.0f));
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(model_matrix));
}
glDrawArrays(GL_TRIANGLES, 0, triangleVertices.size());
glBindVertexArray(0);
如果我在for循环中放入glDrawArrays,它会创建与点相同数量的三角形,这不是我想要的(只有一个三角形)。但是像这样,它看起来只取最后一点的方向,因为它在整个循环结束后才画。但它也不会这样做。就像我说的,不管行结构如何,它都在右下角(直到它从窗口消失)。
编辑:这是我关于如何沿着线转换对象的新设置。无论我做什么,它仍然会偏离直线,向右下角移动。
glBindVertexArray(VAOTriangle);
// Getting time to define translation speed
GLfloat deltaTime = 0.0f;
GLfloat lastFrame = 0.0f;
GLfloat currentFrame = glfwGetTime();
deltaTime = currentFrame - lastFrame;
lastFrame = currentFrame;
GLfloat speed = 3.0f * deltaTime;
// Going through all points of the line
translationX = controlPoints[translationIndex].x;
translationY = controlPoints[translationIndex].y;
if (translationIndex < controlPoints.size() - 1)
{
translationIndex++;
translationIndex %= controlPoints.size();
}
// Update direction vector
translationDirection.x = translationX * float((speed * deltaTime) / 1000.0f);
translationDirection.y = translationY * float((speed * deltaTime) / 1000.0f);
model_matrix = glm::translate(model_matrix, glm::vec3(translationDirection, 0.0f));
glUniformMatrix4fv(transformLoc, 1, GL_FALSE, glm::value_ptr(model_matrix));
glDrawArrays(GL_TRIANGLES, 0, triangleVertices.size());
glBindVertexArray(0);
你一次遍历整个控制点数组,只有最后一次调用glUniformMatrix4fv才重要。
你应该尝试做的是删除这个for循环并增加一些值(每帧或特定的步骤),这将取代你的"i"变量
相关文章:
- 创建一个函数以在输入为负数或零时输出字符串.第一次执行用户定义的函数
- 使用用户定义函数的字符串反转
- 用户定义函数中的指针和输入
- Visual C++(VS2017)中用户定义的转换不明确
- 使用用户定义的参数调用future/async并调用类方法
- 带有用户定义类的c++折叠表达式
- g++用户定义的动态链接库上的全局new和delete运算符
- 直接在 unordered_map 的方法中使用哈希,而不是生成哈希的用户定义对象
- 修改"std::set"中用户定义类型的值
- 参数包构造函数在类模板中隐藏用户定义的转换
- MAKE:找不到包含的用户定义的头文件?
- C++:用户定义的显式类型转换函数错误
- 从用户定义的类生成格式字符串?
- 为用户定义的类正确调用复制构造函数/赋值运算符
- C++ 向量与用户定义的类比较?(==, <, >)
- STL 用户定义的二进制操作
- 将用户定义的类型与 std::vector 和 std::sort 一起使用
- 为什么用户定义的函数不按照给定的顺序对相同长度的元素进行排序?
- 使用宏编译时使用用户定义的数学函数,或者仅使用 c++ 中标准数学库中的函数
- C++:用户定义的类,以成员字段作为地址