commit 52fbce5867030fe382f7fc4ecda3ae465f8c80b5
parent d9955ba697a3ef0d5d248c633705a7fb953d70f0
Author: Michael Savage <mikejsavage@gmail.com>
Date: Sun, 29 Oct 2017 09:35:52 +0200
Sort out the face winding/backface culling
Diffstat:
8 files changed, 38 insertions(+), 40 deletions(-)
diff --git a/bsp.cc b/bsp.cc
@@ -286,7 +286,7 @@ GAME_FRAME( game_frame ) {
m4 V = m4_view( forward, right, up, game->pos );
renderer_begin_frame();
- renderer_begin_pass( RENDERER_CLEAR_COLOUR_DONT, RENDERER_CLEAR_DEPTH_DO );
+ renderer_begin_pass( RENDERER_CLEAR_COLOUR_DO, RENDERER_CLEAR_DEPTH_DO );
UniformBinding view_uniforms = renderer_uniforms( V, P );
@@ -294,6 +294,7 @@ GAME_FRAME( game_frame ) {
RenderState render_state;
render_state.shader = get_shader( SHADER_FLAT_VERTEX_COLOURS );
render_state.uniforms[ UNIFORMS_VIEW ] = view_uniforms;
+ render_state.cull_face = CULLFACE_FRONT;
bspr_render( &game->bspr, game->pos, render_state );
}
diff --git a/gl.cc b/gl.cc
@@ -244,7 +244,7 @@ GLFWwindow * gl_init( WindowType window_type ) {
glDepthFunc( GL_LESS );
glEnable( GL_CULL_FACE );
- glCullFace( GL_FRONT ); // TODO
+ glCullFace( GL_BACK );
if( window_type == WINDOW_GAME ) {
glEnable( GL_FRAMEBUFFER_SRGB );
diff --git a/gpubtt.cc b/gpubtt.cc
@@ -58,8 +58,8 @@ void gpubtt_init(
{
PROFILE_BLOCK( "Convert BTT to mesh" );
- gpubtt_build( verts, &num_vertices, ohm, btts.left_root, bl, tl, tr );
- gpubtt_build( verts, &num_vertices, ohm, btts.right_root, tr, br, bl );
+ gpubtt_build( verts, &num_vertices, ohm, btts.left_root, tr, tl, bl );
+ gpubtt_build( verts, &num_vertices, ohm, btts.right_root, bl, br, tr );
ASSERT( num_leaves * 3 == num_vertices );
}
diff --git a/immediate.cc b/immediate.cc
@@ -15,7 +15,7 @@ void immediate_init( ImmediateContext * ctx, ImmediateTriangle * memory, u32 max
void immediate_triangle( ImmediateContext * ctx, v3 p1, v3 p2, v3 p3, v4 colour ) {
ASSERT( ctx->num_triangles < ctx->max_triangles - 1 );
- v3 normal = normalize( cross( p3 - p1, p2 - p1 ) );
+ v3 normal = normalize( cross( p2 - p1, p3 - p1 ) );
v2 uv( 0, 0 );
ImmediateTriangle triangle = { {
{ p1, normal, colour, uv },
@@ -68,8 +68,8 @@ void immediate_sphere(
ImmediateVertex bottom_right = VERT( v3( ca1 * sp1, sa1 * sp1, cp1 ) );
#undef VERT
- immediate_triangle( ctx, top_left, top_right, bottom_left );
- immediate_triangle( ctx, bottom_left, top_right, bottom_right );
+ immediate_triangle( ctx, top_right, top_left, bottom_left );
+ immediate_triangle( ctx, bottom_left, bottom_right, top_right );
sp0 = sp1;
cp0 = cp1;
@@ -94,18 +94,18 @@ void immediate_aabb( ImmediateContext * ctx, v3 mins, v3 maxs, v4 colour ) {
const v3 p6( mins.x, maxs.y, maxs.z );
const v3 p7( maxs.x, maxs.y, maxs.z );
- immediate_triangle( ctx, p0, p1, p2, colour ); // bottom
- immediate_triangle( ctx, p1, p3, p2, colour );
- immediate_triangle( ctx, p4, p6, p5, colour ); // top
- immediate_triangle( ctx, p6, p7, p5, colour );
- immediate_triangle( ctx, p2, p6, p0, colour ); // left
- immediate_triangle( ctx, p6, p4, p0, colour );
- immediate_triangle( ctx, p1, p5, p3, colour ); // right
- immediate_triangle( ctx, p5, p7, p3, colour );
- immediate_triangle( ctx, p0, p4, p1, colour ); // front
- immediate_triangle( ctx, p4, p5, p1, colour );
- immediate_triangle( ctx, p3, p7, p2, colour ); // back
- immediate_triangle( ctx, p7, p6, p2, colour );
+ immediate_triangle( ctx, p0, p2, p1, colour ); // bottom
+ immediate_triangle( ctx, p1, p2, p3, colour );
+ immediate_triangle( ctx, p4, p5, p6, colour ); // top
+ immediate_triangle( ctx, p6, p5, p7, colour );
+ immediate_triangle( ctx, p2, p0, p6, colour ); // left
+ immediate_triangle( ctx, p6, p0, p4, colour );
+ immediate_triangle( ctx, p1, p3, p5, colour ); // right
+ immediate_triangle( ctx, p5, p3, p7, colour );
+ immediate_triangle( ctx, p0, p1, p4, colour ); // front
+ immediate_triangle( ctx, p4, p1, p5, colour );
+ immediate_triangle( ctx, p3, p2, p7, colour ); // back
+ immediate_triangle( ctx, p7, p2, p6, colour );
}
static m3 about_with_sin_cos( v3 axis, float s, float c ) {
@@ -177,18 +177,18 @@ void immediate_arrow(
cone[ i ] = ( rot * cone[ i ] ) * length + origin;
}
- immediate_triangle( ctx, v[ 0 ], v[ 1 ], v[ 2 ], colour ); // bottom
- immediate_triangle( ctx, v[ 1 ], v[ 3 ], v[ 2 ], colour );
- immediate_triangle( ctx, v[ 4 ], v[ 6 ], v[ 5 ], colour ); // top
- immediate_triangle( ctx, v[ 6 ], v[ 7 ], v[ 5 ], colour );
- immediate_triangle( ctx, v[ 2 ], v[ 6 ], v[ 0 ], colour ); // left
- immediate_triangle( ctx, v[ 6 ], v[ 4 ], v[ 0 ], colour );
- immediate_triangle( ctx, v[ 1 ], v[ 5 ], v[ 3 ], colour ); // right
- immediate_triangle( ctx, v[ 5 ], v[ 7 ], v[ 3 ], colour );
- immediate_triangle( ctx, v[ 0 ], v[ 4 ], v[ 1 ], colour ); // front
- immediate_triangle( ctx, v[ 4 ], v[ 5 ], v[ 1 ], colour );
- immediate_triangle( ctx, v[ 3 ], v[ 7 ], v[ 2 ], colour ); // back
- immediate_triangle( ctx, v[ 7 ], v[ 6 ], v[ 2 ], colour );
+ immediate_triangle( ctx, v[ 0 ], v[ 2 ], v[ 1 ], colour ); // bottom
+ immediate_triangle( ctx, v[ 1 ], v[ 2 ], v[ 3 ], colour );
+ immediate_triangle( ctx, v[ 4 ], v[ 5 ], v[ 6 ], colour ); // top
+ immediate_triangle( ctx, v[ 6 ], v[ 5 ], v[ 7 ], colour );
+ immediate_triangle( ctx, v[ 2 ], v[ 0 ], v[ 6 ], colour ); // left
+ immediate_triangle( ctx, v[ 6 ], v[ 0 ], v[ 4 ], colour );
+ immediate_triangle( ctx, v[ 1 ], v[ 3 ], v[ 5 ], colour ); // right
+ immediate_triangle( ctx, v[ 5 ], v[ 3 ], v[ 7 ], colour );
+ immediate_triangle( ctx, v[ 0 ], v[ 1 ], v[ 4 ], colour ); // front
+ immediate_triangle( ctx, v[ 4 ], v[ 1 ], v[ 5 ], colour );
+ immediate_triangle( ctx, v[ 3 ], v[ 2 ], v[ 7 ], colour ); // back
+ immediate_triangle( ctx, v[ 7 ], v[ 2 ], v[ 6 ], colour );
for( size_t i = 0; i < ARRAY_COUNT( cone ); i++ ) {
immediate_triangle( ctx, v[ 8 ], cone[ ( i + 1 ) % ARRAY_COUNT( cone ) ], cone[ i ], colour );
diff --git a/renderer.cc b/renderer.cc
@@ -199,8 +199,7 @@ static void set_render_state( const RenderState & state ) {
if( previous_render_state.cull_face == CULLFACE_DISABLED ) {
glEnable( GL_CULL_FACE );
}
- // TODO: current renderer is fucked and this should be the other way around
- glCullFace( state.cull_face == CULLFACE_FRONT ? GL_BACK : GL_FRONT );
+ glCullFace( state.cull_face == CULLFACE_FRONT ? GL_FRONT : GL_BACK );
}
}
diff --git a/shadow_map.cc b/shadow_map.cc
@@ -44,14 +44,13 @@ static void draw_scene( RenderState render_state, bool draw_light = false ) {
v3 tr( 50, 50, 0 );
v3 bl( -50, -50, 0 );
v3 br( 50, -50, 0 );
- immediate_triangle( &imm, tl, tr, bl, v4( 0.5, 0.5, 0.5, 1.0 ) );
- immediate_triangle( &imm, bl, tr, br, v4( 0.5, 0.5, 0.5, 1.0 ) );
+ immediate_triangle( &imm, tr, tl, bl, v4( 0.5, 0.5, 0.5, 1.0 ) );
+ immediate_triangle( &imm, bl, br, tr, v4( 0.5, 0.5, 0.5, 1.0 ) );
immediate_render( &imm, render_state );
m4 M = m4_translation( v3( -7, -2, 0 ) ) * m4_rotx( deg_to_rad( 90 ) );
render_state.uniforms[ UNIFORMS_MODEL ] = renderer_uniforms( M );
- render_state.cull_face = CULLFACE_FRONT;
renderer_draw_mesh( tree_mesh, render_state );
}
@@ -73,8 +72,8 @@ GAME_INIT( game_init ) {
{
float verts[] = {
-1.0f, -1.0f, 0.0f,
- -1.0f, -0.7f, 0.0f,
-0.8f, -1.0f, 0.0f,
+ -1.0f, -0.7f, 0.0f,
-0.8f, -0.7f, 0.0f,
};
MeshConfig config;
diff --git a/skybox.cc b/skybox.cc
@@ -134,7 +134,6 @@ void skybox_render( const Skybox * skybox, const m4 & Vsky, const m4 & P, float
RenderState render_state;
render_state.shader = get_shader( SHADER_SKYBOX );
- render_state.cull_face = CULLFACE_FRONT;
render_state.disable_depth_writes = true;
render_state.uniforms[ UNIFORMS_VIEW ] = renderer_uniforms( Vsky, P );
render_state.uniforms[ UNIFORMS_SKY ] = renderer_uniforms( hosek.A, hosek.B, hosek.C, hosek.D, hosek.E, hosek.F, hosek.G, hosek.H, hosek.I, hosek.Z, sun_dir );
diff --git a/text_renderer.cc b/text_renderer.cc
@@ -109,8 +109,8 @@ void draw_text( const char * str, int x, int y, float pixel_size ) {
v2( q.s1, q.t1 ),
};
- immediate_triangle( &imm, tl, tr, bl );
- immediate_triangle( &imm, bl, tr, br );
+ immediate_triangle( &imm, tr, tl, bl );
+ immediate_triangle( &imm, bl, br, tr );
str++;
}