OpenGL 3D and Animation for Simulating a Solar System

Download Report

Transcript OpenGL 3D and Animation for Simulating a Solar System

OpenGL 3D and Animation
for
Simulating a Solar System
Assignment 3
CMPS 160
Demo
• My code is in 3 files: main.cc systems.h
systems.cc
• 364 lines of non-base code
• I have multiple systems of planets and
moons (you only need 1 planet and 1
moon to get credit)
New Concepts
• 3 dimensional world
• Complex hidden state
• Full screen animation
• Multiple frames of reference
• Camera vs Object Motion
How do these changes affect my design
compared to the 2D painting program?
3D World
• 3 parameters (x,y,z), duh - glVertex3f(…)
• Using perspective projection instead of
orthographic - gluPerspective(…)
• Objects can occlude (block visibility) one
another, we need a depth buffer to figure out
when this is happening glutInitDisplayMode(…) with GLUT_DEPTH,
glEnable(GL_DEPTH_TEST), glClear(..) with
GL_DEPTH_BUFFER_BIT
Complex Hidden State
For a simple sun, earth, moon simulation we need to track…
Sun:
earth’s orbital rotation state
Earth:
earth’s axial rotation state
moon’s orbital rotation state
Moon:
moon’s axial rotation state
AND on top of that we need up update this state using some
constants for the rate of each rotation and the amount of
time that has passed before we draw the scene each time!
Full Screen Animation
• In painting assignment we only drew one picture.
This program should draw new pictures over and
over as fast as it can.
• All painting goes in the display callback
• Add an idle callback that tells GLUT to git’ drawin’
again - glutPostRedisplay()
• We don’t want the user to watch us drawing so we
draw to a back buffer while user looks at front, then
swap them when we are ready to show them the new
picture - glutSwapBuffers() -- implicitly flushes
• We want to take up the whole screen -- use
glutFullScreen() after glutCreateWindow(…)
Multiple Frames of Reference
(the important part of this assignment)
•
•
•
•
•
Earth rotates around the sun
Earth rotates around its own axis
The Moon rotates around Earth
The Moon rotates around its own axis
OpenGL does all of the hard math for you as
long as you tell it what frame you want to
work in - glTranslate, glRotate, glPushMatrix,
glPopMatrix, glScale…
• Ideally, you can do the whole assignment
without sin() and cos()
Camera vs Object Motion
• OpenGL doesn’t make a distinction here, but
you should.
• Scene is rendered from eye’s frame of
reference.
• Simulation is run from the sun’s frame of
reference.
• Use gluLookAt(…) or other functions to move
the simulated world’s origin with respect to
the eye before drawing the world. This has
same logical effect as moving the eye within
the world.
Camera Setup
• In your reshape callback reset the projection
matrix to be a perspective projection with the
appropriate aspect ratio, nothing more.
• In your display callback, the first thing change
to the modelview matrix should be the
camera’s transformation (with gluLook at),
after that everything (the solar system) you
draw will be relative to the shifted origin so it
looks correct from the eye point. Save the
modelview matrix anytime you think you’ll
need its state back later.
Complex Hierarchical Drawing
Using the Current State
Variables
(easier viewing on your own computer)
glPushMatrix();
drawTower(tower_height);
glTranslate(0,tower_height,0);
glRotate(arm_angle,1,0,0);
drawControlBox();
drawArm(arm_length);
glTranslate(0,0,arm_length);
glPushMatrix();
glRotate(pulley_angle,0,1,0);
drawPulley();
glPopMatrix();
glTranslate(-cable_length,0,0);
drawBarrel();
glPopMatrix();
glPushMatrix();
glTranslate(first_barrel,0,0);
for(I=0; I<4; I++) {
drawBarrel();
glTranslate(barrel_spacing,0,0);
}
glPopMatrix();
arm_length
pulley_angle
arm_angle
cable_length
tower_height
x
firsrt_barrel
Origin
z
y
barrel_spacing
“I still don’t really feel like I
understand OpenGL in general”
• At other schools there are whole classes on
OpenGL or at least a few lecture sessions
about it
• cmps160 is more concept-based, you only
learn to program with OpenGL in the lab.
• I learned OpenGL myself from examples and
documentation. However, if you learn best
through colorful PowerPoint presentations,
here is a good starter covering everything up
through this assignment:
http://www.cs.virginia.edu/~gfx/Courses/2004/Intro.Spring.04/Le
ctures/lecture04.ppt
Base Code
• You write main.cc from scratch this time
(really!). Copying the old main.cc is fine, but
you’ll still have to make some major changes
to the contents of your callbacks.
• We provide two functions (in helper.h) to
make your solar system pretty if you desire:
• helperSetupLighting() -- call this in main after
you have registered the callbacks
• helperPlaceLight() -- call this when you are
drawing the scene in the display callback -places the sun’s lighting at the current origin