/
main.go
333 lines (310 loc) · 9.88 KB
/
main.go
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
// Demo of loading and displaying various meshes.
package main
import (
"flag"
"log"
"math"
"time"
"image/color"
"fmt"
"github.com/go-gl/mathgl/mgl32"
"github.com/goxjs/gl"
"github.com/goxjs/glfw"
"github.com/omustardo/gome"
"github.com/omustardo/gome/asset"
"github.com/omustardo/gome/camera"
"github.com/omustardo/gome/camera/zoom"
"github.com/omustardo/gome/core/entity"
"github.com/omustardo/gome/input/keyboard"
"github.com/omustardo/gome/input/mouse"
"github.com/omustardo/gome/model"
"github.com/omustardo/gome/model/mesh"
"github.com/omustardo/gome/shader"
"github.com/omustardo/gome/util"
"github.com/omustardo/gome/util/fps"
"github.com/omustardo/gome/view"
)
var (
windowWidth = flag.Int("window_width", 1000, "initial window width")
windowHeight = flag.Int("window_height", 1000, "initial window height")
frameRate = flag.Duration("framerate", time.Second/60, `Cap on framerate. Provide with units, like "16.66ms"`)
// Explicitly listing the base dir is a hack. It's needed because `go run` produces a binary in a tmp folder so we can't
// use relative asset paths. More explanation in omustardo\gome\asset\asset.go
baseDir = flag.String("base_dir", `C:\workspace\Go\src\github.com\omustardo\gome\demos\meshes`, "All file paths should be specified relative to this root.")
)
func main() {
flag.Parse()
terminate := gome.Initialize("Mesh Showcase", *windowWidth, *windowHeight, *baseDir)
defer terminate()
shader.Model.SetAmbientLight(&color.NRGBA{60, 60, 60, 0}) // 3D objects don't look 3D in the default max lighting, so tone it down.
// Load meshes.
// OBJ
cubeMesh, err := asset.LoadOBJ("assets/cube.obj", asset.OBJOpts{})
cubeMesh.Color = &color.NRGBA{255, 25, 75, 255}
if err != nil {
log.Fatal(err)
}
capsuleMesh, err := asset.LoadOBJ("assets/capsule/capsule.obj", asset.OBJOpts{Normalize: true, Center: &mgl32.Vec3{0.5, 0.5, 0.5}})
if err != nil {
log.Fatal(err)
}
capsuleTexture, err := asset.LoadTexture("assets/capsule/capsule0.jpg")
if err != nil {
log.Fatal(err)
}
capsuleMesh.SetTexture(capsuleTexture)
shipMesh, err := asset.LoadOBJ("assets/ship/ship.obj", asset.OBJOpts{Normalize: true, Center: &mgl32.Vec3{0.5, 0.5, 0.5}})
if err != nil {
log.Fatal(err)
}
shipTexture, err := asset.LoadTexture("assets/ship/ship.jpg")
if err != nil {
log.Fatal(err)
}
shipMesh.SetTexture(shipTexture)
// DAE
vehicleMesh, err := asset.LoadDAE("assets/vehicle/vehicle0.dae")
if err != nil {
log.Fatal(err)
}
duckMesh, err := asset.LoadDAE("assets/duck/duck.dae")
if err != nil {
log.Fatal(err)
}
duckTexture, err := asset.LoadTexture("assets/duck/duck.png")
if err != nil {
log.Fatal(err)
}
duckMesh.SetTexture(duckTexture)
fmt.Println("Done loading assets")
// Create models (meshes in world space)
models := []*model.Model{
// Sphere
{
Tag: "Generated Sphere. Detail 0",
Mesh: mesh.NewSphere(0, &color.NRGBA{80, 50, 100, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
{
Tag: "Generated Sphere. Detail 1",
Mesh: mesh.NewSphere(1, &color.NRGBA{80, 50, 150, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
{
Tag: "Generated Sphere. Detail 2",
Mesh: mesh.NewSphere(2, &color.NRGBA{80, 60, 200, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
{
Tag: "Generated Sphere. Detail 3",
Mesh: mesh.NewSphere(3, &color.NRGBA{100, 80, 240, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
{
Tag: "Generated Sphere. Detail 4",
Mesh: mesh.NewSphere(4, &color.NRGBA{120, 100, 255, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
{
Tag: "Generated Icosahedron",
Mesh: mesh.NewIcosahedron(&color.NRGBA{80, 50, 100, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
{
Tag: "Subdivided Icosahedron - 1",
Mesh: mesh.NewSubdividedIcosahedron(1, &color.NRGBA{111, 20, 30, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
{
Tag: "Subdivided Icosahedron - 2",
Mesh: mesh.NewSubdividedIcosahedron(2, &color.NRGBA{165, 20, 30, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
// Cube
{
Tag: "Built in Mesh", // Tag is *only* for human readable output/debugging.
Mesh: mesh.NewCube(cubeMesh.Color, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
// Cube from file
{
Tag: "OBJ Mesh",
Mesh: cubeMesh,
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
// Rect
{
Tag: "Built in Mesh",
Mesh: mesh.NewRect(&color.NRGBA{80, 50, 100, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
// Rect outline
{
Mesh: mesh.NewRectOutline(&color.NRGBA{255, 25, 75, 255}),
Entity: entity.Entity{
Position: mgl32.Vec3{},
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 0},
},
},
// Circle
{
Tag: "Built in Mesh",
Mesh: mesh.NewCircle(&color.NRGBA{200, 50, 100, 255}, gl.Texture{}),
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
// Textured OBJ mesh
{
Tag: "OBJ Textured Mesh",
Mesh: shipMesh,
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
// DAE mesh
{
Tag: "DAE Mesh",
Mesh: vehicleMesh,
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
// Ideally the scale of all provided meshes fits them exactly into a unit cube, so scale is easy to work with.
// In this case the vehicle model is already reasonably large, so don't scale it as much as other models.
Scale: mgl32.Vec3{10, 10, 10},
},
},
{
Tag: "DAE Mesh",
Mesh: duckMesh,
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
// Ideally the scale of all provided meshes fits them exactly into a unit cube, so scale is easy to work with.
// In this case the vehicle model is already reasonably large, so don't scale it as much as other models.
Scale: mgl32.Vec3{0.5, 0.5, 0.5},
},
},
// Capsule
{
Tag: "OBJ Textured Mesh",
Mesh: capsuleMesh,
Entity: entity.Entity{
Rotation: mgl32.QuatIdent(),
Scale: mgl32.Vec3{100, 100, 100},
},
},
}
// Adjust model positions so they're spaced nicely by making them into roughly a square layout that's centered at the origin.
dimensions := float32(math.Ceil(math.Sqrt(float64(len(models)))))
cellSize := float32(200)
offset := -dimensions * cellSize / 2 // by default everything is drawn in quadrant 1, so shift down and left
if int(dimensions)%2 == 0 {
offset += cellSize / 2 // if even number of items, offset so they fit nicely around the axes
}
fmt.Printf("%d models, arranging into %v by %v grid\n", len(models), dimensions, dimensions)
for i := float32(0); i < dimensions; i++ {
for j := float32(0); j < dimensions && (j+i*dimensions < float32(len(models))); j++ {
index := int(i*dimensions + j)
models[index].Position = mgl32.Vec3{j * cellSize, float32(i * cellSize), 0}.Add(mgl32.Vec3{offset, offset, 0})
}
}
// Player is an empty model. It has no mesh so it can't be rendered, but it can still exist in the world.
player := &model.Model{}
player.Position[0] = 0
cam := camera.NewTargetCamera(player, mgl32.Vec3{0, 0, 1000})
cam.Zoomer = zoom.NewScrollZoom(0.1, 3,
func() float32 {
return mouse.Handler.Scroll().Y()
},
)
rotationPerSecond := mgl32.AnglesToQuat(float32(math.Pi/4), float32(math.Pi/4), float32(math.Pi/4), mgl32.XYZ)
ticker := time.NewTicker(*frameRate)
for !view.Window.ShouldClose() {
glfw.PollEvents() // Reads window events, like keyboard and mouse input.
fps.Handler.Update()
keyboard.Handler.Update()
mouse.Handler.Update()
ApplyInputs(player)
// Update the rotation.
for i := range models {
models[i].ModifyRotationLocalQ(util.ScaleQuatRotation(rotationPerSecond, fps.Handler.DeltaTimeSeconds()))
}
// Set up Model-View-Projection Matrix and send it to the shader program.
mvMatrix := cam.ModelView()
w, h := view.Window.GetSize()
pMatrix := cam.ProjectionPerspective(float32(w), float32(h))
shader.Model.SetMVPMatrix(pMatrix, mvMatrix)
cam.Update(fps.Handler.DeltaTime())
// Clear screen, then Draw everything
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
model.RenderXYZAxes()
for _, m := range models {
m.Render()
}
// Swaps the buffer that was drawn on to be visible. The visible buffer becomes the one that gets drawn on until it's swapped again.
view.Window.SwapBuffers()
<-ticker.C // wait up to the framerate cap.
}
}
func ApplyInputs(target *model.Model) {
var move mgl32.Vec2
if keyboard.Handler.IsKeyDown(glfw.KeyA, glfw.KeyLeft) {
move[0] += -1
}
if keyboard.Handler.IsKeyDown(glfw.KeyD, glfw.KeyRight) {
move[0] += 1
}
if keyboard.Handler.IsKeyDown(glfw.KeyW, glfw.KeyUp) {
move[1] += 1
}
if keyboard.Handler.IsKeyDown(glfw.KeyS, glfw.KeyDown) {
move[1] += -1
}
moveSpeed := float32(500)
move = move.Normalize().Mul(moveSpeed * fps.Handler.DeltaTimeSeconds())
target.ModifyPosition(move[0], move[1], 0)
w, h := view.Window.GetSize()
if mouse.Handler.LeftPressed() {
move = mgl32.Vec2{
mouse.Handler.Position().X() - float32(w)/2,
-(mouse.Handler.Position().Y() - float32(h)/2),
}
move = move.Normalize().Mul(moveSpeed * fps.Handler.DeltaTimeSeconds())
target.ModifyPosition(move[0], move[1], 0)
}
}