Java-Gaming.org Hi !
 Featured games (83) games approved by the League of Dukes Games in Showcase (581) Games in Android Showcase (162) games submitted by our members Games in WIP (632) games currently in development
 News: Read the Java Gaming Resources, or peek at the official Java tutorials
Pages: [1]
 ignore  |  Print
 Texture coordinate problem with GLSL  (Read 2176 times) 0 Members and 1 Guest are viewing this topic.
Sparky83

Senior Devvie

Medals: 6
Projects: 1

 « Posted 2013-03-21 13:14:55 »

Hello!

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:
 1  2  3  4  5  6  7  8  9  10  11  12  13  14  15  16  17  18  19  20  21  22  23  24  25  26  27  28  29  30  31  32  33  34  35  36  37  38  39  40  41  42  43  44  45  46  47  48 `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,});      vertBuff.glBufferData();            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:

http://pastebin.java-gaming.org/04394478943

OOOOpppppssss... forgot the rest of the post:

I know you have great knowledge about those things!
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.
Sparky83

Senior Devvie

Medals: 6
Projects: 1

 « Reply #1 - Posted 2013-03-21 14:50:20 »

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.

 1  2  3  4  5  6  7  8 `in vec2 TexCoord; out vec2 sTexCoords;void main( void ) {   gl_Position = gl_ModelViewProjectionMatrix*gl_Vertex;   sTexCoords = TexCoord;}`

 1  2  3  4  5  6  7  8  9  10  11  12 `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.
Magn919

Junior Devvie

Medals: 6
Exp: 4 years

 « Reply #2 - Posted 2013-03-21 15:24:09 »

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

you need these.
glTexCoordPointer
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);

For every new problem, a new source of solutions has come to exist.
Sparky83

Senior Devvie

Medals: 6
Projects: 1

 « Reply #3 - Posted 2013-03-21 16:01:37 »

@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. Especially if I am doing it wrong most of the time.

After all I have now found the mistake.

 1  2  3  4  5  6  7  8  9  10 `   public void draw(){                        [...]         int attrLoc = GL20.glGetAttribLocation(program, "TexCoord");         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);         texCoordBuff.getBuffer().rewind();         GL20.glEnableVertexAttribArray(attrLoc);         GL20.glVertexAttribPointer(attrLoc, 2, false, 0, texCoordBuff.getBuffer());              GL20.glDisableVertexAttribArray(attrLoc);                        [...]   }`

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

 1  2  3  4  5  6  7  8  9  10  11 `   public void draw(){                        [...]         int attrLoc = GL20.glGetAttribLocation(program, "TexCoord");         GL15.glBindBuffer(GL15.GL_ARRAY_BUFFER, 0);         texCoordBuff.getBuffer().rewind();         GL20.glEnableVertexAttribArray(1);         GL20.glVertexAttribPointer(attrLoc, 2, false, 0, texCoordBuff.getBuffer());              [...]                   // at the end of draw()...                   GL20.glDisableVertexAttribArray(1);   }`

Now my cube is messed up somehow but I will surely fix that... i hope.
davedes
 « Reply #4 - Posted 2013-03-21 16:03:58 »

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

you need these.
glTexCoordPointer
glEnableClientState(GL_TEXTURE_COORD_ARRAY);
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
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
 `#version 330`
and use the "layout" type qualifier to specify the attribute locations. Or, you can target
 `#version 150`
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.

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?

Sparky83

Senior Devvie

Medals: 6
Projects: 1

 « Reply #5 - Posted 2013-03-21 16:25:05 »

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.

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

I will do better next time.
Magn919

Junior Devvie

Medals: 6
Exp: 4 years

 « Reply #6 - Posted 2013-03-21 16:28:18 »

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

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

For every new problem, a new source of solutions has come to exist.
Sparky83

Senior Devvie

Medals: 6
Projects: 1

 « Reply #7 - Posted 2013-03-21 16:34:41 »

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.
Sparky83

Senior Devvie

Medals: 6
Projects: 1

 « Reply #8 - Posted 2013-03-21 21:53:56 »

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.

I have a main class starting the application:
http://pastebin.java-gaming.org/43947598347

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.
http://pastebin.java-gaming.org/47937878a4b

This is my my buffer helper class:
http://pastebin.java-gaming.org/9479377874a
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. Thanks for your help so far!
cubus

Junior Devvie

Medals: 2

 « Reply #9 - Posted 2013-03-26 03:49:00 »

hi,
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...?
Sparky83

Senior Devvie

Medals: 6
Projects: 1

 « Reply #10 - Posted 2013-03-26 07:18:16 »

Interesting stuff, thank you very much! I didn't get the part with the VAOs and VBOs though...
cubus

Junior Devvie

Medals: 2

 « Reply #11 - Posted 2013-03-26 07:24:51 »

http://www.swiftless.com/tutorials/opengl4/4-opengl-4-vao.html
Sparky83

Senior Devvie

Medals: 6
Projects: 1

 « Reply #12 - Posted 2013-03-26 07:31:48 »

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!
cubus

Junior Devvie

Medals: 2

 « Reply #13 - Posted 2013-03-26 07:33:48 »

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

« JGO Bitwise Duke »

Medals: 443
Projects: 2
Exp: 8 years

 « Reply #14 - Posted 2013-03-26 15:27:14 »

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.

Myomyomyo.
ra4king

JGO Kernel

Medals: 386
Projects: 3
Exp: 5 years

I'm the King!

 « Reply #15 - Posted 2013-03-27 00:09:51 »

Of course orphaning helps here.

Pages: [1]
 ignore  |  Print

You cannot reply to this message, because it is very, very old.

 MrMapcom (16 views) 2015-05-23 20:26:16 MrMapcom (22 views) 2015-05-23 20:23:34 Waterwolf (29 views) 2015-05-20 15:01:45 chrislo27 (35 views) 2015-05-20 03:42:21 BurntPizza (70 views) 2015-05-10 15:53:18 FrozenShade (56 views) 2015-05-07 09:11:21 TheLopais (218 views) 2015-05-06 13:36:48 TheLopais (202 views) 2015-05-06 13:35:14 TheLopais (207 views) 2015-05-06 13:33:39 TheLopais (226 views) 2015-05-06 13:32:48
 Spasi 32x Riven 16x BurntPizza 16x ra4king 12x theagentd 11x DavidBVal 11x Husk 11x EgonOlsen 11x KevinWorkman 9x Drenius 9x princec 8x scanevaro 8x opiop65 7x Slyth2727 6x revers 6x KaiHH 6x
 List of Learning Resources2015-05-05 10:20:32How to: JGO Wikiby Mac702015-02-17 20:56:162D Dynamic Lighting2015-01-01 20:25:42How do I start Java Game Development?by gouessej2014-12-27 19:41:21Resources for WIP gamesby kpars2014-12-18 10:26:14Understanding relations between setOrigin, setScale and setPosition in libGdx2014-10-09 22:35:00Definite guide to supporting multiple device resolutions on Android (2014)2014-10-02 22:36:02List of Learning Resources2014-08-16 10:40:00
 java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org