Search This Blog

Loading...

OpenGL program to draw a line

Below is the simple OpenGL Computer Graphics Program to draw Line. This is very basic OpenGL program, You can compare this with C "Hello World" program.

// It specifies value to clear color buffer.

// initialize current matrix

// Used to define orthographic projection of 2D matrix

// clear preset values of buffer

//set colour

//  delimit the vertices of a primitive or a group of like primitives

//  delimit the vertices of a primitive or a group of like primitives 

// delimit the vertices of a primitive or a group of like primitives

// Used to set initial display mode. 

//it sets the size of window and position of window. 
// it sets display callback for current window

OpenGL C Program to draw Line:
Title#include <GL/glut.h>

void initialize2D( float r, float g, float b)
{
    glClearColor(r,g,b,0.0);
    glMatrixMode (GL_PROJECTION);
    gluOrtho2D (0.0, 205.0, 0.0, 155.0);
}

void displayLine(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    glColor3f(1.0, 0.0, 0.0);

    glBegin(GL_POINTS);
    for(int j = 0; j < 10; j++)
    {
        glVertex2i(10+5*j,110);
    }
    glEnd();

    glBegin(GL_LINES);
        glVertex2i(10,10);
        glVertex2i(100,100);
    glEnd();

    glFlush();
}

void main(int iargc,char *iargv[])
{
    glutInit(&iargc,iargv);
    glutInitDisplayMode (GLUT_SINGLE | GLUT_RGB); // initialize Display Mode
    glutInitWindowSize (500, 500); // initialize window size
    glutInitWindowPosition (100, 100); // initialize window position
    glutCreateWindow ("points and lines");
    initialize2D(0.0,0.0,0.0);
    glutDisplayFunc(displayLine); // call displayLine() function
    glutMainLoop();
}

No comments:

Post a Comment