我正在绘制一个由3个多维数据集组成的金字塔(左右两个,然后一个在顶部),然后尝试在X,Y或Z轴上旋转此金字塔并上下缩放。
问题是三个立方体无法正确缩放(不同大小),旋转也会产生非常奇怪的结果。
我已经使用glm vec3数组为多维数据集定义了一个数组:
const glm::vec3 cube[] = {
glm::vec3(-1.0f,-1.0f,-1.0f),
glm::vec3( -1.0f,-1.0f, 1.0f),
glm::vec3(-1.0f, 1.0f, 1.0f),
glm::vec3(1.0f, 1.0f,-1.0f),
glm::vec3(-1.0f,-1.0f,-1.0f),
glm::vec3(-1.0f, 1.0f,-1.0f),
glm::vec3(1.0f,-1.0f, 1.0f),
glm::vec3(-1.0f,-1.0f,-1.0f),
glm::vec3(1.0f,-1.0f,-1.0f),
glm::vec3(1.0f, 1.0f,-1.0f),
glm::vec3(1.0f,-1.0f,-1.0f),
glm::vec3(-1.0f,-1.0f,-1.0f),
glm::vec3(-1.0f,-1.0f,-1.0f),
glm::vec3(-1.0f, 1.0f, 1.0f),
glm::vec3(-1.0f, 1.0f,-1.0f),
glm::vec3(1.0f,-1.0f, 1.0f),
glm::vec3(-1.0f,-1.0f, 1.0f),
glm::vec3(-1.0f,-1.0f,-1.0f),
glm::vec3(-1.0f, 1.0f, 1.0f),
glm::vec3(-1.0f,-1.0f, 1.0f),
glm::vec3(1.0f,-1.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
glm::vec3(1.0f,-1.0f,-1.0f),
glm::vec3(1.0f, 1.0f,-1.0f),
glm::vec3(1.0f,-1.0f,-1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
glm::vec3(1.0f,-1.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
glm::vec3(1.0f, 1.0f,-1.0f),
glm::vec3(-1.0f, 1.0f,-1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
glm::vec3(-1.0f, 1.0f,-1.0f),
glm::vec3(-1.0f, 1.0f, 1.0f),
glm::vec3(1.0f, 1.0f, 1.0f),
glm::vec3(-1.0f, 1.0f, 1.0f),
glm::vec3(1.0f,-1.0f, 1.0f)
};
我生成并链接缓冲区和必要的东西以使OpenGL正常工作,然后绘制由三个立方体组成的金字塔。我几乎可以肯定这是所有出错的代码部分:
glm::mat4 P = glm::perspective(glm::radians(60.0f + zoom), float(width()) / height(), 0.01f, 1000.0f);
glm::mat4 V = glm::mat4(1);
V = glm::translate(V, glm::vec3(-3 + moveX, -1 + moveY, -6 + moveZ));
V = glm::rotate(V, glm::radians(rotateCamradX), glm::vec3(1.0, 0.0, 0.0));
V = glm::rotate(V, glm::radians(rotateCamradY), glm::vec3(0.0, 1.0, 0.0));
V = glm::rotate(V, glm::radians(rotateCamradZ), glm::vec3(0.0, 0.0, 1.0));
glm::mat4 M = glm::mat4(1);
M = glm::translate(M, glm::vec3(-1, 0, 0));
M = glm::rotate(M, glm::radians(rotateradX), glm::vec3(1.0, 0.0, 0.0));
M = glm::rotate(M, glm::radians(rotateradY), glm::vec3(0.0, 1.0, 0.0));
M = glm::rotate(M, glm::radians(rotateradZ), glm::vec3(0.0, 0.0, 1.0));
M = glm::scale(M, glm::vec3(scaleVar, scaleVar, scaleVar));
glm::mat4 M1 = glm::mat4(1);
M1 = glm::translate(M, glm::vec3(2, 0, 0));
M1 = glm::rotate(M1, glm::radians(rotateradX), glm::vec3(1.0, 0.0, 0.0));
M1 = glm::rotate(M1, glm::radians(rotateradY), glm::vec3(0.0, 1.0, 0.0));
M1 = glm::rotate(M1, glm::radians(rotateradZ), glm::vec3(0.0, 0.0, 1.0));
M1 = glm::scale(M1, glm::vec3(scaleVar, scaleVar, scaleVar));
glm::mat4 M2 = glm::mat4(1);
M2 = glm::translate(M, glm::vec3(1, 2, 0));
M2 = glm::rotate(M2, glm::radians(rotateradX), glm::vec3(1.0, 0.0, 0.0));
M2 = glm::rotate(M2, glm::radians(rotateradY), glm::vec3(0.0, 1.0, 0.0));
M2 = glm::rotate(M2, glm::radians(rotateradZ), glm::vec3(0.0, 0.0, 1.0));
M2 = glm::scale(M2, glm::vec3(scaleVar, scaleVar, scaleVar));
glm::mat4 PVM = P * V * M;
glm::mat4 PVM1 = P * V * M1;
glm::mat4 PVM2 = P * V * M2;
gl->glUniformMatrix4fv(id_shader_program_PVM_uniform, 1, GL_FALSE, glm::value_ptr(PVM));
gl->glUniform3f(id_shader_program_Color_uniform, 0.3f ,0.1f, 0.2f);
gl->glDrawArrays(GL_TRIANGLES, 0, 3*12);
gl->glUniformMatrix4fv(id_shader_program_PVM_uniform, 1, GL_FALSE, glm::value_ptr(PVM1));
gl->glUniform3f(id_shader_program_Color_uniform, 0.8f ,0.2f, 0.7f);
gl->glDrawArrays(GL_TRIANGLES, 0, 3*12);
gl->glUniformMatrix4fv(id_shader_program_PVM_uniform, 1, GL_FALSE, glm::value_ptr(PVM2));
gl->glUniform3f(id_shader_program_Color_uniform, 0.1f ,0.2f, 0.1f);
gl->glDrawArrays(GL_TRIANGLES, 0, 3*12);
V矩阵中的变量用于摄像机移动,该摄像机起作用,然后在M,M1和M2中为缩放变量和旋转变量。所有变量都是float类型,可以使用UI应用程序中的按钮进行更改。
答案 0 :(得分:0)
尝试翻转转换顺序。代替[translate,rotate,scale]进行[scale,rotate,translate]。
请注意,通常使用glm::lookAt
来计算您的V矩阵要容易得多,而要使用多个步骤。