这是我的代码,我找不到问题所在。
bool setupGraphics(int w, int h) {
printGLString("Version", GL_VERSION);
printGLString("Vendor", GL_VENDOR);
printGLString("Renderer", GL_RENDERER);
printGLString("Extensions", GL_EXTENSIONS);
glViewport(0, 0, w, h);
checkGlError("glViewport");
glEnable(GL_DEPTH_TEST);
LOGI("setupGraphics(%d, %d)", w, h);
gProgram = createProgram(gVertexShader, gFragmentShader);
if (!gProgram) {
LOGE("Could not create program.");
return false;
}
gvPositionHandle = glGetAttribLocation(gProgram, "vPosition");
checkGlError("glGetAttribLocation");
//Init texture.
makeCheckImage();
checkGlError("init texture1");
glGenTextures(1, gTextureHandler);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);
glBindTexture(GL_TEXTURE_2D, gTextureHandler[0]);
checkGlError("init texture2");
gTextureUniformHandler = glGetUniformLocation(gProgram, "uTexture");
gTextureCoordinateHandler = glGetAttribLocation(gProgram, "vTexCoordinate");
checkGlError("init texture");
return true;
}
和日志:
I/libgl2jni(30066): GL Version = OpenGL ES 2.0 build 1.8.GOOGLENEXUS.ED945322@2112805
I/libgl2jni(30066): GL Vendor = Imagination Technologies
I/libgl2jni(30066): GL Renderer = PowerVR SGX 540
I/libgl2jni(30066): GL Extensions = GL_EXT_debug_marker GL_OES_rgb8_rgba8 GL_OES_depth24 GL_OES_vertex_half_float GL_OES_texture_float GL_OES_texture_half_float GL_OES_element_index_uint GL_OES_mapbuffer GL_OES_fragment_precision_high GL_OES_compressed_ETC1_RGB8_texture GL_OES_EGL_image GL_OES_EGL_image_external GL_OES_required_internalformat GL_OES_depth_texture GL_OES_get_program_binary GL_OES_packed_depth_stencil GL_OES_standard_derivatives GL_OES_vertex_array_object GL_OES_egl_sync GL_EXT_multi_draw_arrays GL_EXT_texture_format_BGRA8888 GL_EXT_discard_framebuffer GL_EXT_shader_texture_lod GL_IMG_shader_binary GL_IMG_texture_compression_pvrtc GL_IMG_texture_npot GL_IMG_texture_format_BGRA8888 GL_IMG_read_format GL_IMG_program_binary GL_IMG_multisampled_render_to_texture
I/libgl2jni(30066): setupGraphics(480, 800)
I/libgl2jni(30066): after init texture2() glError (invalide operation)
如果我评论
glTexImage2D(GL_TEXTURE_2D, 0, 4, 64, 64, 0, GL_RGBA, GL_UNSIGNED_BYTE, checkImage);glBindTexture(GL_TEXTURE_2D, gTextureHandler[0]);
,则一切正常。 最佳答案
看来您glBindTexture
和glTexImage
的顺序错误。 glTexImage更新当前绑定到指定目标的纹理。
首先绑定纹理,然后填充数据。另外,在设置最小/最大滤镜之前,您应该绑定纹理,因为它们也会修改绑定的纹理。
关于android - 为什么texImage2D总是给出无效的操作错误?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/13783588/