Changeset e1308e8 in opengl-game


Ignore:
Timestamp:
Nov 27, 2019, 6:39:18 PM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
055750a
Parents:
06d959f
Message:

In VulkanGame, add normals to the ship pipeline and get lighting to work

Files:
3 edited

Legend:

Unmodified
Added
Removed
  • shaders/ship.frag

    r06d959f re1308e8  
    22#extension GL_ARB_separate_shader_objects : enable
    33
    4 //in vec3 position_eye, normal_eye, color, light_position_eye, light2_position_eye;
    54layout(location = 0) in vec3 position_eye;
    65layout(location = 1) in vec3 color;
    7 layout(location = 2) in vec3 light_position_eye;
    8 layout(location = 3) in vec3 light2_position_eye;
     6layout(location = 2) in vec3 normal_eye;
     7layout(location = 3) in vec3 light_position_eye;
    98
    109layout(location = 0) out vec4 outColor;
     
    1211// fixed point light properties
    1312vec3 Ls = vec3(1.0, 1.0, 1.0);
    14 vec3 Ld = vec3(1.0, 1.0, 1.0);
     13vec3 Ld = vec3(0.7, 0.7, 0.7);
    1514vec3 La = vec3(0.2, 0.2, 0.2);
    1615
    17 // surface reflectance
     16// reflectance of the object surface
    1817vec3 Ks = vec3(1.0, 1.0, 1.0);
    19 vec3 Kd = vec3(1.0, 1.5, 1.0);
     18vec3 Kd = vec3(1.0, 0.5, 0.0);
    2019vec3 Ka = vec3(0.2, 0.2, 0.2);
    2120float specular_exponent = 100.0; // specular 'power'
     
    2928
    3029   vec3 direction_to_light_eye = normalize(light_position_eye - position_eye);
    31    //float dot_prod = max(dot(direction_to_light_eye, normal_eye), 0.0);
     30   float dot_prod = max(dot(direction_to_light_eye, normal_eye), 0.0);
    3231
    3332   // diffuse intensity
    34    //vec3 Id = Ld * color * dot_prod;
     33   vec3 Id = Ld * color * dot_prod;
     34   //vec3 Id = Ld * Kd * dot_prod;
    3535
    36    vec3 direction_to_light2_eye = normalize(light2_position_eye - position_eye);
    37    //float dot_prod2 = max(dot(direction_to_light2_eye, normal_eye), 0.0);
     36   vec3 surface_to_viewer_eye = normalize(-position_eye);
    3837
    39    // diffuse intensity
    40    //vec3 Id2 = Ld * color * dot_prod2;
    41 
    42    //vec3 surface_to_viewer_eye = normalize(-position_eye);
    43 
    44    //vec3 reflection_eye = reflect(-direction_to_light_eye, normal_eye);
    45    //float dot_prod_specular = max(dot(reflection_eye, surface_to_viewer_eye), 0.0);
    46    //float specular_factor = pow(dot_prod_specular, specular_exponent);
    47 
    48    //vec3 reflection_eye2 = reflect(-direction_to_light2_eye, normal_eye);
    49    //float dot_prod_specular2 = max(dot(reflection_eye2, surface_to_viewer_eye), 0.0);
    50    //float specular_factor2 = pow(dot_prod_specular2, specular_exponent);
     38   vec3 reflection_eye = reflect(-direction_to_light_eye, normal_eye);
     39   float dot_prod_specular = max(dot(reflection_eye, surface_to_viewer_eye), 0.0);
     40   float specular_factor = pow(dot_prod_specular, specular_exponent);
    5141
    5242   // specular intensity
    53    //vec3 Is = Ls * Ks * specular_factor;
    54    //vec3 Is2 = Ls * Ks * specular_factor2;
     43   vec3 Is = Ls * Ks * specular_factor;
    5544
    56    //outColor = vec4((Is + Id + Ia + Is2 + Id2 + Ia2)/2, 1.0);
    57    outColor = vec4(color, 1.0);
     45   outColor = vec4(Is + Id + Ia, 1.0);
    5846}
  • shaders/ship.vert

    r06d959f re1308e8  
    22#extension GL_ARB_separate_shader_objects : enable
    33
    4 // TODO: Figure out if the UniformBufferObject label is necessary and, if not, remove it
    54layout (binding = 0) uniform UniformBufferObject {
    65   mat4 model;
     
    1110layout(location = 0) in vec3 vertex_position;
    1211layout(location = 1) in vec3 vertex_color;
    13 //layout(location = 2) in vec3 vertex_normal;
     12layout(location = 2) in vec3 vertex_normal;
    1413//layout(location = 3) in uint ubo_index;
    1514
    16 //out vec3 position_eye, normal_eye, color, light_position_eye, light2_position_eye;
    1715layout(location = 0) out vec3 position_eye;
    1816layout(location = 1) out vec3 color;
    19 layout(location = 2) out vec3 light_position_eye;
    20 layout(location = 3) out vec3 light2_position_eye;
     17layout(location = 2) out vec3 normal_eye;
     18layout(location = 3) out vec3 light_position_eye;
    2119
    2220// fixed point light position
    23 vec3 light_position_world = vec3(0.0, 0.0, 2.0);
    24 vec3 light2_position_world = vec3(0.0, 1.5, -0.1);
     21//vec3 light_position_world = vec3(0.0, 0.0, 2.0);
     22vec3 light_position_world = vec3(0.4, 1.5, 0.8);
     23//vec3 light_position_world = vec3(0.0, 1.0, -1.0);
    2524
     25// TODO: This does not account for scaling in the model matrix
     26// Check Anton's book to see how to fix this
    2627void main() {
     28   position_eye = vec3(ubo.view * ubo.model * vec4(vertex_position, 1.0));
    2729   //position_eye = vec3(view * model_mats[ubo_index] * vec4(vertex_position, 1.0));
    28    position_eye = vec3(ubo.view * ubo.model * vec4(vertex_position, 1.0));
     30
     31   // Using 0.0 instead of 1.0 means translations won't effect the normal
     32   normal_eye = normalize(vec3(ubo.view * ubo.model * vec4(vertex_normal, 0.0)));
    2933   //normal_eye = normalize(vec3(view * model_mats[ubo_index] * vec4(vertex_normal, 0.0)));
    3034   color = vertex_color;
    3135   light_position_eye = vec3(ubo.view * vec4(light_position_world, 1.0));
    32    light2_position_eye = vec3(ubo.view * vec4(light2_position_world, 1.0));
    3336
    3437   gl_Position = ubo.proj * vec4(position_eye, 1.0);
  • vulkan-game.cpp

    r06d959f re1308e8  
    248248   shipPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ShipVertex::pos));
    249249   shipPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ShipVertex::color));
     250   shipPipeline.addAttribute(VK_FORMAT_R32G32B32_SFLOAT, offset_of(&ShipVertex::normal));
    250251
    251252   createUniformBuffers<UBO_MvpMat>(uniformBuffers_shipPipeline, uniformBuffersMemory_shipPipeline, uniformBufferInfoList_shipPipeline);
     
    254255      VK_SHADER_STAGE_VERTEX_BIT, &uniformBufferInfoList_shipPipeline);
    255256
    256    shipPipeline.addObject({
     257   // TODO: With the normals, indexing basically becomes pointless since no vertices will have exactly
     258   // the same data. Add an option to make some pipelines not use indexing
     259   shipPipeline.addObject(addVertexNormals<ShipVertex>({
    257260         //back
    258261         {{ -0.5f,   0.3f,   0.0f}, {0.0f, 0.0f, 0.3f}},
     
    450453         {{  1.5f,   0.0f,   0.0f}, {0.0f, 0.0f, 0.3f}},
    451454         {{  1.3f,   0.0f,  -0.3f}, {0.0f, 0.0f, 0.3f}},
    452       }, {
     455      }), {
    453456           0,   1,   2,   3,   4,   5,
    454457           6,   7,   8,   9,  10,  11,
Note: See TracChangeset for help on using the changeset viewer.