什么是OpenGL坐标?忽略OpenGL窗口大小 [英] What the OpenGL coordinates are? Ignore OpenGL window size

查看:257
本文介绍了什么是OpenGL坐标?忽略OpenGL窗口大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始尝试简单的绘制多维数据集 openGl教程。在最终胜利让OpenGL工作后,我仍然有非常veird的结果。我的问题是,对象倾向于调整自己以匹配窗口大小。相反,我想窗口大小决定渲染区域 - 窗口越大,你可以看到越多。



这里有一些截图,调整大小:

正常大小

调整大小

有意保留图片的图片!



这个自动调整大小的行为带来了一个问题,即OpenGL中使用的坐标是什么。

解决方案

首先要记住的是:OpenGL是一个图形API。它不维护一个场景或类似的东西。



因此,OpenGL做的是,它将几何输入坐标以顶点属性的形式映射到屏幕空间。在旧的固定函数中有一个称为顶点位置或者只是短的顶点的特殊顶点属性(实际的顶点不只是位置)。



位置是在三个步骤过程中转换为窗口空间:



1。转换到视图/眼图空间:这是通过将顶点位置乘以模型视图矩阵来完成的。



某些进一步的计算, p>

2。转换到剪辑空间:视图空间位置将转换为剪辑空间。这通常称为投影,适当地描述这种变换的矩阵称为投影矩阵



在剪辑空间中,一些特殊的事情发生,总结为剪辑,你没有仍然担心。



3。在最后一步将剪切的几何转换为标准化的设备坐标(NDC)。 NDC空间实际上是向视口的1:1映射,即NDC体积的极限直接对应于使用glViewport设置的视口的偏移和尺寸。



你不能改变第三步发生的方式,第一步保留用于将东西转换到视图空间。因此,任何调整都必须在第二步中进行。



因此,您必须执行以下操作:投影限制必须与视口范围成正比。像这样例如

  glViewport(0,0,width,height); 
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width / 2,width / 2,-height / 2,height / 2,-1,1);

哦,只是一般的注意事项:你应该总是设置视图和投影设置在绘图功能。如果你看到一个教程,把这些语句放在窗口调整大小处理程序,只是忽略它,只是在绘图代码无论如何。从长远来看,这真的简化了事情。


I just started trying to folow simple "draw cube" openGl tutorial. After final victory over getting OpenGL to work, I still have very veird results. My problem is that the objects tend to resize themselves to match the window size. Instead, I'd like the window size determine the rendering area - the larger the window is, the more you may see.

Here are some screenshots of the resizing:
Normal size
Resized
Images kept as links intentionally!

This auto-resizing behavior brings a question what the coordinates used in OpenGL are.

解决方案

First thing to keep in mind: OpenGL is a drawing API. It doesn't maintain a scene or something like that.

So what OpenGL does is, it maps geometry input coordinates in the form of vertex attributes to screen space. In the old fixed function there's a special vertex attribute called "vertex position" or just short "vertex" (the actual vertex is more than just position).

The position is transformed to window space in a three step process:

1. Transformation into view/eye space: This is done by multiplying the vertex position with the modelview matrix.

Certain further calculations, like illumination calculation are done in view space.

2. Transformation to clip space: The view space position is transformed into clip space. This is usually called the projection and aptly the matrix describing this transformation is called the projection matrix

In clip space some special things happen, summarized as clipping, you don't have to worry about yet.

3. In the final step transformed the clipped geometry into normalized device coordinates (NDC). NDC space is practically a 1:1 mapping toward the viewport, i.e. the limits of the NDC volume directly correspond to the offset and dimension of the viewport set with glViewport.

You can't change the way the 3rd step happens, and the 1st step is reserved for transforming stuff into view space. So any adjustments must happen in the 2nd step.

So here's what you have to do: The projection limits must be directly proportional to the viewport extents. Like this for example

glViewport(0, 0, width, height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-width/2, width/2, -height/2, height/2, -1, 1);

Oh, and just on a general note: You should always set the viewport and projection setup in the drawing function, too. If you see a tutorial that puts those statements in a window resize handler, just disregard it and just do it in the drawing code anyway. On the long term this really simplifies things.

这篇关于什么是OpenGL坐标?忽略OpenGL窗口大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆