简体   繁体   English

未使用OpenGL ES 2.0和Android NDK绘制纹理

[英]Texture not drawn using OpenGL ES 2.0 and Android NDK

I want to display an image as texture on a quad with OpenGL ES 2.0 using the Android NDK. 我想使用Android NDK在OpenGL ES 2.0上以四边形显示图像为纹理。

I have the following simple vertex and fragment shader: 我有以下简单的顶点和片段着色器:

#define DISP_SHADER_V_SRC "\
attribute vec4 aPos;\
attribute vec2 aTexCoord;\
varying vec2 vTexCoord;\
void main() {\
    gl_Position = aPos;\
    vTexCoord = aTexCoord;\
 }"

#define DISP_SHADER_F_SRC "\
precision mediump float;\n\
varying vec2 vTexCoord;\n\
uniform sampler2D sTexture;\n\
void main() {\n\
    gl_FragColor = texture2D(sTexture, vTexCoord);\n\
}"

At first, a native "create" method is called when the GLSurfaceView is created. 首先,当创建GLSurfaceView时,将调用本地“创建”方法。 It sets the clear color, builds the shader and gets me a texture id using glGenTextures . 它使用glGenTextures设置清晰的颜色,构建着色器,并为我提供纹理id。 A "resize" method sets the current view size. “调整大小”方法设置当前视图的大小。 Another method sets the texture data like this: 另一种方法是这样设置纹理数据:

glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, w, h, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels);

I don't believe that there's something wrong there. 我不相信那里有什么问题。 The important thing should be the "draw" method. 重要的应该是“绘制”方法。 After glClear, glViewport and glUseProgram I do the following: 在glClear,glViewport和glUseProgram之后,我执行以下操作:

glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, texId);

glEnableVertexAttribArray(shAPos);
glVertexAttribPointer(shAPos, 3, GL_FLOAT, false, 0, quadVertices);

glVertexAttribPointer(shATexCoord, 2, GL_FLOAT, false, 0, quadTexCoordsStd);
glEnableVertexAttribArray(shATexCoord);

glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
// now glDisableVertex...

I can confirm that the shader basically works, since gl_FragColor=vec4(1.0); 由于gl_FragColor=vec4(1.0);我可以确认着色器基本正常gl_FragColor=vec4(1.0); results in a white screen. 导致白屏。 It just does not work when I load a texture. 加载纹理时,它不起作用。 I tried out setting the pixel data to "all white" using memset just to confirm that the issue is not related with my image data, but still the screen stays black. 我尝试使用memset将像素数据设置为“全白”,只是为了确认问题与我的图像数据无关,但屏幕仍然保持黑色。 What am I missing? 我想念什么?

IsaacKleiner's own comment was correct, I have met with the same problem, I develop a android app with OpenGL ES 2.0 in C++, using NDK. IsaacKleiner的评论是正确的,我遇到了同样的问题,我使用NDK使用C ++的OpenGL ES 2.0开发了一个Android应用。 The function to load texture was in the C++ part originally. 加载纹理的功能最初在C ++部分中。 For some reason, that did not work. 由于某种原因,这没有用。 Only after I move the load texture code to java part , did it work as normal. 只有将加载纹理代码移动到java part之后 ,它才能正常工作。 I load the texture in the java part, bind it as normal, and I pass the texture ID to the C++ JNI code . 我将纹理加载到Java部分中,并按常规进行绑定,然后将纹理ID传递给C ++ JNI代码 I do not know why there is such a problem. 我不知道为什么会有这样的问题。 I can provide with a link , in which there is an example that use OpenGL ES 1.0 and NDK to show a cube. 我可以提供一个链接 ,其中有一个使用OpenGL ES 1.0和NDK显示多维数据集的示例。 Although it is in Chinese, the code can explain itself. 尽管它是中文,但是代码可以解释自己。 please pay attention to how the texture is generated and how the texture id is passed between Java and C++. 请注意如何生成纹理以及如何在Java和C ++之间传递纹理id。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM