Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (513)
Games in Android Showcase (121)
games submitted by our members
Games in WIP (577)
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  
  Migrating from fixed-function to shader but getting 'flat' texture colour  (Read 950 times)
0 Members and 1 Guest are viewing this topic.
Offline StrideColossus
« Posted 2012-11-21 17:09:30 »

I've recently been converting my 3D hobby project from using old-school fixed-function pipeline to nice shiny new shaders.  I have the proverbial spinning, textured cube as the guinea-pig.

However I've come up against a brick-wall with an odd problem that I can't seem to diagnose: the cube is there and rotating as it ever was, but I get the same 'flat' colour rather than my nice texture image on each face of the cube.

I've debugged, checked and re-checked my code against various tutorials, threads on this forum, other forums, etc. but can't find anything that helps - yet I'm obviously doing something wrong.

I'm 99% certain that the code that loads and creates the shader and it's associated parameters is working because:
- the colour that I see is actually part of the texture image (I suspect it's 0,0 but can't be certain)
- if I use a different texture image I get a different colour
- if I hack the fragment shader to use some randomly chosen texture coordinate I get a different colour, ditto if I hard-code the colour
- I still have the fixed-function implementation, if I 'switch off' the shader and re-introduce the older code everything works perfectly, so I don't believe it's a problem with the code that loads the texture (and I switch off glEnable for textures for the shader version)
- I've managed to avoid the (apparently) common mistake of using the texture ID in the shader parameter rather than the texture unit.

So I'm left thinking that I'm doing something wrong in the GLSL code, so here it is:

Vertex shader:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
#version 330

layout(location = 0) in vec3 pos;
layout(location = 1) in vec3 normal;
layout(location = 2) in vec2 coords;

uniform mat4 pvm;

out vec2 tc;

void main()
{
    gl_Position = pvm * vec4( pos, 1 );
    tc = coords;
}


Fragment shader:

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
#version 330

uniform sampler2D tex;

in vec2 tc;

out vec4 fragColour;

void main()
{
   fragColour = texture( tex, tc );
}


I've fiddled with the GLSL code, changed the version number, tried varying rather than in/out for the texture coords, used in rather than 3.x style location attributes - no difference.

I'm not going to post the Java code (or at least not yet) since I'm fairly confident it's correct and it's quite 'mature' so I'd spend more time explaining how it's structured.

So I'm not looking for someone to fix the problem for me, but hoping I can get some ideas or pointers for some other things to try as I'm completely out if ideas.  Has anyone had a similar problem?  Or can anyone suggest any ways I could attempt to diagnose what's going on?  I'm stuck Clueless

Any help is greatly appreciated.
Offline Spasi
« Reply #1 - Posted 2012-11-21 19:01:43 »

Have you verified that the "coords" attribute is indeed bound to vertex attrib 2?
Offline theagentd
« Reply #2 - Posted 2012-11-21 20:46:21 »

What kind of texture are you using? How do you set up your vertex attributes? You might be unintentionally normalizing the texture coordinates (unsigned byte of value 1 ---> normalize ---> 1/255).

Myomyomyo.
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline StrideColossus
« Reply #3 - Posted 2012-11-22 15:59:47 »

Have you verified that the "coords" attribute is indeed bound to vertex attrib 2?

... How do you set up your vertex attributes? ...

Thanks guys, the problem was indeed vertex attributes - or rather the lack of them!  I hadn't got around to implementing VAOs as I (stupidly) believed that the default VAO would be sufficient.

Therefore (presumably) the shader wasn't being 'fed' the texture coordinates, once I'd setup a VAO with the attributes specified in the shader it worked (after some messing about).

I'm surprised it worked at all before explicitly adding the VAO code - I guess that the default VAO has vertices set as attribute zero by default but nothing else, which would explain why I was seeing the cube but no texture.

Again thanks for the suggestions, it pointed me in a different direction, and I've now been forced to understand VAOs as well Smiley
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.

theagentd (19 views)
2014-10-25 15:46:29

Longarmx (52 views)
2014-10-17 03:59:02

Norakomi (45 views)
2014-10-16 15:22:06

Norakomi (34 views)
2014-10-16 15:20:20

lcass (39 views)
2014-10-15 16:18:58

TehJavaDev (68 views)
2014-10-14 00:39:48

TehJavaDev (68 views)
2014-10-14 00:35:47

TehJavaDev (60 views)
2014-10-14 00:32:37

BurntPizza (74 views)
2014-10-11 23:24:42

BurntPizza (45 views)
2014-10-11 23:10:45
Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

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