These two days I have been programming OpenGL for a long time and have forgotten a lot. Now I have come back and summarized my experiences:
First, some beginners may be confused about the viewpoint:
In Windows, the coordinate origin is in the upper right corner, while in OpenGL, the coordinate origin is in the lower right corner.
For rendering a 2D image, the viewpoint settings should be as follows:
[Cpp]
View plaincopy
- GlMatrixMode (GL_PROJECTION );
- GlLoadIdentity ();
- GlOrtho (0, (IPHONE_SCREEN_WIDTH), 0, SCREEN_HEIGHT,-I2F (100), I2F (1000); // here glOrtho (left, right, bottom, top, near, FAR)
- // This is based on openGL coordinates
- GlMatrixMode (GL_MODELVIEW );
- GlLoadIdentity ();
- GlViewport (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // glViewport (x, y, width, height)
- GlScissor (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // glScissor (x, y, width, height)
Then, when you draw an object, for example, draw a square on the screen. If you want to draw it in the upper left corner, you need to perform this conversion.
[Cpp]
View plaincopy
- Void drawRect (x, y, width, height)
- {
- Y = SCREEN_HEIGHT-y-h;
- // Draw a rectangle using GL
- }