Posted By

Desoxena on 05/24/11


Tagged


Versions (?)

openGL particle system (Smoke stream)


 / Published in: C++
 

Very simple openGL particle system

  1. #include <time.h>
  2. #include <cmath>
  3. #include <Windows.h>
  4. #include <glut.h>
  5. #include <GL\GLU.h>
  6. float limits = .8;
  7. float X=0, Y=0;
  8. const int MAX_PARTICLES = 1000;
  9. const int MIN_PARTICLES = 10;
  10. int currentParticle = 1;
  11. float posX[MAX_PARTICLES], posY[MAX_PARTICLES];
  12. void moveParticles(int amount_of_particles) {
  13. srand (time (NULL) );
  14. float myX, myY;
  15. Sleep(3);
  16. glColor3d(2, .5, 0);
  17. for (int i = 0; i < amount_of_particles; i++) {
  18. myX = rand() % 3 + 1;
  19. if(myX==1 && posX[i]<=limits ){
  20. int mytemp = rand() % 100 + 1;
  21. int temp = rand() % 5 + 1;
  22. posX[i]+=temp*.001;
  23. posY[i]+=mytemp*.0004;
  24. }
  25. if(myX==2){posX[i]+=.00;posY[i]+=.01;}
  26. if(myX==3 && posX[i]>=-limits){
  27. int temp = rand() % 5 + 1;
  28. int mytemp = rand() % 100 + 1;
  29. posX[i]-=temp*.001;
  30. posY[i]+=mytemp*.0004;
  31. }
  32. ///////////////////////////////////////////
  33. if(posY[i]>=limits){
  34. posY[i]=0;
  35. posX[i]=0;
  36. }
  37. }
  38. }
  39. void Reshape(int height, int width) {
  40. glViewport(0, 0, width, height);
  41. glClearColor(0, 0, 0, 1);
  42. glMatrixMode(GL_PROJECTION);
  43. glLoadIdentity();
  44. gluPerspective(60, (float)height/(float)width, 1, 100);
  45. glMatrixMode(GL_MODELVIEW);
  46. }
  47. void Draw(void) {
  48.  
  49. int thingy = 1;
  50. bool check = false;
  51. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  52. /*
  53. glPushMatrix();
  54. glColor3d(.3, .1, 0);
  55. glTranslated(0, 0, -3);
  56. glutSolidCube(.3);
  57. glPopMatrix();
  58. */
  59. if (check == false) {
  60. float R, G, B;
  61. glPushMatrix();
  62. glBegin(GL_TRIANGLES);
  63. for (int i = 0; i < MAX_PARTICLES; i++) {
  64. R = rand() % 100 + 1;
  65. G = rand() % 100 + 1;
  66. B = rand() % 100 + 1;
  67. glColor3d(R*.01, G*.01, B*.01);
  68. //glColor3d(10, 10, 0);
  69. //glColor3d(0, 0+posY[i], 0);
  70. glVertex3f(X-.01, Y, -2);
  71. glVertex3f(X+.01, Y, -2);
  72. glVertex3f(X, Y+.02, -2);
  73. X = posX[i];
  74. Y = posY[i];
  75. }
  76. glEnd();
  77. glPopMatrix();
  78. check = true;
  79. }
  80. switch(thingy){
  81. case 1:
  82. Sleep(1);
  83. moveParticles(currentParticle);
  84. if (currentParticle != MAX_PARTICLES) {
  85. currentParticle++;
  86. }
  87. glutPostRedisplay();
  88. break;
  89. }
  90. glutSwapBuffers();
  91. }
  92. void Keyboard(unsigned char key, int x, int y) {
  93. switch(key){
  94. default:
  95. break;
  96. glutPostRedisplay();
  97. }
  98. }
  99. const GLfloat light_ambient[] = { 0.0f, 0.0f, 0.0f, 1.0f };
  100. const GLfloat light_diffuse[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  101. const GLfloat light_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  102. const GLfloat light_position[] = { 2.0f, 5.0f, 5.0f, 0.0f };
  103. const GLfloat mat_ambient[] = { 0.7f, 0.7f, 0.7f, 1.0f };
  104. const GLfloat mat_diffuse[] = { 0.8f, 0.8f, 0.8f, 1.0f };
  105. const GLfloat mat_specular[] = { 1.0f, 1.0f, 1.0f, 1.0f };
  106. const GLfloat high_shininess[] = { 100.0f };
  107. int main(int argc, char **argv) {
  108. glutInitDisplayMode(GLUT_DOUBLE);
  109. glutInit(&argc, argv);
  110. glutInitWindowSize(500, 500);
  111. glutInitWindowPosition(100, 100);
  112. glutCreateWindow("SOME OPENGL PROJECTS");
  113. glutReshapeFunc(Reshape);
  114. glutDisplayFunc(Draw);
  115. glutKeyboardFunc(Keyboard);
  116. glEnable(GL_CULL_FACE);
  117. glCullFace(GL_BACK);
  118. glEnable(GL_DEPTH_TEST);
  119. glDepthFunc(GL_LESS);
  120. glEnable(GL_LIGHT0);
  121. glEnable(GL_NORMALIZE);
  122. glEnable(GL_COLOR_MATERIAL);
  123. glEnable(GL_LIGHTING);
  124. glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
  125. glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
  126. glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
  127. glLightfv(GL_LIGHT0, GL_POSITION, light_position);
  128. glMaterialfv(GL_FRONT, GL_AMBIENT, mat_ambient);
  129. glMaterialfv(GL_FRONT, GL_DIFFUSE, mat_diffuse);
  130. glMaterialfv(GL_FRONT, GL_SPECULAR, mat_specular);
  131. glMaterialfv(GL_FRONT, GL_SHININESS, high_shininess);
  132. glutMainLoop();
  133. }

Report this snippet  

You need to login to post a comment.