Advertisement
Guest User

Updated OpenGL

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