我正在尝试围绕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();
答案 0 :(得分:0)
我解决了这个问题!我没有正确地构建幼仔。正确的方法是
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));