Retrieving a direction vector from the modelview matrix

Hi all, I’m trying to get comfortable with matrix manipulation and stuff, so I thought I’d have a go at backface culling an object thats been transformed and rotated by the modelview matrix.

I have the viewing position set at the default 0,0,0 looking down the –z axis, this stays put and is not transformed.

If I use the code below to cull the backfaces this works fine when the object is drawn in its default position, but obviously when I rotate the object the viewPos vector has no way of knowing how the object has been transformed, say for example I’m displaying a ball, alls I’m getting is a half ball rotating around.

viewPos.set (0, 0, -1);
dot(viewPos, vertexNormal);
if(dot < 0) draw face

I’m guessing I need to perform some magic with the modelview matrix and the viewPos vector but I’m not sure what exactly, I’ve tried setting the viewPos to x =ModelMatrix.[8], y=ModelMatrix.[9], z=-ModelMatrix.[10], this culls the backfaces if I rotate on X while the other rotation angles are set to 0 and likewise, but as soon as more than two rotation angles are none 0 it all goes wrong.

So anyone got any ideas of what I need to do?

Cheers

I would define the position/orientation of the object using a world-space matrix. Now the object geometry is usually defined within the local model-space of the object, so I believe if you just transform the vertex normals by the current world-space matrix of the object, and continue with your original method for culling, it should work - I think.

Ahhh, cheers protonovus, as soon as I read “vertex normals” it all clicked. I’ve been trying to mul the view vector by the objects modelview matrix, but it turns out I needed to mul the vertex normal by the modelview matrix.

It’s a lot of transforms though, as I’m essentially doing one transform on hardware, then another on the CPU for every face of the object. I’m probably well out here (I’m new to matrix maths), but I’m just wondering if there is a way to do it with the view vector, so rather then calculating the transformed normal for every vertex I can just calculate a new view vector after the object has been transformed?

I’ll blindly stab about and see what happens, thanks again.