Displaying Graphics with OpenGL ES(四)——Applying Projection and Camera Views

运用投影和相机视图

在OpenGL ES的环境下,projection和camera views允许像你用眼睛看物体的方式绘制图像对象。通过数学的转换绘制对象坐标能模拟出这种物理视图。
~ Projection - 转变调整坐标绘制的对象是基于他们在GLSurfaceView上显示的宽和高。没有这样的计算,被OpenGL ES 绘制的对象在不同分辨率的显示器上会是歪的。A projection转换算法只在OpenGL view的比例被创建时或者在onSurfaceChanged()渲染的方法里做了改变时被执行。关于更多OpenGL ES projection和坐标映射,see Mapping Coordinates for Drawn Objects

~ Camera View- 这一转变调整基于虚拟相机的位置绘制对象的坐标。需要注意的是OpenGL ES不会定义一个实际相机对象,而是提供了通过转换绘制对象的显示模拟相机的实用方法。一个Camera View 转换算法可能只是当你创建GLSurfaceView时,或者用户行为动态地改变或者来自app的功能。

这一章节描述了怎样创建一个projection和camera view 和用它来绘制形状在GLSurfaceView里。

一. Define a Projection

在GLSurfaceView.Render类的onSurfaceChange()方法里进行投影转换计算。以下示例代码取GLSurfaceView的高度和宽度,并使用Matrix.frustumM()方法来填充投影变换矩阵:

// 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);
}

此代码填充一个投影矩阵,mProjectionMatrix 还可以用在onDrawFrame()方法里,该方法将在下一节中所示的相机视图中结合起来。

Note: 只是应用投影转换到图形对象通常只是个空的视图。一般情况下,还必须应用相机视图转换来显示在屏幕上一些东西。

二.Define a Camera View

完成这个转换处理你的绘制对象被添加到一个相机视图转换作为你的渲染绘制处理的一部分。在以下示例代码,相机视图变换是使用Matrix.setLookAtM()方法计算,然后与先前计算出的投影矩阵相结合。然后将合并的变换矩阵被传递给绘制的形状。

@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

为了使用在前面部分所示的组合的投影和摄像机视图变换矩阵,首先将矩阵变量添加到先前在Triangle类中定义的顶点着色器:

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;

    ...
}

接着,修改图形对象的draw()方法来接受组合后的转换矩阵,并将其应用到shape:

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);
}

一旦你正确地计算和应用了projection和camera view 转换,用正确的比例绘制图形对象应该看到这样子:
这里写图片描述
figrue1.三角绘制应用的projection和camera view。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值