Changeset 8e02b6b in opengl-game


Ignore:
Timestamp:
Nov 22, 2019, 6:27:13 PM (5 years ago)
Author:
Dmitry Portnoy <dmitry.portnoy@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
f97c5e7
Parents:
683dd55
git-author:
Dmitry Portnoy <dmitry.portnoy@…> (11/22/19 18:26:59)
git-committer:
Dmitry Portnoy <dmitry.portnoy@…> (11/22/19 18:27:13)
Message:

To move to a more generic way of updating the scene, rename updateUniformBuffers() to updateScene() in VulkanGame and move the code to copy the data into a new copyDataToMemory() function in VulkanUtils.

Files:
5 edited

Legend:

Unmodified
Added
Removed
  • shaders/ship.frag

    r683dd55 r8e02b6b  
    88layout(location = 0) out vec4 outColor;
    99
    10 /*
    1110// fixed point light properties
    1211vec3 Ls = vec3(1.0, 1.0, 1.0);
     
    1918vec3 Ka = vec3(0.2, 0.2, 0.2);
    2019float specular_exponent = 100.0; // specular 'power'
    21 */
    2220
    2321void main() {
    24   /*
    25   // ambient intensity
    26   vec3 Ia = La * Ka;
     22   // ambient intensity
     23   vec3 Ia = La * Ka;
    2724
    28   // ambient intensity
    29   vec3 Ia2 = La * Ka;
     25   // ambient intensity
     26   vec3 Ia2 = La * Ka;
    3027
    31   vec3 direction_to_light_eye = normalize(light_position_eye - position_eye);
    32   float dot_prod = max(dot(direction_to_light_eye, normal_eye), 0.0);
     28   //vec3 direction_to_light_eye = normalize(light_position_eye - position_eye);
     29   //float dot_prod = max(dot(direction_to_light_eye, normal_eye), 0.0);
    3330
    34   // diffuse intensity
    35   vec3 Id = Ld * color * dot_prod;
     31   // diffuse intensity
     32   //vec3 Id = Ld * color * dot_prod;
    3633
    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);
     34   //vec3 direction_to_light2_eye = normalize(light2_position_eye - position_eye);
     35   //float dot_prod2 = max(dot(direction_to_light2_eye, normal_eye), 0.0);
    3936
    40   // diffuse intensity
    41   vec3 Id2 = Ld * color * dot_prod2;
     37   // diffuse intensity
     38   //vec3 Id2 = Ld * color * dot_prod2;
    4239
    43   vec3 surface_to_viewer_eye = normalize(-position_eye);
     40   //vec3 surface_to_viewer_eye = normalize(-position_eye);
    4441
    45   vec3 reflection_eye = reflect(-direction_to_light_eye, normal_eye);
    46   float dot_prod_specular = max(dot(reflection_eye, surface_to_viewer_eye), 0.0);
    47   float specular_factor = pow(dot_prod_specular, specular_exponent);
     42   //vec3 reflection_eye = reflect(-direction_to_light_eye, normal_eye);
     43   //float dot_prod_specular = max(dot(reflection_eye, surface_to_viewer_eye), 0.0);
     44   //float specular_factor = pow(dot_prod_specular, specular_exponent);
    4845
    49   vec3 reflection_eye2 = reflect(-direction_to_light2_eye, normal_eye);
    50   float dot_prod_specular2 = max(dot(reflection_eye2, surface_to_viewer_eye), 0.0);
    51   float specular_factor2 = pow(dot_prod_specular2, specular_exponent);
     46   //vec3 reflection_eye2 = reflect(-direction_to_light2_eye, normal_eye);
     47   //float dot_prod_specular2 = max(dot(reflection_eye2, surface_to_viewer_eye), 0.0);
     48   //float specular_factor2 = pow(dot_prod_specular2, specular_exponent);
    5249
    53   // specular intensity
    54   vec3 Is = Ls * Ks * specular_factor;
    55   vec3 Is2 = Ls * Ks * specular_factor2;
     50   // specular intensity
     51   //vec3 Is = Ls * Ks * specular_factor;
     52   //vec3 Is2 = Ls * Ks * specular_factor2;
    5653
    57   outColor = vec4((Is + Id + Ia + Is2 + Id2 + Ia2)/2, 1.0);
    58   */
    59   outColor = vec4(color, 1.0);
     54   //frag_color = vec4((Is + Id + Ia + Is2 + Id2 + Ia2)/2, 1.0);
     55   outColor = vec4(color, 1.0);
    6056}
  • shaders/ship.vert

    r683dd55 r8e02b6b  
    22#extension GL_ARB_separate_shader_objects : enable
    33
     4// TODO: Figure out if the UniformBufferObject label is necessary and, if not, remove it
    45layout (binding = 0) uniform UniformBufferObject {
    56   mat4 model;
     
    1011layout(location = 0) in vec3 vertex_position;
    1112layout(location = 1) in vec3 vertex_color;
     13//layout(location = 2) in vec3 vertex_normal;
     14//layout(location = 3) in uint ubo_index;
    1215
    1316//out vec3 position_eye, normal_eye, color, light_position_eye, light2_position_eye;
     
    2023
    2124void main() {
    22   // ORIG: position_eye = vec3(view * model_mats[ubo_index] * vec4(vertex_position, 1.0));
    23   position_eye = vec3(ubo.view * ubo.model * vec4(vertex_position, 1.0));
    24   //normal_eye = normalize(vec3(view * model_mats[ubo_index] * vec4(vertex_normal, 0.0)));
    25   color = vertex_color;
    26   //light_position_eye = vec3(view * vec4(light_position_world, 1.0));
    27   //light2_position_eye = vec3(view * vec4(light2_position_world, 1.0));
     25   //position_eye = vec3(view * model_mats[ubo_index] * vec4(vertex_position, 1.0));
     26   position_eye = vec3(ubo.view * ubo.model * vec4(vertex_position, 1.0));
     27   //normal_eye = normalize(vec3(view * model_mats[ubo_index] * vec4(vertex_normal, 0.0)));
     28   color = vertex_color;
     29   //light_position_eye = vec3(view * vec4(light_position_world, 1.0));
     30   //light2_position_eye = vec3(view * vec4(light2_position_world, 1.0));
    2831
    29   gl_Position = ubo.proj * vec4(position_eye, 1.0);
     32   gl_Position = ubo.proj * vec4(position_eye, 1.0);
    3033}
  • vulkan-game.cpp

    r683dd55 r8e02b6b  
    2626   framebufferResized = false;
    2727
    28    ubo = {};
     28   modelMvpMats = {};
    2929}
    3030
     
    266266   mat4 T = translate(mat4(1.0f), vec3(-cam_pos.x, -cam_pos.y, -cam_pos.z));
    267267
    268    ubo.view = R * T;
    269 
    270    ubo.proj = perspective(radians(FOV_ANGLE), (float)swapChainExtent.width / (float)swapChainExtent.height, NEAR_CLIP, FAR_CLIP);
    271    ubo.proj[1][1] *= -1; // flip the y-axis so that +y is up
     268   modelMvpMats.view = R * T;
     269
     270   modelMvpMats.proj = perspective(radians(FOV_ANGLE), (float)swapChainExtent.width / (float)swapChainExtent.height, NEAR_CLIP, FAR_CLIP);
     271   modelMvpMats.proj[1][1] *= -1; // flip the y-axis so that +y is up
    272272}
    273273
     
    342342}
    343343
     344void VulkanGame::updateScene(uint32_t currentImage) {
     345   static auto startTime = chrono::high_resolution_clock::now();
     346
     347   auto currentTime = chrono::high_resolution_clock::now();
     348   float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
     349
     350   modelMvpMats.model =
     351      translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) *
     352      rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));
     353
     354   VulkanUtils::copyDataToMemory(device, uniformBuffersMemory[currentImage], modelMvpMats);
     355}
     356
    344357void VulkanGame::renderUI() {
    345358   SDL_SetRenderDrawColor(renderer, 0x00, 0x00, 0x00, 0x00);
     
    380393   }
    381394
    382    updateUniformBuffer(imageIndex);
     395   // TODO: Figure out a more elegant way to only do updates and render the UI once per scene render
     396   // Probably move some of the renderScene() code into a higher function that updates the UI, and renders
     397   // the UI and scene
     398   updateScene(imageIndex);
    383399
    384400   VkSubmitInfo submitInfo = {};
     
    880896
    881897void VulkanGame::createUniformBuffers() {
    882    VkDeviceSize bufferSize = sizeof(UniformBufferObject);
     898   VkDeviceSize bufferSize = sizeof(UBO_MvpMat);
    883899
    884900   uniformBuffers.resize(swapChainImages.size());
     
    893909      uniformBufferInfoList[i].buffer = uniformBuffers[i];
    894910      uniformBufferInfoList[i].offset = 0;
    895       uniformBufferInfoList[i].range = sizeof(UniformBufferObject);
     911      uniformBufferInfoList[i].range = sizeof(UBO_MvpMat);
    896912   }
    897913}
     
    10051021}
    10061022
    1007 void VulkanGame::updateUniformBuffer(uint32_t currentImage) {
    1008    static auto startTime = chrono::high_resolution_clock::now();
    1009 
    1010    auto currentTime = chrono::high_resolution_clock::now();
    1011    float time = chrono::duration<float, chrono::seconds::period>(currentTime - startTime).count();
    1012 
    1013    ubo.model =
    1014       translate(mat4(1.0f), vec3(0.0f, -2.0f, -0.0f)) *
    1015       rotate(mat4(1.0f), time * radians(90.0f), vec3(0.0f, 0.0f, 1.0f));
    1016 
    1017    void* data;
    1018    vkMapMemory(device, uniformBuffersMemory[currentImage], 0, sizeof(ubo), 0, &data);
    1019    memcpy(data, &ubo, sizeof(ubo));
    1020    vkUnmapMemory(device, uniformBuffersMemory[currentImage]);
    1021 }
    1022 
    10231023void VulkanGame::cleanupSwapChain() {
    10241024   VulkanUtils::destroyVulkanImage(device, depthImage);
  • vulkan-game.hpp

    r683dd55 r8e02b6b  
    2929};
    3030
    31 struct UniformBufferObject {
     31struct UBO_MvpMat {
    3232   alignas(16) mat4 model;
    3333   alignas(16) mat4 view;
     
    5151      vec3 cam_pos;
    5252
    53       UniformBufferObject ubo;
     53      UBO_MvpMat modelMvpMats;
    5454
    5555      GameGui* gui;
     
    120120      void initMatrices();
    121121      void mainLoop();
     122      void updateScene(uint32_t currentImage);
    122123      void renderUI();
    123124      void renderScene();
     
    147148
    148149      void recreateSwapChain();
    149       void updateUniformBuffer(uint32_t currentImage);
    150150
    151151      void cleanupSwapChain();
  • vulkan-utils.hpp

    r683dd55 r8e02b6b  
    9494            VkDeviceSize srcOffset, VkDeviceSize dstOffset, VkDeviceSize size, VkQueue graphicsQueue);
    9595
     96      template<class DataType>
     97      static void copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, const DataType& srcData);
     98
    9699      static bool hasStencilComponent(VkFormat format);
    97100
     
    122125}
    123126
     127template<class DataType>
     128void VulkanUtils::copyDataToMemory(VkDevice device, VkDeviceMemory bufferMemory, const DataType& srcData) {
     129   void* data;
     130   vkMapMemory(device, bufferMemory, 0, sizeof(DataType), 0, &data);
     131   memcpy(data, &srcData, sizeof(DataType));
     132   vkUnmapMemory(device, bufferMemory);
     133}
     134
    124135#endif // _VULKAN_UTILS_H
Note: See TracChangeset for help on using the changeset viewer.