home *** CD-ROM | disk | FTP | other *** search
/ Chip 2001 February / Chip_2001-02_cd1.bin / bonus / demos / CS / exp / SOURCES / DEMO / vlacik2c.h < prev   
C/C++ Source or Header  |  2000-08-11  |  3KB  |  143 lines

  1. #ifndef _vlacik2c_H
  2. #define _vlacik2c_H
  3.  
  4.  
  5. struct ElementA {
  6.   GLenum type;
  7.   int n;
  8.   GLuint* ind;
  9.  
  10.   ElementA () {}
  11.  
  12.   ElementA (GLenum t, int nn, GLuint* indices) :
  13.     type(t), n(nn), ind(indices) {}
  14.  
  15.   void Render () {
  16.     glDrawElements(type, n, GL_UNSIGNED_INT, (void*)ind);
  17.   }
  18. };
  19.  
  20. class RenderableEntityA : public RenderableEntity {
  21.   Vertex3DS* v;
  22.   int numvertices;
  23.   List<ElementA> elements;
  24.  
  25.   Material3DS* material;
  26.   BoundingBox B;
  27. public:
  28.   RenderableEntityA () : v(0), numvertices(0), material(0) {}
  29.  
  30.   ~RenderableEntityA () {
  31.     delete[] v;
  32.     elements.rewind();
  33.     for(int i=elements.size(); i; i--) {
  34.       delete[] (*elements).ind;
  35.       ++elements;
  36.     }
  37.   }
  38.  
  39.   void SetMaterial (Material3DS* m) {
  40.     material = m;
  41.   }
  42.   void AddElement (GLenum type, int num, GLuint* indices) {
  43.     elements.push_back(ElementA(type, num, indices));
  44.   }
  45.   void Vertices (int n, Vertex3DS* p) {
  46.     numvertices = n;
  47.     v = p;
  48.  
  49.     GLfloat mx, my, mz, Mx, My, Mz, a1, a2;
  50.     mx = Mx = p->x;
  51.     my = My = p->y;
  52.     mz = Mz = p->z;
  53.     Vertex3DS* p1 = p+1;
  54.  
  55.     for(int i=n-1; i; i--, p++, p1++)
  56.     {
  57.       a1 = p->x;
  58.       a2 = p1->x;
  59.       if(a1<=a2) {
  60.         if(a1<mx) mx=a1;
  61.         if(a2>Mx) Mx=a2;
  62.       }
  63.       else {
  64.         if(a2<mx) mx=a2;
  65.         if(a1>Mx) Mx=a1;
  66.       }
  67.  
  68.       a1 = p->y;
  69.       a2 = p1->y;
  70.       if(a1<=a2) {
  71.         if(a1<my) my=a1;
  72.         if(a2>My) My=a2;
  73.       }
  74.       else {
  75.         if(a2<my) my=a2;
  76.         if(a1>My) My=a1;
  77.       }
  78.  
  79.       a1 = p->z;
  80.       a2 = p1->z;
  81.       if(a1<=a2) {
  82.         if(a1<mz) mz=a1;
  83.         if(a2>Mz) Mz=a2;
  84.       }
  85.       else {
  86.         if(a2<mz) mz=a2;
  87.         if(a1>Mz) Mz=a1;
  88.       }
  89.     }
  90.     B.Corner1(mx, my, mz);
  91.     B.Corner2(Mx, My, Mz);
  92.   }
  93.   virtual void Render (double time=0.0)
  94.   {
  95.     if(!(B.IsVisible())) 
  96.       return;
  97. //    B.Render();
  98.  
  99.     glInterleavedArrays(GL_T4F_C4F_N3F_V4F, 0, (void*)v);
  100.  
  101.     glLockArraysEXT(0, numvertices);
  102.  
  103.     if(material) {
  104.       material->GL();
  105.       if(material->Transparency()==50) {
  106.         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_TRUE);
  107.         glDepthMask(GL_ZERO);
  108.         glDisable(GL_CULL_FACE);
  109.         glEnable(GL_BLEND);
  110.       }
  111.       else {
  112.         glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  113.         glDepthMask(GL_ONE);
  114.         glEnable(GL_CULL_FACE);
  115.         glDisable(GL_BLEND);
  116.       }
  117.       if(material->Texture1())
  118.         glEnable(GL_TEXTURE_2D);
  119.       else
  120.         glDisable(GL_TEXTURE_2D);
  121.     }
  122.  
  123.     glEnable(GL_LIGHTING);
  124.     glEnable(GL_DEPTH_TEST);
  125.  
  126.     elements.rewind();
  127.     for(int i=elements.size(); i; i--) {
  128.       (*elements).Render();
  129.       ++elements;
  130.     }
  131.  
  132.     glUnlockArraysEXT();
  133.   
  134.     glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, GL_FALSE);
  135.     glDisable(GL_CULL_FACE);
  136.     glDisable(GL_BLEND);
  137.     glDepthMask(GL_ONE);
  138.   }
  139.  
  140.  
  141. };
  142.  
  143. #endif