Skip to content
Related Articles
Get the best out of our app
GeeksforGeeks App
Open App
geeksforgeeks
Browser
Continue

Related Articles

Bezier Curves in OpenGL

Improve Article
Save Article
Like Article
Improve Article
Save Article
Like Article

OpenGL is a cross-language, cross-platform API for rendering 2D and 3D Vector Graphics. It is used to perform a lot of design as well as animation using OpenGL. In this article, we will discuss the concept and implementation of the Bezier Curves OpenGL.

Beizer Curves:

Béziers curves are one of the parametric curves most frequently used in computer graphics and were independently developed for computer-assisted car design by two engineers, both working for French automobile companies: Pierre Bézier, who was an engineer for Renault, and Paul de Casteljau, who was an engineer for Citroën. 

  • The concept of Bezier curves was discovered by the French engineer Pierre Bézier.
  • Bezier curves are basically used in computer graphics to draw shapes, for CSS animation, and in many other places.
  • These are the curves that are generated under the control of some other points, also called control points.

Properties of Bezier Curves:

  • A very important property of Bezier curves is that they always pass through the first and last control points.
  • The degree of the polynomial defining the curve segment is always one less than the number of defining polygon points. So, for example, if we have 4 control points, then the degree of the polynomial is 3, i.e., cubic polynomial.
  • In the Bezier curves, moving a control point alters the shape of the whole curve.
  • A Bezier curve generally follows the shape of the defining polygon.
  • A curve is always inside the convex hull of control points.

Below is the C++ program to implement the Bezier Curves:

C++




// C++ program to implement Bezier Curves
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <stdlib.h>
 
int i;
 
// Function to initialize the Beizer
// curve pointer
void init(void)
{
    glClearColor(1.0, 1.0, 1.0, 1.0);
}
 
// Function to draw the Bitmap Text
void drawBitmapText(char* string, float x,
                    float y, float z)
{
    char* c;
    glRasterPos2f(x, y);
 
    // Traverse the string
    for (c = string; *c != '\0'; c++) {
        glutBitmapCharacter(
            GLUT_BITMAP_TIMES_ROMAN_24, *c);
    }
}
 
// Function to draw the shapes
void draw(GLfloat ctrlpoints[4][3])
{
    glShadeModel(GL_FLAT);
    glMap1f(GL_MAP1_VERTEX_3, 0.0, 1.0, 3, 4,
            &ctrlpoints[0][0]);
 
    glEnable(GL_MAP1_VERTEX_3);
 
    // Fill the color
    glColor3f(1.0, 1.0, 1.0);
    glBegin(GL_LINE_STRIP);
 
    // Find the coordinates
    for (i = 0; i <= 30; i++)
        glEvalCoord1f((GLfloat)i / 30.0);
 
    glEnd();
    glFlush();
}
 
// Function to display the curved
// drawn using the Beizer Curve
void display(void)
{
    int i;
 
    // Specifying all the control
    // points through which the
    // curve will pass
    GLfloat ctrlpoints[4][3]
        = { { -0.00, 2.00, 0.0 },
            { -2.00, 2.00, 0.0 },
            { -2.00, -1.00, 0.0 },
            { -0.00, -1.00, 0.0 } };
    draw(ctrlpoints);
 
    GLfloat ctrlpoints2[4][3]
        = { { 0.0, -1.00, 0.0 },
            { 0.55, -0.65, 0.0 },
            { 0.65, -0.25, 0.0 },
            { 0.00, 0.70, 0.0 } };
 
    draw(ctrlpoints2);
    GLfloat ctrlpoints3[4][3]
        = { { 0.0, 0.70, 0.0 },
            { 0.15, 0.70, 0.0 },
            { 0.25, 0.70, 0.0 },
            { 0.65, 0.700, 0.0 } };
 
    draw(ctrlpoints3);
    GLfloat ctrlpoints4[4][3]
        = { { 0.65, 0.70, 0.0 },
            { 0.65, -0.90, 0.0 },
            { 0.65, -0.70, 0.0 },
            { 0.65, -1.00, 0.0 } };
 
    draw(ctrlpoints4);
 
    GLfloat ctrlpoints5[4][3]
        = { { 1.00, -1.00, 0.0 },
            { 1.00, -0.5, 0.0 },
            { 1.00, -0.20, 0.0 },
            { 1.00, 1.35, 0.0 } };
 
    draw(ctrlpoints5);
    GLfloat ctrlpoints6[4][3]
        = { { 1.00, 1.35 },
            { 1.10, 1.35, 0.0 },
            { 1.10, 1.35, 0.0 },
            { 1.90, 1.35, 0.0 } };
 
    draw(ctrlpoints6);
    GLfloat ctrlpoints7[4][3]
        = { { 1.00, 0.50, 0.0 },
            { 1.10, 0.5, 0.0 },
            { 1.10, 0.5, 0.0 },
            { 1.90, 0.5, 0.0 } };
 
    draw(ctrlpoints7);
    GLfloat ctrlpoints8[4][3]
        = { { 3.50, 2.00, 0.0 },
            { 1.50, 2.00, 0.0 },
            { 1.50, -1.00, 0.0 },
            { 3.50, -1.00, 0.0 } };
    draw(ctrlpoints8);
 
    GLfloat ctrlpoints9[4][3]
        = { { 3.50, -1.00, 0.0 },
            { 4.05, -0.65, 0.0 },
            { 4.15, -0.25, 0.0 },
            { 3.50, 0.70, 0.0 } };
    draw(ctrlpoints9);
 
    GLfloat ctrlpoints10[4][3]
        = { { 3.50, 0.70, 0.0 },
            { 3.65, 0.70, 0.0 },
            { 3.75, 0.70, 0.0 },
            { 4.15, 0.700, 0.0 } };
 
    draw(ctrlpoints10);
    GLfloat ctrlpoints11[4][3]
        = { { 4.15, 0.70, 0.0 },
            { 4.15, -0.90, 0.0 },
            { 4.15, -0.70, 0.0 },
            { 4.15, -1.00, 0.0 } };
 
    draw(ctrlpoints11);
 
    GLfloat ctrlpoints12[4][3]
        = { { -2.0, 2.50, 0.0 },
            { 2.05, 2.50, 0.0 },
            { 3.15, 2.50, 0.0 },
            { 4.65, 2.50, 0.0 } };
 
    draw(ctrlpoints12);
 
    GLfloat ctrlpoints13[4][3]
        = { { -2.0, -1.80, 0.0 },
            { 2.05, -1.80, 0.0 },
            { 3.15, -1.80, 0.0 },
            { 4.65, -1.80, 0.0 } };
 
    draw(ctrlpoints13);
 
    GLfloat ctrlpoints14[4][3]
        = { { -2.0, -1.80, 0.0 },
            { -2.0, 1.80, 0.0 },
            { -2.0, 1.90, 0.0 },
            { -2.0, 2.50, 0.0 } };
 
    draw(ctrlpoints14);
    GLfloat ctrlpoints15[4][3]
        = { { 4.650, -1.80, 0.0 },
            { 4.65, 1.80, 0.0 },
            { 4.65, 1.90, 0.0 },
            { 4.65, 2.50, 0.0 } };
 
    draw(ctrlpoints15);
 
    // Specifying the colour of
    // text to be displayed
    glColor3f(1, 0, 0);
    drawBitmapText("Bezier Curves "
                   "Implementation",
                   -1.00, -3.0, 0);
    glFlush();
}
 
// Function perform the reshaping
// of the curve
void reshape(int w, int h)
{
    glViewport(0, 0, (GLsizei)w,
               (GLsizei)h);
 
    // Matrix mode
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
 
    if (w <= h)
        glOrtho(-5.0, 5.0, -5.0
                               * (GLfloat)h / (GLfloat)w,
                5.0 * (GLfloat)h / (GLfloat)w, -5.0, 5.0);
    else
        glOrtho(-5.0 * (GLfloat)w / (GLfloat)h,
                5.0 * (GLfloat)w / (GLfloat)h,
                -5.0, 5.0,
                -5.0, 5.0);
 
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}
 
// Driver Code
int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(
        GLUT_SINGLE | GLUT_RGB);
 
    // Specifies the window size
    glutInitWindowSize(500, 500);
    glutInitWindowPosition(100, 100);
 
    // Creates the window as
    // specified by the user
    glutCreateWindow(argv[0]);
    init();
 
    // Links display event with the
    // display event handler(display)
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
 
    // Loops the current event
    glutMainLoop();
 
    return 0;
}

Output:


My Personal Notes arrow_drop_up
Last Updated : 23 Jun, 2022
Like Article
Save Article
Similar Reads