gpt4 book ai didi

android - Opengl Es 2.0 - 显示多个纹理

转载 作者:行者123 更新时间:2023-11-30 02:48:43 24 4
gpt4 key购买 nike

所以我一次可以显示 1 个纹理,但我在显示更多纹理时遇到了问题。分别为每个纹理 (tex_nr) 调用渲染函数,不确定这是否是一个好方法。这是渲染代码:

private void Render(float[] m, int tex_nr) {

// Set our shaderprogram to image shader
GLES20.glUseProgram(riGraphicTools.sp_Image);

// clear Screen and Depth Buffer, we have set the clear color as black.
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

// get handle to vertex shader's vPosition member
int mPositionHandle = GLES20.glGetAttribLocation(riGraphicTools.sp_SolidColor, "vPosition");

// Enable generic vertex attribute array
GLES20.glEnableVertexAttribArray(mPositionHandle);

// Prepare the triangle coordinate data
GLES20.glVertexAttribPointer(mPositionHandle, 3,
GLES20.GL_FLOAT, false,
0, vertexBuffer);

// Get handle to texture coordinates location
int mTexCoordLoc = GLES20.glGetAttribLocation(riGraphicTools.sp_Image,
"a_texCoord" );

// Enable generic vertex attribute array
GLES20.glEnableVertexAttribArray ( mTexCoordLoc );

// Prepare the texturecoordinates
GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
false,
0, uvBuffer);

// Get handle to shape's transformation matrix
int mtrxhandle = GLES20.glGetUniformLocation(riGraphicTools.sp_Image,
"uMVPMatrix");

// Apply the projection and view transformation
GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, m, 0);

// Get handle to textures locations
int mSamplerLoc = GLES20.glGetUniformLocation (riGraphicTools.sp_Image,
"s_texture" );

// Set the sampler texture unit to where we have saved the texture.
GLES20.glUniform1i ( mSamplerLoc, tex_nr);

// Draw the triangle
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
GLES20.GL_UNSIGNED_SHORT, drawListBuffer);

// Disable vertex array
GLES20.glDisableVertexAttribArray(mPositionHandle);
GLES20.glDisableVertexAttribArray(mTexCoordLoc);

}

这里是加载纹理的代码:

texturenames = new int[GameObject.spritePaths.size()];
GLES20.glGenTextures(GameObject.spritePaths.size(), texturenames, 0);
for(int i = 0; i < GameObject.spritePaths.size(); i++)
{
// Retrieve our image from resources.
int id = mContext.getResources().getIdentifier(GameObject.spritePaths.get(i), null, mContext.getPackageName());

// Temporary create a bitmap
Bitmap bmp = BitmapFactory.decodeResource(mContext.getResources(), id);

// Bind texture to texturename
GLES20.glActiveTexture(GLES20.GL_TEXTURE0+i);
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texturenames[i]);

// Set filtering
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);

// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bmp, 0);

// We are done using the bitmap so we should recycle it.
bmp.recycle();
}

由于这段代码,只显示最后加载的纹理,我不知道为什么以前的纹理不显示。如果有任何线索,我将不胜感激!编辑:实际上,我刚刚发现我可以显示我加载的所有纹理,但一次只能显示 1 个。所以我猜这个问题与加载无关?看起来好像每个连续的 Render 调用(我的函数)都阻止了上一个调用中的绘图......

最佳答案

您在 Render() 方法中调用了 glClear():

// clear Screen and Depth Buffer, we have set the clear color as black.
GLES20.glClear(GLES20.GL_COLOR_BUFFER_BIT | GLES20.GL_DEPTH_BUFFER_BIT);

这与名称所暗示的几乎一样,并且在您的评论中也捕获了:它清除了到目前为止已经完成的所有渲染。因此,如果您多次调用 Render() 方法,则只有最后一次调用的渲染结果可见。

如果你想为同一帧多次调用Render(),你需要把它从Render()中取出来,并且只调用一次渲染帧的开始。

您的代码中还有另一件事看起来有点可疑:

int mPositionHandle = GLES20.glGetAttribLocation(riGraphicTools.sp_SolidColor, "vPosition");
....
int mTexCoordLoc = GLES20.glGetAttribLocation(riGraphicTools.sp_Image, "a_texCoord");

除非 sp_SolidColorsp_Image 具有相同的值,否则您正在从两个不同的着色器程序中查询属性位置。由于 sp_Image 是您正在使用的程序,因此您应该将其作为这两个调用的第一个参数传入。

关于android - Opengl Es 2.0 - 显示多个纹理,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24580485/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com