/
basic.go
452 lines (371 loc) · 10.3 KB
/
basic.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
package main
import (
"fmt"
"runtime"
"./wrapper"
"./objects"
"github.com/go-gl/gl/all-core/gl"
"github.com/go-gl/glfw/v3.1/glfw"
"github.com/go-gl/mathgl/mgl32"
)
const windowWidth = 1024
const windowHeight = 768
const windowFPS = 60
/* Define buffer object indices */
var positionBufferObject, colourObject, normalsBufferObject uint32
var shaderProgram uint32 /* Identifier for the shader prgoram */
var vertexArrayObject uint32 /* Vertex array (Containor) object. This is the index of the VAO that will be the container for
our buffer objects */
var colourmode objects.ColorMode /* Index of a uniform to switch the colour mode in the vertex shader
I've included this to show you how to pass in an unsigned integer into
your vertex shader. */
/* Position and view globals */
var angle_x, angle_inc_x, x, scale, z, y float32
var angle_y, angle_inc_y, angle_z, angle_inc_z float32
var aspect_ratio float32 // Aspect ratio of the window defined in the reshape callback
// Uniforms
var modelUniform, projectionUniform, viewUniform int32
var colourmodeUniform int32
// Sphere
var sphere *objects.Sphere
var cube *objects.Cube
// Define vertices for a cube in 12 triangles
var vertexPositions = []float32{
-0.25, 0.25, -0.25,
-0.25, -0.25, -0.25,
0.25, -0.25, -0.25,
0.25, -0.25, -0.25,
0.25, 0.25, -0.25,
-0.25, 0.25, -0.25,
0.25, -0.25, -0.25,
0.25, -0.25, 0.25,
0.25, 0.25, -0.25,
0.25, -0.25, 0.25,
0.25, 0.25, 0.25,
0.25, 0.25, -0.25,
0.25, -0.25, 0.25,
-0.25, -0.25, 0.25,
0.25, 0.25, 0.25,
-0.25, -0.25, 0.25,
-0.25, 0.25, 0.25,
0.25, 0.25, 0.25,
-0.25, -0.25, 0.25,
-0.25, -0.25, -0.25,
-0.25, 0.25, 0.25,
-0.25, -0.25, -0.25,
-0.25, 0.25, -0.25,
-0.25, 0.25, 0.25,
-0.25, -0.25, 0.25,
0.25, -0.25, 0.25,
0.25, -0.25, -0.25,
0.25, -0.25, -0.25,
-0.25, -0.25, -0.25,
-0.25, -0.25, 0.25,
-0.25, 0.25, -0.25,
0.25, 0.25, -0.25,
0.25, 0.25, 0.25,
0.25, 0.25, 0.25,
-0.25, 0.25, 0.25,
-0.25, 0.25, -0.25,
}
// Define an array of colours
var vertexColours = []float32{
0.0, 0.0, 1.0, 1.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, 1, 0,
0, 1, 0,
0, 1, 0,
0, 1, 0,
}
/////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////// Initialization ///////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
// This function is called by go as soon as this class is opened
func init() {
// Locks the Execution in the main Thread as OpenGL is not thread Safe
runtime.LockOSThread()
}
// Entry point of program
func main() {
// Creates the Window Wrapper
glw := wrapper.NewWrapper(windowWidth, windowHeight, "Lab 3: Lights")
glw.SetFPS(windowFPS)
// Creates the Window
glw.CreateWindow()
// Sets the Event Callbacks
glw.SetRenderCallback(drawLoop)
glw.SetKeyCallBack(keyCallback)
glw.SetReshapeCallback(reshape)
// Initializes the App
InitApp(glw)
// Starts the Rendering Loop
glw.StartLoop()
// Sets the Viewport (Important !!, this has to run after the loop!!)
defer gl.Viewport(0, 0, windowWidth, windowHeight)
}
//
// Init App
// This function initializes the variables and sets up the environment.
//
// @param wrapper (*wrapper.Glw) the window wrapper
//
func InitApp(glw *wrapper.Glw) {
/* Set the object transformation controls to their initial values */
x = 0.05;
y = 0;
z = 0;
angle_x = 0
angle_y = 0
angle_z = 0
angle_inc_x = 0
angle_inc_y = 0
angle_inc_z = 0
scale = 1.0;
aspect_ratio = 1.3333
colourmode = objects.COLOR_SOLID
var numLats uint32 = 20 // Number of latitudes in our sphere
var numLongs uint32 = 20 // Number of longitudes in our sphere
// Generate index (name) for one vertex array object
gl.GenVertexArrays(1, &vertexArrayObject);
// Create the vertex array object and make it current
gl.BindVertexArray(vertexArrayObject);
// Create the Cube Object
cube = objects.NewCube(&vertexPositions, &vertexColours, &normals)
cube.MakeVBO()
// create the sphere object
sphere = objects.NewSphere(numLats, numLongs);
sphere.MakeSphereVBO()
// Creates the Shader Program
var err error; shaderProgram, err = wrapper.LoadShader("./shaders/basic.vert", "./shaders/basic.frag")
// If there is any error loading the shaders, it panics
if err != nil {
panic(err)
}
// Define uniforms to send to vertex shader
modelUniform = gl.GetUniformLocation(shaderProgram, gl.Str("model\x00"));
colourmodeUniform = gl.GetUniformLocation(shaderProgram, gl.Str("colourmode\x00"));
viewUniform = gl.GetUniformLocation(shaderProgram, gl.Str("view\x00"));
projectionUniform = gl.GetUniformLocation(shaderProgram, gl.Str("projection\x00"));
}
/////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////// Callbacks /////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////
//
// Draw Loop Function
// This function gets called on every update.
//
func drawLoop(glw *wrapper.Glw) {
// Sets the Clear Color (Background Color)
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
// Clears the Window
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// Enables Depth
gl.Enable(gl.DEPTH_TEST)
// Sets the Shader program to Use
gl.UseProgram(shaderProgram)
// Define the model transformations for the cube
cube.ResetModel()
cube.Translate(x + 0.5, y, z)
cube.Scale(scale, scale, scale) //scale equally in all axis
cube.Rotate(-angle_x, mgl32.Vec3{1, 0, 0}) //rotating in clockwise direction around x-axis
cube.Rotate(-angle_y, mgl32.Vec3{0, 1, 0}) //rotating in clockwise direction around y-axis
cube.Rotate(-angle_z, mgl32.Vec3{0, 0, 1}) //rotating in clockwise direction around z-axis
// Define the model transformations for our sphere
sphere.ResetModel()
sphere.Translate(-x - 0.5, 0, 0)
sphere.Scale(scale / 3.0, scale / 3.0, scale / 3.0) //scale equally in all axis
sphere.Rotate(-angle_x, mgl32.Vec3{1, 0, 0}) //rotating in clockwise direction around x-axis
sphere.Rotate(-angle_y, mgl32.Vec3{0, 1, 0}) //rotating in clockwise direction around y-axis
sphere.Rotate(-angle_z, mgl32.Vec3{0, 0, 1}) //rotating in clockwise direction around z-axis
// Projection matrix : 45° Field of View, 4:3 ratio, display range : 0.1 unit <-> 100 units
var Projection mgl32.Mat4 = mgl32.Perspective(30.0, aspect_ratio, 0.1, 100.0)
// Camera matrix
var View mgl32.Mat4 = mgl32.LookAtV(
mgl32.Vec3{0, 0, 4}, // Camera is at (0,0,4), in World Space
mgl32.Vec3{0, 0, 0}, // and looks at the origin
mgl32.Vec3{0, 1, 0}, // Head is up (set to 0,-1,0 to look upside-down)
);
// Send our uniforms variables to the currently bound shader,
gl.Uniform1ui(colourmodeUniform, uint32(colourmode))
gl.UniformMatrix4fv(viewUniform, 1, false, &View[0])
gl.UniformMatrix4fv(projectionUniform, 1, false, &Projection[0])
// Draws the Cube
gl.UniformMatrix4fv(modelUniform, 1, false, &cube.Model[0])
cube.Draw()
// Draw our sphere
gl.UniformMatrix4fv(modelUniform, 1, false, &sphere.Model[0])
sphere.DrawSphere()
gl.DisableVertexAttribArray(0);
gl.UseProgram(0);
/* Modify our animation variables */
angle_x += angle_inc_x;
angle_y += angle_inc_y;
angle_z += angle_inc_z;
}
//
// key Callback
// This function gets called when a key is pressed
//
// @param window (*glfw.Window) a pointer to the window
// @param key (glfw.Key) the pressed key
// @param scancode (int) the scancode
// @param action (glfw.Action) the state of the key
// @param mods (glfw.ModifierKey) the pressed modified keys.
//
func keyCallback(window *glfw.Window, key glfw.Key, scancode int, action glfw.Action, mods glfw.ModifierKey) {
// React only if the key was just pressed
if action != glfw.Press {
return;
}
switch key {
// If the Key Excape is pressed, it closes the App
case glfw.KeyEscape:
if action == glfw.Press {
window.SetShouldClose(true)
}
break
case glfw.KeyQ:
angle_inc_x += 0.05
break
case glfw.KeyW:
angle_inc_x -= 0.05
break
case glfw.KeyE:
angle_inc_y += 0.05
break
case glfw.KeyR:
angle_inc_y -= 0.05
break
case glfw.KeyT:
angle_inc_z -= 0.05
break
case glfw.KeyY:
angle_inc_z += 0.05
break
case glfw.KeyA:
scale += 0.02
break
case glfw.KeyS:
scale -= 0.02
break
case glfw.KeyZ:
x -= 0.05
break
case glfw.KeyX:
x += 0.05
break
case glfw.KeyC:
y -= 0.05
break
case glfw.KeyV:
y += 0.05
break
case glfw.KeyB:
z -= 0.05
break
case glfw.KeyN:
z += 0.05
break
case glfw.KeyM:
if colourmode == 1 {
colourmode = 0
} else {
colourmode = 1
}
fmt.Printf("Colour Mode: %s \n", colourmode)
break
// Cycle between drawing vertices, mesh and filled polygons
case glfw.KeyK:
sphere.DrawMode ++;
if sphere.DrawMode > 2 {
sphere.DrawMode = 0
}
break
case glfw.KeyL:
cube.DrawMode ++;
if cube.DrawMode > 2 {
cube.DrawMode = 0
}
}
}
//
// Reshape
// This gets called when the window changes its size
//
// @param window (*glfw.Window) a pointer to the window
// @param width (int) the width of the window
// @param height (int) the height of the window
//
func reshape(window *glfw.Window, width, height int) {
gl.Viewport(0, 0, int32(width), int32(height));
aspect_ratio = (float32(width) / 640.0 * 4.0) / (float32(height) / 480.0 * 3.0);
}