@@ -11,24 +11,28 @@ import (
11
11
type HelloCoordinates struct {
12
12
sections.BaseSketch
13
13
shader glutils.Shader
14
- va glutils.VertexArray
14
+ va glutils.VertexArray
15
15
texture1 , texture2 uint32
16
16
transform mgl32.Mat4
17
17
cubePositions []mgl32.Mat4
18
18
rotationAxis mgl32.Vec3
19
19
}
20
20
21
- func (hc * HelloCoordinates ) InitGL () error {
22
- hc .Name = "6. Coordinate Systems"
21
+ func (hc * HelloCoordinates ) GetHeader () string {
22
+ return "6. Coordinate Systems"
23
+ }
23
24
25
+ func (hc * HelloCoordinates ) createShader () error {
24
26
var err error
25
27
hc .shader , err = glutils .NewShader (
26
28
"_assets/getting_started/6.coordinates/coordinate.vs" ,
27
29
"_assets/getting_started/6.coordinates/coordinate.frag" , "" )
28
30
if err != nil {
29
31
return err
30
32
}
31
-
33
+ return nil
34
+ }
35
+ func (hc * HelloCoordinates ) createBuffers () error {
32
36
vertices := []float32 {
33
37
- 0.5 , - 0.5 , - 0.5 , 0.0 , 0.0 ,
34
38
0.5 , - 0.5 , - 0.5 , 1.0 , 0.0 ,
@@ -72,18 +76,22 @@ func (hc *HelloCoordinates) InitGL() error {
72
76
- 0.5 , 0.5 , 0.5 , 0.0 , 0.0 ,
73
77
- 0.5 , 0.5 , - 0.5 , 0.0 , 1.0 ,
74
78
}
75
- attr := make (glutils.AttributesMap )
76
- attr [hc .shader .Attributes ["position" ]] = [2 ]int {0 , 3 }
77
- attr [hc .shader .Attributes ["texCoord" ]] = [2 ]int {3 , 2 }
79
+
80
+ attr := glutils .NewAttributesMap ()
81
+ attr .Add (hc .shader .Attributes ["position" ], 3 , 0 )
82
+ attr .Add (hc .shader .Attributes ["texCoord" ], 2 , 3 )
78
83
79
84
hc .va = glutils.VertexArray {
80
- Data : vertices ,
81
- Stride : 5 ,
82
- DrawMode : gl .STATIC_DRAW ,
85
+ Data : vertices ,
86
+ Stride : 5 ,
87
+ DrawMode : gl .STATIC_DRAW ,
83
88
Normalized : false ,
84
89
Attributes : attr ,
85
90
}
91
+ hc .va .Setup ()
92
+
86
93
hc .rotationAxis = mgl32.Vec3 {1.0 , 0.3 , 0.5 }.Normalize ()
94
+
87
95
hc .cubePositions = []mgl32.Mat4 {
88
96
mgl32 .Translate3D (0.0 , 0.0 , 0.0 ),
89
97
mgl32 .Translate3D (2.0 , 5.0 , - 15.0 ),
@@ -96,8 +104,10 @@ func (hc *HelloCoordinates) InitGL() error {
96
104
mgl32 .Translate3D (1.5 , 0.2 , - 1.5 ),
97
105
mgl32 .Translate3D (- 1.3 , 1.0 , - 1.5 ),
98
106
}
107
+ return nil
108
+ }
99
109
100
-
110
+ func ( hc * HelloCoordinates ) createTextures () error {
101
111
// Texture 1
102
112
if tex , err := glutils .NewTexture (gl .REPEAT , gl .REPEAT , gl .LINEAR , gl .LINEAR , "_assets/images/container.png" ); err != nil {
103
113
return err
@@ -111,56 +121,70 @@ func (hc *HelloCoordinates) InitGL() error {
111
121
} else {
112
122
hc .texture2 = tex
113
123
}
124
+ return nil
125
+ }
114
126
127
+ func (hc * HelloCoordinates ) InitGL () error {
128
+ if err := hc .createShader (); err != nil {
129
+ return err
130
+ }
131
+ if err := hc .createBuffers (); err != nil {
132
+ return err
133
+ }
134
+ if err := hc .createTextures (); err != nil {
135
+ return err
136
+ }
115
137
return nil
116
138
}
117
139
118
- func (hc * HelloCoordinates ) Draw () {
140
+ func (hc * HelloCoordinates) clear () {
119
141
gl .Clear (gl .COLOR_BUFFER_BIT | gl .DEPTH_BUFFER_BIT )
120
142
gl .ClearColor (hc .Color32 .R , hc .Color32 .G , hc .Color32 .B , hc .Color32 .A )
121
-
143
+ }
144
+ func (hc * HelloCoordinates ) setTextures () {
122
145
// Bind Textures using texture units
123
146
gl .ActiveTexture (gl .TEXTURE0 )
124
147
gl .BindTexture (gl .TEXTURE_2D , hc .texture1 )
125
- loc1 := gl .GetUniformLocation (hc .shader , gl .Str ("ourTexture1\x00 " ))
126
- gl .Uniform1i (loc1 , 0 )
148
+ gl .Uniform1i (hc .shader .Uniforms ["ourTexture1" ], 0 )
127
149
128
150
gl .ActiveTexture (gl .TEXTURE1 )
129
151
gl .BindTexture (gl .TEXTURE_2D , hc .texture2 )
130
- loc2 := gl .GetUniformLocation (hc .shader , gl .Str ("ourTexture2\x00 " ))
131
- gl .Uniform1i (loc2 , 1 )
132
-
133
- // Activate shader
134
- gl .UseProgram (hc .shader )
135
-
152
+ gl .Uniform1i (hc .shader .Uniforms ["ourTexture2" ], 1 )
153
+ }
154
+ func (hc * HelloCoordinates ) setTransformations () {
136
155
// Create transformations
137
156
view := mgl32 .Translate3D (0.0 , 0.0 , - 3.0 )
138
157
projection := mgl32 .Perspective (45.0 , sections .RATIO , 0.1 , 100.0 )
139
- // Get their uniform location
140
- modelLoc := gl .GetUniformLocation (hc .shader , gl .Str ("model\x00 " ))
141
- viewLoc := gl .GetUniformLocation (hc .shader , gl .Str ("view\x00 " ))
142
- projLoc := gl .GetUniformLocation (hc .shader , gl .Str ("projection\x00 " ))
143
158
// Pass the matrices to the shader
144
- gl .UniformMatrix4fv (viewLoc , 1 , false , & view [0 ])
159
+ gl .UniformMatrix4fv (hc . shader . Uniforms [ "view" ] , 1 , false , & view [0 ])
145
160
// Note: currently we set the projection matrix each frame,
146
161
// but since the projection matrix rarely changes it's often best practice to set it outside the main loop only once.
147
- gl .UniformMatrix4fv (projLoc , 1 , false , & projection [0 ])
162
+ gl .UniformMatrix4fv (hc .shader .Uniforms ["projection" ], 1 , false , & projection [0 ])
163
+ }
148
164
165
+ func (hc * HelloCoordinates ) renderVertexArray () {
149
166
// Draw container
150
- gl .BindVertexArray (hc .vao )
151
-
167
+ gl .BindVertexArray (hc .va .Vao )
152
168
for i := 0 ; i < 10 ; i ++ {
153
169
// Calculate the model matrix for each object and pass it to shader before drawing
154
170
model := hc .cubePositions [i ]
155
171
156
172
angle := float32 (glfw .GetTime ()) * float32 (i + 1 )
157
173
158
174
model = model .Mul4 (mgl32 .HomogRotate3D (angle , hc .rotationAxis ))
159
- gl .UniformMatrix4fv (modelLoc , 1 , false , & model [0 ])
175
+ gl .UniformMatrix4fv (hc . shader . Uniforms [ "model" ] , 1 , false , & model [0 ])
160
176
gl .DrawArrays (gl .TRIANGLES , 0 , 36 )
161
177
}
162
178
gl .BindVertexArray (0 )
163
179
}
180
+ func (hc * HelloCoordinates ) Draw () {
181
+ hc .clear ()
182
+ // Activate shader
183
+ gl .UseProgram (hc .shader .Program )
184
+ hc .setTextures ()
185
+ hc .setTransformations ()
186
+ hc .renderVertexArray ()
187
+ }
164
188
165
189
func (hc * HelloCoordinates ) Close () {
166
190
hc .shader .Delete ()
0 commit comments