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

application/000_stencil_shadow/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>
//------------------------------------------------------------------------------
// surface program
//------------------------------------------------------------------------------
void surface_program::init(execution_context& ec, video_context& vc) {
auto& gl = vc.gl_api();
gl.create_program() >> _prog;
const auto prog_src{
embed(EAGINE_ID(SurfProg), "stencil_shadow_surface.oglpprog")};
gl.build_program(_prog, prog_src.unpack(ec));
gl.use_program(_prog);
gl.get_uniform_location(_prog, "Model") >> _model_loc;
gl.get_uniform_location(_prog, "View") >> _view_loc;
gl.get_uniform_location(_prog, "Projection") >> _projection_loc;
}
//------------------------------------------------------------------------------
void surface_program::clean_up(video_context& vc) {
auto& gl = vc.gl_api();
gl.delete_program(std::move(_prog));
}
//------------------------------------------------------------------------------
void surface_program::prepare_frame(
video_context& vc,
orbiting_camera& camera,
float t) {
auto& gl = vc.gl_api();
gl.use_program(_prog);
gl.set_uniform(
_prog, _model_loc, oglp::matrix_rotation_x(right_angles_(t))());
gl.set_uniform(_prog, _view_loc, camera.transform_matrix());
gl.set_uniform(
_prog, _projection_loc, camera.perspective_matrix(vc.surface_aspect()));
}
//------------------------------------------------------------------------------
void surface_program::bind_position_location(
video_context& vc,
auto& gl = vc.gl_api();
gl.bind_attrib_location(_prog, loc, "Position");
}
//------------------------------------------------------------------------------
void surface_program::bind_normal_location(
video_context& vc,
auto& gl = vc.gl_api();
gl.bind_attrib_location(_prog, loc, "Normal");
}
//------------------------------------------------------------------------------
// halo program
//------------------------------------------------------------------------------
void halo_program::init(execution_context& ec, video_context& vc) {
auto& gl = vc.gl_api();
gl.create_program() >> _prog;
const auto prog_src{
embed(EAGINE_ID(HaloProg), "stencil_shadow_halo.oglpprog")};
gl.build_program(_prog, prog_src.unpack(ec));
gl.use_program(_prog);
gl.get_uniform_location(_prog, "Model") >> _model_loc;
gl.get_uniform_location(_prog, "View") >> _view_loc;
gl.get_uniform_location(_prog, "Projection") >> _projection_loc;
gl.get_uniform_location(_prog, "CameraPos") >> _camera_pos_loc;
}
//------------------------------------------------------------------------------
void halo_program::clean_up(video_context& vc) {
auto& gl = vc.gl_api();
gl.delete_program(std::move(_prog));
}
//------------------------------------------------------------------------------
void halo_program::prepare_frame(
video_context& vc,
orbiting_camera& camera,
float t) {
auto& gl = vc.gl_api();
gl.use_program(_prog);
gl.set_uniform(
_prog, _model_loc, oglp::matrix_rotation_x(right_angles_(t))());
gl.set_uniform(_prog, _view_loc, camera.transform_matrix());
gl.set_uniform(
_prog, _projection_loc, camera.perspective_matrix(vc.surface_aspect()));
gl.set_uniform(_prog, _camera_pos_loc, camera.position());
}
//------------------------------------------------------------------------------
void halo_program::bind_position_location(
video_context& vc,
auto& gl = vc.gl_api();
gl.bind_attrib_location(_prog, loc, "Position");
}
//------------------------------------------------------------------------------
void halo_program::bind_normal_location(
video_context& vc,
auto& gl = vc.gl_api();
gl.bind_attrib_location(_prog, loc, "Normal");
}
//------------------------------------------------------------------------------
// geometry
//------------------------------------------------------------------------------
void shape_geometry::init(execution_context& ec, video_context& vc) {
const auto& glapi = vc.gl_api();
const auto& gl = glapi;
oglp::shape_generator shape(glapi, _gen);
auto draw_var = shape.draw_variant(0);
_ops.resize(std_size(shape.operation_count(draw_var)));
shape.instructions(glapi, draw_var, 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());
// indices
gl.gen_buffers() >> _indices;
shape.index_setup(glapi, _indices, draw_var, ec.buffer());
}
//------------------------------------------------------------------------------
void shape_geometry::clean_up(video_context& vc) {
const auto& gl = vc.gl_api();
gl.delete_buffers(std::move(_indices));
gl.delete_buffers(std::move(_normals));
gl.delete_buffers(std::move(_positions));
gl.delete_vertex_arrays(std::move(_vao));
}
//------------------------------------------------------------------------------
void shape_geometry::draw(execution_context&, video_context& ec) {
draw_using_instructions(ec.gl_api(), view(_ops));
}
//------------------------------------------------------------------------------
} // 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
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
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 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 constexpr auto right_angles_(T value) noexcept
Creates a tagged quantity in units of right angle.
Definition: quantities.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
@ 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).