How to draw 2D irregular curved shapes in OpenGL
I've tried for the past 3 hours to figure out how to use Bezier curve formula, glMap1f and glEvalCoord1f to create a curved shape. (i basically want to create the right of ki开发者_如何学运维rby).
However, I've achieve little success. When I use glBegin(GL_LINE_STRIP), it traces the outline, but there is a big + (like your x and y axis). When I try to (GL_POLYGON), the fill works, but it has a diamond connected the 4 points i'm using.
I found out what's wrong. It turns out I was using sizeof(array)
incorrectly. (specifcally, with reference to the code below, instead of using i < numCurves
, I naively tried to do i < sizeof(points)
)
Hence, instead of drawing 4-5 points, I ended up drawing 192 curves, leading to weird outcomes. The diamond in the middle was due to not connecting back to the starting point. For reference for future people who wants to do this, here's the code:
Erroneous Picture:
Erroneous Picture http://img535.imageshack.us/img535/8207/failfd.png
Final Product:
Final Product http://img685.imageshack.us/img685/7641/finalsa.png
void drawIrregularPolygon(GLfloat points[][4][3], int numCurves)
// REQUIRES: GLfloat points to be a polygon and center of circle to be current matrix
// EFFECTS: An irregular shape would be drawn
{
for (int i = 0; i < numCurves; i++)
{
glMap1f( GL_MAP1_VERTEX_3, 0.0f, 1.0f, 3, 4, &points[i][0][0] );
glBegin(GL_POLYGON); //replace GL_POLYGON with GL_LINE_STRIP if you need outline
for (int j = 0; j <= TOTAL_SEGMENTS; j++)
glEvalCoord1f((GLfloat) j / TOTAL_SEGMENTS);
glVertex2f(points[0][0][0], points[0][0][1]);
glEnd();
}
}
精彩评论