From ab38f8b150dcc7b0198cc794f31bc08b32c55dca Mon Sep 17 00:00:00 2001 From: Sascha Willems Date: Wed, 22 Apr 2020 20:58:24 +0200 Subject: [PATCH] Code-Cleanup: All samples now use the camera class and it's matrices Cleaned up base class --- base/camera.hpp | 15 ++++- base/vulkanexamplebase.cpp | 24 ++------ base/vulkanexamplebase.h | 11 ---- data/shaders/offscreen/mirror.frag.spv | Bin 2376 -> 2376 bytes data/shaders/offscreen/mirror.vert | 3 +- data/shaders/offscreen/mirror.vert.spv | Bin 1516 -> 1668 bytes data/shaders/offscreen/phong.frag.spv | Bin 1948 -> 1948 bytes data/shaders/offscreen/phong.vert | 5 +- data/shaders/offscreen/phong.vert.spv | Bin 2212 -> 2420 bytes data/shaders/offscreen/quad.frag.spv | Bin 568 -> 568 bytes data/shaders/offscreen/quad.vert.spv | Bin 1436 -> 1436 bytes data/shaders/particlefire/normalmap.frag.spv | Bin 2876 -> 2876 bytes data/shaders/particlefire/normalmap.vert.spv | Bin 4164 -> 4164 bytes data/shaders/particlefire/particle.frag.spv | Bin 2796 -> 2796 bytes data/shaders/particlefire/particle.vert.spv | Bin 2868 -> 2868 bytes examples/bloom/bloom.cpp | 2 +- examples/computeshader/computeshader.cpp | 18 +++--- examples/displacement/displacement.cpp | 22 +++---- .../distancefieldfonts/distancefieldfonts.cpp | 21 +++---- examples/gears/gears.cpp | 11 ++-- examples/gears/vulkangear.cpp | 18 +----- examples/gears/vulkangear.h | 3 +- examples/geometryshader/geometryshader.cpp | 23 ++++--- examples/instancing/instancing.cpp | 15 ++--- examples/multithreading/multithreading.cpp | 25 +++----- examples/multiview/multiview.cpp | 6 +- examples/occlusionquery/occlusionquery.cpp | 28 ++++----- examples/offscreen/offscreen.cpp | 56 +++++++----------- examples/particlefire/particlefire.cpp | 31 ++++------ examples/pipelines/pipelines.cpp | 18 +++--- examples/pushconstants/pushconstants.cpp | 25 +++----- examples/radialblur/radialblur.cpp | 23 ++++--- examples/shadowmapping/shadowmapping.cpp | 20 ++----- .../sphericalenvmapping.cpp | 24 +++----- examples/tessellation/tessellation.cpp | 24 +++----- examples/textoverlay/textoverlay.cpp | 15 ++--- examples/texture/texture.cpp | 25 +++----- examples/texture3d/texture3d.cpp | 25 +++----- examples/texturecubemap/texturecubemap.cpp | 37 ++++-------- examples/triangle/triangle.cpp | 17 +++--- examples/viewportarray/viewportarray.cpp | 8 +-- examples/vulkanscene/vulkanscene.cpp | 32 +++------- 42 files changed, 234 insertions(+), 396 deletions(-) diff --git a/base/camera.hpp b/base/camera.hpp index f602afb5..461da7d7 100644 --- a/base/camera.hpp +++ b/base/camera.hpp @@ -42,6 +42,8 @@ private: matrices.view = transM * rotM; } + viewPos = glm::vec4(position, 0.0f) * glm::vec4(-1.0f, 1.0f, -1.0f, 1.0f); + updated = true; }; public: @@ -50,6 +52,7 @@ public: glm::vec3 rotation = glm::vec3(); glm::vec3 position = glm::vec3(); + glm::vec4 viewPos = glm::vec4(); float rotationSpeed = 1.0f; float movementSpeed = 1.0f; @@ -113,7 +116,7 @@ public: { this->rotation = rotation; updateViewMatrix(); - }; + } void rotate(glm::vec3 delta) { @@ -133,6 +136,16 @@ public: updateViewMatrix(); } + void setRotationSpeed(float rotationSpeed) + { + this->rotationSpeed = rotationSpeed; + } + + void setMovementSpeed(float movementSpeed) + { + this->movementSpeed = movementSpeed; + } + void update(float deltaTime) { updated = false; diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index be758470..66695059 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -336,20 +336,18 @@ void VulkanExampleBase::renderLoop() // Rotate if (std::abs(gamePadState.axisLeft.x) > deadZone) { - rotation.y += gamePadState.axisLeft.x * 0.5f * rotationSpeed; camera.rotate(glm::vec3(0.0f, gamePadState.axisLeft.x * 0.5f, 0.0f)); updateView = true; } if (std::abs(gamePadState.axisLeft.y) > deadZone) { - rotation.x -= gamePadState.axisLeft.y * 0.5f * rotationSpeed; camera.rotate(glm::vec3(gamePadState.axisLeft.y * 0.5f, 0.0f, 0.0f)); updateView = true; } // Zoom if (std::abs(gamePadState.axisRight.y) > deadZone) { - zoom -= gamePadState.axisRight.y * 0.01f * zoomSpeed; + camera.translate(glm::vec3(0.0f, 0.0f, gamePadState.axisRight.y * 0.01f)); updateView = true; } if (updateView) @@ -1180,8 +1178,7 @@ void VulkanExampleBase::handleMessages(HWND hWnd, UINT uMsg, WPARAM wParam, LPAR case WM_MOUSEWHEEL: { short wheelDelta = GET_WHEEL_DELTA_WPARAM(wParam); - zoom += (float)wheelDelta * 0.005f * zoomSpeed; - camera.translate(glm::vec3(0.0f, 0.0f, (float)wheelDelta * 0.005f * zoomSpeed)); + camera.translate(glm::vec3(0.0f, 0.0f, (float)wheelDelta * 0.005f)); viewUpdated = true; break; } @@ -1292,15 +1289,12 @@ int32_t VulkanExampleBase::handleAppInput(struct android_app* app, AInputEvent* int32_t eventX = AMotionEvent_getX(event, 0); int32_t eventY = AMotionEvent_getY(event, 0); - float deltaX = (float)(vulkanExample->touchPos.y - eventY) * vulkanExample->rotationSpeed * 0.5f; - float deltaY = (float)(vulkanExample->touchPos.x - eventX) * vulkanExample->rotationSpeed * 0.5f; + float deltaX = (float)(vulkanExample->touchPos.y - eventY) * vulkanExample->camera.rotationSpeed * 0.5f; + float deltaY = (float)(vulkanExample->touchPos.x - eventX) * vulkanExample->camera.rotationSpeed * 0.5f; vulkanExample->camera.rotate(glm::vec3(deltaX, 0.0f, 0.0f)); vulkanExample->camera.rotate(glm::vec3(0.0f, -deltaY, 0.0f)); - vulkanExample->rotation.x += deltaX; - vulkanExample->rotation.y -= deltaY; - vulkanExample->viewChanged(); vulkanExample->touchPos.x = eventX; @@ -1494,8 +1488,7 @@ void VulkanExampleBase::pointerAxis(wl_pointer *pointer, uint32_t time, switch (axis) { case REL_X: - zoom += d * 0.005f * zoomSpeed; - camera.translate(glm::vec3(0.0f, 0.0f, d * 0.005f * zoomSpeed)); + camera.translate(glm::vec3(0.0f, 0.0f, d * 0.005f)); viewUpdated = true; break; default: @@ -2184,19 +2177,14 @@ void VulkanExampleBase::handleMouseMove(int32_t x, int32_t y) } if (mouseButtons.left) { - rotation.x += dy * 1.25f * rotationSpeed; - rotation.y -= dx * 1.25f * rotationSpeed; camera.rotate(glm::vec3(dy * camera.rotationSpeed, -dx * camera.rotationSpeed, 0.0f)); viewUpdated = true; } if (mouseButtons.right) { - zoom += dy * .005f * zoomSpeed; - camera.translate(glm::vec3(-0.0f, 0.0f, dy * .005f * zoomSpeed)); + camera.translate(glm::vec3(-0.0f, 0.0f, dy * .005f)); viewUpdated = true; } if (mouseButtons.middle) { - cameraPos.x -= dx * 0.01f; - cameraPos.y -= dy * 0.01f; camera.translate(glm::vec3(-dx * 0.01f, -dy * 0.01f, 0.0f)); viewUpdated = true; } diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index e01ce6f3..91f9beaf 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -160,8 +160,6 @@ public: VkClearColorValue defaultClearColor = { { 0.025f, 0.025f, 0.025f, 1.0f } }; - float zoom = 0; - static std::vector args; // Defines a frame rate independent timer value clamped from -1.0...1.0 @@ -169,18 +167,9 @@ public: float timer = 0.0f; // Multiplier for speeding up (or slowing down) the global timer float timerSpeed = 0.25f; - bool paused = false; - // Use to adjust mouse rotation speed - float rotationSpeed = 1.0f; - // Use to adjust mouse zoom speed - float zoomSpeed = 1.0f; - Camera camera; - - glm::vec3 rotation = glm::vec3(); - glm::vec3 cameraPos = glm::vec3(); glm::vec2 mousePos; std::string title = "Vulkan Example"; diff --git a/data/shaders/offscreen/mirror.frag.spv b/data/shaders/offscreen/mirror.frag.spv index dfe1a34bf31a8e121d247844b7b93b260a737bd8..7f382743284bce16aff225531d284ee0af59ec03 100644 GIT binary patch delta 18 ZcmX>hbV7)enMs+Qfq{{MVhbV7)enMs+Qfq{{MeIutmCjcV$0`~v_ diff --git a/data/shaders/offscreen/mirror.vert b/data/shaders/offscreen/mirror.vert index 1b56747f..738333fe 100644 --- a/data/shaders/offscreen/mirror.vert +++ b/data/shaders/offscreen/mirror.vert @@ -6,6 +6,7 @@ layout (location = 1) in vec2 inUV; layout (binding = 0) uniform UBO { mat4 projection; + mat4 view; mat4 model; } ubo; @@ -15,6 +16,6 @@ layout (location = 1) out vec4 outPos; void main() { outUV = inUV; - outPos = ubo.projection * ubo.model * vec4(inPos.xyz, 1.0); + outPos = ubo.projection * ubo.view * ubo.model * vec4(inPos.xyz, 1.0); gl_Position = outPos; } diff --git a/data/shaders/offscreen/mirror.vert.spv b/data/shaders/offscreen/mirror.vert.spv index c31c40a132a2f9ab3e20ea99b424ab83bcea836d..16b29306c2f2d7638c626f2556fa80b36e116bf9 100644 GIT binary patch literal 1668 zcmZ9LTTc{05QW zDql=IUvJmCu}N*$Id!VKx_XFe%?amDxk>#!a)UMJYGRz5a?Pmqxv}{T?BWub?iu700{!2;YZcKmw^k8zzezmi!9ZY^!c<3>; z6z`Nh;*pyZ-g*089md`lbjRKDb9zu^rMifvLPL*Vvh*u)I%#(|?N;eGL9qiKpIF(;{GqX%@OsYld{$K3+4mHk#W-8BjXXPk zm-VXs;vgNym@2V;o)2U6=ZCIXJItvrp9N-4V9o?HC&Qb<%z!m6+tjaN?#UYE^dY!e z&kgdZIhqG1@3qYG;Qr6!j-z>C@=j!y2WNTh@Sbqof%jOIu?#l`&|_-Z45-H&v6uzR zIC_I)H;yylq7e=sj&G7)sE2M|zw8ZhXz&;1#o80uk}|jhm{=k_sU0o+^MLub;fV9! zLO&J|_M)Q*;#7QZNeRDUICxI;recd$FJ1}!}2gPVb)9@^_N=T2VH&zighs6`xn zSH^iO!uPb316P;Fd)eHGVf1}pJ3SERT=06}o@!RyiMg@R{T9P_`$8=5b5_RNyi}Ao z1iz7jc`V78A#*~{Y?fv4rnw{^4YM-MW%=+wRfn7v8F}!Q$G1%$Gqt>{^5HEnkxwo> z8fJS<##}$BHuJd=IBi4jX5g5m>23v%d31DhW_epiK6>K3&kxllc1M=T&@damM>zT* Z{y+xDefVA<%9uN|LJvmsM=$zJ_80BwU0DDC delta 585 zcmZ8d%SyvQ6upy4Q=vqW5{g)B()wx|A4@@0x^pA6_yK}gB-l0!>pER@(`A^YYw;WW z5dXrBA0T*6Oh6nsxpU4v_mNq+pEXxoovMhEs;tOy=H67cMI_2bX?K#`kH#Ps(Nafz zzDsbog@$-KNS^YXBcTaYViDh|4aW~eq*XeRl0iEm_H)tgyZC)e!M z%Bw`K(7WtW`==>zYK*1NDl}bcM(H}{c=}!2vTi=~W7@#BTgW>AeLgtnu@CjLf^!dQ zJ#cyhKxBUDf$!tylj1N>egM$l1n(lUhFS|Z{)79gweutm9Na_X5;Slyd1Suqm-HNr z>-Ydy8R99}vJLnEPpNfq_W|`s+bJ|Y$pBBz@w)))>1RFwGW2C`4`>4PT!`+=S|M@~ a8V&(!eH5%B9sv>H0Vv2hJ>OX46!-zlY%f0m diff --git a/data/shaders/offscreen/phong.frag.spv b/data/shaders/offscreen/phong.frag.spv index 71c4b9fbe2e39c6cf5e2536a7706978e27c2b861..8c26766fb7236bd7fafc0fcd863c6650e075a0ad 100644 GIT binary patch delta 18 ZcmbQkKZl=_nMs+Qfq{{MVD?S-8GDyNl{gQt*7cV4fR7!o>%8J>$mPDr^Blk;(73#c#GHQ^n78fFnUng86BS( z!?Vq6fH_;cch1Qf{oN}D5y}}+iwH}bs9SVLG%9*3YS6D<{I`l_y^MY*u~{L+$A^{m zxV%>kC4-SKh^mmo$S=R;IE>2@g=Jj5AWERD`oAcAH+;M4m$&_L#s7?7V~QhViw}ZI z5Jy6roIf*gco0>#g0DV)W}|P}mcw9wB{--Qqmo~>F%@iwVOX_gbyx1sLB1^hW@$q) zjZOyNFUKGJl50U|*5UN-b-E<@+G-5)mdhR;m@2oLkas2)5G`9i`JA0KkC-T zp%b?tvRq)N=o;BF=ECUyzJ{pY%>~{K5Jqv2)9T4WjQZacW|xE;`>G76Ly-oV@f!9d-g{gpE2^#F-;@>Y%eTFzi=TnK-MHlU!T(dg_D!?bJqo@}aY1 z{IQRSa|g4tA9il@MQ7*eR&P4hiJy^7Z`S{^WP6YJGuu@W$Mk1TJNjjM-s#8lBZ?%? znur+u;Ml2E5T_4vPU_2`F7xDo{}ICX!aeC@MMN*N(&4Ql-qM`Bd2{GyYnRMB!p8eT zUsN{c&3k7D>=S=xe@z5#J_C~R$%)KoP%`)r#!q-H=QiJF~c(lEE$RmSkeV z@nJ@HMeOmra-$E6@B=3|d%Y*(jnX&wLdVZ)-V!Z delta 867 zcmYk3+b#o96o&V7bXo~u@< z5j=*(t;GLLXTnVWto8rvu-9JuyY;oD(GCqnjIm6_L{0vwu}~j3#*7Kc%3Z>0xh$O& zFT@=EU9utf!|Gcm=lp8FbS)d!Pk!n4CU;l-(~>o9=lt~S=D1k!^3v5{}%9dkH4>ttCU6?0yX4e z*9DsA+#@Fu=7_ZW=)CNvrI9}*aH%IP5I!uhHS`T>Tyj)s7dnNEt^+5Jc#mwt{c_sM z<9I^Qc|!jG&XeNgpdYA`+e``Qe1SGCAIgMV~NKI7#C;w*)iM#oRf@({s^ oS^2CY;G3Rr%(K6SEpc+BPpHuG8gENr>x>?39*yK59ls>}0?c$oAOHXW diff --git a/data/shaders/offscreen/quad.frag.spv b/data/shaders/offscreen/quad.frag.spv index 401a74346f9008f15c38c323804b7f38b18a3a12..03440f7c5c0ea9f2475b7bbea8fa8983aaaf0d3f 100644 GIT binary patch delta 18 ZcmdnNvV(<_nMs+Qfq{{MV%IU delta 18 ZcmdlYwndDSnMs+Qfq{{MeIutfHvk~00>uCT diff --git a/examples/bloom/bloom.cpp b/examples/bloom/bloom.cpp index a0333ec6..16d3ee9e 100644 --- a/examples/bloom/bloom.cpp +++ b/examples/bloom/bloom.cpp @@ -721,7 +721,7 @@ public: ubos.scene.projection = camera.matrices.perspective; ubos.scene.view = camera.matrices.view; - ubos.scene.model = glm::translate(glm::mat4(1.0f), glm::vec3(sin(glm::radians(timer * 360.0f)) * 0.25f, -1.0f, cos(glm::radians(timer * 360.0f)) * 0.25f) + cameraPos); + ubos.scene.model = glm::translate(glm::mat4(1.0f), glm::vec3(sin(glm::radians(timer * 360.0f)) * 0.25f, -1.0f, cos(glm::radians(timer * 360.0f)) * 0.25f)); ubos.scene.model = glm::rotate(ubos.scene.model, -sinf(glm::radians(timer * 360.0f)) * 0.15f, glm::vec3(1.0f, 0.0f, 0.0f)); ubos.scene.model = glm::rotate(ubos.scene.model, glm::radians(timer * 360.0f), glm::vec3(0.0f, 1.0f, 0.0f)); diff --git a/examples/computeshader/computeshader.cpp b/examples/computeshader/computeshader.cpp index f9cabbe6..4a650424 100644 --- a/examples/computeshader/computeshader.cpp +++ b/examples/computeshader/computeshader.cpp @@ -74,7 +74,7 @@ public: struct { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; } uboVS; int vertexBufferSize; @@ -83,8 +83,11 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -2.0f; title = "Compute shader image load/store"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -2.0f)); + camera.setRotation(glm::vec3(0.0f)); + camera.setPerspective(60.0f, (float)width * 0.5f / (float)height, 1.0f, 256.0f); settings.overlay = true; } @@ -613,15 +616,8 @@ public: void updateUniformBuffers() { - // Vertex shader uniform buffer block - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width*0.5f / (float)height, 0.1f, 256.0f); - glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - - uboVS.model = viewMatrix * glm::translate(glm::mat4(1.0f), cameraPos); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; memcpy(uniformBufferVS.mapped, &uboVS, sizeof(uboVS)); } diff --git a/examples/displacement/displacement.cpp b/examples/displacement/displacement.cpp index c955101d..d042a9c5 100644 --- a/examples/displacement/displacement.cpp +++ b/examples/displacement/displacement.cpp @@ -63,7 +63,7 @@ public: struct UBOTessEval { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; glm::vec4 lightPos = glm::vec4(0.0f, -1.0f, 0.0f, 0.0f); float tessAlpha = 1.0f; float tessStrength = 0.1f; @@ -80,9 +80,11 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -1.25f; - rotation = glm::vec3(-20.0f, 45.0f, 0.0f); title = "Tessellation shader displacement"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -1.25f)); + camera.setRotation(glm::vec3(-20.0f, 45.0f, 0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); settings.overlay = true; } @@ -445,19 +447,9 @@ public: void updateUniformBuffers() { - // Tessellation eval - glm::mat4 viewMatrix = glm::mat4(1.0f); - uboTessEval.projection = glm::perspective(glm::radians(45.0f), (float)(width) / (float)height, 0.1f, 256.0f); - viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom)); - - uboTessEval.model = glm::mat4(1.0f); - uboTessEval.model = viewMatrix * glm::translate(uboTessEval.model, glm::vec3(0, 0, 0)); - uboTessEval.model = glm::rotate(uboTessEval.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboTessEval.model = glm::rotate(uboTessEval.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboTessEval.model = glm::rotate(uboTessEval.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboTessEval.projection = camera.matrices.perspective; + uboTessEval.modelView = camera.matrices.view; uboTessEval.lightPos.y = -0.5f - uboTessEval.tessStrength; - memcpy(uniformBuffers.tessEval.mapped, &uboTessEval, sizeof(uboTessEval)); // Tessellation control diff --git a/examples/distancefieldfonts/distancefieldfonts.cpp b/examples/distancefieldfonts/distancefieldfonts.cpp index 8269b5ab..0896ef8f 100644 --- a/examples/distancefieldfonts/distancefieldfonts.cpp +++ b/examples/distancefieldfonts/distancefieldfonts.cpp @@ -87,7 +87,7 @@ public: struct UBOVS { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; } uboVS; struct UBOFS { @@ -111,8 +111,11 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -2.0f; title = "Distance field font rendering"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -2.0f)); + camera.setRotation(glm::vec3(0.0f)); + camera.setPerspective(splitScreen ? 30.0f : 45.0f, (float)width / (float)(height * ((splitScreen) ? 0.5f : 1.0f)), 1.0f, 256.0f); settings.overlay = true; } @@ -606,17 +609,8 @@ public: void updateUniformBuffers() { - // Vertex shader - glm::mat4 viewMatrix = glm::mat4(1.0f); - uboVS.projection = glm::perspective(glm::radians(splitScreen ? 30.0f : 45.0f), (float)width / (float)(height * ((splitScreen) ? 0.5f : 1.0f)), 0.001f, 256.0f); - viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, splitScreen ? zoom : zoom - 2.0f)); - - uboVS.model = glm::mat4(1.0f); - uboVS.model = viewMatrix * glm::translate(uboVS.model, cameraPos); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; memcpy(uniformBuffers.vs.mapped, &uboVS, sizeof(uboVS)); } @@ -677,6 +671,7 @@ public: updateFontSettings(); } if (overlay->checkBox("Splitscreen", &splitScreen)) { + camera.setPerspective(splitScreen ? 30.0f : 45.0f, (float)width / (float)(height * ((splitScreen) ? 0.5f : 1.0f)), 1.0f, 256.0f); buildCommandBuffers(); updateUniformBuffers(); } diff --git a/examples/gears/gears.cpp b/examples/gears/gears.cpp index f8ec9dd3..77d369d9 100644 --- a/examples/gears/gears.cpp +++ b/examples/gears/gears.cpp @@ -44,10 +44,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -16.0f; - rotation = glm::vec3(-23.75f, 41.25f, 21.0f); + title = "Vulkan gears"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 2.5f, -16.0f)); + camera.setRotation(glm::vec3(-23.75f, 41.25f, 21.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 0.001f, 256.0f); timerSpeed *= 0.25f; - title = "Rotating gears"; settings.overlay = true; } @@ -321,10 +323,9 @@ public: void updateUniformBuffers() { - glm::mat4 perspective = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); for (auto& gear : gears) { - gear->updateUniformBuffer(perspective, rotation, zoom, timer * 360.0f); + gear->updateUniformBuffer(camera.matrices.perspective, camera.matrices.view, timer * 360.0f); } } diff --git a/examples/gears/vulkangear.cpp b/examples/gears/vulkangear.cpp index f0af7c75..3ccc231c 100644 --- a/examples/gears/vulkangear.cpp +++ b/examples/gears/vulkangear.cpp @@ -265,29 +265,17 @@ void VulkanGear::draw(VkCommandBuffer cmdbuffer, VkPipelineLayout pipelineLayout vkCmdDrawIndexed(cmdbuffer, indexCount, 1, 0, 0, 1); } -void VulkanGear::updateUniformBuffer(glm::mat4 perspective, glm::vec3 rotation, float zoom, float timer) +void VulkanGear::updateUniformBuffer(glm::mat4 perspective, glm::mat4 view, float timer) { ubo.projection = perspective; - - ubo.view = glm::lookAt( - glm::vec3(0, 0, -zoom), - glm::vec3(-1.0, -1.5, 0), - glm::vec3(0, 1, 0) - ); - ubo.view = glm::rotate(ubo.view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - ubo.view = glm::rotate(ubo.view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - + ubo.view = view; ubo.model = glm::mat4(1.0f); ubo.model = glm::translate(ubo.model, pos); - rotation.z = (rotSpeed * timer) + rotOffset; - ubo.model = glm::rotate(ubo.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + ubo.model = glm::rotate(ubo.model, glm::radians((rotSpeed * timer) + rotOffset), glm::vec3(0.0f, 0.0f, 1.0f)); ubo.normal = glm::inverseTranspose(ubo.view * ubo.model); - ubo.lightPos = glm::vec3(0.0f, 0.0f, 2.5f); ubo.lightPos.x = sin(glm::radians(timer)) * 8.0f; ubo.lightPos.z = cos(glm::radians(timer)) * 8.0f; - memcpy(uniformBuffer.mapped, &ubo, sizeof(ubo)); } diff --git a/examples/gears/vulkangear.h b/examples/gears/vulkangear.h index 208c763c..3a59b41a 100644 --- a/examples/gears/vulkangear.h +++ b/examples/gears/vulkangear.h @@ -20,7 +20,6 @@ #include #include "vulkan/vulkan.h" - #include "VulkanTools.h" #include "VulkanDevice.hpp" #include "VulkanBuffer.hpp" @@ -92,7 +91,7 @@ public: VkDescriptorSet descriptorSet; void draw(VkCommandBuffer cmdbuffer, VkPipelineLayout pipelineLayout); - void updateUniformBuffer(glm::mat4 perspective, glm::vec3 rotation, float zoom, float timer); + void updateUniformBuffer(glm::mat4 perspective, glm::mat4 view, float timer); void setupDescriptorSet(VkDescriptorPool pool, VkDescriptorSetLayout descriptorSetLayout); diff --git a/examples/geometryshader/geometryshader.cpp b/examples/geometryshader/geometryshader.cpp index 6569f1e0..229e5792 100644 --- a/examples/geometryshader/geometryshader.cpp +++ b/examples/geometryshader/geometryshader.cpp @@ -48,12 +48,12 @@ public: struct { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; } uboVS; struct { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; glm::vec2 viewportDim; } uboGS; @@ -73,9 +73,11 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -8.0f; - rotation = glm::vec3(0.0f, -25.0f, 0.0f); title = "Geometry shader normal debugging"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -8.0f)); + camera.setRotation(glm::vec3(0.0f, -25.0f, 0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 1.0f, 256.0f); settings.overlay = true; } @@ -409,17 +411,12 @@ public: void updateUniformBuffers() { // Vertex shader - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); - glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - uboVS.model = viewMatrix * glm::translate(glm::mat4(1.0f), cameraPos); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; memcpy(uniformBuffers.VS.mapped, &uboVS, sizeof(uboVS)); - // Geometry shader - uboGS.model = uboVS.model; - uboGS.projection = uboVS.projection; + uboGS.projection = camera.matrices.perspective; + uboGS.modelView = camera.matrices.view; uboGS.viewportDim = glm::vec2(width, height); memcpy(uniformBuffers.GS.mapped, &uboGS, sizeof(uboGS)); } diff --git a/examples/instancing/instancing.cpp b/examples/instancing/instancing.cpp index 31b24e5a..fd41cae5 100644 --- a/examples/instancing/instancing.cpp +++ b/examples/instancing/instancing.cpp @@ -98,10 +98,10 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { title = "Instanced mesh rendering"; - zoom = -18.5f; - rotation = { -17.2f, -4.7f, 0.0f }; - cameraPos = { 5.5f, -1.85f, 0.0f }; - rotationSpeed = 0.25f; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(5.5f, -1.85f, -18.5f)); + camera.setRotation(glm::vec3(-17.2f, -4.7f, 0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 1.0f, 256.0f); settings.overlay = true; } @@ -544,11 +544,8 @@ public: { if (viewChanged) { - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f); - uboVS.view = glm::translate(glm::mat4(1.0f), cameraPos + glm::vec3(0.0f, 0.0f, zoom)); - uboVS.view = glm::rotate(uboVS.view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.view = glm::rotate(uboVS.view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.view = glm::rotate(uboVS.view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); + uboVS.projection = camera.matrices.perspective; + uboVS.view = camera.matrices.view; } if (!paused) diff --git a/examples/multithreading/multithreading.cpp b/examples/multithreading/multithreading.cpp index d38df0d0..06711bc3 100644 --- a/examples/multithreading/multithreading.cpp +++ b/examples/multithreading/multithreading.cpp @@ -122,11 +122,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -32.5f; - zoomSpeed = 2.5f; - rotationSpeed = 0.5f; - rotation = { 0.0f, 37.5f, 0.0f }; title = "Multi threaded command buffer"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, -0.0f, -32.5f)); + camera.setRotation(glm::vec3(0.0f)); + camera.setRotationSpeed(0.5f); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); settings.overlay = true; // Get number of max. concurrrent threads numThreads = std::thread::hardware_concurrency(); @@ -319,12 +320,8 @@ public: vkCmdBindPipeline(secondaryCommandBuffers.background, VK_PIPELINE_BIND_POINT_GRAPHICS, pipelines.starsphere); - glm::mat4 view = glm::mat4(1.0f); - view = glm::rotate(view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - view = glm::rotate(view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - view = glm::rotate(view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - - glm::mat4 mvp = matrices.projection * view; + glm::mat4 mvp = matrices.projection * matrices.view; + mvp[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); vkCmdPushConstants( secondaryCommandBuffers.background, @@ -564,12 +561,8 @@ public: void updateMatrices() { - matrices.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f); - matrices.view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - matrices.view = glm::rotate(matrices.view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - matrices.view = glm::rotate(matrices.view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - matrices.view = glm::rotate(matrices.view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + matrices.projection = camera.matrices.perspective; + matrices.view = camera.matrices.view; frustum.update(matrices.projection * matrices.view); } diff --git a/examples/multiview/multiview.cpp b/examples/multiview/multiview.cpp index 88d6af05..3600e6c8 100644 --- a/examples/multiview/multiview.cpp +++ b/examples/multiview/multiview.cpp @@ -634,9 +634,9 @@ public: float bottom = -wd2; glm::vec3 camFront; - camFront.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y)); - camFront.y = sin(glm::radians(rotation.x)); - camFront.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y)); + camFront.x = -cos(glm::radians(camera.rotation.x)) * sin(glm::radians(camera.rotation.y)); + camFront.y = sin(glm::radians(camera.rotation.x)); + camFront.z = cos(glm::radians(camera.rotation.x)) * cos(glm::radians(camera.rotation.y)); camFront = glm::normalize(camFront); glm::vec3 camRight = glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))); diff --git a/examples/occlusionquery/occlusionquery.cpp b/examples/occlusionquery/occlusionquery.cpp index 4a31a622..089ad0ac 100644 --- a/examples/occlusionquery/occlusionquery.cpp +++ b/examples/occlusionquery/occlusionquery.cpp @@ -49,7 +49,7 @@ public: struct UBOVS { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; glm::vec4 lightPos = glm::vec4(10.0f, 10.0f, 10.0f, 1.0f); float visible; } uboVS; @@ -84,11 +84,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -35.0f; - zoomSpeed = 2.5f; - rotationSpeed = 0.5f; - rotation = { 0.0, -123.75, 0.0 }; title = "Occlusion queries"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -35.0f)); + camera.setRotation(glm::vec3(0.0f, -123.75f, 0.0f)); + camera.setRotationSpeed(0.5f); + camera.setPerspective(60.0f, (float)width / (float)height, 1.0f, 256.0f); settings.overlay = true; } @@ -543,19 +544,10 @@ public: void updateUniformBuffers() { - // Vertex shader - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f); - glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - - glm::mat4 rotMatrix = glm::mat4(1.0f); - rotMatrix = glm::rotate(rotMatrix, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - rotMatrix = glm::rotate(rotMatrix, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - rotMatrix = glm::rotate(rotMatrix, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - - uboVS.model = viewMatrix * rotMatrix; + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; uint8_t *pData; - // Occluder uboVS.visible = 1.0f; memcpy(uniformBuffers.occluder.mapped, &uboVS, sizeof(uboVS)); @@ -563,13 +555,13 @@ public: // Teapot // Toggle color depending on visibility uboVS.visible = (passedSamples[0] > 0) ? 1.0f : 0.0f; - uboVS.model = viewMatrix * rotMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -10.0f)); + uboVS.modelView = camera.matrices.view * glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, -10.0f)); memcpy(uniformBuffers.teapot.mapped, &uboVS, sizeof(uboVS)); // Sphere // Toggle color depending on visibility uboVS.visible = (passedSamples[1] > 0) ? 1.0f : 0.0f; - uboVS.model = viewMatrix * rotMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 10.0f)); + uboVS.modelView = camera.matrices.view * glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 10.0f)); memcpy(uniformBuffers.sphere.mapped, &uboVS, sizeof(uboVS)); } diff --git a/examples/offscreen/offscreen.cpp b/examples/offscreen/offscreen.cpp index 5389cfd2..73903237 100644 --- a/examples/offscreen/offscreen.cpp +++ b/examples/offscreen/offscreen.cpp @@ -56,6 +56,7 @@ public: struct UBO { glm::mat4 projection; + glm::mat4 view; glm::mat4 model; glm::vec4 lightPos = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); } uboShared; @@ -99,16 +100,18 @@ public: VkDescriptorImageInfo descriptor; } offscreenPass; - glm::vec3 meshPos = glm::vec3(0.0f, -1.5f, 0.0f); - glm::vec3 meshRot = glm::vec3(0.0f); + glm::vec3 modelPosition = glm::vec3(0.0f, -1.5f, 0.0f); + glm::vec3 modelRotation = glm::vec3(0.0f); VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -6.0f; - rotation = { -2.5f, 0.0f, 0.0f }; - cameraPos = { 0.0f, 1.0f, 0.0f }; - timerSpeed *= 0.25f; title = "Offscreen rendering"; + timerSpeed *= 0.25f; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 1.0f, -6.0f)); + camera.setRotation(glm::vec3(-2.5f, 0.0f, 0.0f)); + camera.setRotationSpeed(0.5f); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); settings.overlay = true; // The scene shader uses a clipping plane, so this feature has to be enabled enabledFeatures.shaderClipDistance = VK_TRUE; @@ -777,47 +780,34 @@ public: void updateUniformBuffers() { - // Mesh - uboShared.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f); - glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - - uboShared.model = viewMatrix * glm::translate(glm::mat4(1.0f), cameraPos); - uboShared.model = glm::rotate(uboShared.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboShared.model = glm::rotate(uboShared.model, glm::radians(rotation.y + meshRot.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboShared.model = glm::rotate(uboShared.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - - uboShared.model = glm::translate(uboShared.model, meshPos); + uboShared.projection = camera.matrices.perspective; + uboShared.view = camera.matrices.view; + // Model + uboShared.model = glm::mat4(1.0f); + uboShared.model = glm::rotate(uboShared.model, glm::radians(modelRotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); + uboShared.model = glm::translate(uboShared.model, modelPosition); memcpy(uniformBuffers.vsShared.mapped, &uboShared, sizeof(uboShared)); // Mirror - uboShared.model = viewMatrix * glm::translate(glm::mat4(1.0f), cameraPos); - uboShared.model = glm::rotate(uboShared.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboShared.model = glm::rotate(uboShared.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboShared.model = glm::rotate(uboShared.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboShared.model = glm::mat4(1.0f); memcpy(uniformBuffers.vsMirror.mapped, &uboShared, sizeof(uboShared)); // Debug quad + // @todo: Full screen triangle in VS uboShared.projection = glm::ortho(4.0f, 0.0f, 0.0f, 4.0f*(float)height / (float)width, -1.0f, 1.0f); uboShared.model = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, 0.0f)); - memcpy(uniformBuffers.vsDebugQuad.mapped, &uboShared, sizeof(uboShared)); } void updateUniformBufferOffscreen() { - uboShared.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f); - glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - - uboShared.model = viewMatrix * glm::translate(glm::mat4(1.0f), cameraPos); - uboShared.model = glm::rotate(uboShared.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboShared.model = glm::rotate(uboShared.model, glm::radians(rotation.y + meshRot.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboShared.model = glm::rotate(uboShared.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboShared.projection = camera.matrices.perspective; + uboShared.view = camera.matrices.view; + uboShared.model = glm::mat4(1.0f); + uboShared.model = glm::rotate(uboShared.model, glm::radians(modelRotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); uboShared.model = glm::scale(uboShared.model, glm::vec3(1.0f, -1.0f, 1.0f)); - uboShared.model = glm::translate(uboShared.model, meshPos); - + uboShared.model = glm::translate(uboShared.model, modelPosition); memcpy(uniformBuffers.vsOffScreen.mapped, &uboShared, sizeof(uboShared)); } @@ -857,7 +847,7 @@ public: draw(); if (!paused) { - meshRot.y += frameTimer * 10.0f; + modelRotation.y += frameTimer * 10.0f; updateUniformBuffers(); updateUniformBufferOffscreen(); } diff --git a/examples/particlefire/particlefire.cpp b/examples/particlefire/particlefire.cpp index 385de4f6..22d5d5fb 100644 --- a/examples/particlefire/particlefire.cpp +++ b/examples/particlefire/particlefire.cpp @@ -96,14 +96,14 @@ public: struct UBOVS { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; glm::vec2 viewportDim; float pointSize = PARTICLE_SIZE; } uboVS; struct UBOEnv { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; glm::mat4 normal; glm::vec4 lightPos = glm::vec4(0.0f, 0.0f, 0.0f, 0.0f); } uboEnv; @@ -127,11 +127,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -75.0f; - rotation = { -15.0f, 45.0f, 0.0f }; title = "CPU based particle system"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -75.0f)); + camera.setRotation(glm::vec3(-15.0f, 45.0f, 0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 1.0f, 256.0f); settings.overlay = true; - zoomSpeed *= 1.5f; timerSpeed *= 8.0f; rndEngine.seed(benchmark.active ? 0 : (unsigned)time(nullptr)); } @@ -697,24 +698,16 @@ public: void updateUniformBuffers() { - // Vertex shader - glm::mat4 viewMatrix = glm::mat4(1.0f); - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); - viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom)); - - uboVS.model = glm::mat4(1.0f); - uboVS.model = viewMatrix * glm::translate(uboVS.model, glm::vec3(0.0f, 15.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + // Particle system fire + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; uboVS.viewportDim = glm::vec2((float)width, (float)height); memcpy(uniformBuffers.fire.mapped, &uboVS, sizeof(uboVS)); // Environment - uboEnv.projection = uboVS.projection; - uboEnv.model = uboVS.model; - uboEnv.normal = glm::inverseTranspose(uboEnv.model); + uboEnv.projection = camera.matrices.perspective; + uboEnv.modelView = camera.matrices.view; + uboEnv.normal = glm::inverseTranspose(uboEnv.modelView); memcpy(uniformBuffers.environment.mapped, &uboEnv, sizeof(uboEnv)); } diff --git a/examples/pipelines/pipelines.cpp b/examples/pipelines/pipelines.cpp index 4237a2eb..8e3c2d43 100644 --- a/examples/pipelines/pipelines.cpp +++ b/examples/pipelines/pipelines.cpp @@ -61,9 +61,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -10.5f; - rotation = glm::vec3(-25.0f, 15.0f, 0.0f); title = "Pipeline state objects"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -10.5f)); + camera.setRotation(glm::vec3(-25.0f, 15.0f, 0.0f)); + camera.setRotationSpeed(0.5f); + camera.setPerspective(60.0f, (float)(width / 3.0f) / (float)height, 0.1f, 256.0f); settings.overlay = true; } @@ -379,15 +382,8 @@ public: void updateUniformBuffers() { - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)(width / 3.0f) / (float)height, 0.1f, 256.0f); - - glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - - uboVS.modelView = viewMatrix * glm::translate(glm::mat4(1.0f), cameraPos); - uboVS.modelView = glm::rotate(uboVS.modelView, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.modelView = glm::rotate(uboVS.modelView, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.modelView = glm::rotate(uboVS.modelView, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS)); } diff --git a/examples/pushconstants/pushconstants.cpp b/examples/pushconstants/pushconstants.cpp index c56e371a..6b59bbeb 100644 --- a/examples/pushconstants/pushconstants.cpp +++ b/examples/pushconstants/pushconstants.cpp @@ -50,7 +50,7 @@ public: struct UBOVS { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; glm::vec4 lightPos = glm::vec4(0.0, 0.0, -2.0, 1.0); } uboVS; @@ -68,12 +68,13 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -30.0; - zoomSpeed = 2.5f; - rotationSpeed = 0.5f; - timerSpeed *= 0.5f; - rotation = { -32.5, 45.0, 0.0 }; title = "Push constants"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -30.0f)); + camera.setRotation(glm::vec3(-32.5f, 45.0f, 0.0f)); + camera.setRotationSpeed(0.5f); + camera.setPerspective(60.0f, (float)width / (float)height, 1.0f, 256.0f); + timerSpeed *= 0.5f; settings.overlay = true; } @@ -394,16 +395,8 @@ public: void updateUniformBuffers() { - // Vertex shader - glm::mat4 viewMatrix = glm::mat4(1.0f); - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); - viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 2.0f, zoom)); - - uboVS.model = viewMatrix * glm::translate(glm::mat4(1.0f), glm::vec3(0, 0, 0)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS)); } diff --git a/examples/radialblur/radialblur.cpp b/examples/radialblur/radialblur.cpp index 8fe0ae27..a15462ec 100644 --- a/examples/radialblur/radialblur.cpp +++ b/examples/radialblur/radialblur.cpp @@ -58,7 +58,7 @@ public: struct UboVS { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; float gradientPos = 0.0f; } uboScene; @@ -107,10 +107,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -10.0f; - rotation = { -16.25f, -28.75f, 0.0f }; - timerSpeed *= 0.5f; title = "Full screen radial blur effect"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -10.0f)); + camera.setRotation(glm::vec3(-16.25f, -28.75f, 0.0f)); + camera.setPerspective(45.0f, (float)width / (float)height, 1.0f, 256.0f); + timerSpeed *= 0.5f; settings.overlay = true; } @@ -615,15 +617,10 @@ public: void updateUniformBuffersScene() { uboScene.projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 1.0f, 256.0f); - glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - - uboScene.model = glm::mat4(1.0f); - uboScene.model = viewMatrix * glm::translate(uboScene.model, cameraPos); - uboScene.model = glm::rotate(uboScene.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboScene.model = glm::rotate(uboScene.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboScene.model = glm::rotate(uboScene.model, glm::radians(timer * 360.0f), glm::vec3(0.0f, 1.0f, 0.0f)); - uboScene.model = glm::rotate(uboScene.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + camera.setRotation(camera.rotation + glm::vec3(0.0f, frameTimer * 10.0f, 0.0f)); + uboScene.projection = camera.matrices.perspective; + uboScene.modelView = camera.matrices.view; + // split into model view for separating rotation if (!paused) { uboScene.gradientPos += frameTimer * 0.1f; diff --git a/examples/shadowmapping/shadowmapping.cpp b/examples/shadowmapping/shadowmapping.cpp index c0566d31..bc35e202 100644 --- a/examples/shadowmapping/shadowmapping.cpp +++ b/examples/shadowmapping/shadowmapping.cpp @@ -131,9 +131,11 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -20.0f; - rotation = { -15.0f, -390.0f, 0.0f }; title = "Projected shadow mapping"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, -0.0f, -20.0f)); + camera.setRotation(glm::vec3(-15.0f, -390.0f, 0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 1.0f, 256.0f); timerSpeed *= 0.5f; settings.overlay = true; } @@ -689,26 +691,16 @@ public: { // Shadow map debug quad float AR = (float)height / (float)width; - uboVSquad.projection = glm::ortho(2.5f / AR, 0.0f, 0.0f, 2.5f, -1.0f, 1.0f); uboVSquad.model = glm::mat4(1.0f); - memcpy(uniformBuffers.debug.mapped, &uboVSquad, sizeof(uboVSquad)); // 3D scene - uboVSscene.projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, zNear, zFar); - - uboVSscene.view = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - uboVSscene.view = glm::rotate(uboVSscene.view, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVSscene.view = glm::rotate(uboVSscene.view, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVSscene.view = glm::rotate(uboVSscene.view, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboVSscene.projection = camera.matrices.perspective; + uboVSscene.view = camera.matrices.view; uboVSscene.model = glm::mat4(1.0f); - uboVSscene.lightPos = lightPos; - uboVSscene.depthBiasMVP = uboOffscreenVS.depthMVP; - memcpy(uniformBuffers.scene.mapped, &uboVSscene, sizeof(uboVSscene)); } diff --git a/examples/sphericalenvmapping/sphericalenvmapping.cpp b/examples/sphericalenvmapping/sphericalenvmapping.cpp index c9beb7ae..8b239ff3 100644 --- a/examples/sphericalenvmapping/sphericalenvmapping.cpp +++ b/examples/sphericalenvmapping/sphericalenvmapping.cpp @@ -73,11 +73,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -0.9f; - rotationSpeed = 0.75f; - zoomSpeed = 0.25f; - rotation = glm::vec3(-25.0f, 23.75f, 0.0f); title = "Spherical Environment Mapping"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -0.9f)); + camera.setRotation(glm::vec3(-25.0f, 23.75f, 0.0f)); + camera.setRotationSpeed(0.75f); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); settings.overlay = true; } @@ -371,21 +372,10 @@ public: void updateUniformBuffers() { - uboVS.projection = glm::perspective(glm::radians(45.0f), (float)width / (float)height, 0.1f, 256.0f); - - uboVS.view = glm::lookAt( - glm::vec3(0, 0, -zoom), - glm::vec3(0, 0, 0), - glm::vec3(0, 1, 0) - ); - + uboVS.projection = camera.matrices.perspective; + uboVS.view = camera.matrices.view; uboVS.model = glm::mat4(1.0f); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - uboVS.normal = glm::inverseTranspose(uboVS.view * uboVS.model); - memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS)); } diff --git a/examples/tessellation/tessellation.cpp b/examples/tessellation/tessellation.cpp index 652c190d..6da021c8 100644 --- a/examples/tessellation/tessellation.cpp +++ b/examples/tessellation/tessellation.cpp @@ -66,7 +66,7 @@ public: struct UBOTessEval { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; float tessAlpha = 1.0f; } uboTessEval; @@ -83,10 +83,11 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -6.5f; - rotation = glm::vec3(-350.0f, 60.0f, 0.0f); - cameraPos = glm::vec3(-3.0f, 2.3f, 0.0f); title = "Tessellation shader (PN Triangles)"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(-3.0f, 2.3f, -6.5f)); + camera.setRotation(glm::vec3(-350.0f, 60.0f, 0.0f)); + camera.setPerspective(45.0f, (float)(width * ((splitScreen) ? 0.5f : 1.0f)) / (float)height, 0.1f, 256.0f); settings.overlay = true; } @@ -453,20 +454,10 @@ public: void updateUniformBuffers() { - // Tessellation eval - glm::mat4 viewMatrix = glm::mat4(1.0f); - uboTessEval.projection = glm::perspective(glm::radians(45.0f), (float)(width* ((splitScreen) ? 0.5f : 1.0f)) / (float)height, 0.1f, 256.0f); - viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom)); - - uboTessEval.model = glm::mat4(1.0f); - uboTessEval.model = viewMatrix * glm::translate(uboTessEval.model, cameraPos); - uboTessEval.model = glm::rotate(uboTessEval.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboTessEval.model = glm::rotate(uboTessEval.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboTessEval.model = glm::rotate(uboTessEval.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboTessEval.projection = camera.matrices.perspective; + uboTessEval.modelView = camera.matrices.view; // Tessellation evaulation uniform block memcpy(uniformBuffers.tessEval.mapped, &uboTessEval, sizeof(uboTessEval)); - // Tessellation control uniform block memcpy(uniformBuffers.tessControl.mapped, &uboTessControl, sizeof(uboTessControl)); } @@ -522,6 +513,7 @@ public: buildCommandBuffers(); } if (overlay->checkBox("Splitscreen", &splitScreen)) { + camera.setPerspective(45.0f, (float)(width * ((splitScreen) ? 0.5f : 1.0f)) / (float)height, 0.1f, 256.0f); updateUniformBuffers(); buildCommandBuffers(); } diff --git a/examples/textoverlay/textoverlay.cpp b/examples/textoverlay/textoverlay.cpp index 0ef5e90b..b716daec 100644 --- a/examples/textoverlay/textoverlay.cpp +++ b/examples/textoverlay/textoverlay.cpp @@ -649,7 +649,7 @@ public: struct UBOVS { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; glm::vec4 lightPos = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); } uboVS; @@ -662,11 +662,11 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { title = "Vulkan Example - Text overlay"; - settings.overlay = false; camera.type = Camera::CameraType::lookat; camera.setPosition(glm::vec3(0.0f, 0.0f, -4.5f)); camera.setRotation(glm::vec3(-25.0f, -0.0f, 0.0f)); camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); + settings.overlay = false; } ~VulkanExample() @@ -749,7 +749,7 @@ public: { std::stringstream vpos; vpos << std::showpos << x << "/" << y << "/" << z; - glm::vec3 projected = glm::project(glm::vec3((float)x, (float)y, (float)z), uboVS.model, uboVS.projection, glm::vec4(0, 0, (float)width, (float)height)); + glm::vec3 projected = glm::project(glm::vec3((float)x, (float)y, (float)z), uboVS.modelView, uboVS.projection, glm::vec4(0, 0, (float)width, (float)height)); textOverlay->addText(vpos.str(), projected.x, projected.y + (y > -1 ? 5.0f : -20.0f), TextOverlay::alignCenter); } } @@ -762,11 +762,11 @@ public: { ss.str(""); ss << std::fixed << std::setprecision(2) << std::showpos; - ss << uboVS.model[0][i] << " " << uboVS.model[1][i] << " " << uboVS.model[2][i] << " " << uboVS.model[3][i]; + ss << uboVS.modelView[0][i] << " " << uboVS.modelView[1][i] << " " << uboVS.modelView[2][i] << " " << uboVS.modelView[3][i]; textOverlay->addText(ss.str(), (float)width, 25.0f + (float)i * 20.0f, TextOverlay::alignRight); } - glm::vec3 projected = glm::project(glm::vec3(0.0f), uboVS.model, uboVS.projection, glm::vec4(0, 0, (float)width, (float)height)); + glm::vec3 projected = glm::project(glm::vec3(0.0f), uboVS.modelView, uboVS.projection, glm::vec4(0, 0, (float)width, (float)height)); textOverlay->addText("Uniform cube", projected.x, projected.y, TextOverlay::alignCenter); #if defined(__ANDROID__) @@ -888,10 +888,7 @@ public: void updateUniformBuffers() { uboVS.projection = camera.matrices.perspective; - uboVS.model = glm::rotate(glm::mat4(1.0f), glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - uboVS.model = camera.matrices.view * uboVS.model; + uboVS.modelView = camera.matrices.view; memcpy(uniformBuffer.mapped, &uboVS, sizeof(uboVS)); } diff --git a/examples/texture/texture.cpp b/examples/texture/texture.cpp index 3b2aa6df..d99f4f69 100644 --- a/examples/texture/texture.cpp +++ b/examples/texture/texture.cpp @@ -62,7 +62,7 @@ public: struct { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; glm::vec4 viewPos; float lodBias = 0.0f; } uboVS; @@ -77,9 +77,11 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -2.5f; - rotation = { 0.0f, 15.0f, 0.0f }; title = "Texture loading"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -2.5f)); + camera.setRotation(glm::vec3(0.0f, 15.0f, 0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); settings.overlay = true; } @@ -772,26 +774,17 @@ public: &uniformBufferVS, sizeof(uboVS), &uboVS)); + VK_CHECK_RESULT(uniformBufferVS.map()); updateUniformBuffers(); } void updateUniformBuffers() { - // Vertex shader - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); - glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - - uboVS.model = viewMatrix * glm::translate(glm::mat4(1.0f), cameraPos); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - - uboVS.viewPos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f); - - VK_CHECK_RESULT(uniformBufferVS.map()); + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; + uboVS.viewPos = camera.viewPos; memcpy(uniformBufferVS.mapped, &uboVS, sizeof(uboVS)); - uniformBufferVS.unmap(); } void prepare() diff --git a/examples/texture3d/texture3d.cpp b/examples/texture3d/texture3d.cpp index 2363f73a..374bbf23 100644 --- a/examples/texture3d/texture3d.cpp +++ b/examples/texture3d/texture3d.cpp @@ -177,7 +177,7 @@ public: struct UboVS { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; glm::vec4 viewPos; float depth = 0.0f; } uboVS; @@ -192,9 +192,11 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -2.5f; - rotation = { 0.0f, 15.0f, 0.0f }; title = "3D textures"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -2.5f)); + camera.setRotation(glm::vec3(0.0f, 15.0f, 0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); settings.overlay = true; srand((unsigned int)time(NULL)); } @@ -749,7 +751,7 @@ public: &uniformBufferVS, sizeof(uboVS), &uboVS)); - + VK_CHECK_RESULT(uniformBufferVS.map()); updateUniformBuffers(); } @@ -757,15 +759,9 @@ public: { if (viewchanged) { - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); - glm::mat4 viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - - uboVS.model = viewMatrix * glm::translate(glm::mat4(1.0f), cameraPos); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - - uboVS.viewPos = glm::vec4(0.0f, 0.0f, -zoom, 0.0f); + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; + uboVS.viewPos = camera.viewPos; } else { @@ -773,10 +769,7 @@ public: if (uboVS.depth > 1.0f) uboVS.depth = uboVS.depth - 1.0f; } - - VK_CHECK_RESULT(uniformBufferVS.map()); memcpy(uniformBufferVS.mapped, &uboVS, sizeof(uboVS)); - uniformBufferVS.unmap(); } void prepare() diff --git a/examples/texturecubemap/texturecubemap.cpp b/examples/texturecubemap/texturecubemap.cpp index 5532b5f1..2a77ba66 100644 --- a/examples/texturecubemap/texturecubemap.cpp +++ b/examples/texturecubemap/texturecubemap.cpp @@ -54,7 +54,7 @@ public: struct UBOVS { glm::mat4 projection; - glm::mat4 model; + glm::mat4 modelView; float lodBias = 0.0f; } uboVS; @@ -75,10 +75,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -4.0f; - rotationSpeed = 0.25f; - rotation = { -7.25f, -120.0f, 0.0f }; title = "Cube map textures"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -4.0f)); + camera.setRotation(glm::vec3(-7.25f, -120.0f, 0.0f)); + camera.setRotationSpeed(0.25f); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); settings.overlay = true; } @@ -642,39 +644,22 @@ public: void updateUniformBuffers() { // 3D object - glm::mat4 viewMatrix = glm::mat4(1.0f); - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); - viewMatrix = glm::translate(viewMatrix, glm::vec3(0.0f, 0.0f, zoom)); - - uboVS.model = glm::mat4(1.0f); - uboVS.model = viewMatrix * glm::translate(uboVS.model, cameraPos); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboVS.projection = camera.matrices.perspective; + uboVS.modelView = camera.matrices.view; memcpy(uniformBuffers.object.mapped, &uboVS, sizeof(uboVS)); - // Skybox - viewMatrix = glm::mat4(1.0f); - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.001f, 256.0f); - - uboVS.model = glm::mat4(1.0f); - uboVS.model = viewMatrix * glm::translate(uboVS.model, glm::vec3(0, 0, 0)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - + uboVS.modelView = camera.matrices.view; + // Cancel out translation + uboVS.modelView[3] = glm::vec4(0.0f, 0.0f, 0.0f, 1.0f); memcpy(uniformBuffers.skybox.mapped, &uboVS, sizeof(uboVS)); } void draw() { VulkanExampleBase::prepareFrame(); - submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer]; VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE)); - VulkanExampleBase::submitFrame(); } diff --git a/examples/triangle/triangle.cpp b/examples/triangle/triangle.cpp index 4a91d0c8..924eba74 100644 --- a/examples/triangle/triangle.cpp +++ b/examples/triangle/triangle.cpp @@ -113,8 +113,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -2.5f; title = "Vulkan Example - Basic indexed triangle"; + // Setup a default look-at camera + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -2.5f)); + camera.setRotation(glm::vec3(0.0f)); + camera.setPerspective(60.0f, (float)width / (float)height, 1.0f, 256.0f); // Values not set here are initialized in the base class constructor } @@ -1048,15 +1052,10 @@ public: void updateUniformBuffers() { - // Update matrices - uboVS.projectionMatrix = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f); - - uboVS.viewMatrix = glm::translate(glm::mat4(1.0f), glm::vec3(0.0f, 0.0f, zoom)); - + // Pass matrices to the shaders + uboVS.projectionMatrix = camera.matrices.perspective; + uboVS.viewMatrix = camera.matrices.view; uboVS.modelMatrix = glm::mat4(1.0f); - uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.modelMatrix = glm::rotate(uboVS.modelMatrix, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); // Map uniform buffer and update it uint8_t *pData; diff --git a/examples/viewportarray/viewportarray.cpp b/examples/viewportarray/viewportarray.cpp index 50652e75..5435e8c4 100644 --- a/examples/viewportarray/viewportarray.cpp +++ b/examples/viewportarray/viewportarray.cpp @@ -61,7 +61,7 @@ public: camera.type = Camera::CameraType::firstperson; camera.setRotation(glm::vec3(0.0f, 90.0f, 0.0f)); camera.setTranslation(glm::vec3(7.0f, 3.2f, 0.0f)); - camera.movementSpeed = 5.0f; + camera.setMovementSpeed(5.0f); settings.overlay = true; } @@ -312,9 +312,9 @@ public: float bottom = -wd2; glm::vec3 camFront; - camFront.x = -cos(glm::radians(rotation.x)) * sin(glm::radians(rotation.y)); - camFront.y = sin(glm::radians(rotation.x)); - camFront.z = cos(glm::radians(rotation.x)) * cos(glm::radians(rotation.y)); + camFront.x = -cos(glm::radians(camera.rotation.x)) * sin(glm::radians(camera.rotation.y)); + camFront.y = sin(glm::radians(camera.rotation.x)); + camFront.z = cos(glm::radians(camera.rotation.x)) * cos(glm::radians(camera.rotation.y)); camFront = glm::normalize(camFront); glm::vec3 camRight = glm::normalize(glm::cross(camFront, glm::vec3(0.0f, 1.0f, 0.0f))); diff --git a/examples/vulkanscene/vulkanscene.cpp b/examples/vulkanscene/vulkanscene.cpp index a648ed49..33013bd2 100644 --- a/examples/vulkanscene/vulkanscene.cpp +++ b/examples/vulkanscene/vulkanscene.cpp @@ -90,10 +90,12 @@ public: VulkanExample() : VulkanExampleBase(ENABLE_VALIDATION) { - zoom = -3.75f; - rotationSpeed = 0.5f; - rotation = glm::vec3(15.0f, 0.f, 0.0f); - title = "Vulkan Demo Scene - (c) 2016 by Sascha Willems"; + title = "Vulkan Demo Scene - (c) by Sascha Willems"; + camera.type = Camera::CameraType::lookat; + camera.setPosition(glm::vec3(0.0f, 0.0f, -3.75f)); + camera.setRotation(glm::vec3(15.0f, 0.0f, 0.0f)); + camera.setRotationSpeed(0.5f); + camera.setPerspective(60.0f, (float)width / (float)height, 0.1f, 256.0f); settings.overlay = true; } @@ -381,42 +383,26 @@ public: VK_MEMORY_PROPERTY_HOST_VISIBLE_BIT | VK_MEMORY_PROPERTY_HOST_COHERENT_BIT, &uniformData.meshVS, sizeof(uboVS)); - + VK_CHECK_RESULT(uniformData.meshVS.map()); updateUniformBuffers(); } void updateUniformBuffers() { - uboVS.projection = glm::perspective(glm::radians(60.0f), (float)width / (float)height, 0.1f, 256.0f); - - uboVS.view = glm::lookAt( - glm::vec3(0, 0, -zoom), - cameraPos, - glm::vec3(0, 1, 0) - ); - + uboVS.projection = camera.matrices.perspective; + uboVS.view = camera.matrices.view; uboVS.model = glm::mat4(1.0f); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.x), glm::vec3(1.0f, 0.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.y), glm::vec3(0.0f, 1.0f, 0.0f)); - uboVS.model = glm::rotate(uboVS.model, glm::radians(rotation.z), glm::vec3(0.0f, 0.0f, 1.0f)); - uboVS.normal = glm::inverseTranspose(uboVS.view * uboVS.model); - uboVS.lightPos = lightPos; - - VK_CHECK_RESULT(uniformData.meshVS.map()); memcpy(uniformData.meshVS.mapped, &uboVS, sizeof(uboVS)); - uniformData.meshVS.unmap(); } void draw() { VulkanExampleBase::prepareFrame(); - submitInfo.commandBufferCount = 1; submitInfo.pCommandBuffers = &drawCmdBuffers[currentBuffer]; VK_CHECK_RESULT(vkQueueSubmit(queue, 1, &submitInfo, VK_NULL_HANDLE)); - VulkanExampleBase::submitFrame(); }