This project was really simple but was a good introduction into bezier curves and their uses. I didn't have a lot of inspiration for this project but the result was fun and it looked cool. This introduction to bezier curves was a great experience because it gave me an insight into how a lot of graphical tools create curves and splines.
Check out the video demonstration:
Here's the code to draw the curve:
struct Bezier {
//line layout should be p1 c1 c2 p2 from end to end
struct Point p1, p2;
struct Point c1, c2;
struct Point normal;
float t;
float r, g, b;
};
void drawBezier(struct Bezier *b) {
glLineWidth(2);
glColor3f(b->r, b->g, b->b);
glBegin(GL_LINE_STRIP);
for (int i = 0; i < b->t; ++i) {
float t = (float)i / (float)(b->t);
float omt = 1.f - t;
float x = omt * omt*omt*(b->p1.x) + 3.f*t*omt*omt*(b->c1.x) + 3.f*t*t*omt*(b->c2.x) + t * t*t*(b->p2.x);
float y = omt * omt*omt*(b->p1.y) + 3.f*t*omt*omt*(b->c1.y) + 3.f*t*t*omt*(b->c2.y) + t * t*t*(b->p2.y);
float z = omt * omt*omt*(b->p1.z) + 3.f*t*omt*omt*(b->c1.z) + 3.f*t*t*omt*(b->c2.z) + t * t*t*(b->p2.z);
float p = t - 0.5;
float hue = p * 120;
float hsv[3] = { hue, Time + 0.2, 1. };
float rgb[3] = { 1, 1, 1 };
HsvRgb(hsv, rgb);
if (debugPoints)
{
glColor3f(1, 0, 0);
}
else {
glColor3fv(rgb);
}
glVertex3f(x, y, z);
}
glEnd();
//Draw the control lines
if (linesOn) {
glBegin(GL_LINE_STRIP);
glColor3f(1, 1, 1);
glVertex3f(b->p1.x, b->p1.y, b->p1.z);
glVertex3f(b->c1.x, b->c1.y, b->c1.z);
glVertex3f(b->c2.x, b->c2.y, b->c2.z);
glVertex3f(b->p2.x, b->p2.y, b->p2.z);
glEnd();
}
//Draw the debug lines
if (debugPoints) {
glBegin(GL_LINE_STRIP);
glColor3f(1, 1, 1);
glVertex3f(b->p1.x, b->p1.y, b->p1.z);
glVertex3f(b->p2.x, b->p2.y, b->p2.z);
glEnd();
}
//Draw control points
if (pointsOn) {
glPointSize(10);
glBegin(GL_POINTS);
glColor3f(0, 1, 0); //Draw end points in green
glVertex3f(b->p1.x, b->p1.y, b->p1.z);
glVertex3f(b->p2.x, b->p2.y, b->p2.z);
glColor3f(1, 1, 0); //Draw control points in yellow
glVertex3f(b->c1.x, b->c1.y, b->c1.z);
glVertex3f(b->c2.x, b->c2.y, b->c2.z);
glEnd();
}
}