Presentation is loading. Please wait.

Presentation is loading. Please wait.

A code-walkthrough Commentary on our ‘wfmodel.cpp’ demo-program – an initial ‘prototype’ for 3D wire-frame animations.

Similar presentations


Presentation on theme: "A code-walkthrough Commentary on our ‘wfmodel.cpp’ demo-program – an initial ‘prototype’ for 3D wire-frame animations."— Presentation transcript:

1 A code-walkthrough Commentary on our ‘wfmodel.cpp’ demo-program – an initial ‘prototype’ for 3D wire-frame animations

2 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

3 The ‘world’ model ( 1, 1, 1) ( 1,-1, 1)(-1,-1, 1) (-1, 1, 1) ( 1, 1,-1)(-1, 1,-1) (-1,-1,-1) ( 1,-1,-1) 8 vertices and 12 edges X-axis Y-axis

4 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

5 Graphics acronyms VRP: View Reference Point VPN: View-Plane Normal VUP: Vector Upward Pointing

6 VUP VPN The ‘view’ model X-axis Y-axis Z-axis Eye Viewplane View Reference Point View Plane Normal Vector Upward Pointing

7 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’

8 Our ‘model’ type #define MAXVERT 50 #define MAXEDGE 50 typedef struct { intnumverts; vector_t vert[ MAXVERT ]; intnumedges; edge_t edge[ MAXEDGE ]; } model_t;

9 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 );

10 Perspective projection P P’ E World point P is projected into viewplane point P’ along ray toward eye E X-axis Y-axis Z-axis

11 Similar triangles y P(x,y,z) E(0,0,e) -z e y’ P’(x’,y’,0) y’ : e = y : e-z

12 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 );

13 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’

14 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., -key was hit), go back and repeat steps 6 through 10; Otherwise, restore text mode and then quit

15 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

16 The ‘flicker’ problem When you hold down the 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

17 ‘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)

18 How page-flipping works VRAM The ‘Visual’ Page The ‘Active’ Page CRTC START-ADDRESS The user sees this part of video memory Your program draws to this part of video memory 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’

19 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 down- arrow 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 ppt "A code-walkthrough Commentary on our ‘wfmodel.cpp’ demo-program – an initial ‘prototype’ for 3D wire-frame animations."

Similar presentations


Ads by Google