-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlightManager.go
116 lines (101 loc) · 2.81 KB
/
lightManager.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
package in3d
import (
"errors"
"fmt"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/seemywingz/go-toolbox"
)
// LightManager :
type LightManager struct {
Lights []*Light
}
// Light : struct to hold light data
type Light struct {
Radius float32
Ambient []float32
Diffuse []float32
Specular []float32
SceneData
Draw bool
DrawnObject *DrawnObject
}
// NewLightManager :
func NewLightManager() *LightManager {
lightManager = &LightManager{
[]*Light{},
}
return lightManager
}
// NewLight :
func NewLight() *Light {
return BuildLight(
NewPosition(0, 0, 0), // position
60, // radius
[]float32{1.0, 1.0, 1.0}, // ambiant intensity
[]float32{1.0, 1.0, 1.0}, // diffuse intensity
[]float32{1.0, 1.0, 1.0}, // specular intensity
false, // draw
)
}
// NewColorLight :
func NewColorLight(amb, dif, spec []float32) *Light {
return BuildLight(
NewPosition(0, 0, 0), // position
50, // radius
amb, // ambiant intensity
dif, // diffuse intensity
spec, // specular intensity
false, // draw
)
}
// BuildLight :
func BuildLight(position Position, radius float32, amb, dif, spec []float32, draw bool) *Light {
n := len(lightManager.Lights)
if n > MaxLights {
toolbox.EoE(errors.New("max lights reached "+fmt.Sprint(MaxLights)), "Error adding New Light:")
}
fmt.Println("Adding Light:", n)
drawnObject := NewPointsObject(position, Cube, NoTexture, dif, Shader["color"])
drawnObject.Scale = 0.05
light := &Light{
radius,
amb,
dif,
spec,
SceneData{},
draw,
drawnObject,
}
light.Position = position
lightManager.Lights = append(lightManager.Lights, light)
return light
}
// Update :
func (manager *LightManager) Update() {
for n, light := range manager.Lights {
if light.SceneLogic != nil {
light.SceneLogic(&light.SceneData)
}
if light.Draw {
light.DrawnObject.Position = light.Position
for _, mg := range light.DrawnObject.Mesh.MaterialGroups {
mg.Material.Diffuse = light.Diffuse
}
light.DrawnObject.Draw()
}
for _, program := range Shader {
uniform := fmt.Sprintf("Light[%v]", n)
LRadID := gl.GetUniformLocation(program, gl.Str(uniform+".lightRad\x00"))
LPosID := gl.GetUniformLocation(program, gl.Str(uniform+".lightPos\x00"))
AmbID := gl.GetUniformLocation(program, gl.Str(uniform+".Iamb\x00"))
DifID := gl.GetUniformLocation(program, gl.Str(uniform+".Idif\x00"))
SpecID := gl.GetUniformLocation(program, gl.Str(uniform+".Ispec\x00"))
gl.UseProgram(program)
gl.Uniform1f(LRadID, light.Radius)
gl.Uniform3fv(LPosID, 1, &[]float32{light.X, light.Y, light.Z}[0])
gl.Uniform3fv(AmbID, 1, &light.Ambient[0])
gl.Uniform3fv(DifID, 1, &light.Diffuse[0])
gl.Uniform3fv(SpecID, 1, &light.Specular[0])
}
}
}