From 180922c6fc6d6383e13ea40b01174ce74bf8923e Mon Sep 17 00:00:00 2001 From: rexy712 Date: Sat, 26 Sep 2020 10:51:14 -0700 Subject: [PATCH] Rudimentary grid --- src/main.cpp | 87 ++++++++++++++++++++++++++++++++++++++++++++-------- 1 file changed, 74 insertions(+), 13 deletions(-) diff --git a/src/main.cpp b/src/main.cpp index 54c2771..c74d454 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -10,6 +10,7 @@ #include "render.hpp" #include "game_state.hpp" #include "math/math.hpp" +#include "math/debug.hpp" #include "config.hpp" #include "audio/sndrd.hpp" #include "audio/mixdata.hpp" @@ -143,10 +144,28 @@ void gay_thread(sfx::mixer& m, std::atomic_bool& should_gay_thread_stop){ gfx::shader_program create_example_shader(){ static constexpr const char vertex_source[] = "#version 330 core\n" - "layout (location = 0) in vec3 aPos;" + "layout (location = 0) in vec3 position;" + "uniform mat4 vp_mat;" + "void main(){" - "gl_Position = vec4(aPos.x, aPos.y, aPos.z, 1.0);" + "gl_Position = vec4(position, 1.0);" "}"; + static constexpr const char geometry_source[] = "#version 330 core\n" + "layout (points) in;\n" + "layout (triangle_strip, max_vertices=4) out;\n" + "uniform mat4 vp_mat;\n" + + "void main(){\n" + "gl_Position = vp_mat * vec4((gl_in[0].gl_Position + vec4(-0.1, 0.1, 0, 0)).xyz, 1.0);\n" + "EmitVertex();\n" + "gl_Position = vp_mat * vec4((gl_in[0].gl_Position + vec4(-0.1, -0.1, 0, 0)).xyz, 1.0);\n" + "EmitVertex();\n" + "gl_Position = vp_mat * vec4((gl_in[0].gl_Position + vec4(0.1, 0.1, 0, 0)).xyz, 1.0);\n" + "EmitVertex();\n" + "gl_Position = vp_mat * vec4((gl_in[0].gl_Position + vec4(0.1, -0.1, 0, 0)).xyz, 1.0);\n" + "EmitVertex();\n" + "EndPrimitive();\n" + "}"; static constexpr const char fragment_source[] = "#version 330 core\n" "out vec4 FragColor;" "void main(){" @@ -155,6 +174,7 @@ gfx::shader_program create_example_shader(){ gfx::shader_program prog; gfx::shader vert(vertex_source, gfx::shader::type::VERTEX); gfx::shader frag(fragment_source, gfx::shader::type::FRAGMENT); + gfx::shader geo(geometry_source, gfx::shader::type::GEOMETRY); bool error = false; if(vert.has_compile_error()){ debug_print_error("%s\n", vert.get_error().c_str()); @@ -164,9 +184,13 @@ gfx::shader_program create_example_shader(){ debug_print_error("%s\n", frag.get_error().c_str()); error = true; } + if(geo.has_compile_error()){ + debug_print_error("%s\n", geo.get_error().c_str()); + error = true; + } if(error) return prog; - prog.attach_shaders(vert, frag); + prog.attach_shaders(vert, frag, geo); prog.link(); if(prog.has_link_error()){ debug_print_error("%s\n", prog.get_error().c_str()); @@ -174,31 +198,68 @@ gfx::shader_program create_example_shader(){ return prog; } -int main(){ - static constexpr float vertices[] = { - -0.5f, -0.5f, 0.0f, - 0.5f, -0.5f, 0.0f, - 0.0f, 0.5f, 0.0f +class grid +{ +private: + math::mat3> m_mat{ + math::vec3{0.0f, 0.5f, -1.0f}, + math::vec3{0.5f, 0.5f, -1.0f}, + math::vec3{-0.5f, 0.5f, -1.0f}, + + math::vec3{0.0f, 0.0f, -1.0f}, + math::vec3{0.5f, 0.0f, -1.0f}, + math::vec3{-0.5f, 0.0f, -1.0f}, + + math::vec3{0.0f, -0.5f, -1.0f}, + math::vec3{0.5f, -0.5f, -1.0f}, + math::vec3{-0.5f, -0.5f, -1.0f} }; +public: + grid() = default; + grid(const grid&) = default; + grid(grid&&) = default; + ~grid() = default; + + grid& operator=(const grid&) = default; + grid& operator=(grid&&) = default; + + auto operator[](size_t index)const{ + return m_mat[index]; + } +}; + +int main(){ + const grid g; + const math::mat4 identity_mat = math::ortho_projection(10.0f, 10.0f, 0.1f, 100.0f); + srand(time(NULL)); game_state gs = {}; - sfx::mixer mix(sfx::mixer::mode::STEREO, 1, 44100); + //window testing setup render_manager manager(640, 480, "Tic-Tac-Gugh"); manager.handle_window_close_event(handle_window_close); manager.handle_keypress_event(handle_input_events); + //audio testing thread setup + sfx::mixer mix(sfx::mixer::mode::STEREO, 1, 44100); std::atomic_bool should_gay_thread_stop = false; std::thread t(gay_thread, std::ref(mix), std::ref(should_gay_thread_stop)); //create our gl objects - gfx::vbo vbo(sizeof(vertices), gfx::vbo::usage::STATIC_DRAW); + gfx::vbo vbo(sizeof(GLfloat) * sizeof(27), gfx::vbo::usage::STATIC_DRAW); gfx::shader_program prog = create_example_shader(); gfx::vao vao; - //put the vertex data in the gl state - vbo.buffer(vertices, sizeof(vertices)); + //add our view-projection matrix in the glsl uniform variable + prog.set_uniform("vp_mat", identity_mat); + + //put the grid square positions + for(size_t i = 0;i < 3;++i){ + for(size_t j = 0;j < 3;++j){ + vbo.buffer(g[i][j], sizeof(GLfloat) * 3); + } + } //tell gl how to interpret the data in the vbo vao.bind(); @@ -211,7 +272,7 @@ int main(){ //render using program 'prog' with data from 'vao' prog.use(); vao.bind(); - glDrawArrays(GL_TRIANGLES, 0, 3); + glDrawArrays(GL_POINTS, 0, 9); manager.update(); }