Visualization Library 2.1.0

A lightweight C++ OpenGL middleware for 2D/3D graphics

VL     Star     Watch     Fork     Issue

[Download] [Tutorials] [All Classes] [Grouped Classes]
OpenGL.cpp
Go to the documentation of this file.
1 /**************************************************************************************/
2 /* */
3 /* Visualization Library */
4 /* http://visualizationlibrary.org */
5 /* */
6 /* Copyright (c) 2005-2020, Michele Bosi */
7 /* All rights reserved. */
8 /* */
9 /* Redistribution and use in source and binary forms, with or without modification, */
10 /* are permitted provided that the following conditions are met: */
11 /* */
12 /* - Redistributions of source code must retain the above copyright notice, this */
13 /* list of conditions and the following disclaimer. */
14 /* */
15 /* - Redistributions in binary form must reproduce the above copyright notice, this */
16 /* list of conditions and the following disclaimer in the documentation and/or */
17 /* other materials provided with the distribution. */
18 /* */
19 /* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND */
20 /* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */
21 /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE */
22 /* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR */
23 /* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES */
24 /* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; */
25 /* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON */
26 /* ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT */
27 /* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS */
28 /* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */
29 /* */
30 /**************************************************************************************/
31 
32 #include <vlGraphics/OpenGL.hpp>
33 #include <vlCore/String.hpp>
34 #include <vlCore/Log.hpp>
35 #include <vlCore/Say.hpp>
36 #include <algorithm>
37 
38 #if defined(VL_OPENGL_ES1) || defined(VL_OPENGL_ES2)
39  #include <EGL/egl.h> // for eglGetProcAddress()
40 #endif
41 
42 //-----------------------------------------------------------------------------
43 namespace vl
44 {
45  bool Is_OpenGL_Initialized = false;
46  bool Is_OpenGL_Core_Profile = false;
48 
49  bool Has_GL_Version_1_1 = false;
50  bool Has_GL_Version_1_2 = false;
51  bool Has_GL_Version_1_3 = false;
52  bool Has_GL_Version_1_4 = false;
53  bool Has_GL_Version_1_5 = false;
54  bool Has_GL_Version_2_0 = false;
55  bool Has_GL_Version_2_1 = false;
56  bool Has_GL_Version_3_0 = false;
57  bool Has_GL_Version_3_1 = false;
58  bool Has_GL_Version_3_2 = false;
59  bool Has_GL_Version_3_3 = false;
60  bool Has_GL_Version_4_0 = false;
61  bool Has_GL_Version_4_1 = false;
62 
64 
65  // GLES defines
66 
67  bool Has_GLES_Version_1_1 = false;
68  bool Has_GLES_Version_2_0 = false;
69  bool Has_GLES = false;
70 
71  // Helper defines
72 
73  bool Has_GLSL = false;
74  bool Has_GLSL_120_Or_More = false;
75  bool Has_GLSL_130_Or_More = false;
76  bool Has_GLSL_140_Or_More = false;
77  bool Has_GLSL_150_Or_More = false;
78  bool Has_GLSL_330_Or_More = false;
79  bool Has_GLSL_400_Or_More = false;
80  bool Has_GLSL_410_Or_More = false;
81  bool Has_Geometry_Shader = false;
82  bool Has_BufferObject = false;
83  bool Has_FBO = false;
84  bool Has_PBO = false;
85  bool Has_FBO_Multisample = false;
86  bool Has_Cubemap_Textures = false;
87  bool Has_Texture_Rectangle = false;
88  bool Has_Texture_Array = false;
89  bool Has_Texture_Buffer = false;
91  bool Has_Texture_3D = false;
92  bool Has_Multitexture = false;
93  bool Has_Primitive_Restart = false;
94  bool Has_Occlusion_Query = false;
95  bool Has_Transform_Feedback = false;
96  bool Has_glGenerateMipmaps = false;
97  bool Has_GL_GENERATE_MIPMAP = false;
98  bool Has_Point_Sprite = false;
99  bool Has_Base_Vertex = false;
101 
102  #define VL_EXTENSION(extension) bool Has_##extension = false;
103  #include <vlGraphics/GL/GLExtensionList.hpp>
104  #undef VL_EXTENSION
105 
106  #define VL_GLES_EXTENSION(extension) bool Has_##extension = false;
107  #include <vlGraphics/GL/GLESExtensionList.hpp>
108  #undef VL_GLES_EXTENSION
109 
110  #if defined(VL_OPENGL)
111  #define VL_GL_FUNCTION(TYPE, NAME) TYPE NAME = NULL;
112  #include <vlGraphics/GL/GLFunctionList.hpp>
113  #undef VL_GL_FUNCTION
114  #endif
115 
116  #if defined(VL_OPENGL_ES1)
117  #define VL_GL_FUNCTION(TYPE, NAME) TYPE NAME = NULL;
118  #include <vlGraphics/GL/GLES1FunctionList.hpp>
119  #undef VL_GL_FUNCTION
120  #endif
121 
122  #if defined(VL_OPENGL_ES2)
123  #define VL_GL_FUNCTION(TYPE, NAME) TYPE NAME = NULL;
124  #include <vlGraphics/GL/GLES2FunctionList.hpp>
125  #undef VL_GL_FUNCTION
126  #endif
127 
128  const GLenum Translate_Enable[] =
129  {
130  // Common ones
131  GL_BLEND,
132  GL_CULL_FACE,
133  GL_DEPTH_TEST,
134  GL_STENCIL_TEST,
135  GL_DITHER,
136  GL_POLYGON_OFFSET_FILL,
137  GL_POLYGON_OFFSET_LINE,
138  GL_POLYGON_OFFSET_POINT,
139  GL_COLOR_LOGIC_OP,
140  GL_MULTISAMPLE,
141 
142  // Smoothing
143  GL_POINT_SMOOTH,
144  GL_LINE_SMOOTH,
145  GL_POLYGON_SMOOTH,
146 
147  // Stippling
148  GL_LINE_STIPPLE,
149  GL_POLYGON_STIPPLE,
150 
151  // Point sprites
152  GL_POINT_SPRITE,
153  GL_PROGRAM_POINT_SIZE,
154 
155  // Fixed function pipeline
156  GL_ALPHA_TEST,
157  GL_LIGHTING,
158  GL_COLOR_SUM,
159  GL_FOG,
160  GL_NORMALIZE,
161  GL_RESCALE_NORMAL,
162 
163  // Available only under OpenGL 2.x
164  GL_VERTEX_PROGRAM_TWO_SIDE,
165 
166  // OpenGL 3.2
167  GL_TEXTURE_CUBE_MAP_SEAMLESS,
168 
169  // OpenGL 3.0
170  GL_CLIP_DISTANCE0,
171  GL_CLIP_DISTANCE1,
172  GL_CLIP_DISTANCE2,
173  GL_CLIP_DISTANCE3,
174  GL_CLIP_DISTANCE4,
175  GL_CLIP_DISTANCE5,
176  GL_CLIP_DISTANCE6,
177  GL_CLIP_DISTANCE7,
178 
179  // Multisampling
180  GL_SAMPLE_ALPHA_TO_COVERAGE,
181  GL_SAMPLE_ALPHA_TO_ONE,
182  GL_SAMPLE_COVERAGE
183  };
184 
185  const char* Translate_Enable_String[] =
186  {
187  // Common ones
188  "GL_BLEND",
189  "GL_CULL_FACE",
190  "GL_DEPTH_TEST",
191  "GL_STENCIL_TEST",
192  "GL_DITHER",
193  "GL_POLYGON_OFFSET_FILL",
194  "GL_POLYGON_OFFSET_LINE",
195  "GL_POLYGON_OFFSET_POINT",
196  "GL_COLOR_LOGIC_OP",
197  "GL_MULTISAMPLE",
198 
199  // Smoothing
200  "GL_POINT_SMOOTH",
201  "GL_LINE_SMOOTH",
202  "GL_POLYGON_SMOOTH",
203 
204  // Stippling
205  "GL_LINE_STIPPLE",
206  "GL_POLYGON_STIPPLE",
207 
208  // Point sprites
209  "GL_POINT_SPRITE",
210  "GL_PROGRAM_POINT_SIZE",
211 
212  // Fixed function pipeline
213  "GL_ALPHA_TEST",
214  "GL_LIGHTING",
215  "GL_COLOR_SUM",
216  "GL_FOG",
217  "GL_NORMALIZE",
218  "GL_RESCALE_NORMAL",
219 
220  // Available only under OpenGL 2.x
221  "GL_VERTEX_PROGRAM_TWO_SIDE",
222 
223  // OpenGL 3.2
224  "GL_TEXTURE_CUBE_MAP_SEAMLESS",
225 
226  // OpenGL 3.0
227  "GL_CLIP_DISTANCE0",
228  "GL_CLIP_DISTANCE1",
229  "GL_CLIP_DISTANCE2",
230  "GL_CLIP_DISTANCE3",
231  "GL_CLIP_DISTANCE4",
232  "GL_CLIP_DISTANCE5",
233  "GL_CLIP_DISTANCE6",
234  "GL_CLIP_DISTANCE7",
235 
236  // Multisampling
237  "GL_SAMPLE_ALPHA_TO_COVERAGE",
238  "GL_SAMPLE_ALPHA_TO_ONE",
239  "GL_SAMPLE_COVERAGE"
240  };
241 
243  {
244  // Common ones
245  false /*GL_BLEND*/,
246  false /*GL_CULL_FACE*/,
247  false /*GL_DEPTH_TEST*/,
248  false /*GL_STENCIL_TEST*/,
249  false /*GL_DITHER*/,
250  false /*GL_POLYGON_OFFSET_FILL*/,
251  false /*GL_POLYGON_OFFSET_LINE*/,
252  false /*GL_POLYGON_OFFSET_POINT*/,
253  false /*GL_COLOR_LOGIC_OP*/,
254  false /*GL_MULTISAMPLE*/,
255 
256  // Smoothing
257  false /*GL_POINT_SMOOTH*/,
258  false /*GL_LINE_SMOOTH*/,
259  false /*GL_POLYGON_SMOOTH*/,
260 
261  // Stippling
262  false /*GL_LINE_STIPPLE*/,
263  false /*GL_POLYGON_STIPPLE*/,
264 
265  // Point sprites
266  false /*GL_POINT_SPRITE*/,
267  false /*GL_PROGRAM_POINT_SIZE*/,
268 
269  // Fixed function pipeline
270  false /*GL_ALPHA_TEST*/,
271  false /*GL_LIGHTING*/,
272  false /*GL_COLOR_SUM*/,
273  false /*GL_FOG*/,
274  false /*GL_NORMALIZE*/,
275  false /*GL_RESCALE_NORMAL*/,
276 
277  // Available only under OpenGL 2.x
278  false /*GL_VERTEX_PROGRAM_TWO_SIDE*/,
279 
280  // OpenGL 3.2
281  false /*GL_TEXTURE_CUBE_MAP_SEAMLESS*/,
282 
283  // OpenGL 3.0
284  false /*GL_CLIP_DISTANCE0*/,
285  false /*GL_CLIP_DISTANCE1*/,
286  false /*GL_CLIP_DISTANCE2*/,
287  false /*GL_CLIP_DISTANCE3*/,
288  false /*GL_CLIP_DISTANCE4*/,
289  false /*GL_CLIP_DISTANCE5*/,
290  false /*GL_CLIP_DISTANCE6*/,
291  false /*GL_CLIP_DISTANCE7*/,
292 
293  // Multisampling
294  false /*GL_SAMPLE_ALPHA_TO_COVERAGE*/,
295  false /*GL_SAMPLE_ALPHA_TO_ONE*/,
296  false /*GL_SAMPLE_COVERAGE*/
297  };
298 
299  VL_COMPILE_TIME_CHECK( EN_EnableCount == sizeof(Is_Enable_Supported) / sizeof(Is_Enable_Supported[0]) );
300  VL_COMPILE_TIME_CHECK( EN_EnableCount == sizeof(Translate_Enable) / sizeof(Translate_Enable[0]) );
301  VL_COMPILE_TIME_CHECK( EN_EnableCount == sizeof(Translate_Enable_String) / sizeof(Translate_Enable_String[0]) );
302 }
303 //-----------------------------------------------------------------------------
305 {
306  Is_OpenGL_Initialized = false;
307 
308  // clear errors
309  glGetError();
310 
311  // check OpenGL context is present
312  if (glGetError() != GL_NO_ERROR)
313  return false;
314 
315  // - - - OpenGL function pointers - - -
316 
317  // opengl function pointer initialization
318  #if defined(VL_OPENGL)
319  #define VL_GL_FUNCTION(TYPE, NAME) NAME = (TYPE)getGLProcAddress(#NAME);
320  #include <vlGraphics/GL/GLFunctionList.hpp>
321  #endif
322 
323  // opengl function pointer initialization
324  #if defined(VL_OPENGL_ES1)
325  #define VL_GL_FUNCTION(TYPE, NAME) NAME = (TYPE)getGLProcAddress(#NAME);
326  #include <vlGraphics/GL/GLES1FunctionList.hpp>
327  #endif
328 
329  // opengl function pointer initialization
330  #if defined(VL_OPENGL_ES2)
331  #define VL_GL_FUNCTION(TYPE, NAME) NAME = (TYPE)getGLProcAddress(#NAME);
332  #include <vlGraphics/GL/GLES2FunctionList.hpp>
333  #endif
334 
335  // - - - OpenGL versions - - -
336 
337  // GLES detect
338  #if defined(VL_OPENGL_ES1)
340  #endif
341 
342  #if defined(VL_OPENGL_ES2)
344  #endif
345 
346  // GL versions
347  // OpenGL ES returns "OpenGL ES-XX N.M"
348  const char* version_string = (const char*)glGetString(GL_VERSION);
349 
350  // These stay zero for GLES
351  const int vmaj = Has_GLES ? 0 : version_string[0] - '0';
352  const int vmin = Has_GLES ? 0 : version_string[2] - '0';
353 
354  // Check fixed function pipeline
355 #if defined(VL_OPENGL_ES2)
356  Is_OpenGL_Core_Profile = false;
359 #elif defined(VL_OPENGL_ES1)
360  Is_OpenGL_Core_Profile = false;
363 #else
365  if( vmaj >= 3 )
366  {
367  int forward_compatible = 0;
368  glGetIntegerv( GL_CONTEXT_FLAGS, &forward_compatible ); VL_CHECK_OGL();
369  Is_OpenGL_Forward_Compatible = (forward_compatible & GL_CONTEXT_FLAG_FORWARD_COMPATIBLE_BIT) != 0;
370  }
371 
372  Is_OpenGL_Core_Profile = false;
373  const int version = vmaj*10 + vmin;
374  if( version >= 32 )
375  {
376  // Valid for WGL and GLX
377  #define CONTEXT_CORE_PROFILE_BIT 0x00000001
378  #define CONTEXT_COMPATIBILITY_PROFILE_BIT 0x00000002
379  #define CONTEXT_PROFILE_MASK 0x9126
380 
381  // Note:
382  // - This should be non-0 by when using wglCreateContextAttribs() and is == 0 when creating a GL context in the old way.
383  // - Creating a context in the old way returns the highest compatible OpenGL version available thus the presence
384  // of CONTEXT_COMPATIBILITY_PROFILE_BIT is not enough, we need to check the absence of CONTEXT_CORE_PROFILE_BIT
385  int context_flags = 0;
386  glGetIntegerv( CONTEXT_PROFILE_MASK, &context_flags ); VL_CHECK_OGL();
387  Is_OpenGL_Core_Profile = (context_flags & CONTEXT_CORE_PROFILE_BIT) != 0;
388  }
389 
391 #endif
392 
393  Has_GL_Version_1_1 = (vmaj == 1 && vmin >= 1) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
394  Has_GL_Version_1_2 = (vmaj == 1 && vmin >= 2) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
395  Has_GL_Version_1_3 = (vmaj == 1 && vmin >= 3) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
396  Has_GL_Version_1_4 = (vmaj == 1 && vmin >= 4) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
397  Has_GL_Version_1_5 = (vmaj == 1 && vmin >= 5) || (vmaj > 1 && Has_Fixed_Function_Pipeline);
398  Has_GL_Version_2_0 = (vmaj == 2 && vmin >= 0) || (vmaj > 2 && Has_Fixed_Function_Pipeline);
399  Has_GL_Version_2_1 = (vmaj == 2 && vmin >= 1) || (vmaj > 2 && Has_Fixed_Function_Pipeline);
400  Has_GL_Version_3_0 = (vmaj == 3 && vmin >= 0) || (vmaj > 3 && Has_Fixed_Function_Pipeline);
401  Has_GL_Version_3_1 = (vmaj == 3 && vmin >= 1) || (vmaj > 3 && Has_Fixed_Function_Pipeline);
402  Has_GL_Version_3_2 = (vmaj == 3 && vmin >= 2) || (vmaj > 3 && Has_Fixed_Function_Pipeline);
403  Has_GL_Version_3_3 = (vmaj == 3 && vmin >= 3) || (vmaj > 3 && Has_Fixed_Function_Pipeline);
404  Has_GL_Version_4_0 = (vmaj == 4 && vmin >= 0) || (vmaj > 4 && Has_Fixed_Function_Pipeline);
405  Has_GL_Version_4_1 = (vmaj == 4 && vmin >= 1) || (vmaj > 4 && Has_Fixed_Function_Pipeline);
406 
407  // - - - Extension strings init - - -
408 
409  std::string extensions = getOpenGLExtensions();
410 
411  #define VL_EXTENSION(extension) Has_##extension = strstr(extensions.c_str(), #extension" ") != NULL;
412  #include <vlGraphics/GL/GLExtensionList.hpp>
413  #undef VL_EXTENSION
414 
415  #define VL_GLES_EXTENSION(extension) Has_##extension = strstr(extensions.c_str(), #extension" ") != NULL;
416  #include <vlGraphics/GL/GLESExtensionList.hpp>
417  #undef VL_GLES_EXTENSION
418 
419 #if defined(VL_OPENGL_ES1)
420  // mic fixme
421  // POWERVR emulator bugs: http://www.imgtec.com/forum/forum_posts.asp?TID=1379
422  if (Has_GL_OES_texture_cube_map && glTexGenfOES == 0)
423  {
424  Has_GL_OES_texture_cube_map = false;
425  Has_Cubemap_Textures = false;
426  Log::error("GL_OES_texture_cube_map exposed but glTexGenfOES == NULL!\n"); /*VL_TRAP();*/
427  }
428  if(Has_GL_OES_blend_func_separate && glBlendFuncSeparateOES == 0)
429  {
430  Has_GL_OES_blend_func_separate = false;
431  Log::error("GL_OES_blend_func_separate exposed but glBlendFuncSeparateOES == NULL!\n"); /*VL_TRAP();*/
432  }
433  if (Has_GL_OES_fixed_point && glAlphaFuncxOES == NULL)
434  {
435  Log::warning("GL_OES_fixed_point functions are not exposed with their OES suffix!\n");
436  }
437  if (Has_GL_OES_single_precision && glDepthRangefOES == NULL)
438  {
439  Log::warning("GL_OES_single_precision functions are not exposed with their OES suffix!\n");
440  }
441 #endif
442 
443  // - - - Helper defines - - -
444 
445  // Note that GL extensions pertaining to deprecated features seem to be exposed under Core profiles even if they are not supported (like Has_GL_SGIS_generate_mipmap)
446 
447  Has_GLSL = Has_GL_ARB_shading_language_100 || Has_GL_Version_2_0 || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GLES_Version_2_0;
455  Has_Geometry_Shader = Has_GL_NV_geometry_shader4 || Has_GL_EXT_geometry_shader4 || Has_GL_ARB_geometry_shader4 || Has_GL_Version_3_2 || Has_GL_Version_4_0;
456  Has_BufferObject = Has_GL_ARB_vertex_buffer_object || Has_GL_Version_1_5 || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GLES;
457  Has_FBO = Has_GL_EXT_framebuffer_object || Has_GL_ARB_framebuffer_object || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GL_OES_framebuffer_object || Has_GLES_Version_2_0;
458  Has_PBO = Has_GL_ARB_pixel_buffer_object || Has_GL_EXT_pixel_buffer_object || Has_GL_Version_2_1 || Has_GL_Version_3_0 || Has_GL_Version_4_0;
459  // We only support GL_ANGLE_framebuffer_blit for GLES, see also:
460  // http://www.khronos.org/registry/gles/extensions/IMG/IMG_multisampled_render_to_texture.txt
461  // http://www.khronos.org/registry/gles/extensions/APPLE/APPLE_framebuffer_multisample.txt
462  Has_FBO_Multisample = Has_GL_Version_4_0 || Has_GL_Version_3_0 || Has_GL_ARB_framebuffer_object || Has_GL_EXT_framebuffer_multisample || Has_GL_ANGLE_framebuffer_multisample;
463  Has_Cubemap_Textures = Has_GL_ARB_texture_cube_map || Has_GL_Version_1_3 || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GL_OES_texture_cube_map || Has_GLES_Version_2_0;
464  Has_Texture_Rectangle = Has_GL_ARB_texture_rectangle || Has_GL_NV_texture_rectangle || Has_GL_Version_3_1 || Has_GL_Version_4_0;
465  Has_Texture_Array = Has_GL_EXT_texture_array || Has_GL_Version_3_0 || Has_GL_Version_4_0;
466  Has_Texture_Buffer = Has_GL_ARB_texture_buffer_object || Has_GL_EXT_texture_buffer_object || Has_GL_Version_3_1 || Has_GL_Version_4_0;
467  Has_Texture_Multisample = Has_GL_ARB_texture_multisample || Has_GL_Version_3_2 || Has_GL_Version_4_0;
468  Has_Texture_3D = Has_GL_EXT_texture3D || Has_GL_Version_1_2 || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GL_OES_texture_3D;
471  Has_Occlusion_Query = Has_GL_ARB_occlusion_query || Has_GL_Version_1_5 || Has_GL_Version_3_0 || Has_GL_Version_4_0;
472  Has_Transform_Feedback = Has_GL_NV_transform_feedback || Has_GL_EXT_transform_feedback || Has_GL_Version_3_0 || Has_GL_Version_4_0;
473  Has_glGenerateMipmaps = Has_GL_ARB_framebuffer_object || Has_GL_Version_3_0 || Has_GL_Version_4_0 || Has_GLES_Version_2_0;
475  Has_Point_Sprite = Has_GL_NV_point_sprite || Has_GL_ARB_point_sprite || Has_GLSL || Has_GLES_Version_1_1;
476  Has_Base_Vertex = Has_GL_Version_3_2 || Has_GL_Version_4_0 || Has_GL_ARB_draw_elements_base_vertex;
477  Has_Primitive_Instancing = Has_GL_Version_3_1 || Has_GL_Version_4_0 || Has_GL_ARB_draw_instanced || Has_GL_EXT_draw_instanced;
478 
479  // - - - Resolve supported enables - - -
480 
481  // Common ones
492 
493  // Smooth
497 
498  // Stipple
501 
502  // Point sprites
503  // Point sprites when !Has_Fixed_Function_Pipeline is considered always enabled but GL_POINT_SPRITE should not be called even if GL_OES_point_sprite, GL_ARB_point_sprite etc. are exposed!
504  // Note that calling glIsEnabled() with the two below under a Core profile returns true for the same reason.
505  Is_Enable_Supported[EN_POINT_SPRITE] = (Has_GL_NV_point_sprite||Has_GL_ARB_point_sprite||Has_GL_Version_2_0||Has_GL_OES_point_sprite||Has_GLES_Version_1_1) && Has_Fixed_Function_Pipeline;
506  Is_Enable_Supported[EN_PROGRAM_POINT_SIZE] = Has_GLSL && !Has_GLES_Version_2_0; // Only OpenGL ES 2 does not support glPointSize()/GL_POINT_SIZE
507 
508  // Fixed function pipeline
515 
516  // Available only under OpenGL 2.x
517  Is_Enable_Supported[EN_VERTEX_PROGRAM_TWO_SIDE] = ((Has_GL_ARB_vertex_program||Has_GL_NV_vertex_program) && Has_GL_Version_1_1) || Has_GL_Version_2_0;
518 
519  // OpenGL 3.2
520  Is_Enable_Supported[EN_TEXTURE_CUBE_MAP_SEAMLESS] = Has_GL_AMD_seamless_cubemap_per_texture||Has_GL_ARB_seamless_cube_map||Has_GL_Version_3_2||Has_GL_Version_4_0;
521 
522  // Clipping planes
523  int max_clip_planes = 0;
524  // OpenGL ES 2 is the only one without clipping planes!
526  {
527  glGetIntegerv(GL_MAX_CLIP_DISTANCES, &max_clip_planes); // GL_MAX_CLIP_DISTANCES == GL_MAX_CLIP_PLANES
528  }
529  Is_Enable_Supported[EN_CLIP_DISTANCE0] = max_clip_planes >= 1;
530  Is_Enable_Supported[EN_CLIP_DISTANCE1] = max_clip_planes >= 2;
531  Is_Enable_Supported[EN_CLIP_DISTANCE2] = max_clip_planes >= 3;
532  Is_Enable_Supported[EN_CLIP_DISTANCE3] = max_clip_planes >= 4;
533  Is_Enable_Supported[EN_CLIP_DISTANCE4] = max_clip_planes >= 5;
534  Is_Enable_Supported[EN_CLIP_DISTANCE5] = max_clip_planes >= 6;
535  Is_Enable_Supported[EN_CLIP_DISTANCE6] = max_clip_planes >= 7;
536  Is_Enable_Supported[EN_CLIP_DISTANCE7] = max_clip_planes >= 8;
537 
538  // Multisampling
542 
543  // Driver bugs
544  // MESA Mesa 11.2.0 advertises these extensions but does not support them!
545  if ( strstr( version_string, " Mesa " ) ) {
546  Has_GL_ARB_get_program_binary = false;
547  Has_GL_ARB_separate_shader_objects = false;
548  }
549 
550 #ifndef NDEBUG
551  // Enables management debug code
552  VL_CHECK_OGL();
553  bool got_error = false;
554  for(int i=0; i<EN_EnableCount; ++i)
555  {
556  glDisable(Translate_Enable[i]); // glIsEnabled() for some reason is not reliable!
557  bool supported = glGetError() == GL_NO_ERROR;
558  if (supported != Is_Enable_Supported[i])
559  {
560  Log::error( Say("%s: capability %s supported! This is a harmless glitch either in your GL driver or in VL.\n") << Translate_Enable_String[i] << (supported? "*IS*" : "*IS NOT*") );
561  got_error = true;
562  }
563  }
564  if(got_error)
565  {
566  printf("OpenGL Version = %s\n", glGetString(GL_VERSION));
567  #define PRINT_INFO(STRING) printf(#STRING" = %d\n", STRING?1:0)
583  // VL_TRAP();
584  }
585 #endif
586 
587  VL_CHECK_OGL();
588  return Is_OpenGL_Initialized = true;
589 }
590 //-----------------------------------------------------------------------------
591 const char* vl::getGLErrorString(int err)
592 {
593  switch(err)
594  {
595  case GL_INVALID_ENUM: return "Invalid enum";
596  case GL_INVALID_VALUE: return "Invalid value";
597  case GL_INVALID_OPERATION: return "Invalid operation";
598  case GL_STACK_OVERFLOW: return "Stack overflow";
599  case GL_STACK_UNDERFLOW: return "Stack underflow";
600  case GL_OUT_OF_MEMORY: return "Out of memory";
601  default:
602  return "";
603  }
604 }
605 //------------------------------------------------------------------------------
606 int vl::glcheck(const char* file, int line)
607 {
608  unsigned int glerr = glGetError();
609  // if an OpenGL context is available this must be clear!
610  if ( glGetError() )
611  {
612  Log::bug( Say("%s:%n: NO OPENGL CONTEXT ACTIVE!\n") << file << line );
613  }
614  else
615  if (glerr != GL_NO_ERROR)
616  {
617  String msg( (char*)getGLErrorString(glerr) );
618  Log::bug( Say("glGetError() [%s:%n]: %s\n") << file << line << msg );
619  }
620  return glerr;
621 }
622 //------------------------------------------------------------------------------
623 // vl::getGLProcAddress() implementation based on GLEW's
624 //------------------------------------------------------------------------------
625 #if defined(VL_OPENGL_ES1) || defined(VL_OPENGL_ES2)
626 void* vl::getGLProcAddress(const char* name)
627 {
628  void* func = (void*)eglGetProcAddress(name);
629  /*if (func)
630  Log::warning( String().printf("+ %s\n", name) );
631  else
632  Log::error( String().printf("- %s\n", name) );*/
633  return func;
634 }
635 #elif defined(VL_PLATFORM_WINDOWS)
636 void* vl::getGLProcAddress(const char* name)
637 {
638  return (void*)wglGetProcAddress((LPCSTR)name);
639 }
640 #elif defined(VL_PLATFORM_LINUX)
641 void* vl::getGLProcAddress(const char* name)
642 {
643  return (void*)(*glXGetProcAddress)((const GLubyte*)name);
644 }
645 #elif defined(__APPLE__)
646 #include <stdlib.h>
647 #include <string.h>
648 #include <AvailabilityMacros.h>
649 
650 #ifdef MAC_OS_X_VERSION_10_3
651 
652 #include <dlfcn.h>
653 
654 void* vl::getGLProcAddress(const char* name)
655 {
656  static void* image = NULL;
657  if (NULL == image)
658  {
659  image = dlopen("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", RTLD_LAZY);
660  }
661  return image ? dlsym(image, name) : NULL;
662 }
663 
664 #else
665 
666 #include <mach-o/dyld.h>
667 
668 void* vl::getGLProcAddress(const char*name)
669 {
670  static const struct mach_header* image = NULL;
671  NSSymbol symbol;
672  char* symbolName;
673  if (NULL == image)
674  {
675  image = NSAddImage("/System/Library/Frameworks/OpenGL.framework/Versions/Current/OpenGL", NSADDIMAGE_OPTION_RETURN_ON_ERROR);
676  }
677  /* prepend a '_' for the Unix C symbol mangling convention */
678  symbolName = malloc(strlen(name) + 2);
679  strcpy(symbolName+1, name);
680  symbolName[0] = '_';
681  symbol = NULL;
682  /* if (NSIsSymbolNameDefined(symbolName))
683  symbol = NSLookupAndBindSymbol(symbolName); */
684  symbol = image ? NSLookupSymbolInImage(image, symbolName, NSLOOKUPSYMBOLINIMAGE_OPTION_BIND | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR) : NULL;
685  free(symbolName);
686  return symbol ? NSAddressOfSymbol(symbol) : NULL;
687 }
688 
689 #endif /* MAC_OS_X_VERSION_10_3 */
690 
691 /* __APPLE__ end */
692 
693 #elif defined(__sgi) || defined (__sun)
694 
695 #include <dlfcn.h>
696 #include <stdio.h>
697 #include <stdlib.h>
698 
699 void* vl::getGLProcAddress(const char* name)
700 {
701  static void* h = NULL;
702  static void* gpa;
703 
704  if (h == NULL)
705  {
706  if ((h = dlopen(NULL, RTLD_LAZY | RTLD_LOCAL)) == NULL) return NULL;
707  gpa = dlsym(h, "glXGetProcAddress");
708  }
709 
710  if (gpa != NULL)
711  return ((void*(*)(const GLubyte*))gpa)((const GLubyte*)name);
712  else
713  return dlsym(h, name);
714 }
715 
716 /* __sgi || __sun end */
717 
718 #else
719 void* vl::getGLProcAddress(const char* name)
720 {
721  return NULL;
722 }
723 #endif
724 //------------------------------------------------------------------------------
bool Has_GLES_Version_1_1
Definition: OpenGL.cpp:67
bool Has_GL_Version_1_4
Definition: OpenGL.cpp:52
If enabled, clip geometry against user-defined half space #7.
If enabled, clip geometry against user-defined half space #1.
bool Has_Cubemap_Textures
Definition: OpenGL.cpp:86
const GLenum Translate_Enable[]
Definition: OpenGL.cpp:128
bool Has_Texture_Multisample
Definition: OpenGL.cpp:90
bool Has_GL_Version_4_1
Definition: OpenGL.cpp:61
bool Has_GLSL_140_Or_More
Definition: OpenGL.cpp:76
bool Has_glGenerateMipmaps
Definition: OpenGL.cpp:96
bool Has_GL_Version_1_1
Definition: OpenGL.cpp:49
If enabled, do depth comparisons and update the depth buffer; Note that even if the depth buffer exis...
bool Has_Multitexture
Definition: OpenGL.cpp:92
#define PRINT_INFO(STRING)
A simple String formatting class.
Definition: Say.hpp:124
const char * Translate_Enable_String[]
Definition: OpenGL.cpp:185
bool Has_GLSL_410_Or_More
Definition: OpenGL.cpp:80
bool Has_GLSL_150_Or_More
Definition: OpenGL.cpp:77
static void warning(const String &message)
Use this function to provide information about situations that might lead to errors or loss of data...
Definition: Log.cpp:155
bool Has_GLSL_400_Or_More
Definition: OpenGL.cpp:79
bool Has_GL_Version_2_1
Definition: OpenGL.cpp:55
VLGRAPHICS_EXPORT void * getGLProcAddress(const char *name)
Returns the address of the specified OpenGL function if supported by the active OpenGL driver and pro...
Definition: OpenGL.cpp:719
#define CONTEXT_CORE_PROFILE_BIT
bool Is_OpenGL_Forward_Compatible
OpenGL: true if the current context has been created with the WGL_CONTEXT_FORWARD_COMPATIBLE_BIT_ARB ...
Definition: OpenGL.cpp:47
The String class implements an advanced UTF16 (Unicode BMP) string manipulation engine.
Definition: String.hpp:62
VLGRAPHICS_EXPORT int glcheck(const char *file, int line)
Definition: OpenGL.cpp:606
If enabled, use the current polygon stipple pattern when rendering polygons, see also PolygonStipple...
If enabled, use the current lighting parameters to compute the vertex color; Otherwise, simply associate the current color with each vertex, see also Material, LightModel, and Light.
[GL_VERTEX_PROGRAM_POINT_SIZE/GL_PROGRAM_POINT_SIZE] If enabled, and a vertex shader is active...
If enabled, blend the incoming RGBA color values with the values in the color buffers, see also BlendFunc for more information.
If enabled, dither color components or indices before they are written to the color buffer...
If enabled, add the secondary color value to the computed fragment color.
static void error(const String &message)
Use this function to provide information about run-time errors: file not found, out of memory...
Definition: Log.cpp:165
bool Has_Point_Sprite
Definition: OpenGL.cpp:98
bool Has_Texture_Rectangle
Definition: OpenGL.cpp:87
bool Has_GL_Version_3_3
Definition: OpenGL.cpp:59
bool Has_Primitive_Instancing
Definition: OpenGL.cpp:100
bool Has_GL_Version_4_0
Definition: OpenGL.cpp:60
bool Is_OpenGL_Core_Profile
OpenGL: true if the current context has been created with the WGL_CONTEXT_CORE_PROFILE_BIT_ARB or equ...
Definition: OpenGL.cpp:46
If enabled, draw points with proper filtering; Otherwise, draw aliased points, see also PointSize...
bool Is_OpenGL_Initialized
Set to true if the last call to vl::initializeOpenGL() was succesful.
Definition: OpenGL.cpp:45
bool Has_GL_Version_3_0
Definition: OpenGL.cpp:56
If enabled, an offset is added to depth values of a polygon&#39;s fragments before the depth comparison i...
If enabled, performs alpha testing, see also AlphaFunc for more information.
If enabled, and if the polygon is rendered in GL_FILL mode, an offset is added to depth values of a p...
If enabled, clip geometry against user-defined half space #5.
Visualization Library main namespace.
bool Has_GLSL
Definition: OpenGL.cpp:73
bool Has_BufferObject
Definition: OpenGL.cpp:82
bool Has_GLES_Version_2_0
Definition: OpenGL.cpp:68
bool Has_Texture_Array
Definition: OpenGL.cpp:88
static void bug(const String &message)
Use this function to provide information about programming errors: wrong parameter initialization...
Definition: Log.cpp:175
bool Has_Transform_Feedback
Definition: OpenGL.cpp:95
For internal use only.
bool Has_GL_Version_1_3
Definition: OpenGL.cpp:51
bool Has_GLSL_120_Or_More
Definition: OpenGL.cpp:74
If enabled, each sample alpha value is replaced by the maximum representable alpha value...
VLGRAPHICS_EXPORT bool initializeOpenGL()
To test whether OpenGL has been initialized at least once check vl::Is_OpenGL_Initialized.
Definition: OpenGL.cpp:304
bool Has_GL_Version_1_5
Definition: OpenGL.cpp:53
bool Has_GL_Version_3_2
Definition: OpenGL.cpp:58
If enabled, clip geometry against user-defined half space #2.
If enabled, draw lines with correct filtering; Otherwise, draw aliased lines, see also LineWidth...
VL_COMPILE_TIME_CHECK(sizeof(i8) *8==8)
bool Has_GL_GENERATE_MIPMAP
Definition: OpenGL.cpp:97
#define CONTEXT_PROFILE_MASK
bool Has_Occlusion_Query
Definition: OpenGL.cpp:94
If enabled, compute a temporary coverage value where each bit is determined by the alpha value at the...
If enabled, calculate texture coordinates for points based on texture environment and point parameter...
bool Has_GLES
Definition: OpenGL.cpp:69
bool Has_Geometry_Shader
Definition: OpenGL.cpp:81
#define NULL
Definition: OpenGLDefs.hpp:81
If enabled, apply the currently selected logical operation to the incoming RGBA color and color buffe...
If enabled, clip geometry against user-defined half space #0.
If enabled, blend a fog color into the post-texturing color, see also Fog.
bool Has_Texture_Buffer
Definition: OpenGL.cpp:89
bool Has_GL_Version_3_1
Definition: OpenGL.cpp:57
VLGRAPHICS_EXPORT const char * getGLErrorString(int err)
Returns a readable string corresponding to the given OpenGL error code as returned by glGetError() ...
Definition: OpenGL.cpp:591
#define VL_CHECK_OGL()
Definition: OpenGL.hpp:156
If enabled, use multiple fragment samples in computing the final color of a pixel.
bool Has_GLSL_330_Or_More
Definition: OpenGL.cpp:78
If enabled, and a vertex shader is active, it specifies that the GL will choose between front and bac...
If enabled, do stencil testing and update the stencil buffer, see also StencilFunc and StencilOp...
bool Has_GL_Version_2_0
Definition: OpenGL.cpp:54
bool Has_GLSL_130_Or_More
Definition: OpenGL.cpp:75
If enabled, clip geometry against user-defined half space #6.
bool Has_Fixed_Function_Pipeline
OpenGL: true if !Is_OpenGL_Forward_Compatible && !Is_OpenGL_Core_Profile OpenGL ES 1: always true Ope...
Definition: OpenGL.cpp:63
bool Has_Base_Vertex
Definition: OpenGL.cpp:99
bool Has_Primitive_Restart
Definition: OpenGL.cpp:93
bool Is_Enable_Supported[EN_EnableCount]
Definition: OpenGL.cpp:242
If enabled, cubemap textures are sampled such that when linearly sampling from the border between two...
If enabled, the fragment&#39;s coverage is ANDed with the temporary coverage value; If GL_SAMPLE_COVERAGE...
bool Has_PBO
Definition: OpenGL.cpp:84
bool Has_GL_Version_1_2
Definition: OpenGL.cpp:50
bool Has_FBO
Definition: OpenGL.cpp:83
If enabled, draw polygons with proper filtering; Otherwise, draw aliased polygons; For correct antial...
If enabled, normals are scaled by a scaling factor derived from the modelview matrix; vl::EN_RESCALE_...
If enabled, normal vectors are scaled to unit length after transformation, see also vl::EN_RESCALE_NO...
If enabled, and if the polygon is rendered in GL_LINE mode, an offset is added to depth values of a p...
bool Has_Texture_3D
Definition: OpenGL.cpp:91
bool Has_FBO_Multisample
Definition: OpenGL.cpp:85
If enabled, clip geometry against user-defined half space #3.
If enabled, use the current line stipple pattern when drawing lines, see also LineStipple.
If enabled, cull polygons based on their winding in window coordinates, see also CullFace.
If enabled, clip geometry against user-defined half space #4.