40#ifndef GEOGRAM_GFX_GLUP_GLUP_CONTEXT
41#define GEOGRAM_GFX_GLUP_GLUP_CONTEXT
54#ifdef GEO_GL_NO_DOUBLES
55typedef double GLdouble;
87 GLfloat out[16],
const GLfloat m1[16],
const GLfloat m2[16]
96 GLdouble out[16],
const GLdouble m1[16],
const GLdouble m2[16]
108 GLfloat out[4],
const GLfloat m[16],
const GLfloat v[4]
122 GLfloat out[4],
const GLfloat m[16],
const GLfloat v[4]
136 GLdouble out[4],
const GLdouble m[16],
const GLdouble v[4]
146 GLdouble out[4],
const GLdouble m[16],
const GLdouble v[4]
227 to[i] = GLfloat(from[i]);
239 to[i] = GLdouble(from[i]);
249 GLfloat s = 1.0f / ::sqrtf(v[0]*v[0]+v[1]*v[1]+v[2]*v[2]);
261 extern index_t nb_vertices_per_primitive[];
295 return stack_[top_].data();
303 GLdouble* from =
top();
305 GLdouble* to =
top();
338 static const index_t IMMEDIATE_BUFFER_SIZE = 65536;
345 GLUP_VERTEX_ATTRIBUTE = 0,
346 GLUP_COLOR_ATTRIBUTE = 1,
347 GLUP_TEX_COORD_ATTRIBUTE = 2,
348 GLUP_NORMAL_ATTRIBUTE = 3,
349 GLUP_VERTEX_ID_ATTRIBUTE = 4
375 glDeleteBuffers(1, &VBO_);
381 data_ =
new GLfloat[dim * IMMEDIATE_BUFFER_SIZE];
462 return IMMEDIATE_BUFFER_SIZE *
dimension() *
sizeof(GLfloat);
473 return data_ + v*dimension_;
504 dimension_ = rhs.dimension_;
505 is_enabled_ = rhs.is_enabled_;
507 current_[0] = rhs.current_[0];
508 current_[1] = rhs.current_[1];
509 current_[2] = rhs.current_[2];
510 current_[3] = rhs.current_[3];
533 max_current_vertex_(0),
534 primitive_(GLUP_POINTS),
537 buffer[GLUP_VERTEX_ATTRIBUTE].initialize(4);
538 buffer[GLUP_COLOR_ATTRIBUTE].initialize(4);
539 buffer[GLUP_TEX_COORD_ATTRIBUTE].initialize(4);
540 buffer[GLUP_NORMAL_ATTRIBUTE].initialize(4);
543 buffer[GLUP_VERTEX_ATTRIBUTE].
enable();
573 for(
index_t i=0; i<NB_IMMEDIATE_BUFFERS; ++i) {
574 buffer[i].
copy(to, from);
586 max_current_vertex_ =
587 IMMEDIATE_BUFFER_SIZE - (
588 IMMEDIATE_BUFFER_SIZE %
600 for(
index_t i=0; i<NB_IMMEDIATE_BUFFERS; ++i) {
613 return (current_vertex_ == max_current_vertex_);
635 return current_vertex_;
654 return max_current_vertex_;
668 enum { NB_IMMEDIATE_BUFFERS = 4 };
669 ImmediateBuffer buffer[NB_IMMEDIATE_BUFFERS];
719 const std::string&
name()
const {
789 return *
reinterpret_cast<T*
>(address_);
798 *
reinterpret_cast<T*
>(address_) = val;
834 return reinterpret_cast<const GLUPfloat*
>(address_);
849 return reinterpret_cast<GLUPfloat*
>(address_);
905 void get(GLUPfloat* x)
const {
906 Memory::copy(x, address_,
sizeof(GLUPfloat)*dimension_);
914 void set(
const GLUPfloat* x) {
915 Memory::copy(address_, x,
sizeof(GLUPfloat)*dimension_);
927 if(dimension_ == 4) {
928 reinterpret_cast<GLUPfloat*
>(address_)[3] = 1.0f;
988 nb_elements_per_primitive(0),
989 primitive_elements(nullptr),
990 vertex_gather_mode(false),
1001 GL_primitive = rhs.GL_primitive;
1003 elements_VBO = rhs.elements_VBO;
1004 nb_elements_per_primitive = rhs.nb_elements_per_primitive;
1005 primitive_elements = rhs.primitive_elements;
1006 vertex_gather_mode = rhs.vertex_gather_mode;
1007 implemented = rhs.implemented;
1017 for(
auto& it : shader_map) {
1018 if(it.second != 0) {
1019 glDeleteProgram(it.second);
1023 if(elements_VBO != 0) {
1024 glDeleteBuffers(1, &elements_VBO);
1032 bool program_is_initialized(ShaderKey k)
const {
1033 return (shader_map.find(k) != shader_map.end());
1036 GLuint program(ShaderKey k)
const {
1037 auto it = shader_map.find(k);
1038 return ((it == shader_map.end()) ? 0 : it->second);
1041 GLenum GL_primitive;
1042 std::map<ShaderKey, GLuint> shader_map;
1044 GLuint elements_VBO;
1045 index_t nb_elements_per_primitive;
1047 bool vertex_gather_mode;
1119 copy_vector(matrix_stack_[matrix_mode_].top(), m, 16);
1130 copy_vector(matrix_stack_[matrix_mode_].top(), m, 16);
1151 GLdouble product[16];
1162 matrix_stack_[matrix_mode_].
push();
1169 matrix_stack_[matrix_mode_].
pop();
1180 matrix_mode_ = matrix;
1189 return matrix_mode_;
1201 GLfloat x, GLfloat y, GLfloat z=0.0f, GLfloat w=1.0f
1203 immediate_state_.buffer[GLUP_VERTEX_ATTRIBUTE].
set_current(x,y,z,w);
1216 GLfloat r, GLfloat g, GLfloat b, GLfloat a = 1.0f
1218 immediate_state_.buffer[GLUP_COLOR_ATTRIBUTE].
set_current(r,g,b,a);
1227 GLfloat s, GLfloat t=0.0f, GLfloat u=0.0f, GLfloat v=1.0f
1229 immediate_state_.buffer[GLUP_TEX_COORD_ATTRIBUTE].
set_current(
1240 immediate_state_.buffer[GLUP_NORMAL_ATTRIBUTE].
set_current(
1250 user_program_ = program;
1296 GLUPenum type,
const GLUPvoid* indices
1314 return uniform_state_;
1322 return uniform_state_;
1331 uniform_buffer_dirty_ =
true;
1340 uniform_buffer_dirty_ =
true;
1341 lighting_dirty_ =
true;
1349 uniform_buffer_dirty_ =
true;
1350 matrices_dirty_ =
true;
1361 return matrix_stack_[matrix].
top();
1371 std::vector<GLSL::Source>& sources
1381 std::vector<GLSL::Source>& sources
1391 std::vector<GLSL::Source>& sources
1401 std::vector<GLSL::Source>& sources
1411 std::vector<GLSL::Source>& sources
1425 std::vector<GLSL::Source>& sources
1437 std::vector<GLSL::Source>& sources
1449 std::vector<GLSL::Source>& sources
1462 GLUPbitfield toggles_state,
1463 GLUPbitfield toggles_undetermined=0
1482 return immediate_state_;
1624 GLUPprimitive glup_primitive, GLenum gl_primitive, GLuint program,
1625 bool bind_attrib_loc_and_link =
true
1643 GLUPprimitive glup_primitive, GLenum gl_primitive, GLuint program
1667 GLUPprimitive glup_primitive, GLenum gl_primitive, GLuint program,
1668 index_t nb_elements_per_glup_primitive,
1677 if(uniform_buffer_dirty_) {
1723 PrimitiveInfo::ShaderKey toggles_config
1725 if(toggles_config == (1 << GLUP_PICKING)) {
1727 (1 << GLUP_PICKING),
1728 (1 << GLUP_CLIPPING)
1808 for(
index_t lv=0; lv<nb_v; ++lv) {
1810 result = result | (1u << lv);
1828 const GLUPfloat* p1 = immediate_state_.buffer[0].
element_ptr(v1);
1829 const GLUPfloat* p2 = immediate_state_.buffer[0].
element_ptr(v2);
1831 GLUPfloat t = -eqn[3] -(
1838 eqn[0]*(p2[0]-p1[0]) +
1839 eqn[1]*(p2[1]-p1[1]) +
1840 eqn[2]*(p2[2]-p1[2]) ;
1842 if(fabs(
double(d)) < 1e-6) {
1848 GLUPfloat s = 1.0f - t;
1856 if(immediate_state_.buffer[i].
is_enabled()) {
1857 const GLUPfloat* a1 =
1859 const GLUPfloat* a2 =
1886 glUseProgram(program);
1890 glUseProgram(program);
1902 static void initialize();
1907 GLuint default_program_;
1908 GLuint uniform_buffer_;
1909 GLuint uniform_binding_point_;
1910 GLint uniform_buffer_size_;
1911 bool uniform_buffer_dirty_;
1917 bool lighting_dirty_;
1920 GLUPmatrix matrix_mode_;
1922 bool matrices_dirty_;
1940 GLuint user_program_;
1942 PrimitiveInfo::ShaderKey toggles_config_;
1945 GLUPbitfield toggles_source_state_;
1946 GLUPbitfield toggles_source_undetermined_;
1948 bool precompile_shaders_;
1950 bool use_core_profile_;
1951 bool use_ES_profile_;
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.
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.
index_t nb_vertices_per_primitive[]
Gives the number of vertices for each GLUP primitive.
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.
#define geo_debug_assert(x)
Verifies that a condition is met.
A class that can register functions to the GLSL pseudo file system.
Vector with aligned memory allocation.
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 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.
~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.
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...
void immediate_normal(GLfloat x, GLfloat y, GLfloat z)
Specifies the current normal vector for the immediate mode buffers.
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.
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 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.
const char * glup_primitive_name(GLUPprimitive prim)
Gets the name of a primitive by GLUPprimitive.
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.
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.
Implements the MarchingCells algorithm.
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.
Base class for representing GLUP state variables.
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.
byte * pointer
Pointer to unsigned byte(s)
void clear(void *addr, size_t size)
Clears a memory block.
void copy(void *to, const void *from, size_t size)
Copies a memory block.
Global Vorpaline namespace.
geo_index_t index_t
The type for storing and manipulating indices.
Stores the programs and vertex array object used to display a primitive of a given type.
~PrimitiveInfo()
PrimitiveInfo destructor.
PrimitiveInfo()
PrimitiveInfo constructor.
PrimitiveInfo(const PrimitiveInfo &rhs)
PrimitiveInfo copy constructor.