[英]Render texture in fullscreen centered and with the right ratio in opengl es 2.0
Im loading a bitmap into the renderer class and I want to render the texture centered in the screen with the right ratio and as big as it can be 我将位图加载到renderer类中,我想以正确的比例渲染尽可能集中在屏幕中央的纹理
The vertices and texture data: 顶点和纹理数据:
private final float[] mVerticesData = { -1f, 1f, 0.0f, // Position 0 0.0f, 0.0f, // TexCoord 0 -1f, -1f, 0.0f, // Position 1 0.0f, 1.0f, // TexCoord 1 1f, -1f, 0.0f, // Position 2 1.0f, 1.0f, // TexCoord 2 1f, 1f, 0.0f, // Position 3 1.0f, 0.0f // TexCoord 3 }; 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
};
And the draw method : 和绘制方法:
public void onDrawFrame(GL10 glUnused) { 公共无效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 );
}
Right now Im setting the viewport with the bitmap dimensions(not working). 现在,我用位图尺寸设置了视口(不起作用)。 How should I calculate the viewport dimensions for the texture to fit the screen keeping the ratio?
如何计算纹理的视口尺寸以适合屏幕并保持比例?
thank you. 谢谢。
May be you should use ModelViewPrjoection matrix. 可能是您应该使用ModelViewPrjoection矩阵。 Look at this source http://code.google.com/p/android-opengles-samples-for-vikulov-blogspot-com/source/browse/trunk/OpenglTemplate/src/com/vikulov/opengl/MyGLRenderer.java
查看此源http://code.google.com/p/android-opengles-samples-for-vikulov-blogspot-com/source/browse/trunk/OpenglTemplate/src/com/vikulov/opengl/MyGLRenderer.java
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.