Orthographic projection in OpenGL ES

Source: Internet
Author: User

Since the normalized coordinate system used by the device is not pixel-independent, the range is -1~1 in three directions, so the direct hard-coded drawing of the entity may result in horizontal or vertical compression (distortion) of the device pixel problem.

The solution in the book is to put the operation in a virtual coordinate space, that is, we design the vertex value when the illusion is in the virtual coordinate space, as to how to map to the device, you can not consider.

Orthographic projection is a method of projecting a space area onto a two-dimensional plane (the projection is the process of descending the high dimension to the position), and the space area described here is a region that is not as large as the size of an object, no matter how far away it is, and the parts of this area cannot be seen.

The purpose of using the orthogonal projection matrix is to map the virtual coordinate space of our operation to the device screen, and then multiply the orthogonal projection matrix before assigning the vertex position in the vertex shader code to directly convert the coordinates.

Since the device uses a left-handed coordinate system, the default is the right-hand coordinate system when OpenGL is drawn, so the near/far definition needs to be reversed in the setting of the orthogonal matrix.

Final float aspectratio = width > height? (float) width/(float) Height: (float) height/(float) width;         if (Width > height) {            Orthom (ProjectionMatrix,0,-apositionlocation,aspectratio,-1f,1f,-1f,1f);        }  Else{            Orthom (ProjectionMatrix,0,-1f,1f,-aspectratio,aspectratio,-1f,1f);        } 

This is set by device resolution in onsurfacechanged, the Orthom method is an Android-provided method of building an orthogonal matrix that will save the results in ProjectionMatrix (float[16])

Now add a matrix to the vertex shader:

Uniform mat4 U_matrix;attribute vec4 a_position;void Main () {    gl_position = U_matrix * a_position;}

Before drawing the method to join the binding data:

@Override      Public void ondrawframe (GL10 gl) {        glclear (gl_color_buffer_bit);        GLUNIFORMMATRIX4FV (Umatrixlocation,1,false, projectionmatrix,0);        gluniform4f (ucolorlocation,1.0f,1.0f,0.0f,1.0f);        Gldrawarrays (Gl_triangle_strip,0,8);        gluniform4f (ucolorlocation,0.0f,1.0f,1.0f,1.0f);        Gldrawarrays (Gl_triangle_strip,8,8);    }

This is a drawing cube program, which is displayed at the beginning of the drawing:

After adding the matrix is this:

In fact this square should be placed like this (too lazy to draw, in the illusion of a picture to see):

The next step is to study the matrix transformation acting on this space, and see if we can make a cube that rotates automatically.

Orthographic projection in OpenGL ES

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.