# Custom Lighting Transform is Ending Up In the Wrong Space

• 07-18-2014, 05:52 PM
Neil E
Custom Lighting Transform is Ending Up In the Wrong Space
I have sprites and dynamic lights that are passed into a shader as uniforms. Kind of,

Code :

#define MAX_LIGHTS 64
#define AMBIENT 0.08

uniform int number_of_lights;
uniform vec2 world_space_light_position[MAX_LIGHTS];

void main() {
for(int i = 0; i < MAX_LIGHTS; i++) {
if(i < number_of_lights) {
vec2 relative_light_position = world_space_light_position[i] - gl_ModelViewMatrix[3].xy;
shade += vec3(1.0 / (length(relative_light_position) + 1.0));
} else {
break;
}
}
}

I want to move my camera around now but my lights are just ignoring camera movement. I think it's gl_ModelViewMatrix[3].xy that's the problem; I want the world coordinates of the sprite.
• 07-20-2014, 04:30 PM
Neil E
I guess what I'm asking is how to get the world co-ordinates of the vertex.
• 07-21-2014, 04:51 AM
Lolilolight
Quote:

I guess what I'm asking is how to get the world co-ordinates of the vertex.
I suppose that you have the vertex position in view space. (in your shader)

To get the vertex position in world space you have just to multiply your vertex by the inverse of the modelview matrix.
• 07-21-2014, 05:47 AM
carsten neumann
Quote:

To get the vertex position in world space you have just to multiply your vertex by the inverse of the modelview matrix.
Just the inverse of the view matrix, otherwise you are back to model space.
OpenGL normally does not make use of an (explicit) world space, there is just modelspace and viewspace (hence the transformations are lumped together into the modelview matrix). A transformation matrix to/from world space is something you will have to supply manually to your shader (through a uniform variable).
• 07-21-2014, 03:47 PM
Neil E
Fixed
Vertex:

Code :

uniform vec2 sprite_position;
varying vec2 fragment_position;

void main() {
/* this allows for pixel accuracy */
mat2 modelMatrix = mat2(
gl_ModelViewMatrix[0].x, gl_ModelViewMatrix[0].y,
gl_ModelViewMatrix[1].x, gl_ModelViewMatrix[1].y
);
fragment_position = sprite_position + (modelMatrix * gl_Vertex.xy);
gl_Position = ftransform();
}

Fragment:

Code :

uniform vec2 l_pos;
varying vec2 fragment_position;

main() {
vec2 relative = l_pos - fragment_position;
float shade = 1.0 / (length(relative)+1.0);