Monday, 28 March 2016

Bresenham's Line Drawing Algorithm in OpenGL C++ code

//Author: Azhar Khan
//Rajasthan Technical University
//Poornima Institute of Engineering And Technology
#include<GL/glut.h>
#include<windows.h>
#include<stdio.h>
GLint x0,y0,xEnd,yEnd;
void init()
{

    glClearColor(1.0,1.0,1.0,0.0);
    glColor3f(1.0f,0.0f,0.0f);
    glPointSize(1.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,600.0,0.0,600.0);
}
void setPixel(GLint xcoordinate, GLint ycoordinate)
{
    glBegin(GL_POINTS);
    glVertex2i(xcoordinate,ycoordinate);
    glEnd();
    glFlush();
}
void lineBA(GLint x0,GLint y0,GLint xEnd,GLint yEnd)
{
    GLint dx = xEnd-x0;
    GLint dy = yEnd-y0;
    GLint steps,k;
    steps=dx;
    GLint x,y,p0=(2*dy)-dx;
    setPixel(x0,y0);
    x=x0;
    y=y0;
    for(k=0;k<steps;k++)
    {
        if(p0<0)
        {
            p0=p0+(2*dy);
            x+=1;
        }
        else
        {
            p0=p0+(2*dy)-(2*dx);
            x+=1;
            y+=1;
        }
        setPixel(x,y);
    }
}
void readInput()
{

    printf("\nEnter x0, y0, xEnd, yEnd(resp): ");
    scanf("%i %i %i %i",&x0,&y0,&xEnd,&yEnd);

}
void Display(void)
{
    //static int i=1;
    glClear(GL_COLOR_BUFFER_BIT);


    lineBA(x0,y0,xEnd,yEnd);
}
int main(int argc,char** argv)
{

    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE|GLUT_RGB);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(0,0);
    glutCreateWindow("Breshnam's Line Drawing Algorithm");
    readInput();
    glutDisplayFunc(Display);
    init();
    glutMainLoop();
    return 0;
}

Sunday, 27 March 2016

DDA algorithm implementation using OpenGL in C++

#include<GL/glut.h>
#include<windows.h>
#include<stdlib.h>
#include<stdio.h>
GLint x0,y0,xEnd,yEnd;
inline GLint round(const GLfloat a)
{
    return GLint(a+0.5);
}
void myInit(void)
{
    glClearColor(0.0,1.0,1.0,0.0);
    glColor3f(1.0f,0.0f,0.0f);
    glPointSize(3.0);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    gluOrtho2D(0.0,640.0,0.0,480.0);
}
void readInput()
{

    printf("Enter x0, y0, xEnd, yEnd(resp): ");
    scanf("%i %i %i %i",&x0,&y0,&xEnd,&yEnd);

}
void setPixel(GLint xcoordinate, GLint ycoordinate)
{
    glBegin(GL_POINTS);
    glVertex2i(xcoordinate,ycoordinate);
    glEnd();
    glFlush();
}

void lineDDA(GLint x0,GLint y0,GLint xEnd,GLint yEnd)
{
    GLint dx = abs(xEnd-x0);
    GLint dy = abs(yEnd-y0);
    GLint steps,k;
    GLfloat xIncrement,yIncrement,x=x0,y=y0;

    if(dx>dy)
        steps = dx;
    else
        steps = dy;

    xIncrement = GLfloat(dx) / GLfloat(steps);
    yIncrement = GLfloat(dy) / GLfloat(steps);
    setPixel(round(x),round(y));

    for(k=1;k<steps;k++)
    {
        x+= xIncrement;
        y+= yIncrement;
        setPixel(round(x),round(y));
    }
}

void Display(void)
{
    glClear(GL_COLOR_BUFFER_BIT);
    lineDDA(x0,y0,xEnd,yEnd);
}

int main(int argc,char *argv[])
{
    glutInit(&argc,argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(600,600);
    glutInitWindowPosition(50,50);
    glutCreateWindow("DDA Line Algorithum");
    readInput();
    glutDisplayFunc(Display);
    myInit();
    glutMainLoop();
    return EXIT_SUCCESS;
}