Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to apply projection and camera views to Android

2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains "Android how to apply projection and camera view," interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Let's let Xiaobian take you to learn "Android how to apply projection and camera view"!

In the OpenGL ES environment, projected and camera views bring the objects you draw closer to the physical objects you see on display.

This simulation of the physical view is accomplished by mathematical transformation of the coordinates of the drawn object:

Projection---This transformation adjusts coordinates based on the width and height of the GLSurfaceView in which the object being drawn is located. Without this calculation, objects drawn with OpenGL ES will be asymmetrical due to differences in view window scale.

Projection transformations are usually calculated only when the OpenGL view is created, or when there is a change in your renderer's onSurfaceChanged() method. For more information on OpenGL ES projections and coordinate mapping, see "Coordinate Mapping of Drawn Objects."

Camera View-This transformation adjusts the coordinates of the drawn object based on the position of a virtual camera. It is important to note that OpenGL ES does not define an actual camera object, but rather provides practical ways to simulate a camera to transform the display of the object being drawn. Camera view transitions may be calculated only once when GLSurfaceView is established or dynamically changed based on user actions or application functionality.

define projection

The data used for projection transformation is computed in the onSurfaceChanged() method of the GLSurfaceView.Renderer class. The following example code takes the GLSurfaceView height and width and uses the Matrix.frustumM() method to complete the projection transformation:

@Override public void onSurfaceChanged(GL10unused, int width, int height) { GLES20.glViewport(0, 0, width, height); float ratio = (float) width / height; // this projection matrixis applied to object coordinates // in the onDrawFrame()method Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7); }

This code fills in a projection matrix---mProjMatrix, which you can then combine with the camera view transformation in the onDrawFrame() method as shown in the code example below.

Note: Usually, using only the projection transform leaves you drawing an empty object, so in order for it to display on any screen, you must also use the camera view transform.

Defining Camera Views

The transformation process of the drawn object is accomplished by adding camera view transformation as part of the drawing process. In the following example code, the Matrix.setLookAtM() method is used to calculate the camera view transformation, and then combined with the projection matrix calculation above. Then use this combined transformation matrix to draw the graph.

@Override public void onDrawFrame(GL10unused) { ... // Set the camera position(View matrix) Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f); // Calculate theprojection and view transformation Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0); // Draw shape mTriangle.draw(mMVPMatrix); }

Apply projection and camera transformation

To use the projection and camera view transformation matrices we described above, we need to edit the graph object draw() method to accept this combined transformation matrix and apply it to the graph:

publicvoid draw(float[] mvpMatrix){// pass inthe calculated transformation matrix ... // get handle to shape'stransformation matrix mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix"); // Apply the projectionand view transformation GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0); // Draw the triangle GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount); ... } At this point, I believe that everyone has a deeper understanding of "how Android applies projection and camera view", so let's actually operate it! Here is the website, more related content can enter the relevant channels for inquiry, pay attention to us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report