commit ae1b939e6ee51296d7bc15fffaead14ec04d8889 parent 840590e10e0f8c4eb94d8dc4c1b40176d6063cc6 Author: Michael Savage <mikejsavage@gmail.com> Date: Sat Nov 26 22:23:40 +0200 Fix skybox Diffstat:
hm.cc | | | 5 | +++-- |
skybox.cc | | | 83 | +++++++++++++++++++++++++++++++++++++++++++------------------------------------ |
skybox.h | | | 6 | ++++-- |
diff --git a/hm.cc b/hm.cc @@ -9,6 +9,7 @@ #include "log.h" #include "heightmap.h" #include "terrain_manager.h" +#include "skybox.h" #include "work_queue.h" #include "stb_truetype.h" @@ -203,7 +204,7 @@ extern "C" GAME_INIT( game_init ) { game->font_at_uv = glGetAttribLocation( game->font_shader, "uv" ); game->font_un_atlas = glGetUniformLocation( game->font_shader, "atlas" ); - // skybox_init( &game->skybox ); + skybox_init( &game->skybox ); } static void draw_string( const GameState * game, @@ -275,8 +276,8 @@ extern "C" GAME_FRAME( game_frame ) { m4 V = camera_to_view( game->pos, game->angles ); m4 VP = V * P; // TODO: WTF + skybox_render( &game->skybox, game->angles, game->test_sun ); terrain_render( &game->tm, V, VP, game->test_sun, current_time ); - // skybox_render( &game->skybox, game->angles ); glDisable( GL_DEPTH_TEST ); glUseProgram( game->test_shader ); diff --git a/skybox.cc b/skybox.cc @@ -6,49 +6,50 @@ #include "game.h" #include "intrinsics.h" - -/* - * 0------1 - * |`. |`. y - * | `2--+---3 \_x - * | | | | | - * 4---+--5 | -z - * `. | `. | - * `6------7 - */ +#include "skybox.h" static const char * vert_src = GLSL( in vec3 position; - out vec3 pos; - uniform mat3 V; + uniform mat4 V; + uniform mat4 P; void main() { - // gl_Position = vec4( ( V * vec4( position, 1.0 ) ).xy, 1.0, 1.0 ); - gl_Position = vec4( position.xy, 1.0, 1.0 ); - pos = gl_Position.xyz; + gl_Position = P * V * vec4( position, 1.0 ); + pos = position; } ); static const char * frag_src = GLSL( in vec3 pos; - out vec4 screen_colour; + out vec4 colour; + + uniform float sun; void main() { - screen_colour = vec4( pos, 1.0 ); + vec3 sun_direction = normalize( vec3( -1, 0, sun ) ); + + vec4 horizon = vec4( 0.4, 0.7, 1.0, 1.0 ); + vec4 top = vec4( 0.3, 0.6, 1.0, 1.0 ); + vec4 sunny = vec4( 0.9, 0.9, 0.2, 1.0 ); + + float scaled_z = smoothstep( 0.0, 0.2, pos.z ); + float sun_dot = smoothstep( 0.99, 1.0, dot( sun_direction, normalize( pos ) ) ); + + colour = mix( mix( horizon, top, scaled_z ), sunny, sun_dot ); } ); -static glm::vec3 cube_verts[] = { - glm::vec3( -1.0f, 1.0f, 1.0f ), - glm::vec3( 1.0f, 1.0f, 1.0f ), - glm::vec3( -1.0f, -1.0f, 1.0f ), - glm::vec3( 1.0f, -1.0f, 1.0f ), - glm::vec3( -1.0f, 1.0f, -1.0f ), - glm::vec3( 1.0f, 1.0f, -1.0f ), - glm::vec3( -1.0f, -1.0f, -1.0f ), - glm::vec3( 1.0f, -1.0f, -1.0f ), +static v3 cube_verts[] = { + v3( -1.0f, 1.0f, 1.0f ), + v3( 1.0f, 1.0f, 1.0f ), + v3( -1.0f, -1.0f, 1.0f ), + v3( 1.0f, -1.0f, 1.0f ), + v3( -1.0f, 1.0f, -1.0f ), + v3( 1.0f, 1.0f, -1.0f ), + v3( -1.0f, -1.0f, -1.0f ), + 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 }; @@ -56,7 +57,9 @@ static GLuint 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_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 ); @@ -74,26 +77,30 @@ void skybox_init( Skybox * skybox ) { glBindVertexArray( 0 ); } -void skybox_render( const Skybox * skybox, const glm::vec3 & angles ) { +static glm::mat4 P( glm::perspective( glm::radians( 120.0f ), float( WIDTH ) / float( HEIGHT ), 0.1f, 10000.0f ) ); + +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 ); - const glm::mat3 V = glm::mat3( glm::rotate( - glm::rotate( - glm::mat4(), - angles.x, - glm::vec3( 1, 0, 0 ) - ), - angles.y, - glm::vec3( 0, 0, 1 ) - ) ); + m4 V = angles_to_view( angles ); - glUniformMatrix4fv( skybox->un_v, 1, GL_FALSE, glm::value_ptr( V ) ); + 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 ); glDrawElements( GL_TRIANGLE_STRIP, ARRAY_COUNT( cube_indices ), GL_UNSIGNED_INT, 0 ); glBindVertexArray( 0 ); glUseProgram( 0 ); + glEnable( GL_CULL_FACE ); + glDepthMask( GL_TRUE ); } void skybox_destroy( Skybox * skybox ) { diff --git a/skybox.h b/skybox.h @@ -2,12 +2,14 @@ #define _SKYBOX_H_ #include "glad.h" -#include <glm/glm.hpp> +#include "linear_algebra.h" struct Skybox { GLuint shader; GLuint at_position; GLuint un_v; + GLuint un_p; + GLuint un_sun; GLuint vao; GLuint vbo; @@ -15,7 +17,7 @@ struct Skybox { }; void skybox_init( Skybox * skybox ); -void skybox_render( const Skybox * skybox, const glm::vec3 & angles ); +void skybox_render( const Skybox * skybox, v3 angles, float sun ); void skybox_destroy( Skybox * skybox ); #endif // _SKYBOX_H_