OpenGL and You I Cast, Therefore I Am
Ray Casting Idea is simple, implementation takes some work –Cast rays as if you were the camera –Determine intersection of closest shape –Calculate/apply shading –Repeat for all pixels
Nothing to it That really is all there is to it The rest is just math, which can get a little hairy Ray tracing introduces recursive ray casting, but that is for the next assignment
Matrix 2: No nmap this time Remember matrices? They’re back When dealing with matrices, MATLAB is your best friend Spend some time testing your matrix functions 1 or 2 hours in the beginning can save many many many hours later Many
getting the matrix glGetFloatv(GL_MODELVIEW_MATRIX,mat); Will retrieve the current modelview matrix and store it in ‘mat’ Remember, what happens in the modelview matrix, stays in the modelview matrix
Perspective Why not get the perspective matrix? –This creates the viewing frustum through which the world is seen –We can create this on our own –Avoids a matrix multiplication
Example glMatrixMode(perspective) gluPerspective(xxxx) x y z
Example Sets up a frustum that extends in the –z direction x y z eye
A different view Eye z=0 (pretend like it’s centered) z=x near clipping plane z = x’ far clipping plane x y
Modelview Changes in the modelview matrix affect objects in your scene graph zOMG!1! Even gluLookAt is in modelview! You can still think of gluLookAt as moving the camera if that helps you conceptually In terms of matrices, gluLookAt is transforming your geometry around a static camera
Let’s add a sphere glutSolidSphere will put a sphere at the origin with a given radius. We’ll assume 1 for now
Let’s add a sphere Based on the current viewing frustum, this sphere will not be visible, so you translate it into the viewing area
Let’s add a sphere The modelview matrix used to draw this sphere reflects this translation
Cool, now what So if that modelview matrix is used to move that sphere into its position, what would the inverse do? It would perform the opposite move (duh)
How does this apply to rays? When we prepare to shoot rays into the scene, we know a few things –The modelview matrix associated with each object –The eye point (0,0,0) –The perspective we are trying to achieve –The pre-transformed coordinates of our geometry
How to build a ray Since you know the perspective you want, cast the rays from the eye towards the near clipping plane Need to scale your pixels to fit the range of x, y values for your perspective For our version, x will range between -1 and 1, and y will range between -1 and 1 So if you have 512 pixels by 512 pixels, you need to scale pixel (0,0) to be (-1,1) and (512,512) to be (1,-1)
Ray part 2 Rays have two parts: origin and direction R0 + Rd*t Origin can be the eye Direction will be the pixel you want to determine minus the origin For this example, any intersection with t>=1 and t<= (time to travel to far clipping plane) will be a valid intersection
Example x y z eye This ray follows the z-axis. R0 is (0,0,0) and Rd is (0,0,-1)
Remember this? If you just try to shoot rays from this eye towards untransformed geometry, you will never hit your objects
Remember this? If you just try to shoot rays from this eye towards untransformed geometry, you will never hit your objects within the clipping plane Intersection occurs with t<1
Remember this? The solution is to apply the inverse transformation to the ray and then test for intersection. Brilliant!
Remember this? The solution is to apply the inverse transformation to the ray and then test for intersection. Brilliant! M^-1 * ray
Remember this? The solution is to apply the inverse transformation to the ray and then test for intersection. Brilliant! M^-1 * ray Intersection now occurs with t<=1
Step by step, day by day Calculate the matrix for shape M as M Calculate M^-1 Calculate R0, the starting point of the ray Calculate Rd, the direction of the ray Calculate R1, which is R0 + Rd Transform R0 by M^-1 Transform R1 by M^-1 Do perspective division!!! (divide x,y,z by w) Test for intersection with shape M
More steps by steps If there is an intersection, calculate t At each pixel, determine the closest intersection (i.e. lowest valid value of t) Now make the shading calculation based on the normal at that point
The abnormal normal Mr. T pities the fool that thinks normals transform the same way that points do
How normals work Normals do not transform in the same way that points do Calculate your normal Multiply it by M^t (transpose of the modelview) to get it back to the correct space Remember to throw out the translation values Calculate the lighting based on that
Final tips Again, MATLAB is your friend Use it to verify that your calculations are working Start with something small Do not try to raycast your entire image at first, as this will take a long time and will most likely be wrong Try to create a simple building with one floor tile at first and go from there There’s a lot to work on, so don’t get hung up on anything