include/df/g3.hpp

00001 #define G3_DOT  0  //coloured dot
00002 #define G3_LINE 1  //coloured line
00003 #define G3_TRI  2  //coloured triangle
00004 #define G3_TTRI 3  //texture triangle (not implemented yet!)
00005 
00006 #define G3_MAX_TYPE 3
00007 
00008 #define G3_MAX_ANGLE      4095     //maximum angle  (4096 degrees)
00009 
00010 //Object Flags
00011 //WND_VISIBLE
00012 
00013 #define G3_MAX_INTENSITY 31     //32 shades of each colour palette
00014 
00015 //the following are memory objects and must be tight!
00016 #pragma pack(push,1)
00017 
00018 //DF/1.9.0.2 - recent changes in gcc require these forward declarations
00019 class G3Light;
00020 class G3Buffer;
00021 class G3ZBuffer;
00022 class G3Scene;
00023 class G3Object;
00024 
00025 struct Point3D {  //g3_pts
00026   int32 x, y, z;
00027   int32 f;    //flags (bit0=normal vector)
00028   Point3D() : f(0) {};  //init flag
00029 };
00030 
00031 struct G3_def {  //generic header for objects in a object list
00032   uint8 typ;  //type
00033   uint32 clr; //solid clr
00034   uint32 p1;
00035   uint32 p2;
00036   uint32 p3;
00037   uint32 pn;  //normal (for triangles)
00038 };
00039 
00040 struct G3_defx {  //generic header for objects in a object list (transformed)
00041   uint8 typ;  //type
00042   union {
00043     struct {
00044       uint8 r, g, b, R;  //R=reserved (Zero)
00045     }
00046     clr_;
00047     uint32 clr;
00048   };
00049   Point3D p1;
00050   Point3D p2;
00051   Point3D p3;
00052   Point3D pn;
00053 };
00054 
00055 #define G3_dot G3_def
00056 /*dot
00057 struct G3_dot {  //g3_dots (a DOT object)
00058   uint8 typ;
00059   uint32 clr;
00060   uint32 p1;  //point #1 (index into vertixes)
00061   G3_dot() : typ(G3_DOT) {}; //init flgs
00062 };
00063 */
00064 
00065 #define G3_dotx G3_defx
00066 /*dot (expanded)
00067 struct G3_dotx {  //g3_dots (a DOT object)
00068   uint8 typ;
00069   uint32 clr;
00070   Point3D p1;  //point #1 (transformed coords)
00071   G3_dotx() : typ(G3_DOT) {}; //init flgs
00072 };
00073 */
00074 
00075 #define G3_line G3_def
00076 /*line
00077 struct G3_line {  //g3_lines (a Line object)
00078   uint8 typ;
00079   uint32 clr;
00080   uint32 p1;  //point #1 (index into vertixes)
00081   uint32 p2;  //point #2 (index into vertixes)
00082   G3_line() : typ(G3_LINE) {}; //init flgs
00083 };
00084 */
00085 
00086 #define G3_linex G3_defx
00087 /*line (expanded)
00088 struct G3_linex {  //g3_lines (a Line object)
00089   uint8 typ;
00090   uint32 clr;
00091   Point3D p1;  //point #1 (transformed coords)
00092   Point3D p2;  //point #2 (transformed coords)
00093   G3_linex() : typ(G3_LINE) {}; //init flgs
00094 };
00095 */
00096 
00097 #define G3_tri G3_def
00098 /*triangle
00099 struct G3_tri {  //g3_tris (a Triangle object)
00100   uint8 typ;
00101   uint32 clr;
00102   uint32 p1;  //point #1 (index into vertixes)
00103   uint32 p2;  //point #2 (index into vertixes)
00104   uint32 p3;  //point #3 (index into vertixes)
00105   uint32 pn;  //Normal of plane (length must = 256)
00106   G3_tri() : typ(G3_TRI) {}; //init flgs
00107 };
00108 */
00109 
00110 #define G3_trix G3_defx
00111 /*triangle (expanded)
00112 struct G3_trix {  //g3_tris (a Triangle object)
00113   uint8 typ;
00114   union {
00115     struct {
00116       uint8 r,g,b,R;  //R=reserved (Zero)
00117     }clr_;
00118     uint32 clr;
00119   };
00120   Point3D p1;  //point #1 (transformed coords)
00121   Point3D p2;  //point #2 (transformed coords)
00122   Point3D p3;  //point #3 (transformed coords)
00123   Point3D pn;  //Normal of plane (length must = 256) (transformed coords)
00124   G3_trix() : typ(G3_TRI) {}; //init flgs
00125 };
00126 */
00127 
00128 /*
00129 //triangle w/texture
00130 struct G3_ttri {  //g3_trixs (a Triangle object)
00131   uint8 typ;
00132   uint32 clr;
00133   uint32 p1;  //point #1 (index into vertixes)
00134   uint32 p2;  //point #2 (index into vertixes)
00135   uint32 p3;  //point #3 (index into vertixes)
00136   uint32 pn;  //Normal of plane (length must = 256)
00137   uint32 ta;  //Texture offset
00138   uint32 tax; //Size
00139   uint32 tay; // "
00140   uint32 tb;  //Texture offset
00141   uint32 tbx; //Size
00142   uint32 tby; // "
00143   G3_ttri() : typ(G3_TRIX) {}; //init flgs
00144 };
00145 */
00146 
00147 #pragma pack(pop)
00148 
00149 //light
00150 struct G3Light : virtual public SystemCode {    //g3_lightsrcs
00151   union {
00152     struct {
00153       uint8 r, g, b, R;
00154     }
00155     clr_;
00156     uint32 clr;    //intensity
00157   };
00158   uint32 x;      //x/y/z
00159   uint32 y;
00160   uint32 z;
00161   uint32 xang;   //rotation angle
00162   uint32 yang;
00163   uint32 zang;
00164   uint32 axang;   //auto rotation angle
00165   uint32 ayang;
00166   uint32 azang;
00167   uint32 flgs;   //flags (bit0=ON?)
00168   G3Light() {
00169     RaiseException(ERR_BADARGS);
00170   }
00171   G3Light(int _clr, int xa, int ya, int za) {
00172     clr = _clr;
00173     xang = xa;
00174     yang = ya;
00175     zang = za;
00176     flgs = 1;  //on!
00177   }
00178   void SetClr(int _clr) {clr = _clr;}
00179   void SetAngles(uint x, uint y, uint z) { xang = x; yang = y; zang = z; }  //set rotation angles
00180   void GetAngles(uint32 *x, uint32 *y, uint32 *z) { if (x) *x = xang; if (y) *y = yang; if (z) *z = zang; }
00181   void AddAngles(int x, int y, int z) {  //add to rotation angles
00182     xang += x; yang += y; zang += z;
00183     xang &= G3_MAX_ANGLE;
00184     yang &= G3_MAX_ANGLE;
00185     zang &= G3_MAX_ANGLE;
00186   }
00187   void Show() {flgs |= 0x0001;}
00188   void Hide() {flgs &= 0xfffe;}
00189 };
00190 
00191 class G3Buffer : public Bitmap {
00192     friend class G3Scene;
00193     friend class G3Object;
00194   _PROTECTED_:
00195     Window *gfx;
00196     int32 postxadd, postyadd;
00197   public:
00198     G3Buffer();
00199     void Init(Window *_gfx);
00200     void Put() {
00201       gfx->Put(img, 0, 0, x_1, y_1);
00202     }
00203     void Uninit() {}
00204     //    ~G3Buffer() { Uninit(); }  //not really needed
00205     void Reset() {  //prepare for a new drawing
00206       memset24(img, 0, xy);
00207     }
00208 };
00209 
00210 class G3ZBuffer : public Bitmap {
00211     friend class G3Scene;
00212     friend class G3Object;
00213   _PROTECTED_:
00214     int16 *zbuf;  //Z buffer work area (equal to gfx size)
00215     Window *gfx;
00216     int32 postxadd, postyadd;
00217   public:
00218     G3ZBuffer();
00219     void Init(Window *_gfx);
00220     void Put() {
00221       gfx->Put(img, 0, 0, x_1, y_1);
00222     }
00223     void Uninit() {
00224       if (zbuf) {MemFree((PTR)zbuf);zbuf = (int16*)NULL;}
00225     }
00226     ~G3ZBuffer() { Uninit(); }
00227     void Reset() {
00228       if (!img) return;
00229       memset24(img, 0, xy);
00230       memset32(zbuf, 0x7fff7fff, xy / 2);
00231     }
00232 };
00233 
00234 class G3Scene : virtual public SystemCode {
00235   _PROTECTED_:
00236     void rotate_lights();
00237     void rotate_objects();
00238   public:
00239     List<G3Object*> ol;  //objects list
00240     List<G3Light*> ll;  //lights list
00241     void AddObject(G3Object *o) {
00242       ol.Seek(0, SEEK_END);  //add to END of list
00243       ol.Seek(1, SEEK_CUR);  //seek past last object
00244       ol.Insert(o);
00245     }
00246     BOOL DelObject(G3Object *o) {
00247       int offset = ol.Find(o);
00248       if (offset == -1) return FALSE;
00249       ol.Seek(offset, SEEK_SET);
00250       ol.Delete();
00251       return TRUE;
00252     }
00253     void AddLight(G3Light *o) {
00254       ll.Seek(0, SEEK_END);
00255       ll.Seek(1, SEEK_CUR);  //seek past last object
00256       ll.Insert(o);
00257     }
00258     BOOL DelLight(G3Light *o) {
00259       int offset = ll.Find(o);
00260       if (offset == -1) return FALSE;
00261       ll.Seek(offset, SEEK_SET);
00262       ll.Delete();
00263       return TRUE;
00264     }
00265     void Draw(G3Buffer *_buf);  //Draw it!
00266     void DrawZ(G3ZBuffer *_zbuf);  //Draw to ZBuffer
00267     BOOL Load3DS(char *fn);  //Load a (*.3DS)[R4] file into current scene
00268     BOOL Load3DS(char *fn, uint32 clr);  //load a 3DSR4 file (*.3ds)
00269 };
00270 
00271 class G3Object : virtual public SystemCode { //g3_objs
00272     //defines one object
00273     friend class G3Scene;
00274   _PROTECTED_:
00275     static PTR edgeptr; //Edge array data (fillpoly routine)
00276     uint32 nov;      //# of vertex
00277     Point3D *vl;     //vertex list (X,Y,Z)  //double list would be too slow!
00278     Point3D *tvl;    //Temp Vertex list (after rotation)
00279     uint32 noe;      //# of elements (dot,line,triangle)
00280     uint32 elsiz;    //element list size (in bytes)
00281     uint32 telsiz;   //element list size (in bytes) (transformed)
00282     PTR el;          //object list
00283     PTR tel;         //transformed object list
00284     int32 prexadd;  //To move object around
00285     int32 preyadd;
00286     int32 prezadd;
00287     void rotate_vertices(int, int);
00288     void rotate_elements();
00289     void _Draw(G3Buffer *_buf, List<G3Light*> *ll);  //Draw it!
00290     void _DrawZ(G3ZBuffer *_zbuf, List<G3Light*> *ll);  //Draw to ZBuffer
00291     void fillpoly(G3_trix *o, List<G3Light*> *ll, G3Buffer *_buf);
00292     void fillpoly(G3_trix *o, List<G3Light*> *ll, G3ZBuffer *_zbuf);
00293     void calc_normal(G3_def *,int32 &,int32 &,int32 &);
00294     uint32 xang;     //userview angle (0-4095 degrees (in inc of 4))
00295     uint32 yang;
00296     uint32 zang;
00297   public:
00298     uint32 flgs;     //flags  bit0=visible?
00299     int32 axang;   //user data (I use to auto rotate object)
00300     int32 ayang;
00301     int32 azang;
00302     static BOOL use9;   //use faster rotation algo? (could not get working in old ASM build)
00303     static BOOL sides;  //check for sides
00304     G3Object() {
00305       //      memset(toPTR(this),0,sizeof(*this));
00306       flgs = 1;  //visible by default
00307       prexadd = preyadd = prezadd = 0;
00308       xang = yang = zang = 0;
00309       nov = 0;noe = 0;elsiz = 0;telsiz = 0;
00310       vl = (Point3D*)NULL;
00311       tvl = (Point3D*)NULL;
00312       el = NULL;
00313       tel = NULL;
00314       axang = ayang = azang = 0;
00315     }
00316     ~G3Object() {
00317       if (vl) MemFree((PTR)vl);
00318       if (tvl) MemFree((PTR)tvl);
00319       if (el) MemFree((PTR)el);
00320       if (tel) MemFree((PTR)tel);
00321     }
00322     int AddVertex(int, int, int, BOOL normal = FALSE);
00323     void AddElement(int typ, int clr, int p1, int p2 = 0, int p3 = 0);    //add a dot/line/triangle
00324     void SetAngles(int x, int y, int z) {  //set rotation angles
00325       xang = x; yang = y; zang = z;
00326       xang &= G3_MAX_ANGLE;
00327       yang &= G3_MAX_ANGLE;
00328       zang &= G3_MAX_ANGLE;
00329     }
00330     void AddAngles(int x, int y, int z) {  //add to rotation angles
00331       xang += x; yang += y; zang += z;
00332       xang &= G3_MAX_ANGLE;
00333       yang &= G3_MAX_ANGLE;
00334       zang &= G3_MAX_ANGLE;
00335     }
00336     void GetAngles(uint32 *x, uint32 *y, uint32 *z) { if (x) *x = xang; if (y) *y = yang; if (z) *z = zang; }
00337     void SetPos(int x, int y, int z) { prexadd = x; preyadd = y; prezadd = z; }
00338     void AddPos(int x, int y, int z) { prexadd += x; preyadd += y; prezadd += z; }
00339     void GetPos(int *x, int *y, int *z) { if (x) *x = prexadd; if (y) *x = preyadd; if (z) *z = prezadd; }
00340     void UpdateNormals(int idx = -1);  //update normals for vertex (-1 = update all!)
00341     void MoveVertex(int, int, int, int, BOOL = TRUE);
00342     void Show() {flgs |= 0x0001;}
00343     void Hide() {flgs &= 0xfffe;}
00344 };
00345 

Generated on Mon Mar 5 09:49:14 2007 for DigiForce by  doxygen 1.4.7