Advertisement
Guest User

Untitled

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