Skip to content

Commit

Permalink
GLUP_THICK_LINES cleanup impl: nb_vertices_per_primitive is now store…
Browse files Browse the repository at this point in the history
…d in Context (and can be changed by subclasses)
  • Loading branch information
BrunoLevy committed Jan 10, 2024
1 parent 970206a commit c8fa40f
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 41 deletions.
52 changes: 24 additions & 28 deletions src/lib/geogram_gfx/GLUP/GLUP_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,23 +195,6 @@ namespace GLUP {
std::cerr << std::endl;
}

// Used to determine buffer's maximum vertex index, that needs
// to be an integer multiple of the number of vertices
// per primitive.
index_t nb_vertices_per_primitive[GLUP_NB_PRIMITIVES] = {
1, // GLUP_POINTS =0,
2, // GLUP_LINES =1,
3, // GLUP_TRIANGLES =2,
4, // GLUP_QUADS =3,
4, // GLUP_TETRAHEDRA =4,
8, // GLUP_HEXAHEDRA =5,
6, // GLUP_PRISMS =6,
5, // GLUP_PYRAMIDS =7,
4, // GLUP_CONNECTORS =8,
1, // GLUP_SPHERES =9,
2 // GLUP_THICK_LINES =10
};

static index_t nb_vertices_per_GL_primitive(GLenum primitive) {
index_t result = 0;
switch(primitive) {
Expand Down Expand Up @@ -504,6 +487,7 @@ namespace GLUP {


Context::Context() :
immediate_state_(nb_vertices_per_primitive_),
marching_tet_(GLUP_TETRAHEDRA),
marching_hex_(GLUP_HEXAHEDRA),
marching_prism_(GLUP_PRISMS),
Expand Down Expand Up @@ -543,6 +527,18 @@ namespace GLUP {
toggles_source_state_ = 0;
toggles_source_undetermined_ = 0;

nb_vertices_per_primitive_[GLUP_POINTS] = 1;
nb_vertices_per_primitive_[GLUP_LINES] = 2;
nb_vertices_per_primitive_[GLUP_TRIANGLES] = 3;
nb_vertices_per_primitive_[GLUP_QUADS] = 4;
nb_vertices_per_primitive_[GLUP_TETRAHEDRA] = 4;
nb_vertices_per_primitive_[GLUP_HEXAHEDRA] = 8;
nb_vertices_per_primitive_[GLUP_PRISMS] = 6;
nb_vertices_per_primitive_[GLUP_PYRAMIDS] = 5;
nb_vertices_per_primitive_[GLUP_CONNECTORS] = 4;
nb_vertices_per_primitive_[GLUP_SPHERES] = 1;
nb_vertices_per_primitive_[GLUP_THICK_LINES] = 2;

initialize();
}

Expand Down Expand Up @@ -982,7 +978,7 @@ namespace GLUP {
GEO_CHECK_GL();

if(primitive_info_[primitive].vertex_gather_mode) {
index_t n = nb_vertices_per_primitive[primitive];
index_t n = nb_vertices_per_primitive_[primitive];
GLenum GL_primitive = primitive_info_[primitive].GL_primitive;
n /= nb_vertices_per_GL_primitive(GL_primitive);

Expand Down Expand Up @@ -1036,7 +1032,7 @@ namespace GLUP {
GEO_CHECK_GL();

if(primitive_info_[immediate_state_.primitive()].vertex_gather_mode) {
index_t n = nb_vertices_per_primitive[immediate_state_.primitive()];
index_t n = nb_vertices_per_primitive_[immediate_state_.primitive()];
GLenum GL_primitive =
primitive_info_[immediate_state_.primitive()].GL_primitive;
n /= nb_vertices_per_GL_primitive(GL_primitive);
Expand Down Expand Up @@ -1166,7 +1162,7 @@ namespace GLUP {

// Specify number of vertices for GL_PATCH.
glPatchParameteri(
GL_PATCH_VERTICES, GLint(nb_vertices_per_primitive[primitive])
GL_PATCH_VERTICES, GLint(nb_vertices_per_primitive_[primitive])
);
}
#endif
Expand Down Expand Up @@ -1401,7 +1397,7 @@ namespace GLUP {

if(primitive_info_[immediate_state_.primitive()].vertex_gather_mode) {
nb_vertices /= GLsizei(
nb_vertices_per_primitive[immediate_state_.primitive()]
nb_vertices_per_primitive_[immediate_state_.primitive()]
);
nb_vertices *= GLsizei(
nb_vertices_per_GL_primitive(
Expand All @@ -1414,7 +1410,7 @@ namespace GLUP {
if(primitive_info_[immediate_state_.primitive()].elements_VBO != 0) {

index_t nb_primitives = index_t(nb_vertices) /
nb_vertices_per_primitive[immediate_state_.primitive()];
nb_vertices_per_primitive_[immediate_state_.primitive()];

index_t nb_elements = nb_primitives *
primitive_info_[immediate_state_.primitive()].
Expand Down Expand Up @@ -1521,7 +1517,7 @@ namespace GLUP {
GLUPprimitive glup_primitive, GLenum GL_primitive, GLuint program
) {
set_primitive_info(glup_primitive, GL_primitive, program, false);
index_t n = nb_vertices_per_primitive[glup_primitive];
index_t n = nb_vertices_per_primitive_[glup_primitive];
n /= nb_vertices_per_GL_primitive(GL_primitive);


Expand Down Expand Up @@ -1628,7 +1624,7 @@ namespace GLUP {

index_t nb_glup_primitives =
IMMEDIATE_BUFFER_SIZE /
nb_vertices_per_primitive[glup_primitive];
nb_vertices_per_primitive_[glup_primitive];

// Special case (ugly...)
if(
Expand Down Expand Up @@ -1665,7 +1661,7 @@ namespace GLUP {
cur_vertex_offset += 4;
} else {
cur_vertex_offset +=
nb_vertices_per_primitive[glup_primitive];
nb_vertices_per_primitive_[glup_primitive];
}
}

Expand Down Expand Up @@ -1742,7 +1738,7 @@ namespace GLUP {
String::to_string(primitive_dimension[prim]) +
";\n" +
"const int glup_primitive_nb_vertices = " +
String::to_string(nb_vertices_per_primitive[prim]) +
String::to_string(nb_vertices_per_primitive_[prim]) +
";\n" +
"#define GLUP_PRIMITIVE_DIMENSION " +
String::to_string(primitive_dimension[prim]) +
Expand Down Expand Up @@ -1864,7 +1860,7 @@ namespace GLUP {

GLfloat s = uniform_state_.cells_shrink.get();
GLfloat g[3];
index_t nb_v = nb_vertices_per_primitive[immediate_state_.primitive()];
index_t nb_v = nb_vertices_per_primitive_[immediate_state_.primitive()];
index_t v=0;
while(v < immediate_state_.nb_vertices()) {
g[0] = 0.0f;
Expand Down Expand Up @@ -1990,7 +1986,7 @@ namespace GLUP {
}
index_t nb_visible=0;
index_t nb_in_cell =
nb_vertices_per_primitive[immediate_state_.primitive()];
nb_vertices_per_primitive_[immediate_state_.primitive()];
for(index_t lv=0; lv<nb_in_cell; ++lv) {
nb_visible += (v_is_visible_[first_v + lv]);
}
Expand Down
31 changes: 19 additions & 12 deletions src/lib/geogram_gfx/GLUP/GLUP_context.h
Original file line number Diff line number Diff line change
Expand Up @@ -263,14 +263,6 @@ namespace GLUP {
v[2] *= s;
}


/**
* \brief Gives the number of vertices for each GLUP primitive.
* \details The array is indexed by the GLUP primitive type
* (one of GLUP_POINTS, GLUP_LINES, ...)
*/
extern index_t nb_vertices_per_primitive[];

/**********************************************************************/

class Context;
Expand Down Expand Up @@ -554,12 +546,16 @@ namespace GLUP {
public:
/**
* \brief ImmediateState constructor.
* \param[in] nb_vertices_per_primitive a pointer to an array of index_t
* of size GLUP_NB_PRIMITIVES that indicates for each primitive the
* number of vertices (3 for GLUP_TRIANGLES, 4 for GLUP_QUADS etc...).
*/
ImmediateState() :
ImmediateState(index_t* nb_vertices_per_primitive) :
current_vertex_(0),
max_current_vertex_(0),
primitive_(GLUP_POINTS),
VAO_(0)
VAO_(0),
nb_vertices_per_primitive_(nb_vertices_per_primitive)
{
buffer[GLUP_VERTEX_ATTRIBUTE].initialize(4);
buffer[GLUP_COLOR_ATTRIBUTE].initialize(4);
Expand Down Expand Up @@ -621,7 +617,7 @@ namespace GLUP {
max_current_vertex_ =
IMMEDIATE_BUFFER_SIZE - (
IMMEDIATE_BUFFER_SIZE %
nb_vertices_per_primitive[primitive]
nb_vertices_per_primitive_[primitive]
);
}
primitive_ = primitive;
Expand Down Expand Up @@ -677,7 +673,7 @@ namespace GLUP {
* \brief Gets the number of primitives stored in the buffers.
*/
index_t nb_primitives() const {
return current_vertex_ / nb_vertices_per_primitive[
return current_vertex_ / nb_vertices_per_primitive_[
primitive_
];
}
Expand Down Expand Up @@ -711,6 +707,7 @@ namespace GLUP {
index_t max_current_vertex_;
GLUPprimitive primitive_;
GLuint VAO_;
index_t* nb_vertices_per_primitive_;
};


Expand Down Expand Up @@ -2033,6 +2030,16 @@ namespace GLUP {
* \details It is used to emulate gl_VertexID in shaders.
*/
GLuint vertex_id_VBO_;

/**
* \brief Number of vertices per primitive (3 for GLUP_TRIANGLES,
* 4 for GLUP_QUADS etc...)
* \details It is stored as a class member array rather than a
* static array so that particular implementations can change
* it according to the needs (for instance, GLUPES profile temporarily
* uses quads with for vertices to render GLUP_THICK_LINES).
*/
index_t nb_vertices_per_primitive_[GLUP_NB_PRIMITIVES];
};

/*********************************************************************/
Expand Down
2 changes: 1 addition & 1 deletion src/lib/geogram_gfx/GLUP/GLUP_context_ES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -942,7 +942,7 @@ namespace GLUP {
index_t cur_vertex = 0;
index_t cur_element_out = 0;
index_t nb_vertices_per_cell =
nb_vertices_per_primitive[immediate_state_.primitive()];
nb_vertices_per_primitive_[immediate_state_.primitive()];
index_t nb_elements_per_cell = primitive_info_[
immediate_state_.primitive()].nb_elements_per_primitive;

Expand Down

0 comments on commit c8fa40f

Please sign in to comment.