I am trying to make an overlay for one game. I need to make it so that I can use normal coordinates to draw objects instead of -1 and 1. That is, the left must start at zero, and the right must equal the window width. So it is with height. The top should start at zero and the bottom should be equal to the height of the window. But for some reason I don't quite succeed.
I get the size of the viewport using the
GLfloat viewport [4];
glGetFloatv (GL_VIEWPORT, viewport);
There are four values in the viewport array
viewport[0] = 0
viewport[1] = 0
viewport[2] = 1366
viewport[3] = 715
Then I do the following
glOrtho(viewport[0], viewport[2], viewport[3], viewport[1], -1, 1);
And draw a rectangle
glRectf(20, 20, 68, 68);
In theory, it should draw a rectangle in the upper left corner with an indent of 20 pixels, but it is not visible at all. But if you swap viewport[3]
and viewport[1]
, then the rectangle appears, though not in the right place. In the lower left corner.
How to make the rectangle draw in the upper left corner when using coordinates x = 20
and y = 20
?
Full code
BOOL WINAPI __SwapBuffers(HDC hDC)
{
GLfloat viewport[4];
glGetFloatv(GL_VIEWPORT, viewport);
glPushAttrib(GL_ALL_ATTRIB_BITS);
glDisable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, 0);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(viewport[0], viewport[2], viewport[3], viewport[2], -1, 1);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
glDisable(GL_DEPTH_TEST);
glRectf(20, 20, 68, 68);
glPopAttrib();
return _SwapBuffers(hDC);
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…