medfall

A super great game engine
Log | Files | Refs

bsp_renderer.cc (5474B)


      1 #include <string.h>
      2 
      3 #include "intrinsics.h"
      4 #include "linear_algebra.h"
      5 #include "array.h"
      6 #include "bsp.h"
      7 #include "bsp_renderer.h"
      8 #include "memory_arena.h"
      9 #include "renderer.h"
     10 
     11 v3 bspr_face_colour( const BSP * bsp, const BSP_Face & face ) {
     12 	if(strcmp(bsp->textures[face.texture].name, "textures/cocaine/s_lazery_blue") == 0)
     13 		return v3( 0.02, 0.7, 0.65 );
     14 	if(strcmp(bsp->textures[face.texture].name, "textures/cocaine/s_lazery_green") == 0)
     15 		return v3( 0.25, 0.7, 0.02 );
     16 	if(strcmp(bsp->textures[face.texture].name, "textures/cocaine/s_lazery_kush") == 0)
     17 		return v3( 0.6, 0.02, 0.7 );
     18 	if(strcmp(bsp->textures[face.texture].name, "textures/cocaine/s_lazery_orange") == 0)
     19 		return v3( 0.7, 0.4, 0.02 );
     20 	if(strcmp(bsp->textures[face.texture].name, "textures/cocaine/s_lazery_red") == 0)
     21 		return v3( 0.7, 0.02, 0.2 );
     22 	if(strcmp(bsp->textures[face.texture].name, "textures/cocaine/s_lazery_white") == 0)
     23 		return v3( 0.85, 0.85, 0.85 );
     24 	if(strcmp(bsp->textures[face.texture].name, "textures/world/lava") == 0)
     25 		return v3( 1.0, 0.19, 0.02 );
     26 	if(strcmp(bsp->textures[face.texture].name, "textures/blx/new_sky") == 0)
     27 		return v3( 1, 1, 1 );
     28 
     29 	return v3( 0.03, 0.03, 0.03 );
     30 	if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_olive033") == 0) {
     31 		return v3(0, 0.33, 0);
     32 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_olive066") == 0) {
     33 		return v3(0, 0.66, 0);
     34 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_olive100") == 0) {
     35 		return v3(0, 1, 0);
     36 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_olive100_fade") == 0) {
     37 		return v3(0.25, 1, 0.25);
     38 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_red100_fade") == 0) {
     39 		return v3(1, 0.25, 0.25);
     40 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_red100") == 0) {
     41 		return v3(1, 0, 0);
     42 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_red066") == 0) {
     43 		return v3(0.66, 0, 0);
     44 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_red033") == 0) {
     45 		return v3(0.33, 0, 0);
     46 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_purple033") == 0) {
     47 		return v3(0.33, 0, 0.33);
     48 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_purple066") == 0) {
     49 		return v3(0.66, 0, 0.66);
     50 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_purple100") == 0) {
     51 		return v3(1, 0, 1);
     52 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_purple100_fade") == 0) {
     53 		return v3(1, 0.25, 1);
     54 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_cyan033") == 0) {
     55 		return v3(0, 0.33, 0.33);
     56 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_cyan066") == 0) {
     57 		return v3(0, 0.66, 0.66);
     58 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_cyan100") == 0) {
     59 		return v3(0, 1, 1);
     60 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/wall_cyan100_fade") == 0) {
     61 		return v3(0.25, 1, 1);
     62 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/struc_lightgrey") == 0) {
     63 		//return v3(0.85, 0.85, 0.85);
     64 		return v3(0.15, 0.15, 0.15);
     65 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/struc_darkgrey") == 0) {
     66 		return v3(0.5, 0.5, 0.5);
     67 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/sky_black") == 0) {
     68 		return v3( 0.7, 0.7, 0.7 );
     69 	} else if(strcmp(bsp->textures[face.texture].name, "textures/acidwdm2/ink") == 0) {
     70 		return v3( 0, 0, 0 );
     71 	} else {
     72 		return v3( 0, 0, 0 );
     73 	}
     74 }
     75 
     76 struct VertexData {
     77 	v3 pos;
     78 	v3 normal;
     79 	v3 colour;
     80 };
     81 
     82 void bsp_to_mesh( const BSP * bsp, DynamicArray< VertexData > * verts, DynamicArray< u32 > * indices ) {
     83 	for( u32 i = 0; i < bsp->num_vertices; i++ ) {
     84 		VertexData v;
     85 		v.pos = bsp->vertices[ i ].pos;
     86 		v.normal = bsp->vertices[ i ].normal;
     87 		verts->add( v );
     88 	}
     89 
     90 	for( s32 f = 0; f < bsp->map_models[ 0 ].num_faces; f++ ) {
     91 		const BSP_Face & face = bsp->faces[ bsp->map_models[ 0 ].first_face + f ];
     92 		v3 colour = bspr_face_colour( bsp, face );
     93 
     94 		// drop outlines
     95 		if( strcmp( bsp->textures[ face.texture ].name, "textures/acidwdm2/ink" ) == 0 )
     96 			continue;
     97 
     98 		// if( face.type != 1 )
     99 		//      continue;
    100 
    101 		for( s32 m = 0; m < face.num_mesh_verts; m++ ) {
    102 			u32 index = face.first_vert + bsp->mesh_verts[ face.first_mesh_vert + m ];
    103 			indices->add( index );
    104 			( *verts )[ index ].colour = colour;
    105 		}
    106 	}
    107 }
    108 
    109 void bspr_init( BSPRenderer * bspr, const BSP * bsp ) {
    110 	bspr->bsp = bsp;
    111 
    112 	DynamicArray< VertexData > verts( bsp->num_vertices );
    113 	DynamicArray< u32 > indices;
    114 	bsp_to_mesh( bsp, &verts, &indices );
    115 
    116 	MeshConfig mesh_config;
    117 	mesh_config.unified_buffer = renderer_new_vb( verts.ptr(), verts.num_bytes() );
    118 	mesh_config.stride = sizeof( verts[ 0 ] );
    119 	mesh_config.positions_offset = offsetof( VertexData, pos );
    120 	mesh_config.normals_offset = offsetof( VertexData, normal );
    121 	mesh_config.colours_offset = offsetof( VertexData, colour );
    122 	mesh_config.indices = renderer_new_ib( indices.ptr(), indices.num_bytes() );
    123 	mesh_config.num_vertices = indices.size();
    124 
    125 	bspr->mesh = renderer_new_mesh( mesh_config );
    126 }
    127 
    128 void bspr_render( const BSPRenderer * bspr, const RenderState & render_state ) {
    129 	renderer_draw_mesh( bspr->mesh, render_state );
    130 }
    131 
    132 void bspr_destroy( BSPRenderer * bspr ) {
    133 	renderer_delete_mesh( bspr->mesh );
    134 }