Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.91 KB | None | 0 0
  1. // particle_fountain.cpp : Defines the entry point for the console application.
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include<stdlib.h>
  6. #include <stdio.h>
  7. #include<Windows.h>
  8. #include <time.h>
  9. #include <GL\glut.h>
  10. #include<GL\GLU.h>
  11.  
  12. #define MAX_PARTICLES 100 //max number of particles
  13. #define MAX_BOUNCE_COUNT 3 //number of times a particle should bounce
  14. #define MAX_PARTICLE_AGE 90
  15.  
  16. //Colours
  17. float R = 0.8f;
  18. float G = 0.2f;
  19. float B = 0.0f;
  20. float cR = 0.001f;
  21. float cG = 0.002f;
  22. float cB = 0.003f;
  23. float Size = 0.01f; //size for points
  24. GLuint txParticle;
  25. GLuint txPlane;
  26.  
  27.  
  28. struct PARTICLE {
  29.     float X,Y,Z; // Current position
  30.     float sX,sY,sZ; // Current Speed/Movement
  31.     float tX,tY,tZ; // Target Speed/Movement
  32.     float R,B,G; // Particle Colour
  33.     bool Active; // Is particle Active
  34.     int Age; // Age of the particle
  35.     int MaxAge; // Maximum Age before particle dies
  36.     int BounceCount;
  37. } Particles[MAX_PARTICLES];
  38.  
  39. void Init_Particles();
  40. void Activate_Particles();
  41. void Adjust_Particles();
  42. void Render_Particles();
  43. bool LoadBitmapTexture(char * FileName, GLuint &texid);
  44. void timer(int extra);
  45. void Load_Plane();
  46. void DrawGLscene();
  47. void Reshape(GLsizei w, GLsizei h);
  48.  
  49. int main(int argc, char** argv){
  50.      glutInit(&argc,argv);
  51.     glutInitDisplayMode( GLUT_RGBA| GLUT_DOUBLE );
  52.     glutInitWindowSize( 640, 480 );    
  53.     glutCreateWindow("Particle fountain");
  54.  
  55.     glMatrixMode(GL_PROJECTION);
  56.     glLoadIdentity();
  57.     glFrustum(-1.0, 1.0, -1.0, 1.0, 1.0, 10.0);
  58.     glMatrixMode(GL_MODELVIEW);
  59.     glLoadIdentity();
  60.     glTranslatef(0.0, -0.9, -3.0);
  61.  
  62.     Load_Plane();
  63.     Init_Particles();
  64.     glutDisplayFunc(DrawGLscene);
  65.     glutTimerFunc(0, timer, 0);
  66.     glutMainLoop();
  67.  
  68. }
  69.  
  70. void timer(int extra)
  71. {
  72.     glutPostRedisplay();
  73.     glutTimerFunc(16, timer, 0);
  74. }
  75.  
  76. void Load_Plane(){
  77.     glEnable(GL_BLEND);
  78.     glBlendFunc(GL_ONE_MINUS_SRC_ALPHA, GL_SRC_ALPHA);
  79.     glColor4f(0.0f, 0.2f, 0.2f, 0.5f);
  80.     glEnable(GL_TEXTURE_2D);
  81.     glBindTexture(GL_TEXTURE_2D, txPlane);
  82.     glBegin(GL_QUADS);
  83.         glNormal3f(0.0f, 1.0f, 0.0f);
  84.         glTexCoord2f(0.0f, 0.0f);
  85.         glVertex3f(-10.0f, 0.0f, 10.0f);
  86.         glTexCoord2f(1.0f, 0.0f);
  87.         glVertex3f( 10.0f, 0.0f, 10.0f);
  88.         glTexCoord2f(1.0f, 1.0f);
  89.         glVertex3f( 10.0f, 0.0f, -10.0f);
  90.         glTexCoord2f(0.0f, 1.0f);
  91.         glVertex3f(-10.0f, 0.0f, -10.0f);
  92.     glEnd();
  93. }
  94. void DrawGLscene(){
  95.    
  96.     glPushMatrix();
  97.     glScalef(1.0f, -1.0f, 1.0f);
  98.     Render_Particles();
  99.     glPopMatrix();
  100.     Render_Particles();
  101.  
  102. }
  103.  
  104.  
  105. void Init_Particles(){
  106.     LoadBitmapTexture("./Particle.bmp", txParticle);
  107.     LoadBitmapTexture("./Plain.bmp",txPlane);
  108.    
  109.     int p;
  110.     srand((int)time(NULL));
  111.     for(p=0; p<MAX_PARTICLES; p++){
  112.         Particles[p].Active = FALSE;
  113.         Particles[p].tX = 0.0f;
  114.         Particles[p].tY = -0.1f;
  115.         Particles[p].tZ = 0.0f;
  116.     }
  117. }
  118.  
  119. void Activate_Particles(){
  120.     int p;
  121.     for(p=0; p<MAX_PARTICLES; p++){
  122.         if(!Particles[p].Active){
  123.             // Start the particle at 0,0,0 origin
  124.             Particles[p].X = 0.0f;
  125.             Particles[p].Y = 0.0f;
  126.             Particles[p].Z = 0.0f;
  127.             // The following lines set a random speed value
  128.             Particles[p].sX = (((float)((rand() % 100) + 1)) /
  129.                 1000.0f) - 0.05f;
  130.             Particles[p].sY = (((float)((rand() % 100) + 50)) /
  131.                 500.0f);
  132.             Particles[p].sZ = (((float)((rand() % 100) + 1)) /
  133.                 1000.0f) - 0.05f;
  134.             // We also activate the particle
  135.             Particles[p].Active = true;
  136.             // Set it's Age to zero
  137.             Particles[p].Age = 0;
  138.             // We also assign a max age to the particles
  139.             Particles[p].MaxAge = MAX_PARTICLE_AGE;
  140.             // We Also reset the bouncecount to zero
  141.             Particles[p].BounceCount = 0;
  142.  
  143.             //Adding the colours
  144.             Particles[p].R = R;
  145.             Particles[p].G = G;
  146.             Particles[p].B = B;
  147.             R+=cR;
  148.             G+=cG;
  149.             B+=cB;
  150.             if(R>1.0f){R=1.0f; cR=-cR;}
  151.             if(R<0.0f){R=0.0f; cR=-cR;}
  152.             if(G>1.0f){G=1.0f; cG=-cG;}
  153.             if(G<0.0f){G=0.0f; cG=-cG;}
  154.             if(B>1.0f){B=1.0f; cB=-cB;}
  155.             if(B<0.0f){B=0.0f; cB=-cB;}
  156.             return;
  157.         }
  158. }
  159. }
  160.  
  161. void Adjust_Particles(){
  162.     int p;
  163.     for(p=0; p<MAX_PARTICLES; p++){
  164.         // We move the speed towards the target speed by 1/20 (5%)
  165.         Particles[p].sX+= (Particles[p].tX - Particles[p].sX) / 20.0f;
  166.         Particles[p].sY+= (Particles[p].tY - Particles[p].sY) / 20.0f;
  167.         Particles[p].sZ+= (Particles[p].tZ - Particles[p].sZ) / 20.0f;
  168.         // Then we adjust the position of
  169.         // the particle by the new speed
  170.         Particles[p].X+= Particles[p].sX;
  171.         Particles[p].Y+= Particles[p].sY;
  172.         Particles[p].Z+= Particles[p].sZ;
  173.         // Now for the bounce code.
  174.         if(Particles[p].Y < 0.0f){
  175.             Particles[p].Y = 0.0f;
  176.             Particles[p].sY = -Particles[p].sY;
  177.             Particles[p].BounceCount++;
  178.             if(Particles[p].BounceCount > MAX_BOUNCE_COUNT){
  179.                 Particles[p].Active = FALSE;
  180.             }
  181.         }
  182.         // And finally the age check
  183.         Particles[p].Age++;
  184.         if(Particles[p].Age > Particles[p].MaxAge){
  185.             Particles[p].Active = FALSE;
  186.         }
  187.     }
  188.  
  189. }
  190.  
  191. void Render_Particles(){
  192.     Activate_Particles();
  193.     Adjust_Particles();
  194.  
  195.     glClear( GL_COLOR_BUFFER_BIT );
  196.  
  197.     int p;
  198.     // Enable textures and bind our particle texture
  199.     glEnable(GL_TEXTURE_2D);
  200.     glBindTexture(GL_TEXTURE_2D, txParticle);
  201.     // Disable Depth testing.
  202.     glDisable(GL_DEPTH_TEST);
  203.     // Enable blending
  204.     glEnable(GL_BLEND);
  205.     glBlendFunc(GL_SRC_COLOR,GL_ONE);
  206.     for(p=0; p<MAX_PARTICLES; p++){
  207.         if(Particles[p].Active){
  208.             glColor4f(Particles[p].R,
  209.                 Particles[p].G,
  210.                 Particles[p].B, 1.0f);
  211.             glPushMatrix();
  212.             glTranslatef(Particles[p].X,
  213.                 Particles[p].Y,
  214.                 Particles[p].Z);
  215.             glBegin(GL_QUADS);
  216.             glNormal3f(0.0f, 0.0f, 1.0f);
  217.             glTexCoord2f(0.0f, 0.0f);
  218.             glVertex3f(-Size, -Size, 0.0f);
  219.             glTexCoord2f(1.0f, 0.0f);
  220.             glVertex3f(Size, -Size, 0.0f);
  221.             glTexCoord2f(1.0f, 1.0f);
  222.             glVertex3f(Size, Size, 0.0f);
  223.             glTexCoord2f(0.0f, 1.0f);
  224.             glVertex3f(-Size, Size, 0.0f);
  225.             glEnd();
  226.             glPopMatrix();
  227.    
  228.    
  229.         }
  230.     }
  231.      glEnable(GL_DEPTH_TEST);
  232.      glutSwapBuffers();
  233. }
  234.  
  235. bool LoadBitmapTexture(char * FileName, GLuint &texid){
  236.     HBITMAP hBMP; // Handle Of The Bitmap
  237.     BITMAP BMP; // Bitmap Structure
  238.     glGenTextures(1, &texid); // Create The Texture
  239.     hBMP=(HBITMAP)LoadImage(GetModuleHandle(NULL),
  240.         FileName,
  241.         IMAGE_BITMAP, 0, 0,
  242.         LR_CREATEDIBSECTION | LR_LOADFROMFILE
  243.         );
  244.  
  245.     if (!hBMP) // Does The Bitmap Exist?
  246.         return FALSE; // If Not Return False
  247.     GetObject(hBMP, sizeof(BMP), &BMP); // Get The Object
  248.     // hBMP: Handle To Graphics Object
  249.     // sizeof(BMP): Size Of Buffer For Object Information
  250.     // &BMP: Buffer For Object Information
  251.     glPixelStorei(GL_UNPACK_ALIGNMENT, 4);
  252.     // Pixel Storage Mode (Word Alignment / 4 Bytes)
  253.     // Typical Texture Generation Using Data From The Bitmap
  254.     glBindTexture(GL_TEXTURE_2D, texid);// Bind To The Texture ID
  255.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
  256.         GL_LINEAR); // Linear Min Filter
  257.     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER,
  258.         GL_LINEAR); // Linear Mag Filter
  259.     glTexImage2D(GL_TEXTURE_2D, 0, 3, BMP.bmWidth, BMP.bmHeight,
  260.         0, GL_BGR_EXT, GL_UNSIGNED_BYTE, BMP.bmBits);
  261.     DeleteObject(hBMP); // Delete The Object
  262.     return TRUE; // Loading Was Successful
  263. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement