void GLGraphics2D::emintRectVertex(Vec2f const& p1 , Vec2f const& p2) { emitVertex( p1.x , p1.y ); emitVertex( p2.x , p1.y ); emitVertex( p2.x , p2.y ); emitVertex( p1.x , p2.y ); }
void GLGraphics2D::emitRoundRectVertex( Vec2f const& pos , Vec2f const& rectSize , Vec2f const& circleSize) { int numSegment = calcCircleSemgmentNum( ( circleSize.x + circleSize.y ) / 2 ); float yFactor = float( circleSize.y ) / circleSize.x; int num = numSegment / 4; float theta = 2 * 3.1415926 / float(numSegment); float c = cos(theta);//precalculate the sine and cosine float s = sin(theta); float cvn[4][2] = { float( pos.x + rectSize.x - circleSize.x ) , float( pos.y + rectSize.y - circleSize.y ) , float( pos.x + circleSize.x ) , float( pos.y + rectSize.y - circleSize.y ) , float( pos.x + circleSize.x ) , float( pos.y + circleSize.y ) , float( pos.x + rectSize.x - circleSize.x ) , float( pos.y + circleSize.y ) , }; float v[2]; float x , y; float cx = cvn[0][0]; float cy = cvn[0][1]; for( int n = 0 ; n < 4 ; ++n ) { switch( n ) { case 0: x = circleSize.x; y = 0; break; case 1: y = circleSize.x; x = 0; break; case 2: x = -circleSize.x; y = 0; break; case 3: y = -circleSize.x; x = 0; break; } for(int i = 0; i < num; ++i) { v[0] = cx + x; v[1] = cy + yFactor * y; emitVertex( v ); float temp = x; x = c * temp - s * y; y = s * temp + c * y; } int next = ( n == 3 ) ? 0 : n + 1; cx = cvn[next][0]; cy = cvn[next][1]; v[0] = cx + x; v[1] = cy + yFactor * y; emitVertex( v ); } }
void GLGraphics2D::emitPolygonVertex(Vec2i pos[] , int num) { for( int i = 0 ; i < num ; ++i ) { emitVertex( pos[i].x , pos[i].y ); } }
void drawMeshEdges(const Gm::TriangleMesh& mesh) { auto edges = mesh.SilhouetteEdges(Gs::pi*0.01f); const auto color = Gs::Vector4(1, 1, 0, 1); glLineWidth(5); glBegin(GL_LINES); { for (const auto& edge : edges) { const auto& v0 = mesh.vertices[edge.a]; const auto& v1 = mesh.vertices[edge.b]; emitVertex(v0, color); emitVertex(v1, color); } } glEnd(); glLineWidth(1); }
void drawMesh(const Gm::TriangleMesh& mesh, bool wireframe = false) { Gs::Vector4 diffuse(1.0f, 1.0f, 1.0f, 1.0f); Gs::Vector4 ambient(0.4f, 0.4f, 0.4f, 1.0f); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, diffuse.Ptr()); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, ambient.Ptr()); glEnable(GL_LIGHTING); glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_FILL); if (texturedMode) texture->bind(); glBegin(GL_TRIANGLES); for (std::size_t i = 0; i < mesh.triangles.size(); ++i) { const auto& tri = mesh.triangles[i]; const auto& v0 = mesh.vertices[tri.a]; const auto& v1 = mesh.vertices[tri.b]; const auto& v2 = mesh.vertices[tri.c]; emitVertex(v0); emitVertex(v1); emitVertex(v2); } glEnd(); if (texturedMode) texture->unbind(); glDisable(GL_LIGHTING); }
void GLGraphics2D::emitEllipseVertex(float cx, float cy, float r , float yFactor , int numSegment) { float theta = 2 * 3.1415926 / float(numSegment); float c = cos(theta);//precalculate the sine and cosine float s = sin(theta); float x = r;//we start at angle = 0 float y = 0; float v[2]; for(int i = 0; i < numSegment; ++i) { v[0] = cx + x; v[1] = cy + yFactor * y; emitVertex( v ); float temp = x; x = c * temp - s * y; y = s * temp + c * y; } }
void drawMesh(const Gm::TriangleMesh& mesh, bool wireframe = false) { #ifdef TEST_TRIANGLE_NEIGHBORS auto neighbors = mesh.TriangleNeighbors({ 4, 5 }, neighborSearchDepth, false, true); #endif glPolygonMode(GL_FRONT_AND_BACK, wireframe ? GL_LINE : GL_FILL); glBegin(GL_TRIANGLES); for (std::size_t i = 0; i < mesh.triangles.size(); ++i) { const auto& tri = mesh.triangles[i]; const auto& v0 = mesh.vertices[tri.a]; const auto& v1 = mesh.vertices[tri.b]; const auto& v2 = mesh.vertices[tri.c]; #ifdef TEST_TRIANGLE_NEIGHBORS // Highlight test triangle and its neighbors int c = 0; for (auto t : neighbors) { if (t == i) { c = 2; break; } } Gs::Vector4 colors[] = { Gs::Vector4(1, 0, 0, 1), Gs::Vector4(0, 1, 0, 1), Gs::Vector4(0, 0, 1, 1) }; emitVertex(v0, colors[c]); emitVertex(v1, colors[c]); emitVertex(v2, colors[c]); #else emitVertex(v0); emitVertex(v1); emitVertex(v2); #endif } glEnd(); }
void emitVertex(const Gm::TriangleMesh::Vertex& vert) { // generate color from vertex data Gs::Vector4 color(vert.texCoord.x, vert.texCoord.y, 0.5f, 1); emitVertex(vert, color); }