Changeset 60578ce in opengl-game


Ignore:
Timestamp:
Dec 18, 2019, 2:48:28 AM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
a79be34
Parents:
7c929fc
Message:

In VulkanGame, make lighting work correctly in the ship shader with the model, view, and projection matrices all being applied

Files:
6 edited

Legend:

Unmodified
Added
Removed
  • makefile

    r7c929fc r60578ce  
    5959        $(CC) $(CXX_FLAGS) -o $@ $^ $(LIB_FLAGS) -DGAMEGUI_INCLUDE_VULKAN
    6060
    61 SRC_FILES = main-vulkan.cpp vulkan-game.cpp crash-logger.cpp logger.cpp vulkan-utils.cpp game-gui-sdl.cpp
    62 HEADER_FILES = vulkan-game.hpp crash-logger.hpp logger.hpp vulkan-utils.hpp game-gui-sdl.hpp game-gui.hpp graphics-pipeline_vulkan.hpp
     61SRC_FILES = main-vulkan.cpp vulkan-game.cpp crash-logger.cpp logger.cpp vulkan-utils.cpp utils.cpp game-gui-sdl.cpp
     62HEADER_FILES = vulkan-game.hpp crash-logger.hpp logger.hpp vulkan-utils.hpp utils.hpp game-gui-sdl.hpp game-gui.hpp graphics-pipeline_vulkan.hpp
    6363
    6464vulkangame: $(SRC_FILES) $(HEADER_FILES)
  • scene-notes.txt

    r7c929fc r60578ce  
    22In vulkan, it is upper left, so I set the projection matrix [1][1] cell to -1 to flip the y-axis and make it match opengl
    33
    4 +z goes into the screen (in Vulkan)
     4depth ranges ([-1, 1] in OpenGL, [0, 1] in Vulkan)
     5
     6Vulkan coordinates:
     7X+ points toward right
     8Y+ points toward down (but I flip it in vulkangame in the perspective matrix to point up)
     9Z+ points toward inside the screen
     10
     11In opengl, all 3 coordinates range from -1 to 1
     12
     13For the perspective matrix, after the vertex shader finishes, the x, y, and z of the final point are automatically divided
     14by the w. For testing the projection matrix in the console, I could do that manually as well.
  • shaders/ship.frag

    r7c929fc r60578ce  
    66layout(location = 2) in vec3 normal_eye;
    77layout(location = 3) in vec3 light_position_eye;
     8layout(location = 4) in vec3 light2_position_eye;
    89
    9 layout(location = 0) out vec4 outColor;
     10layout(location = 0) out vec4 frag_color;
    1011
    1112// fixed point light properties
     
    3435   vec3 Id = Ld * Kd * dot_prod;
    3536
     37  vec3 direction_to_light2_eye = normalize(light2_position_eye - position_eye);
     38  float dot_prod2 = max(dot(direction_to_light2_eye, normal_eye), 0.0);
     39
     40  // diffuse intensity
     41  vec3 Id2 = Ld * Kd * dot_prod2;
     42
    3643   vec3 surface_to_viewer_eye = normalize(-position_eye);
    3744
     
    4350   vec3 Is = Ls * Ks * specular_factor;
    4451
    45    outColor = vec4(Is + Id + Ia, 1.0);
     52   vec3 reflection_eye2 = reflect(-direction_to_light2_eye, normal_eye);
     53   float dot_prod_specular2 = max(dot(reflection_eye2, surface_to_viewer_eye), 0.0);
     54   float specular_factor2 = pow(dot_prod_specular2, specular_exponent);
     55
     56   // specular intensity
     57   vec3 Is2 = Ls * Ks * specular_factor2;
     58
     59   frag_color = vec4((Is + Id + Ia + Is2 + Id2 + Ia2)/2, 1.0);
    4660}
  • shaders/ship.vert

    r7c929fc r60578ce  
    2424layout(location = 2) out vec3 normal_eye;
    2525layout(location = 3) out vec3 light_position_eye;
     26layout(location = 4) out vec3 light2_position_eye;
    2627
    27 // fixed point light position
     28// fixed point light positions
    2829//vec3 light_position_world = vec3(0.0, 0.0, 2.0);
    29 //vec3 light_position_world = vec3(0.4, 1.5, 0.8);
    30 vec3 light_position_world = vec3(0.4, 0.0, 0.2);
     30//vec3 light2_position_world = vec3(0.0, 1.5, -0.1);
     31vec3 light_position_world = vec3(7.0, -7.0, -10.0);
     32vec3 light2_position_world = vec3(4.0, -3.0, -10.0);
    3133
    3234// TODO: This does not account for scaling in the model matrix
    3335// Check Anton's book to see how to fix this
    3436void main() {
    35    //position_eye = vec3(ubo.view * sbo.objects[obj_index].model * vec4(vertex_position, 1.0));
    36    position_eye = vec3(vec4(vertex_position, 1.0));
     37   position_eye = vec3(ubo.view * sbo.objects[obj_index].model * vec4(vertex_position, 1.0));
    3738
    3839   // Using 0.0 instead of 1.0 means translations won't effect the normal
    39    //normal_eye = normalize(vec3(ubo.view * sbo.objects[obj_index].model * vec4(vertex_normal, 0.0)));
    40    normal_eye = normalize(vec3(vec4(vertex_normal, 0.0)));
     40   normal_eye = normalize(vec3(ubo.view * sbo.objects[obj_index].model * vec4(vertex_normal, 0.0)));
     41
    4142   color = vertex_color;
    42    //light_position_eye = vec3(ubo.view * vec4(light_position_world, 1.0));
    43    light_position_eye = vec3(vec4(light_position_world, 1.0));
    4443
    45    //gl_Position = ubo.proj * vec4(position_eye, 1.0);
    46    gl_Position = vec4(vertex_position, 1.0);
     44   light_position_eye = vec3(ubo.view * vec4(light_position_world, 1.0));
     45   light2_position_eye = vec3(ubo.view * vec4(light2_position_world, 1.0));
     46
     47   gl_Position = ubo.proj * vec4(position_eye, 1.0);
    4748}
  • vulkan-game.cpp

    r7c929fc r60578ce  
    11#include "vulkan-game.hpp"
    2 
    3 #define GLM_FORCE_RADIANS
    4 #define GLM_FORCE_DEPTH_ZERO_TO_ONE
    52
    63#include <array>
     
    6259
    6360   initVulkan();
    64    initMatrices();
    6561   mainLoop();
    6662   cleanup();
     
    186182   createImageResources();
    187183   createFramebuffers();
     184
     185   initMatrices();
    188186
    189187   // TODO: Figure out how much of ubo creation and associated variables should be in the pipeline class
     
    505503      addObjectIndex<ShipVertex>(shipPipeline.getObjects().size(),
    506504      addVertexNormals<ShipVertex>({
    507          {{  0.5f,  -0.5f,   0.5f}, {0.0f, 0.6f, 0.0f}},
    508          {{ -0.5f,  -0.5f,   0.5f}, {0.0f, 0.6f, 0.0f}},
    509          {{ -0.5f,   0.5f,   0.5f}, {0.0f, 0.6f, 0.0f}},
    510          {{  0.5f,  -0.5f,   0.5f}, {0.0f, 0.6f, 0.0f}},
    511          {{ -0.5f,   0.5f,   0.5f}, {0.0f, 0.6f, 0.0f}},
    512          {{  0.5f,   0.5f,   0.5f}, {0.0f, 0.6f, 0.0f}},
    513 
    514          {{  0.3f,  -0.3f,   0.3f}, {0.0f, 0.0f, 0.7f}},
    515          {{ -0.3f,  -0.3f,   0.3f}, {0.0f, 0.0f, 0.7f}},
    516          {{ -0.3f,   0.3f,   0.3f}, {0.0f, 0.0f, 0.7f}},
    517          {{  0.3f,  -0.3f,   0.3f}, {0.0f, 0.0f, 0.7f}},
    518          {{ -0.3f,   0.3f,   0.3f}, {0.0f, 0.0f, 0.7f}},
    519          {{  0.3f,   0.3f,   0.3f}, {0.0f, 0.0f, 0.7f}},
     505         {{  40.0f,   40.0f,  72.0f}, {0.0f, 0.6f, 0.0f}},
     506         {{ -40.0f,   40.0f,  80.0f}, {0.0f, 0.6f, 0.0f}},
     507         {{ -40.0f,  -40.0f,  80.0f}, {0.0f, 0.6f, 0.0f}},
     508         {{  40.0f,   40.0f,  72.0f}, {0.0f, 0.6f, 0.0f}},
     509         {{ -40.0f,  -40.0f,  80.0f}, {0.0f, 0.6f, 0.0f}},
     510         {{  40.0f,  -40.0f,  72.0f}, {0.0f, 0.6f, 0.0f}},
     511
     512         {{   8.0f,    8.0f,  34.0f}, {0.0f, 0.0f, 0.7f}},
     513         {{  -8.0f,    8.0f,  30.0f}, {0.0f, 0.0f, 0.7f}},
     514         {{  -8.0f,   -8.0f,  30.0f}, {0.0f, 0.0f, 0.7f}},
     515         {{   8.0f,    8.0f,  34.0f}, {0.0f, 0.0f, 0.7f}},
     516         {{  -8.0f,   -8.0f,  30.0f}, {0.0f, 0.0f, 0.7f}},
     517         {{   8.0f,   -8.0f,  34.0f}, {0.0f, 0.0f, 0.7f}},
    520518      })), {
    521519         0,   1,   2,   3,   4,   5,
     
    551549
    552550   float cam_yaw = 0.0f;
    553    float cam_pitch = -50.0f;
     551   float cam_pitch = 0.0f; // -50.0f;
    554552
    555553   mat4 yaw_mat = rotate(mat4(1.0f), radians(-cam_yaw), vec3(0.0f, 1.0f, 0.0f));
  • vulkan-game.hpp

    r7c929fc r60578ce  
    11#ifndef _VULKAN_GAME_H
    22#define _VULKAN_GAME_H
     3
     4#define GLM_FORCE_RADIANS
     5#define GLM_FORCE_DEPTH_ZERO_TO_ONE // Since, in Vulkan, the depth range is 0 to 1 instead of -1 to 1
     6#define GLM_FORCE_LEFT_HANDED
    37
    48#include <glm/glm.hpp>
     
    5761      const float NEAR_CLIP = 0.1f;
    5862      const float FAR_CLIP = 100.0f;
    59       const float FOV_ANGLE = 67.0f;
     63      const float FOV_ANGLE = 67.0f; // means the camera lens goes from -33 deg to 33 def
    6064
    6165      vec3 cam_pos;
     
    203207      vec3 p3 = vertices[i+2].pos;
    204208
    205       vec3 normal = normalize(cross(p2 - p1, p3 - p1));
    206       //normal.z = -normal.z;
     209      // flip the y axis so that +y points up
     210      vec3 normal = -normalize(cross(p2 - p1, p3 - p1));
    207211
    208212      // Add the same normal for all 3 vertices
Note: See TracChangeset for help on using the changeset viewer.