GCC Code Coverage Report


Directory: ./
File: lib/geogram_gfx/basic/GL.h
Date: 2026-09-07 02:37:58
Exec Total Coverage
Lines: 0 6 0.0%
Functions: 0 2 0.0%
Branches: 0 0 -%

Line Branch Exec Source
1 /*
2 * Copyright (c) 2000-2022 Inria
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions are met:
7 *
8 * * Redistributions of source code must retain the above copyright notice,
9 * this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright notice,
11 * this list of conditions and the following disclaimer in the documentation
12 * and/or other materials provided with the distribution.
13 * * Neither the name of the ALICE Project-Team nor the names of its
14 * contributors may be used to endorse or promote products derived from this
15 * software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
21 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27 * POSSIBILITY OF SUCH DAMAGE.
28 *
29 * Contact: Bruno Levy
30 *
31 * https://www.inria.fr/fr/bruno-levy
32 *
33 * Inria,
34 * Domaine de Voluceau,
35 * 78150 Le Chesnay - Rocquencourt
36 * FRANCE
37 *
38 */
39
40 #ifndef GEOGRAM_GFX_BASIC_GL
41 #define GEOGRAM_GFX_BASIC_GL
42
43 #ifdef GEO_DEBUG
44 #define GEO_DEBUG_GL
45 #endif
46
47 #include <geogram_gfx/basic/common.h>
48 #include <geogram_gfx/api/defs.h>
49
50
51 #if defined(GEO_OS_EMSCRIPTEN)
52 # define GLFW_INCLUDE_ES2
53 # include <GLFW/glfw3.h>
54 # define GL_GLEXT_PROTOTYPES
55 # include <GLES2/gl2ext.h>
56 # define GL_INVALID_INDEX GLuint(-1)
57 typedef double GLdouble;
58 # define glGenVertexArrays glGenVertexArraysOES
59 # define glBindVertexArray glBindVertexArrayOES
60 # define glDeleteVertexArrays glDeleteVertexArraysOES
61 # define GEO_GL_ES2
62 # define GEO_GL_NO_DOUBLES
63 #elif defined(GEO_OS_ANDROID)
64 # include <GLES3/gl3.h>
65 # include <GLES3/gl31.h>
66 # include <GLES3/gl32.h>
67 # define GEO_GL_TEXTURE_3D
68 # define GEO_GL_ES2
69 # define GEO_GL_140
70 # define GEO_GL_150
71 # define GEO_GL_NO_DOUBLES
72 #else
73 # include <geogram_gfx/third_party/glad/glad.h>
74 # define GEO_GL_TEXTURE_3D
75 # define GEO_GL_140
76 # define GEO_GL_150
77 # define GEO_GL_440
78 # define GEO_GL_ES2
79 #endif
80
81 #include <geogram_gfx/GLUP/GLUP.h>
82 #include <geogram/basic/geometry.h>
83
84 // Some defines missing in Emscripten GL headers
85 #if defined(GEO_OS_EMSCRIPTEN) || defined(GEO_OS_ANDROID)
86 # ifndef GL_RGB8
87 # define GL_RGB8 0x8051
88 # endif
89
90 # ifndef GL_RGBA8
91 # define GL_RGBA8 0x8058
92 # endif
93
94 # ifndef GL_R16F
95 # define GL_R16F 0x822D
96 # endif
97
98 # ifndef GL_R32F
99 # define GL_R32F 0x822E
100 # endif
101
102 # ifndef GL_RED
103 # define GL_RED 0x1903
104 # endif
105
106 # ifndef GL_R8
107 # define GL_R8 0x8229
108 # endif
109
110 # ifndef GL_R16
111 # define GL_R16 0x822A
112 # endif
113
114 # ifndef GL_DEPTH_COMPONENT24
115 # define GL_DEPTH_COMPONENT24 0x81A6
116 # endif
117
118 #endif
119
120 /**
121 * \file geogram_gfx/basic/GL.h
122 * \brief Some utility functions for OpenGL graphics.
123 */
124
125 namespace GEO {
126
127 namespace GL {
128 /**
129 * \brief Initializes some GL functions and objects.
130 * \details Called by GEO::Graphics::initialize()
131 */
132 void GEOGRAM_GFX_API initialize();
133
134 /**
135 * \brief Terminates GL functions and objects.
136 * \details Called by GEO::Graphics::terminate()
137 */
138 void GEOGRAM_GFX_API terminate();
139 }
140
141
142 /**
143 * \brief Sends a vertex to OpenGL.
144 * \param[in] v a const reference to the vertex to be sent.
145 */
146 inline void glupVertex(const vec2& v) {
147 glupVertex2dv(v.data());
148 }
149
150 /**
151 * \brief Sends a vertex to OpenGL.
152 * \param[in] v a const reference to the vertex to be sent.
153 */
154 inline void glupVertex(const vec3& v) {
155 glupVertex3dv(v.data());
156 }
157
158 /**
159 * \brief Sends a vertex to OpenGL.
160 * \param[in] v a const reference to the vertex to be sent, in
161 * homogeneous coordinates (4d).
162 */
163 inline void glupVertex(const vec4& v) {
164 glupVertex4dv(v.data());
165 }
166
167 /**
168 * \brief Sends a RGB color to OpenGL.
169 * \param[in] v a const reference to the color to be sent.
170 */
171 inline void glupColor(const vec3& v) {
172 glupColor3dv(v.data());
173 }
174
175 /**
176 * \brief Sends a RGBA color to OpenGL.
177 * \param[in] v a const reference to the color to be sent.
178 */
179 inline void glupColor(const vec4& v) {
180 glupColor4dv(v.data());
181 }
182
183
184 /**
185 * \brief Sends 2d texture coordinates to OpenGL.
186 * \param[in] v a const reference to the texture coordinates to be sent.
187 */
188 inline void glupTexCoord(const vec2& v) {
189 glupTexCoord2dv(v.data());
190 }
191
192 /**
193 * \brief Sends 3d texture coordinates to OpenGL.
194 * \param[in] v a const reference to the texture coordinates to be sent.
195 */
196 inline void glupTexCoord(const vec3& v) {
197 glupTexCoord3dv(v.data());
198 }
199
200 /**
201 * \brief Sends 4d texture coordinates to OpenGL.
202 * \param[in] v a const reference to the texture coordinates to be sent.
203 */
204 inline void glupTexCoord(const vec4& v) {
205 glupTexCoord4dv(v.data());
206 }
207
208 /**
209 * \brief Applies a translation.
210 * \param[in] v the translation vector.
211 */
212 inline void glupTranslate(const vec3& v) {
213 glupTranslated(v.x, v.y, v.z);
214 }
215
216 /**
217 * \brief Maps texture coordinates from a specified interval to
218 * the unit interval.
219 * \details This changes the GLUP texture matrix. GLUP matrix mode
220 * is reset to GLUP_MODELVIEW_MATRIX on exit.
221 * \param[in] minval minimum value, to be mapped to 0
222 * \param[in] maxval maximum value, to be mapped to 1
223 * \param[in] mult multiplicator, applied after the mapping
224 */
225 void GEOGRAM_GFX_API glupMapTexCoords1d(
226 double minval, double maxval, index_t mult=1
227 );
228
229 /**
230 * \brief Multiplies the current GLUP matrix
231 * with another one.
232 * \param[in] m a const reference to the matrix.
233 * \note m is transposed before being sent to GLUP
234 * because Geogram uses the convention with column
235 * vectors and GLUP the convention with row vectors
236 * to represent the transformed points.
237 */
238 void GEOGRAM_GFX_API glupMultMatrix(const mat4& m);
239
240 /**
241 * \brief Replaces the current GLUP matrix
242 * with a user defined one.
243 * \param[in] m a const reference to the matrix.
244 * \note m is transposed before being sent to OpenGL
245 * because Geogram uses the convention with column
246 * vectors and GLUP the convention with row vectors
247 * to represent the transformed points.
248 */
249 void GEOGRAM_GFX_API glupLoadMatrix(const mat4& m);
250
251 /**
252 * \brief Gets the size (in bytes) of the OpenGL buffer
253 * bound to a specified target.
254 * \param[in] target buffer object target
255 * (GL_ARRAY_BUFFER, GL_INDEX_BUFFER ...)
256 * \return the size in bytes of the buffer object bound
257 * to \p target.
258 */
259 GLint64 GEOGRAM_GFX_API get_size_of_bound_buffer_object(GLenum target);
260
261 /**
262 * \brief Updates the content of an OpenGL buffer object,
263 * and resizes it if need be.
264 * \param[in,out] buffer_id OpenGL opaque id of the buffer object.
265 * 0 means uninitialized.
266 * may be changed on exit if the buffer needed to be created or
267 * destroyed.
268 * \param[in] target buffer object target
269 * (GL_ARRAY_BUFFER, GL_INDEX_BUFFER ...)
270 * \param[in] new_size of the buffer data, in bytes
271 * \param[in] data pointer to the data to be copied into the buffer,
272 * of length new_size
273 */
274 void GEOGRAM_GFX_API update_buffer_object(
275 GLuint& buffer_id, GLenum target, size_t new_size, const void* data
276 );
277
278 /**
279 * \brief Updates the content of an OpenGL buffer object in streaming
280 * mode.
281 * \details Streaming mode means that there will be many updates of
282 * the contents of the same buffer object. stream_buffer_object()
283 * does the same thing as update_buffer_object(), but may
284 * be faster than update_buffer_object() in this situation.
285 * \param[in,out] buffer_id OpenGL opaque id of the buffer object.
286 * 0 means uninitialized.
287 * may be changed on exit if the buffer needed to be created or
288 * destroyed.
289 * \param[in] target buffer object target
290 * (GL_ARRAY_BUFFER, GL_INDEX_BUFFER ...)
291 * \param[in] new_size of the buffer data, in bytes
292 * \param[in] data pointer to the data to be copied into the buffer,
293 * of length new_size
294 */
295 void GEOGRAM_GFX_API stream_buffer_object(
296 GLuint& buffer_id, GLenum target, size_t new_size, const void* data
297 );
298
299
300 /**
301 * \brief Updates the content of an OpenGL buffer object,
302 * and resizes it if need be, or tests whether it has the
303 * size it should have.
304 * \param[in,out] buffer_id OpenGL opaque id of the buffer object.
305 * 0 means uninitialized.
306 * may be changed on exit if the buffer needed to be created or
307 * destroyed.
308 * \param[in] target buffer object target
309 * (GL_ARRAY_BUFFER, GL_INDEX_BUFFER ...)
310 * \param[in] new_size of the buffer data, in bytes
311 * \param[in] data pointer to the data to be copied into the buffer,
312 * of length new_size
313 * \param[in] update
314 * - if true, the buffer will be updated, and resized if need be.
315 * - if false, the size of the buffer will be tested, and an error
316 * message will be displayed in the logger if it does not match
317 * the specified size (and update will be forced).
318 */
319 void GEOGRAM_GFX_API update_or_check_buffer_object(
320 GLuint& buffer_id, GLenum target, size_t new_size, const void* data,
321 bool update
322 );
323
324 /**
325 * \brief Tests for OpenGL errors and displays a message if
326 * OpenGL errors were encountered.
327 * \param[in] file current sourcefile, as given by __FILE__
328 * \param[in] line current line, as given by __LINE__
329 * \param[in] warning_only if true, then errors are reported as warnings.
330 */
331 void GEOGRAM_GFX_API check_gl(
332 const char* file, int line, bool warning_only=false
333 );
334
335 /**
336 * \brief Clears all error flags set by previous OpenGL calls.
337 * \details This function shoud be called to ensure that subsequent
338 * calls to check_gl() will not report any error. This is necessary
339 * to workaround some buggy or incomplete implementations of OpenGL.
340 * In debug mode, error are always reported.
341 * \param[in] file current sourcefile, as given by __FILE__
342 * \param[in] line current line, as given by __LINE__
343 */
344 void GEOGRAM_GFX_API clear_gl_error_flags(const char* file, int line);
345
346 /**
347 * \brief Constants for draw_unit_textured_quad()
348 */
349 enum TexturedQuadMode { TEX_QUAD_RGBA, TEX_QUAD_RRR1, TEX_QUAD_DEPTH };
350
351 /**
352 * \brief Draws a textured quad.
353 * \param[in] mode of of
354 * - TEX_QUAD_RGBA copy input texture to color
355 * - TEX_QUAD_RRR1 copy red channel to r,g,b and set alpha to 1
356 * - TEX_QUAD_DEPTH copy red channel to depth.
357 * \details The textured quad spans the [-1,1]x[-1,1] square with
358 * texture coordinates in [0,1]x[0,1]. If no program is currently
359 * bound, then a default one is used, and it uses the texture bound
360 * to unit 0 of GL_TEXTURE_2D. If a program is bound, then it is used.
361 * Vertices coordinates are sent to vertex attribute 0 and texture
362 * coordinates to vertex attribute 1. To use TEX_QUAD_DEPTH, one needs to
363 * enable GL_DEPTH_TEST.
364 */
365 void GEOGRAM_GFX_API draw_unit_textured_quad(
366 TexturedQuadMode mode = TEX_QUAD_RGBA
367 );
368
369 /**
370 * \brief Tests for OpenGL errors.
371 * \details If an OpenGL error was flagged, display it together
372 * with current file and line number.
373 */
374 #ifdef GEO_DEBUG_GL
375 # define GEO_CHECK_GL() ::GEO::check_gl(__FILE__,__LINE__)
376 #else
377 # define GEO_CHECK_GL()
378 #endif
379
380 /***********************************************************/
381
382 void GEOGRAM_GFX_API glTexImage2Dxpm(char const* const* xpm_data);
383
384 }
385
386 #endif
387