-
Notifications
You must be signed in to change notification settings - Fork 0
/
skybox.go
88 lines (83 loc) · 2.08 KB
/
skybox.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
package sge
import (
"math"
)
import (
"github.com/klkblake/s3dm"
)
type Skybox struct {
*BasicLeaf
CubeMap *Texture
Shader *Program
cachedMatrix s3dm.Mat4
mvpMatrix *Uniform
textureSampler *Uniform
mesh *Mesh
}
func NewSkybox(cubeMap *Texture, shader *Program, far float64) *Skybox {
skybox := new(Skybox)
skybox.BasicLeaf = NewBasicLeaf()
skybox.Link(skybox)
scale := far / math.Sqrt(3)
skybox.Xform().Scale = s3dm.V3{scale, scale, scale}
skybox.cachedMatrix = skybox.Xform().Matrix(s3dm.Position{})
skybox.CubeMap = cubeMap
skybox.Shader = shader
skybox.mvpMatrix = shader.Uniform("mvpMatrix")
skybox.textureSampler = shader.Uniform("textureSampler")
type skyboxVertex [2][3]float32
verticies := []skyboxVertex{
// Positive X
{{1, 1, -1}, {1, 1, -1}},
{{1, -1, -1}, {1, -1, -1}},
{{1, -1, 1}, {1, -1, 1}},
{{1, 1, 1}, {1, 1, 1}},
// Negative X
{{-1, 1, 1}, {-1, 1, 1}},
{{-1, -1, 1}, {-1, -1, 1}},
{{-1, -1, -1}, {-1, -1, -1}},
{{-1, 1, -1}, {-1, 1, -1}},
// Positive Y
{{-1, 1, 1}, {-1, 1, 1}},
{{-1, 1, -1}, {-1, 1, -1}},
{{1, 1, -1}, {1, 1, -1}},
{{1, 1, 1}, {1, 1, 1}},
// Negative Y
{{-1, -1, -1}, {-1, -1, -1}},
{{-1, -1, 1}, {-1, -1, 1}},
{{1, -1, 1}, {1, -1, 1}},
{{1, -1, -1}, {1, -1, -1}},
// Positive Z
{{1, 1, 1}, {1, 1, 1}},
{{1, -1, 1}, {1, -1, 1}},
{{-1, -1, 1}, {-1, -1, 1}},
{{-1, 1, 1}, {-1, 1, 1}},
// Negative Z
{{-1, 1, -1}, {-1, 1, -1}},
{{-1, -1, -1}, {-1, -1, -1}},
{{1, -1, -1}, {1, -1, -1}},
{{1, 1, -1}, {1, 1, -1}},
}
indicies := []uint32{
0, 1, 2, 0, 2, 3,
4, 5, 6, 4, 6, 7,
8, 9, 10, 8, 10, 11,
12, 13, 14, 12, 14, 15,
16, 17, 18, 16, 18, 19,
20, 21, 22, 20, 22, 23,
}
skybox.mesh = NewMesh(verticies, indicies)
return skybox
}
func (skybox *Skybox) Render(view *View, mvpMatrix s3dm.Mat4, pass int) {
if skybox.CubeMap != nil {
skybox.CubeMap.Bind(0)
}
skybox.Shader.Use()
matrix := mvpMatrix.Mul(skybox.cachedMatrix).RawMatrix32()
skybox.mvpMatrix.SetMatrix(matrix[:], 4)
if skybox.CubeMap != nil {
skybox.textureSampler.Set(0)
}
skybox.mesh.Render()
}