Index: branches/bsp_model/src/lib/coord/p_node.h
===================================================================
--- branches/bsp_model/src/lib/coord/p_node.h	(revision 7544)
+++ branches/bsp_model/src/lib/coord/p_node.h	(revision 7563)
@@ -198,7 +198,8 @@
   /** tells the child that the parent's Direction has changed */
   inline void parentDirChanged () { this->bRelDirChanged = true; }
+ public:
   /** @returns the last calculated coordinate */
   inline Vector getLastAbsCoor() { return this->lastAbsCoordinate; }
-
+ private:
   static PNode* createNullParent();
   void reparent();
Index: branches/bsp_model/src/lib/graphics/importer/bsp_file.h
===================================================================
--- branches/bsp_model/src/lib/graphics/importer/bsp_file.h	(revision 7544)
+++ branches/bsp_model/src/lib/graphics/importer/bsp_file.h	(revision 7563)
@@ -62,11 +62,10 @@
 leaf;
 
-typedef struct
+struct brush
 {
   int brushside; 	//!< First brushside for brush.
   int n_brushsides; 	//!< Number of brushsides for brush.
   int texture; 	        //!< Texture index.
-}
-brush;
+};
 
 typedef struct
@@ -128,4 +127,12 @@
 }
 lightmap;
+
+typedef struct
+{
+  char name[64];
+  int flags;
+  int contents;
+}
+BspTexture;
 
 class BspFile
@@ -182,4 +189,6 @@
   int numLightMaps;
 
+
+
   BspTreeNode* build_tree_rec( int i );
   unsigned int loadLightMapToGL(lightmap&);
Index: branches/bsp_model/src/lib/graphics/importer/bsp_manager.cc
===================================================================
--- branches/bsp_model/src/lib/graphics/importer/bsp_manager.cc	(revision 7544)
+++ branches/bsp_model/src/lib/graphics/importer/bsp_manager.cc	(revision 7563)
@@ -74,5 +74,5 @@
 
 
-  this->cam = State::getCameraTargetNode()->getAbsCoor();
+ 
   this->viewDir=    State::getCameraTarget()->getAbsCoor() -  State::getCamera()->getAbsCoor() ;
   float d = (cam.x*viewDir.x + cam.y*viewDir.y + cam.z * viewDir.z);
@@ -84,11 +84,19 @@
 
 
-
-  this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
-
-  if (true /*(((int *)(this->bspFile->header))[35] == 0 )  || viscluster < 0*/)  //!< if (sizeof(Visdata) == 0)
+  this->cam = State::getCameraTargetNode()->getAbsCoor();
+  //this->checkCollision(this->root, &this->cam);   //!< Test Collision Detection
+  this->outputStartsOut = true;
+  this->outputAllSolid = false;
+  this->outputFraction = 1.0f;
+  
+  this->checkCollisionRay(this->root,0.0f,1.0f, &(State::getCameraTargetNode()->getLastAbsCoor()), &this->cam);
+  
+  if(this->outputFraction != 1.0f || this->outputAllSolid ) this->drawDebugCube(&this->cam);
+
+  if (true || (((int *)(this->bspFile->header))[35] == 0 )  || viscluster < 0)  //!< if (sizeof(Visdata) == 0)
   {
-    /** Do Frustum culling and draw 'em all **/
-
+   
+    
+    
     // Iterate through all Leafspublic final double readLEDouble()
     for(int i = 0; i <  this->bspFile->numLeafs   ; i++ )
@@ -96,6 +104,26 @@
       // cluster =  (this->bspFile->leaves)[i].cluster;
       leaf& curLeaf = (this->bspFile->leaves)[i];
-
-
+      if(curLeaf.cluster<0) continue;
+     
+      
+      /** Do Frustum culling and draw 'em all **/
+      bool inFrustum = true;
+      float dMins;
+      float dMaxs;
+      Vector dir;
+      dir.x = this->cam.x - State::getCameraTargetNode()->getLastAbsCoor().x;
+      dir.y = this->cam.y - State::getCameraTargetNode()->getLastAbsCoor().y;
+      dir.z = this->cam.z - State::getCameraTargetNode()->getLastAbsCoor().z;
+      float dist =  dir.x*this->cam.x +dir.y*this->cam.y +dir.z*this->cam.z;
+      //if(dist < 0) dist = -dist;
+      dMins = dir.x*(float)curLeaf.mins[0] +dir.y*(float)curLeaf.mins[1] +dir.z*(float)curLeaf.mins[2] ;
+      dMaxs = dir.x*(float)curLeaf.maxs[0] +dir.y*(float)curLeaf.maxs[1] +dir.z*(float)curLeaf.maxs[2] ;
+     
+      if(dMins < 0.0 && dMaxs < 0.0)
+      {    
+        //continue; 
+      }
+    
+       
       // Iterate through all faces
       for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
@@ -103,11 +131,5 @@
         if (f >=0 && !this->isAlreadyVisible(f)) {
           this->alreadyVisible[f] = true;
-          /*	if(ActLeaf->leafIndex == i)public final double readLEDouble()
-          	{
-          		this->alreadyVisible[i] = false;
-          		this->draw_debug_face(f);
-          		this->alreadyVisible[i] = true;
-          	}  
-                else */
+          	
           addFace(f); // "visibleFaces.append(f)"
         }
@@ -131,15 +153,13 @@
       leaf& curLeaf =  (this->bspFile->leaves)[i] ;
       cluster =  curLeaf.cluster;
-      
-      if(cluster <0) continue;
+
+      if(cluster < 0) continue;
       v = ((viscluster *  ( ((int *)this->bspFile->visData)[1]) ) + (cluster / 8));
-      visSet =((unsigned char*) (this->bspFile->visData))[v+8];
-   
-      
-      if( ((visSet) & ((unsigned char)1 <<(unsigned char) (cluster &  7))) == 0 )
-      {
+      visSet =((unsigned char*) (this->bspFile->visData))[v + 8];
+
+
+      if( !((visSet) & ((unsigned char)1 <<(unsigned char) (cluster &  7)))  ) {
         // Iterate through all faces
-        for (int j = 0; j < curLeaf.n_leaffaces ; ++j)
-        {
+        for (int j = 0; j < curLeaf.n_leaffaces ; ++j) {
           const int f = (j +  curLeaf.leafface) % this->bspFile->numFaces;
 
@@ -152,5 +172,5 @@
         }
 
-    }// if
+      }// if
 
     }//for
@@ -170,6 +190,5 @@
 }//draw
 
-void BspManager::draw_leaf()
-{}
+
 
 void BspManager::draw_face(int curface)
@@ -189,42 +208,39 @@
   }
   if(curFace.type == 3) return;
-//  if(this->bspFile->Materials[curFace.texture] != NULL)
-    
+  //  if(this->bspFile->Materials[curFace.texture] != NULL)
+
   if(this->lastTex != curFace.texture) {
     this->bspFile->Materials[curFace.texture].mat->select();
-     this->lastTex = curFace.texture;
-  }
-    
-    if(curFace.lm_index < 0) 
-    {
-      glActiveTextureARB(GL_TEXTURE1_ARB);
-      glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap );
-      glEnable(GL_TEXTURE_2D);
-    }
-    else
-    {
+    this->lastTex = curFace.texture;
+  }
+
+  if(curFace.lm_index < 0) {
+    glActiveTextureARB(GL_TEXTURE1_ARB);
+    glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap );
+    glEnable(GL_TEXTURE_2D);
+  } else {
     glActiveTextureARB(GL_TEXTURE1_ARB);
     glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[curFace.lm_index]);
     glEnable(GL_TEXTURE_2D);
-    }
-    
-    glColor4f(3.0,3.0,3.0,1.0);
+  }
+
+  glColor4f(3.0,3.0,3.0,1.0);
   glEnableClientState(GL_VERTEX_ARRAY );
   glEnableClientState(GL_TEXTURE_COORD_ARRAY );
   glEnableClientState(GL_NORMAL_ARRAY );
   //  glEnableClientState(GL_COLOR_ARRAY);
-  
+
 
   glVertexPointer(3, GL_FLOAT, stride, &(curVertex[offset].position[0]));
-  
+
   glClientActiveTextureARB(GL_TEXTURE0_ARB);
   glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[0]));
   glEnableClientState(GL_TEXTURE_COORD_ARRAY);
-  
+
   glClientActiveTextureARB(GL_TEXTURE1_ARB);
   glTexCoordPointer(2, GL_FLOAT, stride, &(curVertex[offset].texcoord[1]));
   //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
-  
+
   glNormalPointer( GL_FLOAT, stride, &(curVertex[offset].normal[0]));
   // glColorPointer(4, GL_BYTE, stride, &(curVertex[offset].color[0]));
@@ -292,22 +308,19 @@
     this->lastTex = Face->texture;
   }
-  
-  
-  
-  if(Face->lm_index < 0) 
-  {
+
+
+
+  if(Face->lm_index < 0) {
     glActiveTextureARB(GL_TEXTURE1_ARB);
     glBindTexture(GL_TEXTURE_2D, this->bspFile->whiteLightMap);
     glEnable(GL_TEXTURE_2D);
-  }
-  else
-  {
+  } else {
     glActiveTextureARB(GL_TEXTURE1_ARB);
     glBindTexture(GL_TEXTURE_2D, this->bspFile->glLightMapTextures[Face->lm_index]);
     glEnable(GL_TEXTURE_2D);
-}
-    glColor4f(3.0,3.0,3.0,1.0);
-  
-  glEnable( GL_AUTO_NORMAL);
+  }
+  glColor4f(3.0,3.0,3.0,1.0);
+
+  //glEnable( GL_AUTO_NORMAL);
   glEnableClientState(GL_VERTEX_ARRAY );
   glEnableClientState(GL_TEXTURE_COORD_ARRAY );
@@ -315,24 +328,24 @@
     glFrontFace(GL_CW);
     //PRINTF(0)("BSP Manager: Face->size[0]: %i . \n", Face->size[0]);
-  
- 
+
+
     glEnableClientState(GL_NORMAL_ARRAY );
 
     glVertexPointer(3, GL_FLOAT,44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).position[0]));
-    
-    
+
+
     glClientActiveTextureARB(GL_TEXTURE0_ARB);
     glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[0][0]));
     glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
-    
+
     glClientActiveTextureARB(GL_TEXTURE1_ARB);
     glTexCoordPointer(2, GL_FLOAT, 44, &((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).texcoord[1][0]));
     //glEnableClientState(GL_TEXTURE_COORD_ARRAY);
 
-    
+
     glNormalPointer( GL_FLOAT, 44,&((((BspVertex*)(this->bspFile->patchVertice))[8*8*(Face->meshvert+i)]).normal[0]) );
 
-  
+
 
 
@@ -341,15 +354,15 @@
                      & (     (((GLuint*)  (this->bspFile->patchIndexes))[7*8*2*(Face->meshvert+i)+ row*2*8]  ))  );
     }
-   
+
     glFrontFace(GL_CCW);
   }
   glDisableClientState(GL_TEXTURE0_ARB);
   glDisableClientState(GL_TEXTURE1_ARB);
-  glDisable(GL_AUTO_NORMAL);
+  //glDisable(GL_AUTO_NORMAL);
   glDisableClientState(GL_VERTEX_ARRAY );
   glDisableClientState(GL_TEXTURE_COORD_ARRAY );
-  
+
   glBegin(GL_QUADS);
-  
+
   glEnd();
 }
@@ -376,6 +389,176 @@
 }
 
+void BspManager::checkBrushRay(brush* curBrush)
+{
+  float EPSILON = 0.000001;
+  float startDistance;
+  float endDistance;
+  
+  float startFraction = -1.0f;
+  float endFraction = 1.0f;
+  bool startsOut = false;
+  bool endsOut = false;
+  
+  Vector inputStart = State::getCameraTargetNode()->getLastAbsCoor();
+  Vector inputEnd   = State::getCameraTargetNode()->getAbsCoor();
+
+  for (int i = 0; i < curBrush->n_brushsides; i++)
+  {
+    brushside& curBrushSide =   this->bspFile->brushSides[curBrush->brushside + i]   ;
+    plane& curPlane  =   this->bspFile->planes[curBrushSide.plane] ;
+
+       startDistance = inputStart.x * curPlane.x + inputStart.y * curPlane.y+ inputStart.z * curPlane.z - curPlane.d;
+     endDistance = inputEnd.x * curPlane.x +inputEnd.y * curPlane.y +inputEnd.z * curPlane.z -curPlane.d;
+
+    if (startDistance > 0)
+      startsOut = true;
+    if (endDistance > 0)
+      endsOut = true;
+
+        // make sure the trace isn't completely on one side of the brush
+    if (startDistance > 0 && endDistance > 0)
+    {   // both are in front of the plane, its outside of this brush
+      return;
+    }
+    if (startDistance <= 0 && endDistance <= 0)
+    {   // both are behind this plane, it will get clipped by another one
+      continue;
+    }
+
+        // MMM... BEEFY
+    if (startDistance > endDistance)
+    {   // line is entering into the brush
+      float fraction = (startDistance - EPSILON) / (startDistance - endDistance);  // *
+      if (fraction > startFraction)
+        startFraction = fraction;
+    }
+    else
+    {   // line is leaving the brush
+      float fraction = (startDistance + EPSILON) / (startDistance - endDistance);  // *
+      if (fraction < endFraction)
+        endFraction = fraction;
+    }
+    
+  }
+   if (startsOut == false)
+    {
+      this->outputStartsOut = false;
+      if (endsOut == false)
+        this->outputAllSolid = true;
+      return;
+    }
+
+  if (startFraction < endFraction)
+  {
+    if (startFraction > -1.0f && startFraction < outputFraction)
+    {
+      if (startFraction < 0)
+        startFraction = 0;
+      this->outputFraction = startFraction;
+    }
+  }
+  
+}
+
+void BspManager::checkCollisionRay(BspTreeNode* node, float startFraction, float endFraction, Vector* start, Vector* end)
+{
+
+
+  float EPSILON = 0.000001;
+  float  endDistance = (end)->x * (node->plane.x) +(end)->y * (node->plane.y) +(end)->z * (node->plane.z)  - node->d;
+  float  startDistance = (start)->x * (node->plane.x)+ (start)->y * (node->plane.y)+ (start)->z * (node->plane.z)- node->d;
+
+
+  if(node->isLeaf) {
+    leaf& curLeaf = this->bspFile->leaves[node->leafIndex];
+    for (int i = 0; i <  curLeaf.n_leafbrushes ; i++) {
+      brush& curBrush = this->bspFile->brushes[((int*)(this->bspFile->leafBrushes))[curLeaf.leafbrush_first+i]];
+      //object *brush = &BSP.brushes[BSP.leafBrushes[leaf->firstLeafBrush + i]];
+      if (curBrush.n_brushsides > 0   &&
+        ((((BspTexture*)(this->bspFile->textures))[curBrush.texture]).contents & 1)) 
+      // CheckBrush( brush );
+      this->checkBrushRay(&curBrush);
+      if(curBrush.n_brushsides <=0) this->outputAllSolid = true;
+    }
+    return;
+  }
+
+
+  if (startDistance >= 0 && endDistance >= 0)     // A
+  {   // both points are in front of the plane
+    // so check the front child
+    this->checkCollisionRay(node->left,0,0,start,end);
+  } else if (startDistance < 0 && endDistance < 0)  // B
+  {   // both points are behind the plane
+    // so check the back child
+    this->checkCollisionRay(node->right,0,0,start,end);
+  } else                                            // C
+  {   // the line spans the splitting plane
+    int side;
+    float fraction1, fraction2, middleFraction;
+    Vector middle;
+
+    // STEP 1: split the segment into two
+    if (startDistance < endDistance) {
+      side = 1; // back
+      float inverseDistance = 1.0f / (startDistance - endDistance);
+      fraction1 = (startDistance + EPSILON) * inverseDistance;
+      fraction2 = (startDistance + EPSILON) * inverseDistance;
+    } else if (endDistance < startDistance) {
+      side = 0; // front(start)->x * (node->plane.x)+ 
+      float inverseDistance = 1.0f / (startDistance - endDistance);
+      fraction1 = (startDistance + EPSILON) * inverseDistance;
+      fraction2 = (startDistance - EPSILON) * inverseDistance;
+    } else {
+      side = 0; // front
+      fraction1 = 1.0f;
+      fraction2 = 0.0f;
+    }
+
+    // STEP 2: make sure the numbers are valid
+    if (fraction1 < 0.0f) fraction1 = 0.0f;
+    else if (fraction1 > 1.0f) fraction1 = 1.0f;
+    if (fraction2 < 0.0f) fraction2 = 0.0f;
+    else if (fraction2 > 1.0f) fraction2 = 1.0f;
+
+    // STEP 3: calculate the middle point for the first side
+    middleFraction = startFraction +
+                     (endFraction - startFraction) * fraction1;
+
+    middle.x = start->x + fraction1 * (end->x - start->x);
+    middle.y = start->y + fraction1 * (end->y - start->y);
+    middle.z = start->z + fraction1 * (end->z - start->z);
+
+    // STEP 4: check the first side
+    //CheckNode( node->children[side], startFraction, middleFraction, start, middle );
+    if(side == 0) this->checkCollisionRay(node->left,startFraction, middleFraction, start, &middle );
+
+    else this->checkCollisionRay(node->right,startFraction, middleFraction,
+                                   start, &middle );
+
+    // STEP 5: calculate the middle point for the second side
+    middleFraction = startFraction +
+                     (endFraction - startFraction) * fraction2;
+
+    middle.x = start->x + fraction2 * (end->x - start->x);
+    middle.y = start->y + fraction2 * (end->y - start->y);
+    middle.z = start->z + fraction2 * (end->z - start->z);
+
+    // STEP 6: check the second side
+    if(side == 1)this->checkCollisionRay(node->left,middleFraction, endFraction, &middle, end);
+
+    else this->checkCollisionRay(node->right,middleFraction, endFraction,&middle, end );
+
+
+  }
+
+}
 void  BspManager::checkCollision(BspTreeNode* node, Vector* cam)
 {
+  Vector next = this->cam;
+  next.x =   (State::getCameraTargetNode()->getLastAbsCoor()).x ;
+  next.y =   (State::getCameraTargetNode()->getLastAbsCoor()).y ;
+  next.z =   (State::getCameraTargetNode()->getLastAbsCoor()).z ;
+
   float dist = 0;
   if(!(node->isLeaf)) {
@@ -399,5 +582,5 @@
     leaf& camLeaf =  ((leaf *)(this->bspFile->leaves))[(node->leafIndex ) ];
 
-    if (camLeaf.cluster < 0) {this->drawDebugCube(&this->cam);}
+    if (camLeaf.cluster < 0) {this->drawDebugCube(&this->cam); this->drawDebugCube(&next);}
 
 
Index: branches/bsp_model/src/lib/graphics/importer/bsp_manager.h
===================================================================
--- branches/bsp_model/src/lib/graphics/importer/bsp_manager.h	(revision 7544)
+++ branches/bsp_model/src/lib/graphics/importer/bsp_manager.h	(revision 7563)
@@ -17,5 +17,5 @@
 
 
-// FORWARD DECLARATION
+// FORWARD DECLARATIONS
 class  BspFile;
 class BspTreeLeaf;
@@ -24,4 +24,5 @@
 class set;
 struct face;
+struct brush;
 
 class BspManager
@@ -33,6 +34,5 @@
   // Functions
   void draw();
-  void draw_leaf();
-  void draw_debug_face(int Face);
+  void draw_debug_face(int Face); 
   void draw_face(int Face);
   void draw_patch(face* Face);
@@ -41,18 +41,26 @@
 private:
   // Functions
+  BspTreeNode* getLeaf(BspTreeNode*  node,   Vector* cam) ;  //!< Traverses the tree
+  void  checkCollision(BspTreeNode* node, Vector* cam);
+  void  checkCollisionRay(BspTreeNode * node,float startFraction, float endFraction, Vector* start, Vector* end); 
+  void  checkBrushRay(brush* curBrush);
+  void drawDebugCube(Vector* cam);
+  bool isAlreadyVisible(int Face);
+  void addFace(int Face);
+  
+  // Data
   BspFile*  bspFile;
   BspTreeNode* root;
-  BspTreeNode* getLeaf(BspTreeNode*  node,   Vector* cam) ;  //!< Traverses the tree
-  void  checkCollision(BspTreeNode* node, Vector* cam);
-  void drawDebugCube(Vector* cam);
   Vector cam;
   Vector ship;
   Vector  viewDir;
+  int lastTex;
+  
+  //obsolete
+  bool  outputStartsOut;
+  bool  outputAllSolid;
+  float outputFraction;
+  
   bool * alreadyVisible;
-  bool isAlreadyVisible(int Face);
-  void addFace(int Face);
-  int lastTex;
-
-
   // Deques to store the visible faces
   ::std::deque<int> trasparent; //!< the ones with transparancy go here
