1 / 19

A code-walkthrough

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

gore
Télécharger la présentation

A code-walkthrough

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

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

  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. The ‘view’ model Y-axis Viewplane Vector Upward Pointing X-axis VUP VPN View Reference Point Eye View Plane Normal Z-axis

  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 { int numverts; vector_t vert[ MAXVERT ]; int numedges; 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 Y-axis P P’ X-axis E Z-axis World point P is projected into viewplane point P’ along ray toward eye E

  11. Similar triangles P(x,y,z) P’(x’,y’,0) y y’ e -z E(0,0,e) 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., <ESCAPE>-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 <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

  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 The user sees this part of video memory The ‘Visual’ Page CRTC START-ADDRESS Your program draws to this part of video memory 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

  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)

More Related