Saturday, May 14, 2011

GL_LINE_STRIP example c c++ objc

[GL_LINE_STRIP example] Following code fragment shows a GL_LINE_STRIP example. You can specify what to draw in glBegin() ... glEnd() block. GL_LINE_STRIP  indicates that lines shape will be drawn in the block. [GL_LINE_STRIP example]

#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;
}