medfall

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 88b8291e6289f365e8d074f10fda15e469d9380b
parent 6024d7470c4ee4c4ec63c5627eca45fb98b1dc56
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed Jul 22 23:13:17 +0200

Split rendering methods off into BSP_Renderer class

Diffstat:
Makefile | 2+-
bsp.cc | 151++++++++++++++++++++-----------------------------------------------------------
bsp.h | 9+++------
bsp_renderer.cc | 82+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
bsp_renderer.h | 17+++++++++++++++++
5 files changed, 141 insertions(+), 120 deletions(-)
diff --git a/Makefile b/Makefile @@ -1,6 +1,6 @@ all: bsp -SRCS = bsp.cc gl.cc +SRCS = bsp.cc bsp_renderer.cc gl.cc OBJS := $(patsubst %.cc,%.o,$(SRCS)) CXXFLAGS += -std=c++11 -O2 -Wall -Wextra diff --git a/bsp.cc b/bsp.cc @@ -1,17 +1,16 @@ #include <iostream> #include <fstream> #include <math.h> -#include <string.h> - -#include <glm/glm.hpp> #include <GL/glu.h> #include <GLFW/glfw3.h> +#include <glm/glm.hpp> -#include "stb_easy_font.h" +#include "int.h" #include "gl.h" #include "bsp.h" -#include "int.h" +#include "bsp_renderer.h" +#include "stb_easy_font.h" const float EPSILON = 1.0 / 32.0; @@ -43,55 +42,41 @@ float point_plane_distance( const glm::vec3 & point, const glm::vec3 & normal, f return glm::dot( point, normal ) - d; } -void BSP::pick_color( const BSP_Face & face ) { - if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_olive033") == 0) { - glColor3f(0, 0.33, 0); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_olive066") == 0) { - glColor3f(0, 0.66, 0); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_olive100") == 0) { - glColor3f(0, 1, 0); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_olive100_fade") == 0) { - glColor3f(0.25, 1, 0.25); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_red100_fade") == 0) { - glColor3f(1, 0.25, 0.25); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_red100") == 0) { - glColor3f(1, 0, 0); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_red066") == 0) { - glColor3f(0.66, 0, 0); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_red033") == 0) { - glColor3f(0.33, 0, 0); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_purple033") == 0) { - glColor3f(0.33, 0, 0.33); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_purple066") == 0) { - glColor3f(0.66, 0, 0.66); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_purple100") == 0) { - glColor3f(1, 0, 1); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_purple100_fade") == 0) { - glColor3f(1, 0.25, 1); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_cyan033") == 0) { - glColor3f(0, 0.33, 0.33); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_cyan066") == 0) { - glColor3f(0, 0.66, 0.66); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_cyan100") == 0) { - glColor3f(0, 1, 1); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/wall_cyan100_fade") == 0) { - glColor3f(0.25, 1, 1); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/struc_lightgrey") == 0) { - //glColor3f(0.85, 0.85, 0.85); - glColor3f(0.15, 0.15, 0.15); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/struc_darkgrey") == 0) { - glColor3f(0.5, 0.5, 0.5); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/sky_black") == 0) { - glColor3f(0, 0, 0); - } else if(strcmp(textures[face.texture].name, "textures/acidwdm2/ink") == 0) { - //glColor3f(0, 0, 0); - glColor3f( 0, 0, 0 ); - } else { - //glColor3f( 1, 1, 1 ); - glColor3f( 0, 0, 0 ); - } +BSP::BSP( const std::string filename ) { + std::ifstream file( filename, std::ifstream::binary ); + + assert( file.is_open() ); + + file.seekg( 0, file.end ); + ssize_t len = file.tellg(); + file.seekg( 0, file.beg ); + + contents = new char[ len ]; + file.read( contents, len ); + + assert( ( file.rdstate() & std::ifstream::failbit ) == 0 ); + + file.close(); + + load_lump( num_textures, textures, LUMP_TEXTURES ); + load_lump( num_planes, planes, LUMP_PLANES ); + load_lump( num_nodes, nodes, LUMP_NODES ); + load_lump( num_leaves, leaves, LUMP_LEAVES ); + load_lump( num_leaf_faces, leaf_faces, LUMP_LEAFFACES ); + load_lump( num_leaf_brushes, leaf_brushes, LUMP_LEAFBRUSHES ); + load_lump( num_brushes, brushes, LUMP_BRUSHES ); + load_lump( num_brush_sides, brush_sides, LUMP_BRUSHSIDES ); + load_lump( num_vertices, vertices, LUMP_VERTICES ); + load_lump( num_mesh_verts, mesh_verts, LUMP_MESHVERTS ); + load_lump( num_faces, faces, LUMP_FACES ); + load_lump( num_vis, vis, LUMP_VISIBILITY ); +} + +BSP::~BSP() { + delete contents; } + template< typename T > void BSP::load_lump( u32 & num_ts, T *& ts, BSP_Lump lump ) { const BSP_Header * const header = reinterpret_cast< BSP_Header * >( contents ); @@ -238,66 +223,6 @@ BSP_Leaf & BSP::position_to_leaf( const glm::vec3 & pos ) { return leaves[ -( node_idx + 1 ) ]; } -void BSP::render_leaf( const BSP_Leaf & leaf ) { - for( u32 i = 0; i < leaf.num_faces; i++ ) { - const BSP_LeafFace & leaf_face = leaf_faces[ i + leaf.init_face ]; - const BSP_Face & face = faces[ leaf_face ]; - - pick_color( face ); - - glVertexPointer( 3, GL_FLOAT, sizeof( BSP_Vertex ), &vertices[ face.init_vert ].pos ); - glDrawElements( GL_TRIANGLES, face.num_mesh_verts, GL_UNSIGNED_INT, &mesh_verts[ face.init_mesh_vert ] ); - } -} - -BSP::BSP( const std::string filename ) { - std::ifstream file( filename, std::ifstream::binary ); - - assert( file.is_open() ); - - file.seekg( 0, file.end ); - ssize_t len = file.tellg(); - file.seekg( 0, file.beg ); - - contents = new char[ len ]; - file.read( contents, len ); - - assert( ( file.rdstate() & std::ifstream::failbit ) == 0 ); - - file.close(); - - load_lump( num_textures, textures, LUMP_TEXTURES ); - load_lump( num_planes, planes, LUMP_PLANES ); - load_lump( num_nodes, nodes, LUMP_NODES ); - load_lump( num_leaves, leaves, LUMP_LEAVES ); - load_lump( num_leaf_faces, leaf_faces, LUMP_LEAFFACES ); - load_lump( num_leaf_brushes, leaf_brushes, LUMP_LEAFBRUSHES ); - load_lump( num_brushes, brushes, LUMP_BRUSHES ); - load_lump( num_brush_sides, brush_sides, LUMP_BRUSHSIDES ); - load_lump( num_vertices, vertices, LUMP_VERTICES ); - load_lump( num_mesh_verts, mesh_verts, LUMP_MESHVERTS ); - load_lump( num_faces, faces, LUMP_FACES ); - load_lump( num_vis, vis, LUMP_VISIBILITY ); -} - -BSP::~BSP() { - delete contents; -} - -void BSP::render( const glm::vec3 & camera ) { - // const i32 cluster = position_to_leaf( camera ).cluster; - - for( u32 i = 0; i < num_leaves; i++ ) { - const BSP_Leaf & leaf = leaves[ i ]; - // const i32 other_cluster = leaf.cluster; - // const i32 vis_idx = cluster * num_visdata + other_cluster / 8; - - // printf( "%d\n", vis_idx ); - - render_leaf( leaf ); - } -} - glm::vec3 d2r( const glm::vec3 & degrees ) { return degrees * static_cast< float >( M_PI / 180 ); } @@ -335,7 +260,7 @@ int main() { glRotatef( angles.y, 0.0, 0.0, 1.0 ); glTranslatef( -start.x, -start.y, -start.z ); - bsp.render( start ); + BSP_Renderer::render( bsp, start ); const glm::vec3 test( 353.553, -453.553, 450.000 ); glColor3f( 1, 1, 1 ); diff --git a/bsp.h b/bsp.h @@ -1,6 +1,7 @@ #ifndef _BSP_H_ #define _BSP_H_ +#include <string> #include "int.h" enum BSP_Lump { @@ -145,6 +146,8 @@ struct BSP_Intersection { }; class BSP { + friend class BSP_Renderer; + private: char * contents; @@ -184,8 +187,6 @@ private: u32 num_vis; BSP_Vis * vis; - void pick_color( const BSP_Face & face ); - template< typename T > void load_lump( u32 & num_ts, T *& ts, BSP_Lump lump ); void trace_seg_brush( const BSP_Brush & brush, BSP_Intersection & bis ); @@ -194,15 +195,11 @@ private: BSP_Leaf & position_to_leaf( const glm::vec3 & pos ); - void render_leaf( const BSP_Leaf & leaf ); - public: BSP( std::string filename ); ~BSP(); bool trace_seg( const glm::vec3 & start, const glm::vec3 & end, Intersection & is ); - - void render( const glm::vec3 & camera ); }; #endif // _BSP_H_ diff --git a/bsp_renderer.cc b/bsp_renderer.cc @@ -0,0 +1,82 @@ +#include <string.h> + +#include <GL/gl.h> +#include <glm/glm.hpp> + +#include "bsp.h" +#include "bsp_renderer.h" + +void BSP_Renderer::pick_color( const BSP & bsp, const BSP_Face & face ) { + if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_olive033") == 0) { + glColor3f(0, 0.33, 0); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_olive066") == 0) { + glColor3f(0, 0.66, 0); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_olive100") == 0) { + glColor3f(0, 1, 0); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_olive100_fade") == 0) { + glColor3f(0.25, 1, 0.25); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_red100_fade") == 0) { + glColor3f(1, 0.25, 0.25); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_red100") == 0) { + glColor3f(1, 0, 0); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_red066") == 0) { + glColor3f(0.66, 0, 0); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_red033") == 0) { + glColor3f(0.33, 0, 0); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_purple033") == 0) { + glColor3f(0.33, 0, 0.33); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_purple066") == 0) { + glColor3f(0.66, 0, 0.66); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_purple100") == 0) { + glColor3f(1, 0, 1); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_purple100_fade") == 0) { + glColor3f(1, 0.25, 1); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_cyan033") == 0) { + glColor3f(0, 0.33, 0.33); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_cyan066") == 0) { + glColor3f(0, 0.66, 0.66); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_cyan100") == 0) { + glColor3f(0, 1, 1); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/wall_cyan100_fade") == 0) { + glColor3f(0.25, 1, 1); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/struc_lightgrey") == 0) { + //glColor3f(0.85, 0.85, 0.85); + glColor3f(0.15, 0.15, 0.15); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/struc_darkgrey") == 0) { + glColor3f(0.5, 0.5, 0.5); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/sky_black") == 0) { + glColor3f(0, 0, 0); + } else if(strcmp(bsp.textures[face.texture].name, "textures/acidwdm2/ink") == 0) { + //glColor3f(0, 0, 0); + glColor3f( 0, 0, 0 ); + } else { + //glColor3f( 1, 1, 1 ); + glColor3f( 0, 0, 0 ); + } +} + +void BSP_Renderer::render_leaf( const BSP & bsp, const BSP_Leaf & leaf ) { + for( u32 i = 0; i < leaf.num_faces; i++ ) { + const BSP_LeafFace & leaf_face = bsp.leaf_faces[ i + leaf.init_face ]; + const BSP_Face & face = bsp.faces[ leaf_face ]; + + pick_color( bsp, face ); + + glVertexPointer( 3, GL_FLOAT, sizeof( BSP_Vertex ), &bsp.vertices[ face.init_vert ].pos ); + glDrawElements( GL_TRIANGLES, face.num_mesh_verts, GL_UNSIGNED_INT, &bsp.mesh_verts[ face.init_mesh_vert ] ); + } +} + +void BSP_Renderer::render( const BSP & bsp, const glm::vec3 & camera ) { + // const i32 cluster = bsp.position_to_leaf( camera ).cluster; + + for( u32 i = 0; i < bsp.num_leaves; i++ ) { + const BSP_Leaf & leaf = bsp.leaves[ i ]; + // const i32 other_cluster = leaf.cluster; + // const i32 vis_idx = cluster * num_visdata + other_cluster / 8; + + // printf( "%d\n", vis_idx ); + + render_leaf( bsp, leaf ); + } +} diff --git a/bsp_renderer.h b/bsp_renderer.h @@ -0,0 +1,17 @@ +#ifndef _BSP_RENDERER_H_ +#define _BSP_RENDERER_H_ + +#include <glm/glm.hpp> + +#include "bsp.h" + +class BSP_Renderer { +private: + static void pick_color( const BSP & bsp, const BSP_Face & face ); + static void render_leaf( const BSP & bsp, const BSP_Leaf & leaf ); + +public: + static void render( const BSP & bsp, const glm::vec3 & camera ); +}; + +#endif // _BSP_RENDERER_H_