我正在尝试使用OpenGL在Android上的正方形上渲染512x512纹理。当我使用模拟器时,我的程序工作正常,但是如果我使用Nexus 7(旧的),glGetError()将返回1282.这在我在渲染循环中调用glUseProgram之后发生。 GLUtils.getEGLErrorString表示错误1282对应于错误0x502,它代表GL_INVALID_OPERATION。
我已尝试过this回答,this回答和this文章中提到的解决方案。我已经尝试将我的纹理放在drawable-nodpi中,我也确保我的纹理大小是2的幂。我也尝试将我的纹理放在res / raw中,然后按照加载纹理的替代方法,但这也不起作用。
有人能指出我正确的方向吗?
如果我有什么方法可以改善我的问题,请告诉我!
我的着色器:
private final String vertexShaderCode =
// This matrix member variable provides a hook to manipulate
// the coordinates of the objects that use this vertex shader
"uniform mat4 uMVPMatrix;" +
// Per-vertex texture coordinate info we will pass in
"attribute vec2 a_TexCoordinate;" +
// This will be passed into the fragment shader
"varying vec2 v_TexCoordinate;" +
"attribute vec4 vPosition;" +
"void main() {" +
// the matrix must be included as a modifier of gl_Position
" gl_Position = vPosition * uMVPMatrix;" +
" v_TexCoordinate = a_TexCoordinate;" +
"}";
private final String fragmentShaderCode =
// The input texture
"uniform sampler2D u_Texture;" +
// Interpolated texture coordinate per fragment
"varying vec2 v_TexCoordinate;" +
"precision mediump float;" +
"uniform vec4 vColor;" +
"void main() {" +
" gl_FragColor = (vColor * texture2D(u_Texture, v_TexCoordinate));" +
"}";
我的平局电话:
public void draw(float[] mvpMatrix) {
GLES20.glUseProgram(shaderProgramHandle);
// POSITION INFO - Get a handle to the position attribute in the shader and
// associate the vertices with it
int vPositionHandle = GLES20.glGetAttribLocation(shaderProgramHandle, "vPosition");
GLES20.glEnableVertexAttribArray(vPositionHandle);
GLES20.glVertexAttribPointer(
vPositionHandle,
COORDS_PER_VERTEX,
GLES20.GL_FLOAT,
false,
COORDS_PER_VERTEX * BYTES_PER_COORD,
vertexBuffer);
// COLOR INFO - Get a handle to the color attribute in the shader and associate
// the color with it
int vColorHandle = GLES20.glGetUniformLocation(shaderProgramHandle, "vColor");
GLES20.glUniform4fv(vColorHandle, 1, vertexColor, 0);
// TRANSFORM INFO - Get a handle to the MVP matrix uniform in the shader and
// associate the MVP matrix with it
int uMVPHandle = GLES20.glGetUniformLocation(shaderProgramHandle, "uMVPMatrix");
GLES20.glUniformMatrix4fv(uMVPHandle, 1, false, mvpMatrix, 0);
// TEXTURE INFO
int texCoordinateHandle = GLES20.glGetAttribLocation(shaderProgramHandle, "a_TexCoordinate");
GLES20.glEnableVertexAttribArray(texCoordinateHandle);
GLES20.glVertexAttribPointer(
texCoordinateHandle,
COORDS_PER_TEX,
GLES20.GL_FLOAT,
false,
0,
textureCoordinatesBuffer);
int texUniformHandle = GLES20.glGetUniformLocation(shaderProgramHandle, "u_Texture");
// Set the active texture unit to texture unit 0
GLES20.glActiveTexture(GLES20.GL_TEXTURE0);
// Bind the texture to this unit
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureDataHandle);
// Tell the texture uniform sampler to use this texture in the shader
// by binding to texture unit 0
GLES20.glUniform1i(texUniformHandle, 0);
// Draw the square
GLES20.glDrawArrays(GLES20.GL_TRIANGLE_STRIP, 0, vertices.length / 3);
// Cleanup
GLES20.glDisableVertexAttribArray(vPositionHandle);
GLES20.glDisableVertexAttribArray(texCoordinateHandle);
}
我在GLSurfaceView.Renderer的onSurfaceCreated实现中加载了我的纹理。 这是一个片段,显示我如何加载纹理。
int loadTexture(final Context context, final int resourceId) {
final int[] textureHandle = new int[1];
GLES20.glGenTextures(1, textureHandle, 0);
Utils.checkGLError("glGenTextures");
if (textureHandle[0] != 0) {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false; // No pre-scalings
// Read in the resource
final Bitmap bitmap = BitmapFactory.decodeResource(
context.getResources(),
resourceId,
options);
// InputStream is = context.getResources().openRawResource(resourceId);
// Bitmap bitmap = null;
// try {
// bitmap = BitmapFactory.decodeStream(is);
// } finally {
// //Always clear and close
// try {
// is.close();
// is = null;
// } catch (IOException e) {
// }
// }
// Bind to the texture in OpenGL
GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureHandle[0]);
// 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);
// 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);
// Load the bitmap into the bound texture.
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
// Recycle the bitmap, since its data has been loaded into OpenGL.
bitmap.recycle();
}
if (textureHandle[0] == 0) {
throw new RuntimeException("Error loading texture.");
}
return textureHandle[0];
}