在找到这段代码,对理解opengl旋转很有帮助
...
glPushMatrix();
// initialze ModelView matrix
glLoadIdentity();
// First, transform the camera (viewing matrix) from world space to eye space
// Notice all values are negated, because we move the whole scene with the
// inverse of camera transform
//要理解这段代码:opengl 没有提供操作相机的函数,移动相机就是反向移动模型坐标
//可以理解为:把物体移到视野中去
glRotatef(-cameraAngle[2], 0, 0, 1); // roll
glRotatef(-cameraAngle[1], 0, 1, 0); // heading
glRotatef(-cameraAngle[0], 1, 0, 0); // pitch
glTranslatef(-cameraPosition[0], -cameraPosition[1], -cameraPosition[2]);
// draw the grid at origin before model transform
// 注意:表示
drawGrid();
// transform the object (model matrix)
// The result of GL_MODELVIEW matrix will be:
// ModelView_M = View_M * Model_M
glTranslatef(modelPosition[0], modelPosition[1], modelPosition[2]);
glRotatef(modelAngle[0], 1, 0, 0);
glRotatef(modelAngle[1], 0, 1, 0);
glRotatef(modelAngle[2], 0, 0, 1);
// draw a teapot with model and view transform together
drawTeapot();
glPopMatrix();
...
比较:
http://www.songho.ca/opengl/gl_transform.html#overview
// transform the object (model matrix)
// The result of GL_MODELVIEW matrix will be:
// ModelView_M = View_M * Model_M
glTranslatef(modelPosition[0], modelPosition[1], modelPosition[2]);
glRotatef(modelAngle[0], 1, 0, 0);
glRotatef(modelAngle[1], 0, 1, 0);
glRotatef(modelAngle[2], 0, 0, 1);
//放在这,区别:网格随物体移动(当改变modelPosition时)drawGrid();// draw a teapot with model and view transform togetherdrawTeapot();
http://www.songho.ca/opengl/gl_transform.html#overview
本文深入探讨OpenGL中实现相机移动和物体旋转的代码逻辑,通过实例解释如何使用旋转矩阵和变换函数,如glRotatef和glTranslatef,来动态调整视角和物体位置,提升三维图形渲染体验。

被折叠的 条评论
为什么被折叠?



