• Create Account

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

5 replies to this topic

### #1Noah2033  Members

161
Like
0Likes
Like

Posted 17 June 2013 - 08:08 PM

#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 render();
};

{
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];
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];

for (int k = 0; k <= 3; k++)
{
((char*)&bsp::lumps[i].offset)[k] = lumpoffset[k];
}

for (int k = 0; k <= 3; k++)
{
((char*)&bsp::lumps[i].length)[k] = lumplength[k];
}
}

bsp::bspfile.seekg (bsp::lumps[0].offset, ios::beg);

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
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
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
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;
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);
bitset<8> x = vis[0];
cout<< x << endl;
cout << endl;
//bsp::bspfile.seekg (bsp::lumps[3].offset + visarray[v].pvs, ios::beg);
}

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];
bspnode node;
for (int k = 0; k <= 27; k++)//Read until end of lump
{
((char*)&node)[k] = buffer[k];
}
bsp::nodes.push_back(node);
}

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];
bsptexture texture;
for (int k = 0; k <= 75; k++)//Read until end of lump
{
((char*)&texture)[k] = buffer[k];
}
bsp::textures.push_back(texture);
}

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];
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];
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];
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 );
//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()
{
glEnable(GL_LIGHTING);
glEnable(GL_LIGHT0);
glEnable(GL_NORMALIZE);

angle = angle + 1;
glPushMatrix();
//Clear color buffer
glClear( GL_COLOR_BUFFER_BIT );

GLfloat ambientColor[] = {0.2f, 0.2f, 0.2f, 1.0f}; //Color(0.2, 0.2, 0.2)
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, ambientColor);

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);

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);

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();
do
{
render();
}while(running);
return 0;
}


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

### #2mhagain  Members

12438
Like
1Likes
Like

Posted 18 June 2013 - 02:00 AM

I don't mean "load Quake 2 vis data", I mean just say "I'm having trouble" without saying what the trouble is, then give a code dump and ask others to debug it for you.

Have you cross-checked with the Quake 2 source code here?

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #3Noah2033  Members

161
Like
0Likes
Like

Posted 18 June 2013 - 01:53 PM

I don't mean "load Quake 2 vis data", I mean just say "I'm having trouble" without saying what the trouble is, then give a code dump and ask others to debug it for you.

Have you cross-checked with the Quake 2 source code here?

sorry I was in a hurry when I wrote this

I had to get to work, what Wanted to say was I do not think I am loading it correctly because I get multiple 1011's randomly since this is pvs data I would think the values would be unique. Also I know the data is run length encoded

### #4mhagain  Members

12438
Like
0Likes
Like

Posted 18 June 2013 - 07:10 PM

This is correct and expected.  The PVS in Quake engine games is a run-length encoded bitfield; so if - say - the 17th element in the bitfield is a 1, then the 17th leaf is visible, if it's a 0 the 17th leaf is not visible.

It appears that the gentleman thought C++ was extremely difficult and he was overjoyed that the machine was absorbing it; he understood that good C++ is difficult but the best C++ is well-nigh unintelligible.

### #5Noah2033  Members

161
Like
0Likes
Like

Posted 18 June 2013 - 08:20 PM

This is correct and expected.  The PVS in Quake engine games is a run-length encoded bitfield; so if - say - the 17th element in the bitfield is a 1, then the 17th leaf is visible, if it's a 0 the 17th leaf is not visible.

### #6Noah2033  Members

161
Like
0Likes
Like

Posted 19 June 2013 - 09:00 PM

code for vis is now

				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
for (int k = 0; k <= 3; k++)
{
((char*)&nclusters)[k] = buffer[k];
}
cout << nclusters << endl; //189
bspvis visarray[200];
for (int v = 0; v <=  nclusters - 1; v++)
{
bsp::bspfile.seekg (bsp::lumps[3].offset + 4 + (v * 8), ios::beg);
cout << v << endl;
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);
bitset<8> x;
for(int b = 0; b <= ceil(nclusters/8); b++)
{
x = vis[b];
cout << x ;
}
cout << endl << endl;
//bsp::bspfile.seekg (bsp::lumps[3].offset + visarray[v].pvs, ios::beg);

}


here's the output

00101000010000110000000000000000

140
4236
00000000000001000111000001111111011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

141
4259
00000000000001001111000001111111011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

142
4282
00000000000001001111000001111111011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

143
4305
00000000000001000111000001111110011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

144
4328
00000000000001001111000001111111011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

145
4351
01100000100111010000001011100001111111000111111101111111011110010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

146
4376
00100000100111010000001011100001111111000111111101111111000000000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

147
4393
01100000100111110000001011100001111111000111111101111111000000000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

148
4415
00000000000001001111000001111111011111110001000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

149
4438
00000000000001001111000001111111011111110000000000000011000100000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

150
4458
01100000100111010000001011100001111111000111111101111111000100010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

151
4483
00000000000100101000000010000000000001010000000000000001100000000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

152
4492
00000000000001001111000001111111011111110101000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

153
4516
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

154
4543
00000000000001001111000001111111011111110101000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

155
4566
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

156
4592
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

157
4618
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

158
4644
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

159
4670
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

160
4696
00000000000001110110000000000000000000101111000101101101110100000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

161
4715
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

162
4741
00000000000001110110000000000000000000101111000101101101110100000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

163
4760
00000000000001001111000001111110111111110111111100000101110101000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

164
4782
00000000000000011000000000000010010000001111000001111111111111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

165
4807
00000000000001000011000000010010010010000111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

166
4831
00000000000001000111000001111110011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

167
4855
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

168
4881
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

169
4907
00000000000001001111000001111111011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

170
4930
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

171
4956
00000000000001000011000000010010000010000111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

172
4980
00000000000001000011000000010010010010000000000000001000111000000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

173
4996
00000000000001001111000001111111011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

174
5020
00000000000001001111000001111111011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

175
5044
00000000000001001111000001111111011111110000000000000100010000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

176
5064
00000000000001001111000001111111011111110000000000000111010010000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

177
5081
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

178
5107
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

179
5133
00000000000011100100100011000000001111100000000000000001011100000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

180
5147
00000000000011100100100011000000001111100000000000000100101110000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

181
5157
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

182
5183
00000000000011100100100011100000101111100000001101110000000111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

183
5196
00000000000001110110000000000000000000101111000101101101110100000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

184
5216
00000000000001000011000000000000000000100111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

185
5240
00000000000001001111000001111111011111110111000100000000000000010010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

186
5264
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

187
5291
00000000000001110110000000000000000000101111000101101101110100000010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000

188
5311
00000000000000011000000000000010010000001111000001111111011111110010100001000011
00000000000000000001000001000100000000000000000010010100010000110000000000000000
00101000010000110000000000000000


Edited by Noah2033, 19 June 2013 - 09:02 PM.

Old topic!

Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.