0

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: enter image description here

After texture applied:enter image description here

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"; 
2
  • Is the above sample the whole source? Please post the whole Draw() code and the part where vertex/texture coordinates are generated Commented Jul 25, 2013 at 14:16
  • Ive edited the question to show the whole draw code including the vertex and texture coordinates. Commented Jul 25, 2013 at 14:32

3 Answers 3

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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 i.e. 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 i.e. 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.

6 Comments

I just noticed the conflicting coordinates so have changed them so they match up. MyMatrix is a orthographic projection matrix so it has no effect on the overall positioning of the quad it just normalises the vertex coordinates.
I set MyMatrix to a identity matrix and still no luck. My viewport is set with glViewport(0, 0, mWidth, mHeight), where mWidth and mHeight represent the whole screen measurements so I doubt that is the problem. Thanks though.
When you set the identity matrix the rectangle is supposed to draw the whole screen. What do you see instead?
I see exactly the same apart from the texture image is stretched.
Please print the mWidth, mHeight values and check if they match your screen dimensions the whole time. Then check if the shader executing is actually the one you posted (try to return a fixed red color instead of sampling the texture to verify). Next I would check the function that loads the texture. (Without the whole source is difficult to guess what could be wrong)
|
0

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 @c.s for your help, didnt quite solve the problem (hence the new answer) but put me on the right track.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.