include/df/opengl.hpp

00001 //some 3DS flags
00002 #define _3DS_FLG_TENSION       0x01
00003 #define _3DS_FLG_CONTINUITY    0x02
00004 #define _3DS_FLG_BIAS          0x04
00005 #define _3DS_FLG_EASE_TO       0x08
00006 #define _3DS_FLG_EASE_FROM     0x10
00007 
00008 class GLTexture : public SystemCode {
00009   public:
00010 //data
00011     int x, y;
00012     BOOL alpha;  //only PNG supports Alpha channel
00013     PTR img;
00014     uint32 refcnt;   //reference count
00015     int mipmapcnt;   //0=no mipmaps
00016     PTR mipmaps[8];  //optional mipmaps
00017 //mipmap info: max size = 256x256 : x/y must be power of 2
00018 //members
00019     GLTexture();
00020     ~GLTexture();
00021     BOOL Load(char *, BOOL genmm = TRUE);
00022     BOOL Load(File *f, BOOL genmm = TRUE);
00023     void GenerateMipMaps();  //uses gluScaleImage() to generate mipmaps
00024     void FreeMipMaps();
00025     void AddAlpha(uint8 value=0);  //adds the alpha channel
00026     void SetAlpha(uint32 keyclr, uint8 value_true, uint8 value_false);
00027     void SetAlpha();  //set alpha channel relative to pixel intensity
00028     void operator =(Bitmap &);  //copy from a Bitmap class
00029 };
00030 
00031 //NOTE : Textures filenames are : %08x.png when using GLScene::Load3DS()
00032 
00033 struct GLPos {  //translate (abs pos)
00034   float x, y, z;
00035 };
00036 
00037 struct GLRotate {  //rotate
00038   float angle, x, y, z;
00039 };
00040 
00041 struct GLScale {  //scale
00042   float x, y, z;
00043 };
00044 
00045 struct GLVertex {
00046   float x,y,z;
00047   float tx,ty;  //texture coords
00048 };
00049 
00050 struct GLPoly {
00051   Array<uint32> vl;  //vertex index list
00052   uint32 tex;  //texture offset
00053 };
00054 
00055 struct GLLightType {
00056   float ambient[4];
00057   float diffuse[4];
00058   float specular[4];
00059   float pos4;  //w of light position
00060 };
00061 
00062 struct GLLight {
00063   GLObjectGroup *owner;  //NULL = not in use
00064   float pos[4];  //x,y,z,w
00065   //(w=0.0 (direction-diffuse,specular) ; w=1.0 (non-directional - ambient))
00066 };
00067 
00068 class GLObject : public SystemCode {
00069   friend class GLScene;
00070   private:
00071     Array<GLVertex> vl;  //vertex list
00072     Array<GLPoly> pl;    //poly list
00073     uint32 texidx;     //texture index (0xffffffff = not loaded)
00074     //animation data
00075     Array<GLPos> pos;  //move list
00076     Array<GLRotate> rot;  //rotation list
00077     Array<GLScale> scale;  //scale list
00078     BOOL b_rotate, b_translate, b_scale;  //has any occured?
00079     int i_light;  //-1=object
00080   public:
00081     GLObject();
00082     int32 FrameIndex;
00083     float angle, rx, ry, rz;  //rotation
00084     float tx, ty, tz; //translation
00085     float sx, sy, sz; //scale
00086     //these are non-additive
00087     void SetRotate(float _angle, float x, float y, float z) {
00088       angle = _angle; rx = x; ry = y; rz = z;
00089     }
00090     void SetTranslate(float x, float y, float z) {
00091       tx = x; ty = y; tz = z;
00092     }
00093     void SetScale(float x, float y, float z) {
00094       sx = x; sy = y; sz = z;
00095     }
00096     BOOL SetFrame(uint32 idx);  //0=init state
00097     BOOL NextFrame();
00098     int32 FrameCount();
00099     void AddVertex(int cnt, float *xyz);
00100     void AddPoly(int cnt, uint16 *pts);
00101 };
00102 
00103 class GLObjectGroup : public SystemCode {
00104   friend class GLScene;
00105   private:
00106     float m[16];  //translation, rotation, scale matrix for all sub-objects
00107   public:
00108     GLObjectGroup();
00109     List<GLObject *> ol;
00110     //these are additive
00111     void Rotate(float angle, float x, float y, float z);
00112     void Translate(float x, float y, float z);
00113     void Scale(float x, float y, float z);
00114     BOOL NextFrame();
00115     BOOL SetFrame(int);
00116 };
00117 
00118 class GLScene : public SystemCode {
00119   private:
00120     List<GLObjectGroup *> ogl;
00121     Array<GLLightType> ltl;  //fixed size (max = 0x100)
00122     Array<GLLight> ll;  //actual lights (max = 0x100)
00123     Array<GLTexture *> tl; //texture list
00124     int iwx, iwy;     //window size (int)
00125     float fwx, fwy;   //window size (float)
00126     float ratio;      //fwx / fwy
00127     float m_camera[16];  //camera matrix
00128     int AddLight(GLObject *, GLObjectGroup *);  //returns i_light
00129     void DeleteLights(GLObjectGroup *og);
00130   public:
00131     GLScene();
00132     String TexturePath;
00133     void Init(int x, int y);  //must give size of render window
00134     void SetCamera(float angle, float rx, float ry, float rz, float tx, float ty, float tz);
00135     void CameraRotate(float angle, float rx, float ry, float rz);
00136     void CameraTranslate(float x, float y, float z);
00137 //    void SortObjects();  //Z order sorting
00138     void Render();  //Window must be prepared (see /examples/ex19)
00139     void AddObject(GLObject *);
00140     //load meshes, textures & animation (idx=1st object index cnt=# objects loaded)
00141     BOOL Load3DS(File *);
00142     BOOL Load3DS(char *);
00143     BOOL LoadAllTextures();
00144     BOOL LoadTexture(uint32 texidx);
00145     void FreeTexture(uint32 texidx);
00146     void FreeUnusedTextures();
00147     int ObjectCount() {
00148       return ogl.Count;
00149     }
00150     BOOL NextFrame(int objidx) {
00151       return ogl[objidx]->NextFrame();
00152     }
00153     BOOL SetFrame(int objidx, int frame) {
00154       return ogl[objidx]->SetFrame(frame);
00155     }
00156     void ObjectFree(GLObjectGroup *);
00157     void ObjectFree(int idx);
00158     void ObjectTranslate(int idx, float x, float y, float z) {ogl[idx]->Translate(x,y,z);}
00159     void ObjectRotate(int idx, float angle, float x, float y, float z) {
00160       ogl[idx]->Rotate(angle,x,y,z);
00161     }
00162     void ObjectScale(int idx, float x, float y, float z) {ogl[idx]->Scale(x,y,z);}
00163     void LightSetupAmbient(int idx, float r, float g, float b, float a);
00164     void LightSetupDiffuse(int idx, float r, float g, float b, float a);
00165     void LightSetupSpecular(int idx, float r, float g, float b, float a);
00166     void LightSetupPositionw(int idx, float w);
00167     //adds as an GLObjectGroup with 1 GLObject (good for creating ambient light)
00168     GLObjectGroup *AddLight(int idx, float x, float y, float z);
00169 };
00170 
00171 

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