PDA

View Full Version : Texture not being drawn, UV Coordinates not being passed into shader.



Dooskington
06-11-2013, 08:58 AM
Howdy.

For some reason when trying to render a texture on a quad, the quad just turns the main color of the texture (in this case yellow). I found out that the uv coordinates were not being passed into the fragment shader ( they are 0,0 for each vertex by the time it gets there ). I am pretty sure there is something wrong with this function:
This is with Java and lwjgl but the glVertexAttribPointer calls are where I am sure it is messing up.



FloatBuffer vertBuff = BufferUtils.createFloatBuffer(vertices.length * 9);
for(int i = 0; i < vertices.length; i++) {
vertBuff.put(vertices[i].getElements());
}
vertBuff.flip();

vertexCount = vertices.length;

vaoID = glGenVertexArrays();
glBindVertexArray(vaoID);

vboID = glGenBuffers();
glBindBuffer(GL_ARRAY_BUFFER, vboID);
glBufferData(GL_ARRAY_BUFFER, vertBuff, GL_STATIC_DRAW);

System.out.println("VAO " + vaoID + " created. VBO " + vboID + " created and filled with " + vertBuff.capacity() + " elements.");

glVertexAttribPointer(0, 3, GL_FLOAT, false, 36, 0); // position
glVertexAttribPointer(1, 4, GL_FLOAT, false, 36, 12); // color
glVertexAttribPointer(2, 2, GL_FLOAT, false, 36, 28); // uv coordinates

glEnableVertexAttribArray(0); // Disable our Vertex Array Object
glBindVertexArray(0); // Disable our Vertex Buffer Object


I am sure that my byte offsets and strides are correct because the position gets set correctly. I have triple checked and the uv coordinates are being passed into the vertBuff. Here is my vert shader and frag shader:

vert:


#version 330

layout (location = 0) in vec3 inPosition;
layout (location = 1) in vec4 inColor;
layout (location = 2) in vec2 inUV;

uniform mat4 projectionMatrix;
uniform mat4 viewMatrix;
uniform mat4 modelMatrix;

out vec4 outColor;
out vec2 outUV;

void main(){
outUV = inUV;
outColor = inColor;
gl_Position = projectionMatrix * viewMatrix * modelMatrix * vec4(inPosition, 1.0);
}


frag:


#version 330

in vec4 outColor;
in vec2 outUV;

uniform vec4 uniColor;
uniform sampler2D tex;

out vec4 outputColor;

void main(){
//outputColor = vec4(outUV, 0.0, 1.0);
outputColor = texture(tex, outUV);
}

Aaronugio
06-11-2013, 10:14 AM
I would just like to start by saying, I'm not an expert, by any means. In fact the majority of my experience with OpenGL is with OpenGL ES 2.0 on Android, with a little bit of introduction on OpenGL 3.0 on Windows.

You may want to try "texture2D(tex, outUV);" in your fragment shader.

Also, unless you're using lighting, you should probably to completely get rid of your normal color. and just use your texture colors as the out put from the fragment shader, especially since it doesn't seem that the normal color is used at all.

i.e. outputColor = texture2D(tex, outUV);

Another possible problem I'm seeing is that in your glVertexAttribPointer() function, the last function is supposed to be your buffer, whereas you're using a position in it, I'm assuming?

Another thought, why are you using a custom Fragment output variable, rather than the default gl_FragColor?

If anything I'm saying is wrong, or just seems ignorant, please correct me.

Dooskington
06-11-2013, 11:22 AM
I would just like to start by saying, I'm not an expert, by any means. In fact the majority of my experience with OpenGL is with OpenGL ES 2.0 on Android, with a little bit of introduction on OpenGL 3.0 on Windows.

You may want to try "texture2D(tex, outUV);" in your fragment shader.

Also, unless you're using lighting, you should probably to completely get rid of your normal color. and just use your texture colors as the out put from the fragment shader, especially since it doesn't seem that the normal color is used at all.

i.e. outputColor = texture2D(tex, outUV);

Another possible problem I'm seeing is that in your glVertexAttribPointer() function, the last function is supposed to be your buffer, whereas you're using a position in it, I'm assuming?

Another thought, why are you using a custom Fragment output variable, rather than the default gl_FragColor?

If anything I'm saying is wrong, or just seems ignorant, please correct me.


Using texture2D as the function gives the same result. I am using glVertexAttribPointer() correctly, I am not passing in a position in the last parameter, that is a comment that says position. gl_FragColor and outputcolor seem to do the same thing. I have no idea why but it does :L

I am clueless as to why this isn't working now.

carsten neumann
06-11-2013, 02:11 PM
glEnableVertexAttribArray(0); // Disable our Vertex Array Object


You are only enabling vertex attribute 0 (inPosition) not the others. The comment on the quoted line is bogus, it does not disable your VAO (glBindVertexArray(0) does that). For a vertex attribute to be read from a buffer you need to set up the data source (glVertexAttribPointer) and enable that vertex attribute (glEnableVertexAttribArray).

Dooskington
06-11-2013, 02:23 PM
glEnableVertexAttribArray(0); // Disable our Vertex Array Object


You are only enabling vertex attribute 0 (inPosition) not the others. The comment on the quoted line is bogus, it does not disable your VAO (glBindVertexArray(0) does that). For a vertex attribute to be read from a buffer you need to set up the data source (glVertexAttribPointer) and enable that vertex attribute (glEnableVertexAttribArray).

Yeah sorry, the comment was an error.
For some reason I remember hearing somewhere that enabling the array is not required in modern opengl. Guess I was wrong. Thanks for the help :)