need to finish uniform buffer functions

main
Brett 2023-12-29 03:37:43 -05:00
parent f9766d1a84
commit febfe200a9
11 changed files with 337 additions and 5 deletions

76
include/blt/gfx/state.h Normal file
View File

@ -0,0 +1,76 @@
/*
* <Short Description>
* 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 <https://www.gnu.org/licenses/>.
*/
#ifndef BLT_WITH_GRAPHICS_STATE_H
#define BLT_WITH_GRAPHICS_STATE_H
#include <blt/gfx/shader.h>
#include <blt/math/vectors.h>
#include <blt/math/matrix.h>
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<typename U>
void setView(U&& mat)
{
view = std::forward(mat);
}
template<typename U>
void setPerspective(U&& mat)
{
perspective = std::forward(mat);
}
void update();
};
}
#endif //BLT_WITH_GRAPHICS_STATE_H

@ -1 +1 @@
Subproject commit 5ae17f4a86a021fb914f0c17b116dc511e5f3f93
Subproject commit cc64e71c8d4ef550bad1eed5aaee3bd45fef9cad

View File

@ -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

View File

@ -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

12
resources/models/cube.mtl Normal file
View File

@ -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

48
resources/models/cube.obj Normal file
View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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;

28
src/blt/gfx/state.cpp Normal file
View File

@ -0,0 +1,28 @@
/*
* <Short Description>
* 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 <https://www.gnu.org/licenses/>.
*/
#include <blt/gfx/state.h>
#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));
}

View File

@ -14,7 +14,6 @@ layout (std140) uniform GlobalMatrices
{
mat4 projection;
mat4 view;
// projection view matrix
mat4 pvm;
};