need to think about resource management

This commit is contained in:
Hartmut Seichter 2019-02-20 21:52:41 +01:00
parent d2de6d410f
commit dd908ead95
11 changed files with 180 additions and 23 deletions

View file

@ -56,7 +56,7 @@ struct triangle_renderer
#if 0
mesh::vertex3array_t vertices = {
{ 0.0f, 0.5f, z_val} // 0
{ 0.0f, 0.5f, z_val} // 0
,{ 0.5f, 0.0f, z_val} // 1
,{-0.5f, 0.0f, z_val} // 2
};
@ -74,7 +74,7 @@ struct triangle_renderer
// geometry
mesh::vertex3array_t vertices = {
{-s, s, z_val} // 0
{-s, s, z_val} // 0
,{ s, s, z_val} // 1
,{-s, -s, z_val} // 2
,{ s, -s, z_val} // 3
@ -90,6 +90,8 @@ struct triangle_renderer
amesh.set_indices(indices);
amesh.set_vertices(vertices);
amesh.compute_normals();
amesh_renderer.create(amesh);
const char* vertex_shader_2 = R"(
@ -101,8 +103,8 @@ struct triangle_renderer
in vec3 vertex_p;
void main() {
gl_Position = projection * view * model * vec4(vertex_p, 1.0);
// gl_Position = model * vec4(vertex_p, 1.0);
gl_Position = projection * view * model * vec4(vertex_p, 1.0);
// gl_Position = model * vec4(vertex_p, 1.0);
}
)";
@ -111,7 +113,7 @@ struct triangle_renderer
uniform vec4 input_color = vec4(1.0, 0.0, 0.0, 1.0);
out vec4 frag_colour;
void main() {
frag_colour = input_color;
frag_colour = input_color;
})";
shader_p.set_source(vertex_shader_2,shader::code_type::vertex);
@ -138,8 +140,8 @@ struct triangle_renderer
model_mat = rot.to_matrix();
matrix4x4f view_mat = matrix_transform<float>::look_at(vector3({0,0,0}),
vector3::forward(),
vector3::up());
vector3::forward(),
vector3::up());
// materials should carry this
#if 1
glEnable(GL_CULL_FACE);
@ -148,15 +150,15 @@ struct triangle_renderer
#endif
#if 0
#if 1
auto proj_mat = matrix_transform<float>::orthographic_projection(1.3,1.0,
0.2f,100.f);
#else
auto proj_mat = matrix_transform<float>::perspective_projection(deg_to_rad(60.f),
1.3f,
0.2f,1000.f);
1.3f,
0.2f,1000.f);
#endif
// highly inefficient - should be cached -
@ -165,11 +167,17 @@ struct triangle_renderer
shader_p.set("view",view_mat);
shader_p.set("projection",proj_mat);
#if 0
// new version with ...
shader::uniform_set us;
us["input_color"] = col;
us["model"] = model_mat;
us["view"] = view_mat;
us["projection"] = proj_mat;
// shader_p.set_uniforms(us);
shader_p.set_uniforms(us);
#endif
amesh_renderer.draw();
@ -274,10 +282,10 @@ void pipeline::impl::draw()
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, _fbo_msaa);
// glClearColor(0,0,0,1);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glClearColor(0,0,0,1);
// glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// glViewport(0,0,800,600);
// glViewport(0,0,800,600);
//
// draw pass

View file

@ -220,6 +220,7 @@ void shader::set_uniforms(shader::uniform_set s)
std::visit([u](auto&& arg) {
using T = std::decay_t<decltype(arg)>;
if constexpr (std::is_same_v<T, vector4f>)
// set(arg);
std::cout << "vec4f with type " << typeid(arg).name() << " " << serialize::matrix(arg) << '\n';
else
std::cout << "can't" << std::endl;

View file

@ -13,7 +13,7 @@ struct vertex_array::impl {
GLuint _vao = 0;
std::vector<GLuint> _vbos;
std::size_t _mesh_elements = {0};
GLint _mesh_elements = {0};
impl()
{
@ -37,6 +37,8 @@ struct vertex_array::impl {
}
glGenVertexArrays(1,&_vao);
glBindVertexArray(_vao);
size_t arrays_needed = 0;
@ -56,7 +58,9 @@ struct vertex_array::impl {
// vertices
glBindBuffer(GL_ARRAY_BUFFER, _vbos[0]);
glBufferData(GL_ARRAY_BUFFER, sizeof(m.vertices().front()) * m.vertices().size(), m.vertices().data(),
glBufferData(GL_ARRAY_BUFFER,
sizeof(m.vertices().front()) * m.vertices().size(),
m.vertices().data(),
GL_STATIC_DRAW);
glEnableVertexAttribArray(0);
@ -86,6 +90,8 @@ struct vertex_array::impl {
{
glBindVertexArray(_vao);
glDrawElements(GL_TRIANGLES, _mesh_elements, GL_UNSIGNED_INT, nullptr);
glBindVertexArray(0);
}
// GLint get_mode(vertex_array::)