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
This comment has been removed by a blog administrator.
ReplyDelete