Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (592)
Games in Android Showcase (168)
games submitted by our members
Games in WIP (645)
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 1185 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:

#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:

#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.
Online 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

« JGO Bitwise Duke »

Medals: 474
Projects: 2
Exp: 8 years

« 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).

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.

deepthought (14 views)
2015-06-30 15:39:44

deepthought (18 views)
2015-06-30 15:39:09

deepthought (23 views)
2015-06-30 15:36:52

Za\'Anzabar (18 views)
2015-06-29 05:44:54

TritonDreyja (35 views)
2015-06-24 17:10:40

CopyableCougar4 (31 views)
2015-06-23 00:34:45

BurntPizza (34 views)
2015-06-21 20:36:46

cookiecompiler (78 views)
2015-06-11 15:42:53

cookiecompiler (43 views)
2015-06-11 15:41:14

NegativeZero (69 views)
2015-06-11 09:49:18
How Do I Expand My Game?
by bashfrog
2015-06-14 11:34:43

List of Learning Resources
by PocketCrafter7
2015-05-31 05:37:30

Intersection Methods
by Roquen
2015-05-29 08:19:33

List of Learning Resources
by SilverTiger
2015-05-05 10:20:32

How to: JGO Wiki
by Mac70
2015-02-17 20:56:16

2D Dynamic Lighting
by ThePixelPony
2015-01-01 20:25:42

How do I start Java Game Development?
by gouessej
2014-12-27 19:41:21

Resources for WIP games
by kpars
2014-12-18 10:26:14 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‑
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!