Details about OpenGL glViewport, glOrtho, and glScissor in windows

Source: Internet
Author: User

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
  1. GlMatrixMode (GL_PROJECTION );
  2. GlLoadIdentity ();
  3. GlOrtho (0, (IPHONE_SCREEN_WIDTH), 0, SCREEN_HEIGHT,-I2F (100), I2F (1000); // here glOrtho (left, right, bottom, top, near, FAR)
  4. // This is based on openGL coordinates
  5. GlMatrixMode (GL_MODELVIEW );
  6. GlLoadIdentity ();
  7. GlViewport (0, 0, SCREEN_WIDTH, SCREEN_HEIGHT); // glViewport (x, y, width, height)
  8. 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
  1. Void drawRect (x, y, width, height)
  2. {
  3. Y = SCREEN_HEIGHT-y-h;
  4. // Draw a rectangle using GL
  5. }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.