diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..2c2efb8 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +.cache/** +bin/** +build/** +compile_commands.json diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..bdc22aa --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,126 @@ +cmake_minimum_required(VERSION 3.10) + +project(VulkanRenderer + LANGUAGES CXX +) + +set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_SOURCE_DIR}/bin) +set(CMAKE_EXPORT_COMPILE_COMMANDS ON) + +if (MSVC) + message(FATAL_ERROR + "Use the provided Visual Studio solution file to build this project with visual studio.\n" + "This cmake file is meant for non-windows platforms.") +else () + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wextra -Wpedantic -Werror") + # Display unused parameters as warnings + # set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-error=unused-parameter") + # Suppress warnings about unused parameters + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wno-unused-parameter") +endif () + +if (NOT WIN32) + include(FetchContent) + + set(GLFW_LIBRARY_TYPE STATIC CACHE STRING "" FORCE) + set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE) + set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE) + set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE) + set(GLFW_INSTALL OFF CACHE BOOL "" FORCE) + FetchContent_Declare( + glfw + GIT_REPOSITORY https://github.com/glfw/glfw + GIT_TAG 3.4 + EXCLUDE_FROM_ALL) + FetchContent_MakeAvailable(glfw) + + find_package(Vulkan REQUIRED) +endif () + +add_executable(VulkanRenderer) +target_sources(VulkanRenderer + PRIVATE + code/Fileio.cpp + code/Fileio.h + code/Scene.cpp + code/Scene.h + code/application.cpp + code/application.h + code/main.cpp + code/Math/Bounds.cpp + code/Math/Bounds.h + code/Math/LCP.cpp + code/Math/LCP.h + code/Math/Matrix.h + code/Math/Quat.h + code/Math/Vector.h + code/Physics/Body.cpp + code/Physics/Body.h + code/Physics/Broadphase.cpp + code/Physics/Broadphase.h + code/Physics/Constraints.cpp + code/Physics/Constraints.h + code/Physics/Contact.cpp + code/Physics/Contact.h + code/Physics/GJK.cpp + code/Physics/GJK.h + code/Physics/Intersections.cpp + code/Physics/Intersections.h + code/Physics/Manifold.cpp + code/Physics/Manifold.h + code/Physics/Shapes.cpp + code/Physics/Shapes.h + code/Physics/Constraints/ConstraintBase.h + code/Physics/Constraints/ConstraintConstantVelocity.cpp + code/Physics/Constraints/ConstraintConstantVelocity.h + code/Physics/Constraints/ConstraintDistance.cpp + code/Physics/Constraints/ConstraintDistance.h + code/Physics/Constraints/ConstraintHinge.cpp + code/Physics/Constraints/ConstraintHinge.h + code/Physics/Constraints/ConstraintMotor.cpp + code/Physics/Constraints/ConstraintMotor.h + code/Physics/Constraints/ConstraintMover.cpp + code/Physics/Constraints/ConstraintMover.h + code/Physics/Constraints/ConstraintOrientation.cpp + code/Physics/Constraints/ConstraintOrientation.h + code/Physics/Constraints/ConstraintPenetration.cpp + code/Physics/Constraints/ConstraintPenetration.h + code/Physics/Shapes/ShapeBase.h + code/Physics/Shapes/ShapeBox.cpp + code/Physics/Shapes/ShapeBox.h + code/Physics/Shapes/ShapeConvex.cpp + code/Physics/Shapes/ShapeConvex.h + code/Physics/Shapes/ShapeSphere.cpp + code/Physics/Shapes/ShapeSphere.h + code/Renderer/Buffer.cpp + code/Renderer/Buffer.h + code/Renderer/Descriptor.cpp + code/Renderer/Descriptor.h + code/Renderer/DeviceContext.cpp + code/Renderer/DeviceContext.h + code/Renderer/Fence.cpp + code/Renderer/Fence.h + code/Renderer/FrameBuffer.cpp + code/Renderer/FrameBuffer.h + code/Renderer/Image.cpp + code/Renderer/Image.h + code/Renderer/OffscreenRenderer.cpp + code/Renderer/OffscreenRenderer.h + code/Renderer/Pipeline.cpp + code/Renderer/Pipeline.h + code/Renderer/Samplers.cpp + code/Renderer/Samplers.h + code/Renderer/SwapChain.cpp + code/Renderer/SwapChain.h + code/Renderer/model.cpp + code/Renderer/model.h + code/Renderer/shader.cpp + code/Renderer/shader.h +) + +target_link_libraries(VulkanRenderer + PRIVATE + Vulkan::Vulkan + glfw +) + diff --git a/code/Fileio.cpp b/code/Fileio.cpp index b6fd852..6ac9195 100644 --- a/code/Fileio.cpp +++ b/code/Fileio.cpp @@ -7,8 +7,16 @@ #include #include -#include -#define GetCurrentDir _getcwd +#if defined(_WIN32) + #include + #define GetCurrentDir _getcwd +#elif defined(__linux__) || defined(__APPLE__) + #include + #define GetCurrentDir getcwd +#else + #error "Add platform specific code to get current working directory" +#endif + static char g_ApplicationDirectory[ FILENAME_MAX ]; static bool g_WasInitialized = false; @@ -53,12 +61,13 @@ Opens the file and stores it in data bool GetFileData( const char * fileNameLocal, unsigned char ** data, unsigned int & size ) { InitializeFileSystem(); - char fileName[ 2048 ]; - sprintf( fileName, "%s/%s", g_ApplicationDirectory, fileNameLocal ); - + char fileName[ FILENAME_MAX ]; + int written = snprintf( fileName, sizeof(fileName), "%s/%s", g_ApplicationDirectory, fileNameLocal ); + assert( written < FILENAME_MAX ); + // open file for reading FILE * file = fopen( fileName, "rb" ); - + // handle any errors if ( file == NULL ) { return false; @@ -114,8 +123,9 @@ SaveFileData bool SaveFileData( const char * fileNameLocal, const void * data, unsigned int size ) { InitializeFileSystem(); - char fileName[ 2048 ]; - sprintf( fileName, "%s/%s", g_ApplicationDirectory, fileNameLocal ); + char fileName[ FILENAME_MAX ]; + int written = snprintf( fileName, FILENAME_MAX, "%s/%s", g_ApplicationDirectory, fileNameLocal ); + assert( written < FILENAME_MAX ); // open file for writing FILE * file = fopen( fileName, "wb" ); @@ -139,4 +149,4 @@ bool SaveFileData( const char * fileNameLocal, const void * data, unsigned int s fclose( file ); printf( "Write file was success %s\n", fileName ); return true; -} \ No newline at end of file +} diff --git a/code/Math/Quat.h b/code/Math/Quat.h index 982e902..94a2c39 100644 --- a/code/Math/Quat.h +++ b/code/Math/Quat.h @@ -43,24 +43,24 @@ class Quat { }; inline Quat::Quat() : +w( 1 ), x( 0 ), y( 0 ), -z( 0 ), -w( 1 ) { +z( 0 ) { } inline Quat::Quat( const Quat &rhs ) : +w( rhs.w ), x( rhs.x ), y( rhs.y ), -z( rhs.z ), -w( rhs.w ) { +z( rhs.z ) { } inline Quat::Quat( float X, float Y, float Z, float W ) : +w( W ), x( X ), y( Y ), -z( Z ), -w( W ) { +z( Z ) { } inline Quat::Quat( Vec3 n, const float angleRadians ) { @@ -179,4 +179,4 @@ inline Mat3 Quat::ToMat3() const { mat.rows[ 1 ] = RotatePoint( mat.rows[ 1 ] ); mat.rows[ 2 ] = RotatePoint( mat.rows[ 2 ] ); return mat; -} \ No newline at end of file +} diff --git a/code/Physics/Constraints/ConstraintDistance.cpp b/code/Physics/Constraints/ConstraintDistance.cpp index 7138daf..92623c1 100644 --- a/code/Physics/Constraints/ConstraintDistance.cpp +++ b/code/Physics/Constraints/ConstraintDistance.cpp @@ -7,6 +7,10 @@ void ConstraintDistance::PreSolve( const float dt_sec ) { // TODO: Add code } +void ConstraintDistance::Solve() { + // TODO: Add code +} + void ConstraintDistance::PostSolve() { // TODO: Add code -} \ No newline at end of file +} diff --git a/code/Physics/Constraints/ConstraintDistance.h b/code/Physics/Constraints/ConstraintDistance.h index 30405c6..4ebb945 100644 --- a/code/Physics/Constraints/ConstraintDistance.h +++ b/code/Physics/Constraints/ConstraintDistance.h @@ -12,8 +12,8 @@ ConstraintDistance class ConstraintDistance : public Constraint { public: ConstraintDistance() : Constraint(), - m_cachedLambda( 1 ), - m_Jacobian( 1, 12 ) { + m_Jacobian( 1, 12 ), + m_cachedLambda( 1 ) { m_cachedLambda.Zero(); m_baumgarte = 0.0f; } @@ -27,4 +27,4 @@ class ConstraintDistance : public Constraint { VecN m_cachedLambda; float m_baumgarte; -}; \ No newline at end of file +}; diff --git a/code/Physics/Manifold.h b/code/Physics/Manifold.h index 52df8a7..7fa634b 100644 --- a/code/Physics/Manifold.h +++ b/code/Physics/Manifold.h @@ -29,11 +29,11 @@ class Manifold { static const int MAX_CONTACTS = 4; contact_t m_contacts[ MAX_CONTACTS ]; - int m_numContacts; - Body * m_bodyA; Body * m_bodyB; + int m_numContacts; + ConstraintPenetration m_constraints[ MAX_CONTACTS ]; friend class ManifoldCollector; @@ -59,4 +59,4 @@ class ManifoldCollector { public: std::vector< Manifold > m_manifolds; -}; \ No newline at end of file +}; diff --git a/code/Physics/Shapes/ShapeBase.h b/code/Physics/Shapes/ShapeBase.h index a14af83..fe5fd84 100644 --- a/code/Physics/Shapes/ShapeBase.h +++ b/code/Physics/Shapes/ShapeBase.h @@ -15,6 +15,8 @@ Shape */ class Shape { public: + virtual ~Shape() = default; + virtual Mat3 InertiaTensor() const = 0; virtual Bounds GetBounds( const Vec3 & pos, const Quat & orient ) const = 0; diff --git a/code/Renderer/Buffer.cpp b/code/Renderer/Buffer.cpp index d2cfb0d..6a0e413 100644 --- a/code/Renderer/Buffer.cpp +++ b/code/Renderer/Buffer.cpp @@ -4,6 +4,7 @@ #include "Buffer.h" #include #include +#include /* ================================================================================================ @@ -101,4 +102,4 @@ Buffer::UnmapBuffer */ void Buffer::UnmapBuffer( DeviceContext * device ) { vkUnmapMemory( device->m_vkDevice, m_vkBufferMemory ); -} \ No newline at end of file +} diff --git a/code/Renderer/Descriptor.cpp b/code/Renderer/Descriptor.cpp index ddaab41..ac021bf 100644 --- a/code/Renderer/Descriptor.cpp +++ b/code/Renderer/Descriptor.cpp @@ -68,7 +68,6 @@ bool Descriptors::Create( DeviceContext * device, const CreateParms_t & parms ) int idx = 0; - const int numVertexUniforms = parms.numUniformsVertex; for ( int i = 0; i < parms.numUniformsVertex; i++ ) { uniformBindings[ idx ].binding = idx; uniformBindings[ idx ].descriptorCount = 1; @@ -233,4 +232,4 @@ void Descriptor::BindDescriptor( DeviceContext * device, VkCommandBuffer vkComma vkUpdateDescriptorSets( device->m_vkDevice, (uint32_t)numDescriptors, descriptorWrites, 0, nullptr ); vkCmdBindDescriptorSets( vkCommandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS, pso->m_vkPipelineLayout, 0, 1, &m_parent->m_vkDescriptorSets[ m_id ], 0, nullptr ); -} \ No newline at end of file +} diff --git a/code/Renderer/Descriptor.h b/code/Renderer/Descriptor.h index 2156151..34a4b53 100644 --- a/code/Renderer/Descriptor.h +++ b/code/Renderer/Descriptor.h @@ -8,6 +8,7 @@ class DeviceContext; class Buffer; class Pipeline; class Image; +class Descriptors; struct RenderModel; /* @@ -77,4 +78,4 @@ class Descriptors { m_numDescriptorUsed++; return descriptor; } -}; \ No newline at end of file +}; diff --git a/code/Renderer/DeviceContext.cpp b/code/Renderer/DeviceContext.cpp index 26760dc..93c00db 100644 --- a/code/Renderer/DeviceContext.cpp +++ b/code/Renderer/DeviceContext.cpp @@ -4,6 +4,8 @@ #include "DeviceContext.h" #include "Fence.h" #include +#include +#include /* ================================================================================================ @@ -145,7 +147,7 @@ bool PhysicalDeviceProperties::HasExtensions( const char ** extensions, const in const char * extension = extensions[ i ]; bool doesExist = false; - for ( int j = 0; j < m_vkExtensionProperties.size(); j++ ) { + for ( size_t j = 0; j < m_vkExtensionProperties.size(); j++ ) { if ( 0 == strcmp( extension, m_vkExtensionProperties[ j ].extensionName ) ) { doesExist = true; break; @@ -207,7 +209,7 @@ bool DeviceContext::CreateInstance( bool enableLayers, const std::vector< const std::vector< VkLayerProperties > layerProperties( numLayers ); vkEnumerateInstanceLayerProperties( &numLayers, layerProperties.data() ); - for ( int i = 0; i < numLayers; i++ ) { + for ( uint32_t i = 0; i < numLayers; i++ ) { printf( "Layer: %i %s\n", i, layerProperties[ i ].layerName ); if ( 0 == strcmp( "VK_LAYER_KHRONOS_validation", layerProperties[ i ].layerName ) ) { @@ -368,7 +370,7 @@ bool DeviceContext::CreatePhysicalDevice() { // // Select a physical device // - for ( int i = 0; i < m_physicalDevices.size(); i++ ) { + for ( size_t i = 0; i < m_physicalDevices.size(); i++ ) { const PhysicalDeviceProperties & deviceProperties = m_physicalDevices[ i ]; // Ignore non-drawing devices @@ -388,7 +390,7 @@ bool DeviceContext::CreatePhysicalDevice() { // Find graphics queue family // int graphicsIdx = -1; - for ( int j = 0; j < deviceProperties.m_vkQueueFamilyProperties.size(); ++j ) { + for ( size_t j = 0; j < deviceProperties.m_vkQueueFamilyProperties.size(); ++j ) { const VkQueueFamilyProperties & props = deviceProperties.m_vkQueueFamilyProperties[ j ]; if ( props.queueCount == 0 ) { @@ -410,7 +412,7 @@ bool DeviceContext::CreatePhysicalDevice() { // Find present queue family // int presentIdx = -1; - for ( int j = 0; j < deviceProperties.m_vkQueueFamilyProperties.size(); ++j ) { + for ( size_t j = 0; j < deviceProperties.m_vkQueueFamilyProperties.size(); ++j ) { const VkQueueFamilyProperties & props = deviceProperties.m_vkQueueFamilyProperties[ j ]; if ( props.queueCount == 0 ) { @@ -651,4 +653,4 @@ int DeviceContext::GetAligendUniformByteOffset( const int offset ) const { const int n = ( offset + minByteOffsetAlignment - 1 ) / minByteOffsetAlignment; const int alignedOffset = n * minByteOffsetAlignment; return alignedOffset; -} \ No newline at end of file +} diff --git a/code/Renderer/Pipeline.h b/code/Renderer/Pipeline.h index 2bcda5c..b723f7b 100644 --- a/code/Renderer/Pipeline.h +++ b/code/Renderer/Pipeline.h @@ -3,6 +3,7 @@ // #pragma once #include +#include #include "Descriptor.h" #include "Buffer.h" @@ -66,4 +67,4 @@ class Pipeline { // VkPipelineLayout m_vkPipelineLayout; VkPipeline m_vkPipeline; -}; \ No newline at end of file +}; diff --git a/code/Renderer/Samplers.cpp b/code/Renderer/Samplers.cpp index ccd8e7d..6766838 100644 --- a/code/Renderer/Samplers.cpp +++ b/code/Renderer/Samplers.cpp @@ -4,6 +4,7 @@ #include "Samplers.h" #include #include +#include VkSampler Samplers::m_samplerStandard; VkSampler Samplers::m_samplerDepth; @@ -75,4 +76,4 @@ Samplers::Cleanup void Samplers::Cleanup( DeviceContext * device ) { vkDestroySampler( device->m_vkDevice, m_samplerStandard, nullptr ); vkDestroySampler( device->m_vkDevice, m_samplerDepth, nullptr ); -} \ No newline at end of file +} diff --git a/code/Renderer/SwapChain.cpp b/code/Renderer/SwapChain.cpp index 0629bf6..97833ed 100644 --- a/code/Renderer/SwapChain.cpp +++ b/code/Renderer/SwapChain.cpp @@ -5,6 +5,8 @@ #include "DeviceContext.h" #include "../Fileio.h" #include +#include +#include /* ==================================================== @@ -37,7 +39,7 @@ void SwapChain::Cleanup( DeviceContext * device ) { vkFreeMemory( device->m_vkDevice, m_vkDepthImageMemory, nullptr ); // frame buffer - for ( int i = 0; i < m_vkFramebuffers.size(); i++ ) { + for ( size_t i = 0; i < m_vkFramebuffers.size(); i++ ) { vkDestroyFramebuffer( device->m_vkDevice, m_vkFramebuffers[ i ], nullptr ); } @@ -45,7 +47,7 @@ void SwapChain::Cleanup( DeviceContext * device ) { vkDestroyRenderPass( device->m_vkDevice, m_vkRenderPass, nullptr ); // color buffers - for ( int i = 0; i < m_vkImageViews.size(); i++ ) { + for ( size_t i = 0; i < m_vkImageViews.size(); i++ ) { vkDestroyImageView( device->m_vkDevice, m_vkImageViews[ i ], nullptr ); } @@ -101,7 +103,7 @@ bool SwapChain::Create( DeviceContext * device, int width, int height ) { surfaceFormat.colorSpace = VK_COLOR_SPACE_SRGB_NONLINEAR_KHR; VkPresentModeKHR presentMode = VK_PRESENT_MODE_FIFO_KHR; - for ( int i = 0; i < physicalDeviceInfo.m_vkPresentModes.size(); i++ ) { + for ( size_t i = 0; i < physicalDeviceInfo.m_vkPresentModes.size(); i++ ) { if ( VK_PRESENT_MODE_MAILBOX_KHR == physicalDeviceInfo.m_vkPresentModes[ i ] ) { presentMode = VK_PRESENT_MODE_MAILBOX_KHR; break; @@ -486,4 +488,4 @@ SwapChain::EndRenderPass */ void SwapChain::EndRenderPass( DeviceContext * device ) { vkCmdEndRenderPass( device->m_vkCommandBuffers[ m_currentImageIndex ] ); -} \ No newline at end of file +} diff --git a/code/Renderer/model.cpp b/code/Renderer/model.cpp index e9f9cad..f3e92e9 100644 --- a/code/Renderer/model.cpp +++ b/code/Renderer/model.cpp @@ -8,7 +8,9 @@ #include "../Physics/Shapes.h" #include -#pragma warning( disable : 4996 ) +#if defined( _MSC_VER ) + #pragma warning( disable : 4996 ) +#endif /* ==================================================== @@ -360,7 +362,7 @@ void FillSphere( Model & model, const float radius ) { FillCubeTessellated( model, (int)s ); // Project the tessellated cube onto a sphere - for ( int i = 0; i < model.m_vertices.size(); i++ ) { + for ( size_t i = 0; i < model.m_vertices.size(); i++ ) { Vec3 xyz = model.m_vertices[ i ].xyz; xyz.Normalize(); @@ -404,7 +406,7 @@ bool Model::BuildFromShape( const Shape * shape ) { FillCubeTessellated( *this, 0 ); Vec3 halfdim = ( shapeBox->m_bounds.maxs - shapeBox->m_bounds.mins ) * 0.5f; Vec3 center = ( shapeBox->m_bounds.maxs + shapeBox->m_bounds.mins ) * 0.5f; - for ( int v = 0; v < m_vertices.size(); v++ ) { + for ( size_t v = 0; v < m_vertices.size(); v++ ) { for ( int i = 0; i < 3; i++ ) { m_vertices[ v ].xyz[ i ] *= halfdim[ i ]; m_vertices[ v ].xyz[ i ] += center[ i ]; @@ -417,7 +419,7 @@ bool Model::BuildFromShape( const Shape * shape ) { m_indices.clear(); FillSphere( *this, shapeSphere->m_radius ); - for ( int v = 0; v < m_vertices.size(); v++ ) { + for ( size_t v = 0; v < m_vertices.size(); v++ ) { for ( int i = 0; i < 3; i++ ) { m_vertices[ v ].xyz[ i ] *= shapeSphere->m_radius; } @@ -436,10 +438,10 @@ bool Model::BuildFromShape( const Shape * shape ) { // Calculate smoothed normals std::vector< Vec3 > normals; normals.reserve( hullPts.size() ); - for ( int i = 0; i < hullPts.size(); i++ ) { + for ( int i = 0; i < (int)hullPts.size(); i++ ) { Vec3 norm( 0.0f ); - for ( int t = 0; t < hullTris.size(); t++ ) { + for ( size_t t = 0; t < hullTris.size(); t++ ) { const tri_t & tri = hullTris[ t ]; if ( i != tri.a && i != tri.b && i != tri.c ) { continue; @@ -459,7 +461,7 @@ bool Model::BuildFromShape( const Shape * shape ) { } m_vertices.reserve( hullPts.size() ); - for ( int i = 0; i < hullPts.size(); i++ ) { + for ( int i = 0; i < (int)hullPts.size(); i++ ) { vert_t vert; memset( &vert, 0, sizeof( vert_t ) ); @@ -479,7 +481,7 @@ bool Model::BuildFromShape( const Shape * shape ) { } m_indices.reserve( hullTris.size() * 3 ); - for ( int i = 0; i < hullTris.size(); i++ ) { + for ( int i = 0; i < (int)hullTris.size(); i++ ) { m_indices.push_back( hullTris[ i ].a ); m_indices.push_back( hullTris[ i ].b ); m_indices.push_back( hullTris[ i ].c ); @@ -495,8 +497,6 @@ Model::MakeVBO ================================ */ bool Model::MakeVBO( DeviceContext * device ) { - VkCommandBuffer vkCommandBuffer = device->m_vkCommandBuffers[ 0 ]; - int bufferSize; // Create Vertex Buffer @@ -547,4 +547,4 @@ void Model::DrawIndexed( VkCommandBuffer vkCommandBUffer ) { // Issue draw command vkCmdDrawIndexed( vkCommandBUffer, (uint32_t)m_indices.size(), 1, 0, 0, 0 ); -} \ No newline at end of file +} diff --git a/code/Renderer/shader.cpp b/code/Renderer/shader.cpp index 66e1d94..01cd0c9 100644 --- a/code/Renderer/shader.cpp +++ b/code/Renderer/shader.cpp @@ -4,6 +4,7 @@ #include "shader.h" #include "../Fileio.h" #include +#include #include "model.h" @@ -54,7 +55,7 @@ bool Shader::Load( DeviceContext * device, const char * name ) { // Try loading the spirv code first char nameSpirv[ 1024 ]; - sprintf_s( nameSpirv, 1024, "data/shaders/spirv/%s.%s.spirv", name, fileExtensions[ i ] ); + snprintf( nameSpirv, 1024, "data/shaders/spirv/%s.%s.spirv", name, fileExtensions[ i ] ); if ( GetFileData( nameSpirv, &code, size ) ) { m_vkShaderModules[ i ] = Shader::CreateShaderModule( device->m_vkDevice, (char*)code, size ); continue; @@ -97,4 +98,4 @@ VkShaderModule Shader::CreateShaderModule( VkDevice vkDevice, const char * code, } return shaderModule; -} \ No newline at end of file +} diff --git a/code/Scene.cpp b/code/Scene.cpp index 8d058e4..47aa9f0 100644 --- a/code/Scene.cpp +++ b/code/Scene.cpp @@ -20,7 +20,7 @@ Scene::~Scene ==================================================== */ Scene::~Scene() { - for ( int i = 0; i < m_bodies.size(); i++ ) { + for ( size_t i = 0; i < m_bodies.size(); i++ ) { delete m_bodies[ i ].m_shape; } m_bodies.clear(); @@ -32,7 +32,7 @@ Scene::Reset ==================================================== */ void Scene::Reset() { - for ( int i = 0; i < m_bodies.size(); i++ ) { + for ( size_t i = 0; i < m_bodies.size(); i++ ) { delete m_bodies[ i ].m_shape; } m_bodies.clear(); @@ -67,4 +67,4 @@ Scene::Update */ void Scene::Update( const float dt_sec ) { // TODO: Add code -} \ No newline at end of file +} diff --git a/code/application.cpp b/code/application.cpp index 1815c66..587b79b 100644 --- a/code/application.cpp +++ b/code/application.cpp @@ -20,11 +20,15 @@ Application * g_application = NULL; #include -#include -static bool gIsInitialized( false ); -static unsigned __int64 gTicksPerSecond; -static unsigned __int64 gStartTicks; +#if defined( _WIN32 ) + #include +#elif defined( __linux__ ) || defined( __APPLE__ ) + #include + #include +#else + #error "Add platform specific code to get time in microseconds" +#endif /* ==================================== @@ -32,6 +36,10 @@ GetTimeSeconds ==================================== */ int GetTimeMicroseconds() { +#if defined( _WIN32 ) + static bool gIsInitialized( false ); + static unsigned __int64 gTicksPerSecond; + static unsigned __int64 gStartTicks; if ( false == gIsInitialized ) { gIsInitialized = true; @@ -51,6 +59,13 @@ int GetTimeMicroseconds() { const unsigned __int64 timeMicro = (unsigned __int64)( (double)( tick - gStartTicks ) / ticks_per_micro ); return (int)timeMicro; +#elif defined( __linux__ ) || defined( __APPLE__ ) + struct timeval tv; + gettimeofday( &tv, NULL ); + return (int)( tv.tv_sec * 1000000 + tv.tv_usec ); +#else + #error "Add platform specific code to get time in microseconds" +#endif } /* @@ -77,7 +92,7 @@ void Application::Initialize() { m_scene->Reset(); m_models.reserve( m_scene->m_bodies.size() ); - for ( int i = 0; i < m_scene->m_bodies.size(); i++ ) { + for ( size_t i = 0; i < m_scene->m_bodies.size(); i++ ) { Model * model = new Model(); model->BuildFromShape( m_scene->m_bodies[ i ].m_shape ); model->MakeVBO( &m_deviceContext ); @@ -228,7 +243,7 @@ bool Application::InitializeVulkan() { { bool result; FillFullScreenQuad( m_modelFullScreen ); - for ( int i = 0; i < m_modelFullScreen.m_vertices.size(); i++ ) { + for ( size_t i = 0; i < m_modelFullScreen.m_vertices.size(); i++ ) { m_modelFullScreen.m_vertices[ i ].xyz[ 1 ] *= -1.0f; } m_modelFullScreen.MakeVBO( &m_deviceContext ); @@ -251,8 +266,7 @@ bool Application::InitializeVulkan() { return false; } - Pipeline::CreateParms_t pipelineParms; - memset( &pipelineParms, 0, sizeof( pipelineParms ) ); + Pipeline::CreateParms_t pipelineParms = {}; pipelineParms.renderPass = m_deviceContext.m_swapChain.m_vkRenderPass; pipelineParms.descriptors = &m_copyDescriptors; pipelineParms.shader = &m_copyShader; @@ -290,7 +304,7 @@ void Application::Cleanup() { m_scene = NULL; // Delete models - for ( int i = 0; i < m_models.size(); i++ ) { + for ( size_t i = 0; i < m_models.size(); i++ ) { m_models[ i ]->Cleanup( m_deviceContext ); delete m_models[ i ]; } @@ -339,8 +353,7 @@ void Application::ResizeWindow( int windowWidth, int windowHeight ) { bool result; m_copyPipeline.Cleanup( &m_deviceContext ); - Pipeline::CreateParms_t pipelineParms; - memset( &pipelineParms, 0, sizeof( pipelineParms ) ); + Pipeline::CreateParms_t pipelineParms = {}; pipelineParms.renderPass = m_deviceContext.m_swapChain.m_vkRenderPass; pipelineParms.descriptors = &m_copyDescriptors; pipelineParms.shader = &m_copyShader; @@ -450,6 +463,8 @@ void Application::MainLoop() { static float avgTime = 0.0f; static float maxTime = 0.0f; + timeLastFrame = GetTimeMicroseconds(); + while ( !glfwWindowShouldClose( m_glfwWindow ) ) { int time = GetTimeMicroseconds(); float dt_us = (float)time - (float)timeLastFrame; @@ -519,8 +534,6 @@ void Application::UpdateUniforms() { m_renderModels.clear(); uint32_t uboByteOffset = 0; - uint32_t cameraByteOFfset = 0; - uint32_t shadowByteOffset = 0; struct camera_t { Mat4 matView; @@ -570,8 +583,6 @@ void Application::UpdateUniforms() { // Update the uniform buffer for the camera matrices memcpy( mappedData + uboByteOffset, &camera, sizeof( camera ) ); - cameraByteOFfset = uboByteOffset; - // update offset into the buffer uboByteOffset += m_deviceContext.GetAligendUniformByteOffset( sizeof( camera ) ); } @@ -588,8 +599,6 @@ void Application::UpdateUniforms() { camUp.Normalize(); extern FrameBuffer g_shadowFrameBuffer; - const int windowWidth = g_shadowFrameBuffer.m_parms.width; - const int windowHeight = g_shadowFrameBuffer.m_parms.height; const float halfWidth = 60.0f; const float xmin = -halfWidth; @@ -607,8 +616,6 @@ void Application::UpdateUniforms() { // Update the uniform buffer for the camera matrices memcpy( mappedData + uboByteOffset, &camera, sizeof( camera ) ); - shadowByteOffset = uboByteOffset; - // update offset into the buffer uboByteOffset += m_deviceContext.GetAligendUniformByteOffset( sizeof( camera ) ); } @@ -616,7 +623,7 @@ void Application::UpdateUniforms() { // // Update the uniform buffer with the body positions/orientations // - for ( int i = 0; i < m_scene->m_bodies.size(); i++ ) { + for ( size_t i = 0; i < m_scene->m_bodies.size(); i++ ) { Body & body = m_scene->m_bodies[ i ]; Vec3 fwd = body.m_orientation.RotatePoint( Vec3( 1, 0, 0 ) ); @@ -683,4 +690,4 @@ void Application::DrawFrame() { // End the render frame // m_deviceContext.EndFrame(); -} \ No newline at end of file +}