我刚刚开始阅读蓝皮书的初始章节,并了解投影矩阵可用于修改我们所需坐标系到真实屏幕坐标的映射。它可用于重置坐标系,并通过以下方式将其从左侧,右侧,顶部和底部的-1更改为1(作为示例)
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)并更改坐标系,执行:
glOrtho (-100.0 * aspectRatio, 100.0 * aspectRatio, -100.0, 100.0, 100.0, 1000.0);
我希望OpenGL的坐标系在左边变为-133,在右边变为133,在底部变为-100,在顶部变为100。使用这些坐标,我知道绘图将在这些坐标内完成,并且这些坐标之外的任何内容都将被剪裁。
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(-100 * aspectRatio, 100 * aspectRatio, -100, 100, 100, 1000);
glMatrixMode(GL_MODELVIEW);
glRectf(-50.0, 50.0, 200, 100);
但是,上面的命令不会在屏幕上显示任何输出。我在这里缺少什么?
答案 0 :(得分:2)
我在这里看到两个问题:
所以我建议首先使用不同的投影矩阵,如glOrtho(..., - 1.0f,1.0f);这样实际上覆盖了z = 0,然后在上面的代码中的glMatrixMode(GL_MODELVIEW)之后插入一个glLoadIdentity()调用。
另一种方法是保持glOrtho()不变,并指定一个平移矩阵,它将矩形移动到z = 100和z = 1000之间。