medfall

A super great game engine
Log | Files | Refs

commit fe8439efc08e2ae582ca23af009dab7509b8fa33
parent 4901289a005db1d8f281c2b1287a7acd267aa476
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Wed Jun 28 22:22:49 +0300

Fullscreen

Diffstat:
bsp.cc | 3++-
game.h | 3---
gl.cc | 16+++++++++++++++-
gl.h | 5+++++
hm.cc | 5+++--
renderer.cc | 8+++++---
renderer.h | 1+
shaders/text.glsl | 8++++++--
shadow_map.cc | 4+++-
text_renderer.cc | 12++++++++++--
10 files changed, 50 insertions(+), 15 deletions(-)
diff --git a/bsp.cc b/bsp.cc @@ -4,6 +4,7 @@ #include "game.h" #include "intrinsics.h" #include "log.h" +#include "gl.h" #include "renderer.h" #include "shaders.h" #include "bsp.h" @@ -282,7 +283,7 @@ GAME_FRAME( game_frame ) { game->pos += right * 100.0f * dt * lr; game->pos.z += dz * 100.0f * dt; - m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); + m4 P = m4_perspective( VERTICAL_FOV, get_aspect_ratio(), NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); m4 V = m4_view( forward, right, up, game->pos ); renderer_begin_frame(); diff --git a/game.h b/game.h @@ -15,9 +15,6 @@ #include "memory_arena.h" #include "keys.h" -const int WIDTH = 1024; -const int HEIGHT = 768; - const float NEAR_PLANE_DEPTH = 0.1f; const float FAR_PLANE_DEPTH = 10000.0f; diff --git a/gl.cc b/gl.cc @@ -15,6 +15,8 @@ #define YELLOW "\x1b[1;32m" #define GREEN "\x1b[1;33m" +static v2u32 window_size; + static const char * type_string( GLenum type ) { switch( type ) { case GL_DEBUG_TYPE_ERROR: @@ -106,6 +108,8 @@ GLFWwindow * gl_init() { glfwWindowHint( GLFW_RESIZABLE, 0 ); + const GLFWvidmode * mode = glfwGetVideoMode( glfwGetPrimaryMonitor() ); + glfwWindowHint( GLFW_CLIENT_API, GLFW_OPENGL_API ); glfwWindowHint( GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE ); glfwWindowHint( GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE ); @@ -113,11 +117,13 @@ GLFWwindow * gl_init() { glfwWindowHint( GLFW_CONTEXT_VERSION_MINOR, 3 ); glfwWindowHint( GLFW_OPENGL_DEBUG_CONTEXT, 1 ); - GLFWwindow * window = glfwCreateWindow( WIDTH, HEIGHT, "bsp", NULL, NULL ); + GLFWwindow * window = glfwCreateWindow( mode->width, mode->height, "Medfall", NULL, NULL ); if( !window ) { FATAL( "glfwCreateWindow" ); } + window_size = v2u32( checked_cast< u32 >( mode->width ), checked_cast< u32 >( mode->height ) ); + glfwMakeContextCurrent( window ); if( gladLoadGL() != 1 ) { @@ -204,3 +210,11 @@ void gl_term() { // gl_check_for_leaks(); glfwTerminate(); } + +v2u32 get_window_size() { + return window_size; +} + +float get_aspect_ratio() { + return float( window_size.x ) / float( window_size.y ); +} diff --git a/gl.h b/gl.h @@ -1,6 +1,11 @@ #pragma once +#include "linear_algebra.h" + struct GLFWwindow; GLFWwindow * gl_init(); void gl_term(); + +v2u32 get_window_size(); +float get_aspect_ratio(); diff --git a/hm.cc b/hm.cc @@ -5,6 +5,7 @@ #include "terrain_manager.h" #include "linear_algebra.h" #include "skybox.h" +#include "gl.h" #include "renderer.h" #include "shaders.h" #include "text_renderer.h" @@ -78,7 +79,7 @@ GAME_INIT( game_init ) { ImmediateTriangle * immediate_memory = memarena_push_many( &mem->persistent_arena, ImmediateTriangle, triangles ); immediate_init( &game->test_immediate, immediate_memory, triangles ); - const float aspect = float( WIDTH ) / float( HEIGHT ); + const float aspect = get_aspect_ratio(); const float crosshair_thickness = 0.0025f; const float crosshair_length = 0.01f; @@ -193,7 +194,7 @@ GAME_FRAME( game_frame ) { terrain_update( &game->tm, game->pos ); - m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); + m4 P = m4_perspective( VERTICAL_FOV, get_aspect_ratio(), NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); m4 V = m4_view( forward, right, up, game->pos ); m4 Vsky = m4_view( forward, right, up, v3( 0 ) ); diff --git a/renderer.cc b/renderer.cc @@ -2,6 +2,7 @@ #include "intrinsics.h" #include "game.h" +#include "gl.h" #include "renderer.h" #include "log.h" #include "linear_algebra.h" @@ -311,7 +312,7 @@ Shader renderer_new_shader( ShaderConfig config ) { return INVALID_SHADER; } - const char * ubo_names[] = { "v_cold", "f_hot", "view", "model", "light_view" }; + const char * ubo_names[] = { "v_cold", "f_hot", "view", "model", "light_view", "window" }; for( GLuint i = 0; i < ARRAY_COUNT( ubo_names ); i++ ) { GLuint idx = glGetUniformBlockIndex( program, ubo_names[ i ] ); if( idx != GL_INVALID_INDEX ) { @@ -610,8 +611,9 @@ static void bind_fb( FB fb ) { u32 viewport_width, viewport_height; if( fb.fbo == 0 ) { - viewport_width = WIDTH; - viewport_height = HEIGHT; + v2u32 window_size = get_window_size(); + viewport_width = window_size.x; + viewport_height = window_size.y; } else { viewport_width = fb.width; diff --git a/renderer.h b/renderer.h @@ -21,6 +21,7 @@ const u32 UB_FS_HOT = 1; const u32 UB_VIEW = 2; const u32 UB_MODEL = 3; const u32 UB_LIGHT_VIEW = 4; +const u32 UB_WINDOW = 5; #define RENDERER_MAX_TEXTURES 4 #define RENDERER_MAX_TEXTURE_BUFFERS 4 diff --git a/shaders/text.glsl b/shaders/text.glsl @@ -3,6 +3,10 @@ struct VSOut { vec2 uv; }; +layout( std140 ) uniform window { + uvec2 window_size; +}; + #if VERTEX_SHADER in vec3 position; @@ -13,8 +17,8 @@ out VSOut v2f; void main() { vec3 screen_position = vec3( - ( position.x / 1024.0 * 2.0 - 1.0 ), - -( position.y / 768.0 * 2.0 - 1.0 ), + ( position.x / window_size.x * 2.0 - 1.0 ), + -( position.y / window_size.y * 2.0 - 1.0 ), position.z ); gl_Position = vec4( screen_position, 1.0 ); diff --git a/shadow_map.cc b/shadow_map.cc @@ -1,8 +1,10 @@ #include "game.h" #include "intrinsics.h" #include "linear_algebra.h" +#include "gl.h" #include "renderer.h" #include "shaders.h" +#include "immediate.h" #include "text_renderer.h" #include "log.h" #include "obj.h" @@ -123,7 +125,7 @@ GAME_FRAME( game_frame ) { game->pos += right * dt * lr * speed; game->pos.z += dt * dz * speed; - const m4 P = m4_perspective( VERTICAL_FOV, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); + const m4 P = m4_perspective( VERTICAL_FOV, get_aspect_ratio(), NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); m4 V = m4_view( forward, right, up, game->pos ); light_pos = v3( cos( current_time ) * 10, sin( current_time ) * 10, 10 ); diff --git a/text_renderer.cc b/text_renderer.cc @@ -1,5 +1,6 @@ #include "intrinsics.h" #include "immediate.h" +#include "gl.h" #include "renderer.h" #include "shaders.h" #include "linear_algebra.h" @@ -19,6 +20,9 @@ static stbtt_fontinfo font_info; static const float pixel_sizes[] = { 16.0f, 32.0f, 48.0f }; static FontSize sizes[ ARRAY_COUNT( pixel_sizes ) ]; +static ImmediateTriangle triangles[ 4096 ]; +static UB ub_window; + void text_renderer_init() { ttf = file_get_contents( "LiberationSans-Regular.ttf" ); @@ -53,9 +57,9 @@ void text_renderer_init() { sizes[ i ].ascent = ascent * scale; sizes[ i ].atlas = renderer_new_texture( texture_config ); } -} -static ImmediateTriangle triangles[ 4096 ]; + ub_window = renderer_new_ub(); +} void draw_text( const char * str, float x, float y, float pixel_size ) { const v4 white( 1, 1, 1, 1 ); @@ -114,10 +118,13 @@ void draw_text( const char * str, float x, float y, float pixel_size ) { RenderState render_state; render_state.shader = get_shader( SHADER_TEXT ); render_state.textures[ 0 ] = sizes[ size_idx ].atlas; + render_state.ubs[ UB_WINDOW ] = ub_window; render_state.depth_func = DEPTHFUNC_DISABLED; render_state.enable_alpha_blending = true; // render_state.disable_depth_writes = true; + renderer_ub_easy( ub_window, get_window_size() ); + immediate_render( &imm, render_state ); } @@ -126,4 +133,5 @@ void text_renderer_term() { for( size_t i = 0; i < ARRAY_COUNT( sizes ); i++ ) { renderer_delete_texture( sizes[ i ].atlas ); } + renderer_delete_ub( ub_window ); }