glm 旋转无法正常工作
Posted
技术标签:
【中文标题】glm 旋转无法正常工作【英文标题】:glm rotation not working properly 【发布时间】:2014-12-06 19:51:40 【问题描述】:我正在尝试围绕 X 和 Y 轴旋转一个立方体。我不知道为什么它对这两个不起作用,它只在 Z 轴周围起作用。不工作是指立方体也在移动,并且没有在原地旋转。
我使用的相关代码是:
void rotateZ(float angle)
_modelMatrix = glm::rotate(_modelMatrix, -angle, glm::vec3(0,0,1));
void rotateY(float angle)
_modelMatrix = glm::rotate(_modelMatrix, -angle, glm::vec3(0,1,0));
void rotateX(float angle)
_modelMatrix = glm::rotate(_modelMatrix, -angle, glm::vec3(1,0,0));
void translate_to_origin()
_modelMatrix = glm::translate(_modelMatrix, glm::vec3(-(_x + _n/2), -(_y + _n/2), -(_z + _n/2)));
void translate_to_position()
_modelMatrix = glm::translate(_modelMatrix, glm::vec3(_x + _n/2, _y + _n/2, _z + _n/2));
void translate (float x, float y, float z)
_modelMatrix = glm::translate(_modelMatrix, glm::vec3(x, y, z));
void create(float x, float y, float z, float n)
std::vector<MyVertexFormat>verts;
std::vector<glm::uvec3>indx;
verts.push_back(MyVertexFormat(x, y, z, 1,0,0));
verts.push_back(MyVertexFormat(x + n, y, z, 1,0,0));
verts.push_back(MyVertexFormat(x + n, y, z - n, 0,1,0));
verts.push_back(MyVertexFormat(x, y, z - n, 0,0,1));
verts.push_back(MyVertexFormat(x, y + n, z, 1,0,0));
verts.push_back(MyVertexFormat(x + n, y + n, z, 1,0,0));
verts.push_back(MyVertexFormat(x + n, y + n, z - n, 1,0,1));
verts.push_back(MyVertexFormat(x, y - n, z - n, 1,1,1));
indx.push_back(glm::uvec3(0,1,2));
indx.push_back(glm::uvec3(2,3,0));
indx.push_back(glm::uvec3(4,5,6));
indx.push_back(glm::uvec3(6,7,4));
indx.push_back(glm::uvec3(0,1,4));
indx.push_back(glm::uvec3(4,5,1));
indx.push_back(glm::uvec3(1,2,5));
indx.push_back(glm::uvec3(5,6,2));
indx.push_back(glm::uvec3(0,3,4));
indx.push_back(glm::uvec3(4,7,3));
indx.push_back(glm::uvec3(2,3,7));
indx.push_back(glm::uvec3(7,6,2));
//vao
glGenVertexArrays(1, &mesh_vao);
glBindVertexArray(mesh_vao);
//vbo
glGenBuffers(1, &mesh_vbo);
glBindBuffer(GL_ARRAY_BUFFER, mesh_vbo);
glBufferData(GL_ARRAY_BUFFER, sizeof(MyVertexFormat) * verts.size(), &verts[0], GL_STATIC_DRAW);
//ibo
glGenBuffers(1, &mesh_ibo);
glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, mesh_ibo);
glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * indx.size() * 3, &indx[0], GL_STATIC_DRAW);
int pipe = glGetAttribLocation(_glProgramShader, "in_position");
glEnableVertexAttribArray(pipe);
glVertexAttribPointer(pipe, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertexFormat), (void*)0);
pipe = glGetAttribLocation(_glProgramShader, "in_color");
glEnableVertexAttribArray(pipe);
glVertexAttribPointer(pipe, 3, GL_FLOAT, GL_FALSE, sizeof(MyVertexFormat), (void*)sizeof(glm::vec3));
mesh_num_indices = indx.size() * 3;
旋转我使用的立方体:
cube->translate_to_position();
cube->rotateY(angle);
cube->translate_to_origin();
【问题讨论】:
我还没有研究整个东西,但是translate_to_origin()
中使用的运算符看起来不一致。为什么你用-
表示x,而+
表示y 和z?
对不起.. 这只是一个编辑错误。我编辑了帖子
顶点坐标也有些奇怪。部分是 ` + n` ,部分是 ` - n` 。例如对于 y 坐标,它使用所有 y
、y + n
和 y - n
。我不认为你会从中得到一个立方体。
【参考方案1】:
我解决了这个问题!我没有正确构建幼崽。正确的方法是
verts.push_back(MyVertexFormat(x, y, z, 1,0,0));
verts.push_back(MyVertexFormat(x + n, y, z, 1,0,0));
verts.push_back(MyVertexFormat(x + n, y, z + n, 0,1,0));
verts.push_back(MyVertexFormat(x, y, z + n, 0,0,1));
verts.push_back(MyVertexFormat(x, y + n, z, 1,0,0));
verts.push_back(MyVertexFormat(x + n, y + n, z, 1,0,0));
verts.push_back(MyVertexFormat(x + n, y + n, z + n, 1,0,1));
verts.push_back(MyVertexFormat(x, y + n, z + n, 1,1,1));
【讨论】:
以上是关于glm 旋转无法正常工作的主要内容,如果未能解决你的问题,请参考以下文章