如何只使用一次 gluPerspective?

发布于 2024-12-02 02:24:44 字数 467 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

挽手叙旧 2024-12-09 02:24:44

你在那里调用 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.

I mean calling it only once in initialization.

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.

眼眸 2024-12-09 02:24:44

不要不要在投影矩阵上使用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 after glLoadIdentity), 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.

不及他 2024-12-09 02:24:44

如果您不调用 glLoadIdentity()< /a> (将当前矩阵重置为单位矩阵,即撤消 gluPerspective() 所做的事情)每一帧,而是小心地 push/pop 变换矩阵,您只需在初始化很愉快。通常,每次开始绘图时调用加载标识然后重置它会容易得多。例如:

// Initalisation
glLoadIdentity();
gluPerspective(...);

然后稍后:

// Drawing each frame
glClear(...);

glPushMatrix();
gluLookAt(...);

//draw stuff

glPopMatrix();

If you don't call glLoadIdentity() (which resets the current matrix to be the identity matrix, i.e. undoes what gluPerspective() 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.:

// Initalisation
glLoadIdentity();
gluPerspective(...);

Then later on:

// Drawing each frame
glClear(...);

glPushMatrix();
gluLookAt(...);

//draw stuff

glPopMatrix();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文