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

application/016_torus/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 torus_program::init(execution_context& ec, video_context& vc) {
auto& gl = vc.gl_api();
gl.create_program() >> prog;
const auto prog_src{embed(EAGINE_ID(prog), "checker_torus.oglpprog")};
gl.build_program(prog, prog_src.unpack(ec));
gl.use_program(prog);
gl.get_uniform_location(prog, "Camera") >> camera_loc;
}
//------------------------------------------------------------------------------
void torus_program::clean_up(video_context& vc) {
auto& gl = vc.gl_api();
gl.delete_program(std::move(prog));
}
//------------------------------------------------------------------------------
void torus_program::set_projection(video_context& vc, orbiting_camera& camera) {
if(camera.has_changed()) {
auto& gl = vc.gl_api();
gl.set_uniform(prog, camera_loc, camera.matrix(vc.surface_aspect()));
}
}
//------------------------------------------------------------------------------
void torus_program::bind_position_location(
video_context& vc,
auto& gl = vc.gl_api();
gl.bind_attrib_location(prog, loc, "Position");
}
//------------------------------------------------------------------------------
void torus_program::bind_normal_location(
video_context& vc,
auto& gl = vc.gl_api();
gl.bind_attrib_location(prog, loc, "Normal");
}
//------------------------------------------------------------------------------
void torus_program::bind_texcoord_location(
video_context& vc,
auto& gl = vc.gl_api();
gl.bind_attrib_location(prog, loc, "TexCoord");
}
//------------------------------------------------------------------------------
// geometry
//------------------------------------------------------------------------------
void torus_geometry::init(execution_context& ec, video_context& vc) {
const auto& glapi = vc.gl_api();
const auto& gl = glapi;
oglp::shape_generator shape(
glapi,
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());
// texcoords
gl.gen_buffers() >> texcoords;
shape.attrib_setup(
glapi,
vao,
texcoords,
texcoord_loc(),
ec.buffer());
// indices
gl.gen_buffers() >> indices;
shape.index_setup(glapi, indices, draw_var, ec.buffer());
}
//------------------------------------------------------------------------------
void torus_geometry::clean_up(video_context& vc) {
const auto& gl = vc.gl_api();
gl.delete_buffers(std::move(indices));
gl.delete_buffers(std::move(texcoords));
gl.delete_buffers(std::move(normals));
gl.delete_buffers(std::move(positions));
gl.delete_vertex_arrays(std::move(vao));
}
//------------------------------------------------------------------------------
void torus_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
static auto unit_torus(vertex_attrib_bits attr_bits, valid_if_greater_than< int, 4 > rings, valid_if_greater_than< int, 3 > sections, valid_if_ge0_lt1< float > radius_ratio)
Constructs instances of unit_torus_gen.
Definition: torus.hpp:110
#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
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 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
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).