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.

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

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.

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…

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.

-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.