Viewing and Lighting in OpenGL

trexpeeverSoftware and s/w Development

Dec 13, 2013 (3 years and 8 months ago)

101 views

1





Viewing and Navigating

using

OpenGL

2





The LookAt Function

0
The GLU library contains the function gluLookAt to form the required
modelview matrix through a simple interface

0
Note the need for setting an up direction

0
Still need to initialize

-
Can concatenate with modeling transformations

0
Example: isometric view of cube aligned with axes

glMatrixMode(GL_MODELVIEW):

glLoadIdentity();

gluLookAt(1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0., 1.0. 0.0);

3





gluLookAt

glLookAt(eyex, eyey, eyez, atx, aty, atz, upx, upy, upz)

4





Outside
-
In Viewing

Object is centered. Observer navigates on a sphere
surrounding the object.

5





Inside
-
Out Viewing

Viewer centered. Observer navigates, looks toward a point

On the sphere. Sphere center moves with observer.

6





Inside
-
Out Viewing

Viewer centered. Observer navigates, looks toward a point

On the sphere. Sphere center moves with observer.

7





Some Spherical Geometry

x

y

z

r

f

q

(x,y,z)

q
f
q
q
f
cos
sin
sin
cos
cos
r
z
r
y
r
x




8





Viewing: Outside
-
In

x

y

z

(x,y,z)

gluLookAt(x,y,z,0,0,0,0,1,0);

q
f
q
q
f
cos
sin
sin
cos
cos
r
z
r
y
r
x




9






Outside In Viewing

Sample Code

glLoadIdentity(); // start with an identity in the MODELVIEW transformation


x = radius * cos(theta) * cos(phi);

y = radius * sin(theta);

z =
-
radius * cos(theta)* sin(phi);


gluLookAt(x,y,z,0,0,0,0,1,0); //multiply viewing component of transformation


glutSolidTeapot(1.0); // draw what is being looked at (around origin)

10







Viewing: Outside In


Possible Navigation Key Mapping

0
GLUT_KEY_LEFT
increase phi

0
GLUT_KEY_RIGHT
decrease phi

0
GLUT_KEY_UP
increase theta

0
GLUT_KEY_DOWN
decrease theta

0
GLUT_KEY_PAGE_UP
increase radius

0
GLUT_KEY_PAGE_DOWN
decrease radius

11





Viewing: Inside Out, from Origin

x

y

z

(x,y,z)

gluLookAt(0,0,0,x,y,z,0,1,0);

q
f
q
q
f
cos
sin
sin
cos
cos
r
z
r
y
r
x




12





Viewing: Inside Out, from Arbitrary Point

q
f
q
q
f
cos
sin
sin
cos
cos
0
0
0
r
z
z
r
y
y
r
x
x






X’

Y’

z’

(x,y,z)

gluLookAt(x
0
,y
0
,z
0
,x,y,z,0,1,0);

)
,
,
(
0
0
0
z
y
x
13





Inside Out Viewing

Sample Code

glLoadIdentity(); // start with an identity in the MODELVIEW transformation


x = radius * cos(theta) * cos(phi);

y = radius * sin(theta);

z =
-
radius * cos(theta)* sin(phi);


gluLookAt(xo,yo,zo,xo+x,yo+y,zo+z,0,1,0); //multiply viewing transformation


glutSolidTeapot(1.0);

14







Viewing: Inside Out


Possible Navigation Key Mapping

0
GLUT_KEY_LEFT
increase phi (look left)

0
GLUT_KEY_RIGHT
decrease phi (look right)

0
GLUT_KEY_UP
increase theta (look up)

0
GLUT_KEY_DOWN
decrease theta (look down)

0
GLUT_KEY_PAGE_UP
walk in direction of view

0
GLUT_KEY_PAGE_DOWN
walk away from direction of view