Graphite Version 3
An experimental 3D geometry processing program
Loading...
Searching...
No Matches
GLUP_context.h
Go to the documentation of this file.
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_GLUP_GLUP_CONTEXT
41#define GEOGRAM_GFX_GLUP_GLUP_CONTEXT
42
47#include <map>
48
53
54#ifdef GEO_GL_NO_DOUBLES
55typedef double GLdouble;
56#endif
57
67static constexpr GLUPprimitive GLUP_THICK_LINES = GLUP_RESERVED_PRIMITIVE_1;
68
69namespace GLUP {
70 using namespace GEO;
71
80 GLboolean invert_matrix(GLfloat inv[16], const GLfloat m[16]);
81
90 GLboolean invert_matrix(GLdouble inv[16], const GLdouble m[16]);
91
98 GLfloat out[16], const GLfloat m1[16], const GLfloat m2[16]
99 );
100
107 GLdouble out[16], const GLdouble m1[16], const GLdouble m2[16]
108 );
109
119 GLfloat out[4], const GLfloat m[16], const GLfloat v[4]
120 );
121
122
133 GLfloat out[4], const GLfloat m[16], const GLfloat v[4]
134 );
135
136
147 GLdouble out[4], const GLdouble m[16], const GLdouble v[4]
148 );
149
157 GLdouble out[4], const GLdouble m[16], const GLdouble v[4]
158 );
159
165 void transpose_matrix(GLfloat m[16]);
166
172 void transpose_matrix(GLdouble m[16]);
173
178 void show_matrix(const GLfloat m[16]);
179
184 void show_matrix(const GLdouble m[16]);
185
190 void show_vector(const GLfloat v[4]);
191
196 void show_vector(const GLdouble v[4]);
197
202 void load_identity_matrix(GLfloat out[16]);
203
208 void load_identity_matrix(GLdouble out[16]);
209
216 inline void copy_vector(GLfloat* to, const GLfloat* from, index_t dim) {
217 Memory::copy(to, from, sizeof(GLfloat)*dim);
218 }
219
226 inline void copy_vector(GLdouble* to, const GLdouble* from, index_t dim) {
227 Memory::copy(to, from, sizeof(GLdouble)*dim);
228 }
229
236 inline void copy_vector(GLfloat* to, const GLdouble* from, index_t dim) {
237 for(index_t i=0; i<dim; ++i) {
238 to[i] = GLfloat(from[i]);
239 }
240 }
241
248 inline void copy_vector(GLdouble* to, const GLfloat* from, index_t dim) {
249 for(index_t i=0; i<dim; ++i) {
250 to[i] = GLdouble(from[i]);
251 }
252 }
253
259 inline void normalize_vector(GLfloat v[3]) {
260 GLfloat s = 1.0f / ::sqrtf(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
261 v[0] *= s;
262 v[1] *= s;
263 v[2] *= s;
264 }
265
266 /**********************************************************************/
267
268 class Context;
269
277 public:
278
282 static const int MAX_DEPTH=16;
283
287 MatrixStack() : top_(0) {
289 }
290
297 GLdouble* top() {
298 return stack_[top_].data();
299 }
300
304 void push() {
305 geo_assert(top_ != MAX_DEPTH-1);
306 GLdouble* from = top();
307 ++top_;
308 GLdouble* to = top();
309 copy_vector(to, from, 16);
310 }
311
316 void pop() {
317 geo_assert(top_ != 0);
318 --top_;
319 }
320
321 protected:
322 struct Matrix {
323 GLdouble coeff[16];
324 GLdouble* data() {
325 return &coeff[0];
326 }
327 };
328
329 private:
330 Matrix stack_[MAX_DEPTH];
331 index_t top_;
332 };
333
334
341 static const index_t IMMEDIATE_BUFFER_SIZE = 65536;
342
348 GLUP_VERTEX_ATTRIBUTE = 0,
349 GLUP_COLOR_ATTRIBUTE = 1,
350 GLUP_TEX_COORD_ATTRIBUTE = 2,
351 GLUP_NORMAL_ATTRIBUTE = 3,
352 GLUP_VERTEX_ID_ATTRIBUTE = 4
353 };
354
359
360 public:
361
366 data_(nullptr),
367 dimension_(0),
368 is_enabled_(false),
369 VBO_(0) {
370 }
371
376 delete[] data_;
377 if(VBO_ != 0) {
378 glDeleteBuffers(1, &VBO_);
379 VBO_ = 0;
380 }
381 }
382
383 void initialize(index_t dim) {
384 data_ = new GLfloat[dim * IMMEDIATE_BUFFER_SIZE];
385 dimension_ = dim;
386 is_enabled_ = true;
387 }
388
392 void enable() {
393 is_enabled_ = true;
394 }
395
399 void disable() {
400 is_enabled_ = false;
401 }
402
408 bool is_enabled() const {
409 return is_enabled_;
410 }
411
418 void set_current(GLfloat x, GLfloat y, GLfloat z, GLfloat w) {
419 current_[0] = x;
420 current_[1] = y;
421 current_[2] = z;
422 current_[3] = w;
423 }
424
432 geo_debug_assert(v < IMMEDIATE_BUFFER_SIZE);
433 if(is_enabled()) {
434 copy_vector(element_ptr(v), current_, dimension());
435 }
436 }
437
444 void copy(index_t to, index_t from) {
445 geo_debug_assert(to < IMMEDIATE_BUFFER_SIZE);
446 geo_debug_assert(from < IMMEDIATE_BUFFER_SIZE);
447 if(is_enabled()) {
449 }
450 }
451
459 void copy(index_t to, ImmediateBuffer& from_buffer, index_t from) {
460 geo_debug_assert(to < IMMEDIATE_BUFFER_SIZE);
461 geo_debug_assert(from < IMMEDIATE_BUFFER_SIZE);
462 geo_debug_assert(from_buffer.dimension() == dimension());
464 element_ptr(to), from_buffer.element_ptr(from), dimension()
465 );
466 }
467
473 return dimension_;
474 }
475
480 size_t size_in_bytes() const {
481 return IMMEDIATE_BUFFER_SIZE * dimension() * sizeof(GLfloat);
482 }
483
490 GLfloat* element_ptr(index_t v) {
491 geo_debug_assert(v < IMMEDIATE_BUFFER_SIZE);
492 return data_ + v*dimension_;
493 }
494
500 GLfloat* data() {
501 return data_;
502 }
503
509 GLuint& VBO() {
510 return VBO_;
511 }
512
520 const ImmediateBuffer& rhs
521 ) {
522 data_ = rhs.data_;
523 dimension_ = rhs.dimension_;
524 is_enabled_ = rhs.is_enabled_;
525 VBO_ = rhs.VBO_;
526 current_[0] = rhs.current_[0];
527 current_[1] = rhs.current_[1];
528 current_[2] = rhs.current_[2];
529 current_[3] = rhs.current_[3];
530 geo_assert(data_ == nullptr);
531 }
532
533 private:
534 GLfloat* data_;
535 GLfloat current_[4];
536 index_t dimension_;
537 bool is_enabled_;
538 GLuint VBO_;
539 };
540
546 public:
553 ImmediateState(index_t* nb_vertices_per_primitive) :
554 current_vertex_(0),
555 max_current_vertex_(0),
556 primitive_(GLUP_POINTS),
557 VAO_(0),
558 nb_vertices_per_primitive_(nb_vertices_per_primitive)
559 {
560 buffer[GLUP_VERTEX_ATTRIBUTE].initialize(4);
561 buffer[GLUP_COLOR_ATTRIBUTE].initialize(4);
562 buffer[GLUP_TEX_COORD_ATTRIBUTE].initialize(4);
563 buffer[GLUP_NORMAL_ATTRIBUTE].initialize(4);
564
565 // Vertex is always enabled
566 buffer[GLUP_VERTEX_ATTRIBUTE].enable();
567 }
568
573 if(VAO_ != 0) {
574 glupDeleteVertexArrays(1, &VAO_);
575 VAO_ = 0;
576 }
577 }
578
584 GLuint& VAO() {
585 return VAO_;
586 }
587
595 void copy_element(index_t to, index_t from) {
596 for(index_t i=0; i<NB_IMMEDIATE_BUFFERS; ++i) {
597 buffer[i].copy(to, from);
598 }
599 }
600
601
613 current_vertex_ = 0;
614 if(max_current_vertex != 0) {
615 max_current_vertex_ = max_current_vertex;
616 } else {
617 max_current_vertex_ =
618 IMMEDIATE_BUFFER_SIZE - (
619 IMMEDIATE_BUFFER_SIZE %
620 nb_vertices_per_primitive_[primitive]
621 );
622 }
623 primitive_ = primitive;
624 }
625
631 void next_vertex() {
632 for(index_t i=0; i<NB_IMMEDIATE_BUFFERS; ++i) {
633 buffer[i].copy_current_to(current_vertex_);
634 }
635 ++current_vertex_;
636 }
637
645 return (current_vertex_ == max_current_vertex_);
646 }
647
653 void reset(index_t new_current_vertex = 0) {
654 current_vertex_ = new_current_vertex;
655 }
656
662 return primitive_;
663 }
664
669 return current_vertex_;
670 }
671
676 return current_vertex_ / nb_vertices_per_primitive_[
677 primitive_
678 ];
679 }
680
688 return max_current_vertex_;
689 }
690
698 geo_debug_assert(v <= max_current_vertex_);
699 current_vertex_ = v;
700 }
701
702 enum { NB_IMMEDIATE_BUFFERS = 4 };
703 ImmediateBuffer buffer[NB_IMMEDIATE_BUFFERS];
704
705 private:
706 index_t current_vertex_;
707 index_t max_current_vertex_;
708 GLUPprimitive primitive_;
709 GLuint VAO_;
710 index_t* nb_vertices_per_primitive_;
711 };
712
713
714 /**********************************************************/
715
720 public:
721
725 StateVariableBase() : address_(nullptr), context_(nullptr) {
726 }
727
735 Context* context, const char* name
736 ) {
737 initialize(context,name);
738 }
739
747 void initialize(Context* context, const char* name);
748
754 const std::string& name() const {
755 return name_;
756 }
757
758 protected:
759 friend class Context;
760
767 return address_;
768 }
769
775
776 Memory::pointer address_;
777 Context* context_;
778 std::string name_;
779 };
780
785 template <class T> class StateVariable : public StateVariableBase {
786 public:
787
792 }
793
802 Context* context, const char* name, T value
803 ) : StateVariableBase(context, name) {
804 set(value);
805 }
806
814 void initialize(Context* context, const char* name, T value) {
816 set(value);
817 }
818
823 T get() const {
824 return *reinterpret_cast<T*>(address_);
825 }
826
832 void set(T val) {
833 *reinterpret_cast<T*>(address_) = val;
835 }
836 };
837
844 public:
845
851
859 Context* context, const char* name
860 ) : StateVariableBase(context, name) {
861 }
862
868 const GLUPfloat* get_pointer() const {
869 return reinterpret_cast<const GLUPfloat*>(address_);
870 }
871
879 GLUPfloat* get_pointer() {
880 // This is a non-const pointer, therefore it will be
881 // probably modified by client code (else the 'const'
882 // version of get_pointer() would have been called).
884 return reinterpret_cast<GLUPfloat*>(address_);
885 }
886 };
887
893 public:
894
898 VectorStateVariable() : dimension_(0) {
899 }
900
909 Context* context, const char* name, index_t dimension
910 ) : FloatsArrayStateVariable(context, name), dimension_(dimension) {
911 clear();
912 }
913
921 void initialize(Context* context, const char* name, index_t dimension) {
923 dimension_ = dimension;
924 clear();
925 }
926
932 return dimension_;
933 }
934
940 void get(GLUPfloat* x) const {
941 Memory::copy(x, address_, sizeof(GLUPfloat)*dimension_);
942 }
943
949 void set(const GLUPfloat* x) {
950 Memory::copy(address_, x, sizeof(GLUPfloat)*dimension_);
952 }
953
960 void clear() {
961 Memory::clear(address_, sizeof(GLUPfloat)*dimension_);
962 if(dimension_ == 4) {
963 reinterpret_cast<GLUPfloat*>(address_)[3] = 1.0f;
964 }
966 }
967
968 protected:
969 index_t dimension_;
970 };
971
972
979 VectorStateVariable light_vector;
980 VectorStateVariable light_half_vector;
981 StateVariable<GLfloat> point_size;
982 StateVariable<GLfloat> mesh_width;
983 StateVariable<GLfloat> cells_shrink;
984 StateVariable<GLint> picking_mode;
985 StateVariable<GLint> picking_id;
986 StateVariable<GLint> base_picking_id;
987 StateVariable<GLint> clipping_mode;
988 StateVariable<GLint> texture_mode;
989 StateVariable<GLint> texture_type;
990 StateVariable<GLfloat> alpha_threshold;
991 StateVariable<GLfloat> specular;
992 VectorStateVariable clip_plane;
993 VectorStateVariable world_clip_plane;
994 VectorStateVariable clip_clip_plane;
995 FloatsArrayStateVariable modelview_matrix;
996 FloatsArrayStateVariable modelviewprojection_matrix;
997 FloatsArrayStateVariable projection_matrix;
998 FloatsArrayStateVariable normal_matrix;
999 FloatsArrayStateVariable texture_matrix;
1000 FloatsArrayStateVariable inverse_modelviewprojection_matrix;
1001 FloatsArrayStateVariable inverse_modelview_matrix;
1002 FloatsArrayStateVariable inverse_projection_matrix;
1003 VectorStateVariable viewport;
1004 };
1005
1006 /**********************************************************************/
1007
1013
1014 typedef Numeric::uint64 ShaderKey;
1015
1020 GL_primitive(0),
1021 VAO(0),
1022 elements_VBO(0),
1023 nb_elements_per_primitive(0),
1024 primitive_elements(nullptr),
1025 vertex_gather_mode(false),
1026 implemented(false) {
1027 }
1028
1035 PrimitiveInfo(const PrimitiveInfo& rhs) : shader_map(rhs.shader_map) {
1036 GL_primitive = rhs.GL_primitive;
1037 VAO = rhs.VAO;
1038 elements_VBO = rhs.elements_VBO;
1039 nb_elements_per_primitive = rhs.nb_elements_per_primitive;
1040 primitive_elements = rhs.primitive_elements;
1041 vertex_gather_mode = rhs.vertex_gather_mode;
1042 implemented = rhs.implemented;
1043 geo_assert(GL_primitive == 0);
1044 geo_assert(nb_elements_per_primitive == 0);
1045 }
1046
1052 for(auto& it : shader_map) {
1053 if(it.second != 0) {
1054 glDeleteProgram(it.second);
1055 it.second = 0;
1056 }
1057 }
1058 if(elements_VBO != 0) {
1059 glDeleteBuffers(1, &elements_VBO);
1060 }
1061 if(VAO != 0) {
1062 glupDeleteVertexArrays(1,&VAO);
1063 VAO = 0;
1064 }
1065 }
1066
1067 bool program_is_initialized(ShaderKey k) const {
1068 return (shader_map.find(k) != shader_map.end());
1069 }
1070
1071 GLuint program(ShaderKey k) const {
1072 auto it = shader_map.find(k);
1073 return ((it == shader_map.end()) ? 0 : it->second);
1074 }
1075
1076 GLenum GL_primitive;
1077 std::map<ShaderKey, GLuint> shader_map;
1078 GLuint VAO;
1079 GLuint elements_VBO;
1080 index_t nb_elements_per_primitive;
1081 index_t* primitive_elements;
1082 bool vertex_gather_mode;
1083 bool implemented;
1084 };
1085
1086 /**********************************************************************/
1087
1094 public:
1103 static const char* uniform_state_declaration();
1104
1109
1113 ~Context() override;
1114
1115
1119 virtual const char* profile_name() const = 0;
1120
1124 virtual const char* GLSL_version_directive() const = 0;
1125
1135
1141 virtual void setup();
1142
1150 virtual void bind_uniform_state(GLuint program);
1151
1158 void load_matrix(const GLfloat m[16]) {
1159 copy_vector(matrix_stack_[matrix_mode_].top(), m, 16);
1161 }
1162
1169 void load_matrix(const GLdouble m[16]) {
1170 copy_vector(matrix_stack_[matrix_mode_].top(), m, 16);
1172 }
1173
1179 load_identity_matrix(matrix_stack_[matrix_mode_].top());
1181 }
1182
1190 void mult_matrix(const GLdouble m[16]) {
1191 GLdouble product[16];
1192 mult_matrices(product,m,matrix_stack_[matrix_mode_].top());
1193 load_matrix(product);
1194 }
1195
1202 matrix_stack_[matrix_mode_].push();
1203 }
1204
1208 void pop_matrix() {
1209 matrix_stack_[matrix_mode_].pop();
1211 }
1212
1219 void set_matrix_mode(GLUPmatrix matrix) {
1220 matrix_mode_ = matrix;
1221 }
1222
1228 GLUPmatrix get_matrix_mode() const {
1229 return matrix_mode_;
1230 }
1231
1241 GLfloat x, GLfloat y, GLfloat z=0.0f, GLfloat w=1.0f
1242 ) {
1243 immediate_state_.buffer[GLUP_VERTEX_ATTRIBUTE].set_current(x,y,z,w);
1244 immediate_state_.next_vertex();
1245 if(immediate_state_.buffers_are_full()) {
1247 }
1248 }
1249
1256 GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0f
1257 ) {
1258 immediate_state_.buffer[GLUP_COLOR_ATTRIBUTE].set_current(r,g,b,a);
1259 }
1260
1267 GLfloat s, GLfloat t=0.0f, GLfloat u=0.0f, GLfloat v=1.0f
1268 ) {
1269 immediate_state_.buffer[GLUP_TEX_COORD_ATTRIBUTE].set_current(
1270 s,t,u,v
1271 );
1272 }
1273
1280 void immediate_normal(GLfloat x, GLfloat y, GLfloat z, GLfloat w = 0.0) {
1281 immediate_state_.buffer[GLUP_NORMAL_ATTRIBUTE].set_current(
1282 x,y,z,w
1283 );
1284 }
1285
1290 void set_user_program(GLuint program) {
1291 user_program_ = program;
1292 }
1293
1299 GLuint user_program() const {
1300 return user_program_;
1301 }
1302
1308 virtual void begin(GLUPprimitive primitive);
1309
1314 virtual void end();
1315
1327 virtual void draw_arrays(
1328 GLUPprimitive primitive, GLUPint first, GLUPsizei count
1329 );
1330
1344 virtual void draw_elements(
1345 GLUPprimitive primitive, GLUPsizei count,
1346 GLUPenum type, const GLUPvoid* indices
1347 );
1348
1358
1364 return uniform_state_;
1365 }
1366
1372 return uniform_state_;
1373 }
1374
1381 uniform_buffer_dirty_ = true;
1382 }
1383
1384
1390 uniform_buffer_dirty_ = true;
1391 lighting_dirty_ = true;
1392 }
1393
1399 uniform_buffer_dirty_ = true;
1400 matrices_dirty_ = true;
1401 }
1402
1409 GLUPdouble* get_matrix(GLUPmatrix matrix) {
1410 geo_debug_assert(matrix < 3);
1411 return matrix_stack_[matrix].top();
1412 }
1413
1421 std::vector<GLSL::Source>& sources
1422 );
1423
1431 std::vector<GLSL::Source>& sources
1432 );
1433
1441 std::vector<GLSL::Source>& sources
1442 );
1443
1451 std::vector<GLSL::Source>& sources
1452 );
1453
1461 std::vector<GLSL::Source>& sources
1462 );
1463
1464
1475 std::vector<GLSL::Source>& sources
1476 );
1477
1487 std::vector<GLSL::Source>& sources
1488 );
1489
1499 std::vector<GLSL::Source>& sources
1500 );
1501
1512 GLUPbitfield toggles_state,
1513 GLUPbitfield toggles_undetermined=0
1514 );
1515
1524 GLUPprimitive primitive
1525 );
1526
1532 return immediate_state_;
1533 }
1534
1539
1540
1546 static const char* glup_primitive_name(GLUPprimitive prim);
1547
1553 if(uniform_buffer_dirty_) {
1555 }
1556 }
1557
1558 protected:
1559
1568
1578 bool extension_is_supported(const std::string& extension);
1579
1590 virtual void prepare_to_draw(GLUPprimitive primitive);
1591
1592
1600 virtual void done_draw(GLUPprimitive primitive);
1601
1606
1607
1613
1619
1623 virtual void setup_primitives();
1624
1628 virtual void setup_GLUP_POINTS();
1629
1633 virtual void setup_GLUP_LINES();
1634
1639
1643 virtual void setup_GLUP_TRIANGLES();
1644
1648 virtual void setup_GLUP_QUADS();
1649
1654
1658 virtual void setup_GLUP_HEXAHEDRA();
1659
1663 virtual void setup_GLUP_PRISMS();
1664
1668 virtual void setup_GLUP_PYRAMIDS();
1669
1674
1678 virtual void setup_GLUP_SPHERES();
1679
1690 GLUPprimitive glup_primitive, GLenum gl_primitive, GLuint program,
1691 bool bind_attrib_loc_and_link = true
1692 );
1693
1709 GLUPprimitive glup_primitive, GLenum gl_primitive, GLuint program
1710 );
1711
1733 GLUPprimitive glup_primitive, GLenum gl_primitive, GLuint program,
1734 index_t nb_elements_per_glup_primitive,
1735 index_t* element_indices
1736 );
1737
1738
1745
1750 virtual void update_matrices();
1751
1757 virtual void update_lighting();
1758
1763 virtual void update_base_picking_id(GLint new_value);
1764
1770 std::string primitive_declaration(GLUPprimitive prim) const;
1771
1780 PrimitiveInfo::ShaderKey toggles_config
1781 ) {
1782 if(toggles_config == (1 << GLUP_PICKING)) {
1784 (1 << GLUP_PICKING), // picking=true
1785 (1 << GLUP_CLIPPING) // clipping=undecided (use state)
1786 );
1787 } else {
1788 setup_shaders_source_for_toggles(GLUPbitfield(toggles_config));
1789 }
1790 }
1791
1797
1805
1817
1825
1831
1839
1851
1852
1863 index_t result = 0;
1864 for(index_t lv=0; lv<nb_v; ++lv) {
1865 if(v_is_visible_[first_v+lv]) {
1866 result = result | (1u << lv);
1867 }
1868 }
1869 return result;
1870 }
1871
1883 const GLUPfloat* eqn = world_clip_plane_;
1884 const GLUPfloat* p1 = immediate_state_.buffer[0].element_ptr(v1);
1885 const GLUPfloat* p2 = immediate_state_.buffer[0].element_ptr(v2);
1886
1887 GLUPfloat t = -eqn[3] -(
1888 eqn[0]*p1[0] +
1889 eqn[1]*p1[1] +
1890 eqn[2]*p1[2]
1891 );
1892
1893 GLUPfloat d =
1894 eqn[0]*(p2[0]-p1[0]) +
1895 eqn[1]*(p2[1]-p1[1]) +
1896 eqn[2]*(p2[2]-p1[2]) ;
1897
1898 if(fabs(double(d)) < 1e-6) {
1899 t = 0.5f;
1900 } else {
1901 t /= d;
1902 }
1903
1904 GLUPfloat s = 1.0f - t;
1905
1906 isect_vertex_attribute_[0][4*vi+0] = s*p1[0] + t*p2[0];
1907 isect_vertex_attribute_[0][4*vi+1] = s*p1[1] + t*p2[1];
1908 isect_vertex_attribute_[0][4*vi+2] = s*p1[2] + t*p2[2];
1909 isect_vertex_attribute_[0][4*vi+3] = 1.0f;
1910
1911 for(index_t i=1; i<3; ++i) {
1912 if(immediate_state_.buffer[i].is_enabled()) {
1913 const GLUPfloat* a1 =
1914 immediate_state_.buffer[i].element_ptr(v1);
1915 const GLUPfloat* a2 =
1916 immediate_state_.buffer[i].element_ptr(v2);
1917 isect_vertex_attribute_[i][4*vi+0] = s*a1[0] + t*a2[0];
1918 isect_vertex_attribute_[i][4*vi+1] = s*a1[1] + t*a2[1];
1919 isect_vertex_attribute_[i][4*vi+2] = s*a1[2] + t*a2[2];
1920 isect_vertex_attribute_[i][4*vi+3] = s*a1[3] + t*a2[3];
1921 }
1922 }
1923 }
1924
1931
1940 void use_program(GLuint program) {
1941 if(program != 0 && program != latest_program_) {
1942 glUseProgram(program);
1943 latest_program_ = program;
1945 } else {
1946 glUseProgram(program);
1947 }
1948 }
1949
1957
1958 static void initialize();
1959
1960 protected:
1961
1962 // OpenGL Uniform state.
1963 GLuint default_program_;
1964 GLuint uniform_buffer_;
1965 GLuint uniform_binding_point_;
1966 GLint uniform_buffer_size_;
1967 bool uniform_buffer_dirty_;
1968
1969 // C++ Uniform state.
1970 Memory::byte* uniform_buffer_data_;
1971 UniformState uniform_state_;
1972
1973 bool lighting_dirty_;
1974
1975 // Matrix stacks.
1976 GLUPmatrix matrix_mode_;
1977 MatrixStack matrix_stack_[3];
1978 bool matrices_dirty_;
1979
1989
1990 // Immediate mode buffers.
1991 ImmediateState immediate_state_;
1992
1993 // Primitive informations (i.e., how to
1994 // draw a primitive of a given type).
1995 vector<PrimitiveInfo> primitive_info_;
1996
1997 // The marching cells, for computing
1998 // intersections when clipping mode
1999 // is GLUP_CLIP_SLICE_CELLS
2000 MarchingCell marching_tet_;
2001 MarchingCell marching_hex_;
2002 MarchingCell marching_prism_;
2003 MarchingCell marching_pyramid_;
2004 MarchingCell marching_connector_;
2005
2006 GLuint user_program_;
2007
2008 PrimitiveInfo::ShaderKey toggles_config_;
2009
2010 GLUPprimitive primitive_source_;
2011 GLUPbitfield toggles_source_state_;
2012 GLUPbitfield toggles_source_undetermined_;
2013
2014 bool precompile_shaders_;
2015
2016 bool use_core_profile_;
2017 bool use_ES_profile_;
2018
2025
2031 std::map<std::string, GLsizei> variable_to_offset_;
2032
2038 bool v_is_visible_[IMMEDIATE_BUFFER_SIZE];
2039
2045 GLUPfloat isect_vertex_attribute_[3][12*4];
2046
2053
2059 };
2060
2061 /*********************************************************************/
2062}
2063
2064#endif
Utilities for manipulating GLSL shaders.
GLUP: GL Useful Primitives.
void GLUP_API glupDeleteVertexArrays(GLUPsizei n, const GLUPuint *arrays)
Deletes vertex array objects.
GLUPprimitive
Symbolic values corresponding to GLUP primitive types.
Definition GLUP.h:477
void mult_matrix_vector(GLfloat out[4], const GLfloat m[16], const GLfloat v[4])
Computes the product of a 4x4 matrix and a vector.
void load_identity_matrix(GLfloat out[16])
Resets a matrix to the identity matrix.
GLUPattribute
Index of an ImmediateBuffer in the ImmediateState.
GLboolean invert_matrix(GLfloat inv[16], const GLfloat m[16])
Computes the inverse of a 4x4 matrix.
void normalize_vector(GLfloat v[3])
Normalizes a vector.
void mult_transpose_matrix_vector(GLfloat out[4], const GLfloat m[16], const GLfloat v[4])
Computes the product of the transpose of a 4x4 matrix and a vector.
void transpose_matrix(GLfloat m[16])
Transposes a matrix in-place.
void copy_vector(GLfloat *to, const GLfloat *from, index_t dim)
Copies a vector of floats.
void show_matrix(const GLfloat m[16])
For debugging, outputs a matrix to the standard error.
void mult_matrices(GLfloat out[16], const GLfloat m1[16], const GLfloat m2[16])
Computes the product of two 4x4 matrices.
void show_vector(const GLfloat v[4])
For debugging, outputs a vector to the standard error.
Implementation of the marching cells algorithms.
#define geo_assert(x)
Verifies that a condition is met.
Definition assert.h:155
#define geo_debug_assert(x)
Verifies that a condition is met.
Definition assert.h:202
A class that can register functions to the GLSL pseudo file system.
Definition GLSL.h:192
Vector with aligned memory allocation.
Definition memory.h:703
GLUP context stores a Uniform Buffer Object with state variables similar to OpenGL's fixed functional...
const MarchingCell & get_marching_cell() const
Gets the MarchingCell that corresponds to the current primitive.
Context()
Context constructor.
void setup_shaders_source_for_toggles_config(PrimitiveInfo::ShaderKey toggles_config)
Sets the string that describes the settings of the toggles for a given configuration.
virtual void get_marching_cells_pseudo_file(std::vector< GLSL::Source > &sources)
Gets the content of the virtual file GLUP/current_profile/marching_cells.h.
virtual void update_matrices()
Updates the matrices in the uniform state from the matrices in the stacks.
virtual void done_draw(GLUPprimitive primitive)
This function is called right after rendering primitives. It is called by end(), draw_arrays() and dr...
virtual void set_primitive_info(GLUPprimitive glup_primitive, GLenum gl_primitive, GLuint program, bool bind_attrib_loc_and_link=true)
Initializes the PrimitiveInfo associated with a given GLUP primitive.
virtual void get_geometry_shader_preamble_pseudo_file(std::vector< GLSL::Source > &sources)
Gets the content of the virtual file GLUP/current_profile/geometry_shader_preamble....
void set_user_program(GLuint program)
Sets the user program, to be used instead of the default GLUP programs for drawing the primitives.
void immediate_color(GLfloat r, GLfloat g, GLfloat b, GLfloat a=1.0f)
Specifies the current color for the immediate mode buffers.
void immediate_normal(GLfloat x, GLfloat y, GLfloat z, GLfloat w=0.0)
Specifies the current normal vector for the immediate mode buffers.
void mult_matrix(const GLdouble m[16])
Post-multiplies the top of the current matrix stack with the specified matrix.
static const char * uniform_state_declaration()
Gets the GLSL declaration of GLUP uniform state.
GLUPdouble * get_matrix(GLUPmatrix matrix)
Gets a pointer to the values of the matrix at the top of a given stack.
UniformState & uniform_state()
Gets the uniform state.
virtual Memory::pointer get_state_variable_address(const char *name)
Gets a pointer to the representation of a uniform state variable in host memory from its (unqualified...
virtual void bind_uniform_state(GLuint program)
Binds GLUP uniform state to a program.
virtual void setup_primitives()
Setups the programs and VAOs used for each primitive.
virtual void setup_GLUP_THICK_LINES()
Setups GLSL programs for lines with width > 1.
~Context() override
Context destructor.
virtual void setup_shaders_source_for_primitive(GLUPprimitive primitive)
Sets the configurable GLSL sources for a given primitive type.
void load_matrix(const GLdouble m[16])
Replaces the top of the current matrix stack with the specified matrix.
void setup_shaders_source_for_toggles(GLUPbitfield toggles_state, GLUPbitfield toggles_undetermined=0)
Sets the string that describes the settings of the toggles for a given configuration.
index_t nb_vertices_per_primitive_[GLUP_NB_PRIMITIVES]
Number of vertices per primitive (3 for GLUP_TRIANGLES, 4 for GLUP_QUADS etc...).
virtual void setup_GLUP_PRISMS()
Setups GLSL programs for prisms.
GLUPfloat * world_clip_plane_
Cached pointer to uniform state variable.
std::string primitive_declaration(GLUPprimitive prim) const
Gets the GLSL declaration of the constant that indicates the current primitive.
void pop_matrix()
Pops the top of the current stack matrix.
void immediate_vertex(GLfloat x, GLfloat y, GLfloat z=0.0f, GLfloat w=1.0f)
Creates a new vertex in the immediate mode buffers.
void create_vertex_id_VBO()
Creates a vertex buffer object with 16 bits integers between 0 and 65535.
virtual void update_lighting()
Updates the lighting in the uniform state.
index_t get_config(index_t first_v, index_t nb_v)
Assemble the configuration code of a primitive relative to the clipping plane.
virtual void setup_GLUP_TETRAHEDRA()
Setups GLSL programs for tetrahedra.
void compute_intersection(index_t v1, index_t v2, index_t vi)
Computes the intersection between the clipping plane and a segment.
void shrink_cells_in_immediate_buffers()
Shrinks the cells in the immediate buffer.
virtual void setup_GLUP_LINES()
Setups GLSL programs for lines.
virtual void setup_state_variables()
Initializes the representation of the uniform state.
std::map< std::string, GLsizei > variable_to_offset_
Used by GPU-side uniform buffer.
virtual void prepare_to_draw(GLUPprimitive primitive)
This function is called before starting to render primitives. It is called by begin(),...
void flag_uniform_buffer_as_dirty()
Indicates that the OpenGL representation of the uniform state is no longer in sync with the local cop...
virtual void begin(GLUPprimitive primitive)
Begins rendering in immediate mode.
GLUPmatrix get_matrix_mode() const
Gets the current matrix stack.
GLUPfloat isect_vertex_attribute_[3][12 *4]
computed intersections.
bool extension_is_supported(const std::string &extension)
Tests whether an OpenGL extension is supported.
void classify_vertices_in_immediate_buffers()
Updates v_is_visible_[] according to current clipping plane.
GLuint latest_program_
Latest used GLSL program.
virtual void flush_immediate_buffers()
Flushes the immediate mode buffers.
virtual void stream_immediate_buffers()
Sends all the active immediate buffers to the GPU.
virtual void draw_elements(GLUPprimitive primitive, GLUPsizei count, GLUPenum type, const GLUPvoid *indices)
Draws primitives using current OpenGL array bindings.
void use_program(GLuint program)
A wrapper around glUseProgram that tests whether uniform state needs to be sent to the program.
virtual void set_primitive_info_vertex_gather_mode(GLUPprimitive glup_primitive, GLenum gl_primitive, GLuint program)
Initializes the PrimitiveInfo associated with a given GLUP primitive in vertex-gather mode.
GLuint vertex_id_VBO_
A vertex buffer object with 65536 16 bits integers.
virtual void get_tess_control_shader_preamble_pseudo_file(std::vector< GLSL::Source > &sources)
Gets the content of the virtual file GLUP/current_profile/tess_control_shader_preamble....
virtual void set_primitive_info_immediate_index_mode(GLUPprimitive glup_primitive, GLenum gl_primitive, GLuint program, index_t nb_elements_per_glup_primitive, index_t *element_indices)
Initializes the PrimitiveInfo associated with a given GLUP primitive in immediate mode when an elemen...
virtual void setup_GLUP_PYRAMIDS()
Setups GLSL programs for pyramids.
virtual void setup_GLUP_SPHERES()
Setups GLSL programs for spheres.
virtual void get_primitive_pseudo_file(std::vector< GLSL::Source > &sources)
Gets the content of the virtual file GLUP/current_profile/primitive.h.
virtual void setup_GLUP_CONNECTORS()
Setups GLSL programs for connectors.
virtual void setup_GLUP_HEXAHEDRA()
Setups GLSL programs for hexahedra.
virtual void get_fragment_shader_preamble_pseudo_file(std::vector< GLSL::Source > &sources)
Gets the content of the virtual file GLUP/current_profile/fragment_shader_preamble....
bool cell_is_clipped(index_t first_v)
Tests whether the cell starting at a given vertex in the immediate buffer is clipped,...
virtual void do_update_uniform_buffer()
Copies GLUP uniform state to OpenGL.
void set_matrix_mode(GLUPmatrix matrix)
Sets the current matrix stack.
ImmediateState & immediate_state()
Gets the immediate state.
static const char * glup_primitive_name(GLUPprimitive prim)
Gets the name of a primitive by GLUPprimitive.
virtual void get_vertex_shader_preamble_pseudo_file(std::vector< GLSL::Source > &sources)
Gets the content of the virtual file GLUP/current_profile/vertex_shader_preamble.h.
void load_matrix(const GLfloat m[16])
Replaces the top of the current matrix stack with the specified matrix.
void update_toggles_config()
Updates the toggles_config_ state variable from the individual state of each toggle.
virtual const char * GLSL_version_directive() const =0
Gets the GLSL version directive (with version and newline).
virtual void setup_GLUP_TRIANGLES()
Setups GLSL programs for triangles.
virtual void get_tess_evaluation_shader_preamble_pseudo_file(std::vector< GLSL::Source > &sources)
Gets the content of the virtual file GLUP/current_profile/tess_evaluation_shader_preamble....
virtual void setup_GLUP_QUADS()
Setups GLSL programs for quads.
virtual bool primitive_supports_array_mode(GLUPprimitive prim) const
Tests whether a given GLUP primitive supports array mode.
virtual void setup()
Creates the uniform state and GLSL programs.
void immediate_tex_coord(GLfloat s, GLfloat t=0.0f, GLfloat u=0.0f, GLfloat v=1.0f)
Specifies the current texture coordinates for the immediate mode buffers.
void flag_matrices_as_dirty()
Indicates that cached matrix information needs to be recomputed.
virtual void setup_immediate_buffers()
Set-ups the buffers for immediate rendering.
GLuint user_program() const
Gets the current user program.
virtual void end()
Ends rendering in immediate mode.
void bind_immediate_state_buffers_to_VAO()
Binds the VBOs associated with the immediate state buffers to the currently bound VAO.
void load_identity()
Replaces the top of the current matrix stack with the identity matrix.
virtual const char * profile_name() const =0
Gets the profile name associated with this context.
void push_matrix()
Pushes a copy of the top of the current stack matrix onto the current stack matrix.
virtual void update_base_picking_id(GLint new_value)
Updates the base picking id and sends it to OpenGL.
void update_uniform_buffer()
Copies GLUP uniform state to OpenGL if required.
void create_CPU_side_uniform_buffer()
Creates a buffer for uniform variables for implementations that do not support uniform buffer objects...
virtual void get_toggles_pseudo_file(std::vector< GLSL::Source > &sources)
Gets the content of the virtual file GLUP/current_profile/toggles.h.
const UniformState & uniform_state() const
Gets the uniform state.
void create_program_if_needed(GLUPprimitive primitive)
Creates the GLSL shader that corresponds to the specified primitive and current toggles configuration...
virtual void copy_uniform_state_to_current_program()
Copies the uniform state from client-side memory into the currently bound program,...
virtual void setup_GLUP_POINTS()
Setups GLSL programs for points.
void flag_lighting_as_dirty()
Indicates that cached lighting information needs to be recomputed.
virtual void draw_arrays(GLUPprimitive primitive, GLUPint first, GLUPsizei count)
Draws primitives using current OpenGL array bindings.
bool v_is_visible_[IMMEDIATE_BUFFER_SIZE]
Indicates for a given vertex whether it is clipped or is visible, according to the current clipping p...
A GLUP state variable that contains an array of floating points. This concerns both vectors and matri...
const GLUPfloat * get_pointer() const
Gets a pointer to the variable.
FloatsArrayStateVariable()
FloatsArrayStateVariable default constructor.
FloatsArrayStateVariable(Context *context, const char *name)
FloatsArrayStateVariable constructor.
GLUPfloat * get_pointer()
Gets a modifiable pointer to the variable.
GLuint & VBO()
Gets the Vertex Buffer Object.
bool is_enabled() const
Tests whether this ImmediateBuffer is enabled.
GLfloat * data()
Gets a pointer to the data.
GLfloat * element_ptr(index_t v)
Gets a pointer to one attribute value by index.
void set_current(GLfloat x, GLfloat y, GLfloat z, GLfloat w)
Sets the current attribute value.
size_t size_in_bytes() const
Gets the size of the memory used by the buffer.
void enable()
Enables this ImmediateBuffer.
index_t dimension() const
Gets the dimension of the attribute.
ImmediateBuffer(const ImmediateBuffer &rhs)
ImmediateBuffer copy constructor.
void copy(index_t to, ImmediateBuffer &from_buffer, index_t from)
Copies this attribute from another attribute.
void copy(index_t to, index_t from)
Copies this attribute from a vertex to another one.
void copy_current_to(index_t v)
Copies the current attribute value to a specified vertex in this buffer.
void disable()
Disables this ImmediateBuffer.
Stores all the buffers used to implement the immediate-mode interface.
void begin(GLUPprimitive primitive, index_t max_current_vertex=0)
Configures the immediate state for rendering primitives of a given type.
GLUPprimitive primitive() const
Gets the primitive currently rendered, i.e. the argument to the latest invocation of begin().
void set_current_vertex(index_t v)
Sets the current vertex.
GLuint & VAO()
Gets the Vertex Array Object.
void copy_element(index_t to, index_t from)
Copies an element, i.e. all the attributes attached to a vertex.
void reset(index_t new_current_vertex=0)
Resets the current vertex index.
ImmediateState(index_t *nb_vertices_per_primitive)
ImmediateState constructor.
index_t nb_primitives() const
Gets the number of primitives stored in the buffers.
index_t max_current_vertex() const
Gets the maximum number of vertices in the buffer before the buffer is flushed.
index_t nb_vertices() const
Gets the number of vertices stored in the buffers.
~ImmediateState()
ImmediateState destructor.
void next_vertex()
Advances to the next vertex.
bool buffers_are_full()
Tests whether the buffers are full.
Implements the MarchingCells algorithm.
A Matrix stack.
void push()
Pushes a copy of the top matrix.
MatrixStack()
MatrixStack constructor.
void pop()
Removes a matrix from the top of the stack.
GLdouble * top()
Gets the matrix on the top of the stack.
static const int MAX_DEPTH
Maximum number of matrices in a stack.
void flag_uniform_buffer_as_dirty()
Indicates that the variables in the context need to be sent to OpenGL.
StateVariableBase()
StateVariableBase default constructor.
Memory::pointer address() const
Gets the address of the StateVariableBase.
StateVariableBase(Context *context, const char *name)
StateVariableBase constructor.
const std::string & name() const
Gets the name of this StateVariableBase.
void initialize(Context *context, const char *name)
Initializes a StateVariableBase.
A GLUP state variable of a given type.
void initialize(Context *context, const char *name, T value)
Initializes a StateVariable.
StateVariable(Context *context, const char *name, T value)
StateVariableBase constructor.
void set(T val)
Sets the value.
T get() const
Gets the value.
StateVariable()
StateVariable default constructor.
A GLUP state variable that contains a vector.
VectorStateVariable(Context *context, const char *name, index_t dimension)
VectorStateVariable constructor.
void get(GLUPfloat *x) const
Gets the value.
index_t dimension() const
Gets the dimension.
void set(const GLUPfloat *x)
Sets the value.
VectorStateVariable()
VectorStateVariable default constructor.
void initialize(Context *context, const char *name, index_t dimension)
Initializes a VectorStateVariable.
void clear()
clears the vector to its default value.
Common include file, providing basic definitions. Should be included before anything else by all head...
unsigned char byte
Unsigned byte type.
Definition memory.h:93
byte * pointer
Pointer to unsigned byte(s).
Definition memory.h:105
void clear(void *addr, size_t size)
Clears a memory block.
Definition memory.h:120
void copy(void *to, const void *from, size_t size)
Copies a memory block.
Definition memory.h:133
uint64_t uint64
Definition numeric.h:149
geo_index_t index_t
The type for storing and manipulating indices.
Definition numeric.h:341
void initialize(int flags=GEOGRAM_INSTALL_NONE)
Initialize Geogram.
~PrimitiveInfo()
PrimitiveInfo destructor.
PrimitiveInfo()
PrimitiveInfo constructor.
PrimitiveInfo(const PrimitiveInfo &rhs)
PrimitiveInfo copy constructor.
The set of state variables that represent GLUP uniform state.