OpenGL ES 2.0, texture image not filling screen

I want to upload a simple texture to overlay the square I have drawn on the screen. The code without the texture shows a red square in the centre of the screen. Im editing this code to overlay the texture over the top, however every time I try to apply the texture to the square it distorts the image and moves across the screen. EDIT: Whole code available here: http://codetidy.com/6291/

Before texture applied: 在这里输入图像描述

After texture applied: 在这里输入图像描述

Some sample code:

void init()


// Create an OpenGL 2D texture from the BOX resource.
    glEnable(GL_TEXTURE_2D);
    glEnable(GL_BLEND);
    glBlendFunc(GL_ONE, GL_SRC_COLOR); 
    glGenTextures(1, &TextureHandle);
    glBindTexture(GL_TEXTURE_2D, TextureHandle);

    // Set texture parameters.
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);

    maOpenGLTexImage2D(TEXTURE);

EDIT: Whole draw() function

void draw()
    GLfloat vVertices[] = { -1.0f, 1.0f, 0.0f,
                       -1.0f, -1.0f, 0.0f,
                       1.0f, 1.0f, 0.0f,
                       1.0f, -1.0f, 0.0f
                                    };


            GLfloat TexCoord[] = {0.0, 1.0,
                    0.0, 0.0,
                    1.0, 1.0,
                    1.0, 0.0};

            // Set the viewport
            glViewport(0, 0, mWidth, mHeight);

            // Clear the color buffer
            glClear(GL_COLOR_BUFFER_BIT);

            // Use the program object
            glUseProgram(mShader);
            checkGLError("glUseProgram");

            glBindTexture(GL_TEXTURE_2D, TextureHandle);

            // Set uniform function
            glUniformMatrix4fv(mMvpLoc, 1, false, MyMatrix);
            checkGLError("glUniform4fv");

            //glActiveTexture(GL_TEXTURE0);
            glUniform1i(mTextureLoc, 0);

            // Load the vertex data, i.e attributes and quads
            glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
            glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, TexCoord);
            checkGLError("glVertexAttribPointer");

            glEnableVertexAttribArray(0);
            glEnableVertexAttribArray(1);
            checkGLError("glEnableVertexAttribArray");

            glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
            checkGLError("glDrawArrays");

Vertex and fragment shader:

char vertexShaderSource[] =
        "attribute vec4 vPosition;   n"
        "uniform mat4 uMvp;          n"
        "attribute vec2 TexCoordIn;  n"
        "varying vec2 TexCoordOut;   n"

        "void main()                 n"
        "{                           n"

        "gl_Position = uMvp * vPosition; n"
        "TexCoordOut = TexCoordIn.xy;       n"
            "}                           n";

        char fragmentShaderSource[] =
        "precision highp float;n"
        "varying vec2 TexCoordOut;n"
        "uniform sampler2D Texture;n"

        "void main()n"
        "{n"

        "gl_FragColor = texture2D(Texture, TexCoordOut);n"

        "}n";

Perhaps your vertex and texture attributes are swapped?

I'm slightly suspicious about this part:

glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, vVertices);
glVertexAttribPointer(1, 2, GL_FLOAT, GL_FALSE, 0, TexCoord);

Are you sure the texcoord attribute is actually at location 1?

I suggest using glGetAttribLocation to query these locations.


Though your texture coordinates are somewhat weird - you give the second vertex (bottom left of your rectangle) the coordinate [1, 1] (upper right of your texture) but other than that I can't find anything wrong in the above code.

The next suspect is the MyMatrix and the values it has. It seems like it has an unnessary translation. Perhaps you have some code to center it and that fails?

Edit1: The side of your rectangle is 2 units ie it expands 1 unit from the center of the screen to all directions. When using an identity matrix for projection and model view matrices, such a rectangle would cover the whole screen exactly. Please try to set your matrix to identity and verify that. The only two suspects left are either your orthographic projection is incorrect or otherwise your viewport does not cover the whole screen. I can't think of anything else

Edit2: After you published the whole source in the link in the comments I have tried to run it locally (I have used JOGL but the OpenGL part should be the same). One mistake I have found (though it worked in my system but it might be failing in yours) is the line:

glBindAttribLocation(programObject, 1, "textureCoordinate");

That variable is declared as varying . Your attribute name is inputTextureCoordinate . Other than that the code runs fine in my machine (due to a different environment I had to use different code to load my texture and my shader and to convert all your double values to float ). So if the above fix does not solve your problem try to disable any features you can ie do not multiply the matrix in the shader and do not load/bind textures (use glColor() instead). I would comment-out until I am left with the simple red quad again. One of the removed statements must be the failing one.


Aggghhh such a silly error to make! I was trying to neaten my code and moved my glBindAttributeLocation() commands to before my program was linked. This meant both vertex and texture coordinates were not loaded as I thought they would be in index's 0 and 1. This therefore made my commands calling them ineffective. Big thanks to @cs for your help, didnt quite solve the problem (hence the new answer) but put me on the right track.

链接地址: http://www.djcxy.com/p/33610.html

上一篇: OpenGL ES 2.0中的图像和蒙版

下一篇: OpenGL ES 2.0,纹理图像不填充屏幕