|
||||||||||||||||||
Add Forum to Favorites | Send Topic To a Friend | View Forum FAQ | Track this topic |
Last Thread Next Thread ![]() |
| glsl transformation matrices |
|
![]() Enalis Member since: 10/16/2001 From: Harrisburg, PA, United States |
||||
|
|
||||
| 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; |
||||
|
||||
![]() oc2k1 Member since: 4/23/2007 From: Bremen, Germany |
||||
|
|
||||
| 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. Lumina platform independent GLSL IDE |
||||
|
||||
![]() Enalis Member since: 10/16/2001 From: Harrisburg, PA, United States |
||||
|
|
||||
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 object shader.On(); glPushMatrix(); glMultMatrix(objects_transform_matrix); Graphics->Primitive(mesh); // a simple draw routine, in this shaders case these would be edge lists glPopMatrix(); 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! |
||||
|
||||
![]() dpadam450 Member since: 11/18/2005 From: Silver Spring, MD, United States |
||||
|
|
||||
| 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. |
||||
|
||||
![]() yosh64 Member since: 7/11/2007 |
||||
|
|
||||
| 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 Member since: 10/16/2001 From: Harrisburg, PA, United States |
||||
|
|
||||
| I finally sorted everything out and made this nifty cool chart: http://www.element-games.com/palodequeso Thanks for your help! |
||||
|
||||
![]() yosh64 Member since: 7/11/2007 |
||||
|
|
||||
| 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: cya [Edited by - yosh64 on January 19, 2008 6:36:05 AM] |
||||
|
||||
![]() Enalis Member since: 10/16/2001 From: Harrisburg, PA, United States |
||||
|
|
||||
| Excellent, I wish there was a definitive article that describes all of this, maybe once I solidify myself I'll write one. |
||||
|
||||
All times are ET (US)![]() |
Last Thread Next Thread ![]() |
|