GMgKe586q6suSQnyqZLlGCooeWM

Pages

Search

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 1000

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_LEFT_BUTTON:
            if(state == GLUT_DOWN){
                cout << x << " -> " << y << endl;

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

                glutPostRedisplay();
            }
        break;

    }
}

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



    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;

}

Share/Bookmark

No comments:

Post a Comment