Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

c++ - How to make the height coordinates start from the top?

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);
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

See OpenGL 2.1 API Specification - 2.10 Rectangles:

Rect (x1, y1, x2, y2);

is exactly the same as the following sequence of commands:

Begin(POLYGON);
Vertex2(x1, y1);
Vertex2(x2, y1);
Vertex2(x2, y2);
Vertex2(x1, y2);
End();

Therefore, the geometry can be discarded when Face Culling is enabled. Disable face culling (glDisable(GL_CULL_FACE )) or set the correct winding order.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...