OpenGL 的坐标系如何工作?
这可能是一个非常基本的问题,但我似乎无法让 OpenGL 使用点坐标系统。相反,它似乎以百分比表示,例如 0.99 使我位于屏幕的边缘。是这样的,还是我做错了什么?谢谢!
使用的代码:
#include <iostream>
#include <stdlib.h>
#include <GLUT/GLUT.h>
void RenderScene()
{
glLoadIdentity ();
glClear(GL_COLOR_BUFFER_BIT);
glBegin( GL_POINTS );
glVertex3f( 0, 0, 0 );
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( 600, 600 );
glutCreateWindow( "OpenGL" );
glutDisplayFunc( RenderScene );
glutMainLoop();
return 0;
}
this is probably a pretty basic question but I can't seem to get OpenGL to work with a point coordinate system. Instead it seems to be in percents, for example .99 puts me right at the edge of the screen. Is this how it is, or am I doing something wrong? Thanks!
code used:
#include <iostream>
#include <stdlib.h>
#include <GLUT/GLUT.h>
void RenderScene()
{
glLoadIdentity ();
glClear(GL_COLOR_BUFFER_BIT);
glBegin( GL_POINTS );
glVertex3f( 0, 0, 0 );
glEnd();
glFlush();
}
int main(int argc, char **argv)
{
glutInit( &argc, argv );
glutInitDisplayMode( GLUT_SINGLE | GLUT_RGB );
glutInitWindowSize( 600, 600 );
glutCreateWindow( "OpenGL" );
glutDisplayFunc( RenderScene );
glutMainLoop();
return 0;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
OpenGL 按以下方式工作:启动局部坐标系(任意单位)。该坐标系通过模型-视图矩阵转换为所谓的眼睛空间坐标(之所以称为模型-视图矩阵,是因为它结合了模型和视图变换)。
然后通过投影矩阵将眼睛空间转换为剪辑空间,紧接着透视除法以获得归一化设备坐标< /strong> (
NDC{x,y,z} = Clip{x,y,z}/Clip_w
)。 NDC 空间中的范围 [-1,1]^3 映射到视口(x 和 y)和设置的深度范围 (z)。因此,如果您保留变换矩阵(模型视图和投影)标识,那么坐标范围 [-1,1] 确实会映射到视口。但是,通过选择适当的变换和投影,您可以任意从模型空间单位映射到视口单位。
OpenGL works in the following way: You start of a local coordinate system (of arbitrary units). This coordinate system is transformed to so called eye space coordinates by the model-view matrix (it is called model-view matrix, because it combines model and view transformations).
The eye space is then transformed to clip space by the projection matrix, immediately followed by the perspective divide to obtain normalized device coordinates (
NDC{x,y,z} = Clip{x,y,z}/Clip_w
). The range [-1,1]^3 in NDC space is mapped to the viewport (x and y) and the set depth range (z).So if you leave your transformation matrices (modelview and projection) identity, then indeed the coordinate ranges [-1,1] will map to the Viewport. However by choosing appropriate transformation and projection you can map from Model Space units to Viewport units arbitrarily.
它实际上并不是一个百分比:它与当前视口的大小和纵横比有关。我可以写一个完整的故事,但我建议你看看 NeHe 的关于 OpenGL 的教程,从课程开始1 关于设置窗口。非常全面,强烈推荐。
It's not really a percentage: it's related to the size and aspect ratio of your current viewport. I could write a whole story about it, but I'd suggest you take a look at NeHe's tutorials on OpenGL, starting with lesson 1 about setting up the window. Very thorough, and highly recommended.
您缺少投影矩阵概念。
本质上,投影矩阵是将顶点投影到 2D 空间上的矩阵。有两种投影矩阵:透视和正交。
当前的投影矩阵决定了有效的顶点屏幕位置。由于您没有设置投影矩阵,因此您使用的是默认矩阵,即正交矩阵,水平和垂直范围为 -1 .. +1。
要控制投影矩阵,请参阅 gluOrtho2D 函数。在那里您可以找到所需的所有信息。
you are missing the projection matrix concept.
Essentially, a projection matrix is matrix that projects a vertex on a 2D space. There are two projection matrices: perspective and orthographic.
The current projection matrix determine the effective vertex screen position. Since you do not set the projection matrix, you are using the default one, which is the orthographic, with horizontal and vertical range -1 .. +1.
To control the projection matrix, see gluOrtho2D function. There you can find all information required.