-
Notifications
You must be signed in to change notification settings - Fork 3
/
DrawnObject.go
136 lines (115 loc) · 3.51 KB
/
DrawnObject.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
package in3d
import (
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/mathgl/mgl32"
)
// DrawnObject : a struct to hold openGL object data
type DrawnObject struct {
Mesh *Mesh
MVPID int32
ModelMatrixID int32
NormalMatrixID int32
IambID int32
IdifID int32
IspecID int32
ShininessID int32
TextureID int32
NormalMapID int32
Scale float32
SceneData
}
// NewPointsObject :
func NewPointsObject(position Position, points []float32, texture uint32, color []float32, program uint32) *DrawnObject {
vao := MakeVAO(points, program)
mat := &Material{
"default",
color,
color,
color,
1,
texture,
NoTexture,
}
mg := make(map[string]*MaterialGroup)
mg["dfault"] = &MaterialGroup{
mat,
[]*Face{},
vao,
int32(len(points)),
}
mesh := &Mesh{mg}
return NewMeshObject(position, mesh, program)
}
// NewMeshObject : Create new DrawnObject
func NewMeshObject(position Position, mesh *Mesh, program uint32) *DrawnObject {
ModelMatrixID := gl.GetUniformLocation(program, gl.Str("MODEL\x00"))
NormalMatrixID := gl.GetUniformLocation(program, gl.Str("NormalMatrix\x00"))
MVPID := gl.GetUniformLocation(program, gl.Str("MVP\x00"))
uniform := "Material"
IambID := gl.GetUniformLocation(program, gl.Str(uniform+".Iamb\x00"))
IdifID := gl.GetUniformLocation(program, gl.Str(uniform+".Idif\x00"))
IspecID := gl.GetUniformLocation(program, gl.Str(uniform+".Ispec\x00"))
ShininessID := gl.GetUniformLocation(program, gl.Str(uniform+".shininess\x00"))
TextureID := gl.GetUniformLocation(program, gl.Str("TEXTURE\x00"))
NoramalMapID := gl.GetUniformLocation(program, gl.Str("NORMAL_MAP\x00"))
// println(TextureID, NoramalMapID)
d := &DrawnObject{
mesh,
MVPID,
ModelMatrixID,
NormalMatrixID,
IambID,
IdifID,
IspecID,
ShininessID,
TextureID,
NoramalMapID,
1,
SceneData{},
}
d.Position = position
d.Program = program
return d
}
func (d *DrawnObject) translateRotate() *mgl32.Mat4 {
model := mgl32.Translate3D(d.X, d.Y, d.Z).
Mul4(mgl32.Scale3D(d.Scale, d.Scale, d.Scale))
xrotMatrix := mgl32.HomogRotate3DX(mgl32.DegToRad(d.XRotation))
yrotMatrix := mgl32.HomogRotate3DY(mgl32.DegToRad(d.YRotation))
zrotMatrix := mgl32.HomogRotate3DZ(mgl32.DegToRad(d.ZRotation))
final := model.Mul4(xrotMatrix.Mul4(yrotMatrix.Mul4(zrotMatrix)))
return &final
}
// Draw : draw the object
func (d *DrawnObject) Draw() {
if d.SceneLogic != nil {
d.SceneLogic(&d.SceneData)
}
modelMatrix := d.translateRotate()
normalMatrix := modelMatrix.Inv().Transpose()
gl.UseProgram(d.Program)
gl.UniformMatrix4fv(d.MVPID, 1, false, &camera.MVP[0])
gl.UniformMatrix4fv(d.ModelMatrixID, 1, false, &modelMatrix[0])
gl.UniformMatrix4fv(d.NormalMatrixID, 1, false, &normalMatrix[0])
for _, m := range d.Mesh.MaterialGroups {
gl.UseProgram(d.Program)
gl.BindVertexArray(m.VAO)
// Material
gl.Uniform3fv(d.IambID, 1, &m.Material.Ambient[0])
gl.Uniform3fv(d.IspecID, 1, &m.Material.Specular[0])
gl.Uniform3fv(d.IdifID, 1, &m.Material.Diffuse[0])
gl.Uniform1f(d.ShininessID, m.Material.Shininess)
gl.Uniform1i(d.TextureID, 0)
gl.Uniform1i(d.NormalMapID, 1)
// Bind our diffuse texture in Texture Unit 0
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, m.Material.DiffuseTex)
gl.ActiveTexture(gl.TEXTURE1)
gl.BindTexture(gl.TEXTURE_2D, m.Material.NormalTex)
gl.DrawArrays(gl.TRIANGLES, 0, m.VertCount)
gl.ActiveTexture(gl.TEXTURE0)
gl.BindTexture(gl.TEXTURE_2D, 0)
gl.ActiveTexture(gl.TEXTURE1)
gl.BindTexture(gl.TEXTURE_2D, 0)
}
}