Stumped by World Coordinates in GLSL

I have a 30x30 Quad rotated 90 DEG over the UNIT_X axis with the following shader applied to it:



Vertex

[java]uniform mat4 g_WorldViewProjectionMatrix;

uniform mat4 g_WorldMatrix;

attribute vec3 inPosition;

varying vec4 texCoordProj;



void main(){



vec4 vec4Position = vec4(inPosition, 1.0f);

texCoordProj = (vec4Position * g_WorldMatrix);

gl_Position = g_WorldViewProjectionMatrix * vec4Position;

}[/java]



Fragment

[java]varying vec4 texCoordProj;



uniform mat4 g_WorldMatrix;



void main(){





float comp = (texCoordProj.x)/30;

vec4 element;

if(comp < 1 && comp > 0){

element = vec4(comp,0,0,1);

}

else if(comp >= 1){

element = vec4(0,1,0,1);

}

else{

element = vec4(0,0,1,1);

}

gl_FragColor = mix(vec4(1), element, 0.5);

}[/java]



I expect the coloring of the quad to change if I move it in the X direction, because the coloring is based on the world coordinate [ texCoordProj = (vec4Position * g_WorldMatrix);]. But when I move it around the coloring remains static on the quad.



What am I missing here?

You have to multiply it like this:

g_WorldMatrix * vec4Position



Also, make sure you expose WorldMatrix in your material definition as otherwise it gets filled with NaNs

Awesome, that was it.



I’d tried that, but there was a typo somewhere when I did. Because I didn’t get the required result (due to the typo) I’d thrown away this solution. Thanks.