Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 0f2a76c

Browse files
committed
Improve chrono code
1 parent a5d0176 commit 0f2a76c

8 files changed

Lines changed: 11 additions & 13 deletions

05_Uniform_buffers/00_Descriptor_layout_and_buffer.md

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -332,14 +332,12 @@ void updateUniformBuffer() {
332332
static auto startTime = std::chrono::high_resolution_clock::now();
333333

334334
auto currentTime = std::chrono::high_resolution_clock::now();
335-
float time = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() / 1000.0f;
335+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
336336
}
337337
```
338338

339339
The `updateUniformBuffer` function will start out with some logic to calculate
340-
the time in seconds since rendering has started with millisecond accuracy. If
341-
you need timing to be more precise, then you can use `std::chrono::microseconds`
342-
and divide by `1e6f`, which is short for `1000000.0f`.
340+
the time in seconds since rendering has started with floating point accuracy.
343341

344342
We will now define the model, view and projection transformations in the
345343
uniform buffer object. The model rotation will be a simple rotation around the
@@ -351,7 +349,7 @@ ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0
351349
```
352350
353351
The `glm::rotate` function takes an existing transformation, rotation angle and
354-
rotation axis as parameters. The `glm::mat4(1.0f)` constructor returns an
352+
rotation axis as parameters. The `glm::mat4(1.0f)` constructor returns an
355353
identity matrix. Using a rotation angle of `time * glm::radians(90.0f)`
356354
accomplishes the purpose of rotation 90 degrees per second.
357355
@@ -403,4 +401,4 @@ transformation data.
403401
404402
[C++ code](/code/descriptor_layout.cpp) /
405403
[Vertex shader](/code/shader_ubo.vert) /
406-
[Fragment shader](/code/shader_ubo.frag)
404+
[Fragment shader](/code/shader_ubo.frag)

code/depth_buffering.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1231,7 +1231,7 @@ class HelloTriangleApplication {
12311231
static auto startTime = std::chrono::high_resolution_clock::now();
12321232

12331233
auto currentTime = std::chrono::high_resolution_clock::now();
1234-
float time = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() / 1000.0f;
1234+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
12351235

12361236
UniformBufferObject ubo = {};
12371237
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));

code/descriptor_layout.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ class HelloTriangleApplication {
858858
static auto startTime = std::chrono::high_resolution_clock::now();
859859

860860
auto currentTime = std::chrono::high_resolution_clock::now();
861-
float time = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() / 1000.0f;
861+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
862862

863863
UniformBufferObject ubo = {};
864864
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));

code/descriptor_set.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ class HelloTriangleApplication {
912912
static auto startTime = std::chrono::high_resolution_clock::now();
913913

914914
auto currentTime = std::chrono::high_resolution_clock::now();
915-
float time = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() / 1000.0f;
915+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
916916

917917
UniformBufferObject ubo = {};
918918
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));

code/model_loading.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,7 @@ class HelloTriangleApplication {
12761276
static auto startTime = std::chrono::high_resolution_clock::now();
12771277

12781278
auto currentTime = std::chrono::high_resolution_clock::now();
1279-
float time = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() / 1000.0f;
1279+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
12801280

12811281
UniformBufferObject ubo = {};
12821282
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));

code/sampler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1103,7 +1103,7 @@ class HelloTriangleApplication {
11031103
static auto startTime = std::chrono::high_resolution_clock::now();
11041104

11051105
auto currentTime = std::chrono::high_resolution_clock::now();
1106-
float time = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() / 1000.0f;
1106+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
11071107

11081108
UniformBufferObject ubo = {};
11091109
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));

code/texture_image.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,7 +1067,7 @@ class HelloTriangleApplication {
10671067
static auto startTime = std::chrono::high_resolution_clock::now();
10681068

10691069
auto currentTime = std::chrono::high_resolution_clock::now();
1070-
float time = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() / 1000.0f;
1070+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
10711071

10721072
UniformBufferObject ubo = {};
10731073
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));

code/texture_mapping.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1133,7 +1133,7 @@ class HelloTriangleApplication {
11331133
static auto startTime = std::chrono::high_resolution_clock::now();
11341134

11351135
auto currentTime = std::chrono::high_resolution_clock::now();
1136-
float time = std::chrono::duration_cast<std::chrono::milliseconds>(currentTime - startTime).count() / 1000.0f;
1136+
float time = std::chrono::duration<float, std::chrono::seconds::period>(currentTime - startTime).count();
11371137

11381138
UniformBufferObject ubo = {};
11391139
ubo.model = glm::rotate(glm::mat4(1.0f), time * glm::radians(90.0f), glm::vec3(0.0f, 0.0f, 1.0f));

0 commit comments

Comments
 (0)