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.ambient, alpha, GL_AMBIENT);
319 gl_set_material_color(side, mat.diffuse, alpha, GL_DIFFUSE);
320 gl_set_material_color(side, mat.specular, alpha, GL_SPECULAR);
321 gl_set_material_color(side, mat.emission, alpha, GL_EMISSION);
322 glMaterialf(side, GL_SHININESS, mat.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_RENDERBUFFER_SIZE, &gpu_capabilities.max_render_buffer_size);
480 for(unsigned i = 0; i < 3; ++i)
482 for(unsigned i = 0; i < 3; ++i)
484
485#ifdef _DEBUG
486 std::cout << "OpenGL version " << version_major << "." << version_minor << (core_profile?" (core)":" (compatibility)") << (debug?" (debug)":"") << (forward_compatible?" (forward_compatible)":"") << std::endl;
489 if (vendor_c_string)
490 std::cout << " vendor : " << vendor_c_string << std::endl;
492 std::cout << " renderer : " << renderer_c_string << std::endl;
494 std::cout << " glslversion: " << glslversion_c_string << std::endl;
495#endif
496 if (debug) {
498 if (version >= 43) {
500 if (!check_gl_error("gl_context::configure() debug output"))
501 glDebugMessageCallback(debug_callback, this);
502 }
503 }
504 // use the eye location to compute the specular lighting
505 if (!core_profile) {
507 // this makes opengl normalize all surface normals before lighting calculations,
508 // which is essential when using scaling to deform tesselated primities
510
511 // should be initialized by the driver, but better be safe than risk errors later
513 }
515// if (check_gl_error("gl_context::configure_gl before init of children"))
516// return false;
517
518 group_ptr grp(dynamic_cast<group*>(this));
520 for (unsigned i = 0; i<grp->get_nr_children(); ++i)
521 traverser(sma, "nc").traverse(grp->get_child(i));
522
523// if (check_gl_error("gl_context::configure_gl after init of children."))
524// return false;
525
526 return true;
527}
528
529void gl_context::resize_gl()
530{
531 group_ptr grp(dynamic_cast<group*>(this));
533 if (grp) {
536 }
537}
538
543
548
553
559
560void gl_context::clear_background(bool color_flag, bool depth_flag, bool stencil_flag, bool accum_flag) {
561 GLenum bits = 0;
562 if(color_flag)
564 if(depth_flag)
566 if(stencil_flag)
570 if(bits)
571 glClear(bits);
572}
573
576{
577 if (current_font_size == 0)
578 return get_info_font_size();
579 return current_font_size;
580}
588
589void gl_context::init_render_pass()
590{
591#ifdef WIN32
593#else
595#endif
598 else
600
604 for (unsigned i = 0; i < nr_default_light_sources; ++i)
606
607 for (unsigned i = 0; i < nr_default_light_sources; ++i)
610 else
612 }
613 else if ((last_render_pass_flags & RPF_SET_LIGHTS) == 0) {
614 for (unsigned i = 0; i < nr_default_light_sources; ++i)
617 }
619
622 }
624 // this mode allows to define the ambient and diffuse color of the surface material
625 // via the glColor commands
627 }
629 // set some default settings
631 set_cull_state(CM_BACKFACE);
632 if (!core_profile)
634 }
636 set_projection_matrix(cgv::math::perspective4<double>(45.0, (double)get_width()/get_height(),0.001,1000.0));
637
639 set_modelview_matrix(cgv::math::look_at4<double>(vec3(0,0,10), vec3(0,0,0), vec3(0,1,0)));
640
641 if (check_gl_error("gl_context::init_render_pass before init_frame"))
642 return;
643
644 group* grp = dynamic_cast<group*>(this);
648 }
649
650 if (check_gl_error("gl_context::init_render_pass after init_frame"))
651 return;
652 // this defines the background color to which the frame buffer is set by glClear
655 // this defines the background depth buffer value set by glClear
658 // this defines the background depth buffer value set by glClear
661 // this defines the background color to which the accum buffer is set by glClear
669 );
670}
671
673void gl_context::finish_render_pass()
674{
675}
676
678{
679 std::ostream& os;
680 format_callback_handler(std::ostream& _os) : os(_os)
681 {
682 }
685 {
686 os << "\a";
687 return false;
688 }
691 {
692 os << "\b";
693 return false;
694 }
695
696};
697
698void gl_context::draw_textual_info()
699{
700 if (show_help || show_stats) {
704
707
708 set_cursor(20, get_height()-1-20);
709
710 vec4 bg = get_bg_color();
711 //if (bg_r + bg_g + bg_b < 1.5f)
712 if (bg[0] + bg[1] + bg[2] < 1.5f)
713 set_color(rgba(1, 1, 1, 1));
714 else
715 set_color(rgba(0, 0, 0, 1));
716
717 // traverse objects for show_stats callback
718 format_callback_handler fch(output_stream());
719 group_ptr grp(dynamic_cast<group*>(this));
720 if (grp && show_stats) {
722 traverser(sma, "nc").traverse(grp, &fch);
723 output_stream() << std::endl;
724 }
725 //if (bg_r + bg_g + bg_b < 1.5f)
726 if(bg[0] + bg[1] + bg[2] < 1.5f)
727 set_color(rgba(1, 1, 0, 1));
728 else
729 set_color(rgba(0.4f, 0.3f, 0, 1));
730
731 if (grp && show_help) {
732 // collect help from myself and all children
734 traverser(sma, "nc").traverse(grp, &fch);
735 output_stream().flush();
736 }
739 }
740}
741
742void gl_context::perform_screen_shot()
743{
744 glFlush();
746 if (!read_frame_buffer(dv))
747 return;
748 std::string ext("bmp");
750 if (cgv::utils::is_element("png",exts))
751 ext = "png";
752 else if (cgv::utils::is_element("tif",exts))
753 ext = "tif";
754 cgv::media::image::image_writer wr(std::string("screen_shot.")+ext);
755 if (wr.is_format_supported(*dv.get_format()))
756 wr.write_image(dv);
757}
758
760void 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
761{
762 GLint count;
763 glGetProgramiv(get_gl_id(prog.handle), GL_ACTIVE_UNIFORMS, &count);
764 for (int i = 0; i < count; ++i) {
765 GLchar name[1000];
766 GLsizei length;
767 GLint size;
768 GLenum type;
769 glGetActiveUniform(get_gl_id(prog.handle), i, 1000, &length, &size, &type, name);
770 std::string name_str(name, length);
771 names.push_back(name_str);
772 if (sizes_ptr)
773 sizes_ptr->push_back(size);
774 if (types_ptr)
775 types_ptr->push_back(type);
776 int loc = glGetUniformLocation(get_gl_id(prog.handle), name_str.c_str());
777 if (locations_ptr)
778 locations_ptr->push_back(loc);
779 if (show)
780 std::cout << i << " at " << loc << " = " << name_str << ":" << type << "[" << size << "]" << std::endl;
781 }
782}
783
785void 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
786{
787 GLint count;
788 glGetProgramiv(get_gl_id(prog.handle), GL_ACTIVE_ATTRIBUTES, &count);
789 for (int i = 0; i < count; ++i) {
790 GLchar name[1000];
791 GLsizei length;
792 GLint size;
793 GLenum type;
794 glGetActiveAttrib(get_gl_id(prog.handle), i, 1000, &length, &size, &type, name);
795 std::string name_str(name, length);
796 names.push_back(name_str);
797 if (sizes_ptr)
798 sizes_ptr->push_back(size);
799 if (types_ptr)
800 types_ptr->push_back(type);
801 int loc = glGetAttribLocation(get_gl_id(prog.handle), name_str.c_str());
802 if (locations_ptr)
803 locations_ptr->push_back(loc);
804 if (show)
805 std::cout << i << " at " << loc << " = " << name_str << ":" << type << "[" << size << "]" << std::endl;
806 }
807}
808
811{
814 glColor4fv(&clr[0]);
815 }
816 if (shader_program_stack.empty())
817 return;
819 if (!prog.does_context_set_color())
820 return;
821 int clr_loc = prog.get_color_index();
822 if (clr_loc == -1)
823 return;
824 prog.set_attribute(*this, clr_loc, clr);
825}
826
829{
831 unsigned side = map_to_gl(MS_FRONT_AND_BACK);
832 float alpha = 1.0f - material.transparency;
833 gl_set_material_color(side, material.ambient_occlusion * material.diffuse_reflectance, alpha, GL_AMBIENT);
834 gl_set_material_color(side, material.diffuse_reflectance, alpha, GL_DIFFUSE);
835 gl_set_material_color(side, material.specular_reflectance, alpha, GL_SPECULAR);
836 gl_set_material_color(side, material.emission, alpha, GL_EMISSION);
837 glMaterialf(side, GL_SHININESS, 1.0f / (material.roughness + 1.0f / 128.0f));
838 }
839 context::set_material(material);
840}
841
844{
846 mat.enable_textures(*this);
847}
848
851{
852 mat.disable_textures(*this);
855}
856
857void gl_context::destruct_render_objects()
858{
859 for (unsigned i = 0; i < 4; ++i)
860 progs[i].destruct(*this);
861}
862
865{
866 if (!texture_support) {
867 if (!progs[0].is_created()) {
868 if (!progs[0].build_program(*this, "default.glpr")) {
869 error("could not build default shader program from default.glpr");
870 exit(0);
871 }
872 progs[0].specify_standard_uniforms(true, false, false, true);
873 progs[0].specify_standard_vertex_attribute_names(*this, true, false, false);
874 progs[0].allow_context_to_set_color(true);
875 }
876 return progs[0];
877 }
878 if (!progs[1].is_created()) {
879 if (!progs[1].build_program(*this, "textured_default.glpr")) {
880 error("could not build default shader program with texture support from textured_default.glpr");
881 exit(0);
882 }
883 progs[1].set_uniform(*this, "texture", 0);
884 progs[1].specify_standard_uniforms(true, false, false, true);
885 progs[1].specify_standard_vertex_attribute_names(*this, true, false, true);
886 progs[1].allow_context_to_set_color(true);
887 }
888 return progs[1];
889}
890
893{
894 if (!texture_support) {
895 if (!progs[2].is_created()) {
896 if (!progs[2].build_program(*this, "default_surface.glpr")) {
897 error("could not build surface shader program from default_surface.glpr");
898 exit(0);
899 }
900 progs[2].specify_standard_uniforms(true, true, true, true);
901 progs[2].specify_standard_vertex_attribute_names(*this, true, true, false);
902 progs[2].allow_context_to_set_color(true);
903 }
904 return progs[2];
905 }
906 if (!progs[3].is_created()) {
907 if (!progs[3].build_program(*this, "textured_surface.glpr")) {
908 error("could not build surface shader program with texture support from textured_surface.glpr");
909 exit(0);
910 }
911 progs[3].specify_standard_uniforms(true, true, true, true);
912 progs[3].specify_standard_vertex_attribute_names(*this, true, true, true);
913 progs[3].allow_context_to_set_color(true);
914 }
915 return progs[3];
916}
917
919{
921 GLint max_nr_lights;
922 glGetIntegerv(GL_MAX_LIGHTS, &max_nr_lights);
923 for (GLint light_idx = 0; light_idx < max_nr_lights; ++light_idx) {
926 continue;
927 }
928 GLfloat col[4] = { 1,1,1,1 };
930 *(rgb*)col = light.get_emission()*light.get_ambient_scale();
932 *(rgb*)col = light.get_emission();
934 *(rgb*)col = light.get_emission();
936
937 GLfloat pos[4] = { 0,0,0,light.get_type() == cgv::media::illum::LT_DIRECTIONAL ? 0.0f : 1.0f };
938 *(vec3*)pos = light.get_position();
940 if (light.get_type() != cgv::media::illum::LT_DIRECTIONAL) {
941 glLightf(GL_LIGHT0 + light_idx, GL_CONSTANT_ATTENUATION, light.get_constant_attenuation());
942 glLightf(GL_LIGHT0 + light_idx, GL_LINEAR_ATTENUATION, light.get_linear_attenuation());
943 glLightf(GL_LIGHT0 + light_idx, GL_QUADRATIC_ATTENUATION, light.get_quadratic_attenuation());
944 }
945 else {
949 }
950 if (light.get_type() == cgv::media::illum::LT_SPOT) {
951 glLightf(GL_LIGHT0 + light_idx, GL_SPOT_CUTOFF, light.get_spot_cutoff());
952 glLightf(GL_LIGHT0 + light_idx, GL_SPOT_EXPONENT, light.get_spot_exponent());
953 glLightfv(GL_LIGHT0 + light_idx, GL_SPOT_DIRECTION, light.get_spot_direction().data());
954 }
955 else {
958 static float dir[3] = { 0,0,1 };
960 }
962 }
963 }
965}
966
967void gl_context::tesselate_arrow(double length, double aspect, double rel_tip_radius, double tip_aspect, int res, bool edges)
968{
969 double cyl_radius = length*aspect;
972 double cyl_length = length - cone_length;
976 mul_modelview_matrix(cgv::math::rotate4(180.0,1.0,0.0,0.0));
977 tesselate_unit_disk(res, false, edges);
979
980 mul_modelview_matrix(cgv::math::translate4(0.0,0.0,1.0));
981 tesselate_unit_cylinder(res, false, edges);
982
983 mul_modelview_matrix(cgv::math::translate4(0.0, 0.0, 1.0));
986 mul_modelview_matrix(cgv::math::rotate4(180.0, 1.0, 0.0, 0.0));
987 tesselate_unit_disk(res, false, edges);
989 mul_modelview_matrix(cgv::math::translate4(0.0, 0.0, 1.0));
990 tesselate_unit_cone(res, false, edges);
992}
993
995void gl_context::rotate_vector_to_target(const dvec3& vector, const dvec3& target)
996{
997 double angle;
998 dvec3 axis;
999 compute_rotation_axis_and_angle_from_vector_pair(vector, target, axis, angle);
1000 mul_modelview_matrix(cgv::math::rotate4<double>(180.0 / M_PI * angle, axis));
1001}
1002
1004void gl_context::tesselate_arrow(const dvec3& start, const dvec3& end, double aspect, double rel_tip_radius, double tip_aspect, int res, bool edges)
1005{
1006 if ((start - end).length() < 1e-8) {
1007 error("ignored tesselate arrow called with start and end closer then 1e-8");
1008 return;
1009 }
1011 mul_modelview_matrix(cgv::math::translate4<double>(start));
1012 rotate_vector_to_target(dvec3(0, 0, 1), end - start);
1013 tesselate_arrow((end-start).length(), aspect, rel_tip_radius, tip_aspect, res, edges);
1015}
1016
1018{
1019 set_color(i*l.get_emission());
1021 switch (l.get_type()) {
1022 case LT_DIRECTIONAL :
1023 mul_modelview_matrix(cgv::math::scale4<double>(light_scale, light_scale, light_scale));
1024 tesselate_arrow(vec3(0.0f), l.get_position(), 0.1f,2.0f,0.5f);
1025 break;
1026 case LT_POINT :
1028 cgv::math::translate4<double>(l.get_position())*
1029 cgv::math::scale4<double>(vec3(0.3f*light_scale)));
1031 break;
1032 case LT_SPOT :
1034 cgv::math::translate4<double>(l.get_position())*
1035 cgv::math::scale4<double>(vec3(light_scale))
1036 );
1037 rotate_vector_to_target(dvec3(0, 0, -1), l.get_spot_direction());
1038 {
1039 float t = tan(l.get_spot_cutoff()*(float)M_PI/180);
1040 if (l.get_spot_cutoff() > 45.0f)
1041 mul_modelview_matrix(cgv::math::scale4<double>(1, 1, 0.5f / t));
1042 else
1043 mul_modelview_matrix(cgv::math::scale4<double>(t, t, 0.5f));
1044 mul_modelview_matrix(cgv::math::translate4<double>(0, 0, -1));
1046 set_cull_state(CM_OFF);
1049 }
1050 }
1052}
1053
1055{
1056 if (frame_buffer_stack.empty()) {
1057 error("gl_context::announce_external_frame_buffer_change() called with empty frame buffer stack");
1058 return;
1059 }
1060 GLint fbo_id = 0;
1062 fbo_handle = frame_buffer_stack.top()->handle;
1063 frame_buffer_stack.top()->handle = get_handle(fbo_id);
1064}
1065
1067{
1068 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbo_handle));
1069 if (frame_buffer_stack.empty())
1070 return;
1071 frame_buffer_stack.top()->handle = fbo_handle;
1072}
1073
1075{
1076 if (window_transformation_stack.empty()) {
1077 error("gl_context::announce_external_viewport_change() called with empty window transformation stack");
1078 return;
1079 }
1080 GLint vp[4];
1082 cgv_viewport_storage = window_transformation_stack.top().front().viewport;
1083 window_transformation_stack.top().front().viewport = ivec4(vp[0], vp[1], vp[2], vp[3]);
1084}
1085
1093
1096{
1099
1100 GLint vp[4];
1102
1104 // push projection matrix
1106 // set orthogonal projection
1108 glOrtho(vp[0], vp[0]+vp[2], vp[1], vp[1]+vp[3], -1, 1);
1109 // push modelview matrix
1111 // use identity for modelview
1113 }
1114 set_modelview_matrix(cgv::math::identity4<double>());
1115 set_projection_matrix(cgv::math::ortho4<double>(vp[0], vp[0] + vp[2], vp[1], vp[1]+vp[3], -1, 1));
1116}
1117
1124
1127 unsigned int x, unsigned int y, FrameBufferType buffer_type,
1128 TypeId type, data::ComponentFormat cf, int w, int h)
1129{
1130 const cgv::data::data_format* df = dv.get_format();
1131 if (df) {
1132 w = int(df->get_width());
1133 h = int(df->get_height());
1134 type = df->get_component_type();
1136 if (w < 1 || h < 1) {
1137 error(std::string("read_frame_buffer: received invalid dimensions (") + cgv::utils::to_string(w) + "," + cgv::utils::to_string(h) + ")");
1138 return false;
1139 }
1140 }
1141 else {
1142 if (w < 1 || h < 1) {
1143 GLint vp[4];
1145 w = w < 1 ? vp[2] : w;
1146 h = h < 1 ? vp[3] : h;
1147 }
1148 }
1149 GLuint gl_type = map_to_gl(type);
1150 if (gl_type == 0) {
1151 error(std::string("read_frame_buffer: could not make component type ")+cgv::type::info::get_type_name(df->get_component_type())+" to gl type");
1152 return false;
1153 }
1155 if (cf != cgv::data::CF_D) {
1157 if (cf != cgv::data::CF_S) {
1158 gl_format = map_to_gl(cf);
1159 if (gl_format == GL_RGB && cf != cgv::data::CF_RGB) {
1160 error(std::string("read_frame_buffer: could not match component format ") + cgv::utils::to_string(df->get_component_format()));
1161 return false;
1162 }
1163 }
1164 }
1166 if (buffer_type < FB_BACK)
1168 else {
1169 switch (buffer_type) {
1170 case FB_FRONT : gl_buffer = GL_FRONT; break;
1171 case FB_BACK : gl_buffer = GL_BACK; break;
1172 case FB_FRONT_LEFT : gl_buffer = GL_FRONT_LEFT; break;
1173 case FB_FRONT_RIGHT : gl_buffer = GL_FRONT_RIGHT; break;
1174 case FB_BACK_LEFT : gl_buffer = GL_BACK_LEFT; break;
1175 case FB_BACK_RIGHT : gl_buffer = GL_BACK_RIGHT; break;
1176 default:
1177 error(std::string("invalid buffer type ")+cgv::utils::to_string(buffer_type));
1178 return false;
1179 }
1180 }
1181 // after all necessary information could be extracted, ensure that format
1182 // and data view are created
1183 if (!df) {
1184 df = new cgv::data::data_format(w,h,type,cf);
1185 dv.~data_view();
1186 new(&dv) data::data_view(df);
1187 dv.manage_format(true);
1188 }
1194 glReadPixels(x,y,w,h,gl_format,gl_type,dv.get_ptr<void>());
1198 return true;
1199}
1200
1201
1202void render_vertex(int k, const float* vertices, const float* normals, const float* tex_coords,
1203 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices, bool flip_normals)
1204{
1205 if (normals && normal_indices) {
1206 if (flip_normals) {
1207 float n[3] = { -normals[3*normal_indices[k]],-normals[3*normal_indices[k]+1],-normals[3*normal_indices[k]+2] };
1208 glNormal3fv(n);
1209 }
1210 else
1211 glNormal3fv(normals+3*normal_indices[k]);
1212 }
1213 if (tex_coords && tex_coord_indices)
1214 glTexCoord2fv(tex_coords+2*tex_coord_indices[k]);
1215 glVertex3fv(vertices+3*vertex_indices[k]);
1216}
1217
1218attribute_array_binding*& get_aab_ptr()
1219{
1220 static attribute_array_binding* aab_ptr = 0;
1221 return aab_ptr;
1222}
1223
1224vertex_buffer*& get_vbo_ptr()
1225{
1226 static vertex_buffer* vbo_ptr = 0;
1227 return vbo_ptr;
1228}
1229
1230bool gl_context::release_attributes(const float* normals, const float* tex_coords, const int* normal_indices, const int* tex_coord_indices) const
1231{
1232 shader_program* prog_ptr = static_cast<shader_program*>(get_current_program());
1233 if (!prog_ptr || prog_ptr->get_position_index() == -1)
1234 return false;
1235
1236 attribute_array_binding*& aab_ptr = get_aab_ptr();
1237 if (!aab_ptr) {
1238 attribute_array_binding::disable_global_array(*this, prog_ptr->get_position_index());
1239 if (prog_ptr->get_normal_index() != -1 && normals && normal_indices)
1240 attribute_array_binding::disable_global_array(*this, prog_ptr->get_normal_index());
1241 if (prog_ptr->get_texcoord_index() != -1 && tex_coords && tex_coord_indices)
1242 attribute_array_binding::disable_global_array(*this, prog_ptr->get_texcoord_index());
1243 }
1244 else {
1245 aab_ptr->disable(const_cast<gl_context&>(*this));
1246 aab_ptr->disable_array(*this, prog_ptr->get_position_index());
1247 if (prog_ptr->get_normal_index() != -1 && normals && normal_indices)
1248 aab_ptr->disable_global_array(*this, prog_ptr->get_normal_index());
1249 if (prog_ptr->get_texcoord_index() != -1 && tex_coords && tex_coord_indices)
1250 aab_ptr->disable_global_array(*this, prog_ptr->get_texcoord_index());
1251 vertex_buffer*& vbo_ptr = get_vbo_ptr();
1252 vbo_ptr->destruct(*this);
1253 delete vbo_ptr;
1254 vbo_ptr = 0;
1255 }
1256 return true;
1257}
1258
1259bool gl_context::prepare_attributes(std::vector<vec3>& P, std::vector<vec3>& N, std::vector<vec2>& T, unsigned nr_vertices,
1260 const float* vertices, const float* normals, const float* tex_coords,
1261 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices, bool flip_normals) const
1262{
1263 unsigned i;
1264 shader_program* prog_ptr = static_cast<shader_program*>(get_current_program());
1265 if (!prog_ptr || prog_ptr->get_position_index() == -1)
1266 return false;
1267 P.resize(nr_vertices);
1268 for (i = 0; i < nr_vertices; ++i)
1269 P[i] = *reinterpret_cast<const vec3*>(vertices + 3 * vertex_indices[i]);
1270
1271 if (prog_ptr->get_normal_index() != -1 && normals && normal_indices) {
1272 N.resize(nr_vertices);
1273 for (i = 0; i < nr_vertices; ++i) {
1274 N[i] = *reinterpret_cast<const vec3*>(normals + 3 * normal_indices[i]);
1275 if (flip_normals)
1276 N[i] = -N[i];
1277 }
1278 }
1279 if (prog_ptr->get_texcoord_index() != -1 && tex_coords && tex_coord_indices) {
1280 T.resize(nr_vertices);
1281 for (i = 0; i < nr_vertices; ++i)
1282 T[i] = *reinterpret_cast<const vec2*>(tex_coords + 2 * tex_coord_indices[i]);
1283 }
1284
1285 attribute_array_binding*& aab_ptr = get_aab_ptr();
1286 if (core_profile && !aab_ptr) {
1287 aab_ptr = new attribute_array_binding();
1288 aab_ptr->create(*this);
1289 }
1290 if (!aab_ptr) {
1291 attribute_array_binding::set_global_attribute_array<>(*this, prog_ptr->get_position_index(), P);
1292 attribute_array_binding::enable_global_array(*this, prog_ptr->get_position_index());
1293 if (prog_ptr->get_normal_index() != -1) {
1294 if (normals && normal_indices) {
1295 attribute_array_binding::set_global_attribute_array<>(*this, prog_ptr->get_normal_index(), N);
1296 attribute_array_binding::enable_global_array(*this, prog_ptr->get_normal_index());
1297 }
1298 else
1299 attribute_array_binding::disable_global_array(*this, prog_ptr->get_normal_index());
1300 }
1301 if (prog_ptr->get_texcoord_index() != -1) {
1302 if (tex_coords && tex_coord_indices) {
1303 attribute_array_binding::set_global_attribute_array<>(*this, prog_ptr->get_texcoord_index(), T);
1304 attribute_array_binding::enable_global_array(*this, prog_ptr->get_texcoord_index());
1305 }
1306 else
1307 attribute_array_binding::disable_global_array(*this, prog_ptr->get_texcoord_index());
1308 }
1309 }
1310 else {
1311 vertex_buffer*& vbo_ptr = get_vbo_ptr();
1312 if (!vbo_ptr)
1313 vbo_ptr = new vertex_buffer();
1314 else
1315 vbo_ptr->destruct(*this);
1316 vbo_ptr->create(*this, P.size() * sizeof(vec3) + N.size() * sizeof(vec3) + T.size() * sizeof(vec2));
1317 vbo_ptr->replace(const_cast<gl_context&>(*this), 0, P.data(), P.size());
1318 size_t nml_off = P.size() * sizeof(vec3);
1319 size_t tex_off = nml_off;
1320 if (!N.empty()) {
1321 vbo_ptr->replace(const_cast<gl_context&>(*this), nml_off, N.data(), N.size());
1322 tex_off += N.size() * sizeof(vec3);
1323 }
1324 if (!T.empty())
1325 vbo_ptr->replace(const_cast<gl_context&>(*this), tex_off, T.data(), T.size());
1326
1328 aab_ptr->set_attribute_array(*this, prog_ptr->get_position_index(), td3, *vbo_ptr, 0, P.size());
1329 aab_ptr->enable_array(*this, prog_ptr->get_position_index());
1330 if (prog_ptr->get_normal_index() != -1) {
1331 if (normals && normal_indices) {
1332 aab_ptr->set_attribute_array(*this, prog_ptr->get_normal_index(), td3, *vbo_ptr, nml_off, N.size());
1333 aab_ptr->enable_array(*this, prog_ptr->get_normal_index());
1334 }
1335 else
1336 aab_ptr->disable_array(*this, prog_ptr->get_normal_index());
1337 }
1338 if (prog_ptr->get_texcoord_index() != -1) {
1339 if (tex_coords && tex_coord_indices) {
1341 aab_ptr->set_attribute_array(*this, prog_ptr->get_texcoord_index(), td2, *vbo_ptr, tex_off, T.size());
1342 aab_ptr->enable_array(*this, prog_ptr->get_texcoord_index());
1343 }
1344 else
1345 aab_ptr->disable_array(*this, prog_ptr->get_texcoord_index());
1346 }
1347 aab_ptr->enable(const_cast<gl_context&>(*this));
1348 }
1349 return true;
1350}
1351
1354 const float* vertices, const float* normals, const float* tex_coords,
1355 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
1356 int nr_faces, int face_degree, bool flip_normals) const
1357{
1359 int k = 0;
1360 for (int i = 0; i < nr_faces; ++i) {
1362 for (int j = 0; j < face_degree; ++j, ++k)
1363 render_vertex(k, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1364 glEnd();
1365 }
1366 return;
1367 }
1368 unsigned nr_vertices = face_degree * nr_faces;
1369 std::vector<vec3> P, N;
1370 std::vector<vec2> T;
1371 if (!prepare_attributes(P, N, T, nr_vertices, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals))
1372 return;
1373 for (int i = 0; i < nr_faces; ++i)
1374 glDrawArrays(GL_LINE_LOOP, i*face_degree, face_degree);
1375 release_attributes(normals, tex_coords, normal_indices, tex_coord_indices);
1376}
1377
1378
1379void gl_context::draw_elements_void(GLenum mode, size_t total_count, GLenum type, size_t type_size, const void* indices) const
1380{
1381 ensure_configured();
1382 size_t drawn = 0;
1383 const cgv::type::uint8_type* index_ptr = static_cast<const cgv::type::uint8_type*>(indices);
1384 while (drawn < total_count) {
1385 size_t count = total_count - drawn;
1386 if (count > max_nr_indices)
1387 count = size_t(max_nr_indices);
1388 glDrawElements(mode, GLsizei(count), type, index_ptr + drawn * type_size);
1389 drawn += count;
1390 }
1391}
1392
1393size_t max_nr_indices, max_nr_vertices;
1394void gl_context::ensure_configured() const
1395{
1396 if (max_nr_indices != 0)
1397 return;
1398 glGetInteger64v(GL_MAX_ELEMENTS_INDICES, reinterpret_cast<GLint64*>(&max_nr_indices));
1399 glGetInteger64v(GL_MAX_ELEMENTS_VERTICES, reinterpret_cast<GLint64*>(&max_nr_vertices));
1400}
1401
1404 const float* vertices, const float* normals, const float* tex_coords,
1405 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
1406 int nr_faces, int face_degree, bool is_fan, bool flip_normals) const
1407{
1408 int s = face_degree - 2;
1409 int i, k = 2;
1410 std::vector<GLuint> I;
1411 I.push_back(0); I.push_back(1);
1412 for (i = 0; i < nr_faces; ++i) {
1413 if (is_fan) {
1414 I.push_back(k - 1); I.push_back(k);
1415 I.push_back(0); I.push_back(k);
1416 continue;
1417 }
1418 if (s == 1) {
1419 I.push_back(k - 1); I.push_back(k);
1420 I.push_back(k - 2); I.push_back(k);
1421 }
1422 else {
1423 I.push_back(k - 1); I.push_back(k + 1);
1424 I.push_back(k - 2); I.push_back(k);
1425 I.push_back(k); I.push_back(k + 1);
1426 }
1427 k += 2;
1428 }
1429
1432 for (GLuint j:I)
1433 render_vertex(j, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1434 glEnd();
1435 return;
1436 }
1437 unsigned nr_vertices = 2 + (face_degree - 2) * nr_faces;
1438 std::vector<vec3> P, N;
1439 std::vector<vec2> T;
1440 if (!prepare_attributes(P, N, T, nr_vertices, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals))
1441 return;
1442 draw_elements(GL_LINES, I.size(), &I[0]);
1443 release_attributes(normals, tex_coords, normal_indices, tex_coord_indices);
1444}
1445
1447 const float* vertices, const float* normals, const float* tex_coords,
1448 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
1449 int nr_faces, int face_degree, bool flip_normals) const
1450{
1452 int k = 0;
1453 if (face_degree < 5)
1454 glBegin(face_degree == 3 ? GL_TRIANGLES : GL_QUADS);
1455 for (int i = 0; i < nr_faces; ++i) {
1456 if (face_degree >= 5)
1458 for (int j = 0; j < face_degree; ++j, ++k)
1459 render_vertex(k, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1460 if (face_degree >= 5)
1461 glEnd();
1462 }
1463 if (face_degree < 5)
1464 glEnd();
1465 return;
1466 }
1467 unsigned nr_vertices = face_degree * nr_faces;
1468 std::vector<vec3> P, N;
1469 std::vector<vec2> T;
1470 if (!prepare_attributes(P, N, T, nr_vertices, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals))
1471 return;
1472 /*
1473 for (unsigned x = 0; x < nr_vertices; ++x) {
1474 std::cout << x << ": [" << P[x] << "]";
1475 if (N.size() > 0)
1476 std::cout << ", <" << N[x] << ">";
1477 if (T.size() > 0)
1478 std::cout << ", {" << T[x] << "}";
1479 std::cout << std::endl;
1480 }
1481 */
1482 if (face_degree == 3)
1483 glDrawArrays(GL_TRIANGLES, 0, nr_vertices);
1484 else {
1485 for (int i = 0; i < nr_faces; ++i) {
1486 glDrawArrays(GL_TRIANGLE_FAN, i*face_degree, face_degree);
1487 }
1488 }
1489 release_attributes(normals, tex_coords, normal_indices, tex_coord_indices);
1490}
1491
1493 const float* vertices, const float* normals, const float* tex_coords,
1494 const int* vertex_indices, const int* normal_indices, const int* tex_coord_indices,
1495 int nr_faces, int face_degree, bool is_fan, bool flip_normals) const
1496{
1497 int s = face_degree - 2;
1498 int k = 2;
1500 glBegin(face_degree == 3 ? (is_fan ? GL_TRIANGLE_FAN : GL_TRIANGLE_STRIP) : GL_QUAD_STRIP);
1501 render_vertex(0, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1502 render_vertex(1, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1503 for (int i = 0; i < nr_faces; ++i)
1504 for (int j = 0; j < s; ++j, ++k)
1505 render_vertex(k, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals);
1506 glEnd();
1507 return;
1508 }
1509 unsigned nr_vertices = 2 + (face_degree-2) * nr_faces;
1510 std::vector<vec3> P, N;
1511 std::vector<vec2> T;
1512 if (!prepare_attributes(P, N, T, nr_vertices, vertices, normals, tex_coords, vertex_indices, normal_indices, tex_coord_indices, flip_normals))
1513 return;
1515 release_attributes(normals, tex_coords, normal_indices, tex_coord_indices);
1516}
1517
1519 if(state.enabled)
1521 else
1523 glDepthFunc(map_to_gl(state.test_func));
1525}
1526
1528 glDepthFunc(map_to_gl(func));
1530}
1531
1536
1541
1543 switch(culling_mode) {
1544 case CM_OFF:
1546 break;
1547 case CM_BACKFACE:
1550 break;
1551 case CM_FRONTFACE:
1554 break;
1555 default:
1556 break;
1557 }
1558 context::set_cull_state(culling_mode);
1559}
1560
1562 if(state.enabled)
1564 else
1568 map_to_gl(state.src_color),
1569 map_to_gl(state.dst_color),
1570 map_to_gl(state.src_alpha),
1571 map_to_gl(state.dst_alpha)
1572 );
1573 } else {
1574 glBlendFunc(map_to_gl(state.src_color), map_to_gl(state.dst_color));
1575 }
1577}
1578
1583
1593
1598
1603
1605 glDepthMask(map_to_gl(mask.depth_flag));
1607 map_to_gl(mask.red_flag),
1608 map_to_gl(mask.green_flag),
1609 map_to_gl(mask.blue_flag),
1610 map_to_gl(mask.alpha_flag)
1611 );
1613}
1614
1616 glDepthMask(map_to_gl(flag));
1618}
1619
1622 map_to_gl(flags[0]),
1623 map_to_gl(flags[1]),
1624 map_to_gl(flags[2]),
1625 map_to_gl(flags[3])
1626 );
1628}
1629
1632{
1634 GLdouble V[16];
1636 return dmat4(4,4,V);
1637 }
1638 if (modelview_matrix_stack.empty())
1639 return identity4<double>();
1640 return modelview_matrix_stack.top();
1641}
1642
1645{
1647 GLdouble P[16];
1649 return dmat4(4,4,P);
1650 }
1651 if (projection_matrix_stack.empty())
1652 return identity4<double>();
1653 return projection_matrix_stack.top();
1654}
1657{
1659 update_window_transformation_array();
1660}
1661
1662void gl_context::update_window_transformation_array()
1663{
1664 const std::vector<window_transformation>& wta = window_transformation_stack.top();
1665 if (wta.size() == 1) {
1666 const ivec4& viewport = wta.front().viewport;
1667 const dvec2& depth_range = wta.front().depth_range;
1668 glViewport(viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1669 glScissor(viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1670 glDepthRange(depth_range[0], depth_range[1]);
1671 }
1672 else {
1673 for (GLuint array_index = 0; array_index < (GLuint)wta.size(); ++array_index) {
1674 const ivec4& viewport = wta[array_index].viewport;
1675 const dvec2& depth_range = wta[array_index].depth_range;
1676 glViewportIndexedf(array_index, (GLfloat)viewport[0], (GLfloat)viewport[1], (GLfloat)viewport[2], (GLfloat)viewport[3]);
1677 glScissorIndexed(array_index, viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1678 glDepthRangeIndexed(array_index, (GLclampd)depth_range[0], (GLclampd)depth_range[1]);
1679 }
1680 }
1681}
1682
1684{
1685 GLint max_value = 1;
1686 if (GLEW_VERSION_4_1)
1687 glGetIntegerv(GL_MAX_VIEWPORTS, &max_value);
1688 return max_value;
1689}
1690
1693{
1694 size_t nr = window_transformation_stack.top().size();
1696 if (nr != window_transformation_stack.top().size())
1697 update_window_transformation_array();
1698 else {
1699 if (array_index < 1) {
1700 glViewport(viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1701 glScissor(viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1702 }
1703 else {
1704 glViewportIndexedf(array_index, (GLfloat)viewport[0], (GLfloat)viewport[1], (GLfloat)viewport[2], (GLfloat)viewport[3]);
1705 glScissorIndexed(array_index, viewport[0], viewport[1], (GLsizei)viewport[2], (GLsizei)viewport[3]);
1706 }
1707 }
1708}
1709
1711void gl_context::set_depth_range(const dvec2& depth_range, int array_index)
1712{
1713 size_t nr = window_transformation_stack.top().size();
1715 if (nr != window_transformation_stack.top().size())
1716 update_window_transformation_array();
1717 else {
1718 if (array_index < 1)
1719 glDepthRange(depth_range[0], depth_range[1]);
1720 else
1721 glDepthRangeIndexed(array_index, (GLclampd)depth_range[0], (GLclampd)depth_range[1]);
1722 }
1723}
1724/*
1726gl_context::dmat4 gl_context::get_device_matrix() const
1727{
1728 GLint vp[4];
1729 glGetIntegerv(GL_VIEWPORT, vp);
1730 dmat4 D; D.zeros();
1731 D(0, 0) = 0.5*vp[2];
1732 D(0, 3) = 0.5*vp[2] + vp[0];
1733 D(1, 1) = -0.5*vp[3]; // flip y-coordinate
1734 D(1, 3) = get_height() - 0.5*vp[3] - vp[1];
1735 D(2, 2) = 0.5;
1736 D(2, 3) = 0.5;
1737 D(3, 3) = 1.0;
1738 return D;
1739}
1740*/
1752
1764
1767{
1769
1770 if (!in_render_process() && !is_current())
1771 make_current();
1772
1774 /*
1775 GLenum err = glGetError();
1776 if (err != GL_NO_ERROR) {
1777 std::cout << "gl error:";
1778 switch (err) {
1779 case GL_INVALID_ENUM : std::cout << "invalid enum"; break;
1780 case GL_INVALID_VALUE : std::cout << "invalid value"; break;
1781 case GL_INVALID_OPERATION : std::cout << "invalid operation"; break;
1782 case GL_STACK_OVERFLOW : std::cout << "stack overflow"; break;
1783 case GL_STACK_UNDERFLOW : std::cout << "stack underflow"; break;
1784 case GL_OUT_OF_MEMORY : std::cout << "out of memory"; break;
1785 default: std::cout << "unknown error"; break;
1786 }
1787 std::cout << std::endl;
1788
1789 }
1790 */
1791 return z_window;
1792}
1793
1794#include <iomanip>
1795cgv::data::component_format gl_context::texture_find_best_format(
1797 render_component& rc, const std::vector<cgv::data::data_view>* palettes) const
1798{
1799 GLuint& gl_format = (GLuint&)rc.internal_format;
1801 if (debug_texture_format_matching)
1802 std::cout << "Texture Format Search for '" << cf << "':" << std::endl;
1803 gl_format = find_best_texture_format(cf, &best_cf, palettes, debug_texture_format_matching);
1804 if (debug_texture_format_matching)
1805 std::cout << "found: '" << best_cf << "' = " << std::hex << gl_format << std::endl;
1806 return best_cf;
1807}
1808
1809std::string gl_error_to_string(GLenum eid) {
1810 switch (eid) {
1811 case GL_NO_ERROR: return "";
1812 case GL_INVALID_ENUM: return "invalid enum";
1813 case GL_INVALID_VALUE: return "invalid value";
1814 case GL_INVALID_OPERATION: return "invalid operation";
1815 case GL_INVALID_FRAMEBUFFER_OPERATION: return "invalid framebuffe";
1816 case GL_OUT_OF_MEMORY: return "out of memory";
1817 case GL_STACK_UNDERFLOW: return "stack underflow";
1818 case GL_STACK_OVERFLOW: return "stack overflow";
1819 default:
1820 return "undefined error (id: " + std::to_string(eid) + ")";
1821 }
1822 //return std::string((const char*)gluErrorString(eid));
1823}
1824
1825std::string gl_error() {
1826 GLenum eid = glGetError();
1827 // Clear potential other error flags, see https://registry.khronos.org/OpenGL-Refpages/gl4/html/glGetError.xhtml:
1828 // "glGetError should always be called in a loop, until it returns GL_NO_ERROR, if all error flags are to be reset."
1829 while (glGetError());
1830 return gl_error_to_string(eid);
1831}
1832
1833bool gl_context::check_gl_error(const std::string& where, const cgv::render::render_component* rc) const
1834{
1835 GLenum eid = glGetError();
1836 if (eid == GL_NO_ERROR)
1837 return false;
1838 std::string error_string = where + ": " + gl_error_to_string(eid);
1840 while (glGetError()); // Clear all error flags, see gl_error.
1841 return true;
1842}
1843
1844bool gl_context::check_texture_support(TextureType tt, const std::string& where, const cgv::render::render_component* rc) const
1845{
1846 switch (tt) {
1847 case TT_3D:
1848 if (!GLEW_VERSION_1_2) {
1849 error(where + ": 3D texture not supported", rc);
1850 return false;
1851 }
1852 break;
1853 case TT_CUBEMAP:
1854 if (!GLEW_VERSION_1_3) {
1855 error(where + ": cubemap texture not supported", rc);
1856 return false;
1857 }
1858 default:
1859 break;
1860 }
1861 return true;
1862}
1863
1864bool gl_context::check_shader_support(ShaderType st, const std::string& where, const cgv::render::render_component* rc) const
1865{
1866 switch (st) {
1867 case ST_COMPUTE:
1868 if (GLEW_VERSION_4_3)
1869 return true;
1870 else {
1871 error(where+": compute shader need not supported OpenGL version 4.3", rc);
1872 return false;
1873 }
1874 case ST_TESS_CONTROL:
1875 case ST_TESS_EVALUATION:
1876 if (GLEW_VERSION_4_0)
1877 return true;
1878 else {
1879 error(where+": tessellation shader need not supported OpenGL version 4.0", rc);
1880 return false;
1881 }
1882 case ST_GEOMETRY:
1883 if (GLEW_VERSION_3_2)
1884 return true;
1885 else {
1886 error(where + ": geometry shader need not supported OpenGL version 3.2", rc);
1887 return false;
1888 }
1889 default:
1890 if (GLEW_VERSION_2_0)
1891 return true;
1892 else {
1893 error(where + ": shaders need not supported OpenGL version 2.0", rc);
1894 return false;
1895 }
1896 }
1897}
1898
1899bool gl_context::check_fbo_support(const std::string& where, const cgv::render::render_component* rc) const
1900{
1901 if (!GLEW_VERSION_3_0) {
1902 error(where + ": framebuffer objects not supported", rc);
1903 return false;
1904 }
1905 return true;
1906}
1907
1908GLuint gl_context::texture_generate(texture_base& tb) const
1909{
1910 if (!check_texture_support(tb.tt, "gl_context::texture_generate", &tb))
1911 return get_gl_id(0);
1912 GLuint tex_id = get_gl_id(0);
1913 glGenTextures(1, &tex_id);
1915 error("gl_context::texture_generate: attempt to create texture inside glBegin-glEnd-block", &tb);
1916 return tex_id;
1917}
1918
1919GLuint gl_context::texture_bind(TextureType tt, GLuint tex_id) const
1920{
1921 GLint tmp_id;
1922 glGetIntegerv(get_tex_bind(tt), &tmp_id);
1923 glBindTexture(get_tex_dim(tt), tex_id);
1924 return tmp_id;
1925}
1926
1927void gl_context::texture_unbind(TextureType tt, GLuint tmp_id) const
1928{
1929 glBindTexture(get_tex_dim(tt), tmp_id);
1930}
1931
1932bool gl_context::texture_create(texture_base& tb, cgv::data::data_format& df) const
1933{
1934 GLuint gl_format = (const GLuint&) tb.internal_format;
1935
1936 if (tb.tt == TT_UNDEF)
1937 tb.tt = (TextureType)df.get_nr_dimensions();
1938 GLuint tex_id = texture_generate(tb);
1939 if (tex_id == get_gl_id(0))
1940 return false;
1941 GLuint tmp_id = texture_bind(tb.tt, tex_id);
1942
1943 // extract component type
1944 unsigned int transfer_format = map_to_gl(df.get_standard_component_format(), df.get_integer_interpretation());
1945 if (transfer_format == -1) {
1946 error("could not determine transfer format", &tb);
1947 return false;
1948 }
1949 switch (tb.tt) {
1950 case TT_1D :
1952 gl_format, GLsizei(df.get_width()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1953 break;
1954 case TT_1D_ARRAY :
1956 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1957 break;
1958 case TT_2D :
1959 glTexImage2D(GL_TEXTURE_2D, 0, gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1960 break;
1961 case TT_MULTISAMPLE_2D:
1962 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));
1963 break;
1964 case TT_2D_ARRAY :
1965 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);
1966 break;
1967 case TT_MULTISAMPLE_2D_ARRAY:
1968 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));
1969 break;
1970 case TT_3D :
1972 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), GLsizei(df.get_depth()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1973 break;
1974 case TT_CUBEMAP :
1976 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1978 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1980 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1982 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1984 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1986 gl_format, GLsizei(df.get_width()), GLsizei(df.get_height()), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
1987 default:
1988 break;
1989 }
1990 if (check_gl_error("gl_context::texture_create", &tb)) {
1991 glDeleteTextures(1, &tex_id);
1992 texture_unbind(tb.tt, tmp_id);
1993 return false;
1994 }
1995
1996 texture_unbind(tb.tt, tmp_id);
1997 tb.have_mipmaps = false;
1998 tb.handle = get_handle(tex_id);
1999 return true;
2000}
2001
2002bool gl_context::texture_create(
2003 texture_base& tb,
2005 const cgv::data::const_data_view& data,
2006 int level, int cube_side, int num_array_layers, const std::vector<cgv::data::data_view>* palettes) const
2007{
2008 // query the format to be used for the texture
2009 GLuint gl_tex_format = (const GLuint&) tb.internal_format;
2010
2011 // define the texture type from the data format and the cube_side parameter
2012 tb.tt = (TextureType)data.get_format()->get_nr_dimensions();
2013 if(cube_side > -1) {
2014 if(tb.tt == TT_2D)
2015 tb.tt = TT_CUBEMAP;
2016 } else if(num_array_layers != 0) {
2017 if(num_array_layers < 0) {
2018 // automatic inference of layers from texture dimensions
2019 unsigned n_dims = data.get_format()->get_nr_dimensions();
2020 if(n_dims == 2)
2021 tb.tt = TT_1D_ARRAY;
2022 if(n_dims == 3)
2023 tb.tt = TT_2D_ARRAY;
2024 } else {
2025 switch(tb.tt) {
2026 case TT_1D: tb.tt = TT_1D_ARRAY; break;
2027 case TT_2D: tb.tt = TT_2D_ARRAY; break;
2028 case TT_3D: tb.tt = TT_2D_ARRAY; break;
2029 default:
2030 break;
2031 }
2032 }
2033 }
2034 // create texture is not yet done
2035 GLuint tex_id;
2036 if (tb.is_created())
2037 tex_id = get_gl_id(tb.handle);
2038 else {
2039 tex_id = texture_generate(tb);
2040 if (tex_id == get_gl_id(0))
2041 return false;
2042 tb.handle = get_handle(tex_id);
2043 }
2044
2045 // bind texture
2046 GLuint tmp_id = texture_bind(tb.tt, tex_id);
2047
2048 // load data to texture
2049 tb.have_mipmaps = load_texture(data, gl_tex_format, level, cube_side, num_array_layers, palettes);
2050 bool result = !check_gl_error("gl_context::texture_create", &tb);
2051 // restore old texture
2052 texture_unbind(tb.tt, tmp_id);
2053 return result;
2054}
2055
2056bool gl_context::texture_create_from_buffer(
2057 texture_base& tb,
2059 int x, int y, int level) const
2060{
2061 GLuint gl_format = (const GLuint&) tb.internal_format;
2062
2063 tb.tt = (TextureType)df.get_nr_dimensions();
2064 if (tb.tt != TT_2D) {
2065 tb.last_error = "texture creation from buffer only possible for 2d textures";
2066 return false;
2067 }
2068 GLuint tex_id;
2069 if (tb.is_created())
2070 tex_id = get_gl_id(tb.handle);
2071 else {
2072 tex_id = texture_generate(tb);
2073 if (tex_id == get_gl_id(0))
2074 return false;
2075 tb.handle = get_handle(tex_id);
2076 }
2077 GLuint tmp_id = texture_bind(tb.tt, tex_id);
2078
2079 // check mipmap type
2080 bool gen_mipmap = level == -1;
2081 if (gen_mipmap)
2082 level = 0;
2083
2084 glCopyTexImage2D(GL_TEXTURE_2D, level, gl_format, x, y, GLsizei(df.get_width()), GLsizei(df.get_height()), 0);
2085 bool result = false;
2086 std::string error_string("gl_context::texture_create_from_buffer: ");
2087 switch (glGetError()) {
2088 case GL_NO_ERROR :
2089 result = true;
2090 break;
2091 case GL_INVALID_ENUM :
2092 error_string += "target was not an accepted value.";
2093 break;
2094 case GL_INVALID_VALUE :
2095 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"
2096 "or border was not zero or 1.\n"
2097 "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.";
2098 break;
2099 case GL_INVALID_OPERATION :
2100 error_string += "glCopyTexImage2D was called between a call to glBegin and the corresponding call to glEnd.";
2101 break;
2102 default:
2103 error_string += gl_error_to_string(glGetError());
2104 break;
2105 }
2106 texture_unbind(tb.tt, tmp_id);
2107 if (!result)
2109 else
2110 if (gen_mipmap)
2111 result = texture_generate_mipmaps(tb, tb.tt == TT_CUBEMAP ? 2 : (int)tb.tt);
2112
2113 return result;
2114}
2115
2116bool gl_context::texture_replace(
2117 texture_base& tb,
2118 int x, int y, int z,
2119 const cgv::data::const_data_view& data,
2120 int level, const std::vector<cgv::data::data_view>* palettes) const
2121{
2122 if (!tb.is_created()) {
2123 error("gl_context::texture_replace: attempt to replace in not created texture", &tb);
2124 return false;
2125 }
2126 // determine dimension from location arguments
2127 unsigned int dim = 1;
2128 if (y != -1) {
2129 ++dim;
2130 if (z != -1)
2131 ++dim;
2132 }
2133 // check consistency
2134 if (tb.tt == TT_CUBEMAP) {
2135 if (dim != 3) {
2136 error("gl_context::texture_replace: replace on cubemap without the side defined", &tb);
2137 return false;
2138 }
2139 if (z < 0 || z > 5) {
2140 error("gl_context::texture_replace: replace on cubemap with invalid side specification", &tb);
2141 return false;
2142 }
2143 }
2144 else {
2145 if (tb.tt != dim) {
2146 error("gl_context::texture_replace: replace on texture with invalid position specification", &tb);
2147 return false;
2148 }
2149 }
2150
2151 // bind texture
2152 GLuint tmp_id = texture_bind(tb.tt, get_gl_id(tb.handle));
2153 tb.have_mipmaps = replace_texture(data, level, x, y, z, palettes) || tb.have_mipmaps;
2154 bool result = !check_gl_error("gl_context::texture_replace", &tb);
2155 texture_unbind(tb.tt, tmp_id);
2156 return result;
2157}
2158
2159bool gl_context::texture_replace_from_buffer(
2160 texture_base& tb,
2161 int x, int y, int z,
2162 int x_buffer, int y_buffer,
2163 unsigned int width, unsigned int height,
2164 int level) const
2165{
2166 if (!tb.is_created()) {
2167 error("gl_context::texture_replace_from_buffer: attempt to replace in not created texture", &tb);
2168 return false;
2169 }
2170 // determine dimension from location arguments
2171 unsigned int dim = 2;
2172 if (z != -1)
2173 ++dim;
2174
2175 // consistency checks
2176 if (tb.tt == TT_CUBEMAP) {
2177 if (dim != 3) {
2178 error("gl_context::texture_replace_from_buffer: replace on cubemap without the side defined", &tb);
2179 return false;
2180 }
2181 if (z < 0 || z > 5) {
2182 error("gl_context::texture_replace_from_buffer: replace on cubemap without invalid side specification", &tb);
2183 return false;
2184 }
2185 }
2186 else {
2187 if (tb.tt != dim) {
2188 tb.last_error = "replace on texture with invalid position specification";
2189 return false;
2190 }
2191 }
2192 // check mipmap type
2193 bool gen_mipmap = level == -1;
2194 if (gen_mipmap)
2195 level = 0;
2196
2197 // bind texture
2198 GLuint tmp_id = texture_bind(tb.tt, get_gl_id(tb.handle));
2199 switch (tb.tt) {
2200 case TT_2D : glCopyTexSubImage2D(GL_TEXTURE_2D, level, x, y, x_buffer, y_buffer, width, height); break;
2201 case TT_3D : glCopyTexSubImage3D(GL_TEXTURE_3D, level, x, y, z, x_buffer, y_buffer, width, height); break;
2202 case TT_CUBEMAP : glCopyTexSubImage2D(get_gl_cube_map_target(z), level, x, y, x_buffer, y_buffer, width, height);
2203 default: break;
2204 }
2205 bool result = !check_gl_error("gl_context::texture_replace_from_buffer", &tb);
2206 texture_unbind(tb.tt, tmp_id);
2207
2208 if (result && gen_mipmap)
2209 result = texture_generate_mipmaps(tb, tb.tt == TT_CUBEMAP ? 2 : (int)tb.tt);
2210
2211 return result;
2212}
2213
2214bool gl_context::texture_copy_back(const texture_base& tb, int level, cgv::data::data_view& dv) const
2215{
2216 if(!tb.is_created()) {
2217 error("gl_context::texture_copy_back: attempt to copy from not created texture", &tb);
2218 return false;
2219 }
2220
2221 GLuint tmp_id = texture_bind(tb.tt, get_gl_id(tb.handle));
2222 read_texture(dv, level);
2223
2224 bool result = !check_gl_error("gl_context::texture_copy_back", &tb);
2225 texture_unbind(tb.tt, tmp_id);
2226
2227 return result;
2228}
2229
2230bool gl_context::texture_create_mipmaps(texture_base& tb, cgv::data::data_format& df) const
2231{
2232 GLuint gl_format = (const GLuint&)tb.internal_format;
2233
2234 // extract component type
2235 unsigned int transfer_format = map_to_gl(df.get_standard_component_format(), df.get_integer_interpretation());
2236
2237 if(transfer_format == -1) {
2238 error("could not determine transfer format", &tb);
2239 return false;
2240 }
2241
2242 // extract texture size and compute number of mip-levels
2243 uvec3 size(unsigned(df.get_width()), unsigned(df.get_height()), unsigned(df.get_depth()));
2244
2245 unsigned max_size = cgv::math::max_value(size);
2246 unsigned num_levels = 1 + static_cast<unsigned>(log2(static_cast<float>(max_size)));
2247
2248 // compute mip-level sizes
2249 std::vector<uvec3> level_sizes(num_levels);
2250 level_sizes[0] = size;
2251
2252 for(unsigned level = 1; level < num_levels; ++level) {
2253 uvec3 level_size = level_sizes[level - 1];
2254 level_size = level_size / 2u;
2255 level_size = cgv::math::max(level_size, uvec3(1u));
2256 level_sizes[level] = level_size;
2257 }
2258
2259 GLuint tmp_id = texture_bind(tb.tt, get_gl_id(tb.handle));
2260
2261 bool result = true;
2262
2263 switch(tb.tt) {
2264 case TT_1D:
2265 for(unsigned level = 1; level < num_levels; ++level) {
2266 uvec3 level_size = level_sizes[level];
2268 }
2269 break;
2270 case TT_1D_ARRAY:
2271 //glTexImage2D(GL_TEXTURE_1D_ARRAY, 0, gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2272 error("create mipmaps not implemented for 1D array textures", &tb);
2273 result = false;
2274 break;
2275 case TT_2D:
2276 for(unsigned level = 1; level < num_levels; ++level) {
2277 uvec3 level_size = level_sizes[level];
2279 }
2280 break;
2281 case TT_MULTISAMPLE_2D:
2282 //glTexImage2DMultisample(GL_TEXTURE_2D_MULTISAMPLE, tb.nr_multi_samples, gl_format, df.get_width(), df.get_height(), map_to_gl(tb.fixed_sample_locations));
2283 error("create mipmaps not implemented for 2D multisample textures", &tb);
2284 result = false;
2285 break;
2286 case TT_2D_ARRAY:
2287 //glTexImage3D(GL_TEXTURE_2D_ARRAY, 0, gl_format, df.get_width(), df.get_height(), df.get_depth(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2288 error("create mipmaps not implemented for 2D array textures", &tb);
2289 result = false;
2290 break;
2291 case TT_MULTISAMPLE_2D_ARRAY:
2292 //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));
2293 error("create mipmaps not implemented for 2D multisample array textures", &tb);
2294 result = false;
2295 break;
2296 case TT_3D:
2297 for(unsigned level = 1; level < num_levels; ++level) {
2298 uvec3 level_size = level_sizes[level];
2300 }
2301 break;
2302 case TT_CUBEMAP:
2303 /*glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X, 0,
2304 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2305 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_X, 0,
2306 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2307 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Y, 0,
2308 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2309 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, 0,
2310 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2311 glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_Z, 0,
2312 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2313 glTexImage2D(GL_TEXTURE_CUBE_MAP_NEGATIVE_Z, 0,
2314 gl_format, df.get_width(), df.get_height(), 0, transfer_format, GL_UNSIGNED_BYTE, 0);
2315 */
2316 error("create mipmaps not implemented for cubemap textures", &tb);
2317 result = false;
2318 default:
2319 break;
2320 }
2321
2322 if(check_gl_error("gl_context::texture_create_mipmaps", &tb))
2323 result = false;
2324
2325 if(result)
2326 tb.have_mipmaps = true;
2327
2328 texture_unbind(tb.tt, tmp_id);
2329 return result;
2330}
2331
2332bool gl_context::texture_generate_mipmaps(texture_base& tb, unsigned int dim) const
2333{
2334 GLuint tmp_id = texture_bind(tb.tt,get_gl_id(tb.handle));
2335
2336 bool is_array = tb.tt == TT_1D_ARRAY || tb.tt == TT_2D_ARRAY;
2337 bool is_cubemap = tb.tt == TT_CUBEMAP;
2338 std::string error_string;
2339 bool result = generate_mipmaps(dim, is_cubemap, is_array, &error_string);
2340 if (result)
2341 tb.have_mipmaps = true;
2342 else
2343 error(std::string("gl_context::texture_generate_mipmaps: ") + error_string);
2344
2345 texture_unbind(tb.tt, tmp_id);
2346 return result;
2347}
2348
2349bool gl_context::texture_destruct(texture_base& tb) const
2350{
2351 if (!tb.is_created()) {
2352 error("gl_context::texture_destruct: attempt to destruct not created texture", &tb);
2353 return false;
2354 }
2355 GLuint tex_id = get_gl_id(tb.handle);
2356 glDeleteTextures(1, &tex_id);
2357 bool result = !check_gl_error("gl_context::texture_destruct", &tb);
2358 tb.handle = 0;
2359 tb.ctx_ptr = 0;
2360 return result;
2361}
2362
2363bool gl_context::texture_set_state(const texture_base& tb) const
2364{
2365 if (tb.tt == TT_UNDEF) {
2366 error("gl_context::texture_set_state: attempt to set state on texture without type", &tb);
2367 return false;
2368 }
2369 GLuint tex_id = (GLuint&) tb.handle - 1;
2370 if (tex_id == -1) {
2371 error("gl_context::texture_set_state: attempt of setting texture state of not created texture", &tb);
2372 return false;
2373 }
2374 GLint tmp_id = texture_bind(tb.tt, tex_id);
2375
2376 if (tb.tt != TT_MULTISAMPLE_2D && tb.tt != TT_MULTISAMPLE_2D_ARRAY) {
2377 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_MIN_FILTER, map_to_gl(tb.min_filter));
2378 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_MAG_FILTER, map_to_gl(tb.mag_filter));
2379 if (tb.min_filter == TF_ANISOTROP)
2380 glTexParameterf(get_tex_dim(tb.tt), GL_TEXTURE_MAX_ANISOTROPY_EXT, tb.anisotropy);
2381 else
2382 glTexParameterf(get_tex_dim(tb.tt), GL_TEXTURE_MAX_ANISOTROPY_EXT, 1.0f);
2383 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_COMPARE_FUNC, map_to_gl(tb.compare_function));
2384 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_COMPARE_MODE, (tb.use_compare_function ? GL_COMPARE_REF_TO_TEXTURE : GL_NONE));
2385 if (!core_profile)
2386 glTexParameterf(get_tex_dim(tb.tt), GL_TEXTURE_PRIORITY, tb.priority);
2387 glTexParameterfv(get_tex_dim(tb.tt), GL_TEXTURE_BORDER_COLOR, tb.border_color);
2388 // if (tb.border_color[0] >= 0.0f)
2389 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_WRAP_S, map_to_gl(tb.wrap_s));
2390 if (tb.tt > TT_1D)
2391 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_WRAP_T, map_to_gl(tb.wrap_t));
2392 if (tb.tt == TT_3D)
2393 glTexParameteri(get_tex_dim(tb.tt), GL_TEXTURE_WRAP_R, map_to_gl(tb.wrap_r));
2394 }
2395
2396 bool result = !check_gl_error("gl_context::texture_set_state", &tb);
2397 texture_unbind(tb.tt, tmp_id);
2398 return result;
2399}
2400
2401bool gl_context::texture_enable(texture_base& tb, int tex_unit, unsigned int dim) const
2402{
2403 if (dim < 1 || dim > 3) {
2404 error("gl_context::texture_enable: invalid texture dimension", &tb);
2405 return false;
2406 }
2407 GLuint tex_id = (GLuint&) tb.handle - 1;
2408 if (tex_id == -1) {
2409 error("gl_context::texture_enable: texture not created", &tb);
2410 return false;
2411 }
2412 if (tex_unit >= 0) {
2413 if (!GLEW_VERSION_1_3) {
2414 error("gl_context::texture_enable: multi texturing not supported", &tb);
2415 return false;
2416 }
2417 glActiveTexture(GL_TEXTURE0+tex_unit);
2418 }
2419 GLint& old_binding = (GLint&) tb.user_data;
2420 glGetIntegerv(get_tex_bind(tb.tt), &old_binding);
2421 ++old_binding;
2422 glBindTexture(get_tex_dim(tb.tt), tex_id);
2423 // glEnable is not needed for texture arrays and will throw an invalid enum error
2424 //if(!(tb.tt == TT_1D_ARRAY || tb.tt == TT_2D_ARRAY))
2425 // glEnable(get_tex_dim(tb.tt));
2426 bool result = !check_gl_error("gl_context::texture_enable", &tb);
2427 if (tex_unit >= 0)
2429 return result;
2430}
2431
2432bool gl_context::texture_disable(
2433 texture_base& tb,
2434 int tex_unit, unsigned int dim) const
2435{
2436 if (dim < 1 || dim > 3) {
2437 error("gl_context::texture_disable: invalid texture dimension", &tb);
2438 return false;
2439 }
2440 if (tex_unit == -2) {
2441 error("gl_context::texture_disable: invalid texture unit", &tb);
2442 return false;
2443 }
2444 GLuint old_binding = (const GLuint&) tb.user_data;
2445 --old_binding;
2446 if (tex_unit >= 0)
2447 glActiveTexture(GL_TEXTURE0+tex_unit);
2448 // glDisable is not needed for texture arrays and will throw an invalid enum error
2449 //if(!(tb.tt == TT_1D_ARRAY || tb.tt == TT_2D_ARRAY))
2450 // glDisable(get_tex_dim(tb.tt));
2451 bool result = !check_gl_error("gl_context::texture_disable", &tb);
2452 glBindTexture(get_tex_dim(tb.tt), old_binding);
2453 if (tex_unit >= 0)
2455 return result;
2456}
2457
2458bool gl_context::texture_bind_as_image(texture_base& tb, int tex_unit, int level, bool bind_array, int layer, AccessType access) const
2459{
2460 GLuint tex_id = (GLuint&)tb.handle - 1;
2461 if(tex_id == -1) {
2462 error("gl_context::texture_enable: texture not created", &tb);
2463 return false;
2464 }
2465
2466 if(!GLEW_VERSION_4_2) {
2467 error("gl_context::texture_bind_as_image: image textures not supported", &tb);
2468 return false;
2469 }
2470
2471 GLuint gl_format = (const GLuint&)tb.internal_format;
2472 glBindImageTexture(tex_unit, tex_id, level, map_to_gl(bind_array), layer, map_to_gl(access), gl_format);
2473
2474 bool result = !check_gl_error("gl_context::texture_bind_as_image", &tb);
2475 return result;
2476}
2477
2478bool gl_context::render_buffer_create(render_buffer_base& rb, cgv::data::component_format& cf, int& _width, int& _height) const
2479{
2480 if (!GLEW_VERSION_3_0) {
2481 error("gl_context::render_buffer_create: frame buffer objects not supported", &rb);
2482 return false;
2483 }
2484 if (_width == -1)
2485 _width = get_width();
2486 if (_height == -1)
2487 _height = get_height();
2488
2489 GLuint rb_id;
2492
2493 GLuint& gl_format = (GLuint&)rb.internal_format;
2494 unsigned i = find_best_match(cf, color_buffer_formats);
2495 cgv::data::component_format best_cf(color_buffer_formats[i]);
2496 gl_format = gl_color_buffer_format_ids[i];
2497
2498 i = find_best_match(cf, depth_formats, &best_cf);
2499 if (i != -1) {
2500 best_cf = cgv::data::component_format(depth_formats[i]);
2501 gl_format = gl_depth_format_ids[i];
2502 }
2503
2504 cf = best_cf;
2505 if (rb.nr_multi_samples == 0)
2507 else
2509
2510 if (check_gl_error("gl_context::render_buffer_create", &rb))
2511 return false;
2512 rb.handle = get_handle(rb_id);
2513 return true;
2514}
2515
2516bool gl_context::render_buffer_destruct(render_buffer_base& rc) const
2517{
2518 if (!GLEW_VERSION_3_0) {
2519 error("gl_context::render_buffer_destruct: frame buffer objects not supported", &rc);
2520 return false;
2521 }
2522 GLuint rb_id = ((GLuint&) rc.handle)+1;
2524 if (check_gl_error("gl_context::render_buffer_destruct", &rc))
2525 return false;
2526 rc.handle = 0;
2527 rc.ctx_ptr = 0;
2528 return true;
2529}
2530
2531bool gl_context::frame_buffer_create(frame_buffer_base& fbb) const
2532{
2533 if (!check_fbo_support("gl_context::frame_buffer_create", &fbb))
2534 return false;
2535
2536 if (!context::frame_buffer_create(fbb))
2537 return false;
2538
2539 // allocate framebuffer object
2540 GLuint fbo_id = 0;
2541 glGenFramebuffers(1, &fbo_id);
2542 if (fbo_id == 0) {
2543 error("gl_context::frame_buffer_create: could not allocate framebuffer object", &fbb);
2544 return false;
2545 }
2546 fbb.handle = get_handle(fbo_id);
2547 return true;
2548}
2549
2550bool gl_context::frame_buffer_enable(frame_buffer_base& fbb)
2551{
2552 if (!context::frame_buffer_enable(fbb))
2553 return false;
2554 std::vector<int> buffers;
2555 bool depth_buffer = false;
2556 get_buffer_list(fbb, depth_buffer, buffers, GL_COLOR_ATTACHMENT0);
2557 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbb.handle));
2558
2559 if (buffers.size() == 1)
2560 glDrawBuffer(buffers[0]);
2561 else if (buffers.size() > 1) {
2562 glDrawBuffers(GLsizei(buffers.size()), reinterpret_cast<GLenum*>(&buffers[0]));
2563 }
2564 else if(depth_buffer) {
2566 //glReadBuffer(GL_NONE);
2567 } else {
2568 error("gl_context::frame_buffer_enable: no attached draw buffer selected!!", &fbb);
2569 return false;
2570 }
2571 return true;
2572}
2573
2576{
2577 if (!context::frame_buffer_disable(fbb))
2578 return false;
2579 if (frame_buffer_stack.empty()) {
2580 error("gl_context::frame_buffer_disable called with empty frame buffer stack!!", &fbb);
2581 return false;
2582 }
2583 else
2584 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(frame_buffer_stack.top()->handle));
2585 return true;
2586}
2587
2588bool gl_context::frame_buffer_destruct(frame_buffer_base& fbb) const
2589{
2590 if (!context::frame_buffer_destruct(fbb))
2591 return false;
2592 GLuint fbo_id = get_gl_id(fbb.handle);
2593 glDeleteFramebuffers(1, &fbo_id);
2594 fbb.handle = 0;
2595 fbb.ctx_ptr = 0;
2596 return true;
2597}
2598
2599void complete_rect_from_vp(ivec4& D, GLint vp[4])
2600{
2601 if (D(0) == -1)
2602 D(0) = vp[0];
2603 if (D(1) == -1)
2604 D(1) = vp[1];
2605 if (D(2) == -1)
2606 D(2) = vp[0] + vp[2];
2607 if (D(3) == -1)
2608 D(3) = vp[1] + vp[3];
2609}
2610
2611void gl_context::frame_buffer_blit(
2612 const frame_buffer_base* src_fbb_ptr, const ivec4& _S,
2613 frame_buffer_base* dst_fbb_ptr, const ivec4& _D, BufferTypeBits btbs, bool interpolate) const
2614{
2615 static const GLenum masks[8]{
2616 0,
2624 };
2625 ivec4 S = _S;
2626 ivec4 D = _D;
2627 if ((src_fbb_ptr == 0 && (S(0) == -1 || S(1) == -1 || S(2) == -1 || S(3) == -1)) ||
2628 (dst_fbb_ptr == 0 && (D(0) == -1 || D(1) == -1 || D(2) == -1 || D(3) == -1))) {
2629 GLint vp[4];
2631 if (src_fbb_ptr == 0)
2632 complete_rect_from_vp(S, vp);
2633 if (dst_fbb_ptr == 0)
2634 complete_rect_from_vp(D, vp);
2635 }
2637 if (src_fbb_ptr) {
2640 }
2641 if (dst_fbb_ptr) {
2644 }
2645 glBlitFramebuffer(S(0), S(1), S(2), S(3), D(0), D(1), D(2), D(3), masks[btbs], interpolate ? GL_LINEAR : GL_NEAREST);
2646 if (src_fbb_ptr)
2648 if (dst_fbb_ptr)
2650}
2651
2652bool gl_context::frame_buffer_attach(frame_buffer_base& fbb, const render_buffer_base& rb, bool is_depth, int i) const
2653{
2654 if (!context::frame_buffer_attach(fbb, rb, is_depth, i))
2655 return false;
2658 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbb.handle));
2662 get_gl_id(rb.handle));
2664 return true;
2665}
2666
2668bool gl_context::frame_buffer_attach(frame_buffer_base& fbb,
2669 const texture_base& t, bool is_depth,
2670 int level, int i, int z_or_cube_side) const
2671{
2672 if (!context::frame_buffer_attach(fbb, t, is_depth, level, i, z_or_cube_side))
2673 return false;
2676 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbb.handle));
2677
2678 if (z_or_cube_side == -1) {
2681 t.tt == TT_2D ? GL_TEXTURE_2D : GL_TEXTURE_2D_MULTISAMPLE, get_gl_id(t.handle), level);
2682 }
2683 else {
2684 if (t.tt == TT_CUBEMAP) {
2687 get_gl_cube_map_target(z_or_cube_side), get_gl_id(t.handle), level);
2688 }
2689 else {
2692 t.tt == TT_3D ? GL_TEXTURE_3D : GL_TEXTURE_2D_MULTISAMPLE_ARRAY , get_gl_id(t.handle), level, z_or_cube_side);
2693 }
2694 }
2695 bool result = !check_gl_error("gl_context::frame_buffer_attach", &fbb);
2697 return result;
2698}
2699
2702{
2703 if (fbb.handle == 0) {
2704 error("gl_context::frame_buffer_is_complete: attempt to check completeness on frame buffer that is not created", &fbb);
2705 return false;
2706 }
2709 glBindFramebuffer(GL_FRAMEBUFFER, get_gl_id(fbb.handle));
2712 switch (error) {
2714 return true;
2716 fbb.last_error = "undefined framebuffer";
2717 return false;
2719 fbb.last_error = "incomplete attachment";
2720 return false;
2722 fbb.last_error = "incomplete or missing attachment";
2723 return false;
2725 fbb.last_error = "incomplete multisample";
2726 return false;
2728 fbb.last_error = "incomplete layer targets";
2729 return false;
2731 fbb.last_error = "incomplete draw buffer";
2732 return false;
2734 fbb.last_error = "incomplete read buffer";
2735 return false;
2737 fbb.last_error = "framebuffer objects unsupported";
2738 return false;
2739 }
2740 fbb.last_error = "unknown error";
2741 return false;
2742}
2743
2744int gl_context::frame_buffer_get_max_nr_color_attachments() const
2745{
2746 if (!check_fbo_support("gl_context::frame_buffer_get_max_nr_color_attachments"))
2747 return 0;
2748
2749 GLint nr;
2751 return nr;
2752}
2753
2754int gl_context::frame_buffer_get_max_nr_draw_buffers() const
2755{
2756 if (!check_fbo_support("gl_context::frame_buffer_get_max_nr_draw_buffers"))
2757 return 0;
2758
2759 GLint nr;
2761 return nr;
2762}
2763
2764GLuint gl_shader_type[] =
2765{
2767};
2768
2769void gl_context::shader_code_destruct(render_component& sc) const
2770{
2771 if (sc.handle == 0) {
2772 error("gl_context::shader_code_destruct: shader not created", &sc);
2773 return;
2774 }
2775 glDeleteShader(get_gl_id(sc.handle));
2776 sc.handle = 0;
2777 sc.ctx_ptr = 0;
2778 check_gl_error("gl_context::shader_code_destruct", &sc);
2779}
2780
2781bool gl_context::shader_code_create(render_component& sc, ShaderType st, const std::string& source) const
2782{
2783 if (!check_shader_support(st, "gl_context::shader_code_create", &sc))
2784 return false;
2785
2786 while (glGetError()); // Clear error flags.
2787
2788 GLuint s_id = glCreateShader(gl_shader_type[st]);
2789 if (s_id == -1) {
2790 error(std::string("gl_context::shader_code_create: ")+gl_error(), &sc);
2791 return false;
2792 }
2793 sc.handle = get_handle(s_id);
2794
2795 const char* s = source.c_str();
2796 glShaderSource(s_id, 1, &s,NULL);
2797 if (check_gl_error("gl_context::shader_code_create", &sc))
2798 return false;
2799
2800 return true;
2801}
2802
2803bool gl_context::shader_code_compile(render_component& sc) const
2804{
2805 if (sc.handle == 0) {
2806 error("gl_context::shader_code_compile: shader not created", &sc);
2807 return false;
2808 }
2809 GLuint s_id = get_gl_id(sc.handle);
2811 GLint result = GL_FALSE;
2813 if (result == GL_TRUE)
2814 return true;
2815 sc.last_error = std::string();
2816 GLint infologLength = 0;
2818 if (infologLength > 0) {
2820 sc.last_error = std::string(infologLength, 0);
2821 glGetShaderInfoLog(s_id, infologLength, &charsWritten, &sc.last_error.front());
2822 sc.last_error.resize(static_cast<size_t>(charsWritten));
2823 }
2824 return false;
2825}
2826
2827bool gl_context::shader_program_create(shader_program_base& spb) const
2828{
2829 if (!check_shader_support(ST_VERTEX, "gl_context::shader_program_create", &spb))
2830 return false;
2831 spb.handle = get_handle(glCreateProgram());
2832 return true;
2833}
2834
2835void gl_context::shader_program_attach(shader_program_base& spb, const render_component& sc) const
2836{
2837 if (spb.handle == 0) {
2838 error("gl_context::shader_program_attach: shader program not created", &spb);
2839 return;
2840 }
2841 glAttachShader(get_gl_id(spb.handle), get_gl_id(sc.handle));
2842}
2843
2844void gl_context::shader_program_detach(shader_program_base& spb, const render_component& sc) const
2845{
2846 if (spb.handle == 0) {
2847 error("gl_context::shader_program_detach: shader program not created", &spb);
2848 return;
2849 }
2850 glDetachShader(get_gl_id(spb.handle), get_gl_id(sc.handle));
2851}
2852
2853bool gl_context::shader_program_link(shader_program_base& spb) const
2854{
2855 if (spb.handle == 0) {
2856 error("gl_context::shader_program_link: shader program not created", &spb);
2857 return false;
2858 }
2859 GLuint p_id = get_gl_id(spb.handle);
2861 GLint result = GL_FALSE;
2863 if (result == GL_TRUE)
2864 return context::shader_program_link(spb);
2865 GLint infologLength = 0;
2867 if (infologLength > 0) {
2869 spb.last_error = std::string(infologLength, 0);
2870 glGetProgramInfoLog(p_id, infologLength, &charsWritten, &spb.last_error.front());
2871 spb.last_error.resize(static_cast<size_t>(charsWritten));
2872 error("gl_context::shader_program_link\n" + spb.last_error, &spb);
2873 }
2874
2875 return false;
2876}
2877
2878bool gl_context::shader_program_set_state(shader_program_base& spb) const
2879{
2880 if (spb.handle == 0) {
2881 error("gl_context::shader_program_set_state: shader program not created", &spb);
2882 return false;
2883 }
2884 GLuint p_id = get_gl_id(spb.handle);
2885 glProgramParameteri(p_id, GL_GEOMETRY_VERTICES_OUT_ARB, spb.geometry_shader_output_count);
2886 glProgramParameteri(p_id, GL_GEOMETRY_INPUT_TYPE_ARB, map_to_gl(spb.geometry_shader_input_type));
2887 glProgramParameteri(p_id, GL_GEOMETRY_OUTPUT_TYPE_ARB, map_to_gl(spb.geometry_shader_output_type));
2888 return true;
2889}
2890
2891bool gl_context::shader_program_enable(shader_program_base& spb)
2892{
2893 if (!context::shader_program_enable(spb))
2894 return false;
2895 glUseProgram(get_gl_id(spb.handle));
2896 shader_program& prog = static_cast<shader_program&>(spb);
2897 if (auto_set_lights_in_current_shader_program && spb.does_use_lights())
2898 set_current_lights(prog);
2899 if (auto_set_material_in_current_shader_program && spb.does_use_material())
2901 if (auto_set_view_in_current_shader_program && spb.does_use_view())
2902 set_current_view(prog);
2903 if (auto_set_gamma_in_current_shader_program && spb.does_use_gamma())
2904 set_current_gamma(prog);
2905 if (prog.does_context_set_color() && prog.get_color_index() >= 0)
2906 prog.set_attribute(*this, prog.get_color_index(), current_color);
2907 return true;
2908}
2909
2910bool gl_context::shader_program_disable(shader_program_base& spb)
2911{
2912 if (!context::shader_program_disable(spb))
2913 return false;
2914 if (shader_program_stack.empty())
2915 glUseProgram(0);
2916 else
2917 glUseProgram(get_gl_id(shader_program_stack.top()->handle));
2918 return true;
2919}
2920
2921bool gl_context::shader_program_destruct(shader_program_base& spb) const
2922{
2923 if (!context::shader_program_destruct(spb))
2924 return false;
2925 glDeleteProgram(get_gl_id(spb.handle));
2926 spb.handle = 0;
2927 spb.ctx_ptr = 0;
2928 return true;
2929}
2930
2931bool gl_context::shader_program_get_active_uniforms(shader_program_base& spb, std::vector<std::string>& names) const
2932{
2933 if (spb.handle == 0)
2934 return false;
2935
2936 GLuint p_id = get_gl_id(spb.handle);
2937
2940
2941 names.reserve(num_active_uniforms);
2942
2943 std::vector<GLchar> buffer(256);
2944 for (int i = 0; i < num_active_uniforms; ++i) {
2945 GLint array_size = 0;
2946 GLenum type = 0;
2948
2949 glGetActiveUniform(p_id, i, GLsizei(buffer.size()), &actual_length, &array_size, &type, buffer.data());
2950 std::string name(static_cast<char*>(buffer.data()), actual_length);
2951
2952 // Uniforms for arrays of non-compound (non-struct) types are listed once with a "[0]" suffix and a given array size greater than 1.
2953 if(array_size > 1) {
2954 // Remove the brackets to get the base name of the uniform
2955 size_t bracket_pos = name.find('[');
2956 if(bracket_pos != std::string::npos)
2957 name.resize(bracket_pos);
2958
2959 if(!name.empty()) {
2960 // Store the name without the brackets to allow setting complete arrays using just the uniform name.
2961 names.push_back(name);
2962 // Additionally store an entry for every possible indexed name to allow setting elements individually.
2963 for(GLint i = 0; i < array_size; ++i)
2964 names.push_back(name + "[" + std::to_string(i) + "]");
2965 }
2966 } else {
2967 if(!name.empty())
2968 names.push_back(name);
2969 }
2970 }
2971 return true;
2972}
2973
2974program_variable_info construct_program_variable(const std::vector<GLchar>& buffer, GLsizei actual_length, GLint array_size, GLenum type)
2975{
2976 program_variable_info V;
2977 V.name = std::string(static_cast<const char*>(buffer.data()), actual_length);
2978 // Uniforms for arrays of non-compound (non-struct) types are listed once with a "[0]" suffix and a given array size greater than 1.
2979 if (array_size > 1) {
2980 // Remove the brackets to get the base name of the uniform
2981 size_t bracket_pos = V.name.find('[');
2982 if (bracket_pos != std::string::npos)
2983 V.name.resize(bracket_pos);
2984 if (!V.name.empty())
2985 V.array_size = array_size;
2986 }
2987 else {
2988 V.array_size = 1;
2989 }
2990 switch (type) {
2991 case GL_FLOAT: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, false); break;
2992 case GL_FLOAT_VEC2: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 2u); break;
2993 case GL_FLOAT_VEC3: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 3u); break;
2994 case GL_FLOAT_VEC4: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 4u); break;
2995 case GL_FLOAT_MAT2: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 2u, 2u, false); break;
2996 case GL_FLOAT_MAT3: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 3u, 3u, false); break;
2997 case GL_FLOAT_MAT4: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 4u, 4u, false); break;
2998 case GL_FLOAT_MAT2x3: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 2u, 3u, false); break;
2999 case GL_FLOAT_MAT2x4: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 2u, 4u, false); break;
3000 case GL_FLOAT_MAT3x2: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 3u, 2u, false); break;
3001 case GL_FLOAT_MAT3x4: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 3u, 4u, false); break;
3002 case GL_FLOAT_MAT4x2: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 4u, 2u, false); break;
3003 case GL_FLOAT_MAT4x3: V.type_descr = type_descriptor(cgv::type::info::TI_FLT32, 4u, 3u, false); break;
3004 case GL_BOOL: V.type_descr = type_descriptor(cgv::type::info::TI_BOOL, false); break;
3005 case GL_BOOL_VEC2:V.type_descr = type_descriptor(cgv::type::info::TI_BOOL, 2u); break;
3006 case GL_BOOL_VEC3:V.type_descr = type_descriptor(cgv::type::info::TI_BOOL, 3u); break;
3007 case GL_BOOL_VEC4:V.type_descr = type_descriptor(cgv::type::info::TI_BOOL, 4u); break;
3008 case GL_INT: V.type_descr = type_descriptor(cgv::type::info::TI_INT32, false); break;
3009 case GL_INT_VEC2:V.type_descr = type_descriptor(cgv::type::info::TI_INT32, 2u); break;
3010 case GL_INT_VEC3:V.type_descr = type_descriptor(cgv::type::info::TI_INT32, 3u); break;
3011 case GL_INT_VEC4:V.type_descr = type_descriptor(cgv::type::info::TI_INT32, 4u); break;
3012 case GL_UNSIGNED_INT:V.type_descr = type_descriptor(cgv::type::info::TI_UINT32, false); break;
3013 case GL_UNSIGNED_INT_VEC2:V.type_descr = type_descriptor(cgv::type::info::TI_UINT32, 2u); break;
3014 case GL_UNSIGNED_INT_VEC3:V.type_descr = type_descriptor(cgv::type::info::TI_UINT32, 3u); break;
3015 case GL_UNSIGNED_INT_VEC4:V.type_descr = type_descriptor(cgv::type::info::TI_UINT32, 4u); break;
3016 case GL_DOUBLE: V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, false); break;
3017 case GL_DOUBLE_VEC2:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 2u); break;
3018 case GL_DOUBLE_VEC3:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 3u); break;
3019 case GL_DOUBLE_VEC4:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 4u); break;
3020 case GL_DOUBLE_MAT2:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 2u, 2u, false); break;
3021 case GL_DOUBLE_MAT3:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 3u, 3u, false); break;
3022 case GL_DOUBLE_MAT4:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 4u, 4u, false); break;
3023 case GL_DOUBLE_MAT2x3:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 2u, 3u, false); break;
3024 case GL_DOUBLE_MAT2x4:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 2u, 4u, false); break;
3025 case GL_DOUBLE_MAT3x2:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 3u, 2u, false); break;
3026 case GL_DOUBLE_MAT3x4:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 3u, 4u, false); break;
3027 case GL_DOUBLE_MAT4x2:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 4u, 2u, false); break;
3028 case GL_DOUBLE_MAT4x3:V.type_descr = type_descriptor(cgv::type::info::TI_FLT64, 4u, 3u, false); break;
3029 }
3030 return V;
3031}
3032
3033void gl_context::shader_program_inspect_variables(shader_program_base& spb, ProgramVariableKind kind, std::vector<program_variable_info>& Vs, bool get_location, bool get_value) const
3034{
3035 GLint prog_id;
3036 spb.put_id(prog_id);
3037 // name buffer and sizeing informations
3038 std::vector<GLchar> buffer(256);
3039 GLint array_size = 0;
3040 GLenum type = 0;
3042 // query number of active program variables of \c kind
3045 // for inspecting attribute values, one has to use the to be inspected program. Remember previously used program
3047 if (kind == cgv::render::PVK_ATTRIBUTE) {
3049 if (old_prog_id != prog_id)
3051 }
3052 // iterate all variables of \c kind
3053 for (int i = 0; i < num_active_variables; ++i) {
3054 // extract type and array information of program variable
3055 (kind == cgv::render::PVK_UNIFORM ? glGetActiveUniform : glGetActiveAttrib)(prog_id, i,
3056 GLsizei(buffer.size()), &actual_length, &array_size, &type, buffer.data());
3057 cgv::render::program_variable_info V = construct_program_variable(buffer, actual_length, array_size, type);
3058 // compute component count \c cnt, array element size \c s and total size \c S for all values in array
3059 size_t cnt = 1, s, S;
3060 if (get_value) {
3061 V.compute_sizes(cnt, s, S);
3062 V.current_value.resize(S);
3063 }
3064 // in case of attributes provide a read buffer to read array elements which write
3065 // always 4 components even if only one component is declared
3066 std::vector<char> read_buffer;
3067 if (kind == cgv::render::PVK_ATTRIBUTE && cnt != 4)
3068 read_buffer.resize(4 * s / cnt);
3069 for (unsigned j = 0; j < V.array_size; ++j) {
3070 // in case of array variables use per array element postfix to query location
3071 std::string postfix;
3072 if (V.array_size > 1)
3073 postfix += "[" + cgv::utils::to_string(j) + "]";
3074 // query location of variable in
3075 if (get_location || get_value)
3076 V.program_location = (kind == cgv::render::PVK_UNIFORM ? glGetUniformLocation : glGetAttribLocation)(
3077 prog_id, (V.name + postfix).c_str());
3078 // one is not allowed to query program attribute at location 0
3079 if (kind == cgv::render::PVK_ATTRIBUTE && V.program_location == 0) {
3080 V.current_value.clear();
3081 break;
3082 }
3083 if (!get_value)
3084 break;
3085 // let read_ptr point where program variable values should be queried to
3086 void* value_ptr = V.current_value.data() + j * s;
3087 void* read_ptr = read_buffer.empty() ? value_ptr : read_buffer.data();
3088 // switch over coordinate type to vary gl getter function
3089 switch (V.type_descr.coordinate_type) {
3092 if (kind == cgv::render::PVK_UNIFORM)
3093 glGetUniformiv(prog_id, V.program_location, (GLint*)(read_ptr));
3094 else
3096 break;
3098 if (kind == cgv::render::PVK_UNIFORM)
3099 glGetUniformuiv(prog_id, V.program_location, (GLuint*)(read_ptr));
3100 else
3102 break;
3104 if (kind == cgv::render::PVK_UNIFORM)
3105 glGetUniformfv(prog_id, V.program_location, (GLfloat*)(read_ptr));
3106 else
3108 break;
3110 if (kind == cgv::render::PVK_UNIFORM)
3111 glGetUniformdv(prog_id, V.program_location, (GLdouble*)(read_ptr));
3112 else
3114 break;
3115 }
3116 // in case we copied to read buffer, copy used value[s] to value buffer
3117 if (read_ptr != value_ptr)
3118 std::copy((const char*)read_ptr, (const char*)read_ptr + s, (char*)value_ptr);
3119 }
3120 // add inspected variable to list
3121 Vs.push_back(V);
3122 }
3123 // recover previously used program
3124 if (old_prog_id != prog_id)
3126}
3127
3128
3129int gl_context::get_uniform_location(const shader_program_base& spb, const std::string& name) const
3130{
3131 return glGetUniformLocation(get_gl_id(spb.handle), name.c_str());
3132}
3133
3134std::string value_type_index_to_string(type_descriptor td)
3135{
3136 std::string res = cgv::type::info::get_type_name(td.coordinate_type);
3137 switch (td.element_type) {
3138 case ET_VECTOR:
3139 res = std::string("vector<") + res + "," + cgv::utils::to_string(td.nr_rows) + ">";
3140 break;
3141 case ET_MATRIX:
3142 res = std::string("matrix<") + res + "," + cgv::utils::to_string(td.nr_rows) + "," + cgv::utils::to_string(td.nr_columns) + ">";
3143 if (td.is_row_major)
3144 res += "^T";
3145 default:
3146 break;
3147 }
3148 if (td.is_array)
3149 res += "[]";
3150 return res;
3151}
3152
3153bool gl_context::set_uniform_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const
3154{
3155 if (value_type.is_array) {
3156 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);
3157 return false;
3158 }
3159 if (!spb.handle) {
3160 error("gl_context::set_uniform_void() called on not created program", &spb);
3161 return false;
3162 }
3163 bool not_current = shader_program_stack.empty() || shader_program_stack.top() != &spb;
3164 if (not_current)
3165 glUseProgram(get_gl_id(spb.handle));
3166 bool res = true;
3167 switch (value_type.element_type) {
3168 case ET_VALUE:
3169 switch (value_type.coordinate_type) {
3170 case TI_BOOL: glUniform1i(loc, *reinterpret_cast<const bool*>(value_ptr) ? 1 : 0); break;
3171 case TI_UINT8: glUniform1ui(loc, *reinterpret_cast<const uint8_type*>(value_ptr)); break;
3172 case TI_UINT16: glUniform1ui(loc, *reinterpret_cast<const uint16_type*>(value_ptr)); break;
3173 case TI_UINT32: glUniform1ui(loc, *reinterpret_cast<const uint32_type*>(value_ptr)); break;
3174 case TI_INT8: glUniform1i(loc, *reinterpret_cast<const int8_type*>(value_ptr)); break;
3175 case TI_INT16: glUniform1i(loc, *reinterpret_cast<const int16_type*>(value_ptr)); break;
3176 case TI_INT32: glUniform1i(loc, *reinterpret_cast<const int32_type*>(value_ptr)); break;
3177 case TI_FLT32: glUniform1f(loc, *reinterpret_cast<const flt32_type*>(value_ptr)); break;
3178 case TI_FLT64: glUniform1d(loc, *reinterpret_cast<const flt64_type*>(value_ptr)); break;
3179 default:
3180 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3181 res = false; break;
3182 }
3183 break;
3184 case ET_VECTOR:
3185 switch (value_type.nr_rows) {
3186 case 2:
3187 switch (value_type.coordinate_type) {
3188 case TI_BOOL: glUniform2i(loc, reinterpret_cast<const bool*>(value_ptr)[0] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[1] ? 1 : 0); break;
3189 case TI_UINT8: glUniform2ui(loc, reinterpret_cast<const uint8_type*> (value_ptr)[0], reinterpret_cast<const uint8_type*> (value_ptr)[1]); break;
3190 case TI_UINT16: glUniform2ui(loc, reinterpret_cast<const uint16_type*>(value_ptr)[0], reinterpret_cast<const uint16_type*>(value_ptr)[1]); break;
3191 case TI_UINT32: glUniform2ui(loc, reinterpret_cast<const uint32_type*>(value_ptr)[0], reinterpret_cast<const uint32_type*>(value_ptr)[1]); break;
3192 case TI_INT8: glUniform2i(loc, reinterpret_cast<const int8_type*> (value_ptr)[0], reinterpret_cast<const int8_type*> (value_ptr)[1]); break;
3193 case TI_INT16: glUniform2i(loc, reinterpret_cast<const int16_type*> (value_ptr)[0], reinterpret_cast<const int16_type*> (value_ptr)[1]); break;
3194 case TI_INT32: glUniform2i(loc, reinterpret_cast<const int32_type*> (value_ptr)[0], reinterpret_cast<const int32_type*> (value_ptr)[1]); break;
3195 case TI_FLT32: glUniform2f(loc, reinterpret_cast<const flt32_type*> (value_ptr)[0], reinterpret_cast<const flt32_type*> (value_ptr)[1]); break;
3196 case TI_FLT64: glUniform2d(loc, reinterpret_cast<const flt64_type*> (value_ptr)[0], reinterpret_cast<const flt64_type*> (value_ptr)[1]); break;
3197 default:
3198 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3199 res = false; break;
3200 }
3201 break;
3202 case 3:
3203 switch (value_type.coordinate_type) {
3204 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;
3205 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;
3206 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;
3207 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;
3208 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;
3209 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;
3210 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;
3211 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;
3212 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;
3213 default:
3214 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3215 res = false; break;
3216 }
3217 break;
3218 case 4:
3219 switch (value_type.coordinate_type) {
3220 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;
3221 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;
3222 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;
3223 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;
3224 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;
3225 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;
3226 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;
3227 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;
3228 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;
3229 default:
3230 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3231 res = false; break;
3232 }
3233 break;
3234 }
3235 break;
3236 case ET_MATRIX:
3237 switch (value_type.coordinate_type) {
3238 case TI_FLT32:
3239 switch (value_type.nr_rows) {
3240 case 2:
3241 switch (value_type.nr_columns) {
3242 case 2: glUniformMatrix2fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3243 case 3: glUniformMatrix2x3fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3244 case 4: glUniformMatrix2x4fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3245 default:
3246 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3247 res = false; break;
3248 }
3249 break;
3250 case 3:
3251 switch (value_type.nr_columns) {
3252 case 2: glUniformMatrix3x2fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3253 case 3: glUniformMatrix3fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3254 case 4: glUniformMatrix3x4fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3255 default:
3256 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3257 res = false; break;
3258 }
3259 break;
3260 case 4:
3261 switch (value_type.nr_columns) {
3262 case 2: glUniformMatrix4x2fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3263 case 3: glUniformMatrix4x3fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3264 case 4: glUniformMatrix4fv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt32_type*> (value_ptr)); break;
3265 default:
3266 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3267 res = false; break;
3268 }
3269 break;
3270 default:
3271 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of rows outside [2,..,4].", &spb);
3272 res = false; break;
3273 }
3274 break;
3275 case TI_FLT64:
3276 switch (value_type.nr_rows) {
3277 case 2:
3278 switch (value_type.nr_columns) {
3279 case 2: glUniformMatrix2dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3280 case 3: glUniformMatrix2x3dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3281 case 4: glUniformMatrix2x4dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3282 default:
3283 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3284 res = false; break;
3285 }
3286 break;
3287 case 3:
3288 switch (value_type.nr_columns) {
3289 case 2: glUniformMatrix3x2dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3290 case 3: glUniformMatrix3dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3291 case 4: glUniformMatrix3x4dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3292 default:
3293 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3294 res = false; break;
3295 }
3296 break;
3297 case 4:
3298 switch (value_type.nr_columns) {
3299 case 2: glUniformMatrix4x2dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3300 case 3: glUniformMatrix4x3dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3301 case 4: glUniformMatrix4dv(loc, 1, !value_type.is_row_major, reinterpret_cast<const flt64_type*> (value_ptr)); break;
3302 default:
3303 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..,4].", &spb);
3304 res = false; break;
3305 }
3306 break;
3307 default:
3308 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") matrix number of rows outside [2,..,4].", &spb);
3309 res = false; break;
3310 }
3311 break;
3312 default:
3313 error(std::string("gl_context::set_uniform_void(") + value_type_index_to_string(value_type) + ") non float coordinate type not supported.", &spb);
3314 res = false; break;
3315 }
3316 break;
3317 }
3318 if (not_current)
3319 glUseProgram(shader_program_stack.empty() ? 0 : get_gl_id(shader_program_stack.top()->handle));
3320
3321 if (check_gl_error("gl_context::set_uniform_void()", &spb))
3322 res = false;
3323 return res;
3324}
3325
3326bool 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
3327{
3328 if (!value_type.is_array) {
3329 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") non array type not allowed.", &spb);
3330 return false;
3331 }
3332 if (!spb.handle) {
3333 error("gl_context::set_uniform_array_void() called on not created program", &spb);
3334 return false;
3335 }
3336 bool not_current = shader_program_stack.empty() || shader_program_stack.top() != &spb;
3337 if (not_current)
3338 glUseProgram(get_gl_id(spb.handle));
3339 bool res = true;
3340 switch (value_type.coordinate_type) {
3341 case TI_INT32:
3342 switch (value_type.element_type) {
3343 case ET_VALUE:
3344 glUniform1iv(loc, GLsizei(nr_elements), reinterpret_cast<const int32_type*>(value_ptr));
3345 break;
3346 case ET_VECTOR:
3347 switch (value_type.nr_rows) {
3348 case 2: glUniform2iv(loc, GLsizei(nr_elements), reinterpret_cast<const int32_type*>(value_ptr)); break;
3349 case 3: glUniform3iv(loc, GLsizei(nr_elements), reinterpret_cast<const int32_type*>(value_ptr)); break;
3350 case 4: glUniform4iv(loc, GLsizei(nr_elements), reinterpret_cast<const int32_type*>(value_ptr)); break;
3351 default:
3352 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") vector dimension outside [2,..4].", &spb);
3353 res = false;
3354 break;
3355 }
3356 break;
3357 case ET_MATRIX:
3358 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") type not supported.", &spb);
3359 res = false;
3360 break;
3361 }
3362 break;
3363 case TI_UINT32:
3364 switch (value_type.element_type) {
3365 case ET_VALUE:
3366 glUniform1uiv(loc, GLsizei(nr_elements), reinterpret_cast<const uint32_type*>(value_ptr));
3367 break;
3368 case ET_VECTOR:
3369 switch (value_type.nr_rows) {
3370 case 2: glUniform2uiv(loc, GLsizei(nr_elements), reinterpret_cast<const uint32_type*>(value_ptr)); break;
3371 case 3: glUniform3uiv(loc, GLsizei(nr_elements), reinterpret_cast<const uint32_type*>(value_ptr)); break;
3372 case 4: glUniform4uiv(loc, GLsizei(nr_elements), reinterpret_cast<const uint32_type*>(value_ptr)); break;
3373 default:
3374 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") vector dimension outside [2,..4].", &spb);
3375 res = false;
3376 break;
3377 }
3378 break;
3379 case ET_MATRIX:
3380 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") type not supported.", &spb);
3381 res = false;
3382 break;
3383 }
3384 break;
3385 case TI_FLT32:
3386 switch (value_type.element_type) {
3387 case ET_VALUE:
3388 glUniform1fv(loc, GLsizei(nr_elements), reinterpret_cast<const flt32_type*>(value_ptr));
3389 break;
3390 case ET_VECTOR:
3391 switch (value_type.nr_rows) {
3392 case 2: glUniform2fv(loc, GLsizei(nr_elements), reinterpret_cast<const flt32_type*>(value_ptr)); break;
3393 case 3: glUniform3fv(loc, GLsizei(nr_elements), reinterpret_cast<const flt32_type*>(value_ptr)); break;
3394 case 4: glUniform4fv(loc, GLsizei(nr_elements), reinterpret_cast<const flt32_type*>(value_ptr)); break;
3395 default:
3396 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") vector dimension outside [2,..4].", &spb);
3397 res = false;
3398 break;
3399 }
3400 break;
3401 case ET_MATRIX:
3402 switch (value_type.nr_rows) {
3403 case 2:
3404 switch (value_type.nr_columns) {
3405 case 2: glUniformMatrix2fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3406 case 3: glUniformMatrix2x3fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3407 case 4: glUniformMatrix2x4fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3408 default:
3409 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..4].", &spb);
3410 res = false;
3411 break;
3412 }
3413 break;
3414 case 3:
3415 switch (value_type.nr_columns) {
3416 case 2: glUniformMatrix3x2fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3417 case 3: glUniformMatrix3fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3418 case 4: glUniformMatrix3x4fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3419 default:
3420 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..4].", &spb);
3421 res = false;
3422 break;
3423 }
3424 break;
3425 case 4:
3426 switch (value_type.nr_columns) {
3427 case 2: glUniformMatrix4x2fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3428 case 3: glUniformMatrix4x3fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3429 case 4: glUniformMatrix4fv(loc, GLsizei(nr_elements), value_type.is_row_major, reinterpret_cast<const flt32_type*>(value_ptr)); break;
3430 default:
3431 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") matrix number of columns outside [2,..4].", &spb);
3432 res = false;
3433 break;
3434 }
3435 break;
3436 default:
3437 error(std::string("gl_context::set_uniform_array_void(") + value_type_index_to_string(value_type) + ") matrix number of rows outside [2,..4].", &spb);
3438 res = false;
3439 break;
3440 }
3441 break;
3442 }
3443 break;
3444 default:
3445 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);
3446 res = false;
3447 break;
3448 }
3449 if (check_gl_error("gl_context::set_uniform_array_void()", &spb))
3450 res = false;
3451
3452 if (not_current)
3453 glUseProgram(shader_program_stack.empty() ? 0 : get_gl_id(shader_program_stack.top()->handle));
3454
3455 return res;
3456}
3457
3458int gl_context::get_uniform_block_index(const shader_program_base& spb, const std::string& name) const
3459{
3460 return glGetUniformBlockIndex(get_gl_id(spb.handle), name.c_str());
3461}
3462
3463bool gl_context::set_uniform_block_binding(shader_program_base& spb, int index, int binding) const
3464{
3465 if(!spb.handle) {
3466 error("gl_context::set_uniform_block_binding() called on not created program", &spb);
3467 return false;
3468 }
3469 glUniformBlockBinding(get_gl_id(spb.handle), index, binding);
3470 return check_gl_error("gl_context::set_uniform_block_binding()", &spb);
3471}
3472
3473int gl_context::get_attribute_location(const shader_program_base& spb, const std::string& name) const
3474{
3475 return glGetAttribLocation(get_gl_id(spb.handle), name.c_str());
3476}
3477
3478bool gl_context::set_attribute_void(shader_program_base& spb, int loc, type_descriptor value_type, const void* value_ptr) const
3479{
3480 if (!spb.handle) {
3481 error("gl_context::set_attribute_void() called on not created program", &spb);
3482 return false;
3483 }
3484 bool not_current = shader_program_stack.empty() || shader_program_stack.top() != &spb;
3485 if (not_current)
3486 glUseProgram(get_gl_id(spb.handle));
3487 bool res = true;
3488 switch (value_type.element_type) {
3489 case ET_VALUE:
3490 switch (value_type.coordinate_type) {
3491 case TI_BOOL: glVertexAttrib1s(loc, *reinterpret_cast<const bool*>(value_ptr) ? 1 : 0); break;
3492 case TI_INT8: glVertexAttrib1s(loc, *reinterpret_cast<const int8_type*>(value_ptr)); break;
3493 case TI_INT16: glVertexAttrib1s(loc, *reinterpret_cast<const int16_type*>(value_ptr)); break;
3494 case TI_INT32: glVertexAttribI1i(loc, *reinterpret_cast<const int32_type*>(value_ptr)); break;
3495 case TI_UINT8: glVertexAttrib1s(loc, *reinterpret_cast<const uint8_type*>(value_ptr)); break;
3496 case TI_UINT16: glVertexAttribI1ui(loc, *reinterpret_cast<const uint16_type*>(value_ptr)); break;
3497 case TI_UINT32: glVertexAttribI1ui(loc, *reinterpret_cast<const uint32_type*>(value_ptr)); break;
3498 case TI_FLT32: glVertexAttrib1f(loc, *reinterpret_cast<const flt32_type*>(value_ptr)); break;
3499 case TI_FLT64: glVertexAttrib1d(loc, *reinterpret_cast<const flt64_type*>(value_ptr)); break;
3500 default:
3501 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") type not supported!", &spb);
3502 res = false;
3503 break;
3504 }
3505 break;
3506 case ET_VECTOR:
3507 switch (value_type.nr_rows) {
3508 case 2:
3509 switch (value_type.coordinate_type) {
3510 case TI_BOOL: glVertexAttrib2s(loc, reinterpret_cast<const bool*>(value_ptr)[0] ? 1 : 0, reinterpret_cast<const bool*>(value_ptr)[1] ? 1 : 0); break;
3511 case TI_UINT8: glVertexAttrib2s(loc, reinterpret_cast<const uint8_type*> (value_ptr)[0], reinterpret_cast<const uint8_type*> (value_ptr)[1]); break;
3512 case TI_UINT16: glVertexAttribI2ui(loc, reinterpret_cast<const uint16_type*>(value_ptr)[0], reinterpret_cast<const uint16_type*>(value_ptr)[1]); break;
3513 case TI_UINT32: glVertexAttribI2ui(loc, reinterpret_cast<const uint32_type*>(value_ptr)[0], reinterpret_cast<const uint32_type*>(value_ptr)[1]); break;
3514 case TI_INT8: glVertexAttrib2s(loc, reinterpret_cast<const int8_type*> (value_ptr)[0], reinterpret_cast<const int8_type*> (value_ptr)[1]); break;
3515 case TI_INT16: glVertexAttrib2s(loc, reinterpret_cast<const int16_type*> (value_ptr)[0], reinterpret_cast<const int16_type*> (value_ptr)[1]); break;
3516 case TI_INT32: glVertexAttribI2i(loc, reinterpret_cast<const int32_type*> (value_ptr)[0], reinterpret_cast<const int32_type*> (value_ptr)[1]); break;
3517 case TI_FLT32: glVertexAttrib2f(loc, reinterpret_cast<const flt32_type*> (value_ptr)[0], reinterpret_cast<const flt32_type*> (value_ptr)[1]); break;
3518 case TI_FLT64: glVertexAttrib2d(loc, reinterpret_cast<const flt64_type*> (value_ptr)[0], reinterpret_cast<const flt64_type*> (value_ptr)[1]); break;
3519 default:
3520 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3521 res = false;
3522 break;
3523 }
3524 break;
3525 case 3:
3526 switch (value_type.coordinate_type) {
3527 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;
3528 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;
3529 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;
3530 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;
3531 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;
3532 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;
3533 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;
3534 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;
3535 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;
3536 default:
3537 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3538 res = false; break;
3539 }
3540 break;
3541 case 4:
3542 switch (value_type.coordinate_type) {
3543 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;
3544 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;
3545 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;
3546 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;
3547 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;
3548 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;
3549 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;
3550 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;
3551 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;
3552 default:
3553 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") unsupported coordinate type.", &spb);
3554 res = false;
3555 break;
3556 }
3557 break;
3558 default:
3559 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") vector dimension outside [2..4]", &spb);
3560 res = false;
3561 break;
3562 }
3563 break;
3564 case ET_MATRIX:
3565 error(std::string("gl_context::set_attribute_void(") + value_type_index_to_string(value_type) + ") matrix type not supported!", &spb);
3566 res = false;
3567 break;
3568 }
3569 if (not_current)
3570 glUseProgram(shader_program_stack.empty() ? 0 : get_gl_id(shader_program_stack.top()->handle));
3571
3572 if (check_gl_error("gl_context::set_uniform_array_void()", &spb))
3573 res = false;
3574 return res;
3575}
3576
3577bool gl_context::attribute_array_binding_create(attribute_array_binding_base& aab) const
3578{
3579 if (!GLEW_VERSION_3_0) {
3580 error("gl_context::attribute_array_binding_create() array attribute bindings not supported", &aab);
3581 return false;
3582 }
3583 GLuint a_id;
3585 if (a_id == -1) {
3586 error(std::string("gl_context::attribute_array_binding_create(): ") + gl_error(), &aab);
3587 return false;
3588 }
3589 aab.ctx_ptr = this;
3590 aab.handle = get_handle(a_id);
3591 return true;
3592}
3593
3594bool gl_context::attribute_array_binding_destruct(attribute_array_binding_base& aab) const
3595{
3598 if (!context::attribute_array_binding_destruct(aab))
3599 return false;
3600 if (!aab.handle) {
3601 error("gl_context::attribute_array_binding_destruct(): called on not created attribute array binding", &aab);
3602 return false;
3603 }
3604 GLuint a_id = get_gl_id(aab.handle);
3606 aab.handle = 0;
3607 aab.ctx_ptr = 0;
3608 return !check_gl_error("gl_context::attribute_array_binding_destruct");
3609}
3610
3611bool gl_context::attribute_array_binding_enable(attribute_array_binding_base& aab)
3612{
3613 if (!context::attribute_array_binding_enable(aab))
3614 return false;
3615 glBindVertexArray(get_gl_id(aab.handle));
3616 return !check_gl_error("gl_context::attribute_array_binding_enable");
3617}
3618
3619bool gl_context::attribute_array_binding_disable(attribute_array_binding_base& aab)
3620{
3621 if (!context::attribute_array_binding_disable(aab))
3622 return false;
3625 else
3626 glBindVertexArray(get_gl_id(attribute_array_binding_stack.top()->handle));
3627 return true;
3628}
3629
3630bool gl_context::set_element_array(attribute_array_binding_base* aab, const vertex_buffer_base* vbb) const
3631{
3632 if (!vbb) {
3633 error("gl_context::set_element_array(): called without a vertex buffer object", aab);
3634 return false;
3635 }
3636 if (!vbb->handle) {
3637 error("gl_context::set_element_array(): called with not created vertex buffer object", vbb);
3638 return false;
3639 }
3640 if (vbb->type != VBT_INDICES) {
3641 std::cout << "gl_context::set_element_array() : called on vertex buffer object that is not of type VBT_INDICES" << std::endl;
3642// error("gl_context::set_element_array(): called on vertex buffer object that is not of type VBT_INDICES", vbb);
3643// return false;
3644 }
3645 if (aab) {
3646 if (!aab->handle) {
3647 error("gl_context::set_element_array(): called on not created attribute array binding", aab);
3648 return false;
3649 }
3650 }
3651 // enable vertex array
3653 if (aab && not_current)
3654 glBindVertexArray(get_gl_id(aab->handle));
3655
3656 // bind buffer to element array
3657 glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, get_gl_id(vbb->handle));
3658
3659 if (aab && not_current)
3661
3662 return !check_gl_error("gl_context::set_element_array_void()", aab);
3663}
3664
3665
3666bool 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
3667{
3668 if (value_type == ET_MATRIX) {
3669 error("gl_context::set_attribute_array_void(): called with matrix elements not supported", aab);
3670 return false;
3671 }
3672 if (vbb) {
3673 if (!vbb->handle) {
3674 error("gl_context::set_attribute_array_void(): called with not created vertex buffer object", vbb);
3675 return false;
3676 }
3677 }
3678 if (aab) {
3679 if (!aab->handle) {
3680 error("gl_context::set_attribute_array_void(): called on not created attribute array binding", aab);
3681 return false;
3682 }
3683 }
3684
3686 if (aab && not_current)
3687 glBindVertexArray(get_gl_id(aab->handle));
3688
3689 if (vbb)
3690 glBindBuffer(GL_ARRAY_BUFFER, get_gl_id(vbb->handle));
3691
3692 bool res = true;
3693 unsigned n = value_type.element_type == ET_VALUE ? 1 : value_type.nr_rows;
3694 switch (value_type.coordinate_type) {
3695 case TI_INT8: value_type.normalize ? glVertexAttribPointer(loc, n, GL_BYTE, value_type.normalize, stride, ptr) : glVertexAttribIPointer(loc, n, GL_BYTE, stride, ptr); break;
3696 case TI_INT16: value_type.normalize ? glVertexAttribPointer(loc, n, GL_SHORT, value_type.normalize, stride, ptr) : glVertexAttribIPointer(loc, n, GL_SHORT, stride, ptr); break;
3697 case TI_INT32: value_type.normalize ? glVertexAttribPointer(loc, n, GL_INT, value_type.normalize, stride, ptr) : glVertexAttribIPointer(loc, n, GL_INT, stride, ptr); break;
3698 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;
3699 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;
3700 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;
3701 case TI_FLT32: glVertexAttribPointer(loc, n, GL_FLOAT, value_type.normalize, stride, ptr); break;
3702 case TI_FLT64:
3703 if (GLEW_VERSION_4_1)
3704 glVertexAttribLPointer(loc, n, GL_DOUBLE, stride, ptr);
3705 else {
3706 error("gl_context::set_attribute_array_void(): called with coordinates of type double only supported starting with OpenGL 4.1", aab);
3707 res = false;
3708 }
3709 break;
3710 default:
3711 error("gl_context::set_attribute_array_void(): called with unsupported coordinate type", aab);
3712 res = false;
3713 }
3714
3715 if (res)
3717
3718 if (vbb)
3720
3721 if (aab && not_current)
3723
3724
3725 return res && !check_gl_error("gl_context::set_attribute_array_void()", aab);
3726}
3727
3728bool gl_context::enable_attribute_array(attribute_array_binding_base* aab, int loc, bool do_enable) const
3729{
3731 if (aab) {
3732 if (!aab->handle) {
3733 error("gl_context::enable_attribute_array(): called on not created attribute array binding", aab);
3734 return false;
3735 }
3736 if (not_current)
3737 glBindVertexArray(get_gl_id(aab->handle));
3738 }
3739
3740 if (do_enable)
3742 else
3744
3745 if (aab && not_current)
3747
3748 return !check_gl_error("gl_context::enable_attribute_array()");
3749}
3750
3751bool gl_context::is_attribute_array_enabled(const attribute_array_binding_base* aab, int loc) const
3752{
3754 if (aab) {
3755 if (!aab->handle) {
3756 error("gl_context::is_attribute_array_enabled(): called on not created attribute array binding", aab);
3757 return false;
3758 }
3759 if (not_current)
3760 glBindVertexArray(get_gl_id(aab->handle));
3761 }
3762
3763 GLint res;
3765
3766 if (aab && not_current)
3768
3769 return res == GL_TRUE;
3770}
3771
3772GLenum buffer_target(VertexBufferType vbt)
3773{
3774 static GLenum buffer_targets[] = {
3783 };
3784 return buffer_targets[vbt];
3785}
3786
3787GLenum buffer_usage(VertexBufferUsage vbu)
3788{
3789 static GLenum buffer_usages[] = {
3799 };
3800 return buffer_usages[vbu];
3801}
3802
3803bool gl_context::vertex_buffer_bind(const vertex_buffer_base& vbb, VertexBufferType _type, unsigned _idx) const
3804{
3805 if (_idx == unsigned(-1))
3806 glBindBuffer(buffer_target(_type), get_gl_id(vbb.handle));
3807 else
3808 glBindBufferBase(buffer_target(_type), _idx, get_gl_id(vbb.handle));
3809 return !check_gl_error("gl_context::vertex_buffer_bind", &vbb);
3810}
3811
3812bool gl_context::vertex_buffer_unbind(const vertex_buffer_base& vbb, VertexBufferType _type, unsigned _idx) const {
3813 if(_idx == unsigned(-1))
3814 glBindBuffer(buffer_target(_type), 0);
3815 else
3816 glBindBufferBase(buffer_target(_type), _idx, 0);
3817 return !check_gl_error("gl_context::vertex_buffer_unbind", &vbb);
3818}
3819
3820bool gl_context::vertex_buffer_create(vertex_buffer_base& vbb, const void* array_ptr, size_t size_in_bytes) const
3821{
3822 if (!GLEW_VERSION_2_0) {
3823 error("gl_context::vertex_buffer_create() vertex buffer objects not supported", &vbb);
3824 return false;
3825 }
3826 GLuint b_id;
3827 glGenBuffers(1, &b_id);
3828 if (b_id == -1) {
3829 error(std::string("gl_context::vertex_buffer_create(): ") + gl_error(), &vbb);
3830 return false;
3831 }
3832 vbb.handle = get_handle(b_id);
3833 glBindBuffer(buffer_target(vbb.type), b_id);
3834 glBufferData(buffer_target(vbb.type), size_in_bytes, array_ptr, buffer_usage(vbb.usage));
3835 glBindBuffer(buffer_target(vbb.type), 0);
3836 return !check_gl_error("gl_context::vertex_buffer_create", &vbb);
3837}
3838
3839bool gl_context::vertex_buffer_resize(vertex_buffer_base& vbb, const void* array_ptr, size_t size_in_bytes) const {
3840 if(!vbb.handle) {
3841 error("gl_context::vertex_buffer_resize() vertex buffer object must be created before", &vbb);
3842 return false;
3843 }
3844 GLuint b_id = get_gl_id(vbb.handle);
3845 glBindBuffer(buffer_target(vbb.type), b_id);
3846 glBufferData(buffer_target(vbb.type), size_in_bytes, array_ptr, buffer_usage(vbb.usage));
3847 glBindBuffer(buffer_target(vbb.type), 0);
3848 return !check_gl_error("gl_context::vertex_buffer_resize", &vbb);
3849}
3850
3851bool gl_context::vertex_buffer_clear(vertex_buffer_base& vbb, size_t offset, size_t size_in_bytes) const
3852{
3853 if (!vbb.handle) {
3854 error("gl_context::vertex_buffer_clear() vertex buffer object must be created before", &vbb);
3855 return false;
3856 }
3857 GLuint b_id = get_gl_id(vbb.handle);
3858 glBindBuffer(buffer_target(vbb.type), b_id);
3859 glClearBufferSubData(buffer_target(vbb.type), GL_R8, offset, size_in_bytes, GL_RED, GL_UNSIGNED_BYTE, NULL);
3860 glBindBuffer(buffer_target(vbb.type), 0);
3861 return !check_gl_error("gl_context::vertex_buffer_clear", &vbb);
3862}
3863
3864bool gl_context::vertex_buffer_replace(vertex_buffer_base& vbb, size_t offset, size_t size_in_bytes, const void* array_ptr) const
3865{
3866 if (!vbb.handle) {
3867 error("gl_context::vertex_buffer_replace() vertex buffer object must be created before", &vbb);
3868 return false;
3869 }
3870 GLuint b_id = get_gl_id(vbb.handle);
3871 glBindBuffer(buffer_target(vbb.type), b_id);
3872 glBufferSubData(buffer_target(vbb.type), offset, size_in_bytes, array_ptr);
3873 glBindBuffer(buffer_target(vbb.type), 0);
3874 return !check_gl_error("gl_context::vertex_buffer_replace", &vbb);
3875}
3876
3877bool 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
3878{
3879 if (!src.handle || !target.handle) {
3880 error("gl_context::vertex_buffer_copy() source and destination vertex buffer objects must have been created before", &src);
3881 return false;
3882 }
3883 GLuint b_id = get_gl_id(src.handle);
3884 glBindBuffer(GL_COPY_READ_BUFFER, get_gl_id(src.handle));
3885 glBindBuffer(GL_COPY_WRITE_BUFFER, get_gl_id(target.handle));
3889 return !check_gl_error("gl_context::vertex_buffer_copy", &src);
3890
3891}
3892
3893bool gl_context::vertex_buffer_copy_back(const vertex_buffer_base& vbb, size_t offset, size_t size_in_bytes, void* array_ptr) const
3894{
3895 if (!vbb.handle) {
3896 error("gl_context::vertex_buffer_copy_back() vertex buffer object must be created", &vbb);
3897 return false;
3898 }
3899 GLuint b_id = get_gl_id(vbb.handle);
3901 switch (vbb.type) {
3910 }
3912 glGetBufferSubData(bind_point, offset, size_in_bytes, array_ptr);
3914 return !check_gl_error("gl_context::vertex_buffer_copy_back", &vbb);
3915}
3916
3917bool gl_context::vertex_buffer_destruct(vertex_buffer_base& vbb) const
3918{
3919 if (vbb.handle) {
3920 GLuint b_id = get_gl_id(vbb.handle);
3921 glDeleteBuffers(1, &b_id);
3922 vbb.handle = 0;
3923 vbb.ctx_ptr = 0;
3924 return !check_gl_error("gl_context::vertex_buffer_destruct");
3925 }
3926 else {
3927 error("gl_context::vertex_buffer_destruct(): called on not created vertex buffer", &vbb);
3928 return false;
3929 }
3930}
3931
3932
3933 }
3934 }
3935}
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
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:1877
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:1032
virtual void mul_modelview_matrix(const dmat4 &MV)
multiply given matrix from right to current modelview matrix
Definition context.cxx:1952
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:2052
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:537
virtual void error(const std::string &message, const render_component *rc=0) const
error handling
Definition context.cxx:307
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:682
float current_font_size
store current font size
Definition context.h:841
bool enable_vsync
whether vsync should be enabled
Definition context.h:744
virtual void set_buffer_mask(BufferMask mask)
set the buffer mask for depth and color buffers
Definition context.cxx:1921
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:732
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:1885
virtual void on_lights_changed()
helper function to send light update events
Definition context.cxx:738
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:1813
std::stack< shader_program_base * > shader_program_stack
stack of currently enabled shader programs
Definition context.h:777
bool is_light_source_enabled(void *handle)
check whether light source is enabled
Definition context.cxx:782
virtual void disable_depth_test()
disable the depth test
Definition context.cxx:1839
int get_bg_stencil() const
return the current stencil value for clearing the background
Definition context.cxx:463
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:806
void pop_depth_test_state()
pop the top of the current depth test state from the stack
Definition context.cxx:1817
void tesselate_unit_sphere(int resolution=25, bool flip_normals=false, bool edges=false)
tesselate a sphere of radius 1
Definition context.cxx:1463
virtual RenderPassFlags get_render_pass_flags() const
return the current render pass flags
Definition context.cxx:864
void tesselate_unit_cylinder(int resolution=25, bool flip_normals=false, bool edges=false)
tesselate a cylinder of radius 1
Definition context.cxx:1371
virtual void set_bg_color(vec4 rgba)
set a user defined background color
Definition context.cxx:390
virtual void set_depth_test_state(DepthTestState state)
set the depth test state
Definition context.cxx:1826
size_t get_nr_enabled_light_sources() const
return the number of light sources
Definition context.cxx:771
virtual unsigned int get_height() const =0
return the height of the window
virtual void disable_blending()
disable blending
Definition context.cxx:1904
BufferMask get_buffer_mask() const
return the current buffer mask
Definition context.cxx:1917
void push_cull_state()
push a copy of the current culling state onto the stack saved attributes: cull face enablement,...
Definition context.cxx:1843
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:1037
std::stack< dmat4 > modelview_matrix_stack
keep two matrix stacks for model view and projection matrices
Definition context.h:771
virtual void pop_window_transformation_array()
restore previous viewport and depth range arrays defining the window transformations
Definition context.cxx:2019
virtual void set_color_mask(bvec4 flags)
set the color buffer mask
Definition context.cxx:1938
virtual void set_material(const cgv::media::illum::surface_material &mat)
set the current material
Definition context.cxx:1776
DepthTestState get_depth_test_state() const
return the current depth test state
Definition context.cxx:1822
bool auto_set_material_in_current_shader_program
whether to automatically set material in current shader program, defaults to true
Definition context.h:734
void set_current_lights(shader_program &prog) const
set the shader program lights to the currently enabled lights
Definition context.cxx:723
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:791
void tesselate_unit_disk(int resolution=25, bool flip_normals=false, bool edges=false)
tesselate a circular disk of radius 1
Definition context.cxx:1295
virtual void enable_depth_test()
enable the depth test
Definition context.cxx:1835
bool support_compatibility_mode
whether to support view and lighting management of compatibility mode, defaults to true
Definition context.h:738
void set_current_gamma(shader_program &prog) const
set the shader program gamma values
Definition context.cxx:1699
vec4 get_bg_color() const
return the current color value for clearing the background
Definition context.cxx:399
void pop_projection_matrix()
see push_P for an explanation
Definition context.cxx:1969
std::stack< attribute_array_binding_base * > attribute_array_binding_stack
stack of currently enabled attribute array binding
Definition context.h:791
BlendState get_blend_state() const
return the current blend state
Definition context.cxx:1869
virtual void set_cull_state(CullingMode culling_mode)
set the culling state
Definition context.cxx:1856
virtual void set_depth_func(CompareFunction func)
set the depth test function
Definition context.cxx:1830
void set_light_source(void *handle, const cgv::media::illum::light_source &light, bool place_now=true)
set light source newly
Definition context.cxx:669
void push_projection_matrix()
same as push_V but for the projection matrix - a different matrix stack is used.
Definition context.cxx:1964
bool current_material_is_textured
store flag to tell whether current material is textured
Definition context.h:819
virtual void set_projection_matrix(const dmat4 &P)
set the current projection matrix, which transforms from eye to clip space
Definition context.cxx:1997
rgba current_color
current color value
Definition context.h:746
static const unsigned nr_default_light_sources
number of default light sources
Definition context.h:809
cgv::media::illum::light_source default_light_source[nr_default_light_sources]
default light sources
Definition context.h:811
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:2102
virtual void set_bg_stencil(int s)
set a user defined background stencil value
Definition context.cxx:459
virtual void set_bg_accum_color(vec4 rgba)
set a user defined background color for the accumulation buffer
Definition context.cxx:476
cgv::media::illum::surface_material default_material
store a default material
Definition context.h:815
float get_bg_depth() const
return the current depth value for clearing the background
Definition context.cxx:446
virtual void set_textured_material(const textured_material &mat)
set the current material
Definition context.cxx:1795
std::stack< std::vector< window_transformation > > window_transformation_stack
keep stack of window transformations
Definition context.h:773
vec4 get_bg_accum_color() const
return the current color value for clearing the accumulation buffer
Definition context.cxx:484
const cgv::media::illum::light_source & get_light_source(void *handle) const
read access to light source
Definition context.cxx:654
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:740
virtual void set_blend_state(BlendState state)
set the complete blend state
Definition context.cxx:1873
bool sRGB_framebuffer
whether to use opengl option to support sRGB framebuffer
Definition context.h:748
virtual RenderPassFlags get_default_render_pass_flags() const
return the default render pass flags
Definition context.cxx:882
virtual void set_bg_depth(float d)
set a user defined background depth value
Definition context.cxx:442
virtual void enable_blending()
enable blending
Definition context.cxx:1900
void set_current_material(shader_program &prog) const
set the shader program material to the currently enabled material
Definition context.cxx:711
void pop_cull_state()
pop the top of the current culling state from the stack
Definition context.cxx:1847
void pop_modelview_matrix()
see push_V for an explanation
Definition context.cxx:1958
void * default_light_source_handles[nr_default_light_sources]
handles of default light sources
Definition context.h:813
void push_modelview_matrix()
push the current viewing matrix onto a matrix stack for viewing matrices.
Definition context.cxx:1946
std::stack< frame_buffer_base * > frame_buffer_stack
stack of currently enabled frame buffers
Definition context.h:775
bool auto_set_view_in_current_shader_program
whether to automatically set viewing matrixes in current shader program, defaults to true
Definition context.h:730
void tesselate_unit_cone(int resolution=25, bool flip_normals=false, bool edges=false)
tesselate a cone of radius 1
Definition context.cxx:1327
CullingMode get_cull_state() const
return the current culling state
Definition context.cxx:1852
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:2045
const cgv::media::illum::surface_material * current_material_ptr
store pointer to current material
Definition context.h:817
virtual void set_modelview_matrix(const dmat4 &MV)
set the current modelview matrix, which transforms from world to eye space
Definition context.cxx:1980
void * get_enabled_light_source_handle(size_t i) const
access to handle of i-th light source
Definition context.cxx:777
cgv::media::font::font_face_ptr current_font_face
store current font
Definition context.h:843
virtual void set_depth_mask(bool flag)
set the depth buffer mask
Definition context.cxx:1929
bool auto_set_gamma_in_current_shader_program
whether to automatically set gamma in current shader program, defaults to true
Definition context.h:736
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:529
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:149
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:147
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:357
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:389
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:62
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), bool show_debug_info)
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 read_texture(cgv::data::data_view &data, unsigned level)
read back a texture into a data view with the glGetTexImage command
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
unsigned find_best_texture_format(const cgv::data::component_format &_cf, cgv::data::component_format *best_cf, const std::vector< data_view > *palettes, bool show_debug_info)
map the given component format to the best matching available gl component format
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 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:126
CullingMode
different culling modes
Definition context.h:199
BlendFunction
different blend functions
Definition context.h:206
AccessType
different access types
Definition context.h:322
TextureFilter
different texture filter
Definition context.h:245
TextureWrap
different texture wrap modes
Definition context.h:229
FrameBufferType
different frame buffer types which can be combined together with or
Definition context.h:546
MaterialSide
different sides of a material
Definition context.h:184
ProgramVariableKind
enumerates different kinds of shader program variables
Definition context.h:91
VertexBufferUsage
Provides vertex buffer usage hints as defined in OpenGL.
Definition context.h:487
ShaderType
different shader types
Definition context.h:543
PrimitiveType
different primitive types
Definition context.h:281
VertexBufferType
Provides vertex buffer types to allow implicit binding.
Definition context.h:474
@ VBT_VERTICES
The buffer contains vertices and will be bound to GL_ARRAY_BUFFER.
Definition context.h:476
@ VBT_TEXTURE
The buffer contains texture data and will be bound to GL_TEXTURE_BUFFER.
Definition context.h:478
@ VBT_INDICES
The buffer contains indices and will be bound to GL_ELEMENT_ARRAY_BUFFER.
Definition context.h:477
@ VBT_ATOMIC_COUNTER
The buffer contains atomic counter and will be bound to GL_ATOMIC_COUNTER_BUFFER.
Definition context.h:482
@ VBT_STORAGE
The buffer contains arbitrary data and will be bound to GL_SHADER_STORAGE_BUFFER.
Definition context.h:481
@ VBT_UNIFORM
The buffer contains uniforms and will be bound to GL_UNIFORM_BUFFER.
Definition context.h:479
@ VBT_INDIRECT
The buffer contains indirect draw commands and will be bound to GL_DRAW_INDIRECT_BUFFER.
Definition context.h:483
@ VBT_FEEDBACK
The buffer is used for transform&feedback and will be bound to GL_TRANSFORM_FEEDBACK_BUFFER.
Definition context.h:480
BufferTypeBits
Bits for the selection of different buffer types.
Definition context.h:519
TextureType
different texture types
Definition context.h:257
RenderPassFlags
available flags that can be queried from the context and set for a new render pass
Definition context.h:149
@ RPF_CLEAR_ACCUM
whether to clear the accumulation buffer
Definition context.h:164
@ RPF_SET_MODELVIEW
whether to set default modelview matrix
Definition context.h:152
@ RPF_SET_MATERIAL
whether to define default material
Definition context.h:156
@ RPF_SET_CLEAR_DEPTH
whether to set the clear color
Definition context.h:170
@ RPF_SET_LIGHTS
whether to define default lights
Definition context.h:155
@ RPF_CLEAR_COLOR
whether to clear the color buffer
Definition context.h:161
@ RPF_CLEAR_STENCIL
whether to clear the depth buffer
Definition context.h:163
@ RPF_SET_CLEAR_STENCIL
whether to set the clear color
Definition context.h:171
@ RPF_ENABLE_MATERIAL
whether to enable material
Definition context.h:158
@ RPF_DRAWABLES_INIT_FRAME
whether to call the init_frame method of the drawables
Definition context.h:167
@ RPF_CLEAR_DEPTH
whether to clear the depth buffer
Definition context.h:162
@ RPF_SET_STATE_FLAGS
whether to set depth buffer and culling flags
Definition context.h:168
@ RPF_SET_PROJECTION
whether to set default projection matrix
Definition context.h:151
@ RPF_SET_CLEAR_ACCUM
whether to set the accumulation buffer clear color
Definition context.h:172
@ RPF_SET_CLEAR_COLOR
whether to set the clear color
Definition context.h:169
@ RPF_SET_LIGHTS_ON
whether to turn on default lights
Definition context.h:157
CompareFunction
different comparison functions used for depth testing or texture comparisons
Definition context.h:310
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
Definition bool32_t.h:7
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:373
this header is dependency free
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:898
cgv::math::fvec< double, 3 > dvec3
declare type of 3d double precision floating point vectors
Definition fvec.h:690
cgv::math::fvec< uint32_t, 3 > uvec3
declare type of 3d 32 bit unsigned integer vectors
Definition fvec.h:716
cgv::math::fvec< int32_t, 4 > ivec4
declare type of 4d 32 bit integer vectors
Definition fvec.h:712
cgv::math::fmat< double, 4, 4 > dmat4
declare type of 4x4 matrices
Definition fmat.h:471
cgv::math::fvec< float, 3 > vec3
declare type of 3d single precision floating point vectors
Definition fvec.h:683
Helper functions to process strings.
Stores properties of a phong brdf material.
Stores properties of a surface material.
color_type diffuse_reflectance
diffuse reflectance of surface, defaults to 0.5,0.5,0.5
color_type emission
emissive color component, defaults to 0,0,0
color_type specular_reflectance
specular color used to modulate specular reflection component, should be 1,1,1
float transparency
modulation for transparency, defaults to 0
float roughness
surface roughness in the range [0,1] (1/2 trace of symmetric 2x2 matrix for anisotropic case where di...
float ambient_occlusion
scalar factor to down scale ambient light, defaults to 1
Represents a blend state used to configure fragment blending.
Definition context.h:696
bool enabled
whether blending is enabled
Definition context.h:698
BlendFunction dst_color
the destination color (rgb) factor
Definition context.h:702
BlendFunction dst_alpha
the destination alpha factor
Definition context.h:706
BlendFunction src_alpha
the source alpha factor
Definition context.h:704
BlendFunction src_color
the source color (rgb) factor
Definition context.h:700
Represents a buffer mask used to mask depth and color buffer outputs.
Definition context.h:711
Represents a depth test state used to configure depth testing.
Definition context.h:688
bool enabled
whether the depth test is enabled
Definition context.h:690
CompareFunction test_func
the function used to compare depth values
Definition context.h:692
configuration object used to define context parameters that need to be set already at creation time
Definition context.h:574
int version_minor
default: -1 ... minor version of maximum supported OpenGL version
Definition context.h:603
int version_major
default: -1 ... major version of maximum supported OpenGL version
Definition context.h:601
bool debug
default: false in release and true in debug version
Definition context.h:607
bool forward_compatible
default: false
Definition context.h:605
bool core_profile
default: true
Definition context.h:609
bool depth_buffer
default: true
Definition context.h:578
int max_compute_shared_memory_size
the total number of output values (a component, in GLSL terms, is a component of a vector....
Definition context.h:46
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:48
int max_compute_work_group_invocations
total available storage size in bytes for all shared variables in a compute shader
Definition context.h:47
int max_geometry_shader_total_output_component_count
the maximum number of components of outputs (out variables) written by a geometry shader
Definition context.h:45
int max_geometry_shader_output_vertex_count
the maximum supported size for renderbuffers in any dimension
Definition context.h:43
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:49
int max_geometry_shader_output_component_count
the maximum number that can be provided to the max_vertices output layout qualifier in a geometry sha...
Definition context.h:44
bool on_enter_children(group_ptr) override
called before the children of a group node g are processed, return whether these should be skipped....
bool on_leave_children(group_ptr) override
called when the children of a group node g have been left, return whether to terminate traversal
structure to store information on a shader program variable, i.e.
Definition context.h:100
compact type description of data that can be sent to the context; convertible to int
Definition context.h:61