OGLplus  (0.59.0) a C++ wrapper for rendering APIs

application/024_overdraw/resources.cpp

Copyright Matus Chochlik. Distributed under the Boost Software License, Version 1.0. See accompanying file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt

#include "resources.hpp"
#include "main.hpp"
#include <eagine/embed.hpp>
//------------------------------------------------------------------------------
// draw program
//------------------------------------------------------------------------------
void draw_program::init(example& e) {
auto& gl = e.video().gl_api();
gl.create_program() >> _prog;
gl.delete_program.later_by(e.cleanup(), _prog);
const auto prog_src{embed(EAGINE_ID(DrawProg), "overdraw_draw.oglpprog")};
gl.build_program(_prog, prog_src.unpack(e.ctx()));
gl.use_program(_prog);
gl.get_uniform_location(_prog, "Camera") >> _camera_loc;
}
//------------------------------------------------------------------------------
void draw_program::set_projection(example& e) {
e.video().gl_api().set_uniform(
_prog, _camera_loc, e.camera().matrix(e.video()));
}
//------------------------------------------------------------------------------
void draw_program::bind_position_location(
example& e,
e.video().gl_api().bind_attrib_location(_prog, loc, "Position");
}
//------------------------------------------------------------------------------
void draw_program::use(example& e) {
e.video().gl_api().use_program(_prog);
}
//------------------------------------------------------------------------------
// screen program
//------------------------------------------------------------------------------
void screen_program::init(example& e) {
auto& gl = e.video().gl_api();
gl.create_program() >> _prog;
gl.delete_program.later_by(e.cleanup(), _prog);
const auto prog_src{
embed(EAGINE_ID(ScreenProg), "overdraw_screen.oglpprog")};
gl.build_program(_prog, prog_src.unpack(e.ctx()));
gl.use_program(_prog);
gl.get_uniform_location(_prog, "ScreenSize") >> _screen_size_loc;
gl.get_uniform_location(_prog, "DrawTex") >> _draw_tex_loc;
gl.set_uniform(_prog, _draw_tex_loc, 0);
}
//------------------------------------------------------------------------------
void screen_program::bind_position_location(
example& e,
e.video().gl_api().bind_attrib_location(_prog, loc, "Position");
}
//------------------------------------------------------------------------------
void screen_program::bind_tex_coord_location(
example& e,
e.video().gl_api().bind_attrib_location(_prog, loc, "TexCoord");
}
//------------------------------------------------------------------------------
void screen_program::set_screen_size(example& e) {
const auto [w, h] = e.video().surface_size();
e.video().gl_api().set_uniform(_prog, _screen_size_loc, oglp::vec2(w, h));
}
//------------------------------------------------------------------------------
void screen_program::use(example& e) {
e.video().gl_api().use_program(_prog);
}
//------------------------------------------------------------------------------
// shape geometry
//------------------------------------------------------------------------------
void shape_geometry::init(example& e) {
const auto& glapi = e.video().gl_api();
const auto& [gl, GL] = glapi;
oglp::shape_generator shape(
_ops.resize(std_size(shape.operation_count()));
shape.instructions(glapi, cover(_ops));
// vao
gl.gen_vertex_arrays() >> _vao;
gl.delete_vertex_arrays.later_by(e.cleanup(), _vao);
gl.bind_vertex_array(_vao);
// positions
gl.gen_buffers() >> _positions;
gl.delete_buffers.later_by(e.cleanup(), _positions);
shape.attrib_setup(
glapi,
_vao,
_positions,
position_loc(),
e.ctx().buffer());
// indices
gl.gen_buffers() >> _indices;
gl.delete_buffers.later_by(e.cleanup(), _indices);
shape.index_setup(glapi, _indices, e.ctx().buffer());
// offsets
const float d = 1.414F;
const float h = float(count - 1) * 0.5F;
std::vector<oglp::gl_types::float_type> offset_data;
offset_data.resize(std_size(count * count * count * 4));
auto p = offset_data.begin();
for(int k = 0; k != count; ++k) {
const float z = (float(k) - h) * d;
for(int j = 0; j != count; ++j) {
const float y = (float(j) - h) * d;
for(int i = 0; i != count; ++i) {
const float x = (float(i) - h) * d;
*p++ = x;
*p++ = y;
*p++ = z;
*p++ = 0;
}
}
}
gl.gen_buffers() >> _offsets;
gl.delete_buffers.later_by(e.cleanup(), _offsets);
gl.bind_buffer(GL.uniform_buffer, _offsets);
gl.bind_buffer_base(GL.uniform_buffer, 0, _offsets);
gl.buffer_data(GL.uniform_buffer, view(offset_data), GL.static_draw);
}
//------------------------------------------------------------------------------
void shape_geometry::draw(example& e) {
const auto& glapi = e.video().gl_api();
const auto& gl = glapi;
gl.bind_vertex_array(_vao);
draw_instanced_using_instructions(gl, view(_ops), count * count * count);
}
//------------------------------------------------------------------------------
// screen geometry
//------------------------------------------------------------------------------
void screen_geometry::init(example& e) {
const auto& glapi = e.video().gl_api();
const auto& [gl, GL] = glapi;
oglp::shape_generator shape(
glapi,
_ops.resize(std_size(shape.operation_count()));
shape.instructions(glapi, cover(_ops));
// vao
gl.gen_vertex_arrays() >> _vao;
gl.delete_vertex_arrays.later_by(e.cleanup(), _vao);
gl.bind_vertex_array(_vao);
// positions
gl.gen_buffers() >> _positions;
gl.delete_buffers.later_by(e.cleanup(), _positions);
shape.attrib_setup(
glapi,
_vao,
_positions,
position_loc(),
e.ctx().buffer());
// coords
gl.gen_buffers() >> _tex_coords;
gl.delete_buffers.later_by(e.cleanup(), _tex_coords);
shape.attrib_setup(
glapi,
_vao,
_tex_coords,
tex_coord_loc(),
e.ctx().buffer());
}
//------------------------------------------------------------------------------
void screen_geometry::draw(example& e) {
const auto& glapi = e.video().gl_api();
const auto& gl = glapi;
gl.bind_vertex_array(_vao);
}
//------------------------------------------------------------------------------
// draw buffers
//------------------------------------------------------------------------------
void draw_buffers::init(example& e) {
const auto& [gl, GL] = e.video().gl_api();
const auto [width, height] = e.video().surface_size();
_width = width;
_height = height;
gl.gen_textures() >> _tex;
gl.delete_textures.later_by(e.cleanup(), _tex);
gl.active_texture(GL.texture0);
gl.bind_texture(GL.texture_rectangle, _tex);
gl.tex_parameter_i(GL.texture_rectangle, GL.texture_min_filter, GL.nearest);
gl.tex_parameter_i(GL.texture_rectangle, GL.texture_mag_filter, GL.nearest);
gl.tex_image2d(
GL.texture_rectangle,
0,
GL.rg8,
_width,
_height,
0,
GL.rg,
GL.unsigned_byte_,
gl.gen_renderbuffers() >> _rbo;
gl.delete_renderbuffers.later_by(e.cleanup(), _rbo);
gl.bind_renderbuffer(GL.renderbuffer, _rbo);
gl.renderbuffer_storage(
GL.renderbuffer, GL.depth_component, _width, _height);
gl.gen_framebuffers() >> _fbo;
gl.bind_framebuffer(GL.draw_framebuffer, _fbo);
gl.framebuffer_texture2d(
GL.draw_framebuffer, GL.color_attachment0, GL.texture_rectangle, _tex, 0);
gl.framebuffer_renderbuffer(
GL.draw_framebuffer, GL.depth_attachment, GL.renderbuffer, _rbo);
gl.bind_framebuffer(GL.draw_framebuffer, oglp::framebuffer_name(0));
}
//------------------------------------------------------------------------------
void draw_buffers::resize(example& e) {
const auto& [gl, GL] = e.video().gl_api();
const auto [width, height] = e.video().surface_size();
if(_width != width || _height != height) {
_width = width;
_height = height;
gl.tex_image2d(
GL.texture_rectangle,
0,
GL.rg8,
_width,
_height,
0,
GL.rg,
GL.unsigned_byte_,
gl.renderbuffer_storage(
GL.renderbuffer, GL.depth_component, _width, _height);
}
}
//------------------------------------------------------------------------------
void draw_buffers::draw_offscreen(example& e) {
const auto& [gl, GL] = e.video().gl_api();
gl.bind_framebuffer(GL.draw_framebuffer, _fbo);
}
//------------------------------------------------------------------------------
void draw_buffers::draw_onscreen(example& e) {
const auto& [gl, GL] = e.video().gl_api();
gl.bind_framebuffer(GL.draw_framebuffer, oglp::framebuffer_name(0));
}
//------------------------------------------------------------------------------
} // namespace eagine::application
Application harness / wrapper code is placed in this namespace.
Definition: eagine.hpp:72
tvec< gl_types::float_type, 2 > vec2
Alias for a 2D vector type.
Definition: vector.hpp:28
#define EAGINE_ID(NAME)
Macro for constructing instances of eagine::identifier.
Definition: identifier.hpp:353
static constexpr auto cover(T *addr, S size) noexcept -> span_if_mutable< T >
Creates a span starting at the specified pointer and specified length.
Definition: span.hpp:465
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
static constexpr auto view(T *addr, S size) noexcept -> const_span< T >
Creates a view starting at the specified pointer and specified length.
Definition: span.hpp:458
@ wrap_coord
UV-texture wrapping coordinate.
static auto unit_cube(vertex_attrib_bits attr_bits)
Constructs instances of unit_cube_gen.
Definition: cube.hpp:92
gl_object_name< framebuffer_tag > framebuffer_name
Alias for GL framebuffer object handle.
Definition: object_name.hpp:118
static constexpr auto std_size(T v) noexcept
Converts argument to std size type.
Definition: types.hpp:52
prog_var_location< EAGINE_ID_V(VertexAttr)> vertex_attrib_location
Alias for program vertex attribute location wrapper.
Definition: prog_var_loc.hpp:104
static auto unit_screen(vertex_attrib_bits attr_bits)
Constructs instances of unit_screen_gen.
Definition: screen.hpp:61
void draw_using_instructions(const basic_gl_api< A > &api, span< const shape_draw_operation > ops) noexcept
Takes a sequence of draw operations from a shape generator and draws them.
static auto embed(identifier res_id, string_view src_path) noexcept -> embedded_resource
Triggers the embedding of data from a file on the specified path.
Definition: embed.hpp:105
void draw_instanced_using_instructions(const basic_gl_api< A > &api, span< const shape_draw_operation > ops, gl_types::sizei_type inst_count) noexcept
Takes a sequence of draw operations from a shape generator and draws them.

Copyright © 2015-2021 Matúš Chochlík.
<chochlik -at -gmail.com>
Documentation generated on Tue Apr 13 2021 by Doxygen (version 1.8.17).