Be more fault tolerant for drawing normals

Some open data structures are not yet supported
with vertex normals. Avoid infinite loop for those and
just draw what we got.
This commit is contained in:
hasufell 2014-05-17 14:55:46 +02:00
parent a677668418
commit acadb1161c
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
2 changed files with 17 additions and 3 deletions

View File

@ -100,7 +100,10 @@ static void draw_normals(HE_obj const * const obj,
glBegin(GL_LINES); glBegin(GL_LINES);
for (uint32_t i = 0; i < obj->vc; i++) { for (uint32_t i = 0; i < obj->vc; i++) {
VEC_NORMAL(&(obj->vertices[i]), &vec); /* be fault tolerant here, so we don't just
* kill the whole thing, because the normals failed to draw */
if (!vec_normal(&(obj->vertices[i]), &vec))
break;
glVertex3f(obj->vertices[i].vec->x, glVertex3f(obj->vertices[i].vec->x,
obj->vertices[i].vec->y, obj->vertices[i].vec->y,
@ -108,7 +111,6 @@ static void draw_normals(HE_obj const * const obj,
glVertex3f(obj->vertices[i].vec->x + (vec.x * normals_scale_factor), glVertex3f(obj->vertices[i].vec->x + (vec.x * normals_scale_factor),
obj->vertices[i].vec->y + (vec.y * normals_scale_factor), obj->vertices[i].vec->y + (vec.y * normals_scale_factor),
obj->vertices[i].vec->z + (vec.z * normals_scale_factor)); obj->vertices[i].vec->z + (vec.z * normals_scale_factor));
} }
glEnd(); glEnd();
glPopMatrix(); glPopMatrix();

View File

@ -72,6 +72,7 @@ static bool get_all_emanating_edges(HE_vert const * const vert,
uint32_t *ec_out) uint32_t *ec_out)
{ {
uint32_t ec = 0; /* edge count */ uint32_t ec = 0; /* edge count */
uint32_t max_edges = 500; /* good guess to avoid infinite loop */
HE_edge **edge_array = NULL; HE_edge **edge_array = NULL;
if (!edge_array_out || !vert || !ec_out) if (!edge_array_out || !vert || !ec_out)
@ -89,6 +90,9 @@ static bool get_all_emanating_edges(HE_vert const * const vert,
edge = edge->pair->next; edge = edge->pair->next;
ec++; ec++;
/* sanity check */
if (ec > max_edges)
goto loop_fail_cleanup;
} while (edge && edge != vert->edge); } while (edge && edge != vert->edge);
/* set out-pointers */ /* set out-pointers */
@ -96,6 +100,10 @@ static bool get_all_emanating_edges(HE_vert const * const vert,
*ec_out = ec; /* this is the real size, not the x[ec] value */ *ec_out = ec; /* this is the real size, not the x[ec] value */
return true; return true;
loop_fail_cleanup:
free(edge_array);
return false;
} }
@ -146,7 +154,11 @@ bool vec_normal(HE_vert const * const vert, vector *vec)
if (!vert || !vec) if (!vert || !vec)
return false; return false;
GET_ALL_EMANATING_EDGES(vert, &edge_array, &ec); /* fault tolerance if we didn't get any
* normal */
if (!get_all_emanating_edges(vert, &edge_array, &ec))
return false;
COPY_VECTOR(edge_array[0]->vert->vec, &he_base); COPY_VECTOR(edge_array[0]->vert->vec, &he_base);
SET_NULL_VECTOR(vec); /* set to null for later summation */ SET_NULL_VECTOR(vec); /* set to null for later summation */