Texture coordinate problem with GLSL


I am having a problem again. This time it is the shader. I have made up a cube out of 6*2 triangles and I am feeding the textureCoordinates to the GPU. But I am not able to get the coordinates to work inside the shader. They seem to be 0, 0 all the time, so that the applied texture is just made up of the first pixel at position 0,0.

I have surely made some superstupid mistake. Or maybe a hundred. And I know my code is crap because I thought I would just make a few tests and then do some serious work refactoring it all beautifully… :S

Here is the constructor for the box:

public Box(){
		vertBuff = new VertexBufferObject(6*6*3);
		texCoordBuff = new VertexBufferObject(6*6*2);
		vertBuff.put(new float[]{ -1,-1,1,  1,-1,1,  1,1,1, // first side
								  -1,-1,1,  1,1,1,   -1,1,1,
								  1,-1,1,  1,-1,-1,  1,1,-1, // second side
								  1,-1,1,  1,1,-1,   1,-1,1,
								  1,-1,-1, -1,-1,-1, -1,1,-1, // third side
								  1,-1,-1, -1,1,-1,  1,1,-1,

								 -1,-1,-1, -1,-1,1,  -1,1,1, // fourth side
								 -1,-1,-1,  -1,1,1,  -1,1,-1,

								  -1,1,1,   1,1,1,   1,1,-1, // fifth side
								  -1,1,1,   1,1,-1,  -1,1,-1,

								  -1,-1,1,   1,-1,1,   1,-1,-1, // sixth side
								  -1,-1,1,   1,-1,-1,  -1,-1,-1,
		texCoordBuff.put(new float[]{ 0,0,  1,0,  1,1, 
									  0,0,  1,1,  0,1,
									  0,0,  1,0,  1,1, 
									  0,0,  1,1,  0,1,

									  0,0,  1,0,  1,1, 
									  0,0,  1,1,  0,1,
									  0,0,  1,0,  1,1, 
									  0,0,  1,1,  0,1,
									  0,0,  1,0,  1,1, 
									  0,0,  1,1,  0,1,
									  0,0,  1,0,  1,1, 
									  0,0,  1,1,  0,1,});
		useShader = setupShader(); // does all the shader stuff to get the shader working...
		tM = TextureManager.getTextureManager();
		tM.addTexture("tex", "img/testTextur.png");
		tex = tM.getTexture("tex");

The rest of the code with the relevant parts can be found here:


OOOOpppppssss… forgot the rest of the post:

I know you have great knowledge about those things! :wink:
Can someone help me with this? The tutorials I found are just confusing me. And programs like ShaderDesigner and RenderMonkey couldn’t help me either.

Oh… it seems I forgot about another important thing: the shaders! Please excuse my weird behaviour, I can hardly concentrate on anything here because of my kids jumping around my chair all the time and stuff like that.

My vertex shader:

in vec2 TexCoord; 

out vec2 sTexCoords;

void main( void ) {
	gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;
	sTexCoords = TexCoord;

My fragment shader:

uniform float time;
uniform vec2 resolution;
uniform vec2 mouse;
uniform sampler2D u_texture;

in vec2 sTexCoords;

void main( void ) {
	vec4 texColor = texture2D(u_texture, sTexCoords);

	gl_FragColor = texColor;

I am not using all the uniforms in the shaders. I was just experimenting all the time.

Looks like you aren’t enabling and setting the texture coordinate attributes correctly.

you need these.

@Magn919: I don’t want to use fixedFunction but Shaders. And I try to use more modern techniques. After all I have read, I think that the attribute solution is the best.
Thank you anyway, I know my code is not really readable. So it might be hard to see what I am doing. :slight_smile: Especially if I am doing it wrong most of the time.

After all I have now found the mistake.

	public void draw(){
			int attrLoc = GL20.glGetAttribLocation(program, "TexCoord");
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
			GL20.glVertexAttribPointer(attrLoc, 2, false, 0, texCoordBuff.getBuffer());

I misunderstood the index in glEnableVertexAttribArray(index).
It works with:

	public void draw(){
			int attrLoc = GL20.glGetAttribLocation(program, "TexCoord");
			GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);
			GL20.glVertexAttribPointer(attrLoc, 2, false, 0, texCoordBuff.getBuffer());
                   // at the end of draw()...

Now my cube is messed up somehow but I will surely fix that… i hope.

This is only for the old school fixed-function pipeline, and should not be used alongside the programmable pipeline.

Since you are using “in” and “out” I presume you’re trying to use the modern pipeline. So you should prepend your shaders with [icode]#version 330[/icode] and use the “layout” type qualifier to specify the attribute locations. Or, you can target [icode]#version 150[/icode] and use glBindAttribLocation before linking to set the attribute locations. You shouldn’t expect attrib ‘1’ to be texCoord, as that might lead to problems down the road.

More info on versioning here.

The rest of your code looks really messy, no offense. You are mixing shaders and VBOs with fixed-function pipeline (glColor4f, glMultMatrix). You also do some weird things like enabling the texCoord attribute when no VBO is bound. I would recommend sticking to one or the other (ideally programmable pipeline) rather than trying to mix both.

Further: where do you bind the texture?

Thank you davedes! This clears it up a bit.

Yeah, I know, the code is ugly and I am doing weird things there. g This is because of the learning process I have been through, trying this and that and in the end figuring something out. But I was always too lazy too clean up the code.

Hmmm, I will tidy up a bit and then I will come around again and we’ll see if it is better. :slight_smile:

The texture is bound through coincidence. :slight_smile: You see, I am a pro.

I will do better next time.

Next time you should really make sure to clean it up before posting.

Thats why i thought you were using the fixed-function pipeline.

[quote]Next time you should really make sure to clean it up before posting.

Thats why i thought you were using the fixed-function pipeline.
Of course, like I said. I am learning through stupid mistake. So now I have learned for sure. :wink:

After I have tidied up the whole code (ok, you got me! I just wrote it from scratch…) I am now ready to present the still not perfect result. For me this is one of the most exciting things I have typed into my computer. :slight_smile:

I have a main class starting the application:

And this is the quad I am rendering. It does pretty much everything because I have nothing else and this is just a test. I have no movement implemented yet.

This is my my buffer helper class:
I use it in an abstract class by Composition and make VertexBufferObject and TexCoordBufferObject classes with it that do everything needed with these buffers.

I would be very very glad if someone could give me some hints where problems could be or if the code seems ok.

The problem with the texCoords is fixed now. The solution is to use the GL20-functions correctly and at the right places and to use layout in the shaders. :slight_smile: Thanks for your help so far!

i am working on the same stuff atm. i found the 2nd answer at http://gamedev.stackexchange.com/questions/42145/passing-multiple-vertex-attributes-in-glsl-130 to be quite helpfull.

i am far away from a GL guru, but here are some thoughts. please correct me if i am wrong…

the glGetAttribLocation() and glGetUniformLocation() calls could (or should) be done only once and stored after the program got linked.

if you use “location” in the shaders then the glGetAttribLocation() calls can be removed and instead hard-coded indexes could be used.

with VAOs the VBO bindings could be done only once when the VAO gets created.

i hope this makes sense…?

Interesting stuff, thank you very much! I didn’t get the part with the VAOs and VBOs though…


[quote]VAO’s are Vertex Array Objects and allow you to store multiple VBO’s in one VAO. With this ability, we can now store vertex data and colour data in separate VBO’s, but in the same VAO. The same goes for all types of data you usually send through as a VBO, including data for normals or anything that you need on a per-vertex rate.
Aaaaahh, now I get it! Is this really so? I didn’t spend much attention to VAOs. I definitely have to catch up on this! Thanks for the link!

you are welcome :slight_smile:
yeah, i started to understand VAOs with this tutorial. really nice to work with VAOs…

Important! VAOs don’t store the VBOs! They only store which VBOs to read from and how the data is to be interpreted! Deleting or modifying the VBO will have consequences for all VAOs using it.

Of course orphaning helps here.