medfall

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

commit 40cd26542bbe8a068fcdffb27fdf440010fa4644
parent b9d71e59dab494189c2f2802378ba4c3ab9216c7
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Mon Oct 31 23:15:53 +0200

Checkerboard returns

Diffstat:
bsp.cc | 95+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 95 insertions(+), 0 deletions(-)
diff --git a/bsp.cc b/bsp.cc @@ -44,6 +44,34 @@ static const GLchar * frag_src = GLSL( } ); +static const GLchar * const textured_vert_src = GLSL( + in vec3 position; + in vec4 colour; + in vec2 tex_coord0; + + out vec4 frag_colour; + out vec2 frag_uv; + + void main() { + gl_Position = vec4( position, 1.0 ); + frag_colour = colour; + frag_uv = tex_coord0; + } +); + +static const GLchar * textured_frag_src = GLSL( + in vec4 frag_colour; + in vec2 frag_uv; + + uniform sampler2D tex; + + out vec4 screen_colour; + + void main() { + screen_colour = texture( tex, frag_uv ) * frag_colour; + } +); + static const float EPSILON = 1.0 / 32.0; // TODO: bit twiddling? @@ -295,6 +323,7 @@ glm::vec3 angles_to_vector( const glm::vec3 & angles ) { static const glm::mat4 P( glm::perspective( glm::radians( 120.0f ), 640.0f / 480.0f, 0.1f, 10000.0f ) ); static UB test_ub; +static Shader texture_shader; extern "C" GAME_INIT( game_init ) { bsp_init( &game->bsp, "acidwdm2.bsp" ); @@ -305,6 +334,7 @@ extern "C" GAME_INIT( game_init ) { game->angles = glm::radians( glm::vec3( -90, 135, 0 ) ); game->test_shader = renderer_new_shader( vert_src, frag_src ); + texture_shader = renderer_new_shader( textured_vert_src, textured_frag_src ); bspr_init( &game->bspr, &arena, &game->bsp ); @@ -367,4 +397,69 @@ extern "C" GAME_FRAME( game_frame ) { } immediate_render( &imm, game->test_at_position, game->test_at_colour ); + + GLuint tex; + glActiveTexture( GL_TEXTURE0 ); + glGenTextures( 1, &tex ); + glBindTexture( GL_TEXTURE_2D, tex ); + + const glm::vec4 checkerboard[] = { + glm::vec4( 0, 0, 0, 0.5 ), + glm::vec4( 1, 1, 1, 0.5 ), + glm::vec4( 1, 1, 1, 0.5 ), + glm::vec4( 0, 0, 0, 0.5 ), + }; + TextureConfig texture_config = { }; + texture_config.width = 2; + texture_config.height = 2; + texture_config.data = checkerboard; + Texture texture = renderer_new_texture( texture_config ); + + const float aspect = 640.0f / 480.0f; + const glm::vec4 white( 1, 1, 1, 1 ); + const int num_checks = 20; + + v3 positions[] = { + v3( 0.5, 1.0, 0 ), + v3( 1, 1, 0 ), + v3( 0.5, 0.5, 0 ), + v3( 0.5, 0.5, 0 ), + v3( 1, 1, 0 ), + v3( 1, 0.5, 0 ), + }; + float colours[] = { + 1, 1, 1, + 1, 1, 1, + 1, 1, 1, + 1, 1, 1, + 1, 1, 1, + 1, 1, 1, + }; + v2 texcoords[] = { + v2( 0, 0 ), + v2( num_checks, 0 ), + v2( 0, num_checks / aspect ), + v2( 0, num_checks / aspect ), + v2( num_checks, 0 ), + v2( num_checks, num_checks / aspect ), + }; + + MeshConfig mesh_config = { }; + mesh_config.positions = renderer_new_vb( positions, sizeof( positions ) ); + mesh_config.colours = renderer_new_vb( colours, sizeof( colours ) ); + mesh_config.tex_coords0 = renderer_new_vb( texcoords, sizeof( texcoords ) ); + mesh_config.num_vertices = 6; + Mesh mesh = renderer_new_mesh( mesh_config ); + + RenderState render_state2 = { }; + render_state2.shader = texture_shader; + render_state2.textures[ 0 ] = texture; + + renderer_draw_mesh( mesh, render_state2 ); + + renderer_delete_mesh( mesh ); + renderer_delete_vb( mesh_config.positions ); + renderer_delete_vb( mesh_config.colours ); + renderer_delete_ib( mesh_config.indices ); + renderer_delete_texture( texture ); }