world coordinates to object space

Hi everyone,

how can i calculate the position of a point from world coordinates to object space over the modelviewmatrix? I need the position in a vertex shader.
I tried several hours now, but i just dont get it. :frowning:

I thought it would be the the inverse modelviewmatrix multiplicated by the position, but it doesnt work.

i hope you can help me…

I don’t get it… within a vertex shader you usually start with the object space coordinates. Multiplying these object space coordinates gives you the eye space coordinates and multiplying the eye space coordinates with the projection matrix gives you the window space coordinates.

N.

i have set the light position in the main program and hand it over to the shader. and in the shader i need to calculate the lightposition relative to the vertex position. its a lighting vertex shader written in cg…

Well, that all depends on what coordinate system you specified the light position in, doesn’t it?

N.

The light position is specified in an array in world space coordinates. I dont use opengl itself to calculate the light. i want to add phong lighting via the vertex shader to the objects.

It depends on your definition of world space. If it’s the same as eye space you need to multiply the vertex position with the modelview matrix (not the inverse one) to get it in the same coordinate system as the light position.

N.

Thanks for your fast replies :).
I will try to keep in mind what you wrote and get the program to work hopefully…

-NiCo- may have answered your question, but just in case you’re still a little fuzzy on this…


+--------------+                             +--------------+
| Object Space | -> Modeling Transform   ->  | World Space  |
+--------------+                             +--------------+
+--------------+                             +--------------+
| World  Space | -> Viewing Transform   ->   | Eye Space    |
+--------------+                             +--------------+
+--------------+                             +--------------+
| Object Space | -> Modelview Transform ->   | Eye Space    |
+--------------+                             +--------------+

The modelview matrix gets you from object space to eye space (use the inverse to get back). It doesn’t know about world space.

The only way you can use modelview to get back to world space is if your object space and world space are the same (i.e. your modeling transform is the identity matrix – i.e. a no-op). in other words, if the position vertex data for all objects in your scene are specified in a common coordinate system.

You’re probably going to find it simpler to pre-transform your light to eye space in your app and pass that into your shader, so you don’t have to think about world space in your shader.