Changeset 9f9f9a7 in opengl-game for new-game.cpp


Ignore:
Timestamp:
Jul 17, 2018, 3:21:12 AM (6 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
fd6f465
Parents:
6877ef3
Message:

Improve the laser rendering algorithm by using a translucent white texture that gets blended with am arbitrary color.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • new-game.cpp

    r6877ef3 r9f9f9a7  
    4040 *    -The textures should be grayscale and have transparency
    4141 *    -The laser shader should take an input color to blend with the texture to give the lasers color
     42 *    -DONE
    4243 * -The lasers should be SceneObjects and drawn like all other objects
    4344 *    -DONE
    4445 * -Make lasers shoot from the ends of the ship's wings when the user presses a button and disappear after a second or so
     46 *
     47 * NOTE: Asteroid movement currently depends on framerate, fix this in a generic/reusable way
    4548 */
    4649
     
    233236#endif
    234237
    235    glfwWindowHint(GLFW_SAMPLES, 4);
     238   glfwWindowHint(GLFW_SAMPLES, 16);
    236239
    237240   GLFWwindow* window = NULL;
     
    304307   printf("OpenGL version supported %s\n", version);
    305308
     309   glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
     310
    306311   glEnable(GL_DEPTH_TEST);
    307312   glDepthFunc(GL_LESS);
     
    311316   // glFrontFace(GL_CW);
    312317
     318   /*
    313319   int x, y;
    314320   unsigned char* texImage = loadImage("test.png", &x, &y);
     
    320326   }
    321327
    322    GLuint tex = 0;
    323    glGenTextures(1, &tex);
     328   GLuint testTex = 0;
     329   glGenTextures(1, &testTex);
    324330   glActiveTexture(GL_TEXTURE0);
    325    glBindTexture(GL_TEXTURE_2D, tex);
     331   glBindTexture(GL_TEXTURE_2D, testTex);
     332   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
     333
     334   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
     335   glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
     336   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
     337   glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
     338   */
     339
     340   int x, y;
     341   unsigned char* texImage = loadImage("laser.png", &x, &y);
     342   if (texImage) {
     343      cout << "Laser texture loaded successfully!" << endl;
     344      cout << x << endl;
     345      cout << y << endl;
     346      printf("first 4 bytes are: %i %i %i %i\n", texImage[0], texImage[1], texImage[2], texImage[3]);
     347   }
     348
     349   GLuint laserTex = 0;
     350   glGenTextures(1, &laserTex);
     351   glActiveTexture(GL_TEXTURE0);
     352   glBindTexture(GL_TEXTURE_2D, laserTex);
    326353   glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, x, y, 0, GL_RGBA, GL_UNSIGNED_BYTE, texImage);
    327354
     
    948975   GLuint laser_view_mat_loc = glGetUniformLocation(laser_sp, "view");
    949976   GLuint laser_proj_mat_loc = glGetUniformLocation(laser_sp, "proj");
     977   GLuint laser_color_loc = glGetUniformLocation(laser_sp, "laser_color");
    950978
    951979   glUseProgram(color_sp);
     
    963991   glBindBufferRange(GL_UNIFORM_BUFFER, ub_binding_point, ubo, 0, GL_MAX_UNIFORM_BLOCK_SIZE);
    964992
     993
    965994   glUseProgram(laser_sp);
    966995   glUniformMatrix4fv(laser_view_mat_loc, 1, GL_FALSE, value_ptr(view_mat));
    967996   glUniformMatrix4fv(laser_proj_mat_loc, 1, GL_FALSE, value_ptr(proj_mat));
     997   glUniform3f(laser_color_loc, 0.2f, 1.0f, 0.2f);
    968998
    969999   bool cam_moved = false;
     
    15331563
    15341564   obj.points = {
    1535       start.x + width / 2, start.y, start.z - width,
    1536       start.x - width / 2, start.y, start.z - width,
     1565      start.x + width / 2, start.y, start.z - width / 2,
     1566      start.x - width / 2, start.y, start.z - width / 2,
    15371567      start.x - width / 2, start.y, start.z,
    1538       start.x + width / 2, start.y, start.z - width,
     1568      start.x + width / 2, start.y, start.z - width / 2,
    15391569      start.x - width / 2, start.y, start.z,
    15401570      start.x + width / 2, start.y, start.z,
    1541       end.x + width / 2,   end.y,   end.z + width,
    1542       end.x - width / 2,   end.y,   end.z + width,
    1543       start.x - width / 2, start.y, start.z - width,
    1544       end.x + width / 2,   end.y,   end.z + width,
    1545       start.x - width / 2, start.y, start.z - width,
    1546       start.x + width / 2, start.y, start.z - width,
     1571      end.x + width / 2,   end.y,   end.z + width / 2,
     1572      end.x - width / 2,   end.y,   end.z + width / 2,
     1573      start.x - width / 2, start.y, start.z - width / 2,
     1574      end.x + width / 2,   end.y,   end.z + width / 2,
     1575      start.x - width / 2, start.y, start.z - width / 2,
     1576      start.x + width / 2, start.y, start.z - width / 2,
    15471577      end.x + width / 2,   end.y,   end.z,
    15481578      end.x - width / 2,   end.y,   end.z,
    1549       end.x - width / 2,   end.y,   end.z + width,
     1579      end.x - width / 2,   end.y,   end.z + width / 2,
    15501580      end.x + width / 2,   end.y,   end.z,
    1551       end.x - width / 2,   end.y,   end.z + width,
    1552       end.x + width / 2,   end.y,   end.z + width,
     1581      end.x - width / 2,   end.y,   end.z + width / 2,
     1582      end.x + width / 2,   end.y,   end.z + width / 2,
    15531583   };
    15541584
     
    15751605   };
    15761606
     1607   obj.texcoords = {
     1608      1.0f, 0.5f,
     1609      0.0f, 0.5f,
     1610      0.0f, 0.0f,
     1611      1.0f, 0.5f,
     1612      0.0f, 0.0f,
     1613      1.0f, 0.0f,
     1614      1.0f, 0.51f,
     1615      0.0f, 0.51f,
     1616      0.0f, 0.49f,
     1617      1.0f, 0.51f,
     1618      0.0f, 0.49f,
     1619      1.0f, 0.49f,
     1620      1.0f, 1.0f,
     1621      0.0f, 1.0f,
     1622      0.0f, 0.5f,
     1623      1.0f, 1.0f,
     1624      0.0f, 0.5f,
     1625      1.0f, 0.5f,
     1626   };
     1627
    15771628   obj.num_points = obj.points.size() / 3;
    15781629
     
    17521803      glBindBuffer(GL_ARRAY_BUFFER, selected_colors_vbo);
    17531804      glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.selected_colors.size() * sizeof(GLfloat), &obj.selected_colors[0]);
    1754 
    1755       glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
    1756       glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]);
    1757 
     1805   }
     1806
     1807   glBindBuffer(GL_ARRAY_BUFFER, texcoords_vbo);
     1808   glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 2, obj.texcoords.size() * sizeof(GLfloat), &obj.texcoords[0]);
     1809
     1810   if (obj.type != TYPE_LASER) {
    17581811      glBindBuffer(GL_ARRAY_BUFFER, normals_vbo);
    17591812      glBufferSubData(GL_ARRAY_BUFFER, obj.vertex_vbo_offset * sizeof(GLfloat) * 3, obj.normals.size() * sizeof(GLfloat), &obj.normals[0]);
     
    18091862   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[texture_sp].vbo_base, shaderBufferInfo[texture_sp].vbo_offset);
    18101863
     1864   glEnable(GL_BLEND);
     1865
    18111866   glUseProgram(laser_sp);
    1812    glBindVertexArray(color_vao);
     1867   glBindVertexArray(texture_vao);
    18131868
    18141869   glDrawArrays(GL_TRIANGLES, shaderBufferInfo[laser_sp].vbo_base, shaderBufferInfo[laser_sp].vbo_offset);
     1870
     1871   glDisable(GL_BLEND);
    18151872}
    18161873
Note: See TracChangeset for help on using the changeset viewer.