如何在OpenGL中以像素为单位指定顶点?

时间:2013-12-30 01:23:29

标签: opengl deprecated coordinate-systems opengl-1.x

我想用OpenGL C ++编写一个小项目,我想使用像素而不是浮点值,例如:

glVertext2f(420, 300);

而不是:

glVertex2f(0.4, -0.34);

有可能吗?

1 个答案:

答案 0 :(得分:3)

如果你想使用像素坐标你的渲染,使用正交矩阵很容易做到这一点,你可以使用glOrtho创建。假设您的窗口是800x600,您可以使用以下代码:

// Set your projection matrix
glMatrixMode(GL_PROJECTION);
glOrtho(0, 800, 0, 600, -1, 1);
// Restore the default matrix mode
glMatrixMode(GL_MODELVIEW);

glOrtho期望参数为“left,right,bottom,top”,因此这实际上将原点放在左下方(大多数OpenGL坐标系在向上移动时Y增加)。但是,您希望原点位于左上角,这与大多数基于像素的绘图系统一样,您需要反转底部和顶部参数。

这将允许您使用像素坐标而不是默认剪辑坐标来调用glVertex2f。请注意,您不必调用特殊函数将int转换为float。 C和C ++都应该进行隐式转换。即glVertext2f(420, 300);