OpenGL and You - PowerPoint PPT Presentation

1 / 28
About This Presentation
Title:

OpenGL and You

Description:

Spend some time testing your matrix functions ... Will retrieve the current modelview matrix and store it in mat' ... Why not get the perspective matrix? ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 29
Provided by: Skiz
Category:
Tags: opengl | matrix

less

Transcript and Presenter's Notes

Title: OpenGL and You


1
OpenGL and You
  • I Cast, Therefore I Am

2
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

3
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

4
Matrix 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

5
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

6
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

7
Example
  • glMatrixMode(perspective)
  • gluPerspective(xxxx)

y
z
x
8
Example
  • Sets up a frustum that extends in the z direction

y
eye
z
x
9
A different view
y
z x far clipping plane
zx near clipping plane
x
Eye z0 (pretend like its centered)
10
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

11
Lets add a sphere
glutSolidSphere will put a sphere at the origin
with a given radius. Well assume 1 for now
12
Lets add a sphere
Based on the current viewing frustum, this sphere
will not be visible, so you translate it into the
viewing area
13
Lets add a sphere
The modelview matrix used to draw this sphere
reflects this translation
14
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)

15
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

16
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)

17
Ray 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

18
Example
This ray follows the z-axis. R0 is (0,0,0) and
Rd is (0,0,-1)
y
eye
z
x
19
Remember this?
If you just try to shoot rays from this eye
towards untransformed geometry, you will never
hit your objects
20
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 tlt1
21
Remember this?
The solution is to apply the inverse
transformation to the ray and then test for
intersection. Brilliant!
22
Remember this?
The solution is to apply the inverse
transformation to the ray and then test for
intersection. Brilliant!
M-1 ray
23
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 tlt1
24
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

25
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

26
The abnormal normal
  • Mr. T pities the fool that thinks normals
    transform the same way that points do

27
How 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

28
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
  • Theres a lot to work on, so dont get hung up on
    anything
Write a Comment
User Comments (0)
About PowerShow.com