# How to determine the object-to-world matrix

I want to ray trace a triangular mesh that I load from a file. The vertices' coordinates are expressed in the reference frame associated with the object (i.e the mesh) and I don't know the object-to-world matrix. Is there a way to calculate this matrix given these coordinates? If no, how can I ensure that the mesh's triangles are transformed to the world's reference frame?

No! If you dont have this info and must have a extra frame, just assume the reference frame is a identity matrix*. The point of the extra reference frame is just to be able to move the object but if you have no info about how the object should relate to other objects it does not really matter operator can move it where they want. Everything is relative, if theres no relation then no problems.

* choosing identity is like choosing a arbitrary constant to be 1, its numerically easy to work with.

• Using the identity matrix is equivalent to placing the object at the origin, with no scaling, and no rotation. – RichieSams Jan 28 '16 at 19:29
• @RichieSams yes, same as putting object to world coordinates (or other parent), except theres a matrix in between to manipulate. – joojaa Jan 28 '16 at 19:46

The Object-To-World matrix is often called "model-matrix".

This martix tells us how the object relates to some "World". This world is relative and arbitrary but really convenient because we can place all our objects and even the camera in it and the math works out pretty well.

The short answer is, that you imagine that the model is defined in the origin of the world, so that the center of gravity of the object lies exactly in the $(0.0, 0.0, 0.0)$ position of the world.

Now if you want to place multiple objects in the same scene, you have to give them a position, and it is really convenient to give all objects a position relative to this world-origin. So you just create a matrix for each object that tells you how you move the object in relation to the world-origin.

For example translating an object is $\begin{bmatrix} 1 & 0 & 0 & x \\ 0 & 1 & 0 & y \\ 0 & 0 & 1 & z \\ 0 & 0 & 0 & 1 \end{bmatrix}$ then your object will have the world-relative position of $(x, y, z)$ (I will omit the useage of homogeneous coordinates which would be better suited for another question).

In a similar fashion you can use translation, scaling and rotation (take care of what order you apply these transformations)

Things get a bit complicated if you want to do camera-space computations and projections and stuff like that, but in the simplest version you can just build your model-matrix this way.

Multiplying each point of your model with this model-matrix gives you the position of the point in world-space (meaning relative to the origin of the world!) and for meshes this moves the whole model just as you would expect.

• Is it correct to think of this model-matrix as a model-to-world matrix? in case I want to transform a point to the coordinate system of the model, then will I have to use the inverse of the model-matrix? – BRabbit27 Jun 2 '16 at 5:24
• Regarding the camera-matrix (built with cam position, lookat point and up vector), can I call it a camera-to-world matrix? Then, if I want to go from world to camera space, will I have to use the inverse of that camera-matrix? – BRabbit27 Jun 2 '16 at 5:27
• The model-matrix is indeed a model-to-world matrix. If you can call the camera-matrix or often view-matrix a "camera-to-world" or "world-to-camera" matrix depends on how you build it i think. I aways use a approach of "world-to-camera" in which the matrix is composed with the coordinate system and the negative position of the camera. Then multiplying by this takes a world coordinate into camera/view space. opengl-tutorial.org/beginners-tutorials/tutorial-3-matrices explains this in a much more detailed fashion and in the way I use the terms and matrices. – Dragonseel Jun 2 '16 at 15:55