// Program name: cube3d - Game-Editor Version // Description: Draws a 3D wireframe cube from a database of 3d coordinates // Based on C code from Lee Adams book - // 1988 - High Performance Graphics in C Animation and Simulation // Conversion by: Christopher Hill - TrajectoryLabs.com // First release: 04/30/2006 // Revised: 00/00/0000 // GAME-EDITOR GLOBAL CODE PORTION // D E C L A R A T I O N S ---------------------------------------------------- // Declare Variables and Arrays ----------------------------------------------- float xw=0.0,yw=0.0,zw=0.0; // world coordinates float sx=0.0,sy=0.0; // 3D perspective formulas output float xa=0.0,ya=0.0,za=0.0; // temp 3D formula values float sxa=0.0,sya=0.0,sxb=0.0,syb=0.0; // 2D line endpoints float sxs=0.0,sys=0.0; // temp 2D line startpoints float temp_swap=0.0; // swap variable holder float d=500.0; // angular perspective factor - startval 1200 double r1=5.68319; // yaw angle in radians - startval 5.68319 double r2=6.48319; // roll angle in radians - startval 6.28319 double r3=5.59778; // pitch angle in radians - startval 5.79778 double sr1=0.0,sr2=0.0,sr3=0.0; // sine rotation factors double cr1=0.0,cr2=0.0,cr3=0.0; // cosine rotation factors float mx=-70.0,my=-90.0,mz=-275.0; // viewpoint position - startvals 0.0,0.0,350.0 int maxx=160,minx=-160,maxy=120,miny=-120; // clipping viewport - startvals 638,1,198,1 float screen_x=638,screen_y=199; // screen dimensions - startvals 638, 199 float c=0.0; // line clipping variable float rx=0.0,ry=0.0; // mapping routine scaling values int t1=0,t2=0; // loop counters int p1=0; // array indexer int c0=0,c1=1,c2=2,c3=3,c4=4,c5=5,c6=6,c7=7,c8=8,c9=9,c10=10, c11=11,c12=12,c13=13,c14=14,c15=15; float sx1,sy1,sx2,sy2; float x_res,y_res; // database of xyz cartesian world coordinates for the cube int array1[][3]={ 30,-30, 30, 30,-30,-30, -30,-30,-30, -30,-30, 30, 30,-30, 30, 30, 30,-30, -30, 30,-30, -30,-30,-30, 30,-30,-30, 30, 30,-30, -30, 30,-30, -30, 30, 30, -30,-30, 30, -30,-30,-30, -30, 30,-30, -30, 30, 30, 30, 30, 30, 30,-30, 30, -30,-30, 30, -30, 30, 30, 30, 30, 30, 30, 30,-30, 30,-30,-30, 30,-30, 30, 30, 30, 30, -30, 30,-30, 30, 30,-30, 30, 30, 30, -30, 30, 30, -30, 30, 30, -30, 30,-30, 30, 30,-30, 30, 30, 30, -30, 30, 30, -30, 30,-30}; // F U N C T I O N S ------------------------------------------------------------ // Define Functions ------------------------------------------------------------- // Calculate SIN, COS Factors // Enter with r1,r2,r3 viewing angles of yaw, pitch and roll expressed in // radians (0.0 through 6.28319 // Returns sine and cosine factors void rotation(double r1, double r2, double r3) { sr1=sin(r1); sr2=sin(r2); sr3=sin(r3); cr1=cos(r1); cr2=cos(r2); cr3=cos(r3); } // Standard 3D Formulas // Enter with x,y,z cartesian world coordinates // Returns sx,sy cartesian display coordinates // Returns x,y,z cartesian view coordinates void calc_3d (float xw, float yx, float zw) { xw=(-1)*xw; xa=cr1*xw-sr1*zw; za=sr1*xw+cr1*zw; xw=cr2*xa+sr2*yw; ya=cr2*yw-sr2*xa; zw=cr3*za-sr3*ya; yw=sr3*za+cr3*ya; xw=xw+mx; yw=yw+my; zw=zw+mz; sx=d*xw/zw; sy=d*yw/zw; } // Map Cartesian Coordinates to Physical Screen Coords. // Enter with sx,sy cartesian display coordinates. // returns sx,sy unclipped physical display coordinates. void window (float sx, float sy) { sx=sx+399; //startval +399 sy=sy+299; //startval +299 rx=screen_x/799; //startval 799 ry=screen_y/599; //startval 599 sx=sx*rx; sx=sy*ry; } // 2D Line Clipping // Enter with sxa,sya and sxb, syb line endpoints to be clipped. // Returns line endpoints clipped to screen. void viewport ( float sxa, float sya, float sxb, float syb) { if (sxa>sxb) {temp_swap=sxa;sxa=sxb;sxb=temp_swap; temp_swap=sya;sya=syb;syb=temp_swap;}; if (sxaminx) if (sxb>maxx) return; if (syamaxy) if (syb>maxy) return; if (sxamaxy) if (syb>maxy) return; }; if (sxb>maxx) {{c=(syb-sya)/(sxb-sxa)*(maxx-sxa); sxb=maxx;syb=sya+c;}; if (syamaxy) if (syb>maxy) return; }; if (sya>syb) {temp_swap=sya;sya=syb;syb=temp_swap; temp_swap=sxa;sxa=sxb;sxb=temp_swap;}; if (syamaxy) {c=(sxb-sxa)/(syb-sya)*(maxy-sya); sxb=sxa+c;syb=maxy;}; } // Accepts sx,sy display coordinates and returns scaled sy,sy coordinates void coords(float sx, float sy) { sx=sx*(x_res/640); //Pocket PC screenheight = 320 sy=sy*(y_res/480); //Pocket PC screenwidth = 240 }