/* =============== CreateOptTri =============== */ static void CreateOptTri( optVertex_t *first, optEdge_t *e1, optEdge_t *e2, optIsland_t *island ) { optEdge_t *opposite; optVertex_t *second, *third; optTri_t *optTri; mapTri_t *tri; if ( e1->v1 == first ) { second = e1->v2; } else if ( e1->v2 == first ) { second = e1->v1; } else { common->Error( "CreateOptTri: mislinked edge" ); return; } if ( e2->v1 == first ) { third = e2->v2; } else if ( e2->v2 == first ) { third = e2->v1; } else { common->Error( "CreateOptTri: mislinked edge" ); return; } if ( !IsTriangleValid( first, second, third ) ) { common->Error( "CreateOptTri: invalid" ); return; } //DrawEdges( island ); // identify the third edge if ( dmapGlobals.drawflag ) { qglColor3f(1,1,0); qglBegin( GL_LINES ); qglVertex3fv( e1->v1->pv.ToFloatPtr() ); qglVertex3fv( e1->v2->pv.ToFloatPtr() ); qglEnd(); qglFlush(); qglColor3f(0,1,1); qglBegin( GL_LINES ); qglVertex3fv( e2->v1->pv.ToFloatPtr() ); qglVertex3fv( e2->v2->pv.ToFloatPtr() ); qglEnd(); qglFlush(); } for ( opposite = second->edges ; opposite ; ) { if ( opposite != e1 && ( opposite->v1 == third || opposite->v2 == third ) ) { break; } if ( opposite->v1 == second ) { opposite = opposite->v1link; } else if ( opposite->v2 == second ) { opposite = opposite->v2link; } else { common->Error( "BuildOptTriangles: mislinked edge" ); return; } } if ( !opposite ) { common->Printf( "Warning: BuildOptTriangles: couldn't locate opposite\n" ); return; } if ( dmapGlobals.drawflag ) { qglColor3f(1,0,1); qglBegin( GL_LINES ); qglVertex3fv( opposite->v1->pv.ToFloatPtr() ); qglVertex3fv( opposite->v2->pv.ToFloatPtr() ); qglEnd(); qglFlush(); } // create new triangle optTri = (optTri_t *)Mem_Alloc( sizeof( *optTri ), TAG_DMAP ); optTri->v[0] = first; optTri->v[1] = second; optTri->v[2] = third; optTri->midpoint = ( optTri->v[0]->pv + optTri->v[1]->pv + optTri->v[2]->pv ) * ( 1.0f / 3.0f ); optTri->next = island->tris; island->tris = optTri; if ( dmapGlobals.drawflag ) { qglColor3f( 1, 1, 1 ); qglPointSize( 4 ); qglBegin( GL_POINTS ); qglVertex3fv( optTri->midpoint.ToFloatPtr() ); qglEnd(); qglFlush(); } // find the midpoint, and scan through all the original triangles to // see if it is inside any of them for ( tri = island->group->triList ; tri ; tri = tri->next ) { if ( PointInTri( optTri->midpoint, tri, island ) ) { break; } } if ( tri ) { optTri->filled = true; } else { optTri->filled = false; } if ( dmapGlobals.drawflag ) { if ( optTri->filled ) { qglColor3f( ( 128 + orandom.RandomInt( 127 ) )/ 255.0, 0, 0 ); } else { qglColor3f( 0, ( 128 + orandom.RandomInt( 127 ) ) / 255.0, 0 ); } qglBegin( GL_TRIANGLES ); qglVertex3fv( optTri->v[0]->pv.ToFloatPtr() ); qglVertex3fv( optTri->v[1]->pv.ToFloatPtr() ); qglVertex3fv( optTri->v[2]->pv.ToFloatPtr() ); qglEnd(); qglColor3f( 1, 1, 1 ); qglBegin( GL_LINE_LOOP ); qglVertex3fv( optTri->v[0]->pv.ToFloatPtr() ); qglVertex3fv( optTri->v[1]->pv.ToFloatPtr() ); qglVertex3fv( optTri->v[2]->pv.ToFloatPtr() ); qglEnd(); qglFlush(); } // link the triangle to it's edges LinkTriToEdge( optTri, e1 ); LinkTriToEdge( optTri, e2 ); LinkTriToEdge( optTri, opposite ); }
inline bool PointInTri(glm::ipoint2 p, glm::ipoint2 t1, glm::ipoint2 t2, glm::ipoint2 t3) { return PointInTri(p.x, p.y, t1.x, t1.y, t2.x, t2.y, t3.x, t3.y); }