本代码参考自《计算机图形学》第4版,根据极坐标参数方程绘制曲线:蜗形线、心形线、三叶曲线、四叶曲线、螺旋线共5种图形。
生成定点坐标的程序如下所示,该函数将需要绘制的顶点按顺序添加到入参verts的最后
typedef enum { LIMACON = 1,
CARDIOID,
THREELEAF,
FOURLEAF,
SPIRAL } curveName;
void GLApp::genVertices(std::vector<glm::vec2> &verts, GLint curveNum, const GLint x0, const GLint y0) { const GLdouble twoPi = 6.283185; const GLint a = 175, b = 60; GLfloat r, theta, dtheta = 1.0 / float(a); glm::vec2 curvePt[2]; curvePt[0].x = x0; curvePt[0].y = y0; switch (curveNum) { case LIMACON: curvePt[0].x += a + b; break; case CARDIOID: curvePt[0].x += a + a; break; case THREELEAF: curvePt[0].x += a; break; case FOURLEAF: curvePt[0].x += a; break; case SPIRAL: break; default: break; } theta = dtheta; while (theta < twoPi) { switch (curveNum) { case LIMACON: r = a * cos(theta) + b; break; case CARDIOID: r = a * (1 + cos(theta)); break; case THREELEAF: r = a * cos(3 * theta); break; case FOURLEAF: r = a * cos(2 * theta); break; case SPIRAL: r = (a / 4.0) * theta; break; default: break; } curvePt[1].x = x0 + r * cos(theta); curvePt[1].y = y0 + r * sin(theta); verts.push_back(curvePt[0]); verts.push_back(curvePt[1]); curvePt[0].x = curvePt[1].x; curvePt[0].y = curvePt[1].y; theta += dtheta; } }
绘制时调用glDrawArrays(GL_LINES, 0, vert_num);
(vert_num等于verts.size())