GLM 旋转无法正常工作
glm rotation not working properly
我正在尝试围绕 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();
我解决了问题!我没有正确构造幼崽。正确的方法是
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));
相关文章:
- QSqlquery prepare()和bindvalue()不工作
- 导入库可以跨dll版本工作吗
- 以螺旋方式打印矩阵的程序.(工作不好)
- 对象指针在c++中是如何工作的
- 为什么在Windows上的VS 2019和Clang 9中"size_t"在没有标题的情况下工作
- VSOMEIP-2个设备之间的通信(TCP/UDP)不工作
- 为字符串中每 N 个字符插入空格的函数没有按照我认为的方式工作?
- C++为线程工作动态地分割例程
- 为什么我的 std::ref 无法按预期工作?
- 布尔比较运算符是如何在C++中工作的
- SampleConsensusPrerejective(ext.RANSAC)是如何真正工作的
- 不确定要在我的main中放入什么才能使我的代码正常工作
- 为什么std::condition_variable notify_all的工作速度比notify_one快(对于随机请
- <<操作员在下面的行中工作
- 有人能解释一下为什么下界是这样工作的吗C++的
- ExtractIconEx:可以工作,但偶尔会崩溃
- C++中的memset函数工作不正常
- 为什么STD ::计数将常数传递给Lambda,而不是在弦上工作时而不是字符
- sdl软件渲染器不工作,工作在硬件加速的一个
- C++程序已停止工作-求解常微分方程