Skip to content

Commit d662cb0

Browse files
committed
Add move camera
1 parent d67a432 commit d662cb0

File tree

13 files changed

+2212
-89
lines changed

13 files changed

+2212
-89
lines changed

docs/codes/03/10_movecamera/main.cpp

Lines changed: 1613 additions & 0 deletions
Large diffs are not rendered by default.

docs/codes/03/10_movecamera/main.diff

Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
diff --git a/src/main.cpp b/src/main.cpp
2+
index 4f7b280..4ec3b5f 100644
3+
--- a/src/main.cpp
4+
+++ b/src/main.cpp
5+
@@ -110,6 +110,12 @@ private:
6+
std::vector<vk::raii::Fence> m_inFlightFences;
7+
uint32_t m_currentFrame = 0;
8+
bool m_framebufferResized = false;
9+
+ glm::vec3 m_cameraPos{ 2.0f, 2.0f, 2.0f };
10+
+ glm::vec3 m_cameraUp{ 0.0f, 1.0f, 0.0f };
11+
+ float m_pitch = -35.0f;
12+
+ float m_yaw = -135.0f;
13+
+ float m_cameraMoveSpeed = 1.0f;
14+
+ float m_cameraRotateSpeed = 25.0f;
15+
/////////////////////////////////////////////////////////////////
16+
17+
/////////////////////////////////////////////////////////////////
18+
@@ -1071,21 +1077,29 @@ private:
19+
}
20+
}
21+
void updateUniformBuffer(uint32_t currentImage) {
22+
- static auto startTime = std::chrono::high_resolution_clock::now();
23+
+ updateCamera();
24+
25+
- auto currentTime = std::chrono::high_resolution_clock::now();
26+
- float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
27+
+ glm::vec3 front;
28+
+ front.x = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
29+
+ front.y = std::sin(glm::radians(m_pitch));
30+
+ front.z = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
31+
+ front = glm::normalize(front);
32+
33+
UniformBufferObject ubo{};
34+
ubo.model = glm::rotate(
35+
glm::mat4(1.0f),
36+
- time * glm::radians(90.0f),
37+
+ glm::radians(-90.0f),
38+
+ glm::vec3(1.0f, 0.0f, 0.0f)
39+
+ );
40+
+ ubo.model *= glm::rotate(
41+
+ glm::mat4(1.0f),
42+
+ glm::radians(-90.0f),
43+
glm::vec3(0.0f, 0.0f, 1.0f)
44+
);
45+
ubo.view = glm::lookAt(
46+
- glm::vec3(2.0f, 2.0f, 2.0f),
47+
- glm::vec3(0.0f, 0.0f, 0.0f),
48+
- glm::vec3(0.0f, 0.0f, 1.0f)
49+
+ m_cameraPos,
50+
+ m_cameraPos + front,
51+
+ m_cameraUp
52+
);
53+
ubo.proj = glm::perspective(
54+
glm::radians(45.0f),
55+
@@ -1535,6 +1549,51 @@ private:
56+
}
57+
}
58+
/////////////////////////////////////////////////////////////////
59+
+
60+
+ /////////////////////////////////////////////////////////////////
61+
+ /// move camera
62+
+ void updateCamera() {
63+
+ static auto startTime = std::chrono::high_resolution_clock::now();
64+
+ auto currentTime = std::chrono::high_resolution_clock::now();
65+
+ float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
66+
+ startTime = currentTime;
67+
+
68+
+ glm::vec3 front;
69+
+ front.x = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
70+
+ front.y = 0.0f;
71+
+ front.z = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
72+
+ front = glm::normalize(front);
73+
+
74+
+ if (glfwGetKey(m_window, GLFW_KEY_W) == GLFW_PRESS)
75+
+ m_cameraPos += front * m_cameraMoveSpeed * time;
76+
+ if (glfwGetKey(m_window, GLFW_KEY_S) == GLFW_PRESS)
77+
+ m_cameraPos -= front * m_cameraMoveSpeed * time;
78+
+ if (glfwGetKey(m_window, GLFW_KEY_A) == GLFW_PRESS)
79+
+ m_cameraPos -= glm::normalize(glm::cross(front, m_cameraUp)) * m_cameraMoveSpeed * time;
80+
+ if (glfwGetKey(m_window, GLFW_KEY_D) == GLFW_PRESS)
81+
+ m_cameraPos += glm::normalize(glm::cross(front, m_cameraUp)) * m_cameraMoveSpeed * time;
82+
+ if (glfwGetKey(m_window, GLFW_KEY_SPACE) == GLFW_PRESS)
83+
+ m_cameraPos += m_cameraUp * m_cameraMoveSpeed * time;
84+
+ if (glfwGetKey(m_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
85+
+ m_cameraPos -= m_cameraUp *m_cameraMoveSpeed * time;
86+
+
87+
+ if (glfwGetKey(m_window, GLFW_KEY_UP) == GLFW_PRESS)
88+
+ m_pitch += m_cameraRotateSpeed * time;
89+
+ if (glfwGetKey(m_window, GLFW_KEY_DOWN) == GLFW_PRESS)
90+
+ m_pitch -= m_cameraRotateSpeed * time;
91+
+ if (glfwGetKey(m_window, GLFW_KEY_LEFT) == GLFW_PRESS)
92+
+ m_yaw -= m_cameraRotateSpeed * time;
93+
+ if (glfwGetKey(m_window, GLFW_KEY_RIGHT) == GLFW_PRESS)
94+
+ m_yaw += m_cameraRotateSpeed * time;
95+
+
96+
+ m_yaw = std::fmodf(m_yaw + 180.0f, 360.0f);
97+
+ if (m_yaw < 0.0f) m_yaw += 360.0f;
98+
+ m_yaw -= 180.0f;
99+
+
100+
+ if (m_pitch > 89.0f) m_pitch = 89.0f;
101+
+ if (m_pitch < -89.0f) m_pitch = -89.0f;
102+
+ }
103+
+ /////////////////////////////////////////////////////////////////
104+
};
105+
106+

docs/codes/03/10_mipmaps/main.cpp renamed to docs/codes/03/20_mipmaps/main.cpp

Lines changed: 66 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,12 @@ class HelloTriangleApplication {
111111
std::vector<vk::raii::Fence> m_inFlightFences;
112112
uint32_t m_currentFrame = 0;
113113
bool m_framebufferResized = false;
114+
glm::vec3 m_cameraPos{ 2.0f, 2.0f, 2.0f };
115+
glm::vec3 m_cameraUp{ 0.0f, 1.0f, 0.0f };
116+
float m_pitch = -35.0f;
117+
float m_yaw = -135.0f;
118+
float m_cameraMoveSpeed = 1.0f;
119+
float m_cameraRotateSpeed = 25.0f;
114120
/////////////////////////////////////////////////////////////////
115121

116122
/////////////////////////////////////////////////////////////////
@@ -1077,21 +1083,29 @@ class HelloTriangleApplication {
10771083
}
10781084
}
10791085
void updateUniformBuffer(uint32_t currentImage) {
1080-
static auto startTime = std::chrono::high_resolution_clock::now();
1086+
updateCamera();
10811087

1082-
auto currentTime = std::chrono::high_resolution_clock::now();
1083-
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
1088+
glm::vec3 front;
1089+
front.x = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
1090+
front.y = std::sin(glm::radians(m_pitch));
1091+
front.z = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
1092+
front = glm::normalize(front);
10841093

10851094
UniformBufferObject ubo{};
10861095
ubo.model = glm::rotate(
10871096
glm::mat4(1.0f),
1088-
time * glm::radians(90.0f),
1097+
glm::radians(-90.0f),
1098+
glm::vec3(1.0f, 0.0f, 0.0f)
1099+
);
1100+
ubo.model *= glm::rotate(
1101+
glm::mat4(1.0f),
1102+
glm::radians(-90.0f),
10891103
glm::vec3(0.0f, 0.0f, 1.0f)
10901104
);
10911105
ubo.view = glm::lookAt(
1092-
glm::vec3(2.0f, 2.0f, 2.0f),
1093-
glm::vec3(0.0f, 0.0f, 0.0f),
1094-
glm::vec3(0.0f, 0.0f, 1.0f)
1106+
m_cameraPos,
1107+
m_cameraPos + front,
1108+
m_cameraUp
10951109
);
10961110
ubo.proj = glm::perspective(
10971111
glm::radians(45.0f),
@@ -1576,6 +1590,51 @@ class HelloTriangleApplication {
15761590
}
15771591
/////////////////////////////////////////////////////////////////
15781592

1593+
/////////////////////////////////////////////////////////////////
1594+
/// move camera
1595+
void updateCamera() {
1596+
static auto startTime = std::chrono::high_resolution_clock::now();
1597+
auto currentTime = std::chrono::high_resolution_clock::now();
1598+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
1599+
startTime = currentTime;
1600+
1601+
glm::vec3 front;
1602+
front.x = std::cos(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
1603+
front.y = 0.0f;
1604+
front.z = std::sin(glm::radians(m_yaw)) * std::cos(glm::radians(m_pitch));
1605+
front = glm::normalize(front);
1606+
1607+
if (glfwGetKey(m_window, GLFW_KEY_W) == GLFW_PRESS)
1608+
m_cameraPos += front * m_cameraMoveSpeed * time;
1609+
if (glfwGetKey(m_window, GLFW_KEY_S) == GLFW_PRESS)
1610+
m_cameraPos -= front * m_cameraMoveSpeed * time;
1611+
if (glfwGetKey(m_window, GLFW_KEY_A) == GLFW_PRESS)
1612+
m_cameraPos -= glm::normalize(glm::cross(front, m_cameraUp)) * m_cameraMoveSpeed * time;
1613+
if (glfwGetKey(m_window, GLFW_KEY_D) == GLFW_PRESS)
1614+
m_cameraPos += glm::normalize(glm::cross(front, m_cameraUp)) * m_cameraMoveSpeed * time;
1615+
if (glfwGetKey(m_window, GLFW_KEY_SPACE) == GLFW_PRESS)
1616+
m_cameraPos += m_cameraUp * m_cameraMoveSpeed * time;
1617+
if (glfwGetKey(m_window, GLFW_KEY_LEFT_SHIFT) == GLFW_PRESS)
1618+
m_cameraPos -= m_cameraUp *m_cameraMoveSpeed * time;
1619+
1620+
if (glfwGetKey(m_window, GLFW_KEY_UP) == GLFW_PRESS)
1621+
m_pitch += m_cameraRotateSpeed * time;
1622+
if (glfwGetKey(m_window, GLFW_KEY_DOWN) == GLFW_PRESS)
1623+
m_pitch -= m_cameraRotateSpeed * time;
1624+
if (glfwGetKey(m_window, GLFW_KEY_LEFT) == GLFW_PRESS)
1625+
m_yaw -= m_cameraRotateSpeed * time;
1626+
if (glfwGetKey(m_window, GLFW_KEY_RIGHT) == GLFW_PRESS)
1627+
m_yaw += m_cameraRotateSpeed * time;
1628+
1629+
m_yaw = std::fmodf(m_yaw + 180.0f, 360.0f);
1630+
if (m_yaw < 0.0f) m_yaw += 360.0f;
1631+
m_yaw -= 180.0f;
1632+
1633+
if (m_pitch > 89.0f) m_pitch = 89.0f;
1634+
if (m_pitch < -89.0f) m_pitch = -89.0f;
1635+
}
1636+
/////////////////////////////////////////////////////////////////
1637+
15791638
/////////////////////////////////////////////////////////////////
15801639
/// mipmaps
15811640
void generateMipmaps(

docs/codes/03/10_mipmaps/main.diff renamed to docs/codes/03/20_mipmaps/main.diff

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
diff --git a/src/main.cpp b/src/main.cpp
2-
index 6dabfb1..89a0f11 100644
2+
index 4ec3b5f..46c75ca 100644
33
--- a/src/main.cpp
44
+++ b/src/main.cpp
55
@@ -85,6 +85,7 @@ private:
@@ -10,7 +10,7 @@ index 6dabfb1..89a0f11 100644
1010
vk::raii::Image m_textureImage{ nullptr };
1111
vk::raii::ImageView m_textureImageView{ nullptr };
1212
vk::raii::Sampler m_textureSampler{ nullptr };
13-
@@ -492,7 +493,12 @@ private:
13+
@@ -498,7 +499,12 @@ private:
1414
m_swapChainImageViews.reserve( m_swapChainImages.size() );
1515
for (size_t i = 0; i < m_swapChainImages.size(); ++i) {
1616
m_swapChainImageViews.emplace_back(
@@ -24,15 +24,15 @@ index 6dabfb1..89a0f11 100644
2424
);
2525
}
2626
}
27-
@@ -1185,6 +1191,7 @@ private:
27+
@@ -1196,6 +1202,7 @@ private:
2828
void createImage(
2929
uint32_t width,
3030
uint32_t height,
3131
+ uint32_t mipLevels,
3232
vk::Format format,
3333
vk::ImageTiling tilling,
3434
vk::ImageUsageFlags usage,
35-
@@ -1197,7 +1204,7 @@ private:
35+
@@ -1208,7 +1215,7 @@ private:
3636
imageInfo.extent.width = width;
3737
imageInfo.extent.height = height;
3838
imageInfo.extent.depth = 1;
@@ -41,7 +41,7 @@ index 6dabfb1..89a0f11 100644
4141
imageInfo.arrayLayers = 1;
4242
imageInfo.format = format;
4343
imageInfo.tiling = tilling;
44-
@@ -1221,7 +1228,8 @@ private:
44+
@@ -1232,7 +1239,8 @@ private:
4545
vk::raii::Image& image,
4646
vk::Format format,
4747
vk::ImageLayout oldLayout,
@@ -51,7 +51,7 @@ index 6dabfb1..89a0f11 100644
5151
) {
5252
vk::raii::CommandBuffer commandBuffer = beginSingleTimeCommands();
5353

54-
@@ -1233,7 +1241,7 @@ private:
54+
@@ -1244,7 +1252,7 @@ private:
5555
barrier.image = image;
5656
// barrier.subresourceRange.aspectMask = vk::ImageAspectFlagBits::eColor;
5757
barrier.subresourceRange.baseMipLevel = 0;
@@ -60,15 +60,15 @@ index 6dabfb1..89a0f11 100644
6060
barrier.subresourceRange.baseArrayLayer = 0;
6161
barrier.subresourceRange.layerCount = 1;
6262

63-
@@ -1324,6 +1332,7 @@ private:
63+
@@ -1335,6 +1343,7 @@ private:
6464
throw std::runtime_error("failed to load texture image!");
6565
}
6666
vk::DeviceSize imageSize = texWidth * texHeight * 4;
6767
+ m_mipLevels = static_cast<uint32_t>(std::floor(std::log2(std::max(texWidth, texHeight)))) + 1;
6868

6969
vk::raii::DeviceMemory stagingBufferMemory{ nullptr };
7070
vk::raii::Buffer stagingBuffer{ nullptr };
71-
@@ -1346,8 +1355,10 @@ private:
71+
@@ -1357,8 +1366,10 @@ private:
7272
createImage(
7373
texWidth,
7474
texHeight,
@@ -79,7 +79,7 @@ index 6dabfb1..89a0f11 100644
7979
vk::ImageUsageFlagBits::eTransferDst |
8080
vk::ImageUsageFlagBits::eSampled,
8181
vk::MemoryPropertyFlagBits::eDeviceLocal,
82-
@@ -1359,7 +1370,8 @@ private:
82+
@@ -1370,7 +1381,8 @@ private:
8383
m_textureImage,
8484
vk::Format::eR8G8B8A8Srgb,
8585
vk::ImageLayout::eUndefined,
@@ -89,7 +89,7 @@ index 6dabfb1..89a0f11 100644
8989
);
9090

9191
copyBufferToImage(
92-
@@ -1369,33 +1381,52 @@ private:
92+
@@ -1380,33 +1392,52 @@ private:
9393
static_cast<uint32_t>(texHeight)
9494
);
9595

@@ -148,7 +148,7 @@ index 6dabfb1..89a0f11 100644
148148
}
149149
void createTextureSampler() {
150150
vk::SamplerCreateInfo samplerInfo;
151-
@@ -1420,7 +1451,9 @@ private:
151+
@@ -1431,7 +1462,9 @@ private:
152152
samplerInfo.mipmapMode = vk::SamplerMipmapMode::eLinear;
153153
samplerInfo.mipLodBias = 0.0f;
154154
samplerInfo.minLod = 0.0f;
@@ -159,15 +159,15 @@ index 6dabfb1..89a0f11 100644
159159

160160
m_textureSampler = m_device.createSampler(samplerInfo);
161161
}
162-
@@ -1466,6 +1499,7 @@ private:
162+
@@ -1477,6 +1510,7 @@ private:
163163
createImage(
164164
m_swapChainExtent.width,
165165
m_swapChainExtent.height,
166166
+ 1,
167167
depthFormat,
168168
vk::ImageTiling::eOptimal,
169169
vk::ImageUsageFlagBits::eDepthStencilAttachment,
170-
@@ -1474,13 +1508,19 @@ private:
170+
@@ -1485,13 +1519,19 @@ private:
171171
m_depthImageMemory
172172
);
173173

@@ -189,8 +189,8 @@ index 6dabfb1..89a0f11 100644
189189
// );
190190
}
191191
/////////////////////////////////////////////////////////////////
192-
@@ -1538,6 +1578,114 @@ private:
193-
}
192+
@@ -1594,6 +1634,114 @@ private:
193+
if (m_pitch < -89.0f) m_pitch = -89.0f;
194194
}
195195
/////////////////////////////////////////////////////////////////
196196
+

0 commit comments

Comments
 (0)