Advertisement
GuessGen

Particle attraction by mouse C with OpenGL

Jul 10th, 2012
345
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.38 KB | None | 0 0
  1. #include <GL/glut.h>
  2. #include <math.h>
  3. #include <stdlib.h>
  4.  
  5. int MOUSEX;
  6. int MOUSEY;
  7. int PRESSED_LEFT = 0;
  8. int PRESSED_RIGHT = 0;
  9.  
  10. //numarul de particule
  11. #define N   100
  12.  
  13. struct particles {
  14.     float x;
  15.     float y;
  16.     float dx;
  17.     float dy;
  18.     float color[3];
  19.     float r;
  20. } *array, *i;
  21.  
  22. void mouse(int, int, int, int);
  23. void mouseMotion(int, int);
  24. void display();
  25. void timer(int);
  26.  
  27. int main(int argc, char **argv)
  28. {
  29.     //alocam memorie pentru particule
  30.     array = (struct particles *)malloc(N*sizeof(struct particles));
  31.     if(!array)
  32.         exit(1);
  33.  
  34.     srand(time(0));
  35.     //dam coordonate, pas de miscare, marime si culori intimplatoare particulelor
  36.     for(i = array; i < array+N; i++)
  37.     {
  38.         i->x = rand() % 500;
  39.         i->y = rand() % 500;
  40.  
  41.         //de dx si dy va depinde viteza particulelor
  42.         i->dx = rand() % 500 / 100.0 - 2.5; //-2.5 face ca aproximativ jumate din particule sa se miste
  43.         i->dy = rand() % 500 / 100.0 - 2.5; //in directia opusa a celorlalte jumate
  44.  
  45.         i->color[0] = rand() % 200 / 200.0; //o valoare intr 0.0 si 1.0 rosu
  46.         i->color[1] = rand() % 200 / 200.0; //verde
  47.         i->color[2] = rand() % 300 / 300.0; //albastru
  48.  
  49.         i->r = rand() % 5 + 1; 
  50.     }
  51.  
  52.     glutInit(&argc, argv);
  53.     glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
  54.     glutInitWindowSize(500, 500);
  55.     glutInitWindowPosition(100, 100);
  56.     glutCreateWindow("Move Particles");
  57.  
  58.     //glClearColor(0.9, 0.9, 0.9, 0.9);
  59.     glClearColor(0, 0, 0, 0);
  60.     glMatrixMode(GL_PROJECTION);
  61.     glLoadIdentity();
  62.     glOrtho(0.0, 500.0, 500.0, 0.0, 0.0, 1.0);
  63.  
  64.     glutDisplayFunc(display);
  65.     timer(0);
  66.     glutMouseFunc(mouse);
  67.     glutMotionFunc(mouseMotion);
  68.  
  69.     glutMainLoop();
  70. }
  71.  
  72. void mouse(int button, int state, int x, int y)
  73. {
  74.     //preluam coordonatele mouse-ului in variabilele globale
  75.     MOUSEX = x;
  76.     MOUSEY = y;
  77.  
  78.     //verificam care buton este apasat
  79.     if(button == GLUT_LEFT_BUTTON)
  80.         PRESSED_LEFT = state == GLUT_DOWN;
  81.     else if(button == GLUT_RIGHT_BUTTON)
  82.         PRESSED_RIGHT = state == GLUT_DOWN;
  83. }
  84.  
  85. //miscind mouse-ul cu un buton apasat se chama aceasta functie
  86. void mouseMotion(int x, int y)
  87. {
  88.     MOUSEX = x;
  89.     MOUSEY = y;
  90. }
  91.  
  92. void timer(int p)
  93. {
  94.     display();
  95.  
  96.     for(i = array; i < array+N; i++)
  97.     {
  98.         //nu dam voie particulelor sa iasa din limitele ferestrei
  99.         if(i->x > 495 || i->x < 5)
  100.             i->dx *= -1;
  101.         if(i->y > 495 || i->y < 5)
  102.             i->dy *= -1;
  103.  
  104.         //punem in miscare particulele
  105.         i->x += i->dx;
  106.         i->y += i->dy; 
  107.  
  108.         //daca e apasat butonul sting, atragem particulele
  109.         if(PRESSED_LEFT)
  110.         {
  111.             float dist = sqrt((i->x - MOUSEX)*(i->x - MOUSEX) + (i->y - MOUSEY)*(i->y - MOUSEY));
  112.             i->x += 6 * (MOUSEX - i->x) / dist; //forta de atragere
  113.             i->y += 6 * (MOUSEY - i->y) / dist;
  114.         }
  115.    
  116.         //daca e apasat butonul drept, respingem particulele
  117.         else if(PRESSED_RIGHT)
  118.         {
  119.             float dist = sqrt((i->x - MOUSEX)*(i->x - MOUSEX) + (i->y - MOUSEY)*(i->y - MOUSEY));
  120.             i->x -= 7 * (MOUSEX - i->x) / dist; //forta de respingere mai mare ca cea de atragere
  121.             i->y -= 7 * (MOUSEY - i->y) / dist;
  122.         }
  123.     }
  124.  
  125.     //timer() se va rechema fiecare 10 milisecunde
  126.     glutTimerFunc(10, timer, 0);
  127. }
  128.  
  129. void display()
  130. {
  131.     glClear(GL_COLOR_BUFFER_BIT);
  132.  
  133.     //desenam particulele de diferite marimi si culori
  134.     float k;
  135.     for(i = array; i < array+N; i++)
  136.     {
  137.         glColor3f(i->color[0], i->color[1], i->color[2]);
  138.         glBegin(GL_POLYGON);
  139.         for(k = 0; k < 2*M_PI; k+=0.1)
  140.             glVertex2f(i->r*cos(k)+i->x, i->r*sin(k)+i->y);
  141.         glEnd();
  142.     }
  143.    
  144.  
  145.     glFlush();
  146.     glutSwapBuffers();
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement