In the OpenGL ES environment, projection and camera views allow you to display drawn objects in a way that more closely resembles how you see physical objects with your eyes. This simulation of physical viewing is done with mathematical transformations of
drawn object coordinates:http://blog.csdn.net/sergeycao
-
Projection - This transformation adjusts the coordinates of drawn objects based on the width and height of the
GLSurfaceView
where they are displayed. Without this calculation, objects drawn by OpenGL ES are skewed by
the unequal proportions of the view window. A projection transformation typically only has to be calculated when the proportions of the OpenGL view are established or changed in the
onSurfaceChanged()
method of your renderer. For more information about OpenGL ES projections and coordinate mapping, seeMapping Coordinates for Drawn Objects.
-
Camera View - This transformation adjusts the coordinates of drawn objects based on a virtual camera position. It’s important to note that OpenGL ES does not define an actual camera object, but instead provides utility methods that simulate a camera
by transforming the display of drawn objects. A camera view transformation might be calculated only once when you establish your
GLSurfaceView
, or might change dynamically based on user actions or your application’s function.
This lesson describes how to create a projection and camera view and apply it to shapes drawn in yourGLSurfaceView
.
Define a Projection
The data for a projection transformation is calculated in the onSurfaceChanged()
method of yourGLSurfaceView.Renderer
class. The following example code takes the height and width of theGLSurfaceView
and uses it to populate a projection transformationMatrix
using the
Matrix.frustumM()
method:
@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(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
}
This code populates a projection matrix, mProjMatrix
which you can then combine with a camera view transformation in theonDrawFrame()
method, which is shown in the next section.
Note: Just applying a projection transformation to your drawing objects typically results in a very empty display. In general, you must also apply a camera view transformation in order for anything to show up on screen.
Define a Camera View
Complete the process of transforming your drawn objects by adding a camera view transformation as part of the drawing process. In the following example code, the camera view transformation is calculated using theMatrix.setLookAtM()
method and then combined with the previously calculated projection matrix. The combined transformation matrices are then passed to the drawn shape.
@Override
public void onDrawFrame(GL10 unused) {
...
// Set the camera position (View matrix)
Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
// Calculate the projection and view transformation
Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
// Draw shape
mTriangle.draw(mMVPMatrix);
}
Apply Projection and Camera Transformations
In order to use the combined projection and camera view transformation matrix shown in the previews sections, modify thedraw()
method of your graphic objects to accept the combined transformation matrix and apply it to the shape:
public void draw(float[] mvpMatrix) { // pass in the calculated transformation matrix
...
// get handle to shape's transformation matrix
mMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mMVPMatrixHandle, 1, false, mvpMatrix, 0);
// Draw the triangle
GLES20.glDrawArrays(GLES20.GL_TRIANGLES, 0, vertexCount);
...
}
Once you have correctly calulated and applied the projection and camera view transformations, your graphic objects are drawn in correct proportions and should look like this:
Figure 1. Triangle drawn with a projection and camera view applied.
Now that you have an application that displays your shapes in correct proportions, it's time to add motion to your shapes.
分享到:
相关推荐
UML和模式应用 Applying UML and Patterns(中文版).pdf
Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and the Unified Process, Second Edition
Applying UML and Patterns1
Applying UML and Patterns第三版电子书,完整版
Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development (3rd Edition) <br>by Craig Larman <br>Publisher: Prentice Hall PTR; 3 edition ...
Applying Uml And Patterns: An Introduction To Object-Oriented Analysis And Design, And The Unified Process.Craig Larman,2001
LARMAN的关于软件工程知识方面的经典之作.书中用一个完整的POS项目,详细说明如何在大型软件开发项目中逐步建立OOP的概念,使用UML和PATTERN这些事半功倍的利器. 书是PDF 格式, 很好,很清晰.
uml和模式应用 applying uml and patterns(中文版).part5
Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, Third Edition By Craig Larman 真正的英文版第三版 绝对超级,国外网站现在要10美元!!! 这...
Applying Uml And Patterns
Applying UML and Patterns: An Introduction to Object-Oriented Analysis and Design and Iterative Development, Third Edition By Craig Larman 真正的英文版第三版 绝对超级,国外网站现在要10...
一本从概念到实践都能叙述的很清楚的uml教材。学习uml必读。
本书是经典的OOA/D、迭代式开发和UML方面的入门书,已被翻译成多种语言并在业界和高等院校中广泛使用。它可以帮助开发者和学生掌握OOA/D的重要原则和最佳实践--不仅仅是绘制UML,而是在软件设计过程中应用它们。...
Applying UML and Patterns2
面向对象领域经典巨著,从事OOA/D必读书目,英文原版,第二版,高清
Applying UML.and Patterns 3rd Edition CHM版 很好的一本 UML 和面向对象分析与设计的教材 英文第三版