以全屏为中心渲染纹理,并在opengl es 2.0中使用正确的比例

时间:2012-08-31 08:51:35

标签: opengl-es

我将一个位图加载到渲染器类中,我希望以正确的比例渲染纹理在屏幕中心并且尽可能大

顶点和纹理数据:

private final float [] mVerticesData =     {             -1f,1f,0.0f,//位置0             0.0f,0.0f,// TexCoord 0             -1f,-1f,0.0f,//位置1             0.0f,1.0f,// TexCoord 1             1f,-1f,0.0f,//位置2             1.0f,1.0f,// TexCoord 2             1f,1f,0.0f,//位置3             1.0f,0.0f // TexCoord 3     };

private final short[] mIndicesData =
{ 
        0, 1, 2, 0, 2, 3 
};

绘制方法:

public void onDrawFrame(GL10 glUnused)     {

    // Set the viewport
    GLES20.glViewport(0, 0, bitmap.width, bitmap.height);

    // Clear the color buffer
    GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT);

    // Use the program object
    GLES20.glUseProgram(mProgramObject);

    // Load the vertex position
    mVertices.position(0);
    GLES20.glVertexAttribPointer ( mPositionLoc, 3, GLES20.GL_FLOAT, 
                                   false, 
                                   5 * 4, mVertices );
    // Load the texture coordinate
    mVertices.position(3);
    GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
                                   false, 
                                   5 * 4, 
                                   mVertices );

    GLES20.glEnableVertexAttribArray ( mPositionLoc );
    GLES20.glEnableVertexAttribArray ( mTexCoordLoc );

    // Bind the texture
    GLES20.glActiveTexture ( GLES20.GL_TEXTURE0 );
    GLES20.glBindTexture ( GLES20.GL_TEXTURE_2D, mTextureId );

    // Set the sampler texture unit to 0
    GLES20.glUniform1i ( mSamplerLoc, 0 );

    GLES20.glDrawElements ( GLES20.GL_TRIANGLES, 6, GLES20.GL_UNSIGNED_SHORT, mIndices );
}

现在我用视图尺寸设置视口(不工作)。我应该如何计算纹理的视口尺寸以适应屏幕保持比率?

谢谢。

1 个答案:

答案 0 :(得分:0)