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

  1. #ifndef __OGL2_BOUNDING_BOX__
  2. #define __OGL2_BOUNDING_BOX__
  3.  
  4. #ifdef WIN32
  5. #include <windows.h>
  6. #endif
  7.  
  8. #ifndef WIN32
  9. #define APIENTRY
  10. #define CALLBACK
  11. #endif
  12.  
  13. #include <GL/gl.h>
  14. #include "glmatrix.h"
  15.  
  16. extern "C++" {
  17.  
  18. class BoundingBox {
  19.  
  20.   GLubyte eval(GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
  21.     GLubyte c=0;
  22.     if(x>w)  c|=1;
  23.     if(x<-w) c|=2;
  24.     if(y>w)  c|=4;
  25.     if(y<-w) c|=8;
  26.     if(z>w)  c|=16;
  27.     if(z<-w) c|=32;
  28.     return c;
  29.   }
  30.  
  31.   GLfloat mx, my, mz;
  32.   GLfloat Mx, My, Mz;
  33.   bool initialized1,
  34.        initialized2,
  35.        initialized;
  36.  
  37. public:
  38.   BoundingBox () :
  39.     initialized1(false), initialized2(false), initialized(false) {}
  40.  
  41.   void Corner1(GLfloat x, GLfloat y, GLfloat z) {
  42.     initialized1 = true;
  43.     mx = x;
  44.     my = y;
  45.     mz = z;
  46.     if(initialized2) 
  47.       initialized = true;
  48.   }
  49.   void Corner2(GLfloat x, GLfloat y, GLfloat z) {
  50.     initialized2 = true;
  51.     Mx = x;
  52.     My = y;
  53.     Mz = z;
  54.     if(initialized1) 
  55.       initialized = true;
  56.   }
  57.   bool IsVisible() {
  58.     if(!initialized)
  59.       return true;
  60.     GLmatrix matrix1, matrix2;
  61.  
  62.     glGetFloatv(GL_PROJECTION_MATRIX, matrix1());
  63.     glGetFloatv(GL_MODELVIEW_MATRIX, matrix2());
  64.     matrix1 = matrix1*matrix2;
  65.  
  66.     GLfloat x, y, z, w;
  67.     GLubyte outcode;
  68.  
  69.     x=mx; y=my; z=mz; w=1.0;
  70.     matrix1.MultVertex4f(x, y, z, w);
  71.     outcode = eval(x, y, z, w);
  72.     if(outcode==0)  return true;
  73.  
  74.     x=Mx; y=my; z=mz; w=1.0;
  75.     matrix1.MultVertex4f(x, y, z, w);
  76.     outcode &= eval(x, y, z, w);
  77.     if(outcode==0)  return true;
  78.  
  79.     x=Mx; y=My; z=mz; w=1.0;
  80.     matrix1.MultVertex4f(x, y, z, w);
  81.     outcode &= eval(x, y, z, w);
  82.     if(outcode==0)  return true;
  83.  
  84.     x=mx; y=My; z=mz; w=1.0;
  85.     matrix1.MultVertex4f(x, y, z, w);
  86.     outcode &= eval(x, y, z, w);
  87.     if(outcode==0)  return true;
  88.  
  89.  
  90.     x=mx; y=my; z=Mz; w=1.0;
  91.     matrix1.MultVertex4f(x, y, z, w);
  92.     outcode &= eval(x, y, z, w);
  93.     if(outcode==0)  return true;
  94.  
  95.     x=Mx; y=my; z=Mz; w=1.0;
  96.     matrix1.MultVertex4f(x, y, z, w);
  97.     outcode &= eval(x, y, z, w);
  98.     if(outcode==0)  return true;
  99.  
  100.     x=Mx; y=My; z=Mz; w=1.0;
  101.     matrix1.MultVertex4f(x, y, z, w);
  102.     outcode &= eval(x, y, z, w);
  103.     if(outcode==0)  return true;
  104.  
  105.     x=mx; y=My; z=Mz; w=1.0;
  106.     matrix1.MultVertex4f(x, y, z, w);
  107.     outcode &= eval(x, y, z, w);
  108.     if(outcode==0)  return true;
  109.  
  110.     return false;
  111.   }
  112.  
  113.   // Union with another bounding box
  114.   BoundingBox& operator +=(const BoundingBox& B)
  115.   {
  116.     if(!B.initialized) {
  117.       initialized1 = initialized2 = initialized = false;
  118.       return *this;
  119.     }
  120.     if(initialized) {
  121.       if(B.mx<mx)  mx = B.mx;
  122.       if(B.my<my)  my = B.my;
  123.       if(B.mz<mz)  mz = B.mz;
  124.       if(B.Mx>Mx)  Mx = B.Mx;
  125.       if(B.My>My)  My = B.My;
  126.       if(B.Mz>Mz)  Mz = B.Mz;
  127.       }
  128.     else {
  129.       mx = B.mx; my = B.my; mz = B.mz;
  130.       Mx = B.Mx; My = B.My; Mz = B.Mz;
  131.       initialized1 = initialized2 = initialized = true;
  132.       }
  133.     return *this;
  134.   }
  135.  
  136.   void Render()
  137.   {
  138.     if(!initialized)
  139.       return;
  140.     glDisable(GL_TEXTURE_2D);
  141.     glColor3f(1, 1, 1);
  142.     glBegin(GL_LINE_LOOP);
  143.       glVertex3f(mx, my, mz);
  144.       glVertex3f(Mx, my, mz);
  145.       glVertex3f(Mx, My, mz);
  146.       glVertex3f(mx, My, mz);
  147.     glEnd();
  148.     glBegin(GL_LINE_LOOP);
  149.       glVertex3f(mx, my, Mz);
  150.       glVertex3f(Mx, my, Mz);
  151.       glVertex3f(Mx, My, Mz);
  152.       glVertex3f(mx, My, Mz);
  153.     glEnd();
  154.     glBegin(GL_LINE_LOOP);
  155.       glVertex3f(mx, my, mz);
  156.       glVertex3f(Mx, my, mz);
  157.       glVertex3f(Mx, my, Mz);
  158.       glVertex3f(mx, my, Mz);
  159.     glEnd();
  160.     glBegin(GL_LINE_LOOP);
  161.       glVertex3f(mx, My, mz);
  162.       glVertex3f(Mx, My, mz);
  163.       glVertex3f(Mx, My, Mz);
  164.       glVertex3f(mx, My, Mz);
  165.     glEnd();
  166.   }
  167.  
  168. };
  169.  
  170. } // extern "C++"
  171.  
  172. #endif
  173.  
  174.