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

application/025_recursive_cube/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 <eagine/embed.hpp>
//------------------------------------------------------------------------------
// program
//------------------------------------------------------------------------------
void cube_program::init(execution_context& ec, video_context& vc) {
const auto& gl = vc.gl_api();
gl.create_program() >> prog;
const auto prog_src{embed(EAGINE_ID(prog), "recursive_cube.oglpprog")};
gl.build_program(prog, prog_src.unpack(ec));
gl.use_program(prog);
gl.get_uniform_location(prog, "Projection") >> projection_loc;
gl.get_uniform_location(prog, "Modelview") >> modelview_loc;
gl.get_uniform_location(prog, "LightPos") >> light_pos_loc;
gl.get_uniform_location(prog, "CubeTex") >> cube_tex_loc;
}
//------------------------------------------------------------------------------
void cube_program::clean_up(video_context& vc) {
const auto& gl = vc.gl_api();
gl.delete_program(std::move(prog));
}
//------------------------------------------------------------------------------
void cube_program::set_texture(
video_context& vc,
vc.gl_api().set_uniform(prog, cube_tex_loc, tex_unit);
}
//------------------------------------------------------------------------------
void cube_program::set_projection(
video_context& vc,
const oglp::tmat<float, 4, 4, true>& proj_mat) {
vc.gl_api().set_uniform(prog, projection_loc, proj_mat);
}
//------------------------------------------------------------------------------
void cube_program::update(execution_context& ec, video_context& vc) {
rad += radians_(0.5F * ec.state().frame_duration().value());
auto& glapi = vc.gl_api();
glapi.set_uniform(
prog,
modelview_loc,
glapi.set_uniform(
prog, light_pos_loc, oglp::vec3(cos(rad) * 4, sin(rad) * 4, 8));
}
//------------------------------------------------------------------------------
void cube_program::bind_position_location(
video_context& vc,
vc.gl_api().bind_attrib_location(prog, loc, "Position");
}
//------------------------------------------------------------------------------
void cube_program::bind_normal_location(
video_context& vc,
vc.gl_api().bind_attrib_location(prog, loc, "Normal");
}
//------------------------------------------------------------------------------
void cube_program::bind_tex_coord_location(
video_context& vc,
vc.gl_api().bind_attrib_location(prog, loc, "TexCoord");
}
//------------------------------------------------------------------------------
// geometry
//------------------------------------------------------------------------------
void cube_geometry::init(execution_context& ec, video_context& vc) {
const auto& glapi = vc.gl_api();
const auto& 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.bind_vertex_array(vao);
// positions
gl.gen_buffers() >> positions;
shape.attrib_setup(
glapi,
vao,
positions,
position_loc(),
ec.buffer());
// normals
gl.gen_buffers() >> normals;
shape.attrib_setup(
glapi,
vao,
normals,
normal_loc(),
ec.buffer());
// tex_coords
gl.gen_buffers() >> tex_coords;
shape.attrib_setup(
glapi,
vao,
tex_coords,
tex_coord_loc(),
ec.buffer());
// indices
gl.gen_buffers() >> indices;
shape.index_setup(glapi, indices, ec.buffer());
}
//------------------------------------------------------------------------------
void cube_geometry::clean_up(video_context& vc) {
const auto& gl = vc.gl_api();
gl.delete_buffers(std::move(indices));
gl.delete_buffers(std::move(tex_coords));
gl.delete_buffers(std::move(normals));
gl.delete_buffers(std::move(positions));
gl.delete_vertex_arrays(std::move(vao));
}
//------------------------------------------------------------------------------
void cube_geometry::draw(video_context& vc) {
draw_using_instructions(vc.gl_api(), view(ops));
}
//------------------------------------------------------------------------------
void cube_draw_buffers::init(execution_context&, video_context& vc) {
const auto& glapi = vc.gl_api();
const auto& [gl, GL] = glapi;
for(int i = 0; i < 2; ++i) {
auto& obj = objs.front();
obj.tex_unit = i;
gl.gen_textures() >> obj.tex;
gl.active_texture(GL.texture0 + i);
gl.bind_texture(GL.texture_2d, obj.tex);
gl.tex_parameter_i(GL.texture_2d, GL.texture_min_filter, GL.nearest);
gl.tex_parameter_i(GL.texture_2d, GL.texture_mag_filter, GL.nearest);
gl.tex_image2d(
GL.texture_2d,
0,
GL.rgb,
tex_side,
tex_side,
0,
GL.rgb,
GL.unsigned_byte_,
gl.gen_renderbuffers() >> obj.rbo;
gl.bind_renderbuffer(GL.renderbuffer, obj.rbo);
gl.renderbuffer_storage(
GL.renderbuffer, GL.depth_component, tex_side, tex_side);
gl.gen_framebuffers() >> obj.fbo;
gl.bind_framebuffer(GL.draw_framebuffer, obj.fbo);
gl.framebuffer_texture2d(
GL.draw_framebuffer, GL.color_attachment0, GL.texture_2d, obj.tex, 0);
gl.framebuffer_renderbuffer(
GL.draw_framebuffer, GL.depth_attachment, GL.renderbuffer, obj.rbo);
gl.viewport(tex_side, tex_side);
gl.clear(GL.color_buffer_bit);
objs.swap();
}
gl.bind_framebuffer(GL.draw_framebuffer, oglp::framebuffer_name(0));
gl.bind_renderbuffer(GL.renderbuffer, oglp::renderbuffer_name(0));
}
//------------------------------------------------------------------------------
void cube_draw_buffers::clean_up(video_context& vc) {
const auto& gl = vc.gl_api();
for(int i = 0; i < 2; ++i) {
auto& obj = objs.front();
gl.delete_renderbuffers(std::move(obj.rbo));
gl.delete_textures(std::move(obj.tex));
gl.delete_framebuffers(std::move(obj.fbo));
objs.swap();
}
}
//------------------------------------------------------------------------------
} // namespace eagine::application
Application harness / wrapper code is placed in this namespace.
Definition: eagine.hpp:72
#define EAGINE_ID(NAME)
Macro for constructing instances of eagine::identifier.
Definition: identifier.hpp:353
gl_object_name< renderbuffer_tag > renderbuffer_name
Alias for GL renderbuffer object handle.
Definition: object_name.hpp:138
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
GLint int_type
Signed integer type.
Definition: config.hpp:70
basic_block< true > const_block
Alias for const byte memory span.
Definition: block.hpp:32
convertible_matrix_constructor< rotation_x< matrix< T, 4, 4, true, V > >> matrix_rotation_x
Alias for constructor of rotation along x-axis transformation matrix.
Definition: matrix_rotation.hpp:124
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
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 radians_(T value) noexcept -> radians_t< T >
Creates a tagged quantity storing value in radians.
Definition: quantities.hpp:50
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
convertible_matrix_constructor< rotation_z< matrix< T, 4, 4, true, V > >> matrix_rotation_z
Alias for constructor of rotation along z-axis transformation matrix.
Definition: matrix_rotation.hpp:152
@ face_coord
Generic face coordinate.
tvec< gl_types::float_type, 3 > vec3
Alias for a 3D vector type.
Definition: vector.hpp:32
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.
convertible_matrix_constructor< rotation_y< matrix< T, 4, 4, true, V > >> matrix_rotation_y
Alias for constructor of rotation along y-axis transformation matrix.
Definition: matrix_rotation.hpp:138
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
@ normal
Vertex normal vector.

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