cgv
Loading...
Searching...
No Matches
gl_context.cxx
1#include <cgv/base/group.h>
2#include "gl_context.h"
3#include "gl_tools.h"
4#include <cgv_gl/gl/wgl.h>
5#ifdef _WIN32
6#undef TA_LEFT
7#undef TA_TOP
8#undef TA_RIGHT
9#undef TA_BOTTOM
10#endif
11#include <cgv/base/base.h>
12#include <cgv/base/action.h>
13#include <cgv/render/drawable.h>
14#include <cgv/render/shader_program.h>
15#include <cgv/render/attribute_array_binding.h>
16#include <cgv/render/vertex_buffer.h>
17#include <cgv/render/textured_material.h>
18#include <cgv/utils/scan.h>
19#include <cgv/media/image/image_writer.h>
20#include <cgv/gui/event_handler.h>
21#include <cgv/math/ftransform.h>
22#include <cgv/math/geom.h>
23#include <cgv/math/inv.h>
24#include <cgv/type/standard_types.h>
25#include <cgv/os/clipboard.h>
26
27using namespace cgv::base;
28using namespace cgv::type;
29using namespace cgv::type::info;
30using namespace cgv::gui;
31using namespace cgv::os;
32using namespace cgv::math;
33using namespace cgv::media::font;
34using namespace cgv::media::illum;
35
36namespace cgv {
37 namespace render {
38 namespace gl {
39
41{
42#ifdef _WIN32
51#else
60#endif
61 std::vector<int> attrib_list;
62 int version = 0;
63 if (cc.version_major < 1)
64 version = 46;
65 else
66 version = 10 * cc.version_major + (cc.version_minor >= 0 ? cc.version_minor : 0);
67 if ((version > 30 && cc.forward_compatible) || cc.debug) {
68 attrib_list.push_back(CONTEXT_FLAGS);
69 attrib_list.push_back(
70 (cc.forward_compatible ? CONTEXT_FORWARD_COMPATIBLE_BIT : 0) +
71 (cc.debug ? CONTEXT_DEBUG_BIT : 0));
72 }
73 if (cc.version_major > 0) {
75 attrib_list.push_back(cc.version_major);
76 }
77 if (cc.version_minor >= 0) {
79 attrib_list.push_back(cc.version_minor);
80 }
81 if (version > 31) {
83 attrib_list.push_back(
85 }
86 attrib_list.push_back(0);
87 return attrib_list;
88}
89
90GLenum map_to_gl(PrimitiveType primitive_type)
91{
92 static const GLenum gl_primitive_type[] = {
93 GLenum(-1),
105 GL_QUADS,
109 };
110 return gl_primitive_type[primitive_type];
111}
112
114{
115 static const GLenum gl_material_side[] = {
116 GLenum(0),
117 GL_FRONT,
118 GL_BACK,
120 };
122}
123
125{
126 static const GLenum gl_access_type[] = {
130 };
132}
133
135{
136 static const GLenum gl_blend_func[] = {
137 GL_ZERO,
138 GL_ONE,
156 };
158}
159
161{
162 static const GLenum gl_compare_func[] = {
163 GL_LEQUAL,
164 GL_GEQUAL,
165 GL_LESS,
167 GL_EQUAL,
169 GL_ALWAYS,
171 };
173}
174
175static const GLenum gl_depth_format_ids[] =
176{
181};
182
183static const GLenum gl_color_buffer_format_ids[] =
184{
185 GL_RGB,
186 GL_RGBA
187};
188
189static const char* depth_formats[] =
190{
191 "[D]",
192 "uint16[D]",
193 "uint32[D:24]",
194 "uint32[D]",
195 0
196};
197
198static const char* color_buffer_formats[] =
199{
200 "[R,G,B]",
201 "[R,G,B,A]",
202 0
203};
204
206{
207 static const GLenum gl_texture_type[] = {
208 GLenum(0),
217 };
219}
220
221GLenum get_tex_bind(TextureType texture_type)
222{
223 static const GLenum gl_tex_binding[] = {
224 GLenum(0),
234 };
236}
237
239{
240 static const GLenum gl_texture_wrap[] = {
241 GL_REPEAT,
242 GL_CLAMP,
243 GL_CLAMP_TO_EDGE,
249 };
251}
252
254{
255 static const GLenum gl_texture_filter[] = {
257 GL_LINEAR,
263 };
265}
266
267GLboolean map_to_gl(bool flag) {
268 return flag ? GL_TRUE : GL_FALSE;
269}
270
271GLuint get_gl_id(const void* handle)
272{
273 return (const GLuint&)handle - 1;
274}
275
276void* get_handle(GLuint id)
277{
278 void* handle = 0;
279 (GLuint&)handle = id + 1;
280 return handle;
281}
282
283void gl_context::put_id(void* handle, void* ptr) const
284{
285 *static_cast<GLuint*>(ptr) = get_gl_id(handle);
286}
287
290{
292 (GLuint&)tex.internal_format = gl_format;
293}
294
297{
298 if (tex.internal_format)
299 return (const GLuint&)tex.internal_format;
302 return gl_format;
303}
304
305
306void gl_set_material_color(GLenum side, const cgv::media::illum::phong_material::color_type& c, float alpha, GLenum type)
307{
308 GLfloat v[4] = { c[0],c[1],c[2],c[3] * alpha };
309 glMaterialfv(side, type, v);
310}
311
314{
315 if (ms == MS_NONE)
316 return;
317 unsigned side = map_to_gl(ms);
318 gl_set_material_color(side, mat.get_ambient(), alpha, GL_AMBIENT);
319 gl_set_material_color(side, mat.get_diffuse(), alpha, GL_DIFFUSE);
320 gl_set_material_color(side, mat.get_specular(), alpha, GL_SPECULAR);
321 gl_set_material_color(side, mat.get_emission(), alpha, GL_EMISSION);
322 glMaterialf(side, GL_SHININESS, mat.get_shininess());
323}
324
327{
328 return info_font_size;
329}
332{
333 if (info_font_face.empty()) {
335 if (!info_font.empty())
336 info_font_face = info_font->get_font_face(FFA_REGULAR);
337 }
338 return info_font_face;
339}
340
343{
344 frame_buffer_stack.top()->handle = get_handle(0);
345 max_nr_indices = 0;
346 max_nr_vertices = 0;
347 info_font_size = 14;
348 show_help = false;
349 show_stats = false;
350
351 // set initial GL state from stack contents
356
361}
362
365{
366 return RA_OPENGL;
367}
368
369//GL_STACK_OVERFLOW, "stack overflow"
370//GL_STACK_UNDERFLOW, "stack underflow",
371std::string get_source_tag_name(GLenum tag)
372{
373 static std::map<GLenum, const char*> source_tags = {
374 { GL_DEBUG_SOURCE_API, "api" },
375 { GL_DEBUG_SOURCE_WINDOW_SYSTEM, "window system" },
376 { GL_DEBUG_SOURCE_SHADER_COMPILER, "shader compiler" },
377 { GL_DEBUG_SOURCE_THIRD_PARTY, "3rd party" },
378 { GL_DEBUG_SOURCE_APPLICATION, "application" },
379 { GL_DEBUG_SOURCE_OTHER, "other" }
380 };
381 return source_tags[tag];
382}
383
384std::string get_type_tag_name(GLenum tag)
385{
386 static std::map<GLenum, const char*> type_tags = {
387 { GL_DEBUG_TYPE_ERROR, "error" },
388 { GL_DEBUG_TYPE_DEPRECATED_BEHAVIOR, "deprecated behavior" },
389 { GL_DEBUG_TYPE_UNDEFINED_BEHAVIOR, "undefinded behavior" },
390 { GL_DEBUG_TYPE_PORTABILITY, "portability" },
391 { GL_DEBUG_TYPE_PERFORMANCE, "performance" },
392 { GL_DEBUG_TYPE_OTHER, "other" },
393 { GL_DEBUG_TYPE_MARKER, "marker" },
394 { GL_DEBUG_TYPE_PUSH_GROUP, "push group" },
395 { GL_DEBUG_TYPE_POP_GROUP, "pop group" }
396 };
397 return type_tags[tag];
398}
399
400std::string get_severity_tag_name(GLenum tag)
401{
402 static std::map<GLenum, const char*> severity_tags = {
403 { GL_DEBUG_SEVERITY_NOTIFICATION, "notification" },
404 { GL_DEBUG_SEVERITY_HIGH, "high" },
405 { GL_DEBUG_SEVERITY_MEDIUM, "medium" },
406 { GL_DEBUG_SEVERITY_LOW, "low" }
407 };
408 return severity_tags[tag];
409};
410
411void GLAPIENTRY debug_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length, const GLchar* message, const void* userParam)
412{
414 return;
415 const gl_context* ctx = reinterpret_cast<const gl_context*>(userParam);
416 std::string msg(message, length);
417 msg = std::string("GLDebug Message[") + cgv::utils::to_string(id) + "] from " + get_source_tag_name(source) + " of type " + get_type_tag_name(type) + " of severity " + get_severity_tag_name(severity) + "\n" + msg;
418 ctx->error(msg);
419}
420
423{
425 error("gl_context::configure_gl could not initialize glew");
426 return false;
427 }
429 version_major = version_string[0] - '0';
430 version_minor = version_string[2] - '0';
431 if (version_major >= 3) {
434 // weird behavior under windows or just nvidia or just my laptop (Stefan)??
437 }
438 else {
439 debug = false;
440 forward_compatible = false;
441 }
442 int version = 10 * version_major + version_minor;
443 if (version >= 32) {
446#ifdef _DEBUG
447 if (context_profile != 0) {
449 if (core_profile)
450 std::cerr << "WARNING: Ignoring that GL_CONTEXT_PROFILE_MASK yields compatibility profile for context created as core." << std::endl;
451 else
452 std::cerr << "WARNING: Ignoring that GL_CONTEXT_PROFILE_MASK does not yield compatibility profile for context created as such." << std::endl;
453 }
454 }
455 else {
456 std::cerr << "WARNING: Ignoring that GL_CONTEXT_PROFILE_MASK yields zero." << std::endl;
457 }
458#endif
459 }
460 else
461 core_profile = false;
463 std::string vendor_string(reinterpret_cast<const char*>(vendor_c_string));
465
466 if (vendor_string.find("NVIDIA") != std::string::npos)
467 gpu_vendor = GPU_VENDOR_NVIDIA;
468 else if (vendor_string.find("INTEL") != std::string::npos)
469 gpu_vendor = GPU_VENDOR_INTEL;
470 else if (vendor_string.find("AMD") != std::string::npos || vendor_string.find("ATI") != std::string::npos)
471 gpu_vendor = GPU_VENDOR_AMD;
472
473 // query device capabilities
474 glGetIntegerv(GL_MAX_GEOMETRY_OUTPUT_VERTICES, &gpu_capabilities.max_geometry_shader_output_vertex_count);
477 for(unsigned i = 0; i < 3; ++i)
479 for(unsigned i = 0; i < 3; ++i)
481
482#ifdef _DEBUG
483 std::cout << "OpenGL version " << version_major << "." << version_minor << (core_profile?" (core)":" (compatibility)") << (debug?" (debug)":"") << (forward_compatible?" (forward_compatible)":"") << std::endl;
486 if (vendor_c_string)
487 std::cout << " vendor : " << vendor_c_string << std::endl;
489 std::cout << " renderer : " << renderer_c_string << std::endl;
491 std::cout << " glslversion: " << glslversion_c_string << std::endl;
492#endif
493 if (debug) {
495 if (version >= 43) {
497 if (!check_gl_error("gl_context::configure() debug output"))
498 glDebugMessageCallback(debug_callback, this);
499 }
500 }
501 // use the eye location to compute the specular lighting
502 if (!core_profile) {
504 // this makes opengl normalize all surface normals before lighting calculations,
505 // which is essential when using scaling to deform tesselated primities
507
508 // should be initialized by the driver, but better be safe than risk errors later
510 }
512// if (check_gl_error("gl_context::configure_gl before init of children"))
513// return false;
514
515 group_ptr grp(dynamic_cast<group*>(this));
517 for (unsigned i = 0; i<grp->get_nr_children(); ++i)
518 traverser(sma, "nc").traverse(grp->get_child(i));
519
520// if (check_gl_error("gl_context::configure_gl after init of children."))
521// return false;
522
523 return true;
524}
525
526void gl_context::resize_gl()
527{
528 group_ptr grp(dynamic_cast<group*>(this));
530 if (grp) {
533 }
534}
535
540
545
550
556
557void gl_context::clear_background(bool color_flag, bool depth_flag, bool stencil_flag, bool accum_flag) {
558 GLenum bits = 0;
559 if(color_flag)
561 if(depth_flag)
563 if(stencil_flag)
567 if(bits)
568 glClear(bits);
569}
570
573{
574 if (current_font_size == 0)
575 return get_info_font_size();
576 return current_font_size;
577}
585
586void gl_context::init_render_pass()
587{
588#ifdef WIN32
590#else
592#endif
595 else
597
601 for (unsigned i = 0; i < nr_default_light_sources; ++i)
603
604 for (unsigned i = 0; i < nr_default_light_sources; ++i)
607 else
609 }
610 else if ((last_render_pass_flags & RPF_SET_LIGHTS) == 0) {
611 for (unsigned i = 0; i < nr_default_light_sources; ++i)
614 }
616
619 }
621 // this mode allows to define the ambient and diffuse color of the surface material
622 // via the glColor commands
624 }
626 // set some default settings
628 set_cull_state(CM_BACKFACE);
629 if (!core_profile)
631 }
633 set_projection_matrix(cgv::math::perspective4<double>(45.0, (double)get_width()/get_height(),0.001,1000.0));
634
636 set_modelview_matrix(cgv::math::look_at4<double>(vec3(0,0,10), vec3(0,0,0), vec3(0,1,0)));
637
638 if (check_gl_error("gl_context::init_render_pass before init_frame"))
639 return;
640
641 group* grp = dynamic_cast<group*>(this);
645 }
646
647 if (check_gl_error("gl_context::init_render_pass after init_frame"))
648 return;
649 // this defines the background color to which the frame buffer is set by glClear
652 // this defines the background depth buffer value set by glClear
655 // this defines the background depth buffer value set by glClear
658 // this defines the background color to which the accum buffer is set by glClear
666 );
667}
668
670void gl_context::finish_render_pass()
671{
672}
673
675{
676 std::ostream& os;
677 format_callback_handler(std::ostream& _os) : os(_os)
678 {
679 }
682 {
683 os << "\a";
684 return false;
685 }
688 {
689 os << "\b";
690 return false;
691 }
692
693};
694
695void gl_context::draw_textual_info()
696{
697 if (show_help || show_stats) {
701
704
705 set_cursor(20, get_height()-1-20);
706
707 vec4 bg = get_bg_color();
708 //if (bg_r + bg_g + bg_b < 1.5f)
709 if (bg[0] + bg[1] + bg[2] < 1.5f)
710 set_color(rgba(1, 1, 1, 1));
711 else
712 set_color(rgba(0, 0, 0, 1));
713
714 // traverse objects for show_stats callback
715 format_callback_handler fch(output_stream());
716 group_ptr grp(dynamic_cast<group*>(this));
717 if (grp && show_stats) {
719 traverser(sma, "nc").traverse(grp, &fch);
720 output_stream() << std::endl;
721 }
722 //if (bg_r + bg_g + bg_b < 1.5f)
723 if(bg[0] + bg[1] + bg[2] < 1.5f)
724 set_color(rgba(1, 1, 0, 1));
725 else
726 set_color(rgba(0.4f, 0.3f, 0, 1));
727
728 if (grp && show_help) {
729 // collect help from myself and all children
731 traverser(sma, "nc").traverse(grp, &fch);
732 output_stream().flush();
733 }
736 }
737}
738
739void gl_context::perform_screen_shot()
740{
741 glFlush();
743 if (!read_frame_buffer(dv))
744 return;
745 std::string ext("bmp");
747 if (cgv::utils::is_element("png",exts))
748 ext = "png";
749 else if (cgv::utils::is_element("tif",exts))
750 ext = "tif";
751 cgv::media::image::image_writer wr(std::string("screen_shot.")+ext);
752 if (wr.is_format_supported(*dv.get_format()))
753 wr.write_image(dv);
754}
755
757void gl_context::enumerate_program_uniforms(shader_program& prog, std::vector<std::string>& names, std::vector<int>* locations_ptr, std::vector<int>* sizes_ptr, std::vector<int>* types_ptr, bool show) const
758{
759 GLint count;
760 glGetProgramiv(get_gl_id(prog.handle), GL_ACTIVE_UNIFORMS, &count);
761 for (int i = 0; i < count; ++i) {
762 GLchar name[1000];
763 GLsizei length;
764 GLint size;
765 GLenum type;
766 glGetActiveUniform(get_gl_id(prog.handle), i, 1000, &length, &size, &type, name);
767 std::string name_str(name, length);
768 names.push_back(name_str);
769 if (sizes_ptr)
770 sizes_ptr->push_back(size);
771 if (types_ptr)
772 types_ptr->push_back(type);
773 int loc = glGetUniformLocation(get_gl_id(prog.handle), name_str.c_str());
774 if (locations_ptr)
775 locations_ptr->push_back(loc);
776 if (show)
777 std::cout << i << " at " << loc << " = " << name_str << ":" << type << "[" << size << "]" << std::endl;
778 }
779}
780
782void gl_context::enumerate_program_attributes(shader_program& prog, std::vector<std::string>& names, std::vector<int>* locations_ptr, std::vector<int>* sizes_ptr, std::vector<int>* types_ptr, bool show) const
783{
784 GLint count;
785 glGetProgramiv(get_gl_id(prog.handle), GL_ACTIVE_ATTRIBUTES, &count);
786 for (int i = 0; i < count; ++i) {
787 GLchar name[1000];
788 GLsizei length;
789 GLint size;
790 GLenum type;
791 glGetActiveAttrib(get_gl_id(prog.handle), i, 1000, &length, &size, &type, name);
792 std::string name_str(name, length);
793 names.push_back(name_str);
794 if (sizes_ptr)
795 sizes_ptr->push_back(size);
796 if (types_ptr)
797 types_ptr->push_back(type);
798 int loc = glGetAttribLocation(get_gl_id(prog.handle), name_str.c_str());
799 if (locations_ptr)
800 locations_ptr->push_back(loc);
801 if (show)
802 std::cout << i << " at " << loc << " = " << name_str << ":" << type << "[" << size << "]" << std::endl;
803 }
804}
805
808{
811 glColor4fv(&clr[0]);
812 }
813 if (shader_program_stack.empty())
814 return;
816 if (!prog.does_context_set_color())
817 return;
818 int clr_loc = prog.get_color_index();
819 if (clr_loc == -1)
820 return;
821 prog.set_attribute(*this, clr_loc, clr);
822}
823
826{
828 unsigned side = map_to_gl(MS_FRONT_AND_BACK);
829 float alpha = 1.0f - material.get_transparency();
830 gl_set_material_color(side, material.get_ambient_occlusion()*material.get_diffuse_reflectance(), alpha, GL_AMBIENT);
831 gl_set_material_color(side, material.get_diffuse_reflectance(), alpha, GL_DIFFUSE);
832 gl_set_material_color(side, material.get_specular_reflectance(), alpha, GL_SPECULAR);
833 gl_set_material_color(side, material.get_emission(), alpha, GL_EMISSION);
834 glMaterialf(side, GL_SHININESS, 1.0f/(material.get_roughness()+1.0f/128.0f));
835 }
836 context::set_material(material);
837}
838
841{
843 mat.enable_textures(*this);
844}
845
848{
849 mat.disable_textures(*this);
852}
853
854void gl_context::destruct_render_objects()
855{
856 for (unsigned i = 0; i < 4; ++i)
857 progs[i].destruct(*this);
858}
859
862{
863 if (!texture_support) {
864 if (!progs[0].is_created()) {
865 if (!progs[0].build_program(*this, "default.glpr")) {
866 error("could not build default shader program from default.glpr");
867 exit(0);
868 }
869 progs[0].specify_standard_uniforms(true, false, false, true);
870 progs[0].specify_standard_vertex_attribute_names(*this, true, false, false);
871 progs[0].allow_context_to_set_color(true);
872 }
873 return progs[0];
874 }
875 if (!progs[1].is_created()) {
876 if (!progs[1].build_program(*this, "textured_default.glpr")) {
877 error("could not build default shader program with texture support from textured_default.glpr");
878 exit(0);
879 }
880 progs[1].set_uniform(*this, "texture", 0);
881 progs[1].specify_standard_uniforms(true, false, false, true);
882 progs[1].specify_standard_vertex_attribute_names(*this, true, false, true);
883 progs[1].allow_context_to_set_color(true);
884 }
885 return progs[1];
886}
887
890{
891 if (!texture_support) {
892 if (!progs[2].is_created()) {
893 if (!progs[2].build_program(*this, "default_surface.glpr")) {
894 error("could not build surface shader program from default_surface.glpr");
895 exit(0);
896 }
897 progs[2].specify_standard_uniforms(true, true, true, true);
898 progs[2].specify_standard_vertex_attribute_names(*this, true, true, false);
899 progs[2].allow_context_to_set_color(true);
900 }
901 return progs[2];
902 }
903 if (!progs[3].is_created()) {
904 if (!progs[3].build_program(*this, "textured_surface.glpr")) {
905 error("could not build surface shader program with texture support from textured_surface.glpr");
906 exit(0);
907 }
908 progs[3].specify_standard_uniforms(true, true, true, true);
909 progs[3].specify_standard_vertex_attribute_names(*this, true, true, true);
910 progs[3].allow_context_to_set_color(true);
911 }
912 return progs[3];
913}
914
916{
923 continue;
924 }
925 GLfloat col[4] = { 1,1,1,1 };
927 *(rgb*)col = light.get_emission()*light.get_ambient_scale();
929 *(rgb*)col = light.get_emission();
931 *(rgb*)col = light.get_emission();
933
934 GLfloat pos[4] = { 0,0,0,light.get_type() == cgv::media::illum::LT_DIRECTIONAL ? 0.0f : 1.0f };
935 *(vec3*)pos = light.get_position();
937 if (light.get_type() != cgv::media::illum::LT_DIRECTIONAL) {
938 glLightf(GL_LIGHT0 + light_idx, GL_CONSTANT_ATTENUATION, light.get_constant_attenuation());
939 glLightf(GL_LIGHT0 + light_idx, GL_LINEAR_ATTENUATION, light.get_linear_attenuation());
940 glLightf(GL_LIGHT0 + light_idx, GL_QUADRATIC_ATTENUATION, light.get_quadratic_attenuation());
941 }
942 else {
946 }
947 if (light.get_type() == cgv::media::illum::LT_SPOT) {
948 glLightf(GL_LIGHT0 + light_idx, GL_SPOT_CUTOFF, light.get_spot_cutoff());
949 glLightf(GL_LIGHT0 + light_idx, GL_SPOT_EXPONENT, light.get_spot_exponent());
950 glLightfv(GL_LIGHT0 + light_idx, GL_SPOT_DIRECTION, light.get_spot_direction().data());
951 }
952 else {
955 static float dir[3] = { 0,0,1 };
957 }
959 }
960 }
962}
963
964void gl_context::tesselate_arrow(double length, double aspect, double rel_tip_radius, double tip_aspect, int res, bool edges)
965{
966 double cyl_radius = length*aspect;
969 double cyl_length = length - cone_length;
973 mul_modelview_matrix(cgv::math::rotate4(180.0,1.0,0.0,0.0));
974 tesselate_unit_disk(res, false, edges);
976
977 mul_modelview_matrix(cgv::math::translate4(0.0,0.0,1.0));
978 tesselate_unit_cylinder(res, false, edges);
979
980 mul_modelview_matrix(cgv::math::translate4(0.0, 0.0, 1.0));
983 mul_modelview_matrix(cgv::math::rotate4(180.0, 1.0, 0.0, 0.0));
984 tesselate_unit_disk(res, false, edges);
986 mul_modelview_matrix(cgv::math::translate4(0.0, 0.0, 1.0));
987 tesselate_unit_cone(res, false, edges);
989}
990
992void gl_context::rotate_vector_to_target(const dvec3& vector, const dvec3& target)
993{
994 double angle;
995 dvec3 axis;
996 compute_rotation_axis_and_angle_from_vector_pair(vector, target, axis, angle);
997 mul_modelview_matrix(cgv::math::rotate4<double>(180.0 / M_PI * angle, axis));
998}
999
1001void gl_context::tesselate_arrow(const dvec3& start, const dvec3& end, double aspect, double rel_tip_radius, double tip_aspect, int res, bool edges)
1002{
1003 if ((start - end).length() < 1e-8) {
1004 error("ignored tesselate arrow called with start and end closer then 1e-8");
1005 return;
1006 }
1008 mul_modelview_matrix(cgv::math::translate4<double>(start));
1009 rotate_vector_to_target(dvec3(0, 0, 1), end - start);
1010 tesselate_arrow((end-start).length(), aspect, rel_tip_radius, tip_aspect, res, edges);
1012}
1013
1015{
1016 set_color(i*l.get_emission());
1018 switch (l.get_type()) {
1019 case LT_DIRECTIONAL :
1020 mul_modelview_matrix(cgv::math::scale4<double>(light_scale, light_scale, light_scale));
1021 tesselate_arrow(vec3(0.0f), l.get_position(), 0.1f,2.0f,0.5f);
1022 break;
1023 case LT_POINT :
1025 cgv::math::translate4<double>(l.get_position())*
1026 cgv::math::scale4<double>(vec3(0.3f*light_scale)));
1028 break;
1029 case LT_SPOT :
1031 cgv::math::translate4<double>(l.get_position())*
1032 cgv::math::scale4<double>(vec3(light_scale))
1033 );
1034 rotate_vector_to_target(dvec3(0, 0, -1), l.get_spot_direction());
1035 {
1036 float t = tan(l.get_spot_cutoff()*(float)M_PI/180);
1037 if (l.get_spot_cutoff() > 45.0f)
1038 mul_modelview_matrix(cgv::math::scale4<double>(1, 1, 0.5f / t));
1039 else
1040 mul_modelview_matrix(cgv::math::scale4<double>(t, t, 0.5f));
1041 mul_modelview_matrix(cgv::math::translate4<double>(0, 0, -1));
1043 set_cull_state(CM_OFF);
1046 }
1047 }
1049}
1050
1052{
1053 if (frame_buffer_stack.empty()) {
1054 error("gl_context::announce_external_frame_buffer_change() called with empty frame buffer stack");
1055 return;
1056 }
1057 GLint fbo_id = 0;
1059 fbo_handle = frame_buffer_stack.top()->handle;
1060 frame_buffer_stack.top()->handle = get_handle(fbo_id);
1061}
1062
1064{
1065 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbo_handle));
1066 if (frame_buffer_stack.empty())
1067 return;
1068 frame_buffer_stack.top()->handle = fbo_handle;
1069}
1070
1072{
1073 if (window_transformation_stack.empty()) {
1074 error("gl_context::announce_external_viewport_change() called with empty window transformation stack");
1075 return;
1076 }
1077 GLint vp[4];
1079 cgv_viewport_storage = window_transformation_stack.top().front().viewport;
1080 window_transformation_stack.top().front().viewport = ivec4(vp[0], vp[1], vp[2], vp[3]);
1081}
1082
1090
1093{
1096
1097 GLint vp[4];
1099
1101 // push projection matrix
1103 // set orthogonal projection
1105 glOrtho(vp[0], vp[0]+vp[2], vp[1], vp[1]+vp[3], -1, 1);
1106 // push modelview matrix
1108 // use identity for modelview
1110 }
1111 set_modelview_matrix(cgv::math::identity4<double>());
1112 set_projection_matrix(cgv::math::ortho4<double>(vp[0], vp[0] + vp[2], vp[1], vp[1]+vp[3], -1, 1));
1113}
1114
1121
1124 unsigned int x, unsigned int y, FrameBufferType buffer_type,
1125 TypeId type, data::ComponentFormat cf, int w, int h)
1126{
1127 const cgv::data::data_format* df = dv.get_format();
1128 if (df) {
1129 w = int(df->get_width());
1130 h = int(df->get_height());
1131 type = df->get_component_type();
1133 if (w < 1 || h < 1) {
1134 error(std::string("read_frame_buffer: received invalid dimensions (") + cgv::utils::to_string(w) + "," + cgv::utils::to_string(h) + ")");
1135 return false;
1136 }
1137 }
1138 else {
1139 if (w < 1 || h < 1) {
1140 GLint vp[4];
1142 w = w < 1 ? vp[2] : w;
1143 h = h < 1 ? vp[3] : h;
1144 }
1145 }
1146 GLuint gl_type = map_to_gl(type);
1147 if (gl_type == 0) {
1148 error(std::string("read_frame_buffer: could not make component type ")+cgv::type::info::get_type_name(df->get_component_type())+" to gl type");
1149 return false;
1150 }
1152 if (cf != cgv::data::CF_D) {
1154 if (cf != cgv::data::CF_S) {
1155 gl_format = map_to_gl(cf);
1156 if (gl_format == GL_RGB && cf != cgv::data::CF_RGB) {
1157 error(std::string("read_frame_buffer: could not match component format ") + cgv::utils::to_string(df->get_component_format()));
1158 return false;
1159 }
1160 }
1161 }
1163 if (buffer_type < FB_BACK)
1165 else {
1166 switch (buffer_type) {
1167 case FB_FRONT : gl_buffer = GL_FRONT; break;
1168 case FB_BACK : gl_buffer = GL_BACK; break;
1169 case FB_FRONT_LEFT : gl_buffer = GL_FRONT_LEFT; break;
1170 case FB_FRONT_RIGHT : gl_buffer = GL_FRONT_RIGHT; break;
1171 case FB_BACK_LEFT : gl_buffer = GL_BACK_LEFT; break;
1172 case FB_BACK_RIGHT : gl_buffer = GL_BACK_RIGHT; break;
1173 default:
1174 error(std::string("invalid buffer type ")+cgv::utils::to_string(buffer_type));
1175 return false;
1176 }
1177 }
1178 // after all necessary information could be extracted, ensure that format
1179 // and data view are created
1180 if (!df) {
1181 df = new cgv::data::data_format(w,h,type,cf);
1182 dv.~data_view();
1183 new(&dv) data::data_view(df);
1184 dv.manage_format(true);
1185 }
1191 glReadPixels(x,y,w,h,gl_format,gl_type,dv.get_ptr<void>());
1195 return true;
1196}
1197
1198
1199void render_vertex(int k, const float* vertices, const float* normals, const float* tex_coords,
1200 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices, bool flip_normals)
1201{
1202 if (normals && normal_indices) {
1203 if (flip_normals) {
1204 float n[3] = { -normals[3*normal_indices[k]],-normals[3*normal_indices[k]+1],-normals[3*normal_indices[k]+2] };
1205 glNormal3fv(n);
1206 }
1207 else
1208 glNormal3fv(normals+3*normal_indices[k]);
1209 }
1210 if (tex_coords && tex_coord_indices)
1211 glTexCoord2fv(tex_coords+2*tex_coord_indices[k]);
1212 glVertex3fv(vertices+3*vertex_indices[k]);
1213}
1214
1215attribute_array_binding*& get_aab_ptr()
1216{
1217 static attribute_array_binding* aab_ptr = 0;
1218 return aab_ptr;
1219}
1220
1221vertex_buffer*& get_vbo_ptr()
1222{
1223 static vertex_buffer* vbo_ptr = 0;
1224 return vbo_ptr;
1225}
1226
1227bool gl_context::release_attributes(const float* normals, const float* tex_coords, const int* normal_indices, const int* tex_coord_indices) const
1228{
1229 shader_program* prog_ptr = static_cast<shader_program*>(get_current_program());
1230 if (!prog_ptr || prog_ptr->get_position_index() == -1)
1231 return false;
1232
1233 attribute_array_binding*& aab_ptr = get_aab_ptr();
1234 if (!aab_ptr) {
1235 attribute_array_binding::disable_global_array(*this, prog_ptr->get_position_index());
1236 if (prog_ptr->get_normal_index() != -1 && normals && normal_indices)
1237 attribute_array_binding::disable_global_array(*this, prog_ptr->get_normal_index());
1238 if (prog_ptr->get_texcoord_index() != -1 && tex_coords && tex_coord_indices)
1239 attribute_array_binding::disable_global_array(*this, prog_ptr->get_texcoord_index());
1240 }
1241 else {
1242 aab_ptr->disable(const_cast<gl_context&>(*this));
1243 aab_ptr->disable_array(*this, prog_ptr->get_position_index());
1244 if (prog_ptr->get_normal_index() != -1 && normals && normal_indices)
1245 aab_ptr->disable_global_array(*this, prog_ptr->get_normal_index());
1246 if (prog_ptr->get_texcoord_index() != -1 && tex_coords && tex_coord_indices)
1247 aab_ptr->disable_global_array(*this, prog_ptr->get_texcoord_index());
1248 vertex_buffer*& vbo_ptr = get_vbo_ptr();
1249 vbo_ptr->destruct(*this);
1250 delete vbo_ptr;
1251 vbo_ptr = 0;
1252 }
1253 return true;
1254}
1255
1256bool gl_context::prepare_attributes(std::vector<vec3>& P, std::vector<vec3>& N, std::vector<vec2>& T, unsigned nr_vertices,
1257 const float* vertices, const float* normals, const float* tex_coords,
1258 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices, bool flip_normals) const
1259{
1260 unsigned i;
1261 shader_program* prog_ptr = static_cast<shader_program*>(get_current_program());
1262 if (!prog_ptr || prog_ptr->get_position_index() == -1)
1263 return false;
1264 P.resize(nr_vertices);
1265 for (i = 0; i < nr_vertices; ++i)
1266 P[i] = *reinterpret_cast<const vec3*>(vertices + 3 * vertex_indices[i]);
1267
1268 if (prog_ptr->get_normal_index() != -1 && normals && normal_indices) {
1269 N.resize(nr_vertices);
1270 for (i = 0; i < nr_vertices; ++i) {
1271 N[i] = *reinterpret_cast<const vec3*>(normals + 3 * normal_indices[i]);
1272 if (flip_normals)
1273 N[i] = -N[i];
1274 }
1275 }
1276 if (prog_ptr->get_texcoord_index() != -1 && tex_coords && tex_coord_indices) {
1277 T.resize(nr_vertices);
1278 for (i = 0; i < nr_vertices; ++i)
1279 T[i] = *reinterpret_cast<const vec2*>(tex_coords + 2 * tex_coord_indices[i]);
1280 }
1281
1282 attribute_array_binding*& aab_ptr = get_aab_ptr();
1283 if (core_profile && !aab_ptr) {
1284 aab_ptr = new attribute_array_binding();
1285 aab_ptr->create(*this);
1286 }
1287 if (!aab_ptr) {
1288 attribute_array_binding::set_global_attribute_array<>(*this, prog_ptr->get_position_index(), P);
1289 attribute_array_binding::enable_global_array(*this, prog_ptr->get_position_index());
1290 if (prog_ptr->get_normal_index() != -1) {
1291 if (normals && normal_indices) {
1292 attribute_array_binding::set_global_attribute_array<>(*this, prog_ptr->get_normal_index(), N);
1293 attribute_array_binding::enable_global_array(*this, prog_ptr->get_normal_index());
1294 }
1295 else
1296 attribute_array_binding::disable_global_array(*this, prog_ptr->get_normal_index());
1297 }
1298 if (prog_ptr->get_texcoord_index() != -1) {
1299 if (tex_coords && tex_coord_indices) {
1300 attribute_array_binding::set_global_attribute_array<>(*this, prog_ptr->get_texcoord_index(), T);
1301 attribute_array_binding::enable_global_array(*this, prog_ptr->get_texcoord_index());
1302 }
1303 else
1304 attribute_array_binding::disable_global_array(*this, prog_ptr->get_texcoord_index());
1305 }
1306 }
1307 else {
1308 vertex_buffer*& vbo_ptr = get_vbo_ptr();
1309 if (!vbo_ptr)
1310 vbo_ptr = new vertex_buffer();
1311 else
1312 vbo_ptr->destruct(*this);
1313 vbo_ptr->create(*this, P.size() * sizeof(vec3) + N.size() * sizeof(vec3) + T.size() * sizeof(vec2));
1314 vbo_ptr->replace(const_cast<gl_context&>(*this), 0, &P.front(), P.size());
1315 size_t nml_off = P.size() * sizeof(vec3);
1316 size_t tex_off = nml_off;
1317 if (!N.empty()) {
1318 vbo_ptr->replace(const_cast<gl_context&>(*this), nml_off, &N.front(), N.size());
1319 tex_off += N.size() * sizeof(vec2);
1320 }
1321 if (!T.empty())
1322 vbo_ptr->replace(const_cast<gl_context&>(*this), tex_off, &T.front(), T.size());
1323
1325 aab_ptr->set_attribute_array(*this, prog_ptr->get_position_index(), td3, *vbo_ptr, 0, P.size());
1326 aab_ptr->enable_array(*this, prog_ptr->get_position_index());
1327 if (prog_ptr->get_normal_index() != -1) {
1328 if (normals && normal_indices) {
1329 aab_ptr->set_attribute_array(*this, prog_ptr->get_normal_index(), td3, *vbo_ptr, nml_off, N.size());
1330 aab_ptr->enable_array(*this, prog_ptr->get_normal_index());
1331 }
1332 else
1333 aab_ptr->disable_array(*this, prog_ptr->get_normal_index());
1334 }
1335 if (prog_ptr->get_texcoord_index() != -1) {
1336 if (tex_coords && tex_coord_indices) {
1338 aab_ptr->set_attribute_array(*this, prog_ptr->get_texcoord_index(), td2, *vbo_ptr, tex_off, T.size());
1339 aab_ptr->enable_array(*this, prog_ptr->get_texcoord_index());
1340 }
1341 else
1342 aab_ptr->disable_array(*this, prog_ptr->get_texcoord_index());
1343 }
1344 aab_ptr->enable(const_cast<gl_context&>(*this));
1345 }
1346 return true;
1347}
1348
1351 const float* vertices, const float* normals, const float* tex_coords,
1352 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
1353 int nr_faces, int face_degree, bool flip_normals) const
1354{
1356 int k = 0;
1357 for (int i = 0; i < nr_faces; ++i) {
1359 for (int j = 0; j < face_degree; ++j, ++k)
1360 render_vertex(k, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1361 glEnd();
1362 }
1363 return;
1364 }
1365 unsigned nr_vertices = face_degree * nr_faces;
1366 std::vector<vec3> P, N;
1367 std::vector<vec2> T;
1368 if (!prepare_attributes(P, N, T, nr_vertices, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals))
1369 return;
1370 for (int i = 0; i < nr_faces; ++i)
1371 glDrawArrays(GL_LINE_LOOP, i*face_degree, face_degree);
1372 release_attributes(normals, tex_coords, normal_indices, tex_coord_indices);
1373}
1374
1375
1376void gl_context::draw_elements_void(GLenum mode, size_t total_count, GLenum type, size_t type_size, const void* indices) const
1377{
1378 ensure_configured();
1379 size_t drawn = 0;
1380 const cgv::type::uint8_type* index_ptr = static_cast<const cgv::type::uint8_type*>(indices);
1381 while (drawn < total_count) {
1382 size_t count = total_count - drawn;
1383 if (count > max_nr_indices)
1384 count = size_t(max_nr_indices);
1385 glDrawElements(mode, GLsizei(count), type, index_ptr + drawn * type_size);
1386 drawn += count;
1387 }
1388}
1389
1390size_t max_nr_indices, max_nr_vertices;
1391void gl_context::ensure_configured() const
1392{
1393 if (max_nr_indices != 0)
1394 return;
1395 glGetInteger64v(GL_MAX_ELEMENTS_INDICES, reinterpret_cast<GLint64*>(&max_nr_indices));
1396 glGetInteger64v(GL_MAX_ELEMENTS_VERTICES, reinterpret_cast<GLint64*>(&max_nr_vertices));
1397}
1398
1401 const float* vertices, const float* normals, const float* tex_coords,
1402 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
1403 int nr_faces, int face_degree, bool is_fan, bool flip_normals) const
1404{
1405 int s = face_degree - 2;
1406 int i, k = 2;
1407 std::vector<GLuint> I;
1408 I.push_back(0); I.push_back(1);
1409 for (i = 0; i < nr_faces; ++i) {
1410 if (is_fan) {
1411 I.push_back(k - 1); I.push_back(k);
1412 I.push_back(0); I.push_back(k);
1413 continue;
1414 }
1415 if (s == 1) {
1416 I.push_back(k - 1); I.push_back(k);
1417 I.push_back(k - 2); I.push_back(k);
1418 }
1419 else {
1420 I.push_back(k - 1); I.push_back(k + 1);
1421 I.push_back(k - 2); I.push_back(k);
1422 I.push_back(k); I.push_back(k + 1);
1423 }
1424 k += 2;
1425 }
1426
1429 for (GLuint j:I)
1430 render_vertex(j, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1431 glEnd();
1432 return;
1433 }
1434 unsigned nr_vertices = 2 + (face_degree - 2) * nr_faces;
1435 std::vector<vec3> P, N;
1436 std::vector<vec2> T;
1437 if (!prepare_attributes(P, N, T, nr_vertices, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals))
1438 return;
1439 draw_elements(GL_LINES, I.size(), &I[0]);
1440 release_attributes(normals, tex_coords, normal_indices, tex_coord_indices);
1441}
1442
1444 const float* vertices, const float* normals, const float* tex_coords,
1445 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
1446 int nr_faces, int face_degree, bool flip_normals) const
1447{
1449 int k = 0;
1450 if (face_degree < 5)
1451 glBegin(face_degree == 3 ? GL_TRIANGLES : GL_QUADS);
1452 for (int i = 0; i < nr_faces; ++i) {
1453 if (face_degree >= 5)
1455 for (int j = 0; j < face_degree; ++j, ++k)
1456 render_vertex(k, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1457 if (face_degree >= 5)
1458 glEnd();
1459 }
1460 if (face_degree < 5)
1461 glEnd();
1462 return;
1463 }
1464 unsigned nr_vertices = face_degree * nr_faces;
1465 std::vector<vec3> P, N;
1466 std::vector<vec2> T;
1467 if (!prepare_attributes(P, N, T, nr_vertices, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals))
1468 return;
1469 /*
1470 for (unsigned x = 0; x < nr_vertices; ++x) {
1471 std::cout << x << ": [" << P[x] << "]";
1472 if (N.size() > 0)
1473 std::cout << ", <" << N[x] << ">";
1474 if (T.size() > 0)
1475 std::cout << ", {" << T[x] << "}";
1476 std::cout << std::endl;
1477 }
1478 */
1479 if (face_degree == 3)
1480 glDrawArrays(GL_TRIANGLES, 0, nr_vertices);
1481 else {
1482 for (int i = 0; i < nr_faces; ++i) {
1483 glDrawArrays(GL_TRIANGLE_FAN, i*face_degree, face_degree);
1484 }
1485 }
1486 release_attributes(normals, tex_coords, normal_indices, tex_coord_indices);
1487}
1488
1490 const float* vertices, const float* normals, const float* tex_coords,
1491 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
1492 int nr_faces, int face_degree, bool is_fan, bool flip_normals) const
1493{
1494 int s = face_degree - 2;
1495 int k = 2;
1497 glBegin(face_degree == 3 ? (is_fan ? GL_TRIANGLE_FAN : GL_TRIANGLE_STRIP) : GL_QUAD_STRIP);
1498 render_vertex(0, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1499 render_vertex(1, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1500 for (int i = 0; i < nr_faces; ++i)
1501 for (int j = 0; j < s; ++j, ++k)
1502 render_vertex(k, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1503 glEnd();
1504 return;
1505 }
1506 unsigned nr_vertices = 2 + (face_degree-2) * nr_faces;
1507 std::vector<vec3> P, N;
1508 std::vector<vec2> T;
1509 if (!prepare_attributes(P, N, T, nr_vertices, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals))
1510 return;
1512 release_attributes(normals, tex_coords, normal_indices, tex_coord_indices);
1513}
1514
1516 if(state.enabled)
1518 else
1520 glDepthFunc(map_to_gl(state.test_func));
1522}
1523
1525 glDepthFunc(map_to_gl(func));
1527}
1528
1533
1538
1540 switch(culling_mode) {
1541 case CM_OFF:
1543 break;
1544 case CM_BACKFACE:
1547 break;
1548 case CM_FRONTFACE:
1551 break;
1552 default:
1553 break;
1554 }
1555 context::set_cull_state(culling_mode);
1556}
1557
1559 if(state.enabled)
1561 else
1565 map_to_gl(state.src_color),
1566 map_to_gl(state.dst_color),
1567 map_to_gl(state.src_alpha),
1568 map_to_gl(state.dst_alpha)
1569 );
1570 } else {
1571 glBlendFunc(map_to_gl(state.src_color), map_to_gl(state.dst_color));
1572 }
1574}
1575
1580
1590
1595
1600
1602 glDepthMask(map_to_gl(mask.depth_flag));
1604 map_to_gl(mask.red_flag),
1605 map_to_gl(mask.green_flag),
1606 map_to_gl(mask.blue_flag),
1607 map_to_gl(mask.alpha_flag)
1608 );
1610}
1611
1613 glDepthMask(map_to_gl(flag));
1615}
1616
1619 map_to_gl(flags[0]),
1620 map_to_gl(flags[1]),
1621 map_to_gl(flags[2]),
1622 map_to_gl(flags[3])
1623 );
1625}
1626
1629{
1631 GLdouble V[16];
1633 return dmat4(4,4,V);
1634 }
1635 if (modelview_matrix_stack.empty())
1636 return identity4<double>();
1637 return modelview_matrix_stack.top();
1638}
1639
1642{
1644 GLdouble P[16];
1646 return dmat4(4,4,P);
1647 }
1648 if (projection_matrix_stack.empty())
1649 return identity4<double>();
1650 return projection_matrix_stack.top();
1651}
1654{
1656 update_window_transformation_array();
1657}
1658
1659void gl_context::update_window_transformation_array()
1660{
1661 const std::vector<window_transformation>& wta = window_transformation_stack.top();
1662 if (wta.size() == 1) {
1663 const ivec4& viewport = wta.front().viewport;
1664 const dvec2& depth_range = wta.front().depth_range;
1665 glViewport(viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1666 glScissor(viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1667 glDepthRange(depth_range[0], depth_range[1]);
1668 }
1669 else {
1670 for (GLuint array_index = 0; array_index < (GLuint)wta.size(); ++array_index) {
1671 const ivec4& viewport = wta[array_index].viewport;
1672 const dvec2& depth_range = wta[array_index].depth_range;
1673 glViewportIndexedf(array_index, (GLfloat)viewport[0], (GLfloat)viewport[1], (GLfloat)viewport[2], (GLfloat)viewport[3]);
1674 glScissorIndexed(array_index, viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1675 glDepthRangeIndexed(array_index, (GLclampd)depth_range[0], (GLclampd)depth_range[1]);
1676 }
1677 }
1678}
1679
1681{
1682 GLint max_value = 1;
1683 if (GLEW_VERSION_4_1)
1684 glGetIntegerv(GL_MAX_VIEWPORTS, &max_value);
1685 return max_value;
1686}
1687
1690{
1691 size_t nr = window_transformation_stack.top().size();
1693 if (nr != window_transformation_stack.top().size())
1694 update_window_transformation_array();
1695 else {
1696 if (array_index < 1) {
1697 glViewport(viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1698 glScissor(viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1699 }
1700 else {
1701 glViewportIndexedf(array_index, (GLfloat)viewport[0], (GLfloat)viewport[1], (GLfloat)viewport[2], (GLfloat)viewport[3]);
1702 glScissorIndexed(array_index, viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1703 }
1704 }
1705}
1706
1708void gl_context::set_depth_range(const dvec2& depth_range, int array_index)
1709{
1710 size_t nr = window_transformation_stack.top().size();
1712 if (nr != window_transformation_stack.top().size())
1713 update_window_transformation_array();
1714 else {
1715 if (array_index < 1)
1716 glDepthRange(depth_range[0], depth_range[1]);
1717 else
1718 glDepthRangeIndexed(array_index, (GLclampd)depth_range[0], (GLclampd)depth_range[1]);
1719 }
1720}
1721/*
1723gl_context::dmat4 gl_context::get_device_matrix() const
1724{
1725 GLint vp[4];
1726 glGetIntegerv(GL_VIEWPORT, vp);
1727 dmat4 D; D.zeros();
1728 D(0, 0) = 0.5*vp[2];
1729 D(0, 3) = 0.5*vp[2] + vp[0];
1730 D(1, 1) = -0.5*vp[3]; // flip y-coordinate
1731 D(1, 3) = get_height() - 0.5*vp[3] - vp[1];
1732 D(2, 2) = 0.5;
1733 D(2, 3) = 0.5;
1734 D(3, 3) = 1.0;
1735 return D;
1736}
1737*/
1749
1761
1764{
1766
1767 if (!in_render_process() && !is_current())
1768 make_current();
1769
1771 /*
1772 GLenum err = glGetError();
1773 if (err != GL_NO_ERROR) {
1774 std::cout << "gl error:";
1775 switch (err) {
1776 case GL_INVALID_ENUM : std::cout << "invalid enum"; break;
1777 case GL_INVALID_VALUE : std::cout << "invalid value"; break;
1778 case GL_INVALID_OPERATION : std::cout << "invalid operation"; break;
1779 case GL_STACK_OVERFLOW : std::cout << "stack overflow"; break;
1780 case GL_STACK_UNDERFLOW : std::cout << "stack underflow"; break;
1781 case GL_OUT_OF_MEMORY : std::cout << "out of memory"; break;
1782 default: std::cout << "unknown error"; break;
1783 }
1784 std::cout << std::endl;
1785
1786 }
1787 */
1788 return z_window;
1789}
1790
1791cgv::data::component_format gl_context::texture_find_best_format(
1793 render_component& rc, const std::vector<cgv::data::data_view>* palettes) const
1794{
1795 GLuint& gl_format = (GLuint&)rc.internal_format;
1798 return best_cf;
1799}
1800
1801std::string gl_error_to_string(GLenum eid) {
1802 switch (eid) {
1803 case GL_NO_ERROR: return "";
1804 case GL_INVALID_ENUM: return "invalid enum";
1805 case GL_INVALID_VALUE: return "invalid value";
1806 case GL_INVALID_OPERATION: return "invalid operation";
1807 case GL_INVALID_FRAMEBUFFER_OPERATION: return "invalid framebuffe";
1808 case GL_OUT_OF_MEMORY: return "out of memory";
1809 case GL_STACK_UNDERFLOW: return "stack underflow";
1810 case GL_STACK_OVERFLOW: return "stack overflow";
1811 default:
1812 return "undefined error (id: " + std::to_string(eid) + ")";
1813 }
1814 //return std::string((const char*)gluErrorString(eid));
1815}
1816
1817std::string gl_error() {
1818 GLenum eid = glGetError();
1819 return gl_error_to_string(eid);
1820}
1821
1822bool gl_context::check_gl_error(const std::string& where, const cgv::render::render_component* rc) const
1823{
1824 GLenum eid = glGetError();
1825 if (eid == GL_NO_ERROR)
1826 return false;
1827 std::string error_string = where + ": " + gl_error_to_string(eid);
1829 return true;
1830}
1831
1832bool gl_context::check_texture_support(TextureType tt, const std::string& where, const cgv::render::render_component* rc) const
1833{
1834 switch (tt) {
1835 case TT_3D:
1836 if (!GLEW_VERSION_1_2) {
1837 error(where + ": 3D texture not supported", rc);
1838 return false;
1839 }
1840 break;
1841 case TT_CUBEMAP:
1842 if (!GLEW_VERSION_1_3) {
1843 error(where + ": cubemap texture not supported", rc);
1844 return false;
1845 }
1846 default:
1847 break;
1848 }
1849 return true;
1850}
1851
1852bool gl_context::check_shader_support(ShaderType st, const std::string& where, const cgv::render::render_component* rc) const
1853{
1854 switch (st) {
1855 case ST_COMPUTE:
1856 if (GLEW_VERSION_4_3)
1857 return true;
1858 else {
1859 error(where+": compute shader need not supported OpenGL version 4.3", rc);
1860 return false;
1861 }
1862 case ST_TESS_CONTROL:
1863 case ST_TESS_EVALUATION:
1864 if (GLEW_VERSION_4_0)
1865 return true;
1866 else {
1867 error(where+": tessellation shader need not supported OpenGL version 4.0", rc);
1868 return false;
1869 }
1870 case ST_GEOMETRY:
1871 if (GLEW_VERSION_3_2)
1872 return true;
1873 else {
1874 error(where + ": geometry shader need not supported OpenGL version 3.2", rc);
1875 return false;
1876 }
1877 default:
1878 if (GLEW_VERSION_2_0)
1879 return true;
1880 else {
1881 error(where + ": shaders need not supported OpenGL version 2.0", rc);
1882 return false;
1883 }
1884 }
1885}
1886
1887bool gl_context::check_fbo_support(const std::string& where, const cgv::render::render_component* rc) const
1888{
1889 if (!GLEW_VERSION_3_0) {
1890 error(where + ": framebuffer objects not supported", rc);
1891 return false;
1892 }
1893 return true;
1894}
1895
1896GLuint gl_context::texture_generate(texture_base& tb) const
1897{
1898 if (!check_texture_support(tb.tt, "gl_context::texture_generate", &tb))
1899 return get_gl_id(0);
1900 GLuint tex_id = get_gl_id(0);
1901 glGenTextures(1, &tex_id);
1903 error("gl_context::texture_generate: attempt to create texture inside glBegin-glEnd-block", &tb);
1904 return tex_id;
1905}
1906
1907GLuint gl_context::texture_bind(TextureType tt, GLuint tex_id) const
1908{
1909 GLint tmp_id;
1910 glGetIntegerv(get_tex_bind(tt), &tmp_id);
1911 glBindTexture(get_tex_dim(tt), tex_id);
1912 return tmp_id;
1913}
1914
1915void gl_context::texture_unbind(TextureType tt, GLuint tmp_id) const
1916{
1917 glBindTexture(get_tex_dim(tt), tmp_id);
1918}
1919
1920bool gl_context::texture_create(texture_base& tb, cgv::data::data_format& df) const
1921{
1922 GLuint gl_format = (const GLuint&) tb.internal_format;
1923
1924 if (tb.tt == TT_UNDEF)
1925 tb.tt = (TextureType)df.get_nr_dimensions();
1926 GLuint tex_id = texture_generate(tb);
1927 if (tex_id == get_gl_id(0))
1928 return false;
1929 GLuint tmp_id = texture_bind(tb.tt, tex_id);
1930
1931 // extract component type
1932 unsigned int transfer_format = map_to_gl(df.get_standard_component_format(), df.get_integer_interpretation());
1933 if (transfer_format == -1) {
1934 error("could not determine transfer format", &tb);
1935 return false;
1936 }
1937 switch (tb.tt) {
1938 case TT_1D :
1940 gl_format, GLsizei(df.get_width()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1941 break;
1942 case TT_1D_ARRAY :
1944 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1945 break;
1946 case TT_2D :
1947 glTexImage2D(GL_TEXTURE_2D, 0, gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1948 break;
1949 case TT_MULTISAMPLE_2D:
1950 glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, tb.nr_multi_samples, gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), map_to_gl(tb.fixed_sample_locations));
1951 break;
1952 case TT_2D_ARRAY :
1953 glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), GLsizei(df.get_depth()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1954 break;
1955 case TT_MULTISAMPLE_2D_ARRAY:
1956 glTexStorage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, tb.nr_multi_samples, gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), GLsizei(df.get_depth()), map_to_gl(tb.fixed_sample_locations));
1957 break;
1958 case TT_3D :
1960 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), GLsizei(df.get_depth()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1961 break;
1962 case TT_CUBEMAP :
1964 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1966 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1968 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1970 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1972 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1974 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1975 default:
1976 break;
1977 }
1978 if (check_gl_error("gl_context::texture_create", &tb)) {
1979 glDeleteTextures(1, &tex_id);
1980 texture_unbind(tb.tt, tmp_id);
1981 return false;
1982 }
1983
1984 texture_unbind(tb.tt, tmp_id);
1985 tb.have_mipmaps = false;
1986 tb.handle = get_handle(tex_id);
1987 return true;
1988}
1989
1990bool gl_context::texture_create(
1991 texture_base& tb,
1993 const cgv::data::const_data_view& data,
1994 int level, int cube_side, int num_array_layers, const std::vector<cgv::data::data_view>* palettes) const
1995{
1996 // query the format to be used for the texture
1997 GLuint gl_tex_format = (const GLuint&) tb.internal_format;
1998
1999 // define the texture type from the data format and the cube_side parameter
2000 tb.tt = (TextureType)data.get_format()->get_nr_dimensions();
2001 if(cube_side > -1) {
2002 if(tb.tt == TT_2D)
2003 tb.tt = TT_CUBEMAP;
2004 } else if(num_array_layers != 0) {
2005 if(num_array_layers < 0) {
2006 // automatic inference of layers from texture dimensions
2007 unsigned n_dims = data.get_format()->get_nr_dimensions();
2008 if(n_dims == 2)
2009 tb.tt = TT_1D_ARRAY;
2010 if(n_dims == 3)
2011 tb.tt = TT_2D_ARRAY;
2012 } else {
2013 switch(tb.tt) {
2014 case TT_1D: tb.tt = TT_1D_ARRAY; break;
2015 case TT_2D: tb.tt = TT_2D_ARRAY; break;
2016 case TT_3D: tb.tt = TT_2D_ARRAY; break;
2017 default:
2018 break;
2019 }
2020 }
2021 }
2022 // create texture is not yet done
2023 GLuint tex_id;
2024 if (tb.is_created())
2025 tex_id = get_gl_id(tb.handle);
2026 else {
2027 tex_id = texture_generate(tb);
2028 if (tex_id == get_gl_id(0))
2029 return false;
2030 tb.handle = get_handle(tex_id);
2031 }
2032
2033 // bind texture
2034 GLuint tmp_id = texture_bind(tb.tt, tex_id);
2035
2036 // load data to texture
2037 tb.have_mipmaps = load_texture(data, gl_tex_format, level, cube_side, num_array_layers, palettes);
2038 bool result = !check_gl_error("gl_context::texture_create", &tb);
2039 // restore old texture
2040 texture_unbind(tb.tt, tmp_id);
2041 return result;
2042}
2043
2044bool gl_context::texture_create_from_buffer(
2045 texture_base& tb,
2047 int x, int y, int level) const
2048{
2049 GLuint gl_format = (const GLuint&) tb.internal_format;
2050
2051 tb.tt = (TextureType)df.get_nr_dimensions();
2052 if (tb.tt != TT_2D) {
2053 tb.last_error = "texture creation from buffer only possible for 2d textures";
2054 return false;
2055 }
2056 GLuint tex_id;
2057 if (tb.is_created())
2058 tex_id = get_gl_id(tb.handle);
2059 else {
2060 tex_id = texture_generate(tb);
2061 if (tex_id == get_gl_id(0))
2062 return false;
2063 tb.handle = get_handle(tex_id);
2064 }
2065 GLuint tmp_id = texture_bind(tb.tt, tex_id);
2066
2067 // check mipmap type
2068 bool gen_mipmap = level == -1;
2069 if (gen_mipmap)
2070 level = 0;
2071
2072 glCopyTexImage2D(GL_TEXTURE_2D, level, gl_format, x, y, GLsizei(df.get_width()), GLsizei(df.get_height()), 0);
2073 bool result = false;
2074 std::string error_string("gl_context::texture_create_from_buffer: ");
2075 switch (glGetError()) {
2076 case GL_NO_ERROR :
2077 result = true;
2078 break;
2079 case GL_INVALID_ENUM :
2080 error_string += "target was not an accepted value.";
2081 break;
2082 case GL_INVALID_VALUE :
2083 error_string += "level was less than zero or greater than log sub 2(max), where max is the returned value of GL_MAX_TEXTURE_SIZE.\n"
2084 "or border was not zero or 1.\n"
2085 "or width was less than zero, greater than 2 + GL_MAX_TEXTURE_SIZE; or width cannot be represented as 2n + 2 * border for some integer n.";
2086 break;
2087 case GL_INVALID_OPERATION :
2088 error_string += "glCopyTexImage2D was called between a call to glBegin and the corresponding call to glEnd.";
2089 break;
2090 default:
2091 error_string += gl_error_to_string(glGetError());
2092 break;
2093 }
2094 texture_unbind(tb.tt, tmp_id);
2095 if (!result)
2097 else
2098 if (gen_mipmap)
2099 result = texture_generate_mipmaps(tb, tb.tt == TT_CUBEMAP ? 2 : (int)tb.tt);
2100
2101 return result;
2102}
2103
2104bool gl_context::texture_replace(
2105 texture_base& tb,
2106 int x, int y, int z,
2107 const cgv::data::const_data_view& data,
2108 int level, const std::vector<cgv::data::data_view>* palettes) const
2109{
2110 if (!tb.is_created()) {
2111 error("gl_context::texture_replace: attempt to replace in not created texture", &tb);
2112 return false;
2113 }
2114 // determine dimension from location arguments
2115 unsigned int dim = 1;
2116 if (y != -1) {
2117 ++dim;
2118 if (z != -1)
2119 ++dim;
2120 }
2121 // check consistency
2122 if (tb.tt == TT_CUBEMAP) {
2123 if (dim != 3) {
2124 error("gl_context::texture_replace: replace on cubemap without the side defined", &tb);
2125 return false;
2126 }
2127 if (z < 0 || z > 5) {
2128 error("gl_context::texture_replace: replace on cubemap with invalid side specification", &tb);
2129 return false;
2130 }
2131 }
2132 else {
2133 if (tb.tt != dim) {
2134 error("gl_context::texture_replace: replace on texture with invalid position specification", &tb);
2135 return false;
2136 }
2137 }
2138
2139 // bind texture
2140 GLuint tmp_id = texture_bind(tb.tt, get_gl_id(tb.handle));
2141 tb.have_mipmaps = replace_texture(data, level, x, y, z, palettes) || tb.have_mipmaps;
2142 bool result = !check_gl_error("gl_context::texture_replace", &tb);
2143 texture_unbind(tb.tt, tmp_id);
2144 return result;
2145}
2146
2147bool gl_context::texture_replace_from_buffer(
2148 texture_base& tb,
2149 int x, int y, int z,
2150 int x_buffer, int y_buffer,
2151 unsigned int width, unsigned int height,
2152 int level) const
2153{
2154 if (!tb.is_created()) {
2155 error("gl_context::texture_replace_from_buffer: attempt to replace in not created texture", &tb);
2156 return false;
2157 }
2158 // determine dimension from location arguments
2159 unsigned int dim = 2;
2160 if (z != -1)
2161 ++dim;
2162
2163 // consistency checks
2164 if (tb.tt == TT_CUBEMAP) {
2165 if (dim != 3) {
2166 error("gl_context::texture_replace_from_buffer: replace on cubemap without the side defined", &tb);
2167 return false;
2168 }
2169 if (z < 0 || z > 5) {
2170 error("gl_context::texture_replace_from_buffer: replace on cubemap without invalid side specification", &tb);
2171 return false;
2172 }
2173 }
2174 else {
2175 if (tb.tt != dim) {
2176 tb.last_error = "replace on texture with invalid position specification";
2177 return false;
2178 }
2179 }
2180 // check mipmap type
2181 bool gen_mipmap = level == -1;
2182 if (gen_mipmap)
2183 level = 0;
2184
2185 // bind texture
2186 GLuint tmp_id = texture_bind(tb.tt, get_gl_id(tb.handle));
2187 switch (tb.tt) {
2188 case TT_2D : glCopyTexSubImage2D(GL_TEXTURE_2D, level, x, y, x_buffer, y_buffer, width, height); break;
2189 case TT_3D : glCopyTexSubImage3D(GL_TEXTURE_3D, level, x, y, z, x_buffer, y_buffer, width, height); break;
2190 case TT_CUBEMAP : glCopyTexSubImage2D(get_gl_cube_map_target(z), level, x, y, x_buffer, y_buffer, width, height);
2191 default: break;
2192 }
2193 bool result = !check_gl_error("gl_context::texture_replace_from_buffer", &tb);
2194 texture_unbind(tb.tt, tmp_id);
2195
2196 if (result && gen_mipmap)
2197 result = texture_generate_mipmaps(tb, tb.tt == TT_CUBEMAP ? 2 : (int)tb.tt);
2198
2199 return result;
2200}
2201
2202bool gl_context::texture_create_mipmaps(texture_base& tb, cgv::data::data_format& df) const
2203{
2204 GLuint gl_format = (const GLuint&)tb.internal_format;
2205
2206 // extract component type
2207 unsigned int transfer_format = map_to_gl(df.get_standard_component_format(), df.get_integer_interpretation());
2208
2209 if(transfer_format == -1) {
2210 error("could not determine transfer format", &tb);
2211 return false;
2212 }
2213
2214 // extract texture size and compute number of mip-levels
2215 uvec3 size(unsigned(df.get_width()), unsigned(df.get_height()), unsigned(df.get_depth()));
2216
2217 unsigned max_size = cgv::math::max_value(size);
2218 unsigned num_levels = 1 + static_cast<unsigned>(log2(static_cast<float>(max_size)));
2219
2220 // compute mip-level sizes
2221 std::vector<uvec3> level_sizes(num_levels);
2222 level_sizes[0] = size;
2223
2224 for(unsigned level = 1; level < num_levels; ++level) {
2225 uvec3 level_size = level_sizes[level - 1];
2226 level_size = level_size / 2u;
2227 level_size = cgv::math::max(level_size, uvec3(1u));
2228 level_sizes[level] = level_size;
2229 }
2230
2231 GLuint tmp_id = texture_bind(tb.tt, get_gl_id(tb.handle));
2232
2233 bool result = true;
2234
2235 switch(tb.tt) {
2236 case TT_1D:
2237 for(unsigned level = 1; level < num_levels; ++level) {
2238 uvec3 level_size = level_sizes[level];
2240 }
2241 break;
2242 case TT_1D_ARRAY:
2243 //glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2244 error("create mipmaps not implemented for 1D array textures", &tb);
2245 result = false;
2246 break;
2247 case TT_2D:
2248 for(unsigned level = 1; level < num_levels; ++level) {
2249 uvec3 level_size = level_sizes[level];
2251 }
2252 break;
2253 case TT_MULTISAMPLE_2D:
2254 //glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, tb.nr_multi_samples, gl_format, df.get_width(), df.get_height(), map_to_gl(tb.fixed_sample_locations));
2255 error("create mipmaps not implemented for 2D multisample textures", &tb);
2256 result = false;
2257 break;
2258 case TT_2D_ARRAY:
2259 //glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, gl_format, df.get_width(), df.get_height(), df.get_depth(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2260 error("create mipmaps not implemented for 2D array textures", &tb);
2261 result = false;
2262 break;
2263 case TT_MULTISAMPLE_2D_ARRAY:
2264 //glTexStorage3DMultisample(GL_TEXTURE_2D_MULTISAMPLE_ARRAY, tb.nr_multi_samples, gl_format, df.get_width(), df.get_height(), df.get_depth(), map_to_gl(tb.fixed_sample_locations));
2265 error("create mipmaps not implemented for 2D multisample array textures", &tb);
2266 result = false;
2267 break;
2268 case TT_3D:
2269 for(unsigned level = 1; level < num_levels; ++level) {
2270 uvec3 level_size = level_sizes[level];
2272 }
2273 break;
2274 case TT_CUBEMAP:
2275 /*glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0,
2276 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2277 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0,
2278 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2279 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0,
2280 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2281 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0,
2282 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2283 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0,
2284 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2285 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0,
2286 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2287 */
2288 error("create mipmaps not implemented for cubemap textures", &tb);
2289 result = false;
2290 default:
2291 break;
2292 }
2293
2294 if(check_gl_error("gl_context::texture_create_mipmaps", &tb))
2295 result = false;
2296
2297 if(result)
2298 tb.have_mipmaps = true;
2299
2300 texture_unbind(tb.tt, tmp_id);
2301 return result;
2302}
2303
2304bool gl_context::texture_generate_mipmaps(texture_base& tb, unsigned int dim) const
2305{
2306 GLuint tmp_id = texture_bind(tb.tt,get_gl_id(tb.handle));
2307
2308 bool is_array = tb.tt == TT_1D_ARRAY || tb.tt == TT_2D_ARRAY;
2309 bool is_cubemap = tb.tt == TT_CUBEMAP;
2310 std::string error_string;
2311 bool result = generate_mipmaps(dim, is_cubemap, is_array, &error_string);
2312 if (result)
2313 tb.have_mipmaps = true;
2314 else
2315 error(std::string("gl_context::texture_generate_mipmaps: ") + error_string);
2316
2317 texture_unbind(tb.tt, tmp_id);
2318 return result;
2319}
2320
2321bool gl_context::texture_destruct(texture_base& tb) const
2322{
2323 if (!tb.is_created()) {
2324 error("gl_context::texture_destruct: attempt to destruct not created texture", &tb);
2325 return false;
2326 }
2327 GLuint tex_id = get_gl_id(tb.handle);
2328 glDeleteTextures(1, &tex_id);
2329 bool result = !check_gl_error("gl_context::texture_destruct", &tb);
2330 tb.handle = 0;
2331 return result;
2332}
2333
2334bool gl_context::texture_set_state(const texture_base& tb) const
2335{
2336 if (tb.tt == TT_UNDEF) {
2337 error("gl_context::texture_set_state: attempt to set state on texture without type", &tb);
2338 return false;
2339 }
2340 GLuint tex_id = (GLuint&) tb.handle - 1;
2341 if (tex_id == -1) {
2342 error("gl_context::texture_set_state: attempt of setting texture state of not created texture", &tb);
2343 return false;
2344 }
2345 GLint tmp_id = texture_bind(tb.tt, tex_id);
2346
2347 if (tb.tt != TT_MULTISAMPLE_2D && tb.tt != TT_MULTISAMPLE_2D_ARRAY) {
2348 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_MIN_FILTER, map_to_gl(tb.min_filter));
2349 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_MAG_FILTER, map_to_gl(tb.mag_filter));
2350 if (tb.min_filter == TF_ANISOTROP)
2351 glTexParameterf(get_tex_dim(tb.tt), GL_TEXTURE_MAX_ANISOTROPY_EXT, tb.anisotropy);
2352 else
2353 glTexParameterf(get_tex_dim(tb.tt), GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
2354 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_COMPARE_FUNC, map_to_gl(tb.compare_function));
2355 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_COMPARE_MODE, (tb.use_compare_function ? GL_COMPARE_REF_TO_TEXTURE : GL_NONE));
2356 if (!core_profile)
2357 glTexParameterf(get_tex_dim(tb.tt), GL_TEXTURE_PRIORITY, tb.priority);
2358 glTexParameterfv(get_tex_dim(tb.tt), GL_TEXTURE_BORDER_COLOR, tb.border_color);
2359 // if (tb.border_color[0] >= 0.0f)
2360 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_WRAP_S, map_to_gl(tb.wrap_s));
2361 if (tb.tt > TT_1D)
2362 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_WRAP_T, map_to_gl(tb.wrap_t));
2363 if (tb.tt == TT_3D)
2364 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_WRAP_R, map_to_gl(tb.wrap_r));
2365 }
2366
2367 bool result = !check_gl_error("gl_context::texture_set_state", &tb);
2368 texture_unbind(tb.tt, tmp_id);
2369 return result;
2370}
2371
2372bool gl_context::texture_enable(texture_base& tb, int tex_unit, unsigned int dim) const
2373{
2374 if (dim < 1 || dim > 3) {
2375 error("gl_context::texture_enable: invalid texture dimension", &tb);
2376 return false;
2377 }
2378 GLuint tex_id = (GLuint&) tb.handle - 1;
2379 if (tex_id == -1) {
2380 error("gl_context::texture_enable: texture not created", &tb);
2381 return false;
2382 }
2383 if (tex_unit >= 0) {
2384 if (!GLEW_VERSION_1_3) {
2385 error("gl_context::texture_enable: multi texturing not supported", &tb);
2386 return false;
2387 }
2388 glActiveTexture(GL_TEXTURE0+tex_unit);
2389 }
2390 GLint& old_binding = (GLint&) tb.user_data;
2391 glGetIntegerv(get_tex_bind(tb.tt), &old_binding);
2392 ++old_binding;
2393 glBindTexture(get_tex_dim(tb.tt), tex_id);
2394 // glEnable is not needed for texture arrays and will throw an invalid enum error
2395 //if(!(tb.tt == TT_1D_ARRAY || tb.tt == TT_2D_ARRAY))
2396 // glEnable(get_tex_dim(tb.tt));
2397 bool result = !check_gl_error("gl_context::texture_enable", &tb);
2398 if (tex_unit >= 0)
2400 return result;
2401}
2402
2403bool gl_context::texture_disable(
2404 texture_base& tb,
2405 int tex_unit, unsigned int dim) const
2406{
2407 if (dim < 1 || dim > 3) {
2408 error("gl_context::texture_disable: invalid texture dimension", &tb);
2409 return false;
2410 }
2411 if (tex_unit == -2) {
2412 error("gl_context::texture_disable: invalid texture unit", &tb);
2413 return false;
2414 }
2415 GLuint old_binding = (const GLuint&) tb.user_data;
2416 --old_binding;
2417 if (tex_unit >= 0)
2418 glActiveTexture(GL_TEXTURE0+tex_unit);
2419 // glDisable is not needed for texture arrays and will throw an invalid enum error
2420 //if(!(tb.tt == TT_1D_ARRAY || tb.tt == TT_2D_ARRAY))
2421 // glDisable(get_tex_dim(tb.tt));
2422 bool result = !check_gl_error("gl_context::texture_disable", &tb);
2423 glBindTexture(get_tex_dim(tb.tt), old_binding);
2424 if (tex_unit >= 0)
2426 return result;
2427}
2428
2429bool gl_context::texture_bind_as_image(texture_base& tb, int tex_unit, int level, bool bind_array, int layer, AccessType access) const
2430{
2431 GLuint tex_id = (GLuint&)tb.handle - 1;
2432 if(tex_id == -1) {
2433 error("gl_context::texture_enable: texture not created", &tb);
2434 return false;
2435 }
2436
2437 if(!GLEW_VERSION_4_2) {
2438 error("gl_context::texture_bind_as_image: image textures not supported", &tb);
2439 return false;
2440 }
2441
2442 GLuint gl_format = (const GLuint&)tb.internal_format;
2443 glBindImageTexture(tex_unit, tex_id, level, map_to_gl(bind_array), layer, map_to_gl(access), gl_format);
2444
2445 bool result = !check_gl_error("gl_context::texture_bind_as_image", &tb);
2446 return result;
2447}
2448
2449bool gl_context::render_buffer_create(render_buffer_base& rb, cgv::data::component_format& cf, int& _width, int& _height) const
2450{
2451 if (!GLEW_VERSION_3_0) {
2452 error("gl_context::render_buffer_create: frame buffer objects not supported", &rb);
2453 return false;
2454 }
2455 if (_width == -1)
2456 _width = get_width();
2457 if (_height == -1)
2458 _height = get_height();
2459
2460 GLuint rb_id;
2463
2464 GLuint& gl_format = (GLuint&)rb.internal_format;
2465 unsigned i = find_best_match(cf, color_buffer_formats);
2466 cgv::data::component_format best_cf(color_buffer_formats[i]);
2467 gl_format = gl_color_buffer_format_ids[i];
2468
2469 i = find_best_match(cf, depth_formats, &best_cf);
2470 if (i != -1) {
2471 best_cf = cgv::data::component_format(depth_formats[i]);
2472 gl_format = gl_depth_format_ids[i];
2473 }
2474
2475 cf = best_cf;
2476 if (rb.nr_multi_samples == 0)
2478 else
2480
2481 if (check_gl_error("gl_context::render_buffer_create", &rb))
2482 return false;
2483 rb.handle = get_handle(rb_id);
2484 return true;
2485}
2486
2487bool gl_context::render_buffer_destruct(render_buffer_base& rc) const
2488{
2489 if (!GLEW_VERSION_3_0) {
2490 error("gl_context::render_buffer_destruct: frame buffer objects not supported", &rc);
2491 return false;
2492 }
2493 GLuint rb_id = ((GLuint&) rc.handle)+1;
2495 if (check_gl_error("gl_context::render_buffer_destruct", &rc))
2496 return false;
2497 rc.handle = 0;
2498 return true;
2499}
2500
2501bool gl_context::frame_buffer_create(frame_buffer_base& fbb) const
2502{
2503 if (!check_fbo_support("gl_context::frame_buffer_create", &fbb))
2504 return false;
2505
2506 if (!context::frame_buffer_create(fbb))
2507 return false;
2508
2509 // allocate framebuffer object
2510 GLuint fbo_id = 0;
2511 glGenFramebuffers(1, &fbo_id);
2512 if (fbo_id == 0) {
2513 error("gl_context::frame_buffer_create: could not allocate framebuffer object", &fbb);
2514 return false;
2515 }
2516 fbb.handle = get_handle(fbo_id);
2517 return true;
2518}
2519
2520bool gl_context::frame_buffer_enable(frame_buffer_base& fbb)
2521{
2522 if (!context::frame_buffer_enable(fbb))
2523 return false;
2524 std::vector<int> buffers;
2525 bool depth_buffer = false;
2526 get_buffer_list(fbb, depth_buffer, buffers, GL_COLOR_ATTACHMENT0);
2527 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbb.handle));
2528
2529 if (buffers.size() == 1)
2530 glDrawBuffer(buffers[0]);
2531 else if (buffers.size() > 1) {
2532 glDrawBuffers(GLsizei(buffers.size()), reinterpret_cast<GLenum*>(&buffers[0]));
2533 }
2534 else if(depth_buffer) {
2536 //glReadBuffer(GL_NONE);
2537 } else {
2538 error("gl_context::frame_buffer_enable: no attached draw buffer selected!!", &fbb);
2539 return false;
2540 }
2541 return true;
2542}
2543
2546{
2547 if (!context::frame_buffer_disable(fbb))
2548 return false;
2549 if (frame_buffer_stack.empty()) {
2550 error("gl_context::frame_buffer_disable called with empty frame buffer stack!!", &fbb);
2551 return false;
2552 }
2553 else
2554 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(frame_buffer_stack.top()->handle));
2555 return true;
2556}
2557
2558bool gl_context::frame_buffer_destruct(frame_buffer_base& fbb) const
2559{
2560 if (!context::frame_buffer_destruct(fbb))
2561 return false;
2562 GLuint fbo_id = get_gl_id(fbb.handle);
2563 glDeleteFramebuffers(1, &fbo_id);
2564 fbb.handle = 0;
2565 return true;
2566}
2567
2568void complete_rect_from_vp(ivec4& D, GLint vp[4])
2569{
2570 if (D(0) == -1)
2571 D(0) = vp[0];
2572 if (D(1) == -1)
2573 D(1) = vp[1];
2574 if (D(2) == -1)
2575 D(2) = vp[0] + vp[2];
2576 if (D(3) == -1)
2577 D(3) = vp[1] + vp[3];
2578}
2579
2580void gl_context::frame_buffer_blit(
2581 const frame_buffer_base* src_fbb_ptr, const ivec4& _S,
2582 frame_buffer_base* dst_fbb_ptr, const ivec4& _D, BufferTypeBits btbs, bool interpolate) const
2583{
2584 static const GLenum masks[8]{
2585 0,
2593 };
2594 ivec4 S = _S;
2595 ivec4 D = _D;
2596 if ((src_fbb_ptr == 0 && (S(0) == -1 || S(1) == -1 || S(2) == -1 || S(3) == -1)) ||
2597 (dst_fbb_ptr == 0 && (D(0) == -1 || D(1) == -1 || D(2) == -1 || D(3) == -1))) {
2598 GLint vp[4];
2600 if (src_fbb_ptr == 0)
2601 complete_rect_from_vp(S, vp);
2602 if (dst_fbb_ptr == 0)
2603 complete_rect_from_vp(D, vp);
2604 }
2606 if (src_fbb_ptr) {
2609 }
2610 if (dst_fbb_ptr) {
2613 }
2614 glBlitFramebuffer(S(0), S(1), S(2), S(3), D(0), D(1), D(2), D(3), masks[btbs], interpolate ? GL_LINEAR : GL_NEAREST);
2615 if (src_fbb_ptr)
2617 if (dst_fbb_ptr)
2619}
2620
2621bool gl_context::frame_buffer_attach(frame_buffer_base& fbb, const render_buffer_base& rb, bool is_depth, int i) const
2622{
2623 if (!context::frame_buffer_attach(fbb, rb, is_depth, i))
2624 return false;
2627 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbb.handle));
2631 get_gl_id(rb.handle));
2633 return true;
2634}
2635
2637bool gl_context::frame_buffer_attach(frame_buffer_base& fbb,
2638 const texture_base& t, bool is_depth,
2639 int level, int i, int z_or_cube_side) const
2640{
2641 if (!context::frame_buffer_attach(fbb, t, is_depth, level, i, z_or_cube_side))
2642 return false;
2645 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbb.handle));
2646
2647 if (z_or_cube_side == -1) {
2650 t.tt == TT_2D ? GL_TEXTURE_2D : GL_TEXTURE_2D_MULTISAMPLE, get_gl_id(t.handle), level);
2651 }
2652 else {
2653 if (t.tt == TT_CUBEMAP) {
2656 get_gl_cube_map_target(z_or_cube_side), get_gl_id(t.handle), level);
2657 }
2658 else {
2661 t.tt == TT_3D ? GL_TEXTURE_3D : GL_TEXTURE_2D_MULTISAMPLE_ARRAY , get_gl_id(t.handle), level, z_or_cube_side);
2662 }
2663 }
2664 bool result = !check_gl_error("gl_context::frame_buffer_attach", &fbb);
2666 return result;
2667}
2668
2671{
2672 if (fbb.handle == 0) {
2673 error("gl_context::frame_buffer_is_complete: attempt to check completeness on frame buffer that is not created", &fbb);
2674 return false;
2675 }
2678 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbb.handle));
2681 switch (error) {
2683 return true;
2685 fbb.last_error = "undefined framebuffer";
2686 return false;
2688 fbb.last_error = "incomplete attachment";
2689 return false;
2691 fbb.last_error = "incomplete or missing attachment";
2692 return false;
2694 fbb.last_error = "incomplete multisample";
2695 return false;
2697 fbb.last_error = "incomplete layer targets";
2698 return false;
2700 fbb.last_error = "incomplete draw buffer";
2701 return false;
2703 fbb.last_error = "incomplete read buffer";
2704 return false;
2706 fbb.last_error = "framebuffer objects unsupported";
2707 return false;
2708 }
2709 fbb.last_error = "unknown error";
2710 return false;
2711}
2712
2713int gl_context::frame_buffer_get_max_nr_color_attachments() const
2714{
2715 if (!check_fbo_support("gl_context::frame_buffer_get_max_nr_color_attachments"))
2716 return 0;
2717
2718 GLint nr;
2720 return nr;
2721}
2722
2723int gl_context::frame_buffer_get_max_nr_draw_buffers() const
2724{
2725 if (!check_fbo_support("gl_context::frame_buffer_get_max_nr_draw_buffers"))
2726 return 0;
2727
2728 GLint nr;
2730 return nr;
2731}
2732
2733GLuint gl_shader_type[] =
2734{
2736};
2737
2738void gl_context::shader_code_destruct(render_component& sc) const
2739{
2740 if (sc.handle == 0) {
2741 error("gl_context::shader_code_destruct: shader not created", &sc);
2742 return;
2743 }
2744 glDeleteShader(get_gl_id(sc.handle));
2745 check_gl_error("gl_context::shader_code_destruct", &sc);
2746}
2747
2748bool gl_context::shader_code_create(render_component& sc, ShaderType st, const std::string& source) const
2749{
2750 if (!check_shader_support(st, "gl_context::shader_code_create", &sc))
2751 return false;
2752
2753 GLuint s_id = glCreateShader(gl_shader_type[st]);
2754 if (s_id == -1) {
2755 error(std::string("gl_context::shader_code_create: ")+gl_error(), &sc);
2756 return false;
2757 }
2758 sc.handle = get_handle(s_id);
2759
2760 const char* s = source.c_str();
2761 glShaderSource(s_id, 1, &s,NULL);
2762 if (check_gl_error("gl_context::shader_code_create", &sc))
2763 return false;
2764
2765 return true;
2766}
2767
2768bool gl_context::shader_code_compile(render_component& sc) const
2769{
2770 if (sc.handle == 0) {
2771 error("gl_context::shader_code_compile: shader not created", &sc);
2772 return false;
2773 }
2774 GLuint s_id = get_gl_id(sc.handle);
2776 int result;
2778 if (result == 1)
2779 return true;
2780 sc.last_error = std::string();
2781 GLint infologLength = 0;
2783 if (infologLength > 0) {
2784 int charsWritten = 0;
2785 char *infoLog = (char *)malloc(infologLength);
2787 sc.last_error = infoLog;
2788 free(infoLog);
2789 }
2790 return false;
2791}
2792
2793bool gl_context::shader_program_create(shader_program_base& spb) const
2794{
2795 if (!check_shader_support(ST_VERTEX, "gl_context::shader_program_create", &spb))
2796 return false;
2797 spb.handle = get_handle(glCreateProgram());
2798 return true;
2799}
2800
2801void gl_context::shader_program_attach(shader_program_base& spb, const render_component& sc) const
2802{
2803 if (spb.handle == 0) {
2804 error("gl_context::shader_program_attach: shader program not created", &spb);
2805 return;
2806 }
2807 glAttachShader(get_gl_id(spb.handle), get_gl_id(sc.handle));
2808}
2809
2810void gl_context::shader_program_detach(shader_program_base& spb, const render_component& sc) const
2811{
2812 if (spb.handle == 0) {
2813 error("gl_context::shader_program_detach: shader program not created", &spb);
2814 return;
2815 }
2816 glDetachShader(get_gl_id(spb.handle), get_gl_id(sc.handle));
2817}
2818
2819bool gl_context::shader_program_link(shader_program_base& spb) const
2820{
2821 if (spb.handle == 0) {
2822 error("gl_context::shader_program_link: shader program not created", &spb);
2823 return false;
2824 }
2825 GLuint p_id = get_gl_id(spb.handle);
2827 int result;
2829 if (result == 1)
2830 return context::shader_program_link(spb);
2831 GLint infologLength = 0;
2833 if (infologLength > 0) {
2835 char *infoLog = (char *)malloc(infologLength);
2837 spb.last_error = infoLog;
2838 error(std::string("gl_context::shader_program_link\n")+infoLog, &spb);
2839 free(infoLog);
2840 }
2841 return false;
2842}
2843
2844bool gl_context::shader_program_set_state(shader_program_base& spb) const
2845{
2846 if (spb.handle == 0) {
2847 error("gl_context::shader_program_set_state: shader program not created", &spb);
2848 return false;
2849 }
2850 GLuint p_id = get_gl_id(spb.handle);
2851 glProgramParameteri(p_id, GL_GEOMETRY_VERTICES_OUT_ARB, spb.geometry_shader_output_count);
2852 glProgramParameteri(p_id, GL_GEOMETRY_INPUT_TYPE_ARB, map_to_gl(spb.geometry_shader_input_type));
2853 glProgramParameteri(p_id, GL_GEOMETRY_OUTPUT_TYPE_ARB, map_to_gl(spb.geometry_shader_output_type));
2854 return true;
2855}
2856
2857bool gl_context::shader_program_enable(shader_program_base& spb)
2858{
2859 if (!context::shader_program_enable(spb))
2860 return false;
2861 glUseProgram(get_gl_id(spb.handle));
2862 shader_program& prog = static_cast<shader_program&>(spb);
2863 if (auto_set_lights_in_current_shader_program && spb.does_use_lights())
2864 set_current_lights(prog);
2865 if (auto_set_material_in_current_shader_program && spb.does_use_material())
2867 if (auto_set_view_in_current_shader_program && spb.does_use_view())
2868 set_current_view(prog);
2869 if (auto_set_gamma_in_current_shader_program && spb.does_use_gamma())
2870 set_current_gamma(prog);
2871 if (prog.does_context_set_color() && prog.get_color_index() >= 0)
2872 prog.set_attribute(*this, prog.get_color_index(), current_color);
2873 return true;
2874}
2875
2876bool gl_context::shader_program_disable(shader_program_base& spb)
2877{
2878 if (!context::shader_program_disable(spb))
2879 return false;
2880 if (shader_program_stack.empty())
2881 glUseProgram(0);
2882 else
2883 glUseProgram(get_gl_id(shader_program_stack.top()->handle));
2884 return true;
2885}
2886
2887bool gl_context::shader_program_destruct(shader_program_base& spb) const
2888{
2889 if (!context::shader_program_destruct(spb))
2890 return false;
2891 glDeleteProgram(get_gl_id(spb.handle));
2892 return true;
2893}
2894
2895bool gl_context::shader_program_get_active_uniforms(shader_program_base& spb, std::vector<std::string>& names) const
2896{
2897 if (spb.handle == 0)
2898 return false;
2899
2900 GLuint p_id = get_gl_id(spb.handle);
2901
2904
2905 names.reserve(num_active_uniforms);
2906
2907 std::vector<GLchar> buffer(256);
2908 for (int i = 0; i < num_active_uniforms; ++i) {
2909 GLint array_size = 0;
2910 GLenum type = 0;
2912
2913 glGetActiveUniform(p_id, i, buffer.size(), &actual_length, &array_size, &type, buffer.data());
2914 std::string name(static_cast<char*>(buffer.data()), actual_length);
2915
2916 // Uniforms for arrays of non-compound (non-struct) types are listed once with a "[0]" suffix and a given array size greater than 1.
2917 if(array_size > 1) {
2918 // Remove the brackets to get the base name of the uniform
2919 size_t bracket_pos = name.find('[');
2920 if(bracket_pos != std::string::npos)
2921 name.resize(bracket_pos);
2922
2923 if(!name.empty()) {
2924 // Store the name without the brackets to allow setting complete arrays using just the uniform name.
2925 names.push_back(name);
2926 // Additionally store an entry for every possible indexed name to allow setting elements individually.
2927 for(GLint i = 0; i < array_size; ++i)
2928 names.push_back(name + "[" + std::to_string(i) + "]");
2929 }
2930 } else {
2931 if(!name.empty())
2932 names.push_back(name);
2933 }
2934 }
2935 return true;
2936}
2937
2938int gl_context::get_uniform_location(const shader_program_base& spb, const std::string& name) const
2939{
2940 return glGetUniformLocation(get_gl_id(spb.handle), name.c_str());
2941}
2942
2943std::string value_type_index_to_string(type_descriptor td)
2944{
2945 std::string res = cgv::type::info::get_type_name(td.coordinate_type);
2946 switch (td.element_type) {
2947 case ET_VECTOR:
2948 res = std::string("vector<") + res + "," + cgv::utils::to_string(td.nr_rows) + ">";
2949 break;
2950 case ET_MATRIX:
2951 res = std::string("matrix<") + res + "," + cgv::utils::to_string(td.nr_rows) + "," + cgv::utils::to_string(td.nr_columns) + ">";
2952 if (td.is_row_major)
2953 res += "^T";
2954 default:
2955 break;
2956 }
2957 if (td.is_array)
2958 res += "[]";
2959 return res;
2960}
2961
2962bool gl_context::set_uniform_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const
2963{
2964 if (value_type.is_array) {
2965 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") array type not supported, please use set_uniform_array instead.", &spb);
2966 return false;
2967 }
2968 if (!spb.handle) {
2969 error("gl_context::set_uniform_void() called on not created program", &spb);
2970 return false;
2971 }
2972 bool not_current = shader_program_stack.empty() || shader_program_stack.top() != &spb;
2973 if (not_current)
2974 glUseProgram(get_gl_id(spb.handle));
2975 bool res = true;
2976 switch (value_type.element_type) {
2977 case ET_VALUE:
2978 switch (value_type.coordinate_type) {
2979 case TI_BOOL: glUniform1i(loc, *reinterpret_cast<const bool*>(value_ptr) ? 1 : 0); break;
2980 case TI_UINT8: glUniform1ui(loc, *reinterpret_cast<const uint8_type*>(value_ptr)); break;
2981 case TI_UINT16: glUniform1ui(loc, *reinterpret_cast<const uint16_type*>(value_ptr)); break;
2982 case TI_UINT32: glUniform1ui(loc, *reinterpret_cast<const uint32_type*>(value_ptr)); break;
2983 case TI_INT8: glUniform1i(loc, *reinterpret_cast<const int8_type*>(value_ptr)); break;
2984 case TI_INT16: glUniform1i(loc, *reinterpret_cast<const int16_type*>(value_ptr)); break;
2985 case TI_INT32: glUniform1i(loc, *reinterpret_cast<const int32_type*>(value_ptr)); break;
2986 case TI_FLT32: glUniform1f(loc, *reinterpret_cast<const flt32_type*>(value_ptr)); break;
2987 case TI_FLT64: glUniform1d(loc, *reinterpret_cast<const flt64_type*>(value_ptr)); break;
2988 default:
2989 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
2990 res = false; break;
2991 }
2992 break;
2993 case ET_VECTOR:
2994 switch (value_type.nr_rows) {
2995 case 2:
2996 switch (value_type.coordinate_type) {
2997 case TI_BOOL: glUniform2i(loc, reinterpret_cast<const bool*>(value_ptr)[0] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[1] ? 1 : 0); break;
2998 case TI_UINT8: glUniform2ui(loc, reinterpret_cast<const uint8_type*> (value_ptr)[0], reinterpret_cast<const uint8_type*> (value_ptr)[1]); break;
2999 case TI_UINT16: glUniform2ui(loc, reinterpret_cast<const uint16_type*>(value_ptr)[0], reinterpret_cast<const uint16_type*>(value_ptr)[1]); break;
3000 case TI_UINT32: glUniform2ui(loc, reinterpret_cast<const uint32_type*>(value_ptr)[0], reinterpret_cast<const uint32_type*>(value_ptr)[1]); break;
3001 case TI_INT8: glUniform2i(loc, reinterpret_cast<const int8_type*> (value_ptr)[0], reinterpret_cast<const int8_type*> (value_ptr)[1]); break;
3002 case TI_INT16: glUniform2i(loc, reinterpret_cast<const int16_type*> (value_ptr)[0], reinterpret_cast<const int16_type*> (value_ptr)[1]); break;
3003 case TI_INT32: glUniform2i(loc, reinterpret_cast<const int32_type*> (value_ptr)[0], reinterpret_cast<const int32_type*> (value_ptr)[1]); break;
3004 case TI_FLT32: glUniform2f(loc, reinterpret_cast<const flt32_type*> (value_ptr)[0], reinterpret_cast<const flt32_type*> (value_ptr)[1]); break;
3005 case TI_FLT64: glUniform2d(loc, reinterpret_cast<const flt64_type*> (value_ptr)[0], reinterpret_cast<const flt64_type*> (value_ptr)[1]); break;
3006 default:
3007 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3008 res = false; break;
3009 }
3010 break;
3011 case 3:
3012 switch (value_type.coordinate_type) {
3013 case TI_BOOL: glUniform3i(loc, reinterpret_cast<const bool*>(value_ptr)[0] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[1] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[2] ? 1 : 0); break;
3014 case TI_UINT8: glUniform3ui(loc, reinterpret_cast<const uint8_type*> (value_ptr)[0], reinterpret_cast<const uint8_type*> (value_ptr)[1], reinterpret_cast<const uint8_type*> (value_ptr)[2]); break;
3015 case TI_UINT16: glUniform3ui(loc, reinterpret_cast<const uint16_type*>(value_ptr)[0], reinterpret_cast<const uint16_type*>(value_ptr)[1], reinterpret_cast<const uint16_type*>(value_ptr)[2]); break;
3016 case TI_UINT32: glUniform3ui(loc, reinterpret_cast<const uint32_type*>(value_ptr)[0], reinterpret_cast<const uint32_type*>(value_ptr)[1], reinterpret_cast<const uint32_type*>(value_ptr)[2]); break;
3017 case TI_INT8: glUniform3i(loc, reinterpret_cast<const int8_type*> (value_ptr)[0], reinterpret_cast<const int8_type*> (value_ptr)[1], reinterpret_cast<const int8_type*> (value_ptr)[2]); break;
3018 case TI_INT16: glUniform3i(loc, reinterpret_cast<const int16_type*> (value_ptr)[0], reinterpret_cast<const int16_type*> (value_ptr)[1], reinterpret_cast<const int16_type*> (value_ptr)[2]); break;
3019 case TI_INT32: glUniform3i(loc, reinterpret_cast<const int32_type*> (value_ptr)[0], reinterpret_cast<const int32_type*> (value_ptr)[1], reinterpret_cast<const int32_type*> (value_ptr)[2]); break;
3020 case TI_FLT32: glUniform3f(loc, reinterpret_cast<const flt32_type*> (value_ptr)[0], reinterpret_cast<const flt32_type*> (value_ptr)[1], reinterpret_cast<const flt32_type*> (value_ptr)[2]); break;
3021 case TI_FLT64: glUniform3d(loc, reinterpret_cast<const flt64_type*> (value_ptr)[0], reinterpret_cast<const flt64_type*> (value_ptr)[1], reinterpret_cast<const flt64_type*> (value_ptr)[2]); break;
3022 default:
3023 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3024 res = false; break;
3025 }
3026 break;
3027 case 4:
3028 switch (value_type.coordinate_type) {
3029 case TI_BOOL: glUniform4i(loc, reinterpret_cast<const bool*>(value_ptr)[0] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[1] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[2] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[3] ? 1 : 0); break;
3030 case TI_UINT8: glUniform4ui(loc, reinterpret_cast<const uint8_type*> (value_ptr)[0], reinterpret_cast<const uint8_type*> (value_ptr)[1], reinterpret_cast<const uint8_type*> (value_ptr)[2], reinterpret_cast<const uint8_type*> (value_ptr)[3]); break;
3031 case TI_UINT16: glUniform4ui(loc, reinterpret_cast<const uint16_type*>(value_ptr)[0], reinterpret_cast<const uint16_type*>(value_ptr)[1], reinterpret_cast<const uint16_type*>(value_ptr)[2], reinterpret_cast<const uint16_type*>(value_ptr)[3]); break;
3032 case TI_UINT32: glUniform4ui(loc, reinterpret_cast<const uint32_type*>(value_ptr)[0], reinterpret_cast<const uint32_type*>(value_ptr)[1], reinterpret_cast<const uint32_type*>(value_ptr)[2], reinterpret_cast<const uint32_type*>(value_ptr)[3]); break;
3033 case TI_INT8: glUniform4i(loc, reinterpret_cast<const int8_type*> (value_ptr)[0], reinterpret_cast<const int8_type*> (value_ptr)[1], reinterpret_cast<const int8_type*> (value_ptr)[2], reinterpret_cast<const int8_type*> (value_ptr)[3]); break;
3034 case TI_INT16: glUniform4i(loc, reinterpret_cast<const int16_type*> (value_ptr)[0], reinterpret_cast<const int16_type*> (value_ptr)[1], reinterpret_cast<const int16_type*> (value_ptr)[2], reinterpret_cast<const int16_type*> (value_ptr)[3]); break;
3035 case TI_INT32: glUniform4i(loc, reinterpret_cast<const int32_type*> (value_ptr)[0], reinterpret_cast<const int32_type*> (value_ptr)[1], reinterpret_cast<const int32_type*> (value_ptr)[2], reinterpret_cast<const int32_type*> (value_ptr)[3]); break;
3036 case TI_FLT32: glUniform4f(loc, reinterpret_cast<const flt32_type*> (value_ptr)[0], reinterpret_cast<const flt32_type*> (value_ptr)[1], reinterpret_cast<const flt32_type*> (value_ptr)[2], reinterpret_cast<const flt32_type*> (value_ptr)[3]); break;
3037 case TI_FLT64: glUniform4d(loc, reinterpret_cast<const flt64_type*> (value_ptr)[0], reinterpret_cast<const flt64_type*> (value_ptr)[1], reinterpret_cast<const flt64_type*> (value_ptr)[2], reinterpret_cast<const flt64_type*> (value_ptr)[3]); break;
3038 default:
3039 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3040 res = false; break;
3041 }
3042 break;
3043 }
3044 break;
3045 case ET_MATRIX:
3046 switch (value_type.coordinate_type) {
3047 case TI_FLT32:
3048 switch (value_type.nr_rows) {
3049 case 2:
3050 switch (value_type.nr_columns) {
3051 case 2: glUniformMatrix2fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3052 case 3: glUniformMatrix2x3fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3053 case 4: glUniformMatrix2x4fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3054 default:
3055 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3056 res = false; break;
3057 }
3058 break;
3059 case 3:
3060 switch (value_type.nr_columns) {
3061 case 2: glUniformMatrix3x2fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3062 case 3: glUniformMatrix3fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3063 case 4: glUniformMatrix3x4fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3064 default:
3065 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3066 res = false; break;
3067 }
3068 break;
3069 case 4:
3070 switch (value_type.nr_columns) {
3071 case 2: glUniformMatrix4x2fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3072 case 3: glUniformMatrix4x3fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3073 case 4: glUniformMatrix4fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3074 default:
3075 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3076 res = false; break;
3077 }
3078 break;
3079 default:
3080 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of rows outside [2,..,4].", &spb);
3081 res = false; break;
3082 }
3083 break;
3084 case TI_FLT64:
3085 switch (value_type.nr_rows) {
3086 case 2:
3087 switch (value_type.nr_columns) {
3088 case 2: glUniformMatrix2dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3089 case 3: glUniformMatrix2x3dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3090 case 4: glUniformMatrix2x4dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3091 default:
3092 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3093 res = false; break;
3094 }
3095 break;
3096 case 3:
3097 switch (value_type.nr_columns) {
3098 case 2: glUniformMatrix3x2dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3099 case 3: glUniformMatrix3dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3100 case 4: glUniformMatrix3x4dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3101 default:
3102 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3103 res = false; break;
3104 }
3105 break;
3106 case 4:
3107 switch (value_type.nr_columns) {
3108 case 2: glUniformMatrix4x2dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3109 case 3: glUniformMatrix4x3dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3110 case 4: glUniformMatrix4dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3111 default:
3112 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3113 res = false; break;
3114 }
3115 break;
3116 default:
3117 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of rows outside [2,..,4].", &spb);
3118 res = false; break;
3119 }
3120 break;
3121 default:
3122 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") non float coordinate type not supported.", &spb);
3123 res = false; break;
3124 }
3125 break;
3126 }
3127 if (not_current)
3128 glUseProgram(shader_program_stack.empty() ? 0 : get_gl_id(shader_program_stack.top()->handle));
3129
3130 if (check_gl_error("gl_context::set_uniform_void()", &spb))
3131 res = false;
3132 return res;
3133}
3134
3135bool gl_context::set_uniform_array_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr, size_t nr_elements) const
3136{
3137 if (!value_type.is_array) {
3138 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") non array type not allowed.", &spb);
3139 return false;
3140 }
3141 if (!spb.handle) {
3142 error("gl_context::set_uniform_array_void() called on not created program", &spb);
3143 return false;
3144 }
3145 bool not_current = shader_program_stack.empty() || shader_program_stack.top() != &spb;
3146 if (not_current)
3147 glUseProgram(get_gl_id(spb.handle));
3148 bool res = true;
3149 switch (value_type.coordinate_type) {
3150 case TI_INT32:
3151 switch (value_type.element_type) {
3152 case ET_VALUE:
3153 glUniform1iv(loc, GLsizei(nr_elements), reinterpret_cast<const int32_type*>(value_ptr));
3154 break;
3155 case ET_VECTOR:
3156 switch (value_type.nr_rows) {
3157 case 2: glUniform2iv(loc, GLsizei(nr_elements), reinterpret_cast<const int32_type*>(value_ptr)); break;
3158 case 3: glUniform3iv(loc, GLsizei(nr_elements), reinterpret_cast<const int32_type*>(value_ptr)); break;
3159 case 4: glUniform4iv(loc, GLsizei(nr_elements), reinterpret_cast<const int32_type*>(value_ptr)); break;
3160 default:
3161 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") vector dimension outside [2,..4].", &spb);
3162 res = false;
3163 break;
3164 }
3165 break;
3166 case ET_MATRIX:
3167 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") type not supported.", &spb);
3168 res = false;
3169 break;
3170 }
3171 break;
3172 case TI_UINT32:
3173 switch (value_type.element_type) {
3174 case ET_VALUE:
3175 glUniform1uiv(loc, GLsizei(nr_elements), reinterpret_cast<const uint32_type*>(value_ptr));
3176 break;
3177 case ET_VECTOR:
3178 switch (value_type.nr_rows) {
3179 case 2: glUniform2uiv(loc, GLsizei(nr_elements), reinterpret_cast<const uint32_type*>(value_ptr)); break;
3180 case 3: glUniform3uiv(loc, GLsizei(nr_elements), reinterpret_cast<const uint32_type*>(value_ptr)); break;
3181 case 4: glUniform4uiv(loc, GLsizei(nr_elements), reinterpret_cast<const uint32_type*>(value_ptr)); break;
3182 default:
3183 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") vector dimension outside [2,..4].", &spb);
3184 res = false;
3185 break;
3186 }
3187 break;
3188 case ET_MATRIX:
3189 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") type not supported.", &spb);
3190 res = false;
3191 break;
3192 }
3193 break;
3194 case TI_FLT32:
3195 switch (value_type.element_type) {
3196 case ET_VALUE:
3197 glUniform1fv(loc, GLsizei(nr_elements), reinterpret_cast<const flt32_type*>(value_ptr));
3198 break;
3199 case ET_VECTOR:
3200 switch (value_type.nr_rows) {
3201 case 2: glUniform2fv(loc, GLsizei(nr_elements), reinterpret_cast<const flt32_type*>(value_ptr)); break;
3202 case 3: glUniform3fv(loc, GLsizei(nr_elements), reinterpret_cast<const flt32_type*>(value_ptr)); break;
3203 case 4: glUniform4fv(loc, GLsizei(nr_elements), reinterpret_cast<const flt32_type*>(value_ptr)); break;
3204 default:
3205 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") vector dimension outside [2,..4].", &spb);
3206 res = false;
3207 break;
3208 }
3209 break;
3210 case ET_MATRIX:
3211 switch (value_type.nr_rows) {
3212 case 2:
3213 switch (value_type.nr_columns) {
3214 case 2: glUniformMatrix2fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3215 case 3: glUniformMatrix2x3fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3216 case 4: glUniformMatrix2x4fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3217 default:
3218 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..4].", &spb);
3219 res = false;
3220 break;
3221 }
3222 break;
3223 case 3:
3224 switch (value_type.nr_columns) {
3225 case 2: glUniformMatrix3x2fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3226 case 3: glUniformMatrix3fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3227 case 4: glUniformMatrix3x4fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3228 default:
3229 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..4].", &spb);
3230 res = false;
3231 break;
3232 }
3233 break;
3234 case 4:
3235 switch (value_type.nr_columns) {
3236 case 2: glUniformMatrix4x2fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3237 case 3: glUniformMatrix4x3fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3238 case 4: glUniformMatrix4fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3239 default:
3240 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..4].", &spb);
3241 res = false;
3242 break;
3243 }
3244 break;
3245 default:
3246 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") matrix number of rows outside [2,..4].", &spb);
3247 res = false;
3248 break;
3249 }
3250 break;
3251 }
3252 break;
3253 default:
3254 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type (only int32, uint32, and flt32 supported).", &spb);
3255 res = false;
3256 break;
3257 }
3258 if (check_gl_error("gl_context::set_uniform_array_void()", &spb))
3259 res = false;
3260
3261 if (not_current)
3262 glUseProgram(shader_program_stack.empty() ? 0 : get_gl_id(shader_program_stack.top()->handle));
3263
3264 return res;
3265}
3266
3267int gl_context::get_attribute_location(const shader_program_base& spb, const std::string& name) const
3268{
3269 GLint loc = glGetAttribLocation(get_gl_id(spb.handle), name.c_str());
3270 return loc;
3271}
3272
3273bool gl_context::set_attribute_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const
3274{
3275 if (!spb.handle) {
3276 error("gl_context::set_attribute_void() called on not created program", &spb);
3277 return false;
3278 }
3279 bool not_current = shader_program_stack.empty() || shader_program_stack.top() != &spb;
3280 if (not_current)
3281 glUseProgram(get_gl_id(spb.handle));
3282 bool res = true;
3283 switch (value_type.element_type) {
3284 case ET_VALUE:
3285 switch (value_type.coordinate_type) {
3286 case TI_BOOL: glVertexAttrib1s(loc, *reinterpret_cast<const bool*>(value_ptr) ? 1 : 0); break;
3287 case TI_INT8: glVertexAttrib1s(loc, *reinterpret_cast<const int8_type*>(value_ptr)); break;
3288 case TI_INT16: glVertexAttrib1s(loc, *reinterpret_cast<const int16_type*>(value_ptr)); break;
3289 case TI_INT32: glVertexAttribI1i(loc, *reinterpret_cast<const int32_type*>(value_ptr)); break;
3290 case TI_UINT8: glVertexAttrib1s(loc, *reinterpret_cast<const uint8_type*>(value_ptr)); break;
3291 case TI_UINT16: glVertexAttribI1ui(loc, *reinterpret_cast<const uint16_type*>(value_ptr)); break;
3292 case TI_UINT32: glVertexAttribI1ui(loc, *reinterpret_cast<const uint32_type*>(value_ptr)); break;
3293 case TI_FLT32: glVertexAttrib1f(loc, *reinterpret_cast<const flt32_type*>(value_ptr)); break;
3294 case TI_FLT64: glVertexAttrib1d(loc, *reinterpret_cast<const flt64_type*>(value_ptr)); break;
3295 default:
3296 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") type not supported!", &spb);
3297 res = false;
3298 break;
3299 }
3300 break;
3301 case ET_VECTOR:
3302 switch (value_type.nr_rows) {
3303 case 2:
3304 switch (value_type.coordinate_type) {
3305 case TI_BOOL: glVertexAttrib2s(loc, reinterpret_cast<const bool*>(value_ptr)[0] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[1] ? 1 : 0); break;
3306 case TI_UINT8: glVertexAttrib2s(loc, reinterpret_cast<const uint8_type*> (value_ptr)[0], reinterpret_cast<const uint8_type*> (value_ptr)[1]); break;
3307 case TI_UINT16: glVertexAttribI2ui(loc, reinterpret_cast<const uint16_type*>(value_ptr)[0], reinterpret_cast<const uint16_type*>(value_ptr)[1]); break;
3308 case TI_UINT32: glVertexAttribI2ui(loc, reinterpret_cast<const uint32_type*>(value_ptr)[0], reinterpret_cast<const uint32_type*>(value_ptr)[1]); break;
3309 case TI_INT8: glVertexAttrib2s(loc, reinterpret_cast<const int8_type*> (value_ptr)[0], reinterpret_cast<const int8_type*> (value_ptr)[1]); break;
3310 case TI_INT16: glVertexAttrib2s(loc, reinterpret_cast<const int16_type*> (value_ptr)[0], reinterpret_cast<const int16_type*> (value_ptr)[1]); break;
3311 case TI_INT32: glVertexAttribI2i(loc, reinterpret_cast<const int32_type*> (value_ptr)[0], reinterpret_cast<const int32_type*> (value_ptr)[1]); break;
3312 case TI_FLT32: glVertexAttrib2f(loc, reinterpret_cast<const flt32_type*> (value_ptr)[0], reinterpret_cast<const flt32_type*> (value_ptr)[1]); break;
3313 case TI_FLT64: glVertexAttrib2d(loc, reinterpret_cast<const flt64_type*> (value_ptr)[0], reinterpret_cast<const flt64_type*> (value_ptr)[1]); break;
3314 default:
3315 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3316 res = false;
3317 break;
3318 }
3319 break;
3320 case 3:
3321 switch (value_type.coordinate_type) {
3322 case TI_BOOL: glVertexAttrib3s (loc, reinterpret_cast<const bool*>(value_ptr)[0] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[1] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[2] ? 1 : 0); break;
3323 case TI_UINT8: glVertexAttrib3s (loc, reinterpret_cast<const uint8_type*> (value_ptr)[0], reinterpret_cast<const uint8_type*> (value_ptr)[1], reinterpret_cast<const uint8_type*> (value_ptr)[2]); break;
3324 case TI_UINT16: glVertexAttribI3ui(loc, reinterpret_cast<const uint16_type*>(value_ptr)[0], reinterpret_cast<const uint16_type*>(value_ptr)[1], reinterpret_cast<const uint16_type*>(value_ptr)[2]); break;
3325 case TI_UINT32: glVertexAttribI3ui(loc, reinterpret_cast<const uint32_type*>(value_ptr)[0], reinterpret_cast<const uint32_type*>(value_ptr)[1], reinterpret_cast<const uint32_type*>(value_ptr)[2]); break;
3326 case TI_INT8: glVertexAttrib3s (loc, reinterpret_cast<const int8_type*> (value_ptr)[0], reinterpret_cast<const int8_type*> (value_ptr)[1], reinterpret_cast<const int8_type*> (value_ptr)[2]); break;
3327 case TI_INT16: glVertexAttrib3s (loc, reinterpret_cast<const int16_type*> (value_ptr)[0], reinterpret_cast<const int16_type*> (value_ptr)[1], reinterpret_cast<const int16_type*> (value_ptr)[2]); break;
3328 case TI_INT32: glVertexAttribI3i (loc, reinterpret_cast<const int32_type*> (value_ptr)[0], reinterpret_cast<const int32_type*> (value_ptr)[1], reinterpret_cast<const int32_type*> (value_ptr)[2]); break;
3329 case TI_FLT32: glVertexAttrib3f (loc, reinterpret_cast<const flt32_type*> (value_ptr)[0], reinterpret_cast<const flt32_type*> (value_ptr)[1], reinterpret_cast<const flt32_type*> (value_ptr)[2]); break;
3330 case TI_FLT64: glVertexAttrib3d (loc, reinterpret_cast<const flt64_type*> (value_ptr)[0], reinterpret_cast<const flt64_type*> (value_ptr)[1], reinterpret_cast<const flt64_type*> (value_ptr)[2]); break;
3331 default:
3332 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3333 res = false; break;
3334 }
3335 break;
3336 case 4:
3337 switch (value_type.coordinate_type) {
3338 case TI_BOOL: glVertexAttrib4s (loc, reinterpret_cast<const bool*>(value_ptr)[0] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[1] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[2] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[3] ? 1 : 0); break;
3339 case TI_UINT8: glVertexAttrib4s (loc, reinterpret_cast<const uint8_type*> (value_ptr)[0], reinterpret_cast<const uint8_type*> (value_ptr)[1], reinterpret_cast<const uint8_type*> (value_ptr)[2], reinterpret_cast<const uint8_type*> (value_ptr)[3]); break;
3340 case TI_UINT16: glVertexAttribI4ui(loc, reinterpret_cast<const uint16_type*>(value_ptr)[0], reinterpret_cast<const uint16_type*>(value_ptr)[1], reinterpret_cast<const uint16_type*>(value_ptr)[2], reinterpret_cast<const uint16_type*>(value_ptr)[3]); break;
3341 case TI_UINT32: glVertexAttribI4ui(loc, reinterpret_cast<const uint32_type*>(value_ptr)[0], reinterpret_cast<const uint32_type*>(value_ptr)[1], reinterpret_cast<const uint32_type*>(value_ptr)[2], reinterpret_cast<const uint32_type*>(value_ptr)[3]); break;
3342 case TI_INT8: glVertexAttrib4s (loc, reinterpret_cast<const int8_type*> (value_ptr)[0], reinterpret_cast<const int8_type*> (value_ptr)[1], reinterpret_cast<const int8_type*> (value_ptr)[2], reinterpret_cast<const int8_type*> (value_ptr)[3]); break;
3343 case TI_INT16: glVertexAttrib4s (loc, reinterpret_cast<const int16_type*> (value_ptr)[0], reinterpret_cast<const int16_type*> (value_ptr)[1], reinterpret_cast<const int16_type*> (value_ptr)[2], reinterpret_cast<const int16_type*> (value_ptr)[3]); break;
3344 case TI_INT32: glVertexAttribI4i (loc, reinterpret_cast<const int32_type*> (value_ptr)[0], reinterpret_cast<const int32_type*> (value_ptr)[1], reinterpret_cast<const int32_type*> (value_ptr)[2], reinterpret_cast<const int32_type*> (value_ptr)[3]); break;
3345 case TI_FLT32: glVertexAttrib4f (loc, reinterpret_cast<const flt32_type*> (value_ptr)[0], reinterpret_cast<const flt32_type*> (value_ptr)[1], reinterpret_cast<const flt32_type*> (value_ptr)[2], reinterpret_cast<const flt32_type*> (value_ptr)[3]); break;
3346 case TI_FLT64: glVertexAttrib4d (loc, reinterpret_cast<const flt64_type*> (value_ptr)[0], reinterpret_cast<const flt64_type*> (value_ptr)[1], reinterpret_cast<const flt64_type*> (value_ptr)[2], reinterpret_cast<const flt64_type*> (value_ptr)[3]); break;
3347 default:
3348 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3349 res = false;
3350 break;
3351 }
3352 break;
3353 default:
3354 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") vector dimension outside [2..4]", &spb);
3355 res = false;
3356 break;
3357 }
3358 break;
3359 case ET_MATRIX:
3360 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") matrix type not supported!", &spb);
3361 res = false;
3362 break;
3363 }
3364 if (not_current)
3365 glUseProgram(shader_program_stack.empty() ? 0 : get_gl_id(shader_program_stack.top()->handle));
3366
3367 if (check_gl_error("gl_context::set_uniform_array_void()", &spb))
3368 res = false;
3369 return res;
3370}
3371
3372bool gl_context::attribute_array_binding_create(attribute_array_binding_base& aab) const
3373{
3374 if (!GLEW_VERSION_3_0) {
3375 error("gl_context::attribute_array_binding_create() array attribute bindings not supported", &aab);
3376 return false;
3377 }
3378 GLuint a_id;
3380 if (a_id == -1) {
3381 error(std::string("gl_context::attribute_array_binding_create(): ") + gl_error(), &aab);
3382 return false;
3383 }
3384 aab.ctx_ptr = this;
3385 aab.handle = get_handle(a_id);
3386 return true;
3387}
3388
3389bool gl_context::attribute_array_binding_destruct(attribute_array_binding_base& aab)
3390{
3391 if (&aab == attribute_array_binding_stack.top())
3393 if (!context::attribute_array_binding_destruct(aab))
3394 return false;
3395 if (!aab.handle) {
3396 error("gl_context::attribute_array_binding_destruct(): called on not created attribute array binding", &aab);
3397 return false;
3398 }
3399 GLuint a_id = get_gl_id(aab.handle);
3401 return !check_gl_error("gl_context::attribute_array_binding_destruct");
3402}
3403
3404bool gl_context::attribute_array_binding_enable(attribute_array_binding_base& aab)
3405{
3406 if (!context::attribute_array_binding_enable(aab))
3407 return false;
3408 glBindVertexArray(get_gl_id(aab.handle));
3409 return !check_gl_error("gl_context::attribute_array_binding_enable");
3410}
3411
3412bool gl_context::attribute_array_binding_disable(attribute_array_binding_base& aab)
3413{
3414 if (!context::attribute_array_binding_disable(aab))
3415 return false;
3418 else
3419 glBindVertexArray(get_gl_id(attribute_array_binding_stack.top()->handle));
3420 return true;
3421}
3422
3423bool gl_context::set_element_array(attribute_array_binding_base* aab, const vertex_buffer_base* vbb) const
3424{
3425 if (!vbb) {
3426 error("gl_context::set_element_array(): called without a vertex buffer object", aab);
3427 return false;
3428 }
3429 if (!vbb->handle) {
3430 error("gl_context::set_element_array(): called with not created vertex buffer object", vbb);
3431 return false;
3432 }
3433 if (vbb->type != VBT_INDICES) {
3434 std::cout << "gl_context::set_element_array() : called on vertex buffer object that is not of type VBT_INDICES" << std::endl;
3435// error("gl_context::set_element_array(): called on vertex buffer object that is not of type VBT_INDICES", vbb);
3436// return false;
3437 }
3438 if (aab) {
3439 if (!aab->handle) {
3440 error("gl_context::set_element_array(): called on not created attribute array binding", aab);
3441 return false;
3442 }
3443 }
3444 // enable vertex array
3446 if (aab && not_current)
3447 glBindVertexArray(get_gl_id(aab->handle));
3448
3449 // bind buffer to element array
3450 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, get_gl_id(vbb->handle));
3451
3452 if (aab && not_current)
3454
3455 return !check_gl_error("gl_context::set_element_array_void()", aab);
3456}
3457
3458
3459bool gl_context::set_attribute_array_void(attribute_array_binding_base* aab, int loc, type_descriptor value_type, const vertex_buffer_base* vbb, const void* ptr, size_t nr_elements, unsigned stride) const
3460{
3461 if (value_type == ET_MATRIX) {
3462 error("gl_context::set_attribute_array_void(): called with matrix elements not supported", aab);
3463 return false;
3464 }
3465 if (vbb) {
3466 if (!vbb->handle) {
3467 error("gl_context::set_attribute_array_void(): called with not created vertex buffer object", vbb);
3468 return false;
3469 }
3470 }
3471 if (aab) {
3472 if (!aab->handle) {
3473 error("gl_context::set_attribute_array_void(): called on not created attribute array binding", aab);
3474 return false;
3475 }
3476 }
3477
3479 if (aab && not_current)
3480 glBindVertexArray(get_gl_id(aab->handle));
3481
3482 if (vbb)
3483 glBindBuffer(GL_ARRAY_BUFFER, get_gl_id(vbb->handle));
3484
3485 bool res = true;
3486 unsigned n = value_type.element_type == ET_VALUE ? 1 : value_type.nr_rows;
3487 switch (value_type.coordinate_type) {
3488 case TI_INT8: value_type.normalize ? glVertexAttribPointer(loc, n, GL_BYTE, value_type.normalize, stride, ptr) : glVertexAttribIPointer(loc, n, GL_BYTE, stride, ptr); break;
3489 case TI_INT16: value_type.normalize ? glVertexAttribPointer(loc, n, GL_SHORT, value_type.normalize, stride, ptr) : glVertexAttribIPointer(loc, n, GL_SHORT, stride, ptr); break;
3490 case TI_INT32: value_type.normalize ? glVertexAttribPointer(loc, n, GL_INT, value_type.normalize, stride, ptr) : glVertexAttribIPointer(loc, n, GL_INT, stride, ptr); break;
3491 case TI_UINT8: value_type.normalize ? glVertexAttribPointer(loc, n, GL_UNSIGNED_BYTE, value_type.normalize, stride, ptr) : glVertexAttribIPointer(loc, n, GL_UNSIGNED_BYTE, stride, ptr); break;
3492 case TI_UINT16: value_type.normalize ? glVertexAttribPointer(loc, n, GL_UNSIGNED_SHORT, value_type.normalize, stride, ptr) : glVertexAttribIPointer(loc, n, GL_UNSIGNED_SHORT, stride, ptr); break;
3493 case TI_UINT32: value_type.normalize ? glVertexAttribPointer(loc, n, GL_UNSIGNED_INT, value_type.normalize, stride, ptr) : glVertexAttribIPointer(loc, n, GL_UNSIGNED_INT, stride, ptr); break;
3494 case TI_FLT32: glVertexAttribPointer(loc, n, GL_FLOAT, value_type.normalize, stride, ptr); break;
3495 case TI_FLT64:
3496 if (GLEW_VERSION_4_1)
3497 glVertexAttribLPointer(loc, n, GL_DOUBLE, stride, ptr);
3498 else {
3499 error("gl_context::set_attribute_array_void(): called with coordinates of type double only supported starting with OpenGL 4.1", aab);
3500 res = false;
3501 }
3502 break;
3503 default:
3504 error("gl_context::set_attribute_array_void(): called with unsupported coordinate type", aab);
3505 res = false;
3506 }
3507
3508 if (res)
3510
3511 if (vbb)
3513
3514 if (aab && not_current)
3516
3517
3518 return res && !check_gl_error("gl_context::set_attribute_array_void()", aab);
3519}
3520
3521bool gl_context::enable_attribute_array(attribute_array_binding_base* aab, int loc, bool do_enable) const
3522{
3524 if (aab) {
3525 if (!aab->handle) {
3526 error("gl_context::enable_attribute_array(): called on not created attribute array binding", aab);
3527 return false;
3528 }
3529 if (not_current)
3530 glBindVertexArray(get_gl_id(aab->handle));
3531 }
3532
3533 if (do_enable)
3535 else
3537
3538 if (aab && not_current)
3540
3541 return !check_gl_error("gl_context::enable_attribute_array()");
3542}
3543
3544bool gl_context::is_attribute_array_enabled(const attribute_array_binding_base* aab, int loc) const
3545{
3547 if (aab) {
3548 if (!aab->handle) {
3549 error("gl_context::is_attribute_array_enabled(): called on not created attribute array binding", aab);
3550 return false;
3551 }
3552 if (not_current)
3553 glBindVertexArray(get_gl_id(aab->handle));
3554 }
3555
3556 GLint res;
3558
3559 if (aab && not_current)
3561
3562 return res == GL_TRUE;
3563}
3564
3565GLenum buffer_target(VertexBufferType vbt)
3566{
3567 static GLenum buffer_targets[] = {
3576 };
3577 return buffer_targets[vbt];
3578}
3579
3580GLenum buffer_usage(VertexBufferUsage vbu)
3581{
3582 static GLenum buffer_usages[] = {
3592 };
3593 return buffer_usages[vbu];
3594}
3595
3596bool gl_context::vertex_buffer_bind(const vertex_buffer_base& vbb, VertexBufferType _type, unsigned _idx) const
3597{
3598 if (_idx == unsigned(-1))
3599 glBindBuffer(buffer_target(_type), get_gl_id(vbb.handle));
3600 else
3601 glBindBufferBase(buffer_target(_type), _idx, get_gl_id(vbb.handle));
3602 return !check_gl_error("gl_context::vertex_buffer_bind", &vbb);
3603}
3604
3605bool gl_context::vertex_buffer_unbind(const vertex_buffer_base& vbb, VertexBufferType _type, unsigned _idx) const {
3606 if(_idx == unsigned(-1))
3607 glBindBuffer(buffer_target(_type), 0);
3608 else
3609 glBindBufferBase(buffer_target(_type), _idx, 0);
3610 return !check_gl_error("gl_context::vertex_buffer_unbind", &vbb);
3611}
3612
3613bool gl_context::vertex_buffer_create(vertex_buffer_base& vbb, const void* array_ptr, size_t size_in_bytes) const
3614{
3615 if (!GLEW_VERSION_2_0) {
3616 error("gl_context::vertex_buffer_create() vertex buffer objects not supported", &vbb);
3617 return false;
3618 }
3619 GLuint b_id;
3620 glGenBuffers(1, &b_id);
3621 if (b_id == -1) {
3622 error(std::string("gl_context::vertex_buffer_create(): ") + gl_error(), &vbb);
3623 return false;
3624 }
3625 vbb.handle = get_handle(b_id);
3626 glBindBuffer(buffer_target(vbb.type), b_id);
3627 glBufferData(buffer_target(vbb.type), size_in_bytes, array_ptr, buffer_usage(vbb.usage));
3628 glBindBuffer(buffer_target(vbb.type), 0);
3629 return !check_gl_error("gl_context::vertex_buffer_create", &vbb);
3630}
3631
3632bool gl_context::vertex_buffer_resize(vertex_buffer_base& vbb, const void* array_ptr, size_t size_in_bytes) const {
3633 if(!vbb.handle) {
3634 error("gl_context::vertex_buffer_resize() vertex buffer object must be created before", &vbb);
3635 return false;
3636 }
3637 GLuint b_id = get_gl_id(vbb.handle);
3638 glBindBuffer(buffer_target(vbb.type), b_id);
3639 glBufferData(buffer_target(vbb.type), size_in_bytes, array_ptr, buffer_usage(vbb.usage));
3640 glBindBuffer(buffer_target(vbb.type), 0);
3641 return !check_gl_error("gl_context::vertex_buffer_resize", &vbb);
3642}
3643
3644bool gl_context::vertex_buffer_replace(vertex_buffer_base& vbb, size_t offset, size_t size_in_bytes, const void* array_ptr) const
3645{
3646 if (!vbb.handle) {
3647 error("gl_context::vertex_buffer_replace() vertex buffer object must be created before", &vbb);
3648 return false;
3649 }
3650 GLuint b_id = get_gl_id(vbb.handle);
3651 glBindBuffer(buffer_target(vbb.type), b_id);
3652 glBufferSubData(buffer_target(vbb.type), offset, size_in_bytes, array_ptr);
3653 glBindBuffer(buffer_target(vbb.type), 0);
3654 return !check_gl_error("gl_context::vertex_buffer_replace", &vbb);
3655}
3656
3657bool gl_context::vertex_buffer_copy(const vertex_buffer_base& src, size_t src_offset, vertex_buffer_base& target, size_t target_offset, size_t size_in_bytes) const
3658{
3659 if (!src.handle || !target.handle) {
3660 error("gl_context::vertex_buffer_copy() source and destination vertex buffer objects must have been created before", &src);
3661 return false;
3662 }
3663 GLuint b_id = get_gl_id(src.handle);
3664 glBindBuffer(GL_COPY_READ_BUFFER, get_gl_id(src.handle));
3665 glBindBuffer(GL_COPY_WRITE_BUFFER, get_gl_id(target.handle));
3669 return !check_gl_error("gl_context::vertex_buffer_copy", &src);
3670
3671}
3672
3673bool gl_context::vertex_buffer_copy_back(vertex_buffer_base& vbb, size_t offset, size_t size_in_bytes, void* array_ptr) const
3674{
3675 if (!vbb.handle) {
3676 error("gl_context::vertex_buffer_copy_back() vertex buffer object must be created", &vbb);
3677 return false;
3678 }
3679 GLuint b_id = get_gl_id(vbb.handle);
3681 glGetBufferSubData(GL_COPY_READ_BUFFER, offset, size_in_bytes, array_ptr);
3683 return !check_gl_error("gl_context::vertex_buffer_copy_back", &vbb);
3684}
3685
3686bool gl_context::vertex_buffer_destruct(vertex_buffer_base& vbb) const
3687{
3688 if (vbb.handle) {
3689 GLuint b_id = get_gl_id(vbb.handle);
3690 glDeleteBuffers(1, &b_id);
3691 return !check_gl_error("gl_context::vertex_buffer_destruct");
3692 }
3693 else {
3694 error("gl_context::vertex_buffer_destruct(): called on not created vertex buffer", &vbb);
3695 return false;
3696 }
3697}
3698
3699
3700 }
3701 }
3702}
virtual void stream_stats(std::ostream &)
overload to show the content of this object
Definition base.cxx:24
The group class is a node with children.
Definition group.h:20
complete implementation of method actions that only call one method when entering a node
Definition action.h:113
interface of a handler for traverse callbacks
Definition traverser.h:77
class used to traverse a tree structure
Definition traverser.h:102
bool traverse(base_ptr start, traverse_callback_handler *tch=0)
traverse a tree starting at given node according to set strategy, order and dest and previously comin...
the component format inherits the information of a packing_info and adds information on the component...
cgv::type::info::TypeId get_component_type() const
return the component type
ComponentFormat get_standard_component_format() const
return whether the component format is one of the standard formats
The const_data_view has the functionality of the data_view but uses a const pointer and therefore doe...
Definition data_view.h:221
A data_format describes a multidimensional data block of data entries.
Definition data_format.h:17
size_t get_width() const
return the resolution in the first dimension, or 1 if not defined
size_t get_height() const
return the resolution in the second dimension, or 1 if not defined
const component_format & get_component_format() const
return the component_format info by simple conversion of the this pointer
void manage_format(bool enable=true)
whether to manage the data format pointer
Definition data_view.cxx:59
const data_format * get_format() const
return the component format
Definition data_view.cxx:73
cgv::type::func::transfer_const< P, S * >::type get_ptr() const
return a data pointer to type S
Definition data_view.h:61
the data view gives access to a data array of one, two, three or four dimensions.
Definition data_view.h:153
~data_view()
destruct view and delete data pointer if it is owned by the view
void reflect_horizontally()
reflect 2D data view at horizontal axis
unsigned int get_component_alignment() const
return the component alignment in bits in the packed case and in bytes in the unpacked case
bool empty() const
check if pointer is not yet set
Definition ref_ptr.h:230
virtual void stream_help(std::ostream &os)=0
overload to stream help information to the given output stream
matrix of fixed size dimensions
Definition fmat.h:23
A matrix type (full column major storage) The matrix can be loaded directly into OpenGL without need ...
Definition mat.h:208
>simple class to hold the properties of a light source
>simple class to hold the material properties of a phong material
simple class to hold the material properties of a phong material
the image writer chooses a specific writer automatically based on the extension of the given file nam...
static const std::string & get_supported_extensions(char sep=';')
return a string with a list of supported extensions, where the list entries are separated with the pa...
the attribute_array_binding allows to define vertex attributes (i.e.
bool enable(context &ctx)
enable whole the attribute array binding object
static bool enable_global_array(const context &ctx, int loc)
enable attribute array of given location
bool create(const context &ctx)
create the attribute array binding object
bool disable(context &ctx)
disable whole attribute array binding object
static bool disable_global_array(const context &ctx, int loc)
disable attribute array of given location
bool enable_array(const context &ctx, int loc)
enable array for vertex attribute at location loc
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
virtual void set_blend_func(BlendFunction src_factor, BlendFunction dst_factor)
set the blend function
Definition context.cxx:1737
virtual std::ostream & output_stream()
returns an output stream whose output is printed at the current cursor location, which is managed by ...
Definition context.cxx:905
virtual void mul_modelview_matrix(const dmat4 &MV)
multiply given matrix from right to current modelview matrix
Definition context.cxx:1812
virtual void set_depth_range(const dvec2 &depth_range=dvec2(0, 1), int array_index=-1)
set the current depth range or one of the depth ranges in the window transformation array
Definition context.cxx:1912
virtual bool in_render_process() const =0
return whether the context is currently in process of rendering
virtual bool is_created() const =0
return whether the context is created
shader_program_base * get_current_program() const
check for current program, prepare it for rendering and return pointer to it
Definition context.cxx:444
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
Definition context.cxx:219
void set_current_view(shader_program &prog, bool modelview_deps=true, bool projection_deps=true) const
set the shader program view matrices to the currently enabled view matrices
Definition context.cxx:580
float current_font_size
store current font size
Definition context.h:793
bool enable_vsync
whether vsync should be enabled
Definition context.h:699
virtual void set_buffer_mask(BufferMask mask)
set the buffer mask for depth and color buffers
Definition context.cxx:1781
virtual bool is_current() const =0
return whether the context is current
bool auto_set_lights_in_current_shader_program
whether to automatically set lights in current shader program, defaults to true
Definition context.h:687
virtual void set_blend_func_separate(BlendFunction src_color_factor, BlendFunction dst_color_factor, BlendFunction src_alpha_factor, BlendFunction dst_alpha_factor)
set the blend function separately for color and alpha
Definition context.cxx:1745
virtual void on_lights_changed()
helper function to send light update events
Definition context.cxx:636
void push_depth_test_state()
push a copy of the current depth test state onto the stack saved attributes: depth test enablement,...
Definition context.cxx:1673
std::stack< shader_program_base * > shader_program_stack
stack of currently enabled shader programs
Definition context.h:732
bool is_light_source_enabled(void *handle)
check whether light source is enabled
Definition context.cxx:680
virtual void disable_depth_test()
disable the depth test
Definition context.cxx:1699
int get_bg_stencil() const
return the current stencil value for clearing the background
Definition context.cxx:370
virtual unsigned int get_width() const =0
return the width of the window
bool disable_light_source(void *handle)
disable a given light source and return whether there existed a light source with given handle
Definition context.cxx:704
void pop_depth_test_state()
pop the top of the current depth test state from the stack
Definition context.cxx:1677
void tesselate_unit_sphere(int resolution=25, bool flip_normals=false, bool edges=false)
tesselate a sphere of radius 1
Definition context.cxx:1348
virtual RenderPassFlags get_render_pass_flags() const
return the current render pass flags
Definition context.cxx:762
void tesselate_unit_cylinder(int resolution=25, bool flip_normals=false, bool edges=false)
tesselate a cylinder of radius 1
Definition context.cxx:1256
virtual void set_bg_color(vec4 rgba)
set a user defined background color
Definition context.cxx:297
virtual void set_depth_test_state(DepthTestState state)
set the depth test state
Definition context.cxx:1686
size_t get_nr_enabled_light_sources() const
return the number of light sources
Definition context.cxx:669
virtual unsigned int get_height() const =0
return the height of the window
virtual void disable_blending()
disable blending
Definition context.cxx:1764
BufferMask get_buffer_mask() const
return the current buffer mask
Definition context.cxx:1777
void push_cull_state()
push a copy of the current culling state onto the stack saved attributes: cull face enablement,...
Definition context.cxx:1703
virtual void enable_font_face(media::font::font_face_ptr font_face, float font_size)
enable the given font face with the given size in pixels
Definition context.cxx:910
std::stack< dmat4 > modelview_matrix_stack
keep two matrix stacks for model view and projection matrices
Definition context.h:726
virtual void pop_window_transformation_array()
restore previous viewport and depth range arrays defining the window transformations
Definition context.cxx:1879
virtual void set_color_mask(bvec4 flags)
set the color buffer mask
Definition context.cxx:1798
virtual void set_material(const cgv::media::illum::surface_material &mat)
set the current material
Definition context.cxx:1636
DepthTestState get_depth_test_state() const
return the current depth test state
Definition context.cxx:1682
bool auto_set_material_in_current_shader_program
whether to automatically set material in current shader program, defaults to true
Definition context.h:689
void set_current_lights(shader_program &prog) const
set the shader program lights to the currently enabled lights
Definition context.cxx:621
bool enable_light_source(void *handle)
enable a given light source and return whether there existed a light source with given handle
Definition context.cxx:689
void tesselate_unit_disk(int resolution=25, bool flip_normals=false, bool edges=false)
tesselate a circular disk of radius 1
Definition context.cxx:1180
virtual void enable_depth_test()
enable the depth test
Definition context.cxx:1695
bool support_compatibility_mode
whether to support view and lighting management of compatibility mode, defaults to true
Definition context.h:693
void set_current_gamma(shader_program &prog) const
set the shader program gamma values
Definition context.cxx:1584
vec4 get_bg_color() const
return the current color value for clearing the background
Definition context.cxx:306
void pop_projection_matrix()
see push_P for an explanation
Definition context.cxx:1829
std::stack< attribute_array_binding_base * > attribute_array_binding_stack
stack of currently enabled attribute array binding
Definition context.h:744
BlendState get_blend_state() const
return the current blend state
Definition context.cxx:1729
virtual void set_cull_state(CullingMode culling_mode)
set the culling state
Definition context.cxx:1716
virtual void set_depth_func(CompareFunction func)
set the depth test function
Definition context.cxx:1690
void set_light_source(void *handle, const cgv::media::illum::light_source &light, bool place_now=true)
set light source newly
Definition context.cxx:567
void push_projection_matrix()
same as push_V but for the projection matrix - a different matrix stack is used.
Definition context.cxx:1824
bool current_material_is_textured
store flag to tell whether current material is textured
Definition context.h:772
virtual void set_projection_matrix(const dmat4 &P)
set the current projection matrix, which transforms from eye to clip space
Definition context.cxx:1857
rgba current_color
current color value
Definition context.h:701
static const unsigned nr_default_light_sources
number of default light sources
Definition context.h:762
cgv::media::illum::light_source default_light_source[nr_default_light_sources]
default light sources
Definition context.h:764
virtual void set_cursor(int x, int y)
flush the output_stream and set a new cursor position given in opengl coordinates with (0,...
Definition context.cxx:1963
virtual void set_bg_stencil(int s)
set a user defined background stencil value
Definition context.cxx:366
virtual void set_bg_accum_color(vec4 rgba)
set a user defined background color for the accumulation buffer
Definition context.cxx:383
cgv::media::illum::surface_material default_material
store a default material
Definition context.h:768
float get_bg_depth() const
return the current depth value for clearing the background
Definition context.cxx:353
virtual void set_textured_material(const textured_material &mat)
set the current material
Definition context.cxx:1655
std::stack< std::vector< window_transformation > > window_transformation_stack
keep stack of window transformations
Definition context.h:728
vec4 get_bg_accum_color() const
return the current color value for clearing the accumulation buffer
Definition context.cxx:391
const cgv::media::illum::light_source & get_light_source(void *handle) const
read access to light source
Definition context.cxx:552
virtual bool make_current() const =0
make the current context current if possible
bool draw_in_compatibility_mode
whether to do all drawing in compatibility mode, only possible if support_compatibility_mode is true,...
Definition context.h:695
virtual void set_blend_state(BlendState state)
set the complete blend state
Definition context.cxx:1733
bool sRGB_framebuffer
whether to use opengl option to support sRGB framebuffer
Definition context.h:703
virtual RenderPassFlags get_default_render_pass_flags() const
return the default render pass flags
Definition context.cxx:770
virtual void set_bg_depth(float d)
set a user defined background depth value
Definition context.cxx:349
virtual void enable_blending()
enable blending
Definition context.cxx:1760
void set_current_material(shader_program &prog) const
set the shader program material to the currently enabled material
Definition context.cxx:609
void pop_cull_state()
pop the top of the current culling state from the stack
Definition context.cxx:1707
void pop_modelview_matrix()
see push_V for an explanation
Definition context.cxx:1818
void * default_light_source_handles[nr_default_light_sources]
handles of default light sources
Definition context.h:766
void push_modelview_matrix()
push the current viewing matrix onto a matrix stack for viewing matrices.
Definition context.cxx:1806
std::stack< frame_buffer_base * > frame_buffer_stack
stack of currently enabled frame buffers
Definition context.h:730
bool auto_set_view_in_current_shader_program
whether to automatically set viewing matrixes in current shader program, defaults to true
Definition context.h:685
void tesselate_unit_cone(int resolution=25, bool flip_normals=false, bool edges=false)
tesselate a cone of radius 1
Definition context.cxx:1212
CullingMode get_cull_state() const
return the current culling state
Definition context.cxx:1712
virtual void set_viewport(const ivec4 &viewport, int array_index=-1)
set the current viewport or one of the viewports in the window transformation array
Definition context.cxx:1905
const cgv::media::illum::surface_material * current_material_ptr
store pointer to current material
Definition context.h:770
virtual void set_modelview_matrix(const dmat4 &MV)
set the current modelview matrix, which transforms from world to eye space
Definition context.cxx:1840
void * get_enabled_light_source_handle(size_t i) const
access to handle of i-th light source
Definition context.cxx:675
cgv::media::font::font_face_ptr current_font_face
store current font
Definition context.h:795
virtual void set_depth_mask(bool flag)
set the depth buffer mask
Definition context.cxx:1789
bool auto_set_gamma_in_current_shader_program
whether to automatically set gamma in current shader program, defaults to true
Definition context.h:691
virtual void init_frame(context &)
this method is called in one pass over all drawables before the draw method
Definition drawable.cxx:108
virtual void resize(unsigned int w, unsigned int h)
callback to announce resizing of the output window
Definition drawable.cxx:104
virtual bool init(context &)
this method is called after creation or recreation of the context, return whether all necessary funct...
Definition drawable.cxx:99
base interface for framebuffer
Definition context.h:481
implementation of the context API for the OpenGL API excluding methods for font selection,...
Definition gl_context.h:44
media::font::font_face_ptr get_current_font_face() const override
overwrite function to return info font face in case no font is currently selected
void set_depth_func(CompareFunction func) override
set the depth test function
void push_pixel_coords() override
use this to push transformation matrices on the stack such that x and y coordinates correspond to win...
void set_depth_mask(bool flag) override
set the depth buffer mask
RenderAPI get_render_api() const override
return the used rendering API
bool frame_buffer_disable(frame_buffer_base &fbb) override
disable the framebuffer object
void tesselate_arrow(double length=1, double aspect=0.1, double rel_tip_radius=2.0, double tip_aspect=0.3, int res=25, bool edges=false) override
tesselate an arrow from the origin in z-direction
void set_color(const rgba &clr) override
set the current color
void set_buffer_mask(BufferMask mask) override
set the buffer mask for depth and color buffers
float info_font_size
font size to draw textual info
Definition gl_context.h:143
bool frame_buffer_is_complete(const frame_buffer_base &fbb) const override
check for completeness, if not complete, get the reason in last_error
void set_bg_color(vec4 rgba) override
set a user defined background color
void draw_light_source(const cgv::media::illum::light_source &l, float intensity_scale, float light_scale) override
draw a light source with an emissive material
void announce_external_viewport_change(ivec4 &cgv_viewport_storage) override
announce an external viewport change performed with rendering API to the cgv framework providing spac...
void enumerate_program_attributes(shader_program &prog, std::vector< std::string > &names, std::vector< int > *locations_ptr=0, std::vector< int > *sizes_ptr=0, std::vector< int > *types_ptr=0, bool show=false) const override
get list of program attributes
void enable_depth_test() override
enable the depth test
void pop_window_transformation_array() override
restore previous viewport and depth range arrays defining the window transformations
shader_program & ref_default_shader_program(bool texture_support=false) override
return a reference to a shader program used to render without illumination
bool prepare_attributes(std::vector< vec3 > &P, std::vector< vec3 > &N, std::vector< vec2 > &T, unsigned nr_vertices, const float *vertices, const float *normals, const float *tex_coords, const int *vertex_indices, const int *normal_indices, const int *tex_coord_indices, bool flip_normals) const
helper function to prepare attribute arrays
gl_context()
construct context and attach signals
cgv::media::font::font_face_ptr get_info_font_face() const
return info font face and ensure that it is created
void set_color_mask(bvec4 flags) override
set the color buffer mask
void recover_from_external_frame_buffer_change(void *cgv_fbo_storage) override
restore cgv frame buffer to the state before the external change
dmat4 get_modelview_matrix() const override
return homogeneous 4x4 viewing matrix, which transforms from world to eye space
void set_material(const cgv::media::illum::surface_material &mat) override
set the current material
void enable_blending() override
enable blending
void set_bg_stencil(int s) override
set a user defined background stencil value
void recover_from_external_viewport_change(const ivec4 &cgv_viewport_storage) override
restore cgv viewport to the state before the external change
void set_modelview_matrix(const dmat4 &V) override
set the current modelview matrix, which transforms from world to eye space
double get_window_z(int x_window, int y_window) const override
read the device z-coordinate from the z-buffer for the given device x- and y-coordinates
dmat4 get_projection_matrix() const override
return homogeneous 4x4 projection matrix, which transforms from eye to clip space
void enumerate_program_uniforms(shader_program &prog, std::vector< std::string > &names, std::vector< int > *locations_ptr=0, std::vector< int > *sizes_ptr=0, std::vector< int > *types_ptr=0, bool show=false) const override
get list of program uniforms
virtual bool read_frame_buffer(data::data_view &dv, unsigned int x=0, unsigned int y=0, FrameBufferType buffer_type=FB_BACK, cgv::type::info::TypeId type=type::info::TI_UINT8, data::ComponentFormat cf=data::CF_RGB, int w=-1, int h=-1) override
implement according to specification in context class
void enable_material(textured_material &mat) override
enable a material with textures
float get_info_font_size() const
return info font size
void set_bg_depth(float d) override
set a user defined background depth value
cgv::media::font::font_face_ptr info_font_face
font used to draw textual info
Definition gl_context.h:141
void clear_background(bool color_flag, bool depth_flag, bool stencil_flag=false, bool accum_flag=false) override
clear the buffer contents of the flagged buffers to the set background colors
shader_program & ref_surface_shader_program(bool texture_support=false) override
return a reference to the default shader program used to render surfaces
void set_depth_range(const dvec2 &depth_range=dvec2(0, 1), int array_index=-1) override
set the current depth range or one of the depth ranges in the window transformation array
void draw_strip_or_fan(const float *vertices, const float *normals, const float *tex_coords, const int *vertex_indices, const int *normal_indices, const int *tex_coord_indices, int nr_faces, int face_degree, bool is_fan, bool flip_normals) const override
tesselate with one face strip
void set_blend_state(BlendState state) override
set the complete blend state
bool configure_gl()
ensure that glew is initialized, define lighting mode, viewing pyramid and the rendering mode and ret...
bool release_attributes(const float *normals, const float *tex_coords, const int *normal_indices, const int *tex_coord_indices) const
helper function to disable attribute arrays
float get_current_font_size() const override
overwrite function to return info font size in case no font is currently selected
void set_cull_state(CullingMode culling_mode) override
set the culling state
void draw_edges_of_faces(const float *vertices, const float *normals, const float *tex_coords, const int *vertex_indices, const int *normal_indices, const int *tex_coord_indices, int nr_faces, int face_degree, bool flip_normals=false) const override
pass geometry of given faces to current shader program and generate draw calls to render lines for th...
void set_projection_matrix(const dmat4 &P) override
set the current projection matrix, which transforms from eye to clip space
unsigned get_max_window_transformation_array_size() const override
query the maximum number of supported window transformations, which is at least 1
void set_bg_accum_color(vec4 rgba) override
set a user defined background color for the accumulation buffer
void disable_blending() override
disable blending
void rotate_vector_to_target(const dvec3 &vector, const dvec3 &target)
helper function that multiplies a rotation to modelview matrix such that vector is rotated onto targe...
void set_blend_func(BlendFunction src_factor, BlendFunction dst_factor) override
set the blend function
void set_viewport(const ivec4 &viewport, int array_index=-1) override
set the current viewport or one of the viewports in the window transformation array
void pop_pixel_coords() override
pop previously changed transformation matrices
void disable_material(textured_material &mat) override
disable a material with textures
void on_lights_changed() override
helper function to send light update events
void disable_depth_test() override
disable the depth test
void set_blend_func_separate(BlendFunction src_color_factor, BlendFunction dst_color_factor, BlendFunction src_alpha_factor, BlendFunction dst_alpha_factor) override
set the blend function separately for color and alpha
void draw_faces(const float *vertices, const float *normals, const float *tex_coords, const int *vertex_indices, const int *normal_indices, const int *tex_coord_indices, int nr_faces, int face_degree, bool flip_normals) const override
tesselate with independent faces
void set_depth_test_state(DepthTestState state) override
set the depth test state
void draw_edges_of_strip_or_fan(const float *vertices, const float *normals, const float *tex_coords, const int *vertex_indices, const int *normal_indices, const int *tex_coord_indices, int nr_faces, int face_degree, bool is_fan, bool flip_normals=false) const override
pass geometry of given strip or fan to current shader program and generate draw calls to render lines...
void announce_external_frame_buffer_change(void *&cgv_fbo_storage) override
announce an external frame buffer change performed with rendering API to the cgv framework providing ...
base interface for all render components
Definition context.h:309
a shader program combines several shader code fragments to a complete definition of the shading pipel...
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,...
bool set_attribute(const context &ctx, const std::string &name, const T &value)
set constant default value of a vertex attribute by attribute name, if name does not specify an attri...
base interface for a texture
Definition context.h:340
the texture class encapsulates all functionality independent of the rendering api.
Definition texture.h:15
void set_component_format(const component_format &cf)
change component format and clear internal format
Definition texture.cxx:61
class that extends obj_material with the management of textures
a vertex buffer is an unstructured memory block on the GPU.
bool create(const context &ctx, size_t size_in_bytes)
create empty vertex buffer of size size given in bytes
void destruct(const context &ctx)
destruct the render buffer
bool replace(const context &ctx, size_t buffer_offset_in_bytes, const T *array_ptr, size_t nr_elements)
replace part (starting at byte offset buffer_offset_in_bytes) or whole vertex buffer content from nr_...
the base namespace holds the base hierarchy, support for plugin registration and signals
Definition action.cxx:4
data::ref_ptr< group, true > group_ptr
ref counted pointer to a node
Definition group.h:14
ComponentFormat
define standard formats, which should be used to avoid wrong assignment of component names
@ CF_S
depth component
@ CF_RGB
color format with two components R and G
@ CF_D
color format with components B, G, R and A
unsigned find_best_match(const component_format &fmt, const char **format_descriptions, const component_format *fmt0, bool(*fmt1_better_match)(const component_format &fmt, const component_format &fmt1, const component_format &fmt2))
find the best matching format in a list of formats described by strings and return index of best matc...
namespace that holds the abstract gui interface
Definition vr_calib.cxx:10
void message(const std::string &_message)
tell the user something with a message box
Definition dialog.cxx:14
namespace for font support
Definition font.cxx:6
font_ptr default_font(bool mono_space)
return platform dependend default font
Definition font.cxx:57
bool load_texture(const cgv::data::const_data_view &data, unsigned gl_tex_format, unsigned level, unsigned cube_side, int num_array_layers, const std::vector< data_view > *palettes)
load data to a texture with the glTexImage commands and generate mipmaps if the level parameter is -1...
void gl_set_material(const cgv::media::illum::phong_material &mat, MaterialSide ms, float alpha)
enable a material without textures
bool replace_texture(const cgv::data::const_data_view &data, int level, int x, int y, int z, const std::vector< cgv::data::data_view > *palettes)
replace part or complete data of currently bound texture with the data in the given data view
bool ensure_glew_initialized()
initialize glew in the first call to this function and always return whether this was successful
Definition gl.cxx:19
void set_gl_format(texture &tex, GLuint gl_format, const std::string &component_format_description)
set a very specific texture format. This should be called after the texture is constructed and before...
bool generate_mipmaps(unsigned int dim, bool is_cubemap, bool is_array, std::string *last_error)
generate mipmaps for the currently bound texture, which has the given texture dimension; optionally p...
unsigned find_best_texture_format(const cgv::data::component_format &_cf, cgv::data::component_format *best_cf, const std::vector< data_view > *palettes)
map the given component format to the best matching available gl component format
unsigned get_gl_cube_map_target(unsigned side)
return one of the six cube map sides gl enums
std::vector< int > get_context_creation_attrib_list(cgv::render::context_config &cc)
construct a 0 terminated list of context creation attribute definitions
GLuint get_gl_format(const texture &tex)
return the texture format used for a given texture. If called before texture has been created,...
RenderAPI
enumeration of rendering APIs which can be queried from the context
Definition context.h:78
CullingMode
different culling modes
Definition context.h:151
BlendFunction
different blend functions
Definition context.h:158
AccessType
different access types
Definition context.h:274
TextureFilter
different texture filter
Definition context.h:197
TextureWrap
different texture wrap modes
Definition context.h:181
FrameBufferType
different frame buffer types which can be combined together with or
Definition context.h:498
MaterialSide
different sides of a material
Definition context.h:136
VertexBufferUsage
Provides vertex buffer usage hints as defined in OpenGL.
Definition context.h:437
ShaderType
different shader types
Definition context.h:495
PrimitiveType
different primitive types
Definition context.h:233
VertexBufferType
Provides vertex buffer types to allow implicit binding.
Definition context.h:424
@ VBT_INDICES
The buffer contains indices and will be bound to GL_ELEMENT_ARRAY_BUFFER.
Definition context.h:427
BufferTypeBits
Bits for the selection of different buffer types.
Definition context.h:471
TextureType
different texture types
Definition context.h:209
RenderPassFlags
available flags that can be queried from the context and set for a new render pass
Definition context.h:101
@ RPF_CLEAR_ACCUM
whether to clear the accumulation buffer
Definition context.h:116
@ RPF_SET_MODELVIEW
whether to set default modelview matrix
Definition context.h:104
@ RPF_SET_MATERIAL
whether to define default material
Definition context.h:108
@ RPF_SET_CLEAR_DEPTH
whether to set the clear color
Definition context.h:122
@ RPF_SET_LIGHTS
whether to define default lights
Definition context.h:107
@ RPF_CLEAR_COLOR
whether to clear the color buffer
Definition context.h:113
@ RPF_CLEAR_STENCIL
whether to clear the depth buffer
Definition context.h:115
@ RPF_SET_CLEAR_STENCIL
whether to set the clear color
Definition context.h:123
@ RPF_ENABLE_MATERIAL
whether to enable material
Definition context.h:110
@ RPF_DRAWABLES_INIT_FRAME
whether to call the init_frame method of the drawables
Definition context.h:119
@ RPF_CLEAR_DEPTH
whether to clear the depth buffer
Definition context.h:114
@ RPF_SET_STATE_FLAGS
whether to set depth buffer and culling flags
Definition context.h:120
@ RPF_SET_PROJECTION
whether to set default projection matrix
Definition context.h:103
@ RPF_SET_CLEAR_ACCUM
whether to set the accumulation buffer clear color
Definition context.h:124
@ RPF_SET_CLEAR_COLOR
whether to set the clear color
Definition context.h:121
@ RPF_SET_LIGHTS_ON
whether to turn on default lights
Definition context.h:109
CompareFunction
different comparison functions used for depth testing or texture comparisons
Definition context.h:262
namespace for templates that provide type information
Definition type_access.h:9
const char * get_type_name(TypeId tid)
function that returns the name of a type specified through TypeId
Definition type_id.cxx:117
TypeId
ids for the different types and type constructs
Definition type_id.h:12
@ TI_INT16
signed integer stored in 8 bits
Definition type_id.h:20
@ TI_INT8
boolean
Definition type_id.h:19
@ TI_INT32
signed integer stored in 16 bits
Definition type_id.h:21
@ TI_FLT32
floating point type stored in 16 bits
Definition type_id.h:28
@ TI_UINT32
unsigned integer stored in 16 bits
Definition type_id.h:25
@ TI_UINT8
signed integer stored in 64 bits
Definition type_id.h:23
@ TI_BOOL
void
Definition type_id.h:18
@ TI_UINT16
unsigned integer stored in 8 bits
Definition type_id.h:24
@ TI_FLT64
floating point type stored in 32 bits
Definition type_id.h:29
namespace for compile time type information
std::string to_string(const std::string &v, unsigned int w, unsigned int p, bool)
specialization of conversion from string to strings
char to_upper(char c)
convert char to upper case
Definition scan.cxx:106
bool is_element(char c, const std::string &s)
check if char c arises in string s
Definition scan.cxx:291
the cgv namespace
Definition print.h:11
cgv::media::color< float, cgv::media::RGB, cgv::media::OPACITY > rgba
declare rgba color type with 32 bit components
Definition color.h:855
cgv::math::fvec< double, 3 > dvec3
declare type of 3d double precision floating point vectors
Definition fvec.h:677
cgv::math::fvec< uint32_t, 3 > uvec3
declare type of 3d 32 bit unsigned integer vectors
Definition fvec.h:703
cgv::math::fvec< int32_t, 4 > ivec4
declare type of 4d 32 bit integer vectors
Definition fvec.h:699
cgv::math::fmat< double, 4, 4 > dmat4
declare type of 4x4 matrices
Definition fmat.h:369
cgv::math::fvec< float, 2 > vec2
declare type of 2d single precision floating point vectors
Definition fvec.h:668
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:670
Helper functions to process strings.
Represents a blend state used to configure fragment blending.
Definition context.h:652
bool enabled
whether blending is enabled
Definition context.h:654
BlendFunction dst_color
the destination color (rgb) factor
Definition context.h:658
BlendFunction dst_alpha
the destination alpha factor
Definition context.h:662
BlendFunction src_alpha
the source alpha factor
Definition context.h:660
BlendFunction src_color
the source color (rgb) factor
Definition context.h:656
Represents a buffer mask used to mask depth and color buffer outputs.
Definition context.h:667
Represents a depth test state used to configure depth testing.
Definition context.h:644
bool enabled
whether the depth test is enabled
Definition context.h:646
CompareFunction test_func
the function used to compare depth values
Definition context.h:648
configuration object used to define context parameters that need to be set already at creation time
Definition context.h:530
int version_minor
default: -1 ... minor version of maximum supported OpenGL version
Definition context.h:559
int version_major
default: -1 ... major version of maximum supported OpenGL version
Definition context.h:557
bool debug
default: false in release and true in debug version
Definition context.h:563
bool forward_compatible
default: false
Definition context.h:561
bool core_profile
default: true
Definition context.h:565
bool depth_buffer
default: true
Definition context.h:534
int max_compute_shared_memory_size
the maximum number that can be provided to the max_vertices output layout qualifier in a geometry sha...
Definition context.h:40
ivec3 max_compute_work_group_count
the number of invocations in a single local work group (i.e., the product of the three dimensions) th...
Definition context.h:42
int max_compute_work_group_invocations
total available storage size in bytes for all shared variables in a compute shader
Definition context.h:41
ivec3 max_compute_work_group_size
the maximum number of work groups that may be dispatched to a compute shader; dimension index 0,...
Definition context.h:43
bool on_enter_children(group *)
called before the children of a group node g are processed, return whether these should be skipped....
bool on_leave_children(group *)
called when the children of a group node g have been left, return whether to terminate traversal
compact type description of data that can be sent to the context; convertible to int
Definition context.h:55