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