Skip to content

Commit

Permalink
Final cleanup for thick lines in GLUPES2 (Closes #54)
Browse files Browse the repository at this point in the history
  • Loading branch information
BrunoLevy committed Jan 10, 2024
1 parent c8fa40f commit 2fe1eed
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 70 deletions.
34 changes: 2 additions & 32 deletions src/lib/geogram_gfx/GLUP/GLUP_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1381,12 +1381,7 @@ namespace GLUP {
uniform_state_.toggle[GLUP_PICKING].get() || (
primitive_dimension[immediate_state_.primitive()] >= 2 &&
uniform_state_.toggle[GLUP_DRAW_MESH].get()
) || (
// particular case for GLUP_THICK_LINES in GLUPES2 profile
// ugly ...
!strcmp(profile_name(),"GLUPES2") &&
immediate_state_.primitive() == GLUP_THICK_LINES
)
) || (immediate_state_.primitive() == GLUP_THICK_LINES)
) {
glEnableVertexAttribArray(GLUP_VERTEX_ID_ATTRIBUTE);
vertex_id_attrib_enabled = true;
Expand Down Expand Up @@ -1415,14 +1410,6 @@ namespace GLUP {
index_t nb_elements = nb_primitives *
primitive_info_[immediate_state_.primitive()].
nb_elements_per_primitive ;

// Special case (ugly...)
if(
immediate_state_.primitive() == GLUP_THICK_LINES &&
!strcmp(profile_name(), "GLUPES2")
) {
nb_elements = index_t(nb_vertices / 4) * 6;
}

glDrawElements(
primitive_info_[immediate_state_.primitive()].GL_primitive,
Expand Down Expand Up @@ -1626,14 +1613,6 @@ namespace GLUP {
IMMEDIATE_BUFFER_SIZE /
nb_vertices_per_primitive_[glup_primitive];

// Special case (ugly...)
if(
glup_primitive == GLUP_THICK_LINES &&
!strcmp(profile_name(), "GLUPES2")
) {
nb_glup_primitives /= 2;
}

index_t nb_elements =
nb_glup_primitives * nb_elements_per_glup_primitive;

Expand All @@ -1653,16 +1632,7 @@ namespace GLUP {
);
++cur_element;
}
// Special case (ugly...)
if(
glup_primitive == GLUP_THICK_LINES &&
!strcmp(profile_name(), "GLUPES2")
) {
cur_vertex_offset += 4;
} else {
cur_vertex_offset +=
nb_vertices_per_primitive_[glup_primitive];
}
cur_vertex_offset += nb_vertices_per_primitive_[glup_primitive];
}

glBufferData(
Expand Down
80 changes: 42 additions & 38 deletions src/lib/geogram_gfx/GLUP/GLUP_context_ES.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ namespace GLUP {

extern bool vertex_array_emulate;


void Context_ES2::begin(GLUPprimitive primitive) {
Context::begin(primitive);
if(primitive == GLUP_THICK_LINES) {
// Thick lines need to replace line segments with quads. The additional
// vertices are generated in flush()_immediate_buffers (but we need twice the
// Thick lines need to replace line segments with quads.
// The additional vertices are generated in
// flush()_immediate_buffers (but we need twice the
// room in the buffer, so we flush when the buffer is half-full).
immediate_state_.begin(GLUP_THICK_LINES, IMMEDIATE_BUFFER_SIZE/2);
}
Expand Down Expand Up @@ -759,10 +759,17 @@ namespace GLUP {
}

void Context_ES2::setup_GLUP_THICK_LINES() {
// When rendering, GLUP_THICK_LINES are transformed into quads
// in flush_immediate_buffers(), so we configure the shaders and
// rendering logic with this number of vertices per primitive
nb_vertices_per_primitive_[GLUP_THICK_LINES] = 4;

// For each quad, we draw two triangles
static index_t element_indices[6] = {
0, 1, 2,
2, 1, 3
};

set_primitive_info_immediate_index_mode(
primitive_source_, GL_TRIANGLES,
GLSL::compile_program_with_includes_no_link(
Expand All @@ -775,6 +782,20 @@ namespace GLUP {
index_t(sizeof(element_indices)/sizeof(index_t)),
element_indices
);

// From a client point of view, there are only two vertices for each
// line segment.
nb_vertices_per_primitive_[GLUP_THICK_LINES] = 2;

// The number of vertices per thick line segment is adapted in two
// other places in the code:
// - in Context_ES2::begin(), the maximum index for the immediate buffers
// is set to half-buffer (so that we have enough room to generate the
// additional vertices to render quads).
// - in Context_ES2::flush_immediate_buffers(),
// nb_vertices_per_primitive_[GLUP_THICK_LINES] is set to 4 when
// entering the function, so that base implementation in Context works
// (and it is restored to 2 when exiting the function)
}

void Context_ES2::setup_primitive_generic(
Expand Down Expand Up @@ -1125,8 +1146,16 @@ namespace GLUP {

void Context_ES2::flush_immediate_buffers() {
if(immediate_state_.primitive() == GLUP_THICK_LINES) {

geo_assert(immediate_state_.nb_vertices() <= IMMEDIATE_BUFFER_SIZE/2);
// When rendering, GLUP_THICK_LINES are transformed into quads
// in flush_immediate_buffers(), so we configure the shaders and
// rendering logic with this number of vertices per primitive
nb_vertices_per_primitive_[GLUP_THICK_LINES] = 4;

// Make sure we have enough room to double the number of
// vertices in immediate buffers
geo_assert(
immediate_state_.nb_vertices() <= IMMEDIATE_BUFFER_SIZE/2
);

for(index_t v=0; v<immediate_state_.nb_vertices(); ++v) {
index_t from = immediate_state_.nb_vertices()-v-1;
Expand All @@ -1140,7 +1169,9 @@ namespace GLUP {
immediate_state_.copy_element(to,from);
}

// Double immediate_state_.nb_vertices()
immediate_state_.reset(immediate_state_.nb_vertices()*2);

for(index_t v1=0; v1<immediate_state_.nb_vertices(); v1+=4) {
index_t v2=v1+1;
index_t v3=v1+2;
Expand Down Expand Up @@ -1174,39 +1205,6 @@ namespace GLUP {
immediate_state_.buffer[GLUP_VERTEX_ATTRIBUTE].copy(v3,v1);
immediate_state_.buffer[GLUP_VERTEX_ATTRIBUTE].copy(v4,v1);
}

// TESTING...
if(false)
for(index_t v=0; v<immediate_state_.nb_vertices(); v++) {
switch(v%4) {
case 0:
copy_vector(
immediate_state_.buffer[GLUP_VERTEX_ATTRIBUTE].element_ptr(v),
vec4(0,0,0,1).data(), 4
);
break;
case 1:
copy_vector(
immediate_state_.buffer[GLUP_VERTEX_ATTRIBUTE].element_ptr(v),
vec4(0,1,0,1).data(), 4
);
break;
case 2:
copy_vector(
immediate_state_.buffer[GLUP_VERTEX_ATTRIBUTE].element_ptr(v),
vec4(1,1,0,1).data(), 4
);
break;
case 3:
copy_vector(
immediate_state_.buffer[GLUP_VERTEX_ATTRIBUTE].element_ptr(v),
vec4(1,0,0,1).data(), 4
);
break;
}
}


}
classify_vertices_in_immediate_buffers();
shrink_cells_in_immediate_buffers();
Expand All @@ -1217,6 +1215,12 @@ namespace GLUP {
} else {
Context::flush_immediate_buffers();
}

if(immediate_state_.primitive() == GLUP_THICK_LINES) {
// From a client point of view, there are only two vertices for each
// line segment.
nb_vertices_per_primitive_[GLUP_THICK_LINES] = 2;
}
}


Expand Down

0 comments on commit 2fe1eed

Please sign in to comment.