-
Notifications
You must be signed in to change notification settings - Fork 28
/
Ch27ModelLoading.diff
203 lines (170 loc) · 9.07 KB
/
Ch27ModelLoading.diff
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
diff --git "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch26DepthBuffering.java" "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch27ModelLoading.java"
index 5e5873b..ca38319 100644
--- "a/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch26DepthBuffering.java"
+++ "b/G:\\Java-Dev\\Vulkan-Tutorial-Java\\src\\main\\java\\javavulkantutorial\\Ch27ModelLoading.java"
@@ -3,10 +3,14 @@ package javavulkantutorial;
import javavulkantutorial.ShaderSPIRVUtils.SPIRV;
import org.joml.*;
import org.lwjgl.PointerBuffer;
+import org.lwjgl.assimp.AIScene;
+import org.lwjgl.assimp.Assimp;
+import org.lwjgl.system.Configuration;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.Pointer;
import org.lwjgl.vulkan.*;
+import java.io.File;
import java.lang.Math;
import java.net.URI;
import java.net.URISyntaxException;
@@ -22,9 +26,11 @@ import static java.lang.ClassLoader.getSystemClassLoader;
import static java.util.stream.Collectors.toSet;
import static javavulkantutorial.AlignmentUtils.alignas;
import static javavulkantutorial.AlignmentUtils.alignof;
+import static javavulkantutorial.ModelLoader.*;
import static javavulkantutorial.ShaderSPIRVUtils.ShaderKind.FRAGMENT_SHADER;
import static javavulkantutorial.ShaderSPIRVUtils.ShaderKind.VERTEX_SHADER;
import static javavulkantutorial.ShaderSPIRVUtils.compileShaderFile;
+import static org.lwjgl.assimp.Assimp.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.glfw.GLFWVulkan.glfwCreateWindowSurface;
import static org.lwjgl.glfw.GLFWVulkan.glfwGetRequiredInstanceExtensions;
@@ -38,7 +44,7 @@ import static org.lwjgl.vulkan.KHRSurface.*;
import static org.lwjgl.vulkan.KHRSwapchain.*;
import static org.lwjgl.vulkan.VK10.*;
-public class Ch26DepthBuffering {
+public class Ch27ModelLoading {
private static class HelloTriangleApplication {
@@ -197,23 +203,6 @@ public class Ch26DepthBuffering {
}
- private static final Vertex[] VERTICES = {
- new Vertex(new Vector3f(-0.5f, -0.5f, 0.0f ), new Vector3f(1.0f, 0.0f, 0.0f), new Vector2f(0.0f, 0.0f)),
- new Vertex(new Vector3f(0.5f, -0.5f, 0.0f ), new Vector3f(0.0f, 1.0f, 0.0f), new Vector2f(1.0f, 0.0f)),
- new Vertex(new Vector3f(0.5f, 0.5f, 0.0f ), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(1.0f, 1.0f)),
- new Vertex(new Vector3f(-0.5f, 0.5f, 0.0f ), new Vector3f(1.0f, 1.0f, 1.0f), new Vector2f(0.0f, 1.0f)),
-
- new Vertex(new Vector3f(-0.5f, -0.5f, -0.5f), new Vector3f(1.0f, 0.0f, 0.0f), new Vector2f(0.0f, 0.0f)),
- new Vertex(new Vector3f(0.5f, -0.5f, -0.5f ), new Vector3f(0.0f, 1.0f, 0.0f), new Vector2f(1.0f, 0.0f)),
- new Vertex(new Vector3f(0.5f, 0.5f, -0.5f ), new Vector3f(0.0f, 0.0f, 1.0f), new Vector2f(1.0f, 1.0f)),
- new Vertex(new Vector3f(-0.5f, 0.5f, -0.5f ), new Vector3f(1.0f, 1.0f, 1.0f), new Vector2f(0.0f, 1.0f))
- };
-
- private static final /*uint16_t*/ short[] INDICES = {
- 0, 1, 2, 2, 3, 0,
- 4, 5, 6, 6, 7, 4
- };
-
// ======= FIELDS ======= //
private long window;
@@ -253,6 +242,8 @@ public class Ch26DepthBuffering {
private long textureImageView;
private long textureSampler;
+ private Vertex[] vertices;
+ private int[] indices;
private long vertexBuffer;
private long vertexBufferMemory;
private long indexBuffer;
@@ -319,6 +310,7 @@ public class Ch26DepthBuffering {
createTextureImage();
createTextureImageView();
createTextureSampler();
+ loadModel();
createVertexBuffer();
createIndexBuffer();
createDescriptorSetLayout();
@@ -1067,7 +1059,7 @@ public class Ch26DepthBuffering {
try(MemoryStack stack = stackPush()) {
- String filename = Paths.get(new URI(getSystemClassLoader().getResource("textures/texture.jpg").toExternalForm())).toString();
+ String filename = Paths.get(new URI(getSystemClassLoader().getResource("textures/chalet.jpg").toExternalForm())).toString();
IntBuffer pWidth = stack.mallocInt(1);
IntBuffer pHeight = stack.mallocInt(1);
@@ -1332,11 +1324,37 @@ public class Ch26DepthBuffering {
src.limit(src.capacity()).rewind();
}
+ private void loadModel() {
+
+ File modelFile = new File(getSystemClassLoader().getResource("models/chalet.obj").getFile());
+
+ Model model = ModelLoader.loadModel(modelFile, aiProcess_FlipUVs | aiProcess_DropNormals);
+
+ final int vertexCount = model.positions.size();
+
+ vertices = new Vertex[vertexCount];
+
+ final Vector3fc color = new Vector3f(1.0f, 1.0f, 1.0f);
+
+ for(int i = 0;i < vertexCount;i++) {
+ vertices[i] = new Vertex(
+ model.positions.get(i),
+ color,
+ model.texCoords.get(i));
+ }
+
+ indices = new int[model.indices.size()];
+
+ for(int i = 0;i < indices.length;i++) {
+ indices[i] = model.indices.get(i);
+ }
+ }
+
private void createVertexBuffer() {
try(MemoryStack stack = stackPush()) {
- long bufferSize = Vertex.SIZEOF * VERTICES.length;
+ long bufferSize = Vertex.SIZEOF * vertices.length;
LongBuffer pBuffer = stack.mallocLong(1);
LongBuffer pBufferMemory = stack.mallocLong(1);
@@ -1353,7 +1371,7 @@ public class Ch26DepthBuffering {
vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, data);
{
- memcpy(data.getByteBuffer(0, (int) bufferSize), VERTICES);
+ memcpy(data.getByteBuffer(0, (int) bufferSize), vertices);
}
vkUnmapMemory(device, stagingBufferMemory);
@@ -1377,7 +1395,7 @@ public class Ch26DepthBuffering {
try(MemoryStack stack = stackPush()) {
- long bufferSize = Short.BYTES * INDICES.length;
+ long bufferSize = Integer.BYTES * indices.length;
LongBuffer pBuffer = stack.mallocLong(1);
LongBuffer pBufferMemory = stack.mallocLong(1);
@@ -1394,7 +1412,7 @@ public class Ch26DepthBuffering {
vkMapMemory(device, stagingBufferMemory, 0, bufferSize, 0, data);
{
- memcpy(data.getByteBuffer(0, (int) bufferSize), INDICES);
+ memcpy(data.getByteBuffer(0, (int) bufferSize), indices);
}
vkUnmapMemory(device, stagingBufferMemory);
@@ -1443,7 +1461,7 @@ public class Ch26DepthBuffering {
try(MemoryStack stack = stackPush()) {
- VkDescriptorPoolSize.Buffer poolSizes = VkDescriptorPoolSize.callocStack(2, stack);
+ VkDescriptorPoolSize.Buffer poolSizes = VkDescriptorPoolSize.calloc(2, stack);
VkDescriptorPoolSize uniformBufferPoolSize = poolSizes.get(0);
uniformBufferPoolSize.type(VK_DESCRIPTOR_TYPE_UNIFORM_BUFFER);
@@ -1453,7 +1471,7 @@ public class Ch26DepthBuffering {
textureSamplerPoolSize.type(VK_DESCRIPTOR_TYPE_COMBINED_IMAGE_SAMPLER);
textureSamplerPoolSize.descriptorCount(swapChainImages.size());
- VkDescriptorPoolCreateInfo poolInfo = VkDescriptorPoolCreateInfo.callocStack(stack);
+ VkDescriptorPoolCreateInfo poolInfo = VkDescriptorPoolCreateInfo.calloc(stack);
poolInfo.sType(VK_STRUCTURE_TYPE_DESCRIPTOR_POOL_CREATE_INFO);
poolInfo.pPoolSizes(poolSizes);
poolInfo.maxSets(swapChainImages.size());
@@ -1634,10 +1652,10 @@ public class Ch26DepthBuffering {
}
}
- private void memcpy(ByteBuffer buffer, short[] indices) {
+ private void memcpy(ByteBuffer buffer, int[] indices) {
- for(short index : indices) {
- buffer.putShort(index);
+ for(int index : indices) {
+ buffer.putInt(index);
}
buffer.rewind();
@@ -1728,12 +1746,12 @@ public class Ch26DepthBuffering {
LongBuffer offsets = stack.longs(0);
vkCmdBindVertexBuffers(commandBuffer, 0, vertexBuffers, offsets);
- vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT16);
+ vkCmdBindIndexBuffer(commandBuffer, indexBuffer, 0, VK_INDEX_TYPE_UINT32);
vkCmdBindDescriptorSets(commandBuffer, VK_PIPELINE_BIND_POINT_GRAPHICS,
pipelineLayout, 0, stack.longs(descriptorSets.get(i)), null);
- vkCmdDrawIndexed(commandBuffer, INDICES.length, 1, 0, 0, 0);
+ vkCmdDrawIndexed(commandBuffer, indices.length, 1, 0, 0, 0);
}
vkCmdEndRenderPass(commandBuffer);