OpenGL ES 1.0与OpenGL ES 2.0投射和摄像视角代码区别

本文介绍OpenGLES 1.0与2.0中如何设置投射矩阵和摄像视角,包括使用glFrustumf和gluLookAt进行设置的方法,以及在ES2.0中如何通过顶点着色器应用这些变换。
OpenGL ES 1.0中的投射和摄像视角
1. 投射矩阵 - 使用几何学创建一个投射矩阵,用来计算对象的坐标,以便图像被以正确的比例绘制。下面的例子展示了如何基于屏幕尺寸比例创建一个投射矩阵,然后应用到OpenGL渲染环境中。
 public void onSurfaceChanged(GL10 gl, int width, int height) {
      gl.glViewport(0, 0, width, height);
      // make adjustments for screen ratio
      float ratio = (float) width / height;
      gl.glMatrixMode(GL10.GL_PROJECTION);        // set matrix to projection mode
      gl.glLoadIdentity();                        // reset the matrix to its default state
      gl.glFrustumf(-ratio, ratio, -1, 1, 3, 7);  // apply the projection matrix
  }
2. 摄像转换矩阵 - 一旦你已经使用投射矩阵调整了坐标系统,你也必须使用一个摄像视角。下面的例子展示了如何修改onDrawFrame()方法实现应用一个模式视图,然后使用GLU.gluLookAt()功能创建一个视角转换,类似一个拍摄点。
   public void onDrawFrame(GL10 gl) {
        ...
        // Set GL_MODELVIEW transformation mode
        gl.glMatrixMode(GL10.GL_MODELVIEW);
        gl.glLoadIdentity();                      // reset the matrix to its default state
        // When using GL_MODELVIEW, you must set the camera view
        GLU.gluLookAt(gl, 0, 0, -5, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
        ...
    }
OpenGL ES 2.0中的投射和摄像视角
在ES2.0API中,先要添加一个矩阵成员到图像对象的顶点着色器中。
1. 添加矩阵到顶点着色器 - 下面的代码中包含uMVPMatrix成员,允许你应用投射和摄像视角矩阵到对象坐标中。
    private final String vertexShaderCode =
        // This matrix member variable provides a hook to manipulate
        // the coordinates of objects that use this vertex shader
        "uniform mat4 uMVPMatrix;   \n" +
        "attribute vec4 vPosition;  \n" +
        "void main(){               \n" +
        // the matrix must be included as part of gl_Position
        " gl_Position = uMVPMatrix * vPosition; \n" +
        "}  \n";
2. 访问着色器矩阵 - 在顶点着色器中创建了一个钩子后,我们可以访问顶点着色器中的矩阵变量了。
    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        ...
        muMVPMatrixHandle = GLES20.glGetUniformLocation(mProgram, "uMVPMatrix");
        ...
    }
3. 创建投射和摄像视角矩阵。
    public void onSurfaceCreated(GL10 unused, EGLConfig config) {
        ...
        // Create a camera view matrix
        Matrix.setLookAtM(mVMatrix, 0, 0, 0, -3, 0f, 0f, 0f, 0f, 1.0f, 0.0f);
    }
    public void onSurfaceChanged(GL10 unused, int width, int height) {
        GLES20.glViewport(0, 0, width, height);
        float ratio = (float) width / height;
        // create a projection matrix from device screen geometry
        Matrix.frustumM(mProjMatrix, 0, -ratio, ratio, -1, 1, 3, 7);
    }
4. 应用投射和摄像视角矩阵
    public void onDrawFrame(GL10 unused) {
        ...
        // Combine the projection and camera view matrices
        Matrix.multiplyMM(mMVPMatrix, 0, mProjMatrix, 0, mVMatrix, 0);
        // Apply the combined projection and camera view transformations
        GLES20.glUniformMatrix4fv(muMVPMatrixHandle, 1, false, mMVPMatrix, 0);
        // Draw objects
        ...
    }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值