Title: OpenGL and You
1OpenGL and You
2Ray 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
3Nothing 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
4Matrix 2 No nmap this time
- Remember matrices? Theyre 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
5getting 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
6Perspective
- 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
7Example
- glMatrixMode(perspective)
- gluPerspective(xxxx)
y
z
x
8Example
- Sets up a frustum that extends in the z direction
y
eye
z
x
9A different view
y
z x far clipping plane
zx near clipping plane
x
Eye z0 (pretend like its centered)
10Modelview
- 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
11Lets add a sphere
glutSolidSphere will put a sphere at the origin
with a given radius. Well assume 1 for now
12Lets add a sphere
Based on the current viewing frustum, this sphere
will not be visible, so you translate it into the
viewing area
13Lets add a sphere
The modelview matrix used to draw this sphere
reflects this translation
14Cool, 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)
15How 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
16How 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)
17Ray part 2
- Rays have two parts origin and direction
- R0 Rdt
- Origin can be the eye
- Direction will be the pixel you want to determine
minus the origin - For this example, any intersection with tgt1 and
tlt (time to travel to far clipping plane) will
be a valid intersection
18Example
This ray follows the z-axis. R0 is (0,0,0) and
Rd is (0,0,-1)
y
eye
z
x
19Remember this?
If you just try to shoot rays from this eye
towards untransformed geometry, you will never
hit your objects
20Remember 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 tlt1
21Remember this?
The solution is to apply the inverse
transformation to the ray and then test for
intersection. Brilliant!
22Remember this?
The solution is to apply the inverse
transformation to the ray and then test for
intersection. Brilliant!
M-1 ray
23Remember 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 tlt1
24Step 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
25More 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
26The abnormal normal
- Mr. T pities the fool that thinks normals
transform the same way that points do
27How normals work
- Normals do not transform in the same way that
points do - Calculate your normal
- Multiply it by Mt (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
28Final 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 - Theres a lot to work on, so dont get hung up on
anything