Changeset 9f9f9a7 in opengl-game


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.

Files:
1 added
5 edited

Legend:

Unmodified
Added
Removed
  • NewOpenGLGame.vcxproj

    r6877ef3 r9f9f9a7  
    168168    <None Include="texture.vert" />
    169169  </ItemGroup>
     170  <ItemGroup>
     171    <Image Include="laser.png" />
     172    <Image Include="test.png" />
     173  </ItemGroup>
    170174  <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
    171175  <ImportGroup Label="ExtensionTargets">
  • TODO.txt

    r6877ef3 r9f9f9a7  
    3030==================
    3131-Figure out why rendering doesn't work on the Windows laptop
    32 -Investigate switching to SFML, i.e. create a simple demo of SFML with OpenGL and ImGui
    3332-Implement lasers
    3433
    3534MAJOR TASKS DONE
    3635==================
     36-Investigate switching to SFML, i.e. create a simple demo of SFML with OpenGL and ImGui
     37  -The SFML graphics module requires the compatibility profile, which on OSX is only available for OpenGL < 3.0
     38  -I don't think ImGui would work with SFML
     39  -When the time comes, maybe just try using the networking and audio components of SFML
  • laser.frag

    r6877ef3 r9f9f9a7  
    11#version 410
    22
    3 in vec3 position_eye, color;
     3uniform sampler2D basic_texture;
     4uniform vec3 laser_color;
     5
     6in vec2 texture_coordinates;
     7in vec3 position_eye;
    48
    59out vec4 frag_color;
    610
    711void main() {
    8   frag_color = vec4(color, 1.0f);
     12  vec4 texel = texture(basic_texture, texture_coordinates);
     13
     14  frag_color = vec4(texel.r * laser_color.r, texel.g * laser_color.g, texel.b * laser_color.b, texel.a);
    915}
  • laser.vert

    r6877ef3 r9f9f9a7  
    44
    55layout(location = 0) in vec3 vertex_position;
    6 layout(location = 1) in vec3 vertex_color;
     6layout(location = 1) in vec2 vt;
    77
    8 out vec3 position_eye, color;
     8out vec2 texture_coordinates;
     9out vec3 position_eye;
    910
    1011void main() {
    1112  position_eye = vec3(view * vec4(vertex_position, 1.0));
    12   color = vertex_color;
     13  texture_coordinates = vt;
    1314
    1415  gl_Position = proj * vec4(position_eye, 1.0);
  • 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.