   OpenGL & Cg Shaders: getting vertex world position
  Probably the millionth time that this has been asked, but I haven't found a working answer though.I use OpenGL + Cg shaders. So far I always made the shaders for static geometry where the vertices are already in world-space. But now I have objects that can be moved/scaled/rotated. I could transform (light)positions into the local object space, but I figured out that a reverse approach might be more practical in my case.Thus, how to convert a vertex position/normal(and tangent) to world space, so I can use them with reflection cubeMaps, light positions, and so on? Ussually I see something likeworldPos = worldMatrix * vertexPos;But that is DirectX code right? If I'm right, OpenGL doesn't have this world matrix directly available. I tried the same with the modelView and modelView Inverse matrix, but no proper results so far.Greetings,Rick   
Quote:Original post by spekUsually I see something likeworldPos = worldMatrix * vertexPos;But that is DirectX code right? If I'm right, OpenGL doesn't have this world matrix directly available.So why not make it available yourself? You surely know the world/model matrix in your C++/whatever code, so send it to shader as a uniform matrix.Quote:I tried the same with the modelView and modelView Inverse matrix, but no proper results so far.That will give you view space, not world space. Although, as long as you perform all your calculations in the same space, it shouldn't matter whether it is model, world or view space.   ;  I do have the matrix that is used to transform the object:glPushMatrix;glMultMatrix( m );--> pass m to shaderobject.render;glPopMatrix;...vertex shader...worldPos = mul( in.pos, m );I passed 'm' and multiplied it with the vertex. I think it works, but I'd like to check if this is the proper way. Can I also get the world normal in the same way? My test subject is still very limited, so its hard to check if I get the proper results.I ran into another little problem here, although that is a Cg specific question. I know how to pass vector parameters and array's of them (cgGLSetParameterArray4f, ...), but how to pass matrices (float4x4)? Currently I pass them as an array of 4 float4, and reconstruct the matrix in the vertex shader. It works, but it doesn't look really nice.Thanks!Rick   ;Quote:Original post by spekI do have the matrix that is used to transform the object:glPushMatrix;glMultMatrix( m );--> pass m to shaderobject.render;glPopMatrix;...vertex shader...worldPos = mul( in.pos, m );I passed 'm' and multiplied it with the vertex. I think it works, but I'd like to check if this is the proper way.Yes, this is the world/model matrix.Quote:Can I also get the world normal in the same way?You use the same matrix, but you need to transform it differently - I don't have the code off the top of my head.Quote:I ran into another little problem here, although that is a Cg specific question. I know how to pass vector parameters and array's of them (cgGLSetParameterArray4f, ...), but how to pass matrices (float4x4)? Currently I pass them as an array of 4 float4, and reconstruct the matrix in the vertex shader. It works, but it doesn't look really nice.There should be a way to upload a matrix directly, but I am afraid I haven't used CG in years.   