我有一个学习项目,代表简单的3D场景。我想在某个非原点上画球体。稍后我将把它作为单独的函数或方法来实现。
我使用gluLookAt()设置视点,然后使用带有少量偏移的glTranslatef()转换模型-视图矩阵,并绘制球面。不幸的是,球体没有显示出来。模型视图矩阵正在逼近,我说得对吗?
void display(void){
glClear(GL_COLOR_BUFFER_BIT);
glLoadIdentity();
gluLookAt(1, 0 ,1, 0, 0, 0, 0, 1, 0);
glColor3b(197, 96, 63);
glPushMatrix();
glLoadIdentity();
glTranslatef(0.1, 0, 0);
glutWireSphere(0.2, 20, 10);
glPopMatrix();
glFlush();
}
void reshape(int w, int h){
glViewport(0, 0, (GLsizei) w, (GLsizei) h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho ((float)w/(float)h, (float)-w/(float)h, -1, 1, 0.8, 100);
glMatrixMode(GL_MODELVIEW);
}发布于 2013-02-28 21:02:16
不,你不是。
gluLookAt(1, 0 ,1, 0, 0, 0, 0, 1, 0);
glColor3b(197, 96, 63);
glPushMatrix();
glLoadIdentity(); // why it should be there?通过将视图矩阵调整为零,您可以相对于原点坐标绘制对象,而不考虑glLookAt。对它的调用实际上被忽略了。它应该编码为:
因此,如果你想设置假想的“相机”,你必须将物体的位置与相机矩阵本身结合起来。
发布于 2013-02-28 21:06:58
你的方法看起来并不是那么不合理。问题出在这里:
glPushMatrix();
glLoadIdentity();
glTranslatef(0.1, 0, 0);推送(和后来的弹出)是一个好主意,但是通过在转换之前将矩阵设置为identity,您将丢失之前所做的任何转换,特别是使用gluLookAt建立的查看转换。因此,只需删除此glLoadIdentity即可正确连接各个转换。
请始终记住,所有矩阵变换函数,如glTranslate、glOrtho或gluLookat,都会修改当前选定的(使用glMatrixMode)矩阵,而不只是替换它。这也是在调用glOrtho和gluLookAt之前执行glLoadIdentity的原因。
https://stackoverflow.com/questions/15135861
复制相似问题