A code-walkthrough Commentary on our ‘wfmodel.cpp’ – an initial ‘prototype’ demo-program

advertisement
A code-walkthrough
Commentary on our ‘wfmodel.cpp’
demo-program – an initial ‘prototype’
for 3D wire-frame animations
The ‘cube.dat’ file
• This is a textfile (created with an editor)
• It is organized into two sections:
– Coordinates of points in 3-dimensional space
– Number-pairs indicating endpoints of lines
• Each section begins with a integer, telling
how many data-elements will follow
• First section holds floating-point triples
• Second section holds integer pairs
The ‘world’ model
Y-axis
(-1, 1, 1)
(-1, 1,-1)
( 1, 1, 1)
( 1, 1,-1)
X-axis
(-1,-1,-1)
( 1,-1,-1)
(-1,-1, 1)
( 1,-1, 1)
8 vertices and 12 edges
Data structures
typedef struct { float x, y, z; } vector_t;
- Used for locating points in 3-space
- Also used for representing vectors
typedef
struct { vector_t vrp, vpn, vup; } camera_t;
- Used to specify crucial viewing directions
Graphics acronyms
• VRP: View Reference Point
• VPN: View-Plane Normal
• VUP: Vector Upward Pointing
The ‘view’ model
Y-axis
Viewplane
Vector Upward Pointing
X-axis
VUP
VPN
View Reference Point
Eye
View Plane Normal
Z-axis
More structures…
typedef int edge_t [ 2 ];
- Used to specify a line-segment (by means
of its two end-points)
- We will be using an array of such ‘edges’
Our ‘model’ type
#define MAXVERT 50
#define MAXEDGE 50
typedef struct {
int numverts;
vector_t vert[ MAXVERT ];
int numedges;
edge_t edge[ MAXEDGE ];
} model_t;
Some ‘helper’ functions
int get_model_data( char *filename,
model_t model );
double dot_product( vector_t p, vector_t q );
void normalize( vector_t &v );
void cross_product( vector_t u,
vector_t v, vector_t &w );
Perspective projection
Y-axis
P
P’
X-axis
E
Z-axis
World point P is projected into viewplane point P’ along ray toward eye E
Similar triangles
P(x,y,z)
P’(x’,y’,0)
y
y’
E(0,0,e)
e
y’ : e = y : e-z
-z
The ‘draw_model()’ function
void draw_model( model_t model,
camera_t camera,
float3_t eye );
- It uses these ‘helper’ functions:
void draw_pixel( int x, int y, int color ),
draw_line( int x1, int y1, int x2, int y2, int color ),
fill_rectangle( int x, int y, int h, int v, int color );
Algorithm for ‘main()’
• Preliminary steps (to set up for the demo):
1) setup filename (for the wire-frame data)
2) read the data into our ‘model’ structure
3) setup i/o permissions, vram mapping,
noncanonical input, and graphics mode
4) draw a screen border (as confirmation)
5) setup variables for ‘camera’ and ‘eye’
The demo’s main loop
6) Adjust the location of the viewer’s eye
7) Setup the camera and view parameters
8) Compute the 3D perspective projection
9) Erase old image and draw the new one
10) Then wait for the user to press a key
Unless done (i.e., <ESCAPE>-key was hit),
go back and repeat steps 6 through 10;
Otherwise, restore text mode and then quit
The ‘draw_model()’ function
• 1) setup axes for a new coordinate-system
based on camera-angles and viewer’s eye
• 2) transform the model’s vertices into their
coordinates relative to these new axes
• 3) perform a perspective projection of the
points in 3-space onto the 2D view-plane
• 4) for each edge, draw the line-segment
that joins the images of its two end-points
The ‘flicker’ problem
• When you hold down the <ENTER> key
while running our ‘wfmodel’ demo, you see
annoying image-flicker as the cube rotates
• The visible image is erased and redrawn,
but the redrawing takes considerable time
• For smooth flicker-free animation, we need
to avoid doing the lengthy redrawing while
the partially-drawn image remains visible
‘page-flipping’
• We can utilize off-screen video memory to
eliminate the problem of image flickering
• The technique is called ‘page-flipping’
• We have created a demo-program (named
‘vramscan.cpp’) that lets you take a look at
the entire contents of video display ram
• It works by changing one of the Radeon’s
CRT Controller registers: CRT_START
• (So it won’t run on other vendors’ SVGAs)
How page-flipping works
The ‘Visual’ Page
The user sees
this part of
video memory
CRTC START-ADDRESS
The ‘Active’ Page
When the ‘Active’ page is
ready to be viewed, your
program changes the
CRTC START-ADDRESS
and thus the roles of these
two pages are ‘flipped’
VRAM
Your program
draws to this
part of video
memory
In-class exercises
• Try modifying the ‘wfmodel.cpp’ program (so as
to allow for a greater degree of ‘user control’)
• Specifically, let the user move the viewer’s eye
higher or lower, by pressing up-arrow or downarrow on the keyboard
• Apply the ideas from our ‘animate2.cpp’ demo to
add animation (by removing the keyboard input
delay from the main loop), but let the user still be
able to exert control (via asynchronous keyboard
input notification and a signal-handler function)
Download