1#include <cgv/base/base.h>
2#include "render_info.h"
3#include <cgv_gl/gl/gl.h>
4#include <cgv_gl/gl/gl_tools.h>
5#include <cgv_gl/gl/gl_context.h>
10void attribute_array::add_attribute(type_descriptor element_type, uint32_t vbo_index,
11 size_t byte_offset,
size_t element_count, uint32_t stride,
12 VertexAttributeID vertex_attribute_id, std::string name)
15 va.byte_offset = byte_offset;
16 va.element_count = element_count;
17 va.element_type = element_type;
19 va.vbo_index = vbo_index;
20 va.vertex_attribute_id = vertex_attribute_id;
21 if (vertex_attribute_id == cgv::render::VA_BY_NAME)
31 if (prev_dc && prev_dc->prog == dc.prog)
33 bool prog_enabled =
false;
38 if ((dc.alpha_mode & AM_MASK) != 0) {
40 dc.prog->
set_uniform(ctx,
"alpha_cutoff", dc.alpha_cutoff);
43 GLenum blend_src, blend_dst;
44 if ((dc.alpha_mode & AM_BLEND) != 0) {
45 blend = glIsEnabled(GL_BLEND);
46 glGetIntegerv(GL_BLEND_DST,
reinterpret_cast<GLint*
>(&blend_dst));
47 glGetIntegerv(GL_BLEND_SRC,
reinterpret_cast<GLint*
>(&blend_src));
49 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
53 bool new_material =
true;
54 if (prev_dc && prev_dc->material_index == dc.material_index)
56 if (new_prog || (new_material && dc.material_index >= 0 && dc.material_index <
materials.size())) {
57 if (dc.material_index != -1 && use_materials)
58 ctx.enable_material(*
materials[dc.material_index]);
67 if (prev_dc && prev_dc->aa_index == dc.aa_index)
69 if (new_aab && dc.aa_index >= 0 && dc.aa_index <
aas.size())
70 aas[dc.aa_index].aab_ptr->enable(ctx);
73 GLuint pt = gl::map_to_gl(dc.primitive_type);
74 switch (dc.draw_call_type) {
76 glDrawArrays(pt, dc.vertex_offset, dc.count);
79 glDrawElements(pt, dc.count, gl::map_to_gl(dc.index_type), dc.indices);
81 case RCT_ARRAYS_INSTANCED:
82 glDrawArraysInstanced(pt, dc.vertex_offset, dc.count, dc.instance_count);
84 case RCT_INDEXED_INSTANCED:
85 glDrawElementsInstanced(pt, dc.count, gl::map_to_gl(dc.index_type), dc.indices, dc.instance_count);
90 if (!next_dc || next_dc->aa_index != dc.aa_index)
91 aas[dc.aa_index].aab_ptr->disable(ctx);
94 if (dc.material_index != -1 && (!next_dc || next_dc->material_index != dc.material_index))
95 ctx.disable_material(*
materials[dc.material_index]);
98 if ((!next_dc || next_dc->prog != dc.prog) && prog_enabled)
101 if ((dc.alpha_mode & AM_BLEND) != 0) {
104 glBlendFunc(blend_src, blend_dst);
106 if ((dc.alpha_mode & AM_MASK) != 0)
177 int begin = std::max(0, aa_index);
178 int end = aa_index == -1 ? (int)
ref_aas().size() : aa_index + 1;
181 std::vector<int> locs;
182 for (ai = begin; ai < end; ++ai) {
186 for (
const auto& va : aa.vas) {
188 switch (va.vertex_attribute_id) {
189 case VA_POSITION: loc = prog.get_position_index();
break;
190 case VA_NORMAL: loc = prog.get_normal_index();
break;
191 case VA_TEXCOORD: loc = prog.get_texcoord_index();
break;
192 case VA_COLOR: loc = prog.get_color_index();
break;
204 for (ai = begin; ai < end; ++ai) {
207 for (
int i=0; i<16; ++i)
210 for (
const auto& va : aa.vas) {
211 if (locs[li] == -1) {
216 *
ref_vbos()[va.vbo_index], va.byte_offset,
217 va.element_count, va.stride);
223 if (aa_index == -1 || dc.aa_index == aa_index)
233 std::vector<size_t> dcis;
249 for (
size_t i = 0; i < dcis.size(); ++i) {
275 for (
auto& aa :
aas) {
276 aa.aab_ptr->destruct(ctx);
283 for (
auto& v :
vbos) {
bool disable_array(const context &ctx, int loc)
disable array for attribute at location loc
bool set_attribute_array(const context &ctx, int loc, const T &array)
set vertex attribute location to given array and enable array
base class for all drawables, which is independent of the used rendering API.
shader_program_base * get_current_program() const
check for current program, prepare it for rendering and return pointer to it
const cgv::media::illum::surface_material * get_current_material() const
return pointer to current material or nullptr if no current material is available
virtual void set_material(const cgv::media::illum::surface_material &mat)
set the current material
std::vector< texture * > textures
store textures
std::vector< vertex_buffer * > & ref_vbos()
give write access to vbos
std::vector< textured_material * > materials
store materials
std::vector< texture * > & ref_textures()
give write access to texture
const std::vector< vertex_buffer * > & get_vbos() const
give read access to vbos
virtual bool bind(context &ctx, shader_program &prog, bool force_success, int aa_index=-1)
bind all or specific aa to the passed shader program
std::vector< textured_material * > & ref_materials()
give write access to materials
std::vector< draw_call > & ref_draw_calls()
give write access to draw calls
std::vector< attribute_array > & ref_aas()
give write access to aabs
std::vector< vertex_buffer * > vbos
store buffers
std::vector< attribute_array > aas
store attribute bindings
const std::vector< textured_material * > & get_materials() const
give read access to materials
const std::vector< draw_call > & get_draw_calls() const
give read access to draw calls
render_info()
set vbo and vbe types
void destruct(cgv::render::context &ctx)
destruct render mesh info and free vertex buffer objects
std::vector< draw_call > draw_calls
store vector of render calls
const std::vector< attribute_array > & get_aas() const
give read access to aabs
void draw_all(context &ctx, bool skip_opaque=false, bool skip_blended=false, bool use_materials=true)
execute all draw calls
void draw(context &ctx, const draw_call &dc, const draw_call *prev_dc=0, const draw_call *next_dc=0, bool use_materials=true)
perform a single render call
const std::vector< texture * > & get_textures() const
give read access to texture
a shader program combines several shader code fragments to a complete definition of the shading pipel...
bool enable(context &ctx)
enable the shader program
bool disable(context &ctx)
disable shader program and restore fixed functionality
bool set_uniform(const context &ctx, const std::string &name, const T &value, bool generate_error=false)
Set the value of a uniform by name, where the type can be any of int, unsigned, float,...
int get_attribute_location(const context &ctx, const std::string &name) const
query location index of an attribute