如何只使用一次 gluPerspective?
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//set viewpoint
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(VIEW_ANGLE,Screen_Ratio,NEAR_CLIP,FAR_CLIP);
gluLookAt(0,5,5, 0,0,0, 0,1,0);
//transform model 1
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(Theta, 0,1,0);
//draw model 1
glBegin(GL_QUADS);
...
glEnd();
上面的代码工作正常,但是有什么方法可以删除对 gluPerspective 的调用吗?
我的意思是,我只想在初始化时调用它一次,而不是在每次渲染期间重复调用它。
glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
//set viewpoint
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(VIEW_ANGLE,Screen_Ratio,NEAR_CLIP,FAR_CLIP);
gluLookAt(0,5,5, 0,0,0, 0,1,0);
//transform model 1
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glRotatef(Theta, 0,1,0);
//draw model 1
glBegin(GL_QUADS);
...
glEnd();
The code above works fine, but is there any way to remove the call to gluPerspective?
What I mean is, I would like to call it only once in initialization, instead of repeatedly during each rendering.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你在那里调用 gluPerspective,因为它属于那里。 OpenGL 不是一个可以用来初始化事物的场景图。它是一个状态驱动的绘图 API。投影矩阵是一种状态,每个严肃的图形应用程序都会在单个帧渲染过程中多次更改此状态。
OpenGL 不知道几何对象、位置和摄像机。它只是将点、线和三角形推入处理管道,并将结果绘制到屏幕上。绘制某些内容后,OpenGL 无论如何都不会记得它。
OpenGL 未初始化(除了创建渲染上下文,但实际上这是操作系统图形堆栈的一部分,而不是 OpenGL)。当然,您可以向其中上传纹理和缓冲对象数据,但这可能随时发生。
You call gluPerspective there, because it belongs there. OpenGL is not a scene graph where you initialize things. It's a state driven drawing API. The projection matrix is a state and every serious graphics application changes this state multiple times throughout a single frame rendering.
OpenGL does not know geometrical objects, positions and cameras. It just pushes points, lines and triangles through a processing pipeline, and draws the result to the screen. After something has been drawn, OpenGL has no recollection of it, whatsoever.
OpenGL is not initialized (except creation of the rendering context, but actually this is part of the operating system's graphics stack, not OpenGL). Sure, you upload textures and buffer object data to it, but that can happen anytime.
不要不要在投影矩阵上使用
gluLookAt
,因为它定义了相机/视图,因此属于模型视图矩阵,通常作为最左边的变换(< code>glLoadIdentity),它构成了单词 modelview 的视图部分。尽管它也可以按照您的方式工作,但它在概念上是错误的。这也可以解决您的问题,因为这样您就不必每帧都触摸投影矩阵。但实际上,关于 OpenGL 的状态机架构,datenwolf 的方法在概念上更加清晰。
Do not use
gluLookAt
on the projection matrix, as it defines the camera/view and therefore belongs to the modelview matrix, usually as the left-most transformation (the first afterglLoadIdentity
), where it makes up the view part of the word modelview. Although it also works your way, it's conceptually wrong. This would also solve your issue, as then you just don't have to touch the projection matrix every frame.But actually datenwolf's approach is more conceptually clean regarding OpenGL's state machine architecture.
如果您不调用
glLoadIdentity()
< /a> (将当前矩阵重置为单位矩阵,即撤消 gluPerspective() 所做的事情)每一帧,而是小心地 push/pop 变换矩阵,您只需在初始化很愉快。通常,每次开始绘图时调用加载标识然后重置它会容易得多。例如:然后稍后:
If you don't call
glLoadIdentity()
(which resets the current matrix to be the identity matrix, i.e. undoes whatgluPerspective()
has done) every frame and instead carefully push/pop the transform matrices you can get away with calling it only in initialization quite happily. Usually it's far easier just to call load identity each time your start drawing and then reset it. e.g.:Then later on: