oreilly.comSafari Books Online.Conferences.


AddThis Social Bookmark Button

Build a Simple 3D Pipeline in Tcl
Pages: 1, 2, 3

Projecting 3D onto the 2D Game Console

The last component of our simple 3D pipeline is the 2D projection to our game console viewport. The procedure create_rotation_matrix_R returns a matrix, R, that represents the current transformed orientation of our polygon in world coordinates. Now we must pass in the vertices of the polygon and calculate the new x', y', and z' values.

The x', y', and z' values represent the vertex points in the orientation in which we would like to draw them on the game console. Now we need to project the 3D world coordinates onto the 2D game console viewport. Interestingly enough, most game tutorial sites spend a lot of character bytes on explaining the arbitrary matrix rotation described above, and mention nothing about the projection matrix.

For our projection transformation, we're going to pass in the matrix R we created with the arbitrary rotation procedure. In the back of your mind just remember this: R=P. This is what the projection procedure looks like:

proc gc_projection { x y z d P } { 
global gc_width 
global gc_height 
set xprime \ 
        [expr {[lindex $P 0 0]*$x+\ 
            [lindex $P 1 0]*$y+[lindex $P 2 
set yprime \ 
        [expr {[lindex $P 0 1]*$x+\ 
            [lindex $P 1 1]*$y+[lindex $P 2 
set zprime\ 
        [expr \ 
            {([lindex $P 0 2]*$x+\ 
            [lindex $P 1 2]*$y+[lindex $P 2 2]*$z 
+ 10)}] 
set aspect_ratio [expr $gc_width / $gc_height] 
set xp [expr ($gc_width / 2) + ($xprime * $d / $zprime)] 
set yp [expr ($gc_height /2 ) + \ 
($aspect_ratio * $yprime * $d / $zprime)] 
return [list $xp $yp] 

The aspect ratio is the ratio of the game console's width in pixels, divided by the game console's height in pixels. The projection and aspect ratio equations are covered in the book 3D Math Primer for Graphics and Game Development, by Fletcher Dunn.

The following lines of code convert the polygon from world coordinates to game console (viewport) coordinates.

set R [create_rotation_matrix_R $theta_Rx $theta_Ry 
set d 2000 
set display_list {} 
foreach vertex $vertex_list { 
    set x [lindex $vertex 0] 
    set y [lindex $vertex 1] 
    set z [lindex $vertex 2] 
    set screen_data [gc_projection $x $y $z $d $R] 
    puts "screen data($screen_data)" 
    lappend display_list $screen_data 

This code creates a display list of 2D game console coordinates that represents the polygon points, projected to 2D, that we want to render. That's it! Pretty simple pipeline for learning 3D.

Virtual Game Console

We are now able to prototype game-graphics algorithms on any computer system that runs Tcl. Tcl is a great tool for playing with graphics algorithms. You can test an idea on the fly, or examine and debug values in the interpreter. Once you get a handle on the algorithm, you can go code it in C or C++. At work, I can use my Sun workstation, and when I get home, I usually do my work exclusively on my PowerBook. For compatibility, I will SSH into my SGI from my PowerBook. The code works on all the above platforms.

The pipeline should be simple enough to expand upon. Add a 3D object, such as a cube, using polygons, or finish the code to display a three-sided pyramid. Rotate the objects and add a light source. And by all means--have fun!

Example Code

Michael J. Norton is a software engineer at Cisco Systems.

Return to the Mac DevCenter