Changeset 06d959f in opengl-game for vulkan-game.hpp


Ignore:
Timestamp:
Nov 27, 2019, 5:19:23 PM (5 years ago)
Author:
Dmitry Portnoy <dmp1488@…>
Branches:
feature/imgui-sdl, master, points-test
Children:
e1308e8
Parents:
0cf1a23
Message:

Add an addVertexNormals method to VulkanGame that calculates the normals given a list of vertices

File:
1 edited

Legend:

Unmodified
Added
Removed
  • vulkan-game.hpp

    r0cf1a23 r06d959f  
    3232   vec3 pos;
    3333   vec3 color;
     34   vec3 normal;
    3435};
    3536
     
    158159      void createSyncObjects();
    159160
     161      template<class VertexType>
     162      vector<VertexType> addVertexNormals(vector<VertexType> vertices);
     163
    160164      template<class UniformType>
    161165      void createUniformBuffers(vector<VkBuffer>& buffers, vector<VkDeviceMemory>& buffersMemory,
     
    191195}
    192196
     197template<class VertexType>
     198vector<VertexType> VulkanGame::addVertexNormals(vector<VertexType> vertices) {
     199   for (unsigned int i = 0; i < vertices.size(); i += 3) {
     200      vec3 p1 = vertices[i].pos;
     201      vec3 p2 = vertices[i+1].pos;
     202      vec3 p3 = vertices[i+2].pos;
     203
     204      vec3 normal = normalize(cross(p2 - p1, p3 - p1));
     205      normal.z = -normal.z;
     206
     207      // Add the same normal for all 3 vertices
     208      vertices[i].normal = normal;
     209      vertices[i+1].normal = normal;
     210      vertices[i+2].normal = normal;
     211   }
     212
     213   return vertices;
     214}
     215
    193216#endif // _VULKAN_GAME_H
Note: See TracChangeset for help on using the changeset viewer.