Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (499)
Games in Android Showcase (118)
games submitted by our members
Games in WIP (568)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Texture coordinate problem with GLSL  (Read 1675 times)
0 Members and 1 Guest are viewing this topic.
Offline Sparky83

Senior Member


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

Senior Member


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.

My vertex shader:
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;
}


My fragment shader:
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.
Offline Magn919

Junior Member


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.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Sparky83

Senior Member


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. Smiley 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.
Offline 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.

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?

Offline Sparky83

Senior Member


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

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

I will do better next time.
Offline Magn919

Junior Member


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

Senior Member


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

Senior Member


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

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. Smiley Thanks for your help so far!
Offline cubus

Junior Member


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...?
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Sparky83

Senior Member


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...
Offline cubus

Junior Member


Medals: 2



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

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

Senior Member


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

Junior Member


Medals: 2



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

you are welcome  Smiley
yeah, i started to understand VAOs with this tutorial. really nice to work with VAOs...
Offline theagentd
« 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.
Offline ra4king

JGO Kernel


Medals: 347
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.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Pippogeek (39 views)
2014-09-24 16:13:29

Pippogeek (30 views)
2014-09-24 16:12:22

Pippogeek (20 views)
2014-09-24 16:12:06

Grunnt (46 views)
2014-09-23 14:38:19

radar3301 (28 views)
2014-09-21 23:33:17

BurntPizza (64 views)
2014-09-21 02:42:18

BurntPizza (36 views)
2014-09-21 01:30:30

moogie (42 views)
2014-09-21 00:26:15

UprightPath (51 views)
2014-09-20 20:14:06

BurntPizza (54 views)
2014-09-19 03:14:18
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
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
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!