Pages

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
 */




#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();
void init();

float aa[N];
float bb[N];
float ccc = 0.0;
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;
    ccc = b;
    switch(mouse){
        case GLUT_LEFT_BUTTON:
            if(state == GLUT_DOWN){

                glutPostRedisplay();
                cout << a << " " << b << endl;
            }
            break;
        case GLUT_RIGHT_BUTTON:
            if(state == GLUT_UP){

                particle();
                glutPostRedisplay();
            }
            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()*3.0/100000.00;
        }

}

void particle(){

    glPointSize(2.0);
    glColor3f(0.0, 0.0, 1.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.
            // hoz direction
            if(bbb<x[i]){
                x[i] += 0.01;
            }else if(bbb>x[i]){
                x[i] -= 0.01;
            }

            // vertical direction
            if(ccc<y[i]){
                y[i] += 0.01;
            }else if(ccc>y[i]){
                y[i] -= 0.01;
            }

            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;
}

No comments:

Post a Comment