Saturday, July 23, 2016

2D Model Using OpenGl/Glut/C++

 Final Output would be like this. I use code blocks IDE to create this. Hope this help to anyone  new to this area.

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

void start(){
    glClearColor(0.941176, 0.972549, 1, 0.0);
    glMatrixMode(GL_PROJECTION);
    gluOrtho2D(0.0, 800, 0.0, 600);
}

void createBox(int px1, int py1, int w, int h, float c1, float c2, float c3){

    //poss 1
    int possX1 = px1;
    int possY1 = py1;

    //poss 2
    int possX2 = px1;
    int possY2 = py1+h;

    //poss 3
    int possX3 = px1+w;
    int possY3 = py1+h;

    //poss 4
    int possX4 = px1+w;
    int possY4 = py1;

    glColor3f(c1, c2, c3);
    glBegin(GL_POLYGON);

    glVertex2i(possX1,possY1);
    glVertex2i(possX2,possY2);
    glVertex2i(possX3,possY3);
    glVertex2i(possX4,possY4);
    glEnd();
    glFlush();

}


void building(){

    glClear(GL_COLOR_BUFFER_BIT);


    //Flat Surface
    //createBox(10, 10, 700, 500, 0.827451, 0.827451, 0.827451);
    //glColor3f(0.196078, 0.803922, 0.196078);
    glColor3f(0.545098, 0.270588, 0.0745098);
    glBegin(GL_POLYGON);
    glVertex2i(0,0);
    glVertex2i(750,0);
    glVertex2i(550,250);
    glVertex2i(0, 250);
    glEnd();


    //Bottom box 1
    glColor3f(0.196078, 0.803922, 0.196078);
    glBegin(GL_POLYGON);
    glVertex2i(100,50);
    glVertex2i(100,250);
    glVertex2i(250,250);
    glVertex2i(250, 50);
    glEnd();

    //Box padding -
    glColor3f(0.827451, 0.827451, 0.827451);
    glBegin(GL_POLYGON);
    glVertex2i(100,250);
    glVertex2i(100,260);
    glVertex2i(250,260);
    glVertex2i(250,250);
    glEnd();

    //Bottom box 2
    glColor3f(1, 0.870588, 0.678431);
    glBegin(GL_POLYGON);
    glVertex2i(100,260);
    glVertex2i(100,370);
    glVertex2i(250,370);
    glVertex2i(250,260);
    glEnd();


    //Box padding -
    glColor3f(0.827451, 0.827451, 0.827451);
    glBegin(GL_POLYGON);
    glVertex2i(100,370);
    glVertex2i(100,380);
    glVertex2i(250,380);
    glVertex2i(250,370);
    glEnd();

    //Bottom box 3
    //Top
    glColor3f(0.333333, 0.333333, 0.333333);
    glBegin(GL_POLYGON);
    glVertex2i(100,380);
    glVertex2i(100,500);
    glVertex2i(250,500);
    glVertex2i(250,380);
    glEnd();

    //Coloumn 2
    //Bottom box 1 from bottom
    //Top
    glColor3f(1, 0.647059, 0);
    glBegin(GL_POLYGON);
    glVertex2i(250,50);
    glVertex2i(250,525);
    glVertex2i(425,525);
    glVertex2i(425,50);
    glEnd();

    //Coloumn 3
    //Bottom box 1 from bottom
    //Top
    glColor3f(0.333333, 0.333333, 0.333333);
    glBegin(GL_POLYGON);
    glVertex2i(425,50);
    glVertex2i(425,550);
    glVertex2i(525,550);
    glVertex2i(525,50);
    glEnd();


    //Create Window frame for building
    //bottom
    //1st building  1st- from bottom window
    createBox(138, 73, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(140, 75, 70, 70, 0.941176, 1, 0.941176);
    createBox(173, 73, 2, 70, 0.827451, 0.827451, 0.827451);

    //1st  building  2nd- from bottom window
    createBox(138, 163, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(140, 165, 70, 70, 0.941176, 1, 0.941176);
    createBox(173, 163, 2, 70, 0.827451, 0.827451, 0.827451);

    //1st  building  3rd- from bottom window
    createBox(138, 273, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(140, 275, 70, 70, 0.941176, 1, 0.941176);
    createBox(173, 273, 2, 70, 0.827451, 0.827451, 0.827451);

    //1st  building  4th- from bottom window
    createBox(138, 400, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(140, 402, 70, 70, 0.941176, 1, 0.941176);
    createBox(173, 400, 2, 70, 0.827451, 0.827451, 0.827451);

    //Second building bottom window
    createBox(300, 75, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(302, 77, 70, 70, 0.941176, 1, 0.941176);
    createBox(337, 77, 2, 70, 0.827451, 0.827451, 0.827451);

    //Second building 2nd window
    createBox(300, 275, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(302, 277, 70, 70, 0.941176, 1, 0.941176);
    createBox(337, 277, 2, 70, 0.827451, 0.827451, 0.827451);

    //Second building 3rd window
    createBox(300, 400, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(302, 400, 70, 70, 0.941176, 1, 0.941176);
    createBox(337, 400, 2, 70, 0.827451, 0.827451, 0.827451);

    //3rd building bottom window
    createBox(440, 75, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(442, 77, 70, 70, 0.941176, 1, 0.941176);
    createBox(476, 77, 2, 70, 0.827451, 0.827451, 0.827451);

    //3rd building bottom window
    createBox(440, 170, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(442, 172, 70, 70, 0.941176, 1, 0.941176);
    createBox(476, 170, 2, 70, 0.827451, 0.827451, 0.827451);


    //3rd building bottom window
    createBox(440, 265, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(442, 265, 70, 70, 0.941176, 1, 0.941176);
    createBox(476, 265, 2, 70, 0.827451, 0.827451, 0.827451);


    //3rd building bottom window
    createBox(440, 355, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(442, 355, 70, 70, 0.941176, 1, 0.941176);
    createBox(476, 355, 2, 70, 0.827451, 0.827451, 0.827451);


    //3rd building bottom window
    createBox(440, 450, 74, 74, 0.827451, 0.827451, 0.827451);
    createBox(442, 450, 70, 70, 0.941176, 1, 0.941176);
    createBox(476, 450, 2, 70, 0.827451, 0.827451, 0.827451);



    //Shadow
    //glColor3f(0.827451, 0.827451, 0.827451);
    glColor3f(0.517647, 0.517647, 0.517647);
    glBegin(GL_POLYGON);
    glVertex2i(100,50);
    glVertex2i(100,500);
    glVertex2i(50,400);
    glVertex2i(50,75);
    glEnd();


    //shadow lines
    //Box padding -
    glColor3f(0.827451, 0.827451, 0.827451);
    glBegin(GL_POLYGON);
    glVertex2i(100,370);
    glVertex2i(100,380);
    glVertex2i(50,300);
    glVertex2i(50,290);
    glEnd();

    //shadow lines
    //Box padding -
    glColor3f(0.827451, 0.827451, 0.827451);
    glBegin(GL_POLYGON);
    glVertex2i(100,250);
    glVertex2i(100,260);
    glVertex2i(50,190);
    glVertex2i(50,180);
    glEnd();


    //Shadow triangle
    glColor3f(0.517647, 0.517647, 0.517647);
    glBegin(GL_TRIANGLES);
    glVertex2i(250, 525);
    glVertex2i(250, 500);
    glVertex2i(240, 500);
    glEnd();

    //Shadow triangle
    glColor3f(0.517647, 0.517647, 0.517647);
    glBegin(GL_TRIANGLES);
    glVertex2i(425, 525);
    glVertex2i(425, 550);
    glVertex2i(415, 525);
    glEnd();



    glFlush();
}

int main(int argc, char ** argv)
{
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(100, 100);
    glutInitWindowSize(800, 600);
    glutCreateWindow("New Housing Schemes Model ");
    start();
    glutDisplayFunc(building);
    glutMainLoop();
}
Final Output

No comments:

Post a Comment