Computer Science Canada Programming C, C++, Java, PHP, Ruby, Turing, VB   Username:   Password: Wiki   Blog   Search   Turing   Chat Room  Members
vectors in 3d
Author Message
Homer_simpson

Posted: Tue Jun 24, 2003 1:08 am   Post subject: vectors in 3d

so here's the code
 code: class Homer3Dengine     export load, moveobject, drawobject, centerx, centery, obj     const zoom := 100     var centerx, centery : int     type dot :         record             x, y, z : real         end record     proc assign3d (var d : dot, x, y, z : real)         d.x := x         d.y := y         d.z := z + 10     end assign3d     type object :         record             x, y, z, cx, cy, cz : real             maxpoly : int             poly : array 1 .. 1000, 1 .. 3 of dot         end record     var obj : object     proc load (filename : string, lines : int)         var file : int         var x1, y1, z1, x2, y2, z2, x3, y3, z3 : real         open : file, filename, get         obj.maxpoly := lines         for i : 1 .. lines             get : file, x1, y1, z1, x2, y2, z2, x3, y3, z3             assign3d (obj.poly (i, 1), x1, y1, z1)             assign3d (obj.poly (i, 2), x2, y2, z2)             assign3d (obj.poly (i, 3), x3, y3, z3)         end for         for i : 1 .. obj.maxpoly             for ii : 1 .. 3                 obj.poly (i, ii).z += 200             end for         end for     end load     proc rotate (OriginX, OriginY : real, var secondpartX, secondpartY : real, Rotaion : real)         var tempx := (((OriginX - secondpartX) * cosd (Rotaion)) + ((OriginY - secondpartY) * sind (Rotaion)))         var tempy := (((OriginY - secondpartY) * cosd (Rotaion)) - ((OriginX - secondpartX) * sind (Rotaion)))         secondpartY := OriginY - tempy         secondpartX := OriginX - tempx     end rotate     proc findcenter (var x, y, z : real)         x := 0.0         y := 0.0         z := 0.0         for i : 1 .. obj.maxpoly             for ii : 1 .. 3                 x += obj.poly (i, ii).x                 y += obj.poly (i, ii).y                 z += obj.poly (i, ii).z             end for         end for         x /= (obj.maxpoly * 3)         y /= (obj.maxpoly * 3)         z /= (obj.maxpoly * 3)         %drawfilloval (round (obj.cx / (obj.cz / zoom)) + 320, round (obj.cy / (obj.cz / zoom)) + 200, 10, 10, 12)         centerx := round (x / (z / zoom)) + 320         centery := round (y / (z / zoom)) + 200     end findcenter     proc draw3dpolygon (d1, d2, d3 : dot, c, c2 : int)         if d1.z not= 0 and d2.z not= 0 and d3.z not= 0 then             var xx : array 1 .. 3 of int             var yy : array 1 .. 3 of int             xx (1) := round (d1.x / (d1.z / zoom)) + 320             xx (2) := round (d2.x / (d2.z / zoom)) + 320             xx (3) := round (d3.x / (d3.z / zoom)) + 320             yy (1) := round (d1.y / (d1.z / zoom)) + 200             yy (2) := round (d2.y / (d2.z / zoom)) + 200             yy (3) := round (d3.y / (d3.z / zoom)) + 200             Draw.FillPolygon (xx, yy, 3, c)             Draw.Polygon (xx, yy, 3, c2)         end if     end draw3dpolygon     proc drawobject (col, col2 : int)         for i : 1 .. obj.maxpoly             draw3dpolygon (obj.poly (i, 1), obj.poly (i, 2), obj.poly (i, 3), col, col2)         end for     end drawobject     proc moveobject (s : string, d : real)         findcenter (obj.cx, obj.cy, obj.cz)         case s of             label "xy" :                 for i : 1 .. obj.maxpoly                     for ii : 1 .. 3                         rotate (obj.cx, obj.cy, obj.poly (i, ii).x, obj.poly (i, ii).y, d)                     end for                 end for             label "xz" :                 for i : 1 .. obj.maxpoly                     for ii : 1 .. 3                         rotate (obj.cx, obj.cz, obj.poly (i, ii).x, obj.poly (i, ii).z, d)                     end for                 end for             label "yz" :                 for i : 1 .. obj.maxpoly                     for ii : 1 .. 3                         rotate (obj.cy, obj.cz, obj.poly (i, ii).y, obj.poly (i, ii).z, d)                     end for                 end for             label "x" :                 for i : 1 .. obj.maxpoly                     for ii : 1 .. 3                         obj.poly (i, ii).x += d                     end for                 end for             label "y" :                 for i : 1 .. obj.maxpoly                     for ii : 1 .. 3                         obj.poly (i, ii).y += d                     end for                 end for             label "z" :                 for i : 1 .. obj.maxpoly                     for ii : 1 .. 3                         obj.poly (i, ii).z += d                     end for                 end for             label :         end case     end moveobject end Homer3Dengine class Vector3D     export Scale, SetAngleXY, SetAngleXZ, SetAngleYZ, x, y, z, x2d, y2d, conv2d     var relX, relY, relZ : real     var norm : real     var x2d, y2d : int     var x, y, z : real     proc Normalize         norm := (x ** 2 + y ** 2) ** 0.5         x /= norm         y /= norm     end Normalize     proc SetAngleXY (a : real)         x := cosd (a)         y := sind (a)     end SetAngleXY     proc SetAngleXZ (a : real)         x := cosd (a)         z := sind (a)     end SetAngleXZ     proc SetAngleYZ (a : real)         y := cosd (a)         z := sind (a)     end SetAngleYZ     proc Scale (X, Y, Z : real)         x *= X         y *= Y         z *= Z     end Scale     proc conv2d (xx, yy, zz, sc : real)         var xxx := x * sc         var yyy := y * sc         var zzz := z * sc         x2d := round ((xxx + xx) / ((zzz + zz) / 100)) + 320         y2d := round ((yyy + yy) / ((zzz + zz) / 100)) + 200     end conv2d end Vector3D var vector : ^Vector3D new Vector3D, vector var spike : ^Homer3Dengine new Homer3Dengine, spike spike -> load ("engine.raw", 140) colorback (black) cls View.Set ("offscreenonly") spike -> moveobject ("z", 10) vector -> SetAngleXY (0) vector -> SetAngleXZ (0) vector -> SetAngleYZ (0) var ang1, ang2, ang3 : real := 1 var chars : array char of boolean ang2 := 90 ang3 := 90 const movement := 1 spike -> moveobject ("xz", 180) var scz := 1 loop     %    drawline (xx (1), xx (2),     Input.KeyDown (chars)     %ang1 -= movement     if chars ('a') then         ang2 += movement         %spike -> moveobject ("xz", -movement)     end if     if chars ('d') then         ang2 -= movement         %spike -> moveobject ("xz", movement)     end if     if chars ('w') then         ang3 -= movement         %spike -> moveobject ("yz", movement)     end if     if chars ('s') then         ang3 += movement         %spike -> moveobject ("yz", -movement)     end if     if chars (' ') then         scz += 1     end if     spike -> moveobject ("x", vector -> x)     spike -> moveobject ("y", vector -> y)     spike -> moveobject ("z", vector -> z)     spike -> drawobject (2, 10)     vector -> SetAngleXZ (ang2)     vector -> SetAngleYZ (ang3)     vector -> Scale (10, 10, scz)     vector -> conv2d (spike -> obj.cx, spike -> obj.cy, spike -> obj.cz, 100)     drawline (vector -> x2d, vector -> y2d, spike -> centerx, spike -> centery, 12)     View.Update     delay (5)     cls end loop

Catalyst

Posted: Tue Jun 24, 2003 1:12 am   Post subject: (No subject)

very nice of you to share ur code
once im sure school is over ill be posting the code from my 3d engine
Andy

Posted: Tue Jun 24, 2003 10:38 am   Post subject: (No subject)

ya the upload thing is weird, i tried it with Internet Explore and it doesn't work but if i try it with netscape it works...
 Display posts from previous: All Posts1 Day7 Days2 Weeks1 Month3 Months6 Months1 Year Oldest FirstNewest First

Page 1 of 1  [ 3 Posts ]
 Jump to:  Select a forum  CompSci.ca ------------ - Network News - General Discussion     General Forums   -----------------   - Hello World   - Featured Poll   - Contests     Contest Forums   -----------------   - DWITE   - [FP] Contest 2006/2008   - [FP] 2005/2006 Archive   - [FP] 2004/2005 Archive   - Off Topic     Lounges   ---------   - User Lounge   - VIP Lounge     Programming -------------- - General Programming     General Programming Forums   --------------------------------   - Functional Programming   - Logical Programming   - C     C   --   - C Help   - C Tutorials   - C Submissions   - C++     C++   ----   - C++ Help   - C++ Tutorials   - C++ Submissions   - Java     Java   -----   - Java Help   - Java Tutorials   - Java Submissions   - Ruby     Ruby   -----   - Ruby Help   - Ruby Tutorials   - Ruby Submissions   - Turing     Turing   --------   - Turing Help   - Turing Tutorials   - Turing Submissions   - PHP     PHP   ----   - PHP Help   - PHP Tutorials   - PHP Submissions   - Python     Python   --------   - Python Help   - Python Tutorials   - Python Submissions   - Visual Basic and Other Basics     VB   ---   - Visual Basic Help   - Visual Basic Tutorials   - Visual Basic Submissions     Education ----------- - Student Life   Graphics and Design ----------------------- - Web Design     Web Design Forums   ---------------------   - (X)HTML Help   - (X)HTML Tutorials   - Flash MX Help   - Flash MX Tutorials   - Graphics     Graphics Forums   ------------------   - Photoshop Tutorials   - The Showroom   - 2D Graphics   - 3D Graphics     Teams ------ - dTeam Public

 Style: Appalachia blueSilver eMJay subAppalachia subBlue subCanvas subEmjay subGrey subSilver subVereor Search: