在 OpenGL 中设置绘图坐标系 [英] Setting the coordinate system for drawing in OpenGL

查看:44
本文介绍了在 OpenGL 中设置绘图坐标系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我刚开始阅读 Blue book 的初始章节,并了解到投影矩阵可用于修改我们所需坐标系到真实屏幕坐标的映射.可用于重置坐标系,将左、右、上、下坐标系从-1改为1,如下(举例)

I just started reading initial chapters of Blue book and got to understand that the projection matrix can be used to modify the mapping of our desired coordinate system to real screen coordinates. It can be used to reset the coordinate system and change it from -1 to 1 on left, right, top and bottom by the following (as an example)

glMatrixMode(GL_PROJECTION);
glLoadIdentity(); //With 1's in the diagonal of the identity matrix, the coordinate system is rest from -1 to 1 (and the drawing should happen then inside those coordinates which be mapped later to the screen)

另一个示例:(宽度:1024,高度:768,纵横比:1.33)并更改坐标系,请执行:

Another example: (Width: 1024, Height: 768, Aspect Ratio: 1.33) and to change the coordinate system, do:

glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 100.0, 1000.0);

我希望 OpenGL 的坐标系更改为左侧 -133、右侧 133、底部 -100 和顶部 100.使用这些坐标,我知道绘图将在这些坐标内完成,并且这些坐标之外的任何内容都将被剪裁.

I expected the coordinate system for OpenGL to change to -133 on left, 133 on right, -100 on bottom and 100 on top. Using these coordinates, I understand that the drawing will be done inside these coordinate and anything outside these coordinates will be clipped.

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
    glOrtho(-100 * aspectRatio, 100 * aspectRatio, -100, 100, 100, 1000);
glMatrixMode(GL_MODELVIEW);
    glRectf(-50.0, 50.0, 200, 100);

但是,上面的命令没有在屏幕上给我任何输出.我在这里错过了什么?

However, the above command doesn't give me any output on the screen. What am I missing here?

推荐答案

我在这里看到两个问题:

I see two problems here:

  1. 矩形应该显示,因为 glRectf() 在深度 z=0 处绘制,但是您设置了正交投影以覆盖 z 范围 [100,1000],因此对象位于近平面之前,应该被剪掉.
  2. 您没有指定您使用的 MODELVIEW 矩阵.在评论中,您提到该对象确实出现了,但没有出现在您期望的地方.这也违反了我的第一点,但如果 ModelView 矩阵不是恒等,则可以解释.
  1. The rect should not by show at all, since glRectf() draws at depth z=0, but you set up your orthorgraphic projection to cover the z range [100,1000], so the object lies before the near plane and should be clipped away.
  2. You do not specifiy waht MODELVIEW matrix you use. In the comments, you mention that the object does show up, but not in the place where you expect it. This also violates my first point, but could be explained if the ModelView matrix is not identity.

所以我建议首先使用不同的投影矩阵,比如 glOrtho(..., -1.0f, 1.0f);以便实际覆盖 z=0,然后在上述代码中的 glMatrixMode(GL_MODELVIEW) 之后插入 glLoadIdentity() 调用.

So I suggest to first use a different projection matrix with like glOrtho(..., -1.0f, 1.0f); so that z=0 is actually covered, and second insert a glLoadIdentity() call after the glMatrixMode(GL_MODELVIEW) in the above code.

另一种方法是保持 glOrtho() 不变,并指定一个平移矩阵,将矩形移动到 z=100 和 z=1000 之间的某个位置.

Another approach would be to keep the glOrtho() as it is and to specify a translation matrix wich moves the rect somewhere between z=100 and z=1000.

这篇关于在 OpenGL 中设置绘图坐标系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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