Facebook
From Bistre Hog, 4 Years ago, written in Plain Text.
This paste is a reply to Untitled from Bitty Goose - view diff
Embed
Download Paste or View Raw
Hits: 274
  1. #undef UNICODE
  2. #include <windows.h>
  3. #include <iostream>
  4. #include <vector>
  5. #include <list>
  6. #define _USE_MATH_DEFINES
  7. #define GLUT_DISABLE_ATEXIT_HACK
  8.  
  9.  
  10. #include <math.h>
  11. #include <ctime>
  12. #include "GL/glut.h"
  13. #include <GL/gl.h>
  14. #include "teksture.h"
  15.  
  16.  
  17. using namespace std;
  18.  
  19. static double x, y;
  20. static double dx = 1;
  21. static double dy = 0.5;
  22. static double cr = 0;
  23. double k;
  24. struct SParticle
  25. {
  26.         GLfloat lifetime;                                       //current lifetime of the particle
  27.         GLfloat maximum_lifetime;                       //time of death of particle
  28.         GLfloat r, g, b;                          // color values of the particle
  29.         GLfloat xpos, ypos, zpos;                 // position of the particle
  30.         GLfloat xspeed, yspeed, zspeed;           // speed of the particle
  31.         GLfloat ax, ay, az;
  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(GL_BLEND);
  114.         glBlendFunc(GL_SRC_ALPHA, GL_ONE);
  115.         glDisable(GL_DEPTH_TEST);
  116.         //glEnable() with GL_BLEND, glBlendFunc() with GL_ONE, GL_ONE and glDisable() GL_DEPTH_TEST;
  117.         //notice what happens if they are not set
  118.        
  119.         //...
  120.  
  121.         for (unsigned int i = 0; i<Particles.size(); i++, currP++)
  122.         {
  123.                 glPushMatrix();
  124.  
  125.                 //Draw particle
  126.                 //TODO 1: set particles params like color or size (glColor3f glScalef), position is done for example
  127.  
  128.                 glTranslatef(currP->xpos, currP->ypos, currP->zpos);
  129.                 glColor3f(1, 1, 1);
  130.                 glScalef(0.1, 0.1, 0.1);
  131.  
  132.        
  133.                 //...
  134.  
  135.                 //Draw particle
  136.                 glCallList(1);
  137.                 glPopMatrix();
  138.         }
  139.  
  140.  
  141.  
  142.         glutSwapBuffers();
  143. }
  144.  
  145. // Initialization routine.
  146. void setup(void)
  147. {
  148.         x = 0;
  149.         y = 0;
  150.         // Set background (or clearing) color.
  151.         glClearColor(0.0, 0.0, 0.0, 0.0);
  152.         initTexture();
  153.         bilboarddrawinglist();
  154.  
  155. }
  156.  
  157. // OpenGL window reshape routine.
  158. void resize(int w, int h)
  159. {
  160.         // Set viewport size to be entire OpenGL window.
  161.         glViewport(0, 0, (GLsizei)w, (GLsizei)h);
  162.  
  163.         // Set matrix mode to projection.
  164.         glMatrixMode(GL_PROJECTION);
  165.  
  166.         // Clear current projection matrix to identity.
  167.         glLoadIdentity();
  168.  
  169.         // Specify the orthographic (or perpendicular) projection,
  170.         // i.e., define the viewing box.
  171.         gluPerspective(90, (float)w / (float)h, 1, 200);
  172.         //glOrtho(-10.0, 10.0, -10.0, 10.0,-10.0, 10.0);
  173.         //glFrustum(-10.0, 10.0, -10.0, 10.0, 5.0, 100.0);
  174.  
  175.         // Set matrix mode to modelview.
  176.         glMatrixMode(GL_MODELVIEW);
  177.  
  178.         // Clear current modelview matrix to identity.
  179.         glLoadIdentity();
  180. }
  181. void EmitParticles()
  182. {
  183.         //TODO 2:
  184.         //Emit particles:
  185.         //set parameters for new particles (SParticle structure),
  186.         //You can add new parameters to the SParticle structure
  187.         //For random numbers use NRand() - Gaussian  random number generator - mean 0.0, standard deviation 1.0
  188.         //You should use "for" loop. In each iteration new particle (the variable of type SParticle) should be created.
  189.         //After creation new particle should be pushed front to the list of particles Particles.
  190.        
  191.         //...
  192.  
  193.         int pointsCount = 314;
  194.         for (int i = 0; i < pointsCount; i++) {
  195.                 SParticle newPart = SParticle();
  196.                 newPart.xpos = 5 * sin(i) -10;
  197.                 newPart.zpos = 5 * cos(i) -10;
  198.                 newPart.ypos = -10;
  199.                 newPart.yspeed = 3;
  200.                 newPart.zspeed = 0;
  201.                 //newPart.az = sin(i) * 4;
  202.                 //newPart.ay = 0;
  203.                 //newPart.ax = 0;
  204.                 newPart.maximum_lifetime = 0.1f + (0.5f + NRand());
  205.                 Particles.push_back(newPart);
  206.  
  207.         }
  208. }
  209. void AffectParticles()
  210. {
  211.         list<SParticle> ParticlesToRemove;
  212.         static std::clock_t  oldtime = std::clock();
  213.         std::clock_t newtime = std::clock();
  214.         double timebetweenframes = (double)(newtime - oldtime) / (double)CLOCKS_PER_SEC;
  215.         int frametogrey = 7;
  216.         double greyscale = 0.025;
  217.  
  218.         particle_iter = Particles.begin();
  219.         while (particle_iter != Particles.end())
  220.         {
  221.                 //TODO 3:
  222.                 //update particle parameters:
  223.                 //position, color (fading color for example), speed(gravity for example)
  224.                 //timebetweenframes - delta time, can be used for updating position in current frame like: particle_iter->ypos + particle_iter->yspeed*timebetweenframes
  225.                 //particle_iter->lifetime/particle_iter->maximum_lifetime - usefull proportion for changing color and size
  226.                 //...
  227.                 particle_iter->lifetime += timebetweenframes;
  228.                 particle_iter->yspeed += timebetweenframes * particle_iter->ay;
  229.                 particle_iter->xspeed += timebetweenframes * particle_iter->ax;
  230.                 particle_iter->zspeed += timebetweenframes * particle_iter->az;
  231.  
  232.                 particle_iter->ypos += timebetweenframes * particle_iter->yspeed;
  233.                 particle_iter->xpos += timebetweenframes * particle_iter->xspeed;
  234.                 particle_iter->zpos += timebetweenframes * particle_iter->zspeed;
  235.                
  236.                
  237.                 //removing particles
  238.        
  239.                 if (particle_iter->lifetime >= particle_iter->maximum_lifetime)
  240.                 {
  241.                         //if You need to create particles in place when other died (fireworks particle effect for example)
  242.                         //Then You can create them here like in TODO 1
  243.                         particle_iter = Particles.erase(particle_iter);
  244.                 }
  245.                 else
  246.                         particle_iter++;
  247.  
  248.         }
  249.         cout << Particles.size() << endl;
  250.  
  251.         oldtime = newtime;
  252. }
  253. void animate() {
  254.         EmitParticles();
  255.         AffectParticles();
  256.         glutPostRedisplay();
  257. }
  258.  
  259. // Keyboard input processing routine.
  260. void keyInput(unsigned char key, int x, int y)
  261. {
  262.  
  263. }
  264.  
  265. // Mouse callback routine.
  266. void mouseControl(int button, int state, int x, int y)
  267. {
  268.  
  269.  
  270. }
  271.  
  272. // Main routine: defines window properties, creates window,
  273. // registers callback routines and begins processing.
  274. int main(int argc, char **argv)
  275. {
  276.         // Initialize GLUT.
  277.         glutInit(&argc, argv);
  278.  
  279.         // Set display mode as single-buffered and RGB color.
  280.         glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
  281.  
  282.         // Set OpenGL window size.
  283.         glutInitWindowSize(500, 500);
  284.  
  285.         // Set position of OpenGL window upper-left corner.
  286.         glutInitWindowPosition(100, 100);
  287.  
  288.         // Create OpenGL window with title.
  289.         glutCreateWindow("Laboratorium GK: 11_OGL");
  290.  
  291.         // Initialize.
  292.         setup();
  293.  
  294.         // Register display routine.
  295.         glutDisplayFunc(drawScene);
  296.  
  297.         // Register reshape routine.
  298.         glutReshapeFunc(resize);
  299.  
  300.  
  301.         // Register the mouse and keyboard callback function.
  302.         glutMouseFunc(mouseControl);
  303.         glutKeyboardFunc(keyInput);
  304.  
  305.  
  306.         glutIdleFunc(animate);
  307.         // Begin processing.
  308.         glutMainLoop();
  309.  
  310.         if (bilboard_tex) delete bilboard_tex;
  311.         return 0;
  312. }
  313.