commit 8e369caeb240febddc789fc1dca7b4f7c48a4a70 parent 5ee37304fa4260a4ba6319f9a2f63b25ad5b4a35 Author: Michael Savage <mikejsavage@gmail.com> Date: Sun Jan 31 11:27:14 +0000 Add skybox code so btt builds Diffstat:
skybox.cc | | | 115 | +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ |
skybox.h | | | 21 | +++++++++++++++++++++ |
diff --git a/skybox.cc b/skybox.cc @@ -0,0 +1,115 @@ +#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" + +/* + * 0------1 + * |`. |`. y + * | `2--+---3 \_x + * | | | | | + * 4---+--5 | -z + * `. | `. | + * `6------7 + */ + +static const GLchar * const vert_src = GLSL( + in vec3 position; + + out vec3 direction; + + uniform mat4 VP; + + void main() { + vec4 world_pos = VP * vec4( position, 1.0 ); + gl_Position = world_pos.xyww; + direction = position; + } +); + +static const GLchar * frag_src = GLSL( + in vec3 direction; + + out vec4 screen_colour; + + void main() { + screen_colour = vec4( 1.0, 0.0, 0.0, 1.0 ); + } +); + +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 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_vp = glGetUniformLocation( skybox->shader, "vp" ); + + 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 ); +} + +static const glm::mat4 P( glm::perspective( glm::radians( 120.0f ), ( float ) WIDTH / ( float ) HEIGHT, NEAR, FAR ) ); + +void skybox_render( const Skybox * skybox, const glm::vec3 & angles ) { + glDisable( GL_CULL_FACE ); + for( size_t i = 0; i < array_count( cube_verts ); i++ ) { + glm::vec4 x = P * glm::vec4( cube_verts[ i ], 1.0f ); + printf( "%d: %.3f %.3f %.3f %.3f\n", i, x.x, x.y, x.z, x.w ); + } + glm::mat4 vp = glm::rotate( + glm::rotate( + P, + angles.x, + glm::vec3( 1, 0, 0 ) + ), + angles.y, + glm::vec3( 0, 0, 1 ) + ); + + glUseProgram( skybox->shader ); + glBindVertexArray( skybox->vao ); + + glUniformMatrix4fv( skybox->un_vp, 1, GL_FALSE, glm::value_ptr( vp ) ); + + glDrawElements( GL_TRIANGLE_STRIP, array_count( cube_indices ), GL_UNSIGNED_INT, 0 ); + + glBindVertexArray( 0 ); + glUseProgram( 0 ); + glEnable( GL_CULL_FACE ); +} + +void skybox_destroy( Skybox * skybox ) { + glDeleteBuffers( 1, &skybox->ebo ); + glDeleteBuffers( 1, &skybox->vbo ); + glDeleteVertexArrays( 1, &skybox->vao ); + + glDeleteProgram( skybox->shader ); + + // *skybox = { }; +} diff --git a/skybox.h b/skybox.h @@ -0,0 +1,21 @@ +#ifndef _SKYBOX_H_ +#define _SKYBOX_H_ + +#include "platform_opengl.h" +#include <glm/glm.hpp> + +struct Skybox { + GLuint shader; + GLuint at_position; + GLuint un_vp; + + GLuint vao; + GLuint vbo; + GLuint ebo; +}; + +void skybox_init( Skybox * skybox ); +void skybox_render( const Skybox * skybox, const glm::vec3 & angles ); +void skybox_destroy( Skybox * skybox ); + +#endif // _SKYBOX_H_