Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <GL/glut.h>
- #include <math.h>
- #include <stdlib.h>
- int MOUSEX;
- int MOUSEY;
- int PRESSED_LEFT = 0;
- int PRESSED_RIGHT = 0;
- //numarul de particule
- #define N 100
- struct particles {
- float x;
- float y;
- float dx;
- float dy;
- float color[3];
- float r;
- } *array, *i;
- void mouse(int, int, int, int);
- void mouseMotion(int, int);
- void display();
- void timer(int);
- int main(int argc, char **argv)
- {
- //alocam memorie pentru particule
- array = (struct particles *)malloc(N*sizeof(struct particles));
- if(!array)
- exit(1);
- srand(time(0));
- //dam coordonate, pas de miscare, marime si culori intimplatoare particulelor
- for(i = array; i < array+N; i++)
- {
- i->x = rand() % 500;
- i->y = rand() % 500;
- //de dx si dy va depinde viteza particulelor
- i->dx = rand() % 500 / 100.0 - 2.5; //-2.5 face ca aproximativ jumate din particule sa se miste
- i->dy = rand() % 500 / 100.0 - 2.5; //in directia opusa a celorlalte jumate
- i->color[0] = rand() % 200 / 200.0; //o valoare intr 0.0 si 1.0 rosu
- i->color[1] = rand() % 200 / 200.0; //verde
- i->color[2] = rand() % 300 / 300.0; //albastru
- i->r = rand() % 5 + 1;
- }
- glutInit(&argc, argv);
- glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
- glutInitWindowSize(500, 500);
- glutInitWindowPosition(100, 100);
- glutCreateWindow("Move Particles");
- //glClearColor(0.9, 0.9, 0.9, 0.9);
- glClearColor(0, 0, 0, 0);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- glOrtho(0.0, 500.0, 500.0, 0.0, 0.0, 1.0);
- glutDisplayFunc(display);
- timer(0);
- glutMouseFunc(mouse);
- glutMotionFunc(mouseMotion);
- glutMainLoop();
- }
- void mouse(int button, int state, int x, int y)
- {
- //preluam coordonatele mouse-ului in variabilele globale
- MOUSEX = x;
- MOUSEY = y;
- //verificam care buton este apasat
- if(button == GLUT_LEFT_BUTTON)
- PRESSED_LEFT = state == GLUT_DOWN;
- else if(button == GLUT_RIGHT_BUTTON)
- PRESSED_RIGHT = state == GLUT_DOWN;
- }
- //miscind mouse-ul cu un buton apasat se chama aceasta functie
- void mouseMotion(int x, int y)
- {
- MOUSEX = x;
- MOUSEY = y;
- }
- void timer(int p)
- {
- display();
- for(i = array; i < array+N; i++)
- {
- //nu dam voie particulelor sa iasa din limitele ferestrei
- if(i->x > 495 || i->x < 5)
- i->dx *= -1;
- if(i->y > 495 || i->y < 5)
- i->dy *= -1;
- //punem in miscare particulele
- i->x += i->dx;
- i->y += i->dy;
- //daca e apasat butonul sting, atragem particulele
- if(PRESSED_LEFT)
- {
- float dist = sqrt((i->x - MOUSEX)*(i->x - MOUSEX) + (i->y - MOUSEY)*(i->y - MOUSEY));
- i->x += 6 * (MOUSEX - i->x) / dist; //forta de atragere
- i->y += 6 * (MOUSEY - i->y) / dist;
- }
- //daca e apasat butonul drept, respingem particulele
- else if(PRESSED_RIGHT)
- {
- float dist = sqrt((i->x - MOUSEX)*(i->x - MOUSEX) + (i->y - MOUSEY)*(i->y - MOUSEY));
- i->x -= 7 * (MOUSEX - i->x) / dist; //forta de respingere mai mare ca cea de atragere
- i->y -= 7 * (MOUSEY - i->y) / dist;
- }
- }
- //timer() se va rechema fiecare 10 milisecunde
- glutTimerFunc(10, timer, 0);
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- //desenam particulele de diferite marimi si culori
- float k;
- for(i = array; i < array+N; i++)
- {
- glColor3f(i->color[0], i->color[1], i->color[2]);
- glBegin(GL_POLYGON);
- for(k = 0; k < 2*M_PI; k+=0.1)
- glVertex2f(i->r*cos(k)+i->x, i->r*sin(k)+i->y);
- glEnd();
- }
- glFlush();
- glutSwapBuffers();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement