Saturday, June 15, 2013

Create Lines in OpenGL using C++

//Creating Lines
#include <GL/glut.h>

void draw() {
    //red = 0, green = 0, blue = 0
    //so background color is black
    glClearColor(0.0, 0.0, 0.0, 1.0);

    //glClear — clear buffers to preset values
    //GL_COLOR_BUFFER_BIT - Indicates the buffers currently enabled for color writing.
    glClear(GL_COLOR_BUFFER_BIT);

    /*Specifies the primitive or primitives that will be created from vertices
    presented between glBegin and the subsequent glEnd.
    Ten symbolic constants are accepted:
    GL_POINTS - Treats each vertex as a single point,
    GL_LINES - Treats each pair of vertices as an independent line segment,
    GL_LINE_STRIP - Draws a connected group of line segments from the first vertex to the last,
    GL_LINE_LOOP - Draws a connected group of line segments from the first vertex to the last, then back to the first,
    GL_TRIANGLES - Treats each triplet of vertices as an independent triangle,
    GL_TRIANGLE_STRIP - Draws a connected group of triangles.,
    GL_TRIANGLE_FAN - Draws a connected group of triangles, one triangle is defined for each vertex presented after the first two vertices,
    GL_QUADS - Treats each group of four vertices as an independent quadrilateral,
    GL_QUAD_STRIP - Draws a connected group of quadrilaterals. One quadrilateral is defined for each pair of vertices presented after the first pair,
    GL_POLYGON - Draws a single, connvex polygon
    */

    glBegin(GL_LINES);
        glVertex3f(0.0, 0.0, 0.0); //(x, y, z) 1st vertex
        glVertex3f(1.0, 1.0, 0.0); //(x, y, z) 2nd vertex
    glEnd();

    //glFlush — force execution of GL commands in finite time
    glFlush();
}

int main(int argc, char **argv){

    //glutInit is used to initialize the GLUT library.
    glutInit(&argc, argv);

    //glutInitDisplayMode sets the initial display mode.
    //GLUT_SINGLE - Bit mask to select a single buffered window.
    //This is the default if neither GLUT_DOUBLE or GLUT_SINGLE are specified.
    glutInitDisplayMode(GLUT_SINGLE);

    //Configure Window Postion
    glutInitWindowPosition(50, 25); //x, y

    //Configure Window Size
    glutInitWindowSize(500, 300); //width, height

    //glutCreateWindow - creates a top level window
    //parameter is a string and it is the window title
    glutCreateWindow("My first Program in OpenGL");

    //glutDisplayFunc sets the display callback for the current window.
    glutDisplayFunc(draw);

    //glutMainLoop enters the GLUT event processing loop.
    glutMainLoop();


    return 0;
}



No comments:

Post a Comment