Advertisement
Guest User

Untitled

a guest
Jun 17th, 2019
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.23 KB | None | 0 0
  1. #undef UNICODE
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <list>
  6. #define GLUT_DISABLE_ATEXIT_HACK
  7.  
  8. #include <math.h>
  9. #include <ctime>
  10. #include "GL/glut.h"
  11. #include <GL/gl.h>
  12. #include "teksture.h"
  13. //#include <math.h>
  14.  
  15. using namespace std;
  16.  
  17. static double x, y;
  18. static double dx = 1;
  19. static double dy = 0.5;
  20. static double cr = 0;
  21.  
  22. struct SParticle
  23. {
  24. GLfloat lifetime; //current lifetime of the particle
  25. GLfloat maximum_lifetime; //time of death of particle
  26. GLfloat r, g, b; // color values of the particle
  27. GLfloat xpos, ypos, zpos; // position of the particle
  28. GLfloat xspeed, yspeed, zspeed; // speed of the particle
  29. //TODO: add new parameters if needed
  30. GLfloat x, y, z;
  31. };
  32. struct SVertex {
  33. GLfloat tx, ty;
  34. GLfloat x, y, z;
  35.  
  36. };
  37. const float s = 0.5;
  38. SVertex bilboard[] =
  39. {
  40. { 0, 0, -s, -s, 0 },
  41. { 0, 1, -s, s, 0 },
  42. { 1, 1, s, s, 0 },
  43. { 1, 0, s, -s, 0 }
  44. };
  45. const int MAX_PARTICLES = 500;
  46. list<SParticle> Particles;
  47. list<SParticle>::iterator particle_iter;
  48. //TODO: texture for particles - change if You want
  49. const char texture_name[] = "media//fireball.bmp";
  50. Ctexture *bilboard_tex;
  51. //Gaussian random number generator - mean 0.0, standard deviation 1.0
  52. float NRand()
  53. {
  54. static int q = 15;
  55. static float c1 = (1 << q) - 1;
  56. static float c2 = ((int)(c1 / 3)) + 1;
  57. static float c3 = 1.f / c1;
  58. /* random number in range 0 - 1 not including 1 */
  59. float random = 0.f;
  60.  
  61. /* the white noise */
  62. float noise = 0.f;
  63. random = ((float)(rand()) / (float)(RAND_MAX + 1));
  64. random = (2.f * ((random * c2) + (random * c2) + (random * c2)) - 3.f * (c2 - 1.f)) * c3;
  65. return random;
  66. }
  67. //single bilboard drawing
  68. void bilboarddrawinglist()
  69. {
  70.  
  71. glNewList(1, GL_COMPILE);
  72. glPushMatrix();
  73. // Wlaczamy tablice koordynatow tekstur
  74. glEnableClientState(GL_TEXTURE_COORD_ARRAY);
  75. // Wlaczamy tablice wspolrzednych wierzcholkow
  76. glEnableClientState(GL_VERTEX_ARRAY);
  77.  
  78.  
  79. glInterleavedArrays(GL_T2F_V3F, 0, (GLvoid*)bilboard);
  80. glDrawArrays(GL_QUADS, 0, 4);
  81. glPopMatrix();
  82. glEndList();
  83. }
  84.  
  85. int initTexture(void)
  86. {
  87. glEnable(GL_TEXTURE_2D);
  88.  
  89. glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
  90.  
  91.  
  92. // texture for particle
  93.  
  94. bilboard_tex = new Ctexture(texture_name);
  95.  
  96. //empty texture, currently not needed
  97. //tekstury[0] = new Ctexture();
  98. return true;
  99. }
  100. void drawScene(void)
  101. {
  102. // Clear screen to background color.
  103. glClear(GL_COLOR_BUFFER_BIT);
  104.  
  105. // Set foreground (or drawing) color.
  106. glColor3f(1.0, 0.0, 0.0);
  107.  
  108. if (bilboard_tex)bilboard_tex->Bind();
  109. list<SParticle>::iterator currP = Particles.begin();
  110. //TODO 0: Set proper render states in OpenGL - blending and depth test
  111. //glEnable() with GL_BLEND, glBlendFunc() with GL_ONE, GL_ONE and glDisable() GL_DEPTH_TEST;
  112. //notice what happens if they are not set
  113. glEnable(GL_BLEND);
  114. glBlendFunc(GL_ONE, GL_ONE);
  115. glDisable(GL_DEPTH_TEST);
  116. //...
  117.  
  118. for (unsigned int i = 0; i<Particles.size(); i++, currP++)
  119. {
  120. glPushMatrix();
  121.  
  122. //Draw particle
  123. //TODO 1: set particles params like color or size (glColor3f glScalef), position is done for example
  124.  
  125.  
  126. glTranslatef(currP->xpos, currP->ypos, currP->zpos);
  127. glColor3f(currP->r, currP->g, currP->b);
  128. glScalef(0.2, 0.2, 0.2);
  129. //...
  130.  
  131. //Draw particle
  132. glCallList(1);
  133. glPopMatrix();
  134. }
  135.  
  136.  
  137.  
  138. glutSwapBuffers();
  139. }
  140.  
  141. // Initialization routine.
  142. void setup(void)
  143. {
  144. x = 0;
  145. y = 0;
  146. // Set background (or clearing) color.
  147. glClearColor(0.0, 0.0, 0.0, 0.0);
  148. initTexture();
  149. bilboarddrawinglist();
  150.  
  151. }
  152.  
  153. // OpenGL window reshape routine.
  154. void resize(int w, int h)
  155. {
  156. // Set viewport size to be entire OpenGL window.
  157. glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  158.  
  159. // Set matrix mode to projection.
  160. glMatrixMode(GL_PROJECTION);
  161.  
  162. // Clear current projection matrix to identity.
  163. glLoadIdentity();
  164.  
  165. // Specify the orthographic (or perpendicular) projection,
  166. // i.e., define the viewing box.
  167. gluPerspective(90, (float)w / (float)h, 1, 200);
  168. //glOrtho(-10.0, 10.0, -10.0, 10.0,-10.0, 10.0);
  169. //glFrustum(-10.0, 10.0, -10.0, 10.0, 5.0, 100.0);
  170.  
  171. // Set matrix mode to modelview.
  172. glMatrixMode(GL_MODELVIEW);
  173.  
  174. // Clear current modelview matrix to identity.
  175. glLoadIdentity();
  176. }
  177. void EmitParticles()
  178. {
  179. //TODO 2:
  180. //Emit particles:
  181. //set parameters for new particles (SParticle structure),
  182. //You can add new parameters to the SParticle structure
  183. //For random numbers use NRand() - Gaussian random number generator - mean 0.0, standard deviation 1.0
  184. //You should use "for" loop. In each iteration new particle (the variable of type SParticle) should be created.
  185. //After creation new particle should be pushed front to the list of particles Particles.
  186. float fullCircle = 1;
  187.  
  188. for (float i = 0; i <= 2*3.14; i += 0.1)
  189. {
  190. //double part = i * NRand() / 2;
  191.  
  192. SParticle particle;
  193.  
  194. particle.r = 0.2;
  195. particle.g = 1;
  196. particle.b = 0.9;
  197.  
  198. particle.maximum_lifetime = abs(NRand() * 5);
  199. particle.lifetime = 0;
  200.  
  201. particle.xpos = fullCircle * sin(i);
  202. particle.ypos = -1;
  203. particle.zpos = fullCircle * cos(i)-10;
  204.  
  205. particle.xspeed = 0;
  206. particle.yspeed = 1;
  207. particle.zspeed = 0;
  208.  
  209. particle.x = NRand() * 1.1;
  210. particle.y = 0;
  211. particle.z = 0;
  212.  
  213. Particles.push_back(particle);
  214. }
  215. //...
  216. }
  217. void AffectParticles()
  218. {
  219. list<SParticle> ParticlesToRemove;
  220. static std::clock_t oldtime = std::clock();
  221. std::clock_t newtime = std::clock();
  222. double timebetweenframes = ((double)(newtime - oldtime) / (double)CLOCKS_PER_SEC);
  223. int frametogrey = 7;
  224. double greyscale = 0.025;
  225.  
  226. particle_iter = Particles.begin();
  227. while (particle_iter != Particles.end())
  228. {
  229. //TODO 3:
  230. //update particle parameters:
  231. //position, color (fading color for example), speed(gravity for example)
  232. //timebetweenframes - delta time, can be used for updating position in current frame like: particle_iter->ypos + particle_iter->yspeed*timebetweenframes
  233. //particle_iter->lifetime/particle_iter->maximum_lifetime - usefull proportion for changing color and size
  234.  
  235. //...
  236. particle_iter->xpos += particle_iter->xspeed * timebetweenframes;
  237. particle_iter->ypos += particle_iter->yspeed * timebetweenframes;
  238. particle_iter->zpos += particle_iter->zspeed * timebetweenframes;
  239.  
  240. particle_iter->xspeed += particle_iter->x * timebetweenframes;
  241. particle_iter->yspeed += particle_iter->y * timebetweenframes;
  242. particle_iter->zspeed += particle_iter->z * timebetweenframes;
  243.  
  244. particle_iter->r = sin(NRand());
  245. particle_iter->g = cos(NRand()) / NRand();
  246. particle_iter->b = 0.2 * timebetweenframes / NRand();
  247.  
  248.  
  249. //removing particles
  250. particle_iter->lifetime += timebetweenframes;
  251. if (particle_iter->lifetime >= particle_iter->maximum_lifetime)
  252. {
  253. //if You need to create particles in place when other died (fireworks particle effect for example)
  254. //Then You can create them here like in TODO 1
  255. particle_iter = Particles.erase(particle_iter);
  256. }
  257. else
  258. particle_iter++;
  259.  
  260. }
  261. cout << Particles.size() << endl;
  262.  
  263. oldtime = newtime;
  264. }
  265. void animate() {
  266. EmitParticles();
  267. AffectParticles();
  268. glutPostRedisplay();
  269. }
  270.  
  271. // Keyboard input processing routine.
  272. void keyInput(unsigned char key, int x, int y)
  273. {
  274.  
  275. }
  276.  
  277. // Mouse callback routine.
  278. void mouseControl(int button, int state, int x, int y)
  279. {
  280.  
  281.  
  282. }
  283.  
  284. // Main routine: defines window properties, creates window,
  285. // registers callback routines and begins processing.
  286. int main(int argc, char **argv)
  287. {
  288. // Initialize GLUT.
  289. glutInit(&argc, argv);
  290.  
  291. // Set display mode as single-buffered and RGB color.
  292. glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  293.  
  294. // Set OpenGL window size.
  295. glutInitWindowSize(500, 500);
  296.  
  297. // Set position of OpenGL window upper-left corner.
  298. glutInitWindowPosition(100, 100);
  299.  
  300. // Create OpenGL window with title.
  301. glutCreateWindow("Laboratorium GK: 11_OGL");
  302.  
  303. // Initialize.
  304. setup();
  305.  
  306. // Register display routine.
  307. glutDisplayFunc(drawScene);
  308.  
  309. // Register reshape routine.
  310. glutReshapeFunc(resize);
  311.  
  312.  
  313. // Register the mouse and keyboard callback function.
  314. glutMouseFunc(mouseControl);
  315. glutKeyboardFunc(keyInput);
  316.  
  317.  
  318. glutIdleFunc(animate);
  319. // Begin processing.
  320. glutMainLoop();
  321.  
  322. if (bilboard_tex) delete bilboard_tex;
  323. return 0;
  324. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement