home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / GLENGINE / api3ds.h < prev    next >
C/C++ Source or Header  |  2000-08-19  |  13KB  |  509 lines

  1. /*
  2.  * API for loading and rendering 3D Studio r4 meshes
  3.  */
  4.  
  5. #ifndef __OGL2_3DS_API__
  6. #define __OGL2_3DS_API__
  7.  
  8. #include "objgl2.h"
  9.  
  10. #define BUFFER_SIZE_3DS 1024*1024
  11.  
  12. extern "C++" {
  13.  
  14. class Texture3DS : public Texture {
  15. public:
  16.   static bool voodoo;
  17.   static bool lowtextures;
  18.   void GL ();
  19. };
  20.  
  21. class Textures3DS : public EntityList<Texture3DS> {
  22. public:
  23.   void GL ();
  24.   Texture3DS* GetOrCreate (const char* s);
  25. };
  26.  
  27. class Material3DS : public Material
  28. {
  29.   // Color parameters
  30.   GLfloat ambient[4],
  31.           diffuse[4],
  32.           specular[4];
  33.  
  34.   // Additional parameters
  35.   GLint shininess_percent,
  36.         shininess_strenght_percent,
  37.         transparency_percent,
  38.         transparency_falloff_percent,
  39.         self_illumination_percent,
  40.         reflect_blur_percent;
  41.  
  42.   // Texture maps
  43.   Texture3DS *texture1,
  44.              *texture2,
  45.              *opacity_map,
  46.              *bump_map,
  47.              *specular_map,
  48.              *shininess_map,
  49.              *self_illum_map,
  50.              *reflection_map;
  51.  
  52. public:  
  53.   // Constructor
  54.   Material3DS ();
  55.   void Reset ();
  56.   void GL (GLenum face = GL_FRONT_AND_BACK);
  57.  
  58.   void Ambient (GLfloat r, GLfloat g, GLfloat b),
  59.        Ambient (GLfloat* v),
  60.        Ambient (GLubyte r, GLubyte g, GLubyte b),
  61.        Ambient (GLubyte* v);
  62.   void Diffuse (GLfloat r, GLfloat g, GLfloat b),
  63.        Diffuse (GLfloat* v),
  64.        Diffuse (GLubyte r, GLubyte g, GLubyte b),
  65.        Diffuse (GLubyte* v);
  66.   void Specular (GLfloat r, GLfloat g, GLfloat b),
  67.        Specular (GLfloat* v),
  68.        Specular (GLubyte r, GLubyte g, GLubyte b),
  69.        Specular (GLubyte* v);
  70.   void Shininess (GLint i),
  71.        ShininessStrenght (GLint i),
  72.        Transparency (GLint i),
  73.        TransparencyFalloff (GLint i),
  74.        SelfIllumination (GLint i),
  75.        ReflectBlur (GLint i);
  76.   void Texture1 (Texture3DS* t),
  77.        Texture2 (Texture3DS* t),
  78.        OpacityMap (Texture3DS* t),
  79.        BumpMap (Texture3DS* t),
  80.        SpecularMap (Texture3DS* t),
  81.        ShininessMap (Texture3DS* t),
  82.        SelfIlluminationMap (Texture3DS* t),
  83.        ReflectionMap (Texture3DS* t);
  84.  
  85.   // Feedback
  86.   const GLfloat* Ambient ();
  87.   const GLfloat* Diffuse ();
  88.   const GLfloat* Specular ();
  89.  
  90.   GLint Shininess (),
  91.         ShininessStrenght (),
  92.         Transparency (),
  93.         TransparencyFalloff (),
  94.         SelfIllumination (),
  95.         ReflectBlur ();
  96.  
  97.   Texture3DS* Texture1 ();
  98.   Texture3DS* Texture2 ();
  99.   Texture3DS* OpacityMap ();
  100.   Texture3DS* BumpMap ();
  101.   Texture3DS* SpecularMap ();
  102.   Texture3DS* ShininessMap ();
  103.   Texture3DS* SelfIlluminationMap ();
  104.   Texture3DS* ReflectionMap ();
  105. };
  106.  
  107. template class EntityList<Material3DS>;
  108. typedef EntityList<Material3DS> Materials3DS;
  109.  
  110. class OmniLight3DS : public Light
  111. {
  112.   DynamicSwitch<Vector3f, GLfloat> diffuse;
  113.   DynamicSwitch<Vector3f, GLfloat> position;
  114.  
  115.   GLfloat constant_attenuation;
  116.   GLfloat linear_attenuation;
  117.   GLfloat quadratic_attenuation;
  118.  
  119. public:  
  120.   OmniLight3DS ();
  121.  
  122.   void Reset ();
  123.  
  124.   void GL (GLenum light, double time = 0.0);
  125.  
  126.   void Diffuse (GLfloat r, GLfloat g, GLfloat b),
  127.        Diffuse (GLubyte r, GLubyte g, GLubyte b),
  128.        Diffuse (const Vector3f& V),
  129.        Diffuse (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false);
  130.   void Position (GLfloat x, GLfloat y, GLfloat z),
  131.        Position (const Vector3f& V),
  132.        Position (Dynamic<Vector3f, GLfloat> *ptr, bool strong = false);
  133.   void ConstantAttenuation (GLfloat f),
  134.        LinearAttenuation (GLfloat f),
  135.        QuadraticAttenuation (GLfloat f);
  136.  
  137.   // Feedback
  138.  
  139.   GLfloat ConstantAttenuation (),
  140.        LinearAttenuation (),
  141.        QuadraticAttenuation ();
  142.  
  143.   Vector3f Position (double time = 0.0),
  144.            Diffuse (double time = 0.0);
  145.  
  146. };
  147.  
  148. struct FaceGroup3DS {
  149.   Material3DS* material;
  150.   int numfaces;
  151.   GLuint* indices;
  152.  
  153.   FaceGroup3DS () :
  154.     material(0), numfaces(0), indices(0) {}
  155.   FaceGroup3DS (Material3DS* mat, int n, GLuint* ind) :
  156.     material(mat), numfaces(n), indices(ind) {}
  157. };
  158.  
  159. template class List<FaceGroup3DS>;
  160. typedef List<FaceGroup3DS> FaceGroups3DS;
  161.  
  162. typedef InterleavedArrayElement_T4F_C4F_N3F_V4F Vertex3DS;
  163.  
  164. struct Edge3DS {
  165.   GLuint a, b;
  166.   GLfloat N1_x, N1_y, N1_z;
  167.   GLfloat N2_x, N2_y, N2_z;
  168. };
  169.  
  170. class Object3DS : public RenderableEntity {
  171.   int numvertices;
  172.   Vertex3DS* vertices;
  173.   int numfaces;
  174.   Vector3f* facenormals;
  175.   int numevertices;
  176.   Vertex3DS* evertices;
  177.   int numedges;
  178.   Edge3DS* edges;
  179.   GLuint* edgeindices;
  180.  
  181.   BoundingBox bbox;
  182.  
  183.   FaceGroups3DS facegroups;
  184.  
  185.   DynamicSwitch<GLmatrix, GLfloat> lcs;
  186.  
  187. public:
  188.   Object3DS () :
  189.     numvertices(0), vertices(0),
  190.     numfaces(0), facenormals(0),
  191.     numevertices(0), evertices(0),
  192.     numedges(0), edges(0), edgeindices(0) {
  193.       GLmatrix M;
  194.       M.LoadIdentity();
  195.       lcs(M);
  196.     }
  197.   ~Object3DS ();
  198.  
  199.   void Render (double time = 0.0);
  200.   void Rays (Vector3f C, Vector3f S, GLfloat I, GLenum sfactor, GLenum dfactor);
  201.   void Outline (Vector3f C, GLfloat W, GLfloat r, GLfloat g, GLfloat b);
  202.   void Object3DS::Inline ( Vector3f C, GLfloat W, GLfloat r, GLfloat g, GLfloat b);
  203.   void ShortLine ( Vector3f C, GLfloat W, GLfloat r, GLfloat g, GLfloat b);
  204.   void ShortLine ( Vector3f ang, Vector3f pos, GLfloat scale, GLfloat w, GLfloat r, GLfloat g, GLfloat b);
  205.   void LongLine ( Vector3f ang, Vector3f pos, GLfloat scale, GLfloat w, GLfloat r, GLfloat g, GLfloat b);
  206.   void ShadowVolume (Vector3f S, Vector3f C, GLfloat I, int j);
  207.  
  208.   void Vertices (int n, Vertex3DS* p);
  209.   void AddFaceGroup (Material3DS* mat, int n, GLuint* ind);
  210.  
  211.   void EdgeVertices (int n, Vertex3DS* p);
  212.   void Edges (int n, Edge3DS* p);
  213.   void Facenormals (int n, Vector3f* p);
  214.  
  215.   void CoordinateSystem (const GLmatrix& V),
  216.        CoordinateSystem (Dynamic<GLmatrix, GLfloat> *ptr, bool strong = false);
  217.  
  218.   Vertex3DS* Vertices () {
  219.     return vertices;
  220.   }
  221.   int NumVertices () {
  222.     return numvertices;
  223.   }
  224. };
  225.  
  226. template class EntityList<CameraTarget3DS>;
  227. typedef EntityList<CameraTarget3DS> Cameras3DS;
  228.  
  229. class Scene3DS : public RenderableEntity {
  230.   Lights lights;
  231.   Textures3DS textures;
  232.   Materials3DS materials;
  233.   Cameras3DS cameras;
  234.   RenderableEntities objects;
  235.  
  236.   GLfloat ambient[4];
  237.   int allframes;
  238. public:
  239.   Scene3DS ();
  240.   ~Scene3DS ();
  241.  
  242.   void Render (double time = 0.0);
  243.   void TexturesGL () { textures.GL(); }
  244.  
  245.   void Frames (int n) { allframes = n; }
  246.   int Frames () { return allframes; }
  247.  
  248.   void Add (Light* p);
  249.   void Add (Texture3DS* p);
  250.   void Add (Material3DS* p);
  251.   void Add (CameraTarget3DS* p);
  252.   void Add (RenderableEntity* p);
  253.   void Add (const RenderableEntities & os);
  254.  
  255.   Light* GetLight (String s);
  256.   Texture3DS* GetTexture (String s);
  257.   Material3DS* GetMaterial (String s);
  258.   CameraTarget3DS* GetCamera (String s);
  259.   RenderableEntity* GetObject (String s);
  260.  
  261.   Lights GetLights ();
  262.   Textures3DS GetTextures ();
  263.   Materials3DS GetMaterials ();
  264.   Cameras3DS GetCameras ();
  265.   RenderableEntities GetObjects ();
  266.  
  267.   void Ambient (GLfloat r, GLfloat g, GLfloat b, GLfloat a=1.0);
  268. };
  269.  
  270. struct chunk3ds {
  271.   unsigned short type;
  272.   char *ptr;
  273. };
  274.  
  275. class Loader3DS
  276. {
  277.   enum {
  278.     RGBfloat = 0x0010,
  279.     RGBbyte = 0x0011,
  280.     PERCENTword = 0x0030,
  281.     PERCENTfloat = 0x0031,
  282.    
  283.        MAIN_3DS_CHUNK = 0x4D4D,
  284.  
  285.          EDITOR_CHUNK = 0x3D3D,
  286.   
  287.            AMBIENT_COLOR = 0x2100,
  288.   
  289.            OBJECT_BLOCK = 0x4000,
  290.  
  291.              TRIANGULAR_MESH = 0x4100,
  292.                VERTICES = 0x4110,
  293.                FACES = 0x4120,
  294.                  FACES_MATERIALS = 0x4130,
  295.                  SMOOTHINGS = 0x4150,
  296.                MAPPING_COORDS = 0x4140,
  297.                LOCAL_COORD_SYS = 0x4160,
  298.  
  299.              LIGHT = 0x4600,
  300.                LIGHT_OFF = 0x4620,
  301.   
  302.              CAMERA = 0x4700,
  303.   
  304.            MATERIAL_BLOCK = 0xAFFF,
  305.   
  306.              MATERIAL_NAME = 0xA000,
  307.              MATERIAL_AMBIENT_COLOR = 0xA010,
  308.              MATERIAL_DIFFUSE_COLOR = 0xA020,
  309.              MATERIAL_SPECULAR_COLOR = 0xA030,
  310.              SHININESS_PERCENT = 0xA040,
  311.              SHININESS_STRENGHT_PERCENT = 0xA041,
  312.              TRANSPARENCY_PERCENT = 0xA050,
  313.              TRANSPARENCY_FALLOFF_PERCENT = 0xA052,
  314.              REFLECT_BLUR_PERCENT = 0xA053,
  315.              SELF_ILLUMINATION = 0xA084,
  316.              MAP1 = 0xA200,
  317.              MAP2 = 0xA33A,
  318.              OPACITY_MAP = 0xA210,
  319.              BUMP_MAP = 0xA230,
  320.              SPECULAR_MAP = 0xA204,
  321.              SHININESS_MAP = 0xA33C,
  322.              SELF_ILLUM_MAP = 0xA33D,
  323.              REFLECTION_MAP = 0xA220,
  324.                MAPPING_FILENAME = 0xA300,
  325.                V_SCALE = 0xA354,
  326.                U_SCALE = 0xA356,
  327.                U_OFFSET = 0xA358,
  328.                V_OFFSET = 0xA35A,
  329.  
  330.          KEYFRAMER_CHUNK = 0xB000,
  331.  
  332.            AMBIENT_LIGHT_INFORMATION_BLOCK = 0xB001,
  333.            MESH_INFORMATION_BLOCK = 0xB002,
  334.            CAMERA_INFORMATION_BLOCK = 0xB003,
  335.            CAMERA_TARGET_INFORMATION_BLOCK = 0xB004,
  336.            OMNI_LIGHT_INFORMATION_BLOCK = 0xB005,
  337.            SPOT_LIGHT_TARGET_INFORMATION_BLOCK = 0xB006,
  338.            SPOT_LIGHT_INFORMATION_BLOCK = 0xB007,
  339.            FRAMES = 0xB008,
  340.  
  341.              OBJECT_INFO = 0xB010,
  342.              OBJECT_PIVOT_POINT = 0xB013,
  343.              POSITION_TRACK = 0xB020,
  344.              ROTATION_TRACK = 0xB021,
  345.              SCALE_TRACK = 0xB022,
  346.              FOV_TRACK = 0xB023,
  347.              ROLL_TRACK = 0xB024,
  348.              COLOR_TRACK = 0xB025,
  349.              MORPH_TRACK = 0xB026,
  350.              HOTSPOT_TRACK = 0xB027,
  351.              FALLOFF_TRACK = 0xB028,
  352.              HIDE_TRACK = 0xB029,
  353.              HIERARCHY_POSITION = 0xB030
  354.   };
  355.  
  356.   struct Vector3DS {
  357.     float x,y,z;
  358.     };
  359.  
  360.   struct Face3DS {
  361.     unsigned short a,b,c,flags;
  362.     };
  363.  
  364.   struct Mapping3DS {
  365.     float u,v;
  366.     };
  367.  
  368.   struct iface {
  369.     GLuint a, b, c;
  370.     Vector3f N;
  371.     unsigned long sgroup;
  372.     bool hasmaterial;
  373.     bool ab_done, ac_done, bc_done;
  374.   };
  375.   
  376.   struct ftable {
  377.     GLuint fi;
  378.     GLuint group;
  379.   };
  380.  
  381.   struct shinfo {
  382.     int numfaces;
  383.     int used;
  384.     int splits;
  385.     ftable* p;
  386.     Vertex3DS* v;
  387.     GLuint iv;
  388.   };
  389.  
  390.   struct TCBEE
  391.   {
  392.     float tension;
  393.     float continuity;
  394.     float bias;
  395.     float easeto;
  396.     float easefrom;
  397.     TCBEE() :
  398.       tension(0.0F), continuity(0.0F), bias(0.0F),
  399.       easeto(0.0F), easefrom(0.0F) {}
  400.   };
  401.  
  402.   struct position_key : public TCBEE {
  403.     int frame;
  404.     Vector3f p;
  405.     position_key() :
  406.       frame(0) {}
  407.   };
  408.  
  409.   struct position_track
  410.   {
  411.     int count;
  412.     position_key *keys;
  413.     unsigned int mode;
  414.   
  415.     position_track (int n, int m) : count(n), mode(m) {
  416.       keys = new position_key[n];
  417.     }
  418.     position_key& operator [] (int i) {
  419.       return keys[i];
  420.     }
  421.   
  422.     Vector3f getv (int sel, int n);
  423.   
  424.     ~position_track () {
  425.       delete[] keys;
  426.     }
  427.   };
  428.  
  429.   struct roll_key : public TCBEE
  430.   {
  431.     int frame;
  432.     GLfloat p;
  433.     roll_key() :
  434.       frame(0) {}
  435.   };
  436.  
  437.   struct roll_track
  438.   {
  439.     int count;
  440.     roll_key *keys;
  441.     unsigned int mode;
  442.  
  443.     roll_track(int n, int m) : count(n), mode(m) {
  444.       keys = new roll_key[n];
  445.     }
  446.     roll_key& operator [] (int i) { return keys[i]; }
  447.  
  448.     GLfloat getv(int sel, int n);
  449.  
  450.     ~roll_track() {
  451.       delete[] keys;
  452.     }
  453.   };
  454.  
  455. /*
  456.   struct rotation_key : public TCBEE
  457.   {
  458.     int frame;
  459.     float_quaternion q;
  460.     rotation_key() : frame(0) {}
  461.   };
  462.  
  463.   struct rotation_track
  464.   {
  465.     int count;
  466.     rotation_key *keys;
  467.     unsigned int mode;
  468.  
  469.     rotation_track(int n, int m) : count(n), mode(m) {
  470.       keys = new rotation_key[n];
  471.     }
  472.     rotation_key& operator [] (int i) { return keys[i]; }
  473.  
  474.     float_quaternion getq(int sel, int n);
  475.  
  476.     ~rotation_track() {
  477.       delete[] keys;
  478.     }
  479.   };
  480. */
  481.   enum {an,bn,point};
  482.  
  483.   unsigned char getbyte(char *p) { return *(unsigned char *)p; }
  484.   unsigned short getshort(char *p) { return *(unsigned short *)p; }
  485.   unsigned long getlong(char *p) { return *(unsigned long *)p; }
  486.   float getfloat(char *p) { return *(float *)p; }
  487.  
  488.   char* find_chunk(char *father, unsigned short, char *act);
  489.   List<chunk3ds> subchunks(char *father, char *act);
  490.   List<chunk3ds> subchunks_by_type(char *father, unsigned short chunk_type, char *act);
  491.  
  492.   void track_type_info(unsigned short flag);
  493.   void tcb_info(unsigned short flag);
  494.   char* fill_tcbee(unsigned short flag, char *p, TCBEE *tcb);
  495.   Dynamic<Vector3f, GLfloat>* process_position_track(int allframes, int keys, char *track, unsigned short tracktype);
  496.   Dynamic<GLfloat, GLfloat>* process_roll_track(int allframes, int keys, char *track, unsigned short tracktype);
  497.  
  498. public:
  499.   static char *buffer;
  500.  
  501.   Loader3DS() {}
  502.   Scene3DS* Load(const char *inputfile, Textures3DS* texturelib = 0);
  503.  
  504. };
  505.  
  506. } // extern "C++"
  507.  
  508. #endif
  509.