繁体   English   中英

在opengl es 2.0中以全屏居中渲染纹理并以正确的比例渲染

[英]Render texture in fullscreen centered and with the right ratio in opengl es 2.0

我将位图加载到renderer类中,我想以正确的比例渲染尽可能集中在屏幕中央的纹理

顶点和纹理数据:

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

和绘制方法:

公共无效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 );
}

现在,我用位图尺寸设置了视口(不起作用)。 如何计算纹理的视口尺寸以适合屏幕并保持比例?

谢谢。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM