OpenGL未按预期旋转场景
OpenGL rotate scene not as expected
使用我的代码,当我旋转Modelview时;它单独旋转每个对象
我正在尝试旋转整个视图平面。我该怎么做?
我知道gluLookAt,但我正在努力理解它背后的数学原理。
这是我的代码:
Prepare_scene仅在opengl初始化和窗口重塑时调用
// prepare_scene
glViewport(0, 0, windowWidth, windowHeight);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, windowWidth, 0, windowHeight, 0, 40);
glEnable(GL_DEPTH_TEST);
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_POINT_SMOOTH_HINT, GL_NICEST);
glEnable(GL_POINT_SMOOTH);
glPointSize(4);
glClearColor(0.0f, 0.0f, 0.0f, 1);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
渲染称为每个绘图帧
// render
glClearColor(0.0f, 0.0f, 0.0f, 1); // black
//glClearColor(1.0f, 1.0f, 1.0f, 1); // white
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
//
// try to control the camera
// push_matrix (and at the end of render, add pop_matrix)
glMatrixMode(GL_MODELVIEW);
glPushMatrix();
glTranslatef(eye_x, eye_y, eye_z);
glRotatef(x_angular, 0, 1, 0);
// Go back to projection mode
glMatrixMode(GL_PROJECTION);
GLfloat mat_black[] = {0.0,0.0,0.0, 1.0};
// Transparent white-grey window
GLfloat mat_specular_w[] = {0.8,0.88,0.88, 1.0};
GLfloat mat_shine_w[] = {20.0};
glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular_w);
glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_specular_w);
glMaterialfv(GL_FRONT, GL_AMBIENT, mat_specular_w);
glMaterialfv(GL_FRONT, GL_SHININESS, mat_shine_w);
// for each object in objects:
glPushMatrix();
// object "world coordinates"
// in glTranslatef(x,y,z); // z is always 0; for this test
glTranslatef( (*obj_i)->anchor[0] , (*obj_i)->anchor[1], (*obj_i)->anchor[2]);
glutWireSphere((*obj_i)->r, 15, 15);
//glutSolidSphere((*obj_i)->r, 30, 30); // (*obj_i)->r
glPopMatrix();
glMatrixMode(GL_MODELVIEW);
glPopMatrix();
glFlush();
SwapBuffers(hdc);
停止对GL_PROJECTION
堆栈进行奇怪的相机/对象填充。
保存为GL_MODELVIEW
:
// Every frame
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
// do projection matrix stuff here (gluPerspective(), glOrtho(), etc.)
glOrtho(0, windowWidth, 0, windowHeight, 0, 40);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
// do camera transform (gluLookAt(), etc.) here
// I'm guessing this was your camera transform(s):
glTranslatef(eye_x, eye_y, eye_z);
glRotatef(x_angular, 0, 1, 0);
// foreach object:
glPushMatrix();
{
// do whatever
glTranslatef( (*obj_i)->anchor[0] , (*obj_i)->anchor[1], (*obj_i)->anchor[2]);
...
}
glPopMatrix();
相关文章:
- 发布旋转矩阵(openGL/glm)
- 如何在OpenGL中正确旋转和缩放对象?
- 使用 C++在 OpenGL 中移动自动旋转的 3D 多边形
- 按时间自动旋转形状,同时在 OpenGL 中使用C++移动它们
- 如何在 openGL 中围绕全局轴旋转模型?
- 如何在 opengl 中仅旋转特定对象而不影响其他对象?
- 在OpenGL(GLM/C++)中使用四元数旋转时出现问题
- 鼠标单击 OpenGL 时无法围绕其自己的中心旋转对象
- OpenGL:在那个方向上旋转和移动
- 在 OpenGL 中旋转一个正方形
- 如何使用gluLookAt围绕OpenGL中的点旋转相机视图
- 如何在 opengl 中旋转矢量?
- 现代 OpenGL 多维数据集旋转未按预期工作
- 在使用PI上的OpenGL ES绘制之前,如何旋转纹理
- C++ OpenGL 如何从 quaterion 获取旋转 x,y,z
- Opengl 旋转不起作用
- 绕摄影机旋转opengl c++/设置旋转中心
- 使用 GLM 围绕点旋转 OpenGL
- 第一人称相机,旋转OPENGL
- 立方体旋转- OpenGL