Android OpenGL Es Development: Projection and Camera View Creation and Application

The OpenGL es environment allows you to display the objects you draw in a way closer to the physical objects your eyes see. The simulation of physical view is completed by mathematically transforming the coordinates of the object you draw:

•   Projection - this transformation adjusts the coordinates of the painted objects based on the width and height of the glsurfaceview they display. Without this computational transformation, shapes drawn through OpenGL will deform in different display windows. This projection change is usually only determined in the scale of OpenGL view or calculated in your renderer's onsurfacechanged () method. For more information about projection and coordinate mapping, see drawing object coordinate mapping.

•   Camera view - this switch adjusts the coordinates of the drawing object based on the position of the virtual camera. It should be noted that OpenGL es does not define a real camera object, but provides a practical method to simulate a camera by transforming the display of the drawn object. The camera view transformation may only be calculated when your glsurfaceview is determined, or it may be changed dynamically based on user actions or the functions of your application.

This course describes how to create projection and camera views and apply them to the painted objects of your glsurfaceview.

Define projection

The projection change data is calculated in the onsurfacechanged () method of your glsurfaceview. Renderer class. The following example code is to obtain the height and width of glsurfaceview and fill them into the projection transformation matrix through the matrix. Frustumm() method.

// mMVPMatrix is an abbreviaTIon for "Model View ProjecTIon Matrix" private final float[] mMVPMatrix = new float[16]; private final float[] mProjecTIonMatrix = new float[16]; private final float[] mViewMatrix = new float[16]; @ Override public void onSurfaceChanged(GL10 unused, int width, int height) GLES20.glViewport(0, 0, width, height); float raTIo = (float) width / height; // this projection matrix is applied to object coordinates // in the onDrawFrame() method Matrix.frustumM(mProjectionMatrix, 0, -ratio, ratio, -1, 1, 3, 7);

The above code is filled with a projection matrix mprojectionmatrix, which can be combined with the camera view in the next part in the onframedraw() method.

Note: if you only apply the projection matrix to the object you draw, you will usually get a very empty display. In general, you must also apply camera views for anything you want to display on the screen.

Define camera view

Complete the transformation process of your drawn image by adding camera view transformation in your renderer as part of your drawing process. In the following code, the camera view transformation is calculated by the matrix. Setlookatm() method, and then combined with the previously calculated projection matrix. The merged matrix is then passed to the drawing.

@Override public void onDrawFrame(GL10 unused) ... // Set the camera position (View matrix) Matrix.setLookAtM(mViewMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); // Calculate the projection and view transformation Matrix.multiplyMM(mMVPMatrix, 0, mProjectionMatrix, 0, mViewMatrix, 0); // Draw shape mTriangle.draw(mMVPMatrix);

Apply projection and camera transformations

In order to use the combined matrix of projection and camera view transformation shown in the previous section, first add a matrix variable to the fixed-point shader code defined in the triangle class:

public class Triangle private final String vertexShaderCode = // This matrix member variable provides a hook to manipulate // the coordinates of the objects that use this vertex shader "uniform mat4 uMVPMatrix;" "attribute vec4 vPosition;" "void main() " // the matrix must be included as a modifier of gl_Position // Note that the uMVPMatrix factor *must be first* in order // for the matrix multiplication product to be correct. " gl_Position = uMVPMatrix * vPosition;" ""; // Use to access and set the view transformation private int mMVPMatrixHandle; ...

Next, modify the draw () method of your graphic object to receive the joint transformation matrix and apply them to the graph:

public void draw(float[] mvpMatrix) // pass in the calculated transformation matrix ... // get handle to shape's transformation matrix mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); // Pass the projection and view transformation to the shader GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0) ; // Draw the triangle GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); // Disable vertex array GLES20.glDisableVertexAttribArray(mPositionHandle);

Once you correctly calculate and apply projection and camera view transformation, your drawing object will be drawn at the correct scale. It should look like the following:

Now you have an application that can display graphics at the correct scale. In the following chapters, we can learn how to add motion to your graphics.

Android OpenGL Es Development: Projection and Camera View Creation and Application 1

recommended articles
Related Blogs Info Center
Insights network launched the install blockchain, which is one of the most unique blockchain technology use cases seen in the world. In this article, we will discuss...
Reasons why we should impose tariffs on goods from China...?I really see no good so I will give you things to prepare for. 1.) Trade is good for everyone 2.) This wo...
On March 8, the new surface Pro X was officially launched in China. As Microsoft's lightest, strongest and most connected surface product so far, surface Pro x adopt...
Well there is a lot of empty space in an atom.They actually say that if you take all the space out of atoms then the Empire State building would shrink to the size o...
Beijing, August 4, 2011 - Texas Instruments (TI) recently announced the launch of a 50 Ma, 60 V synchronous swift â„¢ The step-down regulator can pr...
This is the wiki text version of lesson 1 and lesson 2 of arm bare metal phase 1 enhanced version.Why do you want to learn SCM without a future?Because it's a good e...
Since the outbreak of the epidemic, health codes, online diagnosis and treatment, infrared thermometers, etc. have been gradually known by the public. They share a "...
Both begin with S.And the word Soul also begins with the letter S as well.Songs have hooks, bass lines, riddim, lyrics, chord progression, solos and song structure.S...
Recently, the Ministry of education, the national development and Reform Commission and the Ministry of Finance jointly issued several opinions on building "double f...
As users and enthusiasts of traditional cars, facing the rapid development of new energy vehicles, there will indeed be a period of pain. However, from a macro persp...
no data
Contact Us
Contact Person: AI customer service
Tel: +86 0757-23368757
Address: No.4 Of Xingye Road, Shafu Industrial Park, Longjiang Town, Shunde District, Foshan 
WHATSAPP: +86-15919090839
WECHAT: w87735492
Better Touch Better Business
Contact Sales at JuJiao.
Call Us
+86 0757-23368757
Customer service