forked from vkngwrapper/vulkan-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path27_model_loading.diff
185 lines (171 loc) · 6.6 KB
/
27_model_loading.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
diff --git a/../steps/26_depth_buffering/main.go b/../steps/27_model_loading/main.go
index 6065855..adacdcb 100644
--- a/../steps/26_depth_buffering/main.go
+++ b/../steps/27_model_loading/main.go
@@ -5,6 +5,7 @@ import (
"embed"
"encoding/binary"
"github.com/cockroachdb/errors"
+ "github.com/g3n/engine/loader/obj"
"github.com/loov/hrtime"
"github.com/veandco/go-sdl2/sdl"
"github.com/vkngwrapper/core/v2"
@@ -23,7 +24,7 @@ import (
"unsafe"
)
-//go:embed shaders images
+//go:embed shaders images meshes
var fileSystem embed.FS
const MaxFramesInFlight = 2
@@ -95,23 +96,6 @@ func getVertexAttributeDescriptions() []core1_0.VertexInputAttributeDescription
}
}
-var vertices = []Vertex{
- {Position: vkngmath.Vec3[float32]{X: -0.5, Y: -0.5, Z: 0}, Color: vkngmath.Vec3[float32]{X: 1, Y: 0, Z: 0}, TexCoord: vkngmath.Vec2[float32]{X: 1, Y: 0}},
- {Position: vkngmath.Vec3[float32]{X: 0.5, Y: -0.5, Z: 0}, Color: vkngmath.Vec3[float32]{X: 0, Y: 1, Z: 0}, TexCoord: vkngmath.Vec2[float32]{X: 0, Y: 0}},
- {Position: vkngmath.Vec3[float32]{X: 0.5, Y: 0.5, Z: 0}, Color: vkngmath.Vec3[float32]{X: 0, Y: 0, Z: 1}, TexCoord: vkngmath.Vec2[float32]{X: 0, Y: 1}},
- {Position: vkngmath.Vec3[float32]{X: -0.5, Y: 0.5, Z: 0}, Color: vkngmath.Vec3[float32]{X: 1, Y: 1, Z: 1}, TexCoord: vkngmath.Vec2[float32]{X: 1, Y: 1}},
-
- {Position: vkngmath.Vec3[float32]{X: -0.5, Y: -0.5, Z: -0.5}, Color: vkngmath.Vec3[float32]{X: 1, Y: 0, Z: 0}, TexCoord: vkngmath.Vec2[float32]{X: 0, Y: 0}},
- {Position: vkngmath.Vec3[float32]{X: 0.5, Y: -0.5, Z: -0.5}, Color: vkngmath.Vec3[float32]{X: 0, Y: 1, Z: 0}, TexCoord: vkngmath.Vec2[float32]{X: 1, Y: 0}},
- {Position: vkngmath.Vec3[float32]{X: 0.5, Y: 0.5, Z: -0.5}, Color: vkngmath.Vec3[float32]{X: 0, Y: 0, Z: 1}, TexCoord: vkngmath.Vec2[float32]{X: 1, Y: 1}},
- {Position: vkngmath.Vec3[float32]{X: -0.5, Y: 0.5, Z: -0.5}, Color: vkngmath.Vec3[float32]{X: 1, Y: 1, Z: 1}, TexCoord: vkngmath.Vec2[float32]{X: 0, Y: 1}},
-}
-
-var indices = []uint16{
- 0, 1, 2, 2, 3, 0,
- 4, 5, 6, 6, 7, 4,
-}
-
type HelloTriangleApplication struct {
window *sdl.Window
loader core.Loader
@@ -151,6 +135,8 @@ type HelloTriangleApplication struct {
currentFrame int
frameStart float64
+ vertices []Vertex
+ indices []uint32
vertexBuffer core1_0.Buffer
vertexBufferMemory core1_0.DeviceMemory
indexBuffer core1_0.Buffer
@@ -284,6 +270,10 @@ func (app *HelloTriangleApplication) initVulkan() error {
return err
}
+ err = app.loadModel()
+ if err != nil {
+ return err
+ }
err = app.createVertexBuffer()
if err != nil {
return err
@@ -1148,7 +1138,7 @@ func hasStencilComponent(format core1_0.Format) bool {
func (app *HelloTriangleApplication) createTextureImage() error {
//Put image data into staging buffer
- imageBytes, err := fileSystem.ReadFile("images/texture.png")
+ imageBytes, err := fileSystem.ReadFile("images/viking_room.png")
if err != nil {
return err
}
@@ -1390,9 +1380,68 @@ func writeData(memory core1_0.DeviceMemory, offset int, data any) error {
return nil
}
+func (app *HelloTriangleApplication) addVertex(decoder *obj.Decoder, uniqueVertices map[int]uint32, face obj.Face, faceIndex int) {
+ vertInd := face.Vertices[faceIndex]
+ index, vertexExists := uniqueVertices[vertInd]
+
+ if !vertexExists {
+ vert := Vertex{Position: vkngmath.Vec3[float32]{
+ X: decoder.Vertices[vertInd*3],
+ Y: decoder.Vertices[vertInd*3+1],
+ Z: decoder.Vertices[vertInd*3+2],
+ }, Color: vkngmath.Vec3[float32]{X: 1, Y: 1, Z: 1}}
+
+ uvInd := face.Uvs[faceIndex]
+ vert.TexCoord = vkngmath.Vec2[float32]{
+ X: decoder.Uvs[uvInd*2],
+ Y: 1.0 - decoder.Uvs[uvInd*2+1],
+ }
+
+ index = uint32(len(app.vertices))
+ app.vertices = append(app.vertices, vert)
+ uniqueVertices[vertInd] = index
+ }
+
+ app.indices = append(app.indices, index)
+}
+
+func (app *HelloTriangleApplication) loadModel() error {
+ meshFile, err := fileSystem.Open("meshes/viking_room.obj")
+ if err != nil {
+ return err
+ }
+ defer meshFile.Close()
+
+ matFile, err := fileSystem.Open("meshes/viking_room.mtl")
+ if err != nil {
+ return err
+ }
+ defer matFile.Close()
+
+ decoder, err := obj.DecodeReader(meshFile, matFile)
+ if err != nil {
+ return err
+ }
+
+ uniqueVertices := make(map[int]uint32)
+
+ for _, decodedObj := range decoder.Objects {
+ for _, face := range decodedObj.Faces {
+ // We need to triangularize faces
+ for i := 2; i < len(face.Vertices); i++ {
+ app.addVertex(decoder, uniqueVertices, face, 0)
+ app.addVertex(decoder, uniqueVertices, face, i-1)
+ app.addVertex(decoder, uniqueVertices, face, i)
+ }
+ }
+ }
+
+ return nil
+}
+
func (app *HelloTriangleApplication) createVertexBuffer() error {
var err error
- bufferSize := binary.Size(vertices)
+ bufferSize := binary.Size(app.vertices)
stagingBuffer, stagingBufferMemory, err := app.createBuffer(bufferSize, core1_0.BufferUsageTransferSrc, core1_0.MemoryPropertyHostVisible|core1_0.MemoryPropertyHostCoherent)
if stagingBuffer != nil {
@@ -1406,7 +1455,7 @@ func (app *HelloTriangleApplication) createVertexBuffer() error {
return err
}
- err = writeData(stagingBufferMemory, 0, vertices)
+ err = writeData(stagingBufferMemory, 0, app.vertices)
if err != nil {
return err
}
@@ -1420,7 +1469,7 @@ func (app *HelloTriangleApplication) createVertexBuffer() error {
}
func (app *HelloTriangleApplication) createIndexBuffer() error {
- bufferSize := binary.Size(indices)
+ bufferSize := binary.Size(app.indices)
stagingBuffer, stagingBufferMemory, err := app.createBuffer(bufferSize, core1_0.BufferUsageTransferSrc, core1_0.MemoryPropertyHostVisible|core1_0.MemoryPropertyHostCoherent)
if stagingBuffer != nil {
@@ -1434,7 +1483,7 @@ func (app *HelloTriangleApplication) createIndexBuffer() error {
return err
}
- err = writeData(stagingBufferMemory, 0, indices)
+ err = writeData(stagingBufferMemory, 0, app.indices)
if err != nil {
return err
}
@@ -1677,11 +1726,11 @@ func (app *HelloTriangleApplication) createCommandBuffers() error {
buffer.CmdBindPipeline(core1_0.PipelineBindPointGraphics, app.graphicsPipeline)
buffer.CmdBindVertexBuffers(0, []core1_0.Buffer{app.vertexBuffer}, []int{0})
- buffer.CmdBindIndexBuffer(app.indexBuffer, 0, core1_0.IndexTypeUInt16)
+ buffer.CmdBindIndexBuffer(app.indexBuffer, 0, core1_0.IndexTypeUInt32)
buffer.CmdBindDescriptorSets(core1_0.PipelineBindPointGraphics, app.pipelineLayout, 0, []core1_0.DescriptorSet{
app.descriptorSets[bufferIdx],
}, nil)
- buffer.CmdDrawIndexed(len(indices), 1, 0, 0, 0)
+ buffer.CmdDrawIndexed(len(app.indices), 1, 0, 0, 0)
buffer.CmdEndRenderPass()
_, err = buffer.End()