Android OpenGLES 2.0加载更多纹理
我想在OpenGL中加载多个纹理并使用它们 这是TextureLoader,我认为它正确地将纹理加载到idx位置Android OpenGLES 2.0加载更多纹理,android,opengl-es,opengl-es-2.0,Android,Opengl Es,Opengl Es 2.0,我想在OpenGL中加载多个纹理并使用它们 这是TextureLoader,我认为它正确地将纹理加载到idx位置 public int[] texture = new int[10]; public int loadTexture(Context context, int resource,int idx){ GLES20.glGenTextures(idx, texture, 0); BitmapFactory.Options bo = new BitmapFactor
public int[] texture = new int[10];
public int loadTexture(Context context, int resource,int idx){
GLES20.glGenTextures(idx, texture, 0);
BitmapFactory.Options bo = new BitmapFactory.Options();
Bitmap tex = BitmapFactory.decodeResource(context.getResources(), resource, bo);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_NEAREST);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, tex, 0);
tex.recycle();
if(texture[idx] == 0){
// Displays error
}
return texture[idx];
}
这是我的渲染,我不知道如何选择加载的纹理
int mPositionHandle =
GLES20.glGetAttribLocation(riGraphicTools.sp_Image, "vPosition");
GLES20.glEnableVertexAttribArray(mPositionHandle);
GLES20.glVertexAttribPointer(mPositionHandle, 3,
GLES20.GL_FLOAT, false,
0, vertexBuffer);
int mTexCoordLoc = GLES20.glGetAttribLocation(riGraphicTools.sp_Image,
"a_texCoord" );
GLES20.glEnableVertexAttribArray ( mTexCoordLoc );
// Prepare the texturecoordinates
GLES20.glVertexAttribPointer ( mTexCoordLoc, 2, GLES20.GL_FLOAT,
false,
0, uvBuffer);
int mtrxhandle = GLES20.glGetUniformLocation(riGraphicTools.sp_Image,
"uMVPMatrix");
GLES20.glUniformMatrix4fv(mtrxhandle, 1, false, m, 0);
int mSamplerLoc = GLES20.glGetUniformLocation (riGraphicTools.sp_Image,
"s_texture" );
GLES20.glUniform1i ( mSamplerLoc, 0);
GLES20.glDrawElements(GLES20.GL_TRIANGLES, indices.length,
GLES20.GL_UNSIGNED_SHORT, drawListBuffer);
GLES20.glDisableVertexAttribArray(mPositionHandle);
GLES20.glDisableVertexAttribArray(mTexCoordLoc);
假设您用必需的数据填充了缓冲区,那么看起来您已经拥有了所需的大部分内容。您的代码中有一个调用,其中的参数不是您想要的:
GLES20.glGenTextures(idx, texture, 0);
对于Android中的Java绑定,第一个参数是要生成的ID数量,第二个参数是ID数组,第三个参数是存储生成的ID的数组中的开始索引。因此,要生成一个id并将其存储为纹理[idx]
,正确的参数如下:
GLES20.glGenTextures(1, texture, idx);
然后,对于问题的核心部分,您要寻找的电话是glBindTexture()
。此调用定义大多数纹理相关OpenGL操作使用的特定纹理。您将需要在多个位置拨打此电话
- 在
方法中,loadTexture()
和GLES20.glTexParameteri()
都对当前绑定的纹理进行操作。因此,在这些调用之前,您需要添加此调用以绑定新纹理:GLUtils.texImage2D()
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, texture[idx]);
- 在进行绘制操作之前,需要进行相同的调用。因此,在渲染方法中的某个地方,在调用
之前,使用一个类似于上面的GLES20.glDrainElements()
调用,使用要使用的纹理的glBindTexture()
值。这是您真正选择在渲染期间应用加载的纹理的地方idx
glActiveTexture()
调用