Changeset db06984 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Nov 14, 2018, 4:55:06 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
adb104f
Parents:
f71d87d
Message:

Create a particle system that will later be used to render exploding asteroids

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    rf71d87d rdb06984  
    157157                  GLuint* model_mat_idx_vbo);
    158158
     159GLuint initializeParticleEffectBuffers();
     160
    159161void populateBuffers(vector<SceneObject*>& objects,
    160162                  map<GLuint, BufferInfo>& shaderBufferInfo,
     
    193195
    194196void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    195                   GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp,
    196                   GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao,
     197                  GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp, GLuint explosion_sp,
     198                  GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao, GLuint explosion_vao,
    197199                  GLuint colors_vbo, GLuint selected_colors_vbo,
    198200                  SceneObject* selectedObject);
     201void renderExplosion(GLuint explosion_sp, GLuint explosion_vao, GLuint tex);
     202
    199203void renderSceneGui();
    200204
     
    206210const int KEY_STATE_UNCHANGED = -1;
    207211const bool FULLSCREEN = false;
     212const int EXPLOSION_PARTICLE_COUNT = 300;
    208213unsigned int MAX_UNIFORMS = 0; // Requires OpenGL constants only available at runtime, so it can't be const
    209214
     
    438443   GLuint texture_sp = loadShaderProgram("./texture.vert", "./texture.frag");
    439444   GLuint laser_sp = loadShaderProgram("./laser.vert", "./laser.frag");
     445   GLuint explosion_sp = loadShaderProgram("./explosion.vert", "./explosion.frag");
    440446
    441447   shaderBufferInfo[color_sp] = BufferInfo();
     
    558564   glVertexAttribIPointer(2, 1, GL_UNSIGNED_INT, 0, 0);
    559565
     566   GLuint explosion_vao = initializeParticleEffectBuffers();
     567
    560568   float cam_speed = 1.0f;
    561569   float cam_yaw_speed = 60.0f*ONE_DEG_IN_RAD;
     
    600608   GLuint asteroid_proj_mat_loc = glGetUniformLocation(asteroid_sp, "proj");
    601609   GLuint asteroid_sp_models_ub_index = glGetUniformBlockIndex(asteroid_sp, "models");
    602    //GLuint asteroid_sp_hp_ub_index = glGetUniformBlockIndex(asteroid_sp, "hp_uniform");
    603610
    604611   GLuint view_mat_loc = glGetUniformLocation(texture_sp, "view");
     
    610617   GLuint laser_color_loc = glGetUniformLocation(laser_sp, "laser_color");
    611618   GLuint laser_sp_models_ub_index = glGetUniformBlockIndex(laser_sp, "models");
     619
     620   GLuint elapsed_system_time_loc = glGetUniformLocation(explosion_sp, "elapsed_system_time");
    612621
    613622
     
    905914      }
    906915
     916      glUseProgram(explosion_sp);
     917      glUniform1f(elapsed_system_time_loc, (GLfloat)current_seconds);
     918
    907919      // Render scene
    908920
     
    916928         case STATE_GAME:
    917929            renderScene(shaderBufferInfo,
    918                color_sp, asteroid_sp, texture_sp, laser_sp,
    919                color_vao, asteroid_vao, texture_vao, laser_vao,
     930               color_sp, asteroid_sp, texture_sp, laser_sp, explosion_sp,
     931               color_vao, asteroid_vao, texture_vao, laser_vao, explosion_vao,
    920932               colors_vbo, selected_colors_vbo,
    921933               selectedObject);
     
    18861898}
    18871899
     1900GLuint initializeParticleEffectBuffers() {
     1901   float vv[EXPLOSION_PARTICLE_COUNT * 3]; // initial velocities vec3
     1902   float vt[EXPLOSION_PARTICLE_COUNT]; // initial times
     1903   float t_accum = 0.0f; // start time
     1904
     1905   for (int i = 0; i < EXPLOSION_PARTICLE_COUNT; i++) {
     1906      vt[i] = t_accum;
     1907      t_accum += 0.01f;
     1908
     1909      float randx = ((float)rand() / (float)RAND_MAX) * 1.0f - 0.5f;
     1910      float randz = ((float)rand() / (float)RAND_MAX) * 1.0f - 0.5f;
     1911      vv[i*3] = randx;
     1912      vv[i*3 + 1] = 1.0f;
     1913      vv[i*3 + 2] = randz;
     1914   }
     1915
     1916   GLuint velocity_vbo;
     1917   glGenBuffers(1, &velocity_vbo);
     1918   glBindBuffer(GL_ARRAY_BUFFER, velocity_vbo);
     1919   glBufferData(GL_ARRAY_BUFFER, sizeof(vv), vv, GL_STATIC_DRAW);
     1920
     1921   GLuint time_vbo;
     1922   glGenBuffers(1, &time_vbo);
     1923   glBindBuffer(GL_ARRAY_BUFFER, time_vbo);
     1924   glBufferData(GL_ARRAY_BUFFER, sizeof(vt), vt, GL_STATIC_DRAW);
     1925
     1926   GLuint vao;
     1927   glGenVertexArrays(1, &vao);
     1928   glBindVertexArray(vao);
     1929
     1930   glBindBuffer(GL_ARRAY_BUFFER, velocity_vbo);
     1931   glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, NULL);
     1932
     1933   glBindBuffer(GL_ARRAY_BUFFER, time_vbo);
     1934   glVertexAttribPointer(1, 1, GL_FLOAT, GL_FALSE, 0, NULL);
     1935
     1936   glEnableVertexAttribArray(0);
     1937   glEnableVertexAttribArray(1);
     1938
     1939   return vao;
     1940}
     1941
    18881942void populateBuffers(vector<SceneObject*>& objects,
    18891943                  map<GLuint, BufferInfo>& shaderBufferInfo,
     
    22192273
    22202274void renderScene(map<GLuint, BufferInfo>& shaderBufferInfo,
    2221                   GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp,
    2222                   GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao,
     2275                  GLuint color_sp, GLuint asteroid_sp, GLuint texture_sp, GLuint laser_sp, GLuint explosion_sp,
     2276                  GLuint color_vao, GLuint asteroid_vao, GLuint texture_vao, GLuint laser_vao, GLuint explosion_vao,
    22232277                  GLuint colors_vbo, GLuint selected_colors_vbo,
    22242278                  SceneObject* selectedObject) {
     
    22592313   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);
    22602314
     2315   glDisable(GL_BLEND);
     2316
     2317   renderExplosion(explosion_sp, explosion_vao, 0);
     2318}
     2319
     2320void renderExplosion(GLuint explosion_sp, GLuint explosion_vao, GLuint tex) {
     2321   glEnable(GL_BLEND);
     2322   glEnable(GL_PROGRAM_POINT_SIZE);
     2323
     2324   //glActiveTexture(GL_TEXTURE1);
     2325   //glBindTexture(GL_TEXTURE_2D, tex);
     2326   glUseProgram(explosion_sp);
     2327
     2328   glBindVertexArray(explosion_vao);
     2329
     2330   glDrawArrays(GL_POINTS, 0, EXPLOSION_PARTICLE_COUNT);
     2331
     2332   glDisable(GL_PROGRAM_POINT_SIZE);
    22612333   glDisable(GL_BLEND);
    22622334}
Note: See TracChangeset for help on using the changeset viewer.