<< Schedule Steam (and use your Offpeak bandwidth) | Home | Unity 3D: A rough and ready computation of normals - useful for procedural meshes >>

Unity 3D Immediate Mode: What's the trick with GL.modelview?

Unity 3D's immediate mode is really useful for debugging or adding a bit of chrome to a scene. While it's not the most efficient way of getting something on the screen it's so quick and handy. For those not using Unity 3D Pro. The GL namespace and functionality isn't available to you.

Here's a little tip for setting the GL.modelview matrix so you can pump local space vertices into you GL.Vertex calls and have everything appear in the right spot.

For example if I want to draw a line using model local space I need to setup the modelview matrix so GL primitives appear in the place in our 3D world.

First of all we grab the scene camera using for example (using the C# API) :

GameObject camera = GameObject.Find("Main Camera");

Next we need to compose a matrix that will take into account the scene camera's position and the position of the model we're using. The final trick of composing this matrix is to convert from a Left handed co-ordinate system to a right handed co-ordinate system.

Unity 3D normally uses a Left Handed camera co-ordinate system, where Z is postive leading out of the front of the camera. The underlying rendering system (originally designed on the Macintosh andOpenGL) is a Right Handed System (where Z is negative out of the Camera). The GL.modelview is expected to be in Right Handed.

So to composite the correct modelview matrix we're going to first create a matrix to transform from a left handed to right handed system. We could do:

Matrix4x4 mat = Matrix4x4.identity;
mat[2,2] *= -1.0f;

Now we're ready to go, if we have the camera transform, the model tranforms and our conversion matrix. The result looks like this:

GL.modelview = mat * (camera.transform.worldToLocalMatrix  * transform.localToWorldMatrix);

Now you can issue GL.Vertex3 commands in model local space