Robotics C++ Physics II AP Physics B Electronics Java Astronomy Other Courses Summer Session  

Examples

 

Example 1: Triangle
Example 2: Lines
Example 3: Points
   
Example 4: Polygon Using #define
Example 5: Stippled Lines
Example 6: Polygon Stipple

 

 

 

 

Example 1: Triangle

 

 #include <GL/glut.h>                  //Allows access to the glut library

void init()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);        //will discuss this late in course
    glShadeModel (GL_FLAT);                    //will discuss this late in course
}

void display()
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 0.0, 0.0);            //This sets the color of the polygon - RGB format (red, green, blue) (covered next chapter)
    glBegin(GL_POLYGON);           ///polygon - many sided figure
        glVertex2f(200.0,200.0);     //f means floating point or those with decimals  - setting the vertices of the polygon
        glVertex2f(400.0,200.0);
        glVertex2f(400.0, 400.0);
    glEnd();
    glFlush ();    //forces previously issued commands to execute
}

void reshape (int w, int h)       //Covered later
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

int main(int argc, char** argv)    //Because of glut, parameters are necessary
{
    glutInit(&argc, argv);              //calling various methods above
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);    // | means or
    glutInitWindowSize (600, 600);
    glutInitWindowPosition (100,100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

Example 2: Lines

 

 #include <GL/glut.h>

void init()                                                              
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);
}

void display()                    
{
    glClear (GL_COLOR_BUFFER_BIT);
    glColor3f (1.0, 1.0, 0.0);
    glBegin(GL_LINE_STRIP);
        glVertex2f(100.0,100.0);      //f means floating point or those with decimals
        glVertex2f(400.0,100.0);
        glVertex2f(400.0, 500.0);
    glEnd();
    glFlush ();      //forces previously issued commands to execute
}

void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

int main(int argc, char** argv)     //Because of glut, parameters are necessary
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);    // | means or
    glutInitWindowSize (600, 600);
    glutInitWindowPosition (100,100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

Example 3: Points with Point Size and Window Title

   

 #include <GL/glut.h>            //OpenGL windows management, includes gl.h and glu.h

void init()
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glPointSize(4.0);                                    //point size goes here
    glShadeModel (GL_FLAT);
}

void display()
{
    glClear (GL_COLOR_BUFFER_BIT);

    glColor3f (1.0, 0.0, 0.0);
    glBegin(GL_POINTS);
        glVertex2i(100, 300);        //using the integer option
        glVertex2i(200,300);
        glVertex2i(200,400);
    glEnd();
    glFlush ();      //forces previously issued commands to execute
}

void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

int main(int argc, char** argv)     //Because of glut, parameters are necessary
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);    // | means or
    glutInitWindowSize (640, 480);
    glutInitWindowPosition (100,150);
    glutCreateWindow ("Illustrating Points");    //Using title for window
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

 

 

Example 4: Polygon Using #define

 

#include <GL/glut.h>
#include <stdlib.h>

#define drawPolygon(x1,y1,x2,y2,x3,y3,x4,y4) glBegin(GL_POLYGON); \
    glVertex2f (x1,y1); glVertex2f (x2,y2); glVertex2f (x3,y3); glVertex2f(x4,y4); glEnd();

void init(void)      //parameter void not necessary
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);
}

void display(void)     //parameter void not necessary
{
    glClear (GL_COLOR_BUFFER_BIT);

    /* select white for all items */
    glColor3f (1.0, 1.0, 1.0);

    drawPolygon(20.0,40.0, 80.0, 40.0, 30.0, 60.0, 100.0, 150.0);
    glFlush ();           //forces previously issued commands to execute
}

void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

int main(int argc, char** argv)     //Because of glut, parameters are necessary
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (400, 150);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutMainLoop();
    return 0;
}

 

 

Example 5: Stippled Lines Code
//This program demonstrates geometric primitives and their attributes.

#include <GL/glut.h>
#include <stdlib.h>
#define drawOneLine(x1,y1,x2,y2)  glBegin(GL_LINES);  \             \\The line concatenation, \, is required

   glVertex2f ((x1),(y1)); glVertex2f ((x2),(y2)); glEnd();


void init(void)
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);
}

void display(void)
{
    int i;

    glClear (GL_COLOR_BUFFER_BIT);

    /* select white for all lines */
    glColor3f (1.0, 1.0, 1.0);
   
    /* in 1st row, 3 lines, each with a different stipple */
    glEnable (GL_LINE_STIPPLE);

    glLineStipple (1, 0x0101); /* dotted */
    drawOneLine (50.0, 125.0, 150.0, 125.0);
    glLineStipple (1, 0x00FF); /* dashed */
    drawOneLine (150.0, 125.0, 250.0, 125.0);
    glLineStipple (1, 0x1C47); /* dash/dot/dash */
    drawOneLine (250.0, 125.0, 350.0, 125.0);

    /* in 2nd row, 3 wide lines, each with different stipple */
    glLineWidth (5.0);
    glLineStipple (1, 0x0101); /* dotted */
    drawOneLine (50.0, 100.0, 150.0, 100.0);
    glLineStipple (1, 0x00FF); /* dashed */
    drawOneLine (150.0, 100.0, 250.0, 100.0);
    glLineStipple (1, 0x1C47); /* dash/dot/dash */
    drawOneLine (250.0, 100.0, 350.0, 100.0);
    glLineWidth (1.0);

    /* in 3rd row, 6 lines, with dash/dot/dash stipple */
    /* as part of a single connected line strip */
    glLineStipple (1, 0x1C47); /* dash/dot/dash */
    glBegin (GL_LINE_STRIP);
    for (i = 0; i < 7; i++)
        glVertex2f (50.0 + ((GLfloat) i * 50.0), 75.0);
    glEnd ();

    /* in 4th row, 6 independent lines with same stipple */
    for (i = 0; i < 6; i++)

    {
        drawOneLine (50.0 + ((GLfloat) i * 50.0), 50.0,
        50.0 + ((GLfloat)(i+1) * 50.0), 50.0);
    }

    /* in 5th row, 1 line, with dash/dot/dash stipple */
    /* and a stipple repeat factor of 5 */
    glLineStipple (5, 0x1C47); /* dash/dot/dash */
    drawOneLine (50.0, 25.0, 350.0, 25.0);

    glDisable (GL_LINE_STIPPLE);
    glFlush ();
}

void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

void keyboard(unsigned char key, int x, int y)
{
    switch (key)

    {
        case 27:
        exit(0);
        break;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (400, 150);
    glutInitWindowPosition (100, 100);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc(keyboard);
    glutMainLoop();
    return 0;
}

 

Example 6: Polygon Stipple

 

#include <GL/glut.h>
#include <stdlib.h>

void display(void)
{
    GLubyte fly[ ] =

    {
        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
        0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
        0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
        0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
        0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
        0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
        0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
        0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
        0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
        0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
        0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
        0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
        0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
        0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
        0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
        0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08};

    GLubyte halftone[ ] =

    {
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55,
        0xAA, 0xAA, 0xAA, 0xAA, 0x55, 0x55, 0x55, 0x55};

        glClear (GL_COLOR_BUFFER_BIT);
        glColor3f (1.0, 1.0, 1.0);

         /* draw one solid, unstippled rectangle, */
        /* then two stippled rectangles */
        glRectf (25.0, 25.0, 125.0, 125.0);
        glEnable (GL_POLYGON_STIPPLE);
        glPolygonStipple (fly);
        glRectf (125.0, 25.0, 225.0, 125.0);
        glPolygonStipple (halftone);
        glRectf (225.0, 25.0, 325.0, 125.0);
        glDisable (GL_POLYGON_STIPPLE);
   
        glFlush ();
    }

void init (void)
{
    glClearColor (0.0, 0.0, 0.0, 0.0);
    glShadeModel (GL_FLAT);
}

void reshape (int w, int h)
{
    glViewport (0, 0, (GLsizei) w, (GLsizei) h);
    glMatrixMode (GL_PROJECTION);
    glLoadIdentity ();
    gluOrtho2D (0.0, (GLdouble) w, 0.0, (GLdouble) h);
}

void keyboard(unsigned char key, int x, int y)
    {
    switch (key)

    {
        case 27:
        exit(0);
        break;
    }
}

int main(int argc, char** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize (350, 150);
    glutCreateWindow (argv[0]);
    init ();
    glutDisplayFunc(display);
    glutReshapeFunc(reshape);
    glutKeyboardFunc (keyboard);
    glutMainLoop();
    return 0;
}