medfall

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

commit 4d2d7fdd77bd3dfe7ade323714d2cd474689cd13
parent b3701d2134bf7068c4eed301dd437aa10acb5fba
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Aug 16 19:36:56 +0200

Start immediate rendering implementation. Only draws unshaded triangles for now

Diffstat:
Makefile | 2+-
game.h | 5+++++
hm.cc | 79++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
immediate.cc | 45+++++++++++++++++++++++++++++++++++++++++++++
immediate.h | 23+++++++++++++++++++++++
5 files changed, 128 insertions(+), 26 deletions(-)
diff --git a/Makefile b/Makefile @@ -2,7 +2,7 @@ all: medfall hm.so pp OBJS = main.o gl.o BSPOBJS = bsp.o bsp_renderer.o gl.o -HMOBJS = hm.o heightmap.o terrain_manager.o work_queue.o stb_image.o stb_perlin.o +HMOBJS = hm.o heightmap.o terrain_manager.o work_queue.o stb_image.o stb_perlin.o immediate.o PPOBJS = pp.o stb_image.o stb_image_write.o WARNINGS = -Wall -Wextra -Wno-unused-parameter -Wno-unused-function -Wno-write-strings diff --git a/game.h b/game.h @@ -6,6 +6,7 @@ #include "intrinsics.h" #include "terrain_manager.h" +#include "immediate.h" #include "work_queue.h" struct GameState { @@ -14,6 +15,10 @@ struct GameState { TerrainManager tm; WorkQueue background_tasks; + + GLuint test_shader; + GLuint test_at_position; + ImmediateContext test_immediate; }; struct GameMemory { diff --git a/hm.cc b/hm.cc @@ -13,20 +13,26 @@ #include "heightmap.h" #include "terrain_manager.h" #include "work_queue.h" -// #include "stb_easy_font.h" #include "stb_image.h" #include "stb_perlin.h" -#define TEXT( x, y, form, ... ) \ - do { \ - static char buffer[ 99999 ]; \ - static char text[ 2048 ]; \ - sprintf( text, form, __VA_ARGS__ ); \ - const int num_quads = stb_easy_font_print( x, y, text, nullptr, buffer, sizeof( buffer ) ); \ - glColor3f(1,1,1); \ - glVertexPointer(2, GL_FLOAT, 16, buffer); \ - glDrawArrays(GL_QUADS, 0, num_quads*4); \ - } while( 0 ) +#include "shitty_glsl.h" + +static const GLchar * const vert_src = GLSL( + in vec3 position; + + void main() { + gl_Position = vec4( position, 1.0 ); + } +); + +static const GLchar * frag_src = GLSL( + out vec4 screen_colour; + + void main() { + screen_colour = vec4( 1.0, 0.0, 0.0, 1.0 ); + } +); glm::vec3 angles_to_vector( const glm::vec3 & angles ) { return glm::vec3( @@ -65,6 +71,39 @@ extern "C" GAME_INIT( game_init ) { } workqueue_exhaust( &state->background_tasks ); + state->test_shader = compile_shader( vert_src, frag_src, "screen_colour" ); + state->test_at_position = glGetAttribLocation( state->test_shader, "position" ); + + // TODO: persistent memory should be an arena + const size_t triangles = 65536; + ImmediateTriangle * immediate_memory = ( ImmediateTriangle * ) reserve_persistent( mem, sizeof( ImmediateTriangle ) * triangles ); + immediate_init( &state->test_immediate, immediate_memory, triangles ); + + const float aspect = 640.0f / 480.0f; + const float crosshair_thickness = 0.005; + const float crosshair_length = 0.02; + + immediate_triangle( &state->test_immediate, + glm::vec3( -crosshair_length, -crosshair_thickness, 0 ), + glm::vec3( -crosshair_length, crosshair_thickness, 0 ), + glm::vec3( crosshair_length, crosshair_thickness, 0 ) + ); + immediate_triangle( &state->test_immediate, + glm::vec3( crosshair_length, crosshair_thickness, 0 ), + glm::vec3( crosshair_length, -crosshair_thickness, 0 ), + glm::vec3( -crosshair_length, -crosshair_thickness, 0 ) + ); + immediate_triangle( &state->test_immediate, + glm::vec3( -crosshair_thickness / aspect, crosshair_length * aspect , 0 ), + glm::vec3( crosshair_thickness / aspect, crosshair_length * aspect , 0 ), + glm::vec3( crosshair_thickness / aspect, -crosshair_length * aspect , 0 ) + ); + immediate_triangle( &state->test_immediate, + glm::vec3( crosshair_thickness / aspect, -crosshair_length * aspect , 0 ), + glm::vec3( -crosshair_thickness / aspect, -crosshair_length * aspect , 0 ), + glm::vec3( -crosshair_thickness / aspect, crosshair_length * aspect , 0 ) + ); + glClearColor( 0, 0.5, 0.7, 1 ); } @@ -106,18 +145,8 @@ extern "C" GAME_FRAME( game_frame ) { state->tm.render( VP ); - // glLoadIdentity(); - // - // glBegin( GL_TRIANGLE_STRIP ); - // glColor3f( 0.2, 0.2, 0.2 ); - // glVertex2f( -1, 1 ); - // glVertex2f( 1, 1 ); - // glVertex2f( -1, 0.95 ); - // glVertex2f( 1, 0.95 ); - // glEnd(); - // - // glOrtho( 0, 640, 480, 0, -1, 1 ); - // TEXT( 2, 2, "%d", ( int ) ( 1 / dt ) ); - // - // glPopMatrix(); + glDisable( GL_DEPTH_TEST ); + glUseProgram( state->test_shader ); + immediate_render( &state->test_immediate, state->test_at_position ); + glEnable( GL_DEPTH_TEST ); } diff --git a/immediate.cc b/immediate.cc @@ -0,0 +1,45 @@ +#include "platform_opengl.h" +#include <glm/glm.hpp> + +#include "intrinsics.h" +#include "immediate.h" + +void immediate_init( ImmediateContext * const ctx, ImmediateTriangle * const memory, const size_t max_triangles ) { + ctx->triangles = memory; + ctx->num_triangles = 0; + ctx->max_triangles = max_triangles; +} + +void immediate_triangle( ImmediateContext * const ctx, const glm::vec3 v1, const glm::vec3 v2, const glm::vec3 v3 ) { + assert( ctx->num_triangles < ctx->max_triangles - 1 ); + + ctx->triangles[ ctx->num_triangles++ ] = { v1, v2, v3 }; +} + +void immediate_render( ImmediateContext * const ctx, const GLuint at_position ) { + GLuint vao; + glGenVertexArrays( 1, &vao ); + glBindVertexArray( vao ); + + GLuint vbo; + glGenBuffers( 1, &vbo ); + + glBindBuffer( GL_ARRAY_BUFFER, vbo ); + glBufferData( GL_ARRAY_BUFFER, ctx->num_triangles * sizeof( ImmediateTriangle ), ctx->triangles, GL_STATIC_DRAW ); + + glEnableVertexAttribArray( at_position ); + glVertexAttribPointer( at_position, 3, GL_FLOAT, GL_FALSE, 0, 0 ); + + glDrawArrays( GL_TRIANGLES, 0, ctx->num_triangles * 3 ); + + glBindVertexArray( 0 ); + + glDeleteBuffers( 1, &vbo ); + glDeleteVertexArrays( 1, &vao ); + + printf( "drew %zu triangles vao %u vbo %u\n", ctx->num_triangles, vbo, vbo ); +} + +void immediate_clear( ImmediateContext * const ctx ) { + ctx->num_triangles = 0; +} diff --git a/immediate.h b/immediate.h @@ -0,0 +1,23 @@ +#ifndef _IMMEDIATE_H_ +#define _IMMEDIATE_H_ + +#include <glm/glm.hpp> + +struct ImmediateTriangle { + glm::vec3 v1; + glm::vec3 v2; + glm::vec3 v3; +}; + +struct ImmediateContext { + ImmediateTriangle * triangles; + size_t num_triangles; + size_t max_triangles; +}; + +void immediate_init( ImmediateContext * const ctx, ImmediateTriangle * const memory, const size_t max_triangles ); +void immediate_triangle( ImmediateContext * const ctx, const glm::vec3 v1, const glm::vec3 v2, const glm::vec3 v3 ); +void immediate_render( ImmediateContext * const ctx, const GLuint at_position ); +void immediate_clear( ImmediateContext * const ctx ); + +#endif // _IMMEDIATE_H_