Skip to content

Commit a4990f0

Browse files
committed
Changed Perspective to Projection
1 parent 800adbb commit a4990f0

File tree

38 files changed

+219
-219
lines changed

38 files changed

+219
-219
lines changed

bookcontents/chapter-07/chapter-07.md

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -367,36 +367,36 @@ After that we create a reference to the depth attachment (`VkAttachmentReference
367367

368368
## Scene changes
369369

370-
If we are going to represent 3D scenes, we need to project from the 3D world into a 2D space (the screen). We will need to use a perspective projection matrix in order to avoid distortions and to represent far away objects smaller than closer ones. We will create a new class that will support its creation and update (due to windows resizing) named `Perspective`, which is defined like this:
370+
If we are going to represent 3D scenes, we need to project from the 3D world into a 2D space (the screen). We will need to use a perspective projection matrix in order to avoid distortions and to represent far away objects smaller than closer ones. We will create a new class that will support its creation and update (due to windows resizing) named `Projection`, which is defined like this:
371371

372372
```java
373373
package org.vulkanb.eng.scene;
374374

375375
import org.joml.Matrix4f;
376376
import org.vulkanb.eng.EngineProperties;
377377

378-
public class Perspective {
378+
public class Projection {
379379

380-
private Matrix4f perspectiveMatrix;
380+
private Matrix4f projectionMatrix;
381381

382-
public Perspective() {
383-
perspectiveMatrix = new Matrix4f();
382+
public Projection() {
383+
projectionMatrix = new Matrix4f();
384384
}
385385

386-
public Matrix4f getPerspectiveMatrix() {
387-
return perspectiveMatrix;
386+
public Matrix4f getProjectionMatrix() {
387+
return projectionMatrix;
388388
}
389389

390390
public void resize(int width, int height) {
391391
EngineProperties engProps = EngineProperties.getInstance();
392-
perspectiveMatrix.identity();
393-
perspectiveMatrix.perspective(engProps.getFov(), (float) width / (float) height,
392+
projectionMatrix.identity();
393+
projectionMatrix.perspective(engProps.getFov(), (float) width / (float) height,
394394
engProps.getZNear(), engProps.getZFar(), true);
395395
}
396396
}
397397
```
398398

399-
We are using the [JOML](https://github.com/JOML-CI/JOML) library to create a `Matrix4f` which will hold the projection matrix. The `resize`should be called, at least once, to initialize the parameters of the matrix with the correct parameters (by invoking the `Matrix4f` `perspective`method). The `perspective` method receives the usual arguments, the field of view (in radians), the aspect ratio and the near and far planes. You may have noticed that it receives an extra argument, which in JOML documentation is named `zZeroToOne`. We have already talked about that Vulkan uses a different coordinate System where y axis point downwards. Vulkan also defines a different range for z coordinates. The Normalized Device Coordinates (NDC), in OpenGL use the range [-1, 1] to represent screen coordinates and also for the z value. Anything outside that range is considered to be out of the field of view and will not be presented. In Vulkan, and also in Direct3D, the NDC range used for the z coordinates is [0, 1]. We need to set the `zZeroToOne` parameter to true to be compliant with Vulkan coordinates System.
399+
We are using the [JOML](https://github.com/JOML-CI/JOML) library to create a `Matrix4f` which will hold the projection matrix. The `resize`should be called, at least once, to initialize the parameters of the matrix with the correct parameters (by invoking the `Matrix4f` `Projection`method). The `Projection` method receives the usual arguments, the field of view (in radians), the aspect ratio and the near and far planes. You may have noticed that it receives an extra argument, which in JOML documentation is named `zZeroToOne`. We have already talked about that Vulkan uses a different coordinate System where y axis point downwards. Vulkan also defines a different range for z coordinates. The Normalized Device Coordinates (NDC), in OpenGL use the range [-1, 1] to represent screen coordinates and also for the z value. Anything outside that range is considered to be out of the field of view and will not be presented. In Vulkan, and also in Direct3D, the NDC range used for the z coordinates is [0, 1]. We need to set the `zZeroToOne` parameter to true to be compliant with Vulkan coordinates System.
400400

401401
The parameters of the perspective matrix can be configured in the `eng.properties` file, so we need to update the `EngineProperties` class:
402402
```java
@@ -498,24 +498,24 @@ public class Entity {
498498

499499
Each `Entity` shall have an identifier which should be unique. It is also linked to the mesh that will be used to render it through the `meshId` attribute. An `Entity` will have also a position, a rotation (modeled using a quaternion) and a scale. With all that information we are able to construct a model matrix by calling the `updateModelMatrix` from the `Matrix4f` class. The `updateModelMatrix` should be called, each time the position, rotation or scale changes.
500500

501-
Now we can setup the required infrastructure to put the `Perspective` and `Entity` classes into work. We will add this to an empty class which has been there since the beginning, the `Scene` class. The class definition starts like this:
501+
Now we can setup the required infrastructure to put the `Projection` and `Entity` classes into work. We will add this to an empty class which has been there since the beginning, the `Scene` class. The class definition starts like this:
502502

503503
```java
504504
public class Scene {
505505

506506
private Map<String, List<Entity>> entitiesMap;
507-
private Perspective perspective;
507+
private Projection projection;
508508

509509
public Scene(Window window) {
510510
entitiesMap = new HashMap<>();
511-
perspective = new Perspective();
512-
perspective.resize(window.getWidth(), window.getHeight());
511+
projection = new Projection();
512+
projection.resize(window.getWidth(), window.getHeight());
513513
}
514514
...
515515
}
516516
```
517517

518-
The constructor receives the a `Window` instance and creates a `Map` of `List`s which will contain `Entity` instances. That map will organized the entities by its `meshId`. The constructor initializes that map and also creates an instance of the `Perspective` class, which will hold the perspective matrix. The next method will be used to add new entities:
518+
The constructor receives the a `Window` instance and creates a `Map` of `List`s which will contain `Entity` instances. That map will organized the entities by its `meshId`. The constructor initializes that map and also creates an instance of the `Projection` class, which will hold the perspective matrix. The next method will be used to add new entities:
519519

520520
```java
521521
public class Scene {
@@ -532,7 +532,7 @@ public class Scene {
532532
}
533533
```
534534

535-
As it can be seen, the entities are organized by their `meshId`. Those entities which share the same `meshId` will be placed inside a `List` associated to that identifier. This will allow us to organize the rendering later on in an efficient way. Although each entity has different parameters they will share the vertices, textures, etc. defined by the mesh. Organizing the rendering around mesh information will allow us to bind those common resources just once per mesh. The rest of the methods of the `Scene` class, are used to access the entities map, to get and remove specific entities (suing its identifier) and to get the perspective matrix.
535+
As it can be seen, the entities are organized by their `meshId`. Those entities which share the same `meshId` will be placed inside a `List` associated to that identifier. This will allow us to organize the rendering later on in an efficient way. Although each entity has different parameters they will share the vertices, textures, etc. defined by the mesh. Organizing the rendering around mesh information will allow us to bind those common resources just once per mesh. The rest of the methods of the `Scene` class, are used to access the entities map, to get and remove specific entities (suing its identifier) and to get the projection matrix.
536536

537537
```java
538538
public class Scene {
@@ -545,8 +545,8 @@ public class Scene {
545545
return entitiesMap;
546546
}
547547

548-
public Perspective getPerspective() {
549-
return perspective;
548+
public Projection getProjection() {
549+
return projection;
550550
}
551551

552552
public void removeAllEntities() {
@@ -689,7 +689,7 @@ public class Render {
689689
if (window.isResized() || this.swapChain.acquireNextImage()) {
690690
window.resetResized();
691691
resize(window);
692-
scene.getPerspective().resize(window.getWidth(), window.getHeight());
692+
scene.getProjection().resize(window.getWidth(), window.getHeight());
693693
swapChain.acquireNextImage();
694694
}
695695

@@ -842,7 +842,7 @@ public class ForwardRenderActivity {
842842
843843
List<Entity> entities = scene.getEntitiesByMeshId(mesh.getId());
844844
for (Entity entity : entities) {
845-
setPushConstants(cmdHandle, scene.getPerspective().getPerspectiveMatrix(), entity.getModelMatrix(),
845+
setPushConstants(cmdHandle, scene.getProjection().getProjectionMatrix(), entity.getModelMatrix(),
846846
pushConstantBuffer);
847847
vkCmdDrawIndexed(cmdHandle, mesh.getIndicesCount(), 1, 0, 0, 0);
848848
}

bookcontents/chapter-08/chapter-08.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1210,7 +1210,7 @@ public class ForwardRenderActivity {
12101210
...
12111211
public ForwardRenderActivity(SwapChain swapChain, CommandPool commandPool, PipelineCache pipelineCache, Scene scene) {
12121212
...
1213-
VulkanUtils.copyMatrixToBuffer(projMatrixUniform, scene.getPerspective().getPerspectiveMatrix());
1213+
VulkanUtils.copyMatrixToBuffer(projMatrixUniform, scene.getProjection().getProjectionMatrix());
12141214
}
12151215
...
12161216
}
@@ -1323,7 +1323,7 @@ The `resize` method needs also to be modified to update the buffer that will bac
13231323
public class ForwardRenderActivity {
13241324
...
13251325
public void resize(SwapChain swapChain, Scene scene) {
1326-
VulkanUtils.copyMatrixToBuffer(projMatrixUniform, scene.getPerspective().getPerspectiveMatrix());
1326+
VulkanUtils.copyMatrixToBuffer(projMatrixUniform, scene.getProjection().getProjectionMatrix());
13271327
...
13281328
}
13291329
...

bookcontents/chapter-09/chapter-09.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -665,7 +665,7 @@ public class ForwardRenderActivity {
665665

666666
We will be using four descriptor sets now, the first one for the perspective projection matrix, the second one will be used for the view matrix, the third one for the texture sampler and, finally, the fourth one for the materials uniform. Therefore, we need to update the descriptor sets layouts that will be used in the pipeline. After that, we setup the number of descriptor sets we need according to their type:
667667

668-
- We will use the `VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER` descriptor sets for the uniforms associated to the perspective projection matrix and the view matrix. We just need one descriptor set for the perspective uniform, since we will not be changing it while drawing a frame. However, the view matrix can be modified while rendering, so we will create separate descriptor sets (and also the associated buffers), one per swap chain image.
668+
- We will use the `VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER` descriptor sets for the uniforms associated to the perspective projection matrix and the view matrix. We just need one descriptor set for the projection uniform, since we will not be changing it while drawing a frame. However, the view matrix can be modified while rendering, so we will create separate descriptor sets (and also the associated buffers), one per swap chain image.
669669
- We will use the `VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER` descriptor sets for the combined texture samplers. We will need as many descriptor sets as textures we will have. Here we are defining a new property to configure the maximum number of supported materials. As explained in the previous chapter, we don't need separate descriptor sets per swap chain image. Textures here are read only.
670670
- We will use the `VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER_DYNAMIC` descriptor sets for the uniform associated to the material data. If we were not using dynamic uniform buffers, we would need at least one per material. We are using a single descriptor set to hold all the material data, since we will not be updating them while rendering.
671671

bookcontents/chapter-10/chapter-10.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ public class GeometryRenderActivity {
338338
createDescriptorSets(numImages);
339339
createPipeline();
340340
createCommandBuffers(commandPool, numImages);
341-
VulkanUtils.copyMatrixToBuffer(projMatrixUniform, scene.getPerspective().getPerspectiveMatrix());
341+
VulkanUtils.copyMatrixToBuffer(projMatrixUniform, scene.getProjection().getProjectionMatrix());
342342
}
343343

344344
public void cleanup() {
@@ -641,7 +641,7 @@ This method is almost identical than the one used in the `ForwardRenderActivity`
641641
public class GeometryRenderActivity {
642642
...
643643
public void resize(SwapChain swapChain, Scene scene) {
644-
VulkanUtils.copyMatrixToBuffer(projMatrixUniform, scene.getPerspective().getPerspectiveMatrix());
644+
VulkanUtils.copyMatrixToBuffer(projMatrixUniform, scene.getProjection().getProjectionMatrix());
645645
this.swapChain = swapChain;
646646
geometryFrameBuffer.resize(swapChain);
647647
}

bookcontents/chapter-11/chapter-11.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,7 +983,7 @@ Finally, we need to add two new methods to update the inverse projection matrix
983983
public class LightingRenderActivity {
984984
...
985985
private void updateInvProjMatrix(Scene scene) {
986-
Matrix4f invProj = new Matrix4f(scene.getPerspective().getPerspectiveMatrix()).invert();
986+
Matrix4f invProj = new Matrix4f(scene.getProjection().getProjectionMatrix()).invert();
987987
VulkanUtils.copyMatrixToBuffer(device, invProjBuffer, invProj);
988988
}
989989

booksamples/chapter-07/src/main/java/org/vulkanb/eng/graph/ForwardRenderActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ public void recordCommandBuffers(List<VulkanMesh> meshes, Scene scene) {
167167

168168
List<Entity> entities = scene.getEntitiesByMeshId(mesh.getId());
169169
for (Entity entity : entities) {
170-
setPushConstants(cmdHandle, scene.getPerspective().getPerspectiveMatrix(), entity.getModelMatrix(),
170+
setPushConstants(cmdHandle, scene.getProjection().getProjectionMatrix(), entity.getModelMatrix(),
171171
pushConstantBuffer);
172172
vkCmdDrawIndexed(cmdHandle, mesh.getIndicesCount(), 1, 0, 0, 0);
173173
}

booksamples/chapter-07/src/main/java/org/vulkanb/eng/graph/Render.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public void render(Window window, Scene scene) {
6969
if (window.isResized() || swapChain.acquireNextImage()) {
7070
window.resetResized();
7171
resize(window);
72-
scene.getPerspective().resize(window.getWidth(), window.getHeight());
72+
scene.getProjection().resize(window.getWidth(), window.getHeight());
7373
swapChain.acquireNextImage();
7474
}
7575

booksamples/chapter-07/src/main/java/org/vulkanb/eng/scene/Perspective.java

Lines changed: 0 additions & 24 deletions
This file was deleted.
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package org.vulkanb.eng.scene;
2+
3+
import org.joml.Matrix4f;
4+
import org.vulkanb.eng.EngineProperties;
5+
6+
public class Projection {
7+
8+
private Matrix4f projectionMatrix;
9+
10+
public Projection() {
11+
projectionMatrix = new Matrix4f();
12+
}
13+
14+
public Matrix4f getProjectionMatrix() {
15+
return projectionMatrix;
16+
}
17+
18+
public void resize(int width, int height) {
19+
EngineProperties engProps = EngineProperties.getInstance();
20+
projectionMatrix.identity();
21+
projectionMatrix.perspective(engProps.getFov(), (float) width / (float) height,
22+
engProps.getZNear(), engProps.getZFar(), true);
23+
}
24+
}

booksamples/chapter-07/src/main/java/org/vulkanb/eng/scene/Scene.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
public class Scene {
88

99
private Map<String, List<Entity>> entitiesMap;
10-
private Perspective perspective;
10+
private Projection projection;
1111

1212
public Scene(Window window) {
1313
entitiesMap = new HashMap<>();
14-
perspective = new Perspective();
15-
perspective.resize(window.getWidth(), window.getHeight());
14+
projection = new Projection();
15+
projection.resize(window.getWidth(), window.getHeight());
1616
}
1717

1818
public void addEntity(Entity entity) {
@@ -32,8 +32,8 @@ public Map<String, List<Entity>> getEntitiesMap() {
3232
return entitiesMap;
3333
}
3434

35-
public Perspective getPerspective() {
36-
return perspective;
35+
public Projection getProjection() {
36+
return projection;
3737
}
3838

3939
public void removeAllEntities() {

0 commit comments

Comments
 (0)