Sign in to follow this  
Followers 0

rendering Quake 3 bsp geometry

0 posts in this topic

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



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


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
  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; (name.c_str(), istream::binary);
 if(bsp::bspfile == NULL)
   cout << "ERROR: No file named \""<< name <<"\" found" << endl;
      char magic[64];                 //Number used in Quake 3 BSP header , 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;
           int version;
           char vbuffer[4];                
 , 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;
                for (int i = 0; i <= 16; i++)
                    char lumpoffset[4];
                    char lumplength[4];
                    //Read lumps offset
          , 4);                    
                    for (int k = 0; k <= 3; k++)    
                        ((char*)&bsp::lumps[i].offset)[k] = lumpoffset[k];
                    //Read lumps length   
          , 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::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];           
          , 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         
          , 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    
          , 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    
          , 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    
         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);

bsp bspbuffer;

bool initGL()
 //Initialize Projection Matrix
 glMatrixMode( GL_PROJECTION );
 //Initialize Modelview Matrix
 glMatrixMode( GL_MODELVIEW );
 //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;
 //Clear color buffer
 //Render quad
 //Update screen
 //While there are events to handle
 while( SDL_PollEvent( &event ) )
       if(event.type == SDL_QUIT)
          running = false;
 SDL_Delay( 1000 / 30 );
bool init()
 //Initialize SDL
 if( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )
    return false;
 //Create Window
    return false;
 //Initialize OpenGL
 if( initGL() == false )
    return false;
 //Set caption
 SDL_WM_SetCaption( "OpenGL BSP", NULL );
 return true;

#undef main

int main()
 return 0;

any help would be appreciated

Edited by industrialgrok

Share this post

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  
Followers 0

  • Similar Content

    • By Jon Alma
      Some time ago I implemented a particle system using billboarding techniques to ensure that the particles are always facing the viewer.  These billboards are always centered on one 3d coordinate.
      I would like to build on this and use billboarding as the basis for things like laser bolts and gunshots.  Here the difference is that instead of a single point particle I now have to draw a billboard between two points - the start and end of the laser bolt for example.  I appreciate that having two end points places limits on how much the billboard can be rotated to face the viewer, but I'm looking to code a best effort solution.  For the moment I am struggling to work out how to do this or find any tutorials / code examples that explain how to draw a billboard between two points ... can anyone help?
    • By Sagaceil
      It's always better to fight with a bro.
    • By recp
      I'm working on new asset importer ( based on COLLADA specs, the question is not about COLLADA directly
      also I'm working on a new renderer to render ( imported document.
      In the future I'll spend more time on this renderer of course, currently rendering imported (implemented parts) is enough for me
      assetkit imports COLLADA document (it will support glTF too),
      importing scene, geometries, effects/materials, 2d textures and rendering them seems working
      My actual confusion is about shaders. COLLADA has COMMON profile and GLSL... profiles,
      GLSL profile provides shaders for effects so I don't need to wory about them just compile, link, group them before render

      The problem occours in COMMON profile because I need to write shaders,
      Actually I wrote them for basic matrials and another version for 2d texture
      I would like to create multiple program but I am not sure how to split this this shader into smaller ones,

      Basic material version (only colors):
      Texture version:
      I used subroutines to bind materials, actually I liked it,
      In scene graph every node can have different program, and it switches between them if parentNode->program != node->program
      (I'll do scene graph optimizations e.g.  view frustum culling, grouping shaders... later)

      I'm going to implement transparency but I'm considering to create separate shaders,
      because default shader is going to be branching hell
      I can't generate shader for every node because I don't know how many node can be exist, there is no limit.
      I don't know how to write a good uber-shader for different cases:

      Here material struct:
      struct Material { ColorOrTexture emission; ColorOrTexture ambient; ColorOrTexture specular; ColorOrTexture reflective; ColorOrTexture transparent; ColorOrTexture diffuse; float shininess; float reflectivEyety; float transparency; float indexOfRefraction; }; ColorOrTexture could be color or 2d texture, if there would be single colorOrTex then I could split into two programs,
      Also I'm going to implement transparency, I am not sure how many program that I needed

      I'm considering to maintain a few default shaders for COMMON profile,
      1-no-texture, 2-one of colorOrTexture contains texture, 3-........

      Any advices in general or about how to optimize/split (if I need) these shaders which I provied as link?
      What do you think the shaders I wrote, I would like to write them without branching if posible,
      I hope I don't need to write 50+ or 100+ shaders, and 100+ default programs

      PS: These default shaders should render any document, they are not specific, they are general purpose...
             I'm compiling and linking default shaders when app launched

    • By CircleOfLight97
      Hi guys,
      I would like to contribute to a game project as a developer (open source possibly). I have some experiences in C/C++ in game development (perso projects). I don't know either unreal or unity but I have some knowledges in opengl, glsl and shading theory as I had some courses at university regarding to that. I have some knowledges in maths and basic in physics. I know a little how to use blender to do modelling, texturing and simple game assets (no characters, no animation no skinning/rigging). I have no game preferences but I like aventure game, dungeon crawler, platformers, randomly generated things. I know these kind of projects involve a lot of time and I'd be really to work on it but if there are no cleary defined specific design goals/stories/gameplay mechanics I would like to not be part of it x) and I would rather prefer a smaller but well defined project to work on that a huge and not 'finishable' one.
    • By gamesthatcouldbeworse
      Hi, I finally released KILL COMMANDO on gamejolt for free. It is a retro-funsplatter-shooter with C64 style. Give it a try.
  • Popular Now