Pages

Showing posts with label OpenGL. Show all posts
Showing posts with label OpenGL. Show all posts

Monday, March 28, 2011

OpenGL - Gridding

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

using namespace std;

#define WIDTH 400
#define HEIGHT 300
#define N 10
float dens[N][N] = {};
void init(){
    glClearColor(1.0, 1.0, 1.0,1.0);
    //glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    //glOrtho(0, WIDTH, HEIGHT, 0, 0, 1);
    //glMatrixMode(GL_MODELVIEW);
    //gluOrtho2D(0.0,1.0,0.0,1.0);
    //glDisable(GL_DEPTH_TEST);

}



void display(){
    glClear(GL_COLOR_BUFFER_BIT);
    glPointSize(3.0);
    glBegin(GL_QUADS);
    float h = 1.00/N;
    for(int i=0; i<N; i++){
        float x = 1.0*i/N;
        for(int j=0; j<N; j++){
            float y = 1.0*j/N;
            glColor3f(1.0,0.0,0.0);
            glVertex2f(x, y);
            glColor3f(0.0,1.0,0.0);
            glVertex2f(x+h, y);
            glColor3f(0.0,0.0,1.0);
            glVertex2f(x+h, y+h);
            glColor3f(0.0,1.0,1.0);
            glVertex2f(x, y+h);
        }
    }
    glEnd();
    glFlush();
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(WIDTH, HEIGHT);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("Lucia OpenGL");

    glClearColor(1.0, 1.0, 1.0, 0.0);
    //glMatrixMode(GL_PROJECTION);
    //glLoadIdentity();
    gluOrtho2D(0.0,1.0,0.0,1.0);
    glutDisplayFunc(display);
    glutMainLoop();

    return 0;
}

Tuesday, March 22, 2011

OpenGL - Dragging Single Object


#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;

#define WIDTH 400.00
#define HEIGHT 300.00

typedef struct point{
    float x;
    float y;
}point;

point titik[4];


void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(0,1,0,1);

    titik[0].x = 0.1;
    titik[0].y = 0.1;

    titik[1].x = 0.2;
    titik[1].y = 0.1;

    titik[2].x = 0.2;
    titik[2].y = 0.2;

    titik[3].x = 0.1;
    titik[3].y = 0.2;
}

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

    glBegin(GL_QUADS);
        glVertex2f(titik[0].x, titik[0].y);
        glVertex2f(titik[1].x, titik[1].y);
        glVertex2f(titik[2].x, titik[2].y);
        glVertex2f(titik[3].x, titik[3].y);
    glEnd();

    glFlush();
}

void keyboard(unsigned char key, int x, int ){
    switch(key){
        case 27:
            exit(1);
            break;
    }
}

void mouse(int mouse, int state, int x, int y){
    switch(mouse){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){

            }
            break;
    }
}


void motion(int x, int y){

    float xx = x / WIDTH;
    titik[0].x = xx;
    titik[1].x = xx + 0.1;
    titik[2].x = xx + 0.1;
    titik[3].x = xx;

    float yy = y / HEIGHT;

    yy = 1 - yy;

    titik[0].y = yy;
    titik[1].y = yy;
    titik[2].y = yy + 0.1;
    titik[3].y = yy + 0.1;

    cout << xx << endl;
    glutPostRedisplay();
}

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

    system("color 2f");

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(WIDTH,HEIGHT);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);
    glutMouseFunc(mouse);

    glutMainLoop();

    return 0;
}

Sunday, March 20, 2011

OpenGL - Dragging Points

/**
 * This program will do the particle generation. Then directs the
 * particles move as the mouse moves.
 * The hope of the program is that the particle will move to the
 * right direction if the particle position
 * lies in left side than the mouse cursor. And reciprocally.
 *
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 1.0
 * @date     : March 20, 2011
 */


#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;
typedef struct{
    float x;
    float y;
}point;

#define nn 1000
point titik[nn];

// record the previous state
int prev_x = 0, prev_y=0;

void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(0,400,0,300);

    srand((unsigned)time(NULL));
    for(int i=0;i<nn;i++){
        titik[i].x = rand()/100;
        titik[i].y = rand()/100;
    }
}

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

    glPointSize(2.0);
    glBegin(GL_POINTS);
        for(int i=0; i<nn; i++){
            glVertex2f(titik[i].x, titik[i].y);
        }
    glEnd();

    glFlush();
}

void keyboard(unsigned char key, int x, int ){
    switch(key){
        case 27:
            exit(1);
            break;
    }
}


void motion(int x, int y){

    // horizontal dragging
    if(x > prev_x){
        for(int i=0; i<nn; i++){
            titik[i].x++;
        }
    }else if(x < prev_x){
         for(int i=0; i<nn; i++){
            titik[i].x--;
         }
    }

    // vertical dragging: reverse
    if(y < prev_y){
        for(int i=0; i<nn; i++){
            titik[i].y++;
        }
    }else if(y > prev_y){
        for(int i=0; i<nn; i++){
            titik[i].y--;
        }
    }

    prev_x = x;
    prev_y = y;

    glutPostRedisplay();
}

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

    system("color 2f");

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

    init();
    glutDisplayFunc(display);
    glutKeyboardFunc(keyboard);
    glutMotionFunc(motion);

    glutMainLoop();

    return 0;
}

Thursday, March 10, 2011

OpenGL - Centering The Particles

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>
#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;

void init();
void display();

#define N 10000

float xx[N];
float yy[N];

void setRand(){
    srand((unsigned) time(NULL));
    int i = 0;
    for(i=0; i< N; i++){
        xx[i] = rand() / 100.00;
        yy[i] = rand() / 100.00;
    }
}

void setPoints(){
    glBegin(GL_POINTS);
        glColor3f(1.0,0.0,0.0);
        int i = 0;
        for(i=0;i<N;i++){
            glVertex2f(xx[i], yy[i]);
        }
    glEnd();
}

void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(0.0, 400.0, 0.0, 300.0);
}

void display(){
    glClear( GL_COLOR_BUFFER_BIT);
    glPointSize(2.0);

    setPoints();


    glFlush();
}

void mouse(int mouse, int state, int x, int y){
    switch(mouse){
        case GLUT_RIGHT_BUTTON:
            if(state == GLUT_DOWN){
                cout << x << " -> " << y << endl;

                setRand();
                glutPostRedisplay();
            }
        break;

    }
}

void drag(int x, int y){
    y = 300.0 - y;

    int a = 0;
                for(a=0;a<N;a++){
                    if(xx[a] < x){
                        xx[a] += 1;
                    }
                    if(xx[a] > x){
                        xx[a] -= 1;
                    }
                    if(yy[a] < y){
                        yy[a] += 1;
                    }
                    if(yy[a] > y){
                        yy[a] -= 1;
                    }
                }

    glutPostRedisplay();
}

int main(int argc, char* argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("OpenGL Program");

    init();
    setRand();

    glutDisplayFunc(display);
    glutMotionFunc(drag);
    glutMouseFunc(mouse);



    glutMainLoop();

    return 0;

}

Monday, March 7, 2011

OpenGL - Break Away The Particles In 2D

/**
 * This program will do the particle generation. 
 * Then directs the particles move as the mouse moves.
 * The hope of the program is that the particle will move 
 * to the right direction if the particle position
 * lies in left side than the mouse cursor. And reciprocally.
 *
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 3.0
 * @date     : March 7, 2011
 */


OpenGL - Break Away The Particles In 1D

/**
 * This program will do the particle generation. 
 * Then directs the particles move as the mouse moves.
 * The hope of the program is that the particle will move
 * to the right direction if the particle position
 * lies in left side than the mouse cursor. And reciprocally.
 *
 * @author   : irfanudin ridho
 * @email    : irfan.ub@gmail.com
 * @version  : 3.0
 * @date     : March 7, 2011
 */
 

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;


#define N 200 // particle size
void grid();
void particle(float p);

float aa[N];
float bb[N];
float bbb = 0.0;

void mouse(int mouse, int state, int x, int y){

    float a = x/1000.00;
    float b = y/1000.00;
    bbb = a;
    switch(mouse){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){

                glutPostRedisplay();
                cout << a << " " << b << endl;
            }
            break;
    }
}

void grid(){
    int i = 0, j = 0;
    glColor3f(0.89,0.99,0.79);
    glBegin(GL_LINES);
        //hoz line
        for(i=0; i<= 10; i++){
            glVertex2f(-0.1, i/10.0);
            glVertex2f(1.0, i/10.0);
        }
        //verical line
        for(j=0; j<=10; j++){
            glVertex2f(j/10.0, -0.1);
            glVertex2f(j/10.0, 1.0);
        }

        // coordinate line
        glColor3f(0.0,1.0,0.0);
        //hoz line
        glVertex2f(-0.1, 0.0);
        glVertex2f(1.0, 0.0);
        // vertical line
        glVertex2f(0.0, -0.1);
        glVertex2f(0.0, 1.0);


    glEnd();
}




void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(-0.1,1,-0.1,1.0);

    // creating position of the particles
    // starting position of the particle
        srand((unsigned) time(NULL));
        int i=0;
        for(i;i<N;i++){
            aa[i] = rand()*4.0/100000.00;
            bb[i] = rand()*5.0/100000.00;
        }

}

void particle(){

    glPointSize(2.0);
    glColor3f(0.0, 0.0, 0.0);

    float *x = aa;
    float *y = bb;

    glBegin(GL_POINTS);
        int i=0;
        for(i;i<N;i++){
            x[i] ;
            y[i] ;

            // if mouse position is greater than
            // particle position, move the particle right away.
            if(bbb<x[i]){
                x[i] += 0.01;
            }else if(bbb>x[i]){
                x[i] -= 0.01;
            }
            cout << "bbb: " << bbb << endl;

            glVertex2f(x[i], y[i]);
        }

    glEnd();


}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    // particle rendering
    particle();
    grid();

    glFlush();

}


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


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

    init();
    glutDisplayFunc(display);
    glutMouseFunc(mouse);

    glutMainLoop();



    return 0;
}

OpenGL - Particle Moving

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <ctime>
#include <cstdlib>

#include <iostream>

using namespace std;

#define N 2000 // particle size

void grid();
void particle();

float aa[N];
float bb[N];

void mouse(int mouse, int state, int x, int y){
    float a = x/1000.00;
    float b = y/1000.00;
    switch(mouse){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){
                particle();
                glutPostRedisplay();
                cout << a << " " << b << endl;
            }
            break;
    }
}

void grid(){
    int i = 0, j = 0;
    glColor3f(0.89,0.99,0.79);
    glBegin(GL_LINES);
        //hoz line
        for(i=0; i<= 10; i++){
            glVertex2f(-0.1, i/10.0);
            glVertex2f(1.0, i/10.0);
        }
        //verical line
        for(j=0; j<=10; j++){
            glVertex2f(j/10.0, -0.1);
            glVertex2f(j/10.0, 1.0);
        }

        // coordinate line
        glColor3f(0.0,1.0,0.0);
        //hoz line
        glVertex2f(-0.1, 0.0);
        glVertex2f(1.0, 0.0);
        // vertical line
        glVertex2f(0.0, -0.1);
        glVertex2f(0.0, 1.0);

    glEnd();
}

void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(-0.1,1,-0.1,1.0);

    // creating position of the particles
        srand((unsigned) time(NULL));
        int i=0;
        for(i;i<N;i++){
            aa[i] = rand()*4.0/100000.00;
            bb[i] = rand()*5.0/100000.00;
        }

}

/**
 * You can also this code inside display function directly
 */
void particle(){

    glPointSize(1.0);
    glColor3f(0.0, 0.0, 0.0);

    float *x = aa;
    float *y = bb;

    glBegin(GL_POINTS);
        int i=0;
        for(i;i<N;i++){
            x[i] ;
            y[i] ;

            x[i] += 0.01;
            y[i] += 0.03;

            glVertex2f(x[i], y[i]);
        }

    glEnd();


}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    particle();
    grid();

    glFlush();
    cout << "lol" << endl;
}


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


    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

    init();
    glutDisplayFunc(display);
   
    glutMouseFunc(mouse);

    glutMainLoop();

    return 0;
}

OpenGL - Particle Generation

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <ctime>
#include <cstdlib>

#include <iostream>
using namespace std;

#define PARTICLE_SIZE 2000


void grid(){
    int i = 0, j = 0;
    glColor3f(0.89,0.99,0.79);
    glBegin(GL_LINES);
        //hoz line
        for(i=0; i<= 10; i++){
            glVertex2f(-0.1, i/10.0);
            glVertex2f(1.0, i/10.0);
        }
        //verical line
        for(j=0; j<=10; j++){
            glVertex2f(j/10.0, -0.1);
            glVertex2f(j/10.0, 1.0);
        }

        // coordinate line
        glColor3f(0.0,1.0,0.0);
        //hoz line
        glVertex2f(-0.1, 0.0);
        glVertex2f(1.0, 0.0);
        // vertical line
        glVertex2f(0.0, -0.1);
        glVertex2f(0.0, 1.0);


    glEnd();
}

void particle(){
    glPointSize(1.0);
    glColor3f(0.0, 0.0, 0.0);

    glBegin(GL_POINTS);
        srand((unsigned) time(NULL));
        int i=0;
        for(i;i<PARTICLE_SIZE;i++){
            float x = rand()*4.0/100000.00;
            float y = rand()*5.0/100000.00;
            glVertex2f(x, y);
            //cout << x << " " << y << endl;
        }
    glEnd();
}

void init(){
    glClearColor(1.0,1.0,1.0,1.0);
    gluOrtho2D(-0.1,1,-0.1,1.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);

    particle();
    grid();

    glFlush();
}


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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_RGB | GLUT_SINGLE);
    glutInitWindowSize(400,300);
    glutInitWindowPosition(200,100);
    glutCreateWindow("Simulation App");

    init();
    glutDisplayFunc(display);
    glutIdleFunc(display);

    glutMainLoop();



    return 0;
}

Wednesday, March 2, 2011

OpenGL - Dragging Object Within It

/**
 * this program moving the geometric object with the mouse dragging
 *
 * @author  : irfanudin ridho
 * @email   : irfan.ub@gmail.com
 * @version : alpha
 */


#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <iostream>

using namespace std;
float* draw(float x=100.0, float y=100.0);

void init(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(-1.0, 400.0, -1.0, 300.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
}

void motion(int x, int y){

    float* bb;

    bb = draw();

    if(bb[0] < x && x < bb[0]+100 && bb[1] < y && y < bb[1]+100){
        y = 300 - y;
        float xx = (float) x/1.00;
        float yy = (float) y/1.00;
        draw(xx,yy);

        glutPostRedisplay();

        cout << x << " " << y << endl;
    }
}

float* draw(float x, float y){
    glColor3f(1.0, 0.5, 0.1);
    glRectf(x, y, x+100.0, y+100.0);
    glFinish();
    float b[] = {x,y};
    return b;
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 300);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("iPod OpenGL");
    init();
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();

    return 0;
}

OpenGL - Dragging Object

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <iostream>

using namespace std;
void draw(float, float);

void init(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(-1.0, 400.0, -1.0, 300.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
}

void motion(int x, int y){
    y = 300 - y;
    draw(x/1.0, y/1.0);
    glutPostRedisplay();

    cout << x << " " << y << endl;
}

void draw(float x, float y){
    glColor3f(1.0, 0.5, 0.1);
    glRectf(x, y, x-100.0, y-50.0);  // x1, y1, x2, y2
    glFinish();
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 300);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("iPod OpenGL");
    init();
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();

    return 0;
}

OpneGL - Mouse Dragging

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <iostream>

using namespace std;
void draw(float, float);

void init(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(-1.0, 400.0, -1.0, 300.0);
}

void display(){
    glClear(GL_COLOR_BUFFER_BIT);
}

void motion(int x, int y){
    /**
     * The value of y must be converted, because the difference 
     * of coordinate space system.
     */     
    y = 300 - y;          
    draw(x/1.0, y/1.0);
    glutPostRedisplay();

    cout << x << " " << y << endl;
}

void draw(float x, float y){
    glColor3f(3.0, 0.9, 0.7);
    glRectf(10.0, 10.0, x,y);
    glFinish();
}

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

    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowSize(400, 300);
    glutInitWindowPosition(200, 100);
    glutCreateWindow("iPod OpenGL");
    init();
    glutDisplayFunc(display);
    glutMotionFunc(motion);

    glutMainLoop();

    return 0;
}

OpenGL - Between Green And Blue

/**
 * This function must be called by glutMotionFunc(motion)
 */

void motion(int x, int y){
    float xx  = (x/400.00);
    float yy = (y/300.00);
    glClearColor(0.0, xx, yy, 1.0);
    glutPostRedisplay();
}

OpenGL - Between Red and Blue

void motion(int x, int y){
    float xx  = (x/400.00);        // must be float divider
    float yy = (y/300.00);         // must be float divider
    glClearColor(xx, 0.0, yy, 1.0);
    glutPostRedisplay();
}

OpenGL - Between Red and Green

void motion(int x, int y){
    float xx  = (x/400.00);      // must be float divider
    float yy = (y/300.00);       // must be float divider
    glClearColor(xx, yy, 0.0, 1.0);
    glutPostRedisplay();
}
/**
 * This snippet of code will change your background color from
 * pure of black to pure of white when your drag has been reached
 * 1000 event. This event will invoked when you drag your mouse button
 */

void motion(int x, int y){
    static float i = 0;
    glClearColor(i/1000, i/1000, i/1000, 1.0);
    glutPostRedisplay();
    i++;
}

Tuesday, March 1, 2011

OpenGL - Backgrouning Using Keyboard

/**
 * This function will backgrouning the background based on
 * the key you type.
 * for example, if you type y, you will get the yellow background
 */

void keyboard(unsigned char key, int x, int y){

    switch(key){
        case 'r':
            glClearColor(1.0, 0.0, 0.0, 1.0);
            glutPostRedisplay();              // refresh window
            break;
        case 'b':
            glClearColor(0.0, 0.0, 0.0, 1.0);
            glutPostRedisplay();
            break;
        case 'g':
            glClearColor(0.0, 1.0, 0.0, 1.0);
            glutPostRedisplay();
            break;
        case 'y':
            glClearColor(1.0, 1.0, 0.0, 1.0);
            glutPostRedisplay();
            break;
        case 'm':
            glClearColor(1.0, 0.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'a':
            glClearColor(0.0, 1.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'u':
            glClearColor(0.0, 0.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'w':
            glClearColor(1.0, 1.0, 1.0, 1.0);
            glutPostRedisplay();
            break;
        case 'h':
            cout << "Enter a key on keyboard to backgrouning!" << endl;
            cout << "a -> aqua" << endl;
            cout << "r -> red" << endl;
            cout << "b -> blue" << endl;
            cout << "g -> green" << endl;
            cout << "y -> yellow" << endl;
            cout << "m -> magenta" << endl;
            cout << "b -> black" << endl;
            cout << "w -> white" << endl;
            break;
    }
}

Monday, February 28, 2011

OpenGL - Coordinate System

/**
 * This program will draw coordinate system line
 * x line and y line with their corresponding strips
 */
 
#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#define LIMIT 20.0      // limit
#define CENTER (LIMIT - LIMIT - 1)


// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object

void createCoordinate(){
    glColor3f(1.0, 0.0, 0.0);
    glBegin(GL_LINES);
        // horizontal lines
        glVertex2f(-LIMIT, 0.0);
        glVertex2f(LIMIT, 0.0);

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    glEnd();

    // creating strips
    int i=0;
    for(i=0;i<20;i++){
    glColor3f(0.1, 0.0, 1.0);
    glBegin(GL_LINES);
        // horizontal strips
        glVertex2f(i, 0.10);
        glVertex2f(i, -0.10);

        // vertical strips
        glVertex2f(0.10, i);
        glVertex2f(-0.10, i);
    glEnd();
    }
}

// initialization setting
void setup(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(CENTER, LIMIT, CENTER, LIMIT); // -x1, x2, -y1, y2
}

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


    createCoordinate();
    draw();


    glFlush();
}

// drawing area
void draw(){
    // your code here
}


int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(200, 100);
    glutInitWindowSize(400, 300);
    glutCreateWindow("Hello World");
    glutDisplayFunc(display);

    setup();
    glutMainLoop();


    return 0;

}

OpenGL - Histogram

#include <GL/gl.h>
#include <GL/glu.h>
#include <GL/glut.h>

#include <iostream>
using namespace std;

#define LIMIT 20.0      // limit
#define CENTER (LIMIT - LIMIT - 1)


// prototype
void setup();   // initialization of the program
void display(); // drawing method
void createCoordinate();  // certasian coordinate
void draw();    // draw the object
void createBox(float, float, float, float);

void setup(){
    glClearColor(1.0, 1.0, 1.0, 1.0);
    gluOrtho2D(CENTER, LIMIT, CENTER, LIMIT); // -x1, x2, -y1, y2
}

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

    draw();
    createCoordinate();

    glFlush();
}


void draw(){

    createBox(1.0, 1.0, 2.0, 10.0);
    createBox(4.0, 1.0, 2.0, 15.0);
    createBox(7.0, 1.0, 2.0, 13.0);
    createBox(10.0, 1.0, 2.0, 9.0);
    createBox(13.0, 1.0, 2.0, 7.0);
    createBox(16.0, 1.0, 2.0, 10.0);
   
}

void createBox(float x, float y, float width, float height){
    glBegin(GL_POLYGON);
        glVertex2f(x, y);
        glVertex2f(x, y+height);
        glVertex2f(x+width, y+height);
        glVertex2f(x+width, y);
    glEnd();
}

void createCoordinate(){
    glBegin(GL_LINES);
        // horizontal lines
        glVertex2f(-LIMIT, 0.0);
        glVertex2f(LIMIT, 0.0);

        // vertical lines
        glVertex2f(0.0, -LIMIT);
        glVertex2f(0.0, LIMIT);
    glEnd();
}

int main(int argc, char *argv[]){
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
    glutInitWindowPosition(200, 100);
    glutInitWindowSize(400, 300);
    glutCreateWindow("Hello World");
    glutDisplayFunc(display);

    setup();
    glutMainLoop();



    return 0;

}