medfall

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

commit 3cacd7b6ef1b9ea5aea92752ad720fca96286f1e
parent 1cb06957726f890a351b6541af9d362e7c33df41
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sat Nov 26 22:47:01 +0200

Use the new renderer API to draw the skybox

Diffstat:
skybox.cc | 106++++++++++++++++++++++++++++++++++++++-----------------------------------------
skybox.h | 15+++++----------
2 files changed, 56 insertions(+), 65 deletions(-)
diff --git a/skybox.cc b/skybox.cc @@ -1,19 +1,16 @@ -#include "glad.h" -#include "glsl.h" -#include <glm/glm.hpp> -#include <glm/gtc/matrix_transform.hpp> -#include <glm/gtc/type_ptr.hpp> - -#include "game.h" #include "intrinsics.h" +#include "game.h" +#include "linear_algebra.h" #include "skybox.h" static const char * vert_src = GLSL( in vec3 position; out vec3 pos; - uniform mat4 V; - uniform mat4 P; + layout( std140 ) uniform v_hot { + mat4 V; + mat4 P; + }; void main() { gl_Position = P * V * vec4( position, 1.0 ); @@ -25,7 +22,9 @@ static const char * frag_src = GLSL( in vec3 pos; out vec4 colour; - uniform float sun; + layout( std140 ) uniform f_hot { + float sun; + }; void main() { vec3 sun_direction = normalize( vec3( -1, 0, sun ) ); @@ -41,7 +40,7 @@ static const char * frag_src = GLSL( } ); -static v3 cube_verts[] = { +static const v3 cube_verts[] = { v3( -1.0f, 1.0f, 1.0f ), v3( 1.0f, 1.0f, 1.0f ), v3( -1.0f, -1.0f, 1.0f ), @@ -52,63 +51,60 @@ static v3 cube_verts[] = { v3( 1.0f, -1.0f, -1.0f ), }; -static GLuint cube_indices[] = { 7, 6, 3, 2, 0, 6, 4, 7, 5, 3, 1, 0, 5, 4 }; +static const u32 cube_indices[] = { 7, 6, 3, 2, 0, 6, 4, 7, 5, 3, 1, 0, 5, 4 }; void skybox_init( Skybox * skybox ) { - skybox->shader = compile_shader( vert_src, frag_src, "screen_colour" ); - skybox->at_position = glGetAttribLocation( skybox->shader, "position" ); - skybox->un_v = glGetUniformLocation( skybox->shader, "V" ); - skybox->un_p = glGetUniformLocation( skybox->shader, "P" ); - skybox->un_sun = glGetUniformLocation( skybox->shader, "sun" ); - - glGenVertexArrays( 1, &skybox->vao ); - glBindVertexArray( skybox->vao ); - - glGenBuffers( 1, &skybox->vbo ); - glBindBuffer( GL_ARRAY_BUFFER, skybox->vbo ); - glBufferData( GL_ARRAY_BUFFER, sizeof( cube_verts ), cube_verts, GL_STATIC_DRAW ); - glEnableVertexAttribArray( skybox->at_position ); - glVertexAttribPointer( skybox->at_position, 3, GL_FLOAT, GL_FALSE, sizeof( glm::vec3 ), 0 ); - - glGenBuffers( 1, &skybox->ebo ); - glBindBuffer( GL_ELEMENT_ARRAY_BUFFER, skybox->ebo ); - glBufferData( GL_ELEMENT_ARRAY_BUFFER, sizeof( cube_indices ), cube_indices, GL_STATIC_DRAW ); - - glBindVertexArray( 0 ); + VB vb = renderer_new_vb( cube_verts, sizeof( cube_verts ) ); + IB ib = renderer_new_ib( cube_indices, sizeof( cube_indices ) ); + + MeshConfig mesh_config = { }; + mesh_config.positions = vb; + mesh_config.indices = ib; + mesh_config.num_vertices = ARRAY_COUNT( cube_indices ); + mesh_config.primitive_type = PRIMITIVETYPE_TRIANGLE_STRIP; + + skybox->shader = renderer_new_shader( vert_src, frag_src ); + skybox->mesh = renderer_new_mesh( mesh_config ); + skybox->vertex_uniforms = renderer_new_ub(); + skybox->fragment_uniforms = renderer_new_ub(); + + renderer_delete_vb( vb ); + renderer_delete_ib( ib ); } -static glm::mat4 P( glm::perspective( glm::radians( 120.0f ), float( WIDTH ) / float( HEIGHT ), 0.1f, 10000.0f ) ); +struct VSData { + m4 V; + m4 P; +}; + +struct FSData { + float sun; +}; static m4 angles_to_view( v3 angles ) { return m4_rotz( -angles.y ) * m4_rotx( -angles.x ); } -void skybox_render( const Skybox * skybox, v3 angles, float sun ) { - glDisable( GL_CULL_FACE ); - glDepthMask( GL_FALSE ); - glUseProgram( skybox->shader ); - glBindVertexArray( skybox->vao ); - - m4 V = angles_to_view( angles ); +void skybox_render( const Skybox * skybox, v3 view_angles, float sun ) { + m4 P = m4_perspective( 120.0f, ( float ) WIDTH / ( float ) HEIGHT, NEAR_PLANE_DEPTH, FAR_PLANE_DEPTH ); + m4 V = angles_to_view( view_angles ); - glUniformMatrix4fv( skybox->un_v, 1, GL_FALSE, ( float * ) &V ); - glUniformMatrix4fv( skybox->un_p, 1, GL_FALSE, glm::value_ptr( P ) ); - glUniform1f( skybox->un_sun, sun ); + VSData vsdata = { V, P }; + FSData fsdata = { sun }; + renderer_ub_data( skybox->vertex_uniforms, &vsdata, sizeof( vsdata ) ); + renderer_ub_data( skybox->fragment_uniforms, &fsdata, sizeof( fsdata ) ); - glDrawElements( GL_TRIANGLE_STRIP, ARRAY_COUNT( cube_indices ), GL_UNSIGNED_INT, 0 ); + RenderState render_state = { }; + render_state.shader = skybox->shader; + render_state.cull_face = CULLFACE_FRONT; + render_state.disable_depth_writes = true; + render_state.ubs[ UB_VS_HOT ] = skybox->vertex_uniforms; + render_state.ubs[ UB_FS_HOT ] = skybox->fragment_uniforms; - glBindVertexArray( 0 ); - glUseProgram( 0 ); - glEnable( GL_CULL_FACE ); - glDepthMask( GL_TRUE ); + renderer_draw_mesh( skybox->mesh, render_state ); } void skybox_destroy( Skybox * skybox ) { - glDeleteBuffers( 1, &skybox->ebo ); - glDeleteBuffers( 1, &skybox->vbo ); - glDeleteVertexArrays( 1, &skybox->vao ); - - glDeleteProgram( skybox->shader ); - - // *skybox = { }; + renderer_delete_mesh( skybox->mesh ); + // renderer_delete_shader( skybox->shader ); } diff --git a/skybox.h b/skybox.h @@ -1,19 +1,14 @@ #ifndef _SKYBOX_H_ #define _SKYBOX_H_ -#include "glad.h" #include "linear_algebra.h" +#include "renderer.h" struct Skybox { - GLuint shader; - GLuint at_position; - GLuint un_v; - GLuint un_p; - GLuint un_sun; - - GLuint vao; - GLuint vbo; - GLuint ebo; + Shader shader; + Mesh mesh; + UB vertex_uniforms; + UB fragment_uniforms; }; void skybox_init( Skybox * skybox );