Pages

Monday, March 7, 2011

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

No comments:

Post a Comment