0
我想繞X軸和Y軸旋轉一個立方體。我不知道爲什麼它不適用於這兩個,它只能繞Z軸工作。由於不工作,我的意思是立方體也在移動,它不在原地旋轉。glm旋轉不能正常工作
我使用的相關代碼:
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? – 2014-12-06 19:59:13
對不起。這只是一個編輯錯誤。我編輯了帖子 – 2014-12-06 20:02:48
頂點座標也有一些奇怪的地方。部分是'+ n',部分是' - n'。例如,對於y座標,它使用所有'y','y + n'和'y - n'。我不認爲你會從中得到一個立方體。 – 2014-12-06 20:10:05