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

来源:互联网 发布:网络问卷调查技巧 编辑:程序博客网 时间:2024/05/16 16:58
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        ...    }

0 0