Skip to main content
GameDev.net gamedev.net
🔒 Locked

glsl transformation matrices

Started by Enalis Jan 17, 2008 at 7:22 PM 6 replies 11k views
Original Post
Enalis
Enalis
Primer: My current algorithm for rendering uses glMultMatrix and pulls the matrix from the physics system and calls that with the matrix from bullet. This works when I'm using the standard pipeline and as far as I understand, I'm in model_view mode in opengl so I'm affecting the opengl modelview matrix for this level in my matrix stack (achieved via push and pop matrix). Question: I'm trying to write my shader library but before I do I was to solidify some things. If I pass in the light position in world space, then the vertex has to be in world space. So my question is, is gl_Vertex in local non transformed object space, and if so will saying gl_ModelViewMatrix * gl_Vertex tranform my vertex to world space? Which I think would mean after I do this vec4 world_vert = gl_ModelViewMatrix * gl_Vertex; vec4 light_dir = world_vert - light_pos_world_space;
Douglas Eugene Reisinger II
Projects/Profile Site
oc2k1
oc2k1
You have to calculate the light position in (model)view Space. It can be done, by transforming the light positions and rotations with the modelviewmatrix, after the camera is set. You should do this calculations before the shader runs, but it is also possible to pass the camera matrix (calculation in modelviewspace) or ModelMatrix (calculation in WorldSpace) to a shader.
Enalis
Enalis
Ok, I've tried several things, but I'd rather just see what you think of this short extrusion shader which kind of shows some of my problems.


// I do the drawing like this// for each objectshader.On();glPushMatrix();glMultMatrix(objects_transform_matrix);Graphics->Primitive(mesh); // a simple draw routine, in this shaders case these would be edge listsglPopMatrix();shader.Off();


// this vertex glsl shader is one I've modified from the web. I'm trying to account for the//   usage of glMultMatrix as most examples are done with static scenes//  The light_position is in world space, so, all I should have to do it tranaform the //   vertex into world space as well, but not projecting it until we send it to be rendered.uniform vec4 light_position;uniform float infinity;void main(void){	vec4 vertex = gl_ModelViewMatrix * gl_Vertex;	vec3 light_direction = normalize(light_position.xyz - vertex.xyz);	vec4 p,q;	p = gl_ProjectionMatrix * (vec4(-light_direction, 0.0) * infinity);	q = ftransform();	float ndl = dot(gl_NormalMatrix * gl_Normal, light_direction.xyz);	if ( ndl >= 0.0 ) p = q;	gl_Position = p;}


Does this make sense to you?
Thanks for the first prompt reply!
Douglas Eugene Reisinger II
Projects/Profile Site
dpadam450
dpadam450
So first a couple things:

gl_Position = blah blah ....wtf?, it should just be = ftransform(); there is no dot product case that will make this position not be ftransform().

In GL, when you set the attributes gl_Lightfv(), you need to call this after the CAMERA transform, not the objects Modelview. Image if you rotate the object 50 and the light 50 with it. Then the lighting will be the same, but you know that if i turn 50 degrees from the sun, that the back of my head will start to light.

That probably didnt make much sense but basically if your behind a soldier, and he turns 90 degrees left, the suns vector in the world doesnt change. But if YOU turn 90 degrees left, then the suns vector relative to you, does change.
NBA2K, Madden, Maneater, Killing Floor, Sims 
yosh64
yosh64
hey

Actually I think it should be fine how it is, assuming the light position is in the correct coordinate space :). I can't comment on the extrusion equation though, as I haven't looked into and made sense of it yet. Anyhows you may want to confirm this with someone else.

But yea, I remember figuring this sorta thing out was a bit tricky in GLSL also, anyhows keep us updated :).

cya

[Edited by - yosh64 on January 18, 2008 7:39:24 PM]
Enalis
Enalis
I finally sorted everything out and made this nifty cool chart:
http://www.element-games.com/palodequeso

Thanks for your help!
Douglas Eugene Reisinger II
Projects/Profile Site
yosh64
yosh64
hey

I just took at look at your chart, anyhows you say that ftransform() translates a vertex into Eye Space, but I don't think this is correct.

Well I think ftransform() is the same as gl_ProjectionMatrix * gl_ModelViewMatrix * gl_Vertex, transforming by the modelview and projection matrix. Thus, I think it would translate the vertex into Screen Space?

I think you are forgetting that when in GLSL, transforming the vertex by the modelview matrix actually transforms into eye space. Remember that the modelview matrix should already have been transformed by the camera's matrix ;). BTW, this is why it's called the modelview matrix, cause it handles both the models, and view ;).

Anyhows I hope this helps, and makes some sense, hehe.

edit
I thought I'd go futher into things, and give an example of the flow of things...

Firstly, when rendering you start by resetting the modelview matrix to the identity matrix. You then transform the modelview matrix by the inverse of your camera's world space matrix (note, ya normally use the handy gluLookAt() instead for handling the camera). You then set the position of your lights in world space via glLight(), and these will automatically be transformed with the modelview matrix into eye space. Finally you then go onto rendering your models by multiplying your models world space matrix with the modelview matrix.

Now we then head on into ya GLSL vertex program, which will be executed for each of ya vertices. Each vertex will be received via gl_Vertex, and will be situated in object space. Also, for lighting equations and such you can access each light via gl_LightSource[n].position, which will be in Eye Space. You then transform the vertex by the modelview matrix to bring it into eye space (remember the modelview matrix is a combination of the inverse of the camera's world space matrix, and the models world space matrix). So the vertex will now be in eye space, you then transform it by the projection matrix to bring it into screen space, and then send it off to the fragment shader via gl_Position.

Now I hope this is all correct, and makes some sense :).

another edit
I just wanted to clear something else up...

That is that the opengl light position is automatically transformed by the modelview matrix, when specified via glLight(). Anyhows rather than trying to explain things I have just updated my previous edit about the flow of things to include lights :).

If you want to look further into this, then please see OpenGL FAQ - Lights and Shadows, and I also quote the following from OpenGL Reference Pages - glLight.
Quote:

The position is transformed by the modelview matrix when glLight is called (just as if it were a point), and it is stored in eye coordinates.


cya

[Edited by - yosh64 on January 19, 2008 6:36:05 AM]
Enalis
Enalis
Excellent, I wish there was a definitive article that describes all of this, maybe once I solidify myself I'll write one.
Douglas Eugene Reisinger II
Projects/Profile Site

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.