简体   繁体   English

OpenGL ES 2.0纹理

[英]OpenGL ES 2.0 texturing

I'm trying to render a simple textured quad in OpenGL ES 2.0 on an iPhone. 我正试图在iPhone上渲染OpenGL ES 2.0中的简单纹理四边形。 The geometry is fine and I get the expected quad if I use a solid color in my shader: 几何图形很好,如果我在着色器中使用纯色,我会得到预期的四边形:

gl_FragColor = vec4 (1.0, 0.0, 0.0, 1.0);

And I get the expected gradients if I render the texture coordinates directly: 如果直接渲染纹理坐标,我会得到预期的渐变:

gl_FragColor = vec4 (texCoord.x, texCoord.y, 0.0, 1.0);

The image data is loaded from a UIImage, scaled to fit within 1024x1024, and loaded into a texture like so: 图像数据从UIImage加载,缩放以适应1024x1024,并加载到如下纹理:

glGenTextures (1, &_texture);
    glBindTexture (GL_TEXTURE_2D, _texture);

    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, 
        GL_UNSIGNED_BYTE, data);

width , height , and the contents of data are all correct, as examined in the debugger. widthheightdata内容都是正确的,如调试器中所检查的那样。

When I change my fragment shader to use the texture: 当我更改片段着色器以使用纹理时:

gl_FragColor = texture2D (tex, texCoord);

... and bind the texture and render like so: ...并绑定纹理并像这样渲染:

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

    // this is unnecessary, as it defaults to 0, but for completeness...
    GLuint texLoc = glGetUniformLocation(_program, "tex");          
    glUniform1i(texLoc, 0);

    glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

... I get nothing. ......我一无所获。 A black quad. 黑色四边形。 glGetError() doesn't return an error and glIsTexture(_texture) returns true. glGetError()不返回错误, glIsTexture(_texture)返回true。

What am I doing wrong here? 我在这做错了什么? I've been over and over every example I could find online, but everybody is doing it exactly as I am, and the debugger shows my parameters to the various GL functions are what I expect them to be. 我已经遍及我在网上找到的每一个例子,但是每个人都像我一样,并且调试器显示我对各种GL功能的参数是我所期望的。

在glTexImage2D之后,使用glTexParameter设置MIN / MAG过滤器,默认使用mipmap,因此该代码的纹理不完整。

I was experiencing the same issue (black quad) and could not find an answer until a response by jfcalvo from this question led me to the cause. 我遇到了同样的问题(黑色四边形),直到jfcalvo从这个问题的回答引导我找到答案才找到答案。 Basically make sure you are not loading the texture in a different thread. 基本上确保您没有在不同的线程中加载纹理。

make sure you set the texture wrap parameters to GL_CLAMP_TO_EDGE in both S and T directions. 确保在S和T方向上将纹理包装参数设置为GL_CLAMP_TO_EDGE。 Without this, the texture is incomplete and will appear black. 如果没有这个,纹理就不完整了,会显得很黑。

maybe you forgot to 也许你忘记了

glEnable(GL_TEXTURE_2D); glEnable(GL_TEXTURE_2D);

is such case, texture2D in the shader would return black, as the OP seems to suffer. 就是这种情况,着色器中的texture2D会返回黑色,因为OP似乎受到了影响。

make sure that you are calling (glTexImage2D) with right formats(constants) 确保使用正确的格式(常量)调用(glTexImage2D)

make sure that you are freeing resources of image after glTexImage2D 确保在glTexImage2D之后释放图像资源

that's how i'm do it on android: 这就是我在android上做的方式:

    int[] textures = new int[1];
            GLES20.glGenTextures(1, textures, 0);

            mTextureID = textures[0];
            GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, mTextureID);

            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER,
                    GLES20.GL_NEAREST);
            GLES20.glTexParameterf(GLES20.GL_TEXTURE_2D,
                    GLES20.GL_TEXTURE_MAG_FILTER,
                    GLES20.GL_LINEAR);

            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S,
                    GLES20.GL_REPEAT);
            GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T,
                    GLES20.GL_REPEAT);

            InputStream is = mContext.getResources()
                .openRawResource(R.drawable.ywemmo2);
            Bitmap bitmap;
            try {
                bitmap = BitmapFactory.decodeStream(is);
            } finally {
                try {
                    is.close();
                } catch(IOException e) {
                    // Ignore.
                }
 GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
        bitmap.recycle();

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

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