问题描述
我想用OpenGL C ++写一个小项目,我想用像素代替浮点值,例如:
I want to write a small project with OpenGL C++ and I want to use pixel instead of float value, for example like that:
glVertext2f(420, 300);
代替:
glVertex2f(0.4, -0.34);
有可能吗?
推荐答案
如果要使用像素坐标进行渲染,使用正交矩阵非常容易,可以使用 glOrtho
.假设您的窗口是800x600,则可以使用以下代码:
If you want to use pixel coordinates your rendering, it's pretty easy to do so using an orthographic matrix, which you can create using glOrtho
. Assuming your window is 800x600 you could use the following code:
// Set your projection matrix
glMatrixMode(GL_PROJECTION);
glOrtho(0, 800, 0, 600, -1, 1);
// Restore the default matrix mode
glMatrixMode(GL_MODELVIEW);
glOrtho
期望参数为左,右,下,上",因此实际上会将原点放在左下角(大多数OpenGL坐标系随着向上移动而增加Y).但是,您希望将原点放在左上角,这与大多数基于像素的绘图系统一样,您需要反转bottom和top参数.
glOrtho
expects the parameters to be 'left, right, bottom, top' so this will actually put the origin at the lower left (most OpenGL coordinate systems have Y increase as you move up). However, you want to have the origin in the upper left, as is common with most pixel based drawing systems, you'd want to reverse the bottom and top parameters.
这将使您使用像素坐标而不是默认剪辑坐标来调用glVertex2f.请注意,您不必调用特殊函数即可将int转换为float. C和C ++都应进行隐式转换.即glVertext2f(420, 300);
This will let you call glVertex2f with pixel coordinates instead of the default clip coordinates. Note that you don't have to call a special function to convert from an int to a float. C and C++ should both do an implicit conversion. i.e. glVertext2f(420, 300);
这篇关于如何在OpenGL中以像素为单位指定顶点?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!