diff --git a/include/blt/gfx/state.h b/include/blt/gfx/state.h new file mode 100644 index 0000000..51d4176 --- /dev/null +++ b/include/blt/gfx/state.h @@ -0,0 +1,76 @@ +/* + * + * Copyright (C) 2023 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ + +#ifndef BLT_WITH_GRAPHICS_STATE_H +#define BLT_WITH_GRAPHICS_STATE_H + +#include +#include +#include + +namespace blt::gfx +{ + /** + * layout (std140) uniform GlobalMatrices + * { + * mat4 projection; + * mat4 view; + * mat4 pvm; + * }; + */ + class matrix_state_manager + { + private: + blt::mat4x4 perspective; + blt::mat4x4 view; + blt::mat4x4 pv; + uniform_buffer* global_matrix_ubo = nullptr; + public: + [[nodiscard]] inline const blt::mat4x4& computedPV() const + { + return pv; + } + + void create_internals() + { + global_matrix_ubo = new uniform_buffer(sizeof(blt::mat4x4) * 3); + } + + void delete_internals() + { + delete global_matrix_ubo; + } + + template + void setView(U&& mat) + { + view = std::forward(mat); + } + + template + void setPerspective(U&& mat) + { + perspective = std::forward(mat); + } + + void update(); + }; + +} + +#endif //BLT_WITH_GRAPHICS_STATE_H diff --git a/libraries/BLT b/libraries/BLT index 5ae17f4..cc64e71 160000 --- a/libraries/BLT +++ b/libraries/BLT @@ -1 +1 @@ -Subproject commit 5ae17f4a86a021fb914f0c17b116dc511e5f3f93 +Subproject commit cc64e71c8d4ef550bad1eed5aaee3bd45fef9cad diff --git a/resources/models/complex_cube.mtl b/resources/models/complex_cube.mtl new file mode 100644 index 0000000..d8308ca --- /dev/null +++ b/resources/models/complex_cube.mtl @@ -0,0 +1,22 @@ +# Blender 3.4.1 MTL File: 'None' +# www.blender.org + +newmtl CubeMat1 +Ns 39.367584 +Ka 0.396825 0.396825 0.396825 +Kd 0.800000 0.025358 0.008865 +Ks 0.936508 0.936508 0.936508 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 3 + +newmtl CubeMat2 +Ns 250.000000 +Ka 1.000000 1.000000 1.000000 +Kd 0.004087 0.010183 0.800000 +Ks 0.500000 0.500000 0.500000 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 2 diff --git a/resources/models/complex_cube.obj b/resources/models/complex_cube.obj new file mode 100644 index 0000000..83a7cf2 --- /dev/null +++ b/resources/models/complex_cube.obj @@ -0,0 +1,91 @@ +# Blender 3.4.1 +# www.blender.org +mtllib complex_cube.mtl +o Cube +v -1.000000 -1.000000 1.000000 +v -1.000000 1.000000 1.000000 +v -1.000000 -1.000000 -1.000000 +v -1.000000 1.000000 -1.000000 +v 1.000000 -1.000000 1.000000 +v 1.000000 1.000000 1.000000 +v 1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 -1.000000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -0.0000 -1.0000 -0.0000 +vn -0.0000 1.0000 -0.0000 +vt 0.000000 0.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 1.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 0.000000 1.000000 +vt 1.000000 1.000000 +vt 1.000000 0.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 1.000000 1.000000 +s 0 +usemtl CubeMat1 +f 2/2/1 3/6/1 1/1/1 +f 4/8/2 7/14/2 3/4/2 +f 8/16/3 5/9/3 7/14/3 +f 6/12/4 1/1/4 5/10/4 +f 7/15/5 1/1/5 3/5/5 +f 4/8/6 6/13/6 8/16/6 +f 2/2/1 4/7/1 3/6/1 +f 4/8/2 8/16/2 7/14/2 +f 8/16/3 6/11/3 5/9/3 +f 6/12/4 2/2/4 1/1/4 +f 7/15/5 5/10/5 1/1/5 +f 4/8/6 2/3/6 6/13/6 +o Cube.001 +v -0.540163 0.959887 0.540163 +v -0.540163 2.040214 0.540163 +v -0.540163 0.959887 -0.540163 +v -0.540163 2.040214 -0.540163 +v 0.540163 0.959887 0.540163 +v 0.540163 2.040214 0.540163 +v 0.540163 0.959887 -0.540163 +v 0.540163 2.040214 -0.540163 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -0.0000 -1.0000 -0.0000 +vn -0.0000 1.0000 -0.0000 +vt 0.375000 0.000000 +vt 0.375000 1.000000 +vt 0.125000 0.750000 +vt 0.625000 0.000000 +vt 0.625000 1.000000 +vt 0.875000 0.750000 +vt 0.125000 0.500000 +vt 0.375000 0.250000 +vt 0.625000 0.250000 +vt 0.875000 0.500000 +vt 0.375000 0.750000 +vt 0.625000 0.750000 +vt 0.375000 0.500000 +vt 0.625000 0.500000 +s 0 +usemtl CubeMat2 +f 10/20/7 11/24/7 9/17/7 +f 12/25/8 15/29/8 11/24/8 +f 16/30/9 13/27/9 15/29/9 +f 14/28/10 9/18/10 13/27/10 +f 15/29/11 9/19/11 11/23/11 +f 12/26/12 14/28/12 16/30/12 +f 10/20/7 12/25/7 11/24/7 +f 12/25/8 16/30/8 15/29/8 +f 16/30/9 14/28/9 13/27/9 +f 14/28/10 10/21/10 9/18/10 +f 15/29/11 13/27/11 9/19/11 +f 12/26/12 10/22/12 14/28/12 diff --git a/resources/models/cube.mtl b/resources/models/cube.mtl new file mode 100644 index 0000000..fed3761 --- /dev/null +++ b/resources/models/cube.mtl @@ -0,0 +1,12 @@ +# Blender 3.4.1 MTL File: 'None' +# www.blender.org + +newmtl CubeMat1 +Ns 39.367584 +Ka 0.396825 0.396825 0.396825 +Kd 0.800000 0.025358 0.008865 +Ks 0.936508 0.936508 0.936508 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 3 diff --git a/resources/models/cube.obj b/resources/models/cube.obj new file mode 100644 index 0000000..db4d894 --- /dev/null +++ b/resources/models/cube.obj @@ -0,0 +1,48 @@ +# Blender 3.4.1 +# www.blender.org +mtllib cube.mtl +o Cube +v -1.000000 -1.000000 1.000000 +v -1.000000 1.000000 1.000000 +v -1.000000 -1.000000 -1.000000 +v -1.000000 1.000000 -1.000000 +v 1.000000 -1.000000 1.000000 +v 1.000000 1.000000 1.000000 +v 1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 -1.000000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -0.0000 -1.0000 -0.0000 +vn -0.0000 1.0000 -0.0000 +vt 0.000000 0.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 1.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 0.000000 1.000000 +vt 1.000000 1.000000 +vt 1.000000 0.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 1.000000 1.000000 +s 0 +usemtl CubeMat1 +f 2/2/1 3/6/1 1/1/1 +f 4/8/2 7/14/2 3/4/2 +f 8/16/3 5/9/3 7/14/3 +f 6/12/4 1/1/4 5/10/4 +f 7/15/5 1/1/5 3/5/5 +f 4/8/6 6/13/6 8/16/6 +f 2/2/1 4/7/1 3/6/1 +f 4/8/2 8/16/2 7/14/2 +f 8/16/3 6/11/3 5/9/3 +f 6/12/4 2/2/4 1/1/4 +f 7/15/5 5/10/5 1/1/5 +f 4/8/6 2/3/6 6/13/6 diff --git a/resources/models/cube_non_triangle.mtl b/resources/models/cube_non_triangle.mtl new file mode 100644 index 0000000..fed3761 --- /dev/null +++ b/resources/models/cube_non_triangle.mtl @@ -0,0 +1,12 @@ +# Blender 3.4.1 MTL File: 'None' +# www.blender.org + +newmtl CubeMat1 +Ns 39.367584 +Ka 0.396825 0.396825 0.396825 +Kd 0.800000 0.025358 0.008865 +Ks 0.936508 0.936508 0.936508 +Ke 0.000000 0.000000 0.000000 +Ni 1.450000 +d 1.000000 +illum 3 diff --git a/resources/models/cube_non_triangle.obj b/resources/models/cube_non_triangle.obj new file mode 100644 index 0000000..9e4353e --- /dev/null +++ b/resources/models/cube_non_triangle.obj @@ -0,0 +1,42 @@ +# Blender 3.4.1 +# www.blender.org +mtllib cube_non_triangle.mtl +o Cube +v -1.000000 -1.000000 1.000000 +v -1.000000 1.000000 1.000000 +v -1.000000 -1.000000 -1.000000 +v -1.000000 1.000000 -1.000000 +v 1.000000 -1.000000 1.000000 +v 1.000000 1.000000 1.000000 +v 1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 -1.000000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -0.0000 -1.0000 -0.0000 +vn -0.0000 1.0000 -0.0000 +vt 0.000000 0.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 0.000000 0.000000 +vt 0.000000 1.000000 +vt 1.000000 1.000000 +vt 0.000000 1.000000 +vt 0.000000 0.000000 +vt 1.000000 0.000000 +vt 0.000000 1.000000 +vt 1.000000 1.000000 +vt 1.000000 0.000000 +vt 1.000000 0.000000 +vt 1.000000 1.000000 +vt 1.000000 1.000000 +s 0 +usemtl CubeMat1 +f 1/1/1 2/2/1 4/7/1 3/4/1 +f 3/5/2 4/8/2 8/16/2 7/14/2 +f 7/14/3 8/16/3 6/11/3 5/9/3 +f 5/10/4 6/12/4 2/2/4 1/1/4 +f 3/6/5 7/15/5 5/10/5 1/1/5 +f 8/16/6 4/8/6 2/3/6 6/13/6 diff --git a/src/blt/gfx/model.cpp b/src/blt/gfx/model.cpp index 20576f0..98fb460 100644 --- a/src/blt/gfx/model.cpp +++ b/src/blt/gfx/model.cpp @@ -67,7 +67,11 @@ namespace blt::gfx void vbo_t::destroy() { + // prevent multiple destroys of the same object, in cases of multi attribute binds + if (bufferID_ == 0) + return; glDeleteBuffers(1, &bufferID_); + bufferID_ = 0; } void vbo_t::allocate(GLsizeiptr size, GLint mem_type, const void* data) @@ -125,9 +129,7 @@ namespace blt::gfx bind(); vbo.bind(); - glVertexAttribPointer( - attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset - ); + glVertexAttribPointer(attribute_number, coordinate_size, type, GL_FALSE, stride < 0 ? 0 : stride, (void*) offset); glEnableVertexAttribArray(attribute_number); VBOs[attribute_number] = vbo; diff --git a/src/blt/gfx/state.cpp b/src/blt/gfx/state.cpp new file mode 100644 index 0000000..8fc3413 --- /dev/null +++ b/src/blt/gfx/state.cpp @@ -0,0 +1,28 @@ +/* + * + * Copyright (C) 2023 Brett Terpstra + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +#include +#include "blt/std/assert.h" + +void blt::gfx::matrix_state_manager::update() +{ + pv = perspective * view; + BLT_ASSERT(global_matrix_ubo != nullptr && "You forgot to call create_internals(). Make sure you call delete_internals() as well!"); + global_matrix_ubo->upload((void*)perspective.ptr(), sizeof(blt::mat4x4), 0); + global_matrix_ubo->upload((void*)view.ptr(), sizeof(blt::mat4x4), sizeof(blt::mat4x4)); + global_matrix_ubo->upload((void*)pv.ptr(), sizeof(blt::mat4x4), sizeof(blt::mat4x4)); +} diff --git a/tests/include/shaders/test.vert b/tests/include/shaders/test.vert index 4fffb2d..76b1895 100644 --- a/tests/include/shaders/test.vert +++ b/tests/include/shaders/test.vert @@ -14,7 +14,6 @@ layout (std140) uniform GlobalMatrices { mat4 projection; mat4 view; -// projection view matrix mat4 pvm; };