Advertisement
Guest User

Untitled

a guest
Aug 20th, 2014
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.27 KB | None | 0 0
  1. #include <GL/glew.h>
  2. #include <GL/glfw.h>
  3. #include <iostream>
  4. #include <cmath>
  5. #include <cstdio>
  6.  
  7. using namespace std;
  8. GLuint mainProgram;
  9. const float PIE = 3.1416;
  10.  
  11. struct Vtx {
  12.     GLfloat vx, vy, vz; //velocity
  13.     GLfloat startTime; //start time
  14.     GLuint color; //color
  15. };
  16.  
  17. enum {ATTRIB_VEL, ATTRIB_STARTTIME, ATTRIB_COLOR}; //enumerate certain constants in this, get assigned to unique addresses
  18. //assumes everything is uint (starts at 0,1,2,3...); no more datatype
  19. //but if you assign it, papalitan accordingly (try put =15; at the end of ATTRIB_COLOR and hover over each one)
  20.  
  21. const size_t nParticles = 600; //instead of declare as int, have size_t instead
  22. const float maxVelocity = 2, minVelocity =1;
  23. const float accelX = 0, accelY = 1, accelZ = 0; //fountain paakyat, sa taas gumagalaw
  24. const float timeSpeed = 0.01f; //delta time, how fast is time
  25. const float particleLifetime = 4.00f; //lifetime of particle awwwwwwww. disappears after 4 sec
  26. const float elev = PIE/2.5; //angle of elevation
  27.  
  28.  
  29. Vtx particles[nParticles]; //declare array of structs
  30.  
  31.  
  32. bool loadShaderSource(GLuint shader, const char *filePath)
  33. {
  34.     FILE *f = fopen(filePath, "r");
  35.     if ( !f ) {
  36.         std::cerr << "ERROR: shader source not found: " << filePath << '\n';
  37.         return false;
  38.     }
  39.     fseek(f, 0, SEEK_END);
  40.     const size_t sz = ftell(f)+1;
  41.     GLchar *buffer = new GLchar[sz];
  42.     fseek(f, 0, SEEK_SET);
  43.     fread(buffer, 1, sz, f);
  44.     fclose(f);
  45.     buffer[sz-1]=0;
  46.     glShaderSource(shader, 1, (const GLchar **) &buffer, NULL);
  47.     delete [] buffer;
  48.  
  49.     if ( glGetError() != GL_NO_ERROR ) {
  50.         std::cerr << "ERROR: Unable to load shader\n";
  51.         return false;
  52.     }
  53.  
  54.     glCompileShader(shader);
  55.  
  56.     //check for loading errors
  57.     GLint logLength;
  58.     glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &logLength);
  59.     if (logLength > 0) {
  60.         GLchar *log = (GLchar *)malloc(logLength);
  61.         glGetShaderInfoLog(shader, logLength, &logLength, log);
  62.         std::cout << "Shader compile log:\n" << log << endl;
  63.         free(log);
  64.     }
  65.  
  66.     //hahanapin source, compare, lalagay sa shader na variable tas icocompile
  67.     return true;
  68. }
  69.  
  70. float fRand()
  71. {
  72.     return (float)rand()/RAND_MAX;
  73. }
  74.  
  75. //create certain velocity that's random
  76.  
  77. void setParticle(Vtx &particle)
  78. {//may pumapasok na struct sa set particle na yun tapos we can edit the particle
  79.     float vel = fRand() * (maxVelocity - minVelocity) + minVelocity;
  80.     float azimuth = fRand() * 2 * PIE;  //assign specific angle of firing?
  81.     particle.vx = vel * cos(azimuth)*cos(elev);
  82.     particle.vy = vel * sin(elev);
  83.     particle.vz = vel * sin(azimuth) * cos(elev);
  84.     particle.color = 0xFF000000 | rand() << 16 | rand(); //opaque fountain since first 2 letters FF alpha
  85.         //pipe rand on 16th bit, only last 6 digits affected
  86. }
  87.  
  88. void setupShaders()
  89. {
  90.     //declare shaders
  91.     GLuint fragShader = glCreateShader(GL_FRAGMENT_SHADER),
  92.             vtxShader = glCreateShader(GL_VERTEX_SHADER);
  93.  
  94.     //load shaders
  95.     loadShaderSource(fragShader, "fountain.fsh");
  96.     loadShaderSource(vtxShader, "fountain.vsh");
  97.  
  98.         //declare shaders first, then load shaders
  99.         //inside loading, open file and create shader inof. after, check for loading errors. then compile, then check for link errors.
  100.  
  101.     mainProgram = glCreateProgram();
  102.  
  103.     //attach shader to main prog
  104.    
  105.     glAttachShader(mainProgram, fragShader);
  106.     glAttachShader(mainProgram, vtxShader);
  107.  
  108.     //whatever att is in vertex shader, hahanapin sa loob ng program. magbbind
  109.     //ok lang diff color as long as all bound
  110.     //binds vtxShader attributes
  111.     glBindAttribLocation(mainProgram, ATTRIB_VEL, "initialVelocity"); //see fountain.vsh for initialVelocity
  112.     glBindAttribLocation(mainProgram, ATTRIB_STARTTIME, "startTime"); //see fountain.vsh
  113.     glBindAttribLocation(mainProgram, ATTRIB_COLOR, "color"); //see fountain.vsh
  114.  
  115.     //link prog na
  116.  
  117.     glLinkProgram(mainProgram);
  118.  
  119.     //check for link errors - checks program, not shader. may error checking for shaders
  120.     //and error checking for program
  121.     GLint logLength;
  122.     glGetProgramiv(mainProgram, GL_INFO_LOG_LENGTH, &logLength);
  123.     if (logLength > 0) {
  124.         GLchar *log = (GLchar *)malloc(logLength);
  125.         glGetProgramInfoLog(mainProgram, logLength, &logLength, log);
  126.         std::cout << "Program compile log:\n" << log << endl;
  127.         free(log);
  128.     }
  129.    
  130. }
  131.  
  132.  
  133. void setupVertices()
  134. {
  135.     glEnableVertexAttribArray(ATTRIB_VEL);
  136.     glEnableVertexAttribArray(ATTRIB_STARTTIME);
  137.     glEnableVertexAttribArray(ATTRIB_COLOR);
  138.  
  139.     glVertexAttribPointer(ATTRIB_VEL, 3, GL_FLOAT, GL_FALSE, sizeof(Vtx), &(particles[0].vx)); //3: kasi may vx vy vz
  140.     //sizeofVtx: stride. parang hop. ano yung next hop para malaman ko im on the next/right(?) stride
  141.     //attrib = attrib from vtxshader. instead of glvtxpointer (?) use this
  142.     glVertexAttribPointer(ATTRIB_STARTTIME, 1, GL_FLOAT, GL_FALSE, sizeof(Vtx), &(particles[0].startTime)); //1 var lang //change pointer. after vx vy vz is start
  143.     //time. point to starttime para start throw value there
  144.     glVertexAttribPointer(ATTRIB_COLOR, 4, GL_UNSIGNED_BYTE, GL_TRUE, sizeof(Vtx), &(particles[0].vx)); //3: kasi may vx vy vz
  145.     //ubyte: hex vars //size 4 kasi abgr
  146.  
  147.     for (size_t i = 0; i < nParticles; ++i) {
  148.         particles[i].startTime = fRand() * particleLifetime; //randomizes lifetime
  149.         setParticle(particles[i]);
  150.     }
  151.  
  152. }
  153.  
  154. int main() {
  155.     if ( !glfwInit() ) {
  156.         std::cerr << "Unable to initialize OpenGL!\n";
  157.         return -1;
  158.     }
  159.    
  160.     if ( !glfwOpenWindow(640,640, //width and height of the screen
  161.                 8,8,8,8, //Red, Green, Blue and Alpha bits
  162.                 0,0, //Depth and Stencil bits
  163.                 GLFW_WINDOW)) {
  164.         std::cerr << "Unable to create OpenGL window.\n";
  165.         glfwTerminate();
  166.         return -1;
  167.     }
  168.  
  169.     if ( glewInit() != GLEW_OK ) {
  170.         std::cerr << "Unable to hook OpenGL extensions!\n";
  171.         return -1;
  172.     }
  173.  
  174.     setupShaders();
  175.     setupVertices();
  176.     glEnable(GL_PROGRAM_POINT_SIZE);
  177.     glEnable(GL_DEPTH_TEST);
  178.  
  179.     glUseProgram(mainProgram);
  180.  
  181.          //establish constants valid for 1 frame only
  182.     GLuint UNIF_T = glGetUniformLocation(mainProgram, "t"),
  183.             UNIF_ACCEL = glGetUniformLocation(mainProgram, "accel"),
  184.             UNIF_LIFETIME = glGetUniformLocation(mainProgram, "lifetime");
  185.  
  186.  
  187.     glfwSetWindowTitle("GLFW Simple Example");
  188.  
  189.     // Ensure we can capture the escape key being pressed below
  190.     glfwEnable( GLFW_STICKY_KEYS );
  191.  
  192.     // Enable vertical sync (on cards that support it)
  193.     glfwSwapInterval( 1 );
  194.  
  195.     float t = particleLifetime;
  196.  
  197.     glUniform1f(UNIF_T, t); //1f = 1dimensional
  198.     glUniform3f(UNIF_ACCEL, accelX, accelY, accelZ); //acceleration has 3 d
  199.     glUniform1f(UNIF_LIFETIME, particleLifetime);
  200.  
  201.     //what we did: declare const for uniforms, then make uniforms go to vtxshader
  202.  
  203.     glClearColor(0,0,0,0);
  204.  
  205.     do {
  206.         int width, height;
  207.         // Get window size (may be different than the requested size)
  208.         //we do this every frame to accommodate window resizing.
  209.         glfwGetWindowSize( &width, &height );
  210.         glViewport( 0, 0, width, height );
  211.    
  212.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  213.  
  214.         t += timeSpeed;
  215.         glUniform1f(UNIF_T, t);
  216.  
  217.         //go through all part again in array
  218.         for (size_t i = 0; i < nParticles; ++i)
  219.         {
  220.             if (t-particles[i].startTime >= particleLifetime)
  221.             {
  222.                 particles[i].startTime = t;
  223.                 setParticle(particles[i]);
  224.             }
  225.         }
  226.         glDrawArrays(GL_POINTS, 0, nParticles);
  227.         //VERY IMPORTANT: displays the buffer to the screen
  228.         glfwSwapBuffers();
  229.     } while ( glfwGetKey(GLFW_KEY_ESC) != GLFW_PRESS &&
  230.             glfwGetWindowParam(GLFW_OPENED) );
  231.  
  232.     glfwTerminate();
  233.     return 0;
  234.  
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement