c++ - Trouble rendering quake 3 maps -
recently i've been working on quake 3 bsp loader while. cannot faces render correctly. http://postimg.org/image/o2lf8vlp9/ here vertices map. here's happens when render faces on map. http://postimg.org/image/6hmoht7ep/
this code rendered.
void bsp::render() { ( 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); ( 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(); } } }
here's full source
#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; // index texture array int effect; // index effects (or -1 = n/a) int type; // 1=polygon, 2=patch, 3=mesh, 4=billboard int vertexindex; // index face's first vertex int numofverts; // number of vertices face int meshvertindex; // index first meshvertex int nummeshverts; // number of mesh vertices int lightmapid; // texture index lightmap int lmapcorner[2]; // face's lightmap corner in image int lmapsize[2]; // size of lightmap section float lmappos[3]; // 3d origin of lightmap. float lmapbitsets[2][3]; // 3d space s , t unit vectors. float vnormal[3]; // face normal. int size[2]; // 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 vertex }; struct bsptexture { char name[64]; // name of texture w/o extension int flags; // surface flags (unknown) int contents; // content flags (unknown) }; struct bspbrush { int brushside; // starting brush side brush int numofbrushsides; // number of brush sides brush int textureid; // texture index 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 magic number in header of bsp file should "ibsp" if((magic[0] != 'i')||(magic[1] != 'b')||(magic[2] != 's')||(magic[3] != 'p')) { cout << "error: not valid quake 3 bsp file" << endl; } else { int version; char vbuffer[4]; bsp::bspfile.read(vbuffer, 4); ( 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 { ( int = 0; <= 16; i++) { char lumpoffset[4]; char lumplength[4]; //read lumps offset bsp::bspfile.read(lumpoffset, 4); ( int k = 0; k <= 3; k++) { ((char*)&bsp::lumps[i].offset)[k] = lumpoffset[k]; } //read lumps length bsp::bspfile.read(lumplength, 4); ( int k = 0; k <= 3; k++) { ((char*)&bsp::lumps[i].length)[k] = lumplength[k]; } cout << "lump " << << " offset " << bsp::lumps[i].offset << endl << "lump " << << " length " << 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); ( int j = 0; j <= bsp::lumps[1].length/sizeof(bsptexture); j++) //read until end of lump { char buffer[72]; bsp::bspfile.read(buffer, 72); ( 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); ( int j = 0; j <= bsp::lumps[5].length/sizeof(bspvertex); j++) //read until end of lump { char buffer[4]; //create buffer leaffaces bsp::bspfile.read(buffer, 4); //read ( 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 vertex lump (10) ( int j = 0; j <= bsp::lumps[10].length/sizeof(bspvertex); j++)//read until end of lump { char buffer[44]; //create buffer verts bsp::bspfile.read(buffer, 44); //read ( 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 face lump (13) ( int j = 0; j <= bsp::lumps[13].length/sizeof(bspface); j++)//read until end of lump { char buffer[104]; //create buffer faces bsp::bspfile.read(buffer, 104); //read ( int k = 0; k <= 103; k++) //read until end of lump { ((char*)&bsp::faces[j])[k] = buffer[k]; } } } } } } void bsp::render() { ( 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); ( 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 events 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"); { render(); }while(running); return 0; }
the index you're using pointing face polygon vertices lump. if want see map rendered can try replacing gl_triangle_strip mode gl_polygon.
the triangle version of polygon stored in meshverts lump. in order render triangles need indexes stored in meshverts , offset vertexindex of face.
Comments
Post a Comment