Jump to content

  • Log In with Google      Sign In   
  • Create Account

Noah2033

Member Since 17 Feb 2013
Offline Last Active Sep 19 2013 09:22 PM

Topics I've Started

Opengl Occlusion queries

06 July 2013 - 04:28 PM

Hello, Ive been working on an Occlusion culler that uses opengl's occlusion queries

although when I render the scene meshes can occlude meshes but when models are in front of each other they still render.

here is the code

void renderer::draw()
{
 for( int m = 0; m < renderer::meshes.size(); m++ )
    {
	 glPushMatrix();
	  if(renderer::meshes[m].yaw != 0 )
	    glRotatef(renderer::meshes[m].yaw,1.0,0.0,0.0);
	  
	  if(renderer::meshes[m].pitch != 0 )		
	    glRotatef(renderer::meshes[m].pitch,0.0,1.0,0.0); 
	  
	  if(renderer::meshes[m].roll != 0 )
	    glRotatef(renderer::meshes[m].roll,0.0,0.0,1.0);
	 
	 if((renderer::meshes[m].scaley != 1)&&(renderer::meshes[m].scaley != 1)&&(renderer::meshes[m].scalez != 1))
	    glScalef(renderer::meshes[m].scaley,renderer::meshes[m].scaley,renderer::meshes[m].scalez);
	 
	 if((renderer::meshes[m].x != 0)&&(renderer::meshes[m].y != 0)&&(renderer::meshes[m].z != 0))	 	
        glTranslatef(renderer::meshes[m].x,renderer::meshes[m].y,renderer::meshes[m].z);	
	 
	 for( int l = 0; l < renderer::meshes[m].trislists.size(); l++ )
        {
         glGenQueriesARB( 1, &renderer::meshes[m].trislists[l].query );
         glBeginQueryARB( GL_SAMPLES_PASSED_ARB, renderer::meshes[m].trislists[l].query);
		 for( int t = 0; t < renderer::meshes[m].trislists[l].tris.size(); t++ )
             {   
              drawtriangle(renderer::meshes[m].trislists[l].tris[t]);
             }
		 glEndQueryARB( GL_SAMPLES_PASSED_ARB );
		}
	 glPopMatrix();
    }
 glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  
 for( int m = 0; m < renderer::meshes.size(); m++ )
    {
	 for( int l = 0; l < renderer::meshes[m].trislists.size(); l++ )
        {
		 glGetQueryObjectuivARB( renderer::meshes[m].trislists[l].query, GL_QUERY_RESULT_ARB, &renderer::meshes[m].trislists[l].fragmentcount);
		}
    }  
  
for( int m = 0; m < renderer::meshes.size(); m++ )
    {
	 glPushMatrix();
	  if(renderer::meshes[m].yaw != 0 )
	    glRotatef(renderer::meshes[m].yaw,1.0,0.0,0.0);
	  
	  if(renderer::meshes[m].pitch != 0 )		
	    glRotatef(renderer::meshes[m].pitch,0.0,1.0,0.0); 
	  
	  if(renderer::meshes[m].roll != 0 )
	    glRotatef(renderer::meshes[m].roll,0.0,0.0,1.0);
	 
	 if((renderer::meshes[m].scaley != 1)&&(renderer::meshes[m].scaley != 1)&&(renderer::meshes[m].scalez != 1))
	    glScalef(renderer::meshes[m].scaley,renderer::meshes[m].scaley,renderer::meshes[m].scalez);
	 
	 if((renderer::meshes[m].x != 0)&&(renderer::meshes[m].y != 0)&&(renderer::meshes[m].z != 0))	 	
        glTranslatef(renderer::meshes[m].x,renderer::meshes[m].y,renderer::meshes[m].z);	
	 
	 for( int l = 0; l < renderer::meshes[m].trislists.size(); l++ )
        {
		if(renderer::meshes[m].trislists[l].fragmentcount != 0 )
		  {
		   glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
         //draw
         for( int t = 0; t < renderer::meshes[m].trislists[l].tris.size(); t++ )
             {         
              drawtriangle(renderer::meshes[m].trislists[l].tris[t]);
		     }
		   glPolygonMode( GL_FRONT_AND_BACK, GL_FILL );
		  }
		  
		  
		}
	 glPopMatrix();
    }

 //clear
 renderer::meshes.clear();
}

Loading Quake 2 BSP vis data

17 June 2013 - 08:08 PM

so, I've made a Quake 2 bsp loader but I'm having trouble loading the visibility data here is my code

#include <stdio.h>                                   
#include <cstdio>
#include <string>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <GL/GL.h>                                    
#include <SDL/SDL.h>
#include <assert.h>                                   
#include <vector>
#include <bitset>
using namespace std;

int SCREEN_WIDTH = 640;
int SCREEN_HEIGHT = 480;
int SCREEN_BPP = 24;
bool running = true;
bool lightmaps;
SDL_Event event;

struct pos
{
 float x;
 float y;
 float z;
};

struct bspface
{
 uint16_t     plane;             // index of the plane the face is parallel to
 uint16_t     plane_side;        // set if the normal is parallel to the plane normal
 unsigned int first_edge;        // index of the first edge (in the face edge array)
 uint16_t     num_edges;         // number of consecutive edges (in the face edge array)
 uint16_t     texture_info;      // index of the texture info structure	
 uint8_t      lightmap_syles[4]; // styles (bit flags) for the lightmaps
 unsigned int lightmap_offset;   // offset of the lightmap (in bytes) in the lightmap lump
};

struct bspvertex
{
 pos position;
};

struct point3s
{
 int16_t x;
 int16_t y;
 int16_t z;
};

struct bspplane
{
 float   normal[3];      // A, B, C components of the plane equation
 float     distance;    // D component of the plane equation
 unsigned  int type;        // ?
};

struct bsptexture
{
 float          u_axis[3];
 float          u_offset;
 float          v_axis[3];
 float          v_offset;
 
 unsigned int   flags;
 unsigned int   value;
 
 char           name[32];
 unsigned int   next_texinfo;
};

struct bspnode
{
 unsigned int plane;             // index of the splitting plane (in the plane array)
 unsigned int    front_child;       // index of the front child node or leaf
 unsigned int    back_child;        // index of the back child node or leaf
   
 float  bbox_min[3];          // minimum x, y and z of the bounding box
 float  bbox_max[3];          // maximum x, y and z of the bounding box
	
 uint16_t   first_face;        // index of the first face (in the face array)
 uint16_t   num_faces;         // number of consecutive edges (in the face array)
};

struct bspedge
{
 uint16_t vertex[2]; // Indices into vertex array
};


struct bspvis
{
 unsigned int pvs;   // offset (in bytes) from the beginning of the visibility lump
 unsigned int phs;   // ?
};

struct bsplump
{
 int offset;
 int length;
};

class bsp
{
 public:
  ifstream    bspfile;
  bsplump     lumps[19];
  char        entities[10000];
  vector <bspvertex>	vertices;
  vector <bspface>      faces;
  vector <bsptexture>   textures;
  vector <bspplane>     planes;
  vector <bspnode>      nodes;
  vector <bspedge>      edges;
  vector <unsigned int> faceedges;
  vector <bspvis>       visarray;
  void load(string);
  void render();  
};

void bsp::load(string name)
{
 cout << "Loading BSP \"" << name << "\"" << endl;
 bsp::bspfile.open (name.c_str(), istream::binary);
 if(bsp::bspfile == NULL)
   cout << "ERROR: No file named \""<< name <<"\" found" << endl;
 else
     {
      char magic[64];                 //Number used in Quake 3 BSP header    
      bsp::bspfile.read(magic, 4);    //Read the magic number in the header of the BSP file it should be "IBSP"
      if((magic[0] != 'I')||(magic[1] != 'B')||(magic[2] != 'S')||(magic[3] != 'P'))
        {
         cout << "ERROR: Not a valid Quake 2 BSP file" << endl;
        }
      else
          {
           int version;
           char vbuffer[4];                
           bsp::bspfile.read(vbuffer, 4);   
           for (int k = 0; k <= 3; k++)    
              {
               ((char*)&version)[k] = vbuffer[k];
              }
              
           if(version != 38)//46 = 0x2e in hexidecimal
             cout << "ERROR: Unknown version of Quake 2 BSP" << endl;
           else
               {
                for (int i = 0; i <= 18; i++)
                   {
                    char lumpoffset[4];
                    char lumplength[4];
                    
                    //Read lumps offset
                    bsp::bspfile.read(lumpoffset, 4);                    
                    for (int k = 0; k <= 3; k++)    
                       {
                        ((char*)&bsp::lumps[i].offset)[k] = lumpoffset[k];
                       }
                       
                    //Read lumps length   
                    bsp::bspfile.read(lumplength, 4);                    
                    for (int k = 0; k <= 3; k++)    
                       {
                        ((char*)&bsp::lumps[i].length)[k] = lumplength[k];
                       }               
                   }
                   
                //Load entities (LUMP 0)
                bsp::bspfile.seekg (bsp::lumps[0].offset, ios::beg);
                bsp::bspfile.read(bsp::entities, bsp::lumps[0].length);  
				
			    //Load planes (LUMP 1)
                bsp::bspfile.seekg (bsp::lumps[1].offset, ios::beg);
                for (int j = 0; j <= bsp::lumps[1].length/sizeof(bspplane); j++) //Read until end of lump
                   {
                    char buffer[20];                 //create buffer for Leaffaces         
                    bsp::bspfile.read(buffer, 20);   //Read
					bspplane plane;
                    for (int k = 0; k <= 19; k++)    //Read until end of lump
                       {
                        ((char*)&plane)[k] = buffer[k];
                       }
					bsp::planes.push_back(plane);
                   }
				   
                bsp::bspfile.seekg (bsp::lumps[2].offset, ios::beg); //Load vertex data from vertex lump (10)
                for (int j = 0; j <= (bsp::lumps[2].length/sizeof(bspvertex)) -1; j++)//Read until end of lump
                   {
                    char buffer[12];           //create buffer for verts    
                    bsp::bspfile.read(buffer, 12);   //Read
					bspvertex vertex;
                    for (int k = 0; k <= 11; k++)//Read until end of lump
                       {
                        ((char*)&vertex)[k] = buffer[k];
                       }
					bsp::vertices.push_back(vertex);
                   }
				   
				bsp::bspfile.seekg (bsp::lumps[3].offset, ios::beg); //Load vertex data from vertex lump (10)
                unsigned int nclusters;
				char buffer[8];           //create buffer for verts    
                bsp::bspfile.read(buffer, 4);   //Read
				for (int k = 0; k <= 3; k++)//Read until end of lump
                   {
                    ((char*)&nclusters)[k] = buffer[k];
                   }
				cout << nclusters << endl; //189
				bspvis visarray[200];
				for (int v = 0; v <=  nclusters - 1; v++)//Read until end of lump	
				    {
                     bsp::bspfile.seekg (bsp::lumps[3].offset + 4 + (v * 8), ios::beg);
					 cout << v << endl;
				     bsp::bspfile.read(buffer, 8);
                     for (int k = 0; k <= 7; k++)//Read until end of lump
                        {
                         ((char*)&visarray[v])[k] = buffer[k];
                        }	
				     cout << visarray[v].pvs << endl; 
					 char vis[6];
					 bsp::bspfile.seekg (bsp::lumps[3].offset + visarray[v].pvs, ios::beg);
					 bsp::bspfile.read(vis, 8);   //Read
				     unsigned char mask;
				     bitset<8> x = vis[0];
				     cout<< x << endl;
					 cout << endl;
                     //bsp::bspfile.seekg (bsp::lumps[3].offset + visarray[v].pvs, ios::beg);
                    }

				   
				 //Load nodes    (LUMP 4)            
                bsp::bspfile.seekg (bsp::lumps[4].offset, ios::beg);
                for (int j = 0; j <= bsp::lumps[4].length/sizeof(bspnode); j++) //Read until end of lump
                   {				   
                    char buffer[28];           
                    bsp::bspfile.read(buffer, 28);  
                    bspnode node;
                    for (int k = 0; k <= 27; k++)//Read until end of lump
                       {
                        ((char*)&node)[k] = buffer[k];
                       }
				    bsp::nodes.push_back(node); 
                   }
				   
                //Load textures    (LUMP 5)            
                bsp::bspfile.seekg (bsp::lumps[5].offset, ios::beg);
                for (int j = 0; j <= bsp::lumps[5].length/sizeof(bsptexture); j++) //Read until end of lump
                   {				   
                    char buffer[76];           
                    bsp::bspfile.read(buffer, 76);  
                    bsptexture texture;
                    for (int k = 0; k <= 75; k++)//Read until end of lump
                       {
                        ((char*)&texture)[k] = buffer[k];
                       }
				    bsp::textures.push_back(texture);
                   }
                   
                //Load faces    (LUMP 6)            
                bsp::bspfile.seekg (bsp::lumps[6].offset, ios::beg);
                for (int j = 0; j <= bsp::lumps[6].length/sizeof(bspface); j++) //Read until end of lump
                   {				   
                    char buffer[20];           
                    bsp::bspfile.read(buffer, 20);  
                    bspface face;
                    for (int k = 0; k <= 19; k++)//Read until end of lump
                       {
                        ((char*)&face)[k] = buffer[k];
                       }
					bsp::faces.push_back(face);
                   }
				   
				bsp::bspfile.seekg (bsp::lumps[12].offset, ios::beg);
                for (int j = 0; j <= bsp::lumps[12].length/sizeof(4); j++) //Read until end of lump
                   {
				    char buffer[4];           
                    bsp::bspfile.read(buffer, 4);  
                    unsigned int faceedge;
                    for (int k = 0; k <= 3; k++)//Read until end of lump
                       {
                        ((char*)&faceedge)[k] = buffer[k];
                       }
					bsp::faceedges.push_back(faceedge);
                   }
				   
				bsp::bspfile.seekg (bsp::lumps[11].offset, ios::beg);
                for (int j = 0; j <= bsp::lumps[11].length/sizeof(bspedge); j++) //Read until end of lump
                   {
				    char buffer[4];           
                    bsp::bspfile.read(buffer, 4);  
                    bspedge edge;
                    for (int k = 0; k <= 3; k++)//Read until end of lump
                       {
                        ((char*)&edge)[k] = buffer[k];
                       }
					bsp::edges.push_back(edge);
                   }				   

               }
          }

     }
}

void bsp::render()
{ glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
 for (int j=0; j< bsp::lumps[6].length/sizeof(bspface); j++)
    {
 glBegin(GL_TRIANGLE_FAN);

 for (int i=0; i< bsp::faces[j].num_edges; i++)
    {
	/*
     /*if(g_bLightmaps)
        glTexCoord2f(pFaceTexCoords[iFace].pLightmapCoords[i].fS, pFaceTexCoords[iFace].pLightmapCoords[i].fT);
     else
          glTexCoord2f(pFaceTexCoords[iFace].pTexCoords[i].fS, pFaceTexCoords[iFace].pTexCoords[i].fT);

     // normal
     VECTOR3D vNormal = pPlanes[pFaces[iFace].iPlane].vNormal;
    if (pFaces[iFace].nPlaneSide)
    vNormal = vNormal * -1;
    glNormal3f(vNormal.x, vNormal.y, vNormal.z);*/

   int iEdge = bsp::faceedges[bsp::faces[j].first_edge + i]; // This gives the index into the edge lump

   if (iEdge > 0)
     {
      glVertex3f(bsp::vertices[bsp::edges[iEdge].vertex[0]].position.x, bsp::vertices[bsp::edges[iEdge].vertex[0]].position.y, bsp::vertices[bsp::edges[iEdge].vertex[0]].position.z);
     }
   else
       {
        iEdge *= -1;
       glVertex3f(bsp::vertices[bsp::edges[iEdge].vertex[1]].position.x, bsp::vertices[bsp::edges[iEdge].vertex[1]].position.y, bsp::vertices[bsp::edges[iEdge].vertex[1]].position.z);
       }
   }
 glEnd();
 }
}

bsp bspbuffer;

bool initGL()
{
 //Initialize Projection Matrix
 glMatrixMode( GL_PROJECTION );
 glLoadIdentity();
 //Initialize Modelview Matrix
 glMatrixMode( GL_MODELVIEW );
 glLoadIdentity();
 //Initialize clear color
 glClearColor( 0.f, 0.f, 0.f, 1.f );
 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
 
 return true;
}
 
float angle;

void render()
{
 glEnable(GL_LIGHTING);
 glEnable(GL_LIGHT0);
  glEnable(GL_NORMALIZE);
  
 angle = angle + 1;
 glPushMatrix();
 //Clear color buffer
 glClear( GL_COLOR_BUFFER_BIT );
 
 //Add ambient light
  GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color(0.2, 0.2, 0.2)
  glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);
  
  
	    //Add positioned light
    GLfloat lightColor0[] = {0.5f, 0.5f, 0.5f, 1.0f}; //Color (0.5, 0.5, 0.5)
    GLfloat lightPos0[] = {4.0f, 0.0f, 8.0f, 1.0f}; //Positioned at (4, 0, 8)
    glLightfv(GL_LIGHT0, GL_DIFFUSE, lightColor0);
    glLightfv(GL_LIGHT0, GL_POSITION, lightPos0);
	
	    //Add directed light
    GLfloat lightColor1[] = {0.5f, 0.2f, 0.2f, 1.0f}; //Color (0.5, 0.2, 0.2)
    //Coming from the direction (-1, 0.5, 0.5)
    GLfloat lightPos1[] = {-1.0f, 0.5f, 0.5f, 0.0f};
    glLightfv(GL_LIGHT1, GL_DIFFUSE, lightColor1);
    glLightfv(GL_LIGHT1, GL_POSITION, lightPos1);
	
 //Render quad
 glPointSize(5.0);
 glRotatef(angle,1,1,1);
 glScalef(.002,.002,.002);
 bspbuffer.render();
 //Update screen
 glPopMatrix();
 SDL_GL_SwapBuffers();
 
 //While there are events to handle
 while( SDL_PollEvent( &event ) )
      {
       if(event.type == SDL_QUIT)
         {
          running = false;
          exit(0);
         }
      }       
 SDL_Delay( 1000 / 30 );
}
 
bool init()
{
 //Initialize SDL
 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
   {
    return false;
   }
 //Create Window
 if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
   {
    return false;
   }
 //Initialize OpenGL
 if( initGL() == false )
   {
    return false;
   }
 //Set caption
 SDL_WM_SetCaption( "OpenGL BSP", NULL );
 return true;
}

#undef main

int main()
{
 init();
 bspbuffer.load("q2.bsp");
 do
   {
    render();
   }while(running);   
 return 0;
}

if anyone could point me in the right direction that would be greatly appreciated


rendering Quake 3 bsp geometry

13 April 2013 - 05:43 PM

I have been working on rendering Quake 3 bsp maps for awhile but I cant get the faces to render correctly

 

 

http://postimg.org/image/o2lf8vlp9/ Here are the vertices for a map

 

 

and here's what happens when I render faces on a map

 

http://postimg.org/image/6hmoht7ep/

 

here's the code

#include <stdio.h>                                   
#include <cstdio>
#include <string>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <sstream>
#include <GL/GL.h>                                    
#include <SDL/SDL.h>
#include <assert.h>                                   

using namespace std;

int SCREEN_WIDTH = 640;
int SCREEN_HEIGHT = 480;
int SCREEN_BPP = 24;
bool running = true;
bool lightmaps;
SDL_Event event;

#define MAX_BRUSHES     10000
#define MAX_FACES       10000
#define MAX_VERTS       10000000
#define MAX_TEXTURES    1000
#define MAX_LEAFFACES   65536

struct pos
{
 float x;
 float y;
 float z;
};

struct bspface
{
 int   textureid;        // The index into the texture array
 int   effect;           // The index for the effects (or -1 = n/a)
 int   type;             // 1=polygon, 2=patch, 3=mesh, 4=billboard
 int   vertexindex;      // The index into this face's first vertex
 int   numofverts;       // The number of vertices for this face
 int   meshvertindex;    // The index into the first meshvertex
 int   nummeshverts;     // The number of mesh vertices
 int   lightmapid;       // The texture index for the lightmap
 int   lmapcorner[2];    // The face's lightmap corner in the image
 int   lmapsize[2];      // The size of the lightmap section
 float lmappos[3];     // The 3D origin of lightmap.
 float lmapbitsets[2][3]; // The 3D space for s and t unit vectors.
 float vnormal[3];     // The face normal.
 int   size[2];          // The bezier patch dimensions.
};

struct bspvertex
{
 pos position;      //x y z
 float texturecoord[2];  //u, v texture coordinate
 float lightmapcoord[2]; //u, v lightmap coordinate
 float normal[3];        //x, y, z normalized vector
 char  color[4];         //RGBA color for the vertex
};

struct bsptexture
{
 char name[64];      // The name of the texture w/o the extension
 int flags;          // The surface flags (unknown)
 int contents;       // The content flags (unknown)
};

struct bspbrush
{
 int brushSide;           // The starting brush side for the brush
 int numofbrushsides;     // Number of brush sides for the brush
 int textureid;           // The texture index for the brush
};

struct bsplump
{
 int offset;
 int length;
};

class bsp
{
 public:
  ifstream    bspfile;
  bsplump     lumps[16];
  char        entities[10000];
  bspvertex   vertices[MAX_VERTS];
  bspface     faces[MAX_FACES];
  bsptexture  textures[MAX_TEXTURES];
  bspbrush    brushs[MAX_BRUSHES];
  int         faceindex[MAX_LEAFFACES];
  void load(string);
  void render();  
};

void bsp::load(string name)
{
 cout << "Loading BSP \"" << name << "\"" << endl;
 bsp::bspfile.open (name.c_str(), istream::binary);
 if(bsp::bspfile == NULL)
   cout << "ERROR: No file named \""<< name <<"\" found" << endl;
 else
     {
      char magic[64];                 //Number used in Quake 3 BSP header    
      bsp::bspfile.read(magic, 4);    //Read the magic number in the header of the BSP file it should be "IBSP"
      if((magic[0] != 'I')||(magic[1] != 'B')||(magic[2] != 'S')||(magic[3] != 'P'))
        {
         cout << "ERROR: Not a valid Quake 3 BSP file" << endl;
        }
      else
          {
           int version;
           char vbuffer[4];                
           bsp::bspfile.read(vbuffer, 4);   
           for (int k = 0; k <= 3; k++)    
              {
               ((char*)&version)[k] = vbuffer[k];
              }
              
           if(version != 46)//46 = 0x2e in hexidecimal
             cout << "ERROR: Unknown version of Quake 3 BSP" << endl;
           else
               {
                for (int i = 0; i <= 16; i++)
                   {
                    char lumpoffset[4];
                    char lumplength[4];
                    
                    //Read lumps offset
                    bsp::bspfile.read(lumpoffset, 4);                    
                    for (int k = 0; k <= 3; k++)    
                       {
                        ((char*)&bsp::lumps[i].offset)[k] = lumpoffset[k];
                       }
                       
                    //Read lumps length   
                    bsp::bspfile.read(lumplength, 4);                    
                    for (int k = 0; k <= 3; k++)    
                       {
                        ((char*)&bsp::lumps[i].length)[k] = lumplength[k];
                       }
                       
                    cout << "Lump " << i << " offset is " << bsp::lumps[i].offset << endl
                         << "Lump " << i << " length is " << bsp::lumps[i].length << endl << endl;                   
                   }
                   
                //Load entities (LUMP 0)
                bsp::bspfile.seekg (bsp::lumps[0].offset, ios::beg);
                bsp::bspfile.read(bsp::entities, bsp::lumps[0].length);  
                
                //Load textures    (LUMP 1)            
                bsp::bspfile.seekg (bsp::lumps[1].offset, ios::beg);
                for (int j = 0; j <= bsp::lumps[1].length/sizeof(bsptexture); j++) //Read until end of lump
                   {               
                    char buffer[72];           
                    bsp::bspfile.read(buffer, 72);  
                    
                    for (int k = 0; k <= 71; k++)//Read until end of lump
                       {
                        ((char*)&bsp::textures[j])[k] = buffer[k];
                       }
                   }
                   
                //Load Leaffaces (LUMP 5)
                bsp::bspfile.seekg (bsp::lumps[5].offset, ios::beg);
                for (int j = 0; j <= bsp::lumps[5].length/sizeof(bspvertex); j++) //Read until end of lump
                   {
                    char buffer[4];                 //create buffer for Leaffaces         
                    bsp::bspfile.read(buffer, 4);   //Read
                    for (int k = 0; k <= 3; k++)    //Read until end of lump
                       {
                        ((char*)&bsp::faceindex[j])[k] = buffer[k];
                       }
                   }
                   
                //Load vertices (LUMP 10)
                bsp::bspfile.seekg (bsp::lumps[10].offset, ios::beg); //Load vertex data from vertex lump (10)
                for (int j = 0; j <= bsp::lumps[10].length/sizeof(bspvertex); j++)//Read until end of lump
                   {
                    char buffer[44];           //create buffer for verts    
                    bsp::bspfile.read(buffer, 44);   //Read
                    for (int k = 0; k <= 43; k++)//Read until end of lump
                       {
                        ((char*)&bsp::vertices[j])[k] = buffer[k];
                       }
                   }
                
                //Load faces (LUMP 13)
                bsp::bspfile.seekg (bsp::lumps[13].offset, ios::beg); //Load face data from face lump (13)
                for (int j = 0; j <= bsp::lumps[13].length/sizeof(bspface); j++)//Read until end of lump
                   {
                    char buffer[104];                 //create buffer for faces    
                    bsp::bspfile.read(buffer, 104);   //Read
                    for (int k = 0; k <= 103; k++)    //Read until end of lump
                       {
                        ((char*)&bsp::faces[j])[k] = buffer[k];
                       }
                   }
               }
          }

     }
}
void bsp::render()
{  
 for (int j = 0; j <= bsp::lumps[13].length/sizeof(bspface); j++)//Read until end of lump
    {
     if ((bsp::faces[j].type == 1)||(bsp::faces[j].type == 3))       // 1=polygon, 2=patch, 3=mesh, 4=billboard    
       {        
         glFrontFace(GL_CW);
         glBegin(GL_TRIANGLE_STRIP);
         for (int k = 0; k <=  bsp::faces[j].numofverts - 1; k++)//Read until end of lump
            {
             glVertex3f(bsp::vertices[bsp::faces[j].vertexindex+k].position.x, bsp::vertices[bsp::faces[j].vertexindex+k].position.y, bsp::vertices[bsp::faces[j].vertexindex+k].position.z);
            }
         glEnd();    
        }
    }
}

bsp bspbuffer;

bool initGL()
{
 //Initialize Projection Matrix
 glMatrixMode( GL_PROJECTION );
 glLoadIdentity();
 //Initialize Modelview Matrix
 glMatrixMode( GL_MODELVIEW );
 glLoadIdentity();
 //Initialize clear color
 glClearColor( 0.f, 0.f, 0.f, 1.f );
 //glPolygonMode( GL_FRONT_AND_BACK, GL_LINE );
 return true;
}
 
float angle;

void render()
{
 angle = angle + 1;
 glPushMatrix();
 //Clear color buffer
 glClear( GL_COLOR_BUFFER_BIT );
 //Render quad
 glPointSize(5.0);
 glRotatef(angle,1,1,1);
 glScalef(.002,.002,.002);
 bspbuffer.render();
 //Update screen
 glPopMatrix();
 SDL_GL_SwapBuffers();
 
 //While there are events to handle
 while( SDL_PollEvent( &event ) )
      {
       if(event.type == SDL_QUIT)
         {
          running = false;
          exit(0);
         }
      }       
 SDL_Delay( 1000 / 30 );
}
 
bool init()
{
 //Initialize SDL
 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
   {
    return false;
   }
 //Create Window
 if( SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_OPENGL ) == NULL )
   {
    return false;
   }
 //Initialize OpenGL
 if( initGL() == false )
   {
    return false;
   }
 //Set caption
 SDL_WM_SetCaption( "OpenGL BSP", NULL );
 return true;
}

#undef main

int main()
{
 init();
 bspbuffer.load("test1.bsp");
 do
   {
    render();
   }while(running);   
 return 0;
}

any help would be appreciated


Cannot run ODE code (open dynamics engine)

25 February 2013 - 07:39 PM

so a while back I messed with ode because i was going to impliment it in my engine, its been about a month so i dont have the compile command in my terminal history and i forgot specificly what i used to compile it but here is the code

 

#include <ode/ode.h>
#include <drawstuff/drawstuff.h>
#include <string.h>
#include <iostream>

#ifdef dDOUBLE
#define dsDrawSphere dsDrawSphereD
#endif

using namespace std;
const int VertexCount = 5;
const int IndexCount = 12;

static dVector3 Size;
static float Vertices[VertexCount][3];
static dTriIndex Indices[IndexCount];

static dJointGroupID contactgroup;
//static int flag = 0;
dsFunctions fn;


const double   radius = 0.2;
const double   mass   = 1.0;

struct cworld
{
 int numobjects;
 dSpaceID space;
 dWorldID world;
};

struct object
{
 string name;
 string type;
 const double *position;
 const double *rotation;
 dBodyID body;
 dGeomID geom;
 dMass mass;
};
cworld world;

object objects[10];
//object objects[0];
//object objects[1];
//object objects[3];

static void nearCallback(void *data, dGeomID o1, dGeomID o2)
{
  const int N = 10;
  dContact contact[N];
  int n;

  for(int i = 0; i <= world.numobjects; i++)
     {
      n =  dCollide(o1,o2,N,&contact[0].geom,sizeof(dContact));
      if(n >= 1)                                   //object hit somthing
        {
         if(objects[i].geom == o1)
           {
            //cout << objects[i].type << endl;
            for(int j = 0; j <= world.numobjects; j++)
               {
                if(objects[j].geom == o2)
                  {
                   cout << objects[j].name << " is hitting a " << objects[j].name << endl;
                  }  
               }
           }
         }
     }

    for(int i = 0; i < n; i++) 
       {
        contact[i].surface.mode = dContactBounce;
        contact[i].surface.mu   = dInfinity;
        contact[i].surface.bounce     = 0.0; // (0.0~1.0) restitution parameter
        contact[i].surface.bounce_vel = 0.0; // minimum incoming velocity for bounce
        dJointID c = dJointCreateContact(world.world,contactgroup,&contact[i]);
        dJointAttach (c,dGeomGetBody(contact[i].geom.g1),dGeomGetBody(contact[i].geom.g2));
       }

}


static void simLoop (int pause)
{
  //flag = 0;
  dSpaceCollide(world.space,0,&nearCallback);

  dWorldStep(world.world,0.01);

  dJointGroupEmpty(contactgroup);

  /*if(flag == 0) 
    dsSetColor(1.0, 0.0, 0.0);
  else           
      dsSetColor(0.0, 0.0, 1.0);*/
                                   
  objects[0].position = dBodyGetPosition(objects[0].body);
  objects[0].rotation   = dBodyGetRotation(objects[0].body);
  dsDrawSphere(objects[0].position,objects[0].rotation,radius);

  objects[1].position = dBodyGetPosition(objects[1].body);
  objects[1].rotation   = dBodyGetRotation(objects[1].body);
  dsDrawSphere(objects[1].position,objects[1].rotation,radius);
}

void start()
{
  static float xyz[3] = {0.0,-3.0,1.0};
  static float hpr[3] = {90.0,0.0,0.0};
  dsSetViewpoint (xyz,hpr);
}

void  prepDrawStuff() {
  fn.version = DS_VERSION;
  fn.start   = &start;
  fn.step    = &simLoop;
  fn.command = NULL;
  fn.stop    = NULL;
  fn.path_to_textures = "../../drawstuff/textures";
}

int main (int argc, char *argv[])
{
  double x0 = 0.0, y0 = 0.0, z0 = 2.0;


  prepDrawStuff();

  dInitODE();
  world.world = dWorldCreate();
  world.space = dHashSpaceCreate(0);
  contactgroup = dJointGroupCreate(0);

  dWorldSetGravity(world.world,0,0,-0.5);
  //Create a trimesh
  Size[0] = 5.0f;
  Size[1] = 5.0f;
  Size[2] = 2.5f;
  
  Vertices[0][0] = -Size[0];
  Vertices[0][1] = -Size[1];
  Vertices[0][2] = Size[2];
  
  Vertices[1][0] = Size[0];
  Vertices[1][1] = -Size[1];
  Vertices[1][2] = Size[2];
  
  Vertices[2][0] = Size[0];
  Vertices[2][1] = Size[1];
  Vertices[2][2] = Size[2];
  
  Vertices[3][0] = -Size[0];
  Vertices[3][1] = Size[1];
  Vertices[3][2] = Size[2];
  
  Vertices[4][0] = 0;
  Vertices[4][1] = 0;
  Vertices[4][2] = 0;
  
  Indices[0] = 0;
  Indices[1] = 1;
  Indices[2] = 4;
  
  Indices[3] = 1;
  Indices[4] = 2;
  Indices[5] = 4;
  
  Indices[6] = 2;
  Indices[7] = 3;
  Indices[8] = 4;
  
  Indices[9] = 3;
  Indices[10] = 0;
  Indices[11] = 4;

  dTriMeshDataID Data = dGeomTriMeshDataCreate();
  //dGeomTriMeshDataBuildSimple(Data, (dReal*)Vertices, VertexCount, Indices, IndexCount);
  dGeomTriMeshDataBuildSingle(Data, Vertices[0], 3 * sizeof(float), VertexCount, &Indices[0], IndexCount, 3 * sizeof(dTriIndex));
  objects[4].geom = dCreateTriMesh(world.space, Data, 0, 0, 0);
  dGeomSetPosition(objects[4].geom, 0, 0, 0.0);
  objects[4].type = "world";
  objects[4].name = "crappy sky";
  world.numobjects = world.numobjects + 1;

  // Create a ground
  objects[3].geom = dCreatePlane(world.space,0,0,1,0);
  objects[3].type = "ground";
  objects[3].name = "ground(duh)";
  world.numobjects = world.numobjects + 1;

  // Create objects[0]
  objects[0].type = "sphere";
  objects[0].name = "ball1";
  objects[0].body = dBodyCreate(world.world);
  dMassSetZero(&objects[0].mass);
  dMassSetSphereTotal(&objects[0].mass,mass,radius);
  dBodySetMass(objects[0].body,&objects[0].mass);
  dBodySetPosition(objects[0].body, x0, y0, z0);
  objects[0].geom = dCreateSphere(world.space,radius);
  dGeomSetBody(objects[0].geom,objects[0].body);
  dBodySetLinearVel(objects[0].body, 1.0, 0.0,1.0); 
  world.numobjects = world.numobjects + 1;

  //Create objects[1]
  objects[1].type = "sphere";
  objects[1].name = "ball2";
  objects[1].body = dBodyCreate(world.world);
  dMassSetZero(&objects[1].mass);
  dMassSetSphereTotal(&objects[1].mass,mass,radius);
  dBodySetMass(objects[1].body,&objects[1].mass);
  dBodySetPosition(objects[1].body, -1.0, y0, 3.0);
  objects[1].geom = dCreateSphere(world.space,radius);
  dGeomSetBody(objects[1].geom,objects[1].body);
  world.numobjects = world.numobjects + 1;



  dsSimulationLoop (argc,argv,352,288,&fn);

  dWorldDestroy (world.world);
  dCloseODE();

  return 0;
}

 

here is the output :

sample6.cpp: In function ‘void simLoop(int)’:
sample6.cpp:102:57: error: cannot convert ‘const dReal* {aka const float*}’ to ‘const double*’ in assignment
sample6.cpp:103:59: error: cannot convert ‘const dReal* {aka const float*}’ to ‘const double*’ in assignment
sample6.cpp:104:62: error: cannot convert ‘const double*’ to ‘const float*’ for argument ‘1’ to ‘void dsDrawSphere(const float*, const float*, float)’
sample6.cpp:106:57: error: cannot convert ‘const dReal* {aka const float*}’ to ‘const double*’ in assignment
sample6.cpp:107:59: error: cannot convert ‘const dReal* {aka const float*}’ to ‘const double*’ in assignment
sample6.cpp:108:62: error: cannot convert ‘const double*’ to ‘const float*’ for argument ‘1’ to ‘void dsDrawSphere(const float*, const float*, float)’
 

when I fix these errors and run it I get a zero inertia runtime error

now I remember this code compiling and running fine. So I must be compiling it wrong (I bet its somthing stupid I simply forgot)

have any idea?


Problem rendering Quake 3 BSP files

17 February 2013 - 01:28 PM

Ok, so for the past week ive been working on a Quake 3 bsp veiwer this is what I have finished already

//standard librarys 
#include <SDL/SDL.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <cstdlib>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <cstdio>
#include <iostream>
#include <stdlib.h>
#include <sstream>

#include "sys_mem.h"
using namespace std;

#define SCREEN_WIDTH  640
#define SCREEN_HEIGHT 480
#define SCREEN_BPP    32

#define MAX_FACES  10000
#define MAX_VERTS  100000

struct bspface
{
 int textureID;           // The index into the texture array 
 int effect;              // The index for the effects (or -1 = n/a) 
 int type;                // 1=polygon, 2=patch, 3=mesh, 4=billboard 
 int vertexIndex;         // The index into this face's first vertex 
 int numOfVerts;          // The number of vertices for this face 
 int meshVertIndex;       // The index into the first meshvertex 
 int numMeshVerts;        // The number of mesh vertices 
 int lightmapID;          // The texture index for the lightmap 
 int lMapCorner[1];       // The face's lightmap corner in the image 
 int lMapSize[1];         // The size of the lightmap section 
 float lMapPos[2];        // The 3D origin of lightmap. 
 float lMapBitsets[1][2]; // The 3D space for s and t unit vectors. 
 float vNormal[2];        // The face normal. 
 int size[1];             // The bezier patch dimensions. 
};

//

struct bspvertex
{
 float position[2];      //x y z 
 float texturecoord[1];  //u, v texture coordinate
 float lightmapcoord[1]; //u, v lightmap coordinate
 float normal[2];        //x, y, z normalized vector
 char  color[3];         //RGBA color for the vertex //char = byte?
};

struct bsplump
{
 int offset;
 int length;
};

struct bsp
{
 bsplump   lumps[16];
 bspvertex vertices[MAX_VERTS];
 bspface   faces[MAX_FACES];
};

bsp bspbuffer;

bool running = true;  //if renderer is running 
bool lightmaps;

void loadbsp(string);    //prototype

void init()
{
 glClearColor(0.0,0.0,0.0,1.0);            //Background color
 glMatrixMode(GL_PROJECTION);
 glLoadIdentity();                         //Reset 
 gluPerspective(45,640.0/480.0,1.0,500.0); //Perspective 
 glMatrixMode(GL_MODELVIEW);
 glEnable(GL_CULL_FACE);
 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
 glEnable( GL_BLEND );
 glEnable(GL_DEPTH_TEST);
 glEnable(GL_LIGHTING);
 glEnable(GL_LIGHT0);
 glEnable(GL_LIGHT1); 
 glEnable(GL_COLOR_MATERIAL);

float light_0_amb[] = {0.2f, 0.2, 0.2};
glLightfv(GL_LIGHT0,GL_AMBIENT, light_0_amb);

float light_0_pos[] = {1.0,3.0,1.0};
glLightfv(GL_LIGHT0, GL_POSITION, light_0_pos);

float light_0_dif[] = {1.0,1.0,1.0,0.0};
glLightfv(GL_LIGHT0, GL_DIFFUSE, light_0_dif);

float light_0_spec[] = {1.0, 1.0, 1.0, 0.0};
glLightfv(GL_LIGHT0, GL_SPECULAR, light_0_spec);
} 

void display()
{
 glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
 glFlush();
 glLoadIdentity();   

 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); //OpenGL's texture parameters 
 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

 //RENDERLOOP

 SDL_GL_SwapBuffers();
}

int main(int argc, char *argv[])
{
 if(argv[1] == NULL)
   cout << "ERROR: No bsp file used as an argument example \"./bsploader.x86 -map.bsp\"" << endl;
 else
     {
      /*tolkenize argument*/
      istringstream namebuffer(argv[1]);  //zero is ./bsploader
      while (!namebuffer.eof())
           {
            string tolken;                      //tolken buffer
            getline(namebuffer, tolken, '-' );  //split and store in tolken buffer
            if(tolken != "")
              {
               loadbsp(tolken);
              }
           }
     do
       {
        SDL_Surface* screen=SDL_SetVideoMode(SCREEN_WIDTH,SCREEN_HEIGHT,SCREEN_BPP,SDL_SWSURFACE|SDL_OPENGL);
                           //Window           //Tab
        SDL_WM_SetCaption( "BSPloader","BSPloader");
        Uint32 start;
        init();
        display(); 
        SDL_Delay(1000/30);
       }while(running);
      
     }

 return 0;
}

void loadbsp(string name)
{
 cout << "Loading BSP \"" << name << "\"" << endl; 
 ifstream bspfile;
 bspfile.open (name.c_str(),ios::in|ios::binary);//Load BSP file into memory
 if(bspfile == NULL)
   cout << "ERROR: No file named \""<< name <<"\" found" << endl;
 else
     {
      char magic[64];           //Number used in BSP header	
      bspfile.read(magic, 4);   //Read the magic number in the header of the BSP file it should be "IBSP"
      if((magic[0] != 'I')||(magic[1] != 'B')||(magic[2] != 'S')||(magic[3] != 'P'))
        {
         cout << "ERROR: Not a valid Quake 3 BSP file" << endl;
        }
      else
          {
           int version;
           version = readInt(bspfile);
           if(version != 46)//46 = 0x2e in hexidecimal
             cout << "ERROR: Unknown version of Quake 3 BSP" << endl;
           else
               {
                for (int i = 0; i <= 16; i++)
                   {                 
                    bspbuffer.lumps[i].offset = readInt(bspfile);
                    bspbuffer.lumps[i].length = readInt(bspfile);
                    cout << "Lump " << i << " offset is " << bspbuffer.lumps[i].offset << endl
                         << "Lump " << i << " length is " << bspbuffer.lumps[i].length << endl << endl;                   
                   }
                //Load vertices
                bspfile.seekg (bspbuffer.lumps[10].offset, ios::beg); //Load vertex data from vertex lump (10)
                long readmemory = 0;
                cout << "Loading";

                for (int j = 0; readmemory <= bspbuffer.lumps[10].length; j++)//Read until end of lump
                   { 
                    //Actual vertexes
                    bspbuffer.vertices[j].position[0] = readFloat(bspfile);
                    bspbuffer.vertices[j].position[1] = readFloat(bspfile);
                    bspbuffer.vertices[j].position[2] = readFloat(bspfile);

                    //U,V coordinates
                    bspbuffer.vertices[j].texturecoord[0] = readFloat(bspfile);
                    bspbuffer.vertices[j].texturecoord[1] = readFloat(bspfile);

                    //Lightmap U,V coordinates
                    bspbuffer.vertices[j].lightmapcoord[0] = readFloat(bspfile);
                    bspbuffer.vertices[j].lightmapcoord[1] = readFloat(bspfile);

                    //Normalized vertex coordinates
                    bspbuffer.vertices[j].position[0] = readFloat(bspfile);
                    bspbuffer.vertices[j].position[1] = readFloat(bspfile);
                    bspbuffer.vertices[j].position[2] = readFloat(bspfile);

                    char buffer[64];           	
                    bspfile.read(buffer, 4); //Read RGBA color index
                    bspbuffer.vertices[j].color[0] = buffer[0];
                    bspbuffer.vertices[j].color[1] = buffer[1];
                    bspbuffer.vertices[j].color[2] = buffer[2];
                    bspbuffer.vertices[j].color[3] = buffer[3];
                    //10 floats 4 char so 10(4) + 4(1)
                    readmemory = readmemory + 44;  
                    cout << ".";       
                   }

                bspfile.seekg (bspbuffer.lumps[13].offset, ios::beg); //Load face data from vertex lump (13)
                long readmemory2 = 0;
                cout << "Loading";
                for (int b = 0; readmemory2 <= bspbuffer.lumps[13].length; b++)//Read until end of lump
                   { 
                    bspbuffer.faces[b].textureID     = readInt(bspfile); // The index into the texture array 
                    bspbuffer.faces[b].effect        = readInt(bspfile); // The index for the effects (or -1 = n/a) 
                    bspbuffer.faces[b].type          = readInt(bspfile); // 1=polygon, 2=patch, 3=mesh, 4=billboard 
                    bspbuffer.faces[b].vertexIndex   = readInt(bspfile); // The index into this face's first vertex
                    cout << bspbuffer.faces[b].vertexIndex << endl;
                    bspbuffer.faces[b].numOfVerts    = readInt(bspfile); // The number of vertices for this face 
                    bspbuffer.faces[b].meshVertIndex = readInt(bspfile); // The index into the first meshvertex 
                    bspbuffer.faces[b].numMeshVerts  = readInt(bspfile); // The number of mesh vertices 
                    bspbuffer.faces[b].lightmapID    = readInt(bspfile); // The texture index for the lightmap 

                    bspbuffer.faces[b].lMapCorner[0] = readInt(bspfile); // The face's lightmap corner in the image 
                    bspbuffer.faces[b].lMapCorner[1] = readInt(bspfile); // The face's lightmap corner in the image 

                    bspbuffer.faces[b].lMapSize[0]   = readInt(bspfile); // The size of the lightmap section 
                    bspbuffer.faces[b].lMapSize[1]   = readInt(bspfile); // The size of the lightmap section 

                    bspbuffer.faces[b].lMapPos[0]    = readFloat(bspfile); // The 3D origin of lightmap. 
                    bspbuffer.faces[b].lMapPos[1]    = readFloat(bspfile); // The 3D origin of lightmap. 
                    bspbuffer.faces[b].lMapPos[2]    = readFloat(bspfile); // The 3D origin of lightmap. 

                    bspbuffer.faces[b].lMapBitsets[0][0]  = readFloat(bspfile); // The 3D space for s and t unit vectors. 
                    bspbuffer.faces[b].lMapBitsets[0][1]  = readFloat(bspfile); // The 3D space for s and t unit vectors. 
                    bspbuffer.faces[b].lMapBitsets[0][2]  = readFloat(bspfile); // The 3D space for s and t unit vectors. 

                    bspbuffer.faces[b].lMapBitsets[1][0]  = readFloat(bspfile); // The 3D space for s and t unit vectors. 
                    bspbuffer.faces[b].lMapBitsets[1][1]  = readFloat(bspfile); // The 3D space for s and t unit vectors. 
                    bspbuffer.faces[b].lMapBitsets[1][2]  = readFloat(bspfile); // The 3D space for s and t unit vectors. 
             
                    bspbuffer.faces[b].vNormal[0]    = readFloat(bspfile); // The face normal. 
                    bspbuffer.faces[b].vNormal[1]    = readFloat(bspfile); // The face normal. 
                    bspbuffer.faces[b].vNormal[2]    = readFloat(bspfile); // The face normal. 

                    bspbuffer.faces[b].size[0] = readInt(bspfile);            // The bezier patch dimensions. 
                    bspbuffer.faces[b].size[1] = readInt(bspfile);             // The bezier patch dimensions. 
                    //26(4)
                    readmemory2 = readmemory2 + 104;       
                   }
                
                cout << "Loaded" << endl;
               }
          }

     }
}

the vertex information is mostly gibberish (as that two of the values are zero) Im aware that you have to use vertexIndex for the first vertex and put them into a triangle fan, but how?


PARTNERS