medfall

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

commit 5ee37304fa4260a4ba6319f9a2f63b25ad5b4a35
parent 5cf6f34a70e3297c9eed22ac6291559b8e4a0244
Author: Michael Savage <mikejsavage@gmail.com>
Date:   Sun Jan 31 11:27:01 +0000

Add shadow_map.cc so sm.so builds

Diffstat:
shadow_map.cc | 150+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 150 insertions(+), 0 deletions(-)
diff --git a/shadow_map.cc b/shadow_map.cc @@ -0,0 +1,150 @@ +#include "platform_opengl.h" +#include <glm/glm.hpp> +#include <glm/gtc/matrix_transform.hpp> +#include <glm/gtc/type_ptr.hpp> + +#include "game.h" +#include "intrinsics.h" + +static ImmediateTriangle triangles[ 512000 ]; +static ImmediateContext imm; + +static GLuint prog; +static GLint at_position; +static GLint at_colour; +static GLint un_vp; + +static GLuint fbo; +static GLuint tex_depth; + +static const GLchar * const vert_src = GLSL( + in vec3 position; + in vec3 colour; + + out vec3 frag_colour; + + uniform mat4 VP; + + void main() { + gl_Position = VP * vec4( position, 1.0 ); + frag_colour = colour; + } +); + +static const GLchar * frag_src = GLSL( + in vec3 frag_colour; + + out vec4 screen_colour; + + void main() { + screen_colour = vec4( frag_colour, 1.0 ); + } +); + +static void draw_scene( GLint at_position, GLint at_colour ) { + immediate_init( &imm, triangles, array_count( triangles ) ); + + immediate_sphere( &imm, glm::vec3( 0, 0, 5 ), 3, glm::vec4( 1, 0, 0, 1 ) ); + immediate_sphere( &imm, glm::vec3( -3, 7, 5 ), 2, glm::vec4( 0, 1, 0, 1 ) ); + + glm::vec3 tl( -15, 15, 0 ); + glm::vec3 tr( 15, 15, 0 ); + glm::vec3 bl( -15, -15, 0 ); + glm::vec3 br( 15, -15, 0 ); + immediate_triangle( &imm, tl, tr, bl, glm::vec4( 0.5, 0.5, 0.5, 1.0 ) ); + immediate_triangle( &imm, bl, tr, br, glm::vec4( 0.5, 0.5, 0.5, 1.0 ) ); + + immediate_render( &imm, at_position, at_colour ); +} + +static glm::vec3 angles_to_vector( const glm::vec3 & angles ) { + return glm::vec3( + -sin( angles.y ) * sin( angles.x ), + -cos( angles.y ) * sin( angles.x ), + -cos( angles.x ) + ); +} + +static void update_camera( + const GameInput * input, float dt, + glm::vec3 position, glm::vec3 angles, + glm::vec3 * out_position, glm::vec3 * out_angles +) { + float speed = 6.0f; + float angular_speed = 2.0f; + + float dx = ( float ) input->keys[ 'a' ] - ( float ) input->keys[ 'd' ]; + float dy = ( float ) input->keys[ 'w' ] - ( float ) input->keys[ 's' ]; + float dz = ( float ) input->keys[ KEY_SPACE ] - ( float ) input->keys[ KEY_LEFTSHIFT ]; + + glm::vec3 forward = angles_to_vector( angles ); + glm::vec3 sideways = glm::vec3( -cosf( angles.y ), sinf( angles.y ), 0 ); + + *out_position = position + + forward * dt * dy * speed + + sideways * dt * dx * speed; + out_position->z += dt * dz * speed; + + float pitch = ( float ) input->keys[ KEY_UPARROW ] - ( float ) input->keys[ KEY_DOWNARROW ]; + float yaw = ( float ) input->keys[ KEY_RIGHTARROW ] - ( float ) input->keys[ KEY_LEFTARROW ]; + + *out_angles = angles + glm::vec3( dt * pitch * angular_speed, dt * yaw * angular_speed, 0 ); +} + +static void camera_to_vp( glm::vec3 position, glm::vec3 angles, glm::mat4 * out_vp ) { + const glm::mat4 P( glm::perspective( glm::radians( 120.0f ), ( float ) WIDTH / ( float ) HEIGHT, NEAR, 100.0f ) ); + + *out_vp = glm::translate( + glm::rotate( + glm::rotate( + P, + angles.x, + glm::vec3( 1, 0, 0 ) + ), + angles.y, + glm::vec3( 0, 0, 1 ) + ), + -position + ); +} + +extern "C" GAME_INIT( game_init ) { + prog = compile_shader( vert_src, frag_src, "screen_colour" ); + at_position = glGetAttribLocation( prog, "position" ); + at_colour = glGetAttribLocation( prog, "colour" ); + un_vp = glGetUniformLocation( prog, "VP" ); + + glGenFramebuffers( 1, &fbo ); + glBindFramebuffer( GL_DRAW_FRAMEBUFFER, fbo ); + glGenTextures( 1, &tex_depth ); + glBindTexture( GL_TEXTURE_2D, tex_depth ); + + glTexImage2D( GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT32F, WIDTH, HEIGHT, 0, GL_DEPTH_COMPONENT, GL_FLOAT, NULL ); + glFramebufferTexture2D( GL_DRAW_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, tex_depth, 0 ); + + glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 ); + + state->pos = glm::vec3( -10, -10, 5 ); + state->angles = glm::radians( ( glm::vec3( -90, 45, 0 ) ) ); +} + +extern "C" GAME_FRAME( game_frame ) { + glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); + + update_camera( input, dt, state->pos, state->angles, &state->pos, &state->angles ); + glm::mat4 vp; + camera_to_vp( state->pos, state->angles, &vp ); + + glUseProgram( prog ); + glUniformMatrix4fv( un_vp, 1, GL_FALSE, glm::value_ptr( vp ) ); + + glBindFramebuffer( GL_DRAW_FRAMEBUFFER, fbo ); + draw_scene( at_position, at_colour ); + glBindFramebuffer( GL_DRAW_FRAMEBUFFER, 0 ); + + glBindFramebuffer( GL_READ_FRAMEBUFFER, fbo ); + glBlitFramebuffer( 0, 0, WIDTH, HEIGHT, 0, 0, WIDTH, HEIGHT, GL_COLOR_BUFFER_BIT, GL_LINEAR ); + glBindFramebuffer( GL_READ_FRAMEBUFFER, 0 ); + + glUseProgram( 0 ); +}