-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblock.go
164 lines (140 loc) · 4.8 KB
/
block.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
package main
import (
"github.com/Ferguzz/glam"
"github.com/go-gl/gl"
"math"
"math/rand"
"time"
)
// Blocks are what are drawn to the screen so they need to remeber where they are and what color they are etc.
type Block struct {
shape *Shape
position glam.Vec3
orientation int
reflect int
color Color
}
// Shapes contain all the information that a block needs to draw itself e.g. vertex arrays, element buffers etc.
type Shape struct {
vertices []gl.GLfloat
elements []gl.GLushort
vao gl.VertexArray
vbo gl.Buffer
elementBuffer gl.Buffer
numElements int
}
// This slice holds all the blocks that need to be drawn.
var blocks []Block
const numShapes = 4
var Shapes []Shape = make([]Shape, numShapes)
type Color []float32
func init() {
rand.Seed(time.Now().UnixNano())
}
func randomColor() Color {
var color Color
switch rand.Intn(4) {
case 0:
// Red.
color = Color{1, 0, 0}
case 1:
// Green.
color = Color{0, 1, 0}
case 2:
// Blue.
color = Color{0, 0, 1}
case 3:
// White.
color = Color{1, 1, 1}
}
return color
}
// Generate all the vaos on startup. I could do this only when they are first required, but this may delay the block generation.
func GenerateShapes() {
// Square
Shapes[0].vertices = []gl.GLfloat{-1, -1, 1, -1, -1, 1, 1, 1}
Shapes[0].elements = []gl.GLushort{0, 1, 2, 2, 3, 1}
// ___|
Shapes[1].vertices = []gl.GLfloat{-2, 0, -2, -1, 2, -1, 2, 0, 2, 1, 1, 1, 1, 0}
Shapes[1].elements = []gl.GLushort{0, 1, 2, 2, 3, 0, 3, 4, 5, 5, 6, 3}
// _|_
Shapes[2].vertices = []gl.GLfloat{-1.5, 0, -0.5, 0, -0.5, 1, 0.5, 1, 0.5, 0, 1.5, 0, 1.5, -1, -1.5, -1}
Shapes[2].elements = []gl.GLushort{1, 2, 3, 3, 4, 1, 0, 7, 6, 6, 0, 5}
// Snake
Shapes[3].vertices = []gl.GLfloat{-1.5, -1, -1.5, 0, -0.5, 0, -0.5, 1, 1.5, 1, 1.5, 0, 0.5, 0, 0.5, -1}
Shapes[3].elements = []gl.GLushort{0, 1, 6, 6, 7, 0, 2, 3, 4, 4, 5, 2}
// Now fill out the rest automatically.
// FIXME why doesn't using _, shape in this loop work ?
for i := range Shapes {
Shapes[i].vao = gl.GenVertexArray()
Shapes[i].vao.Bind()
Shapes[i].vbo = gl.GenBuffer()
Shapes[i].vbo.Bind(gl.ARRAY_BUFFER)
gl.BufferData(gl.ARRAY_BUFFER, len(Shapes[i].vertices)*4, Shapes[i].vertices, gl.STATIC_DRAW)
Shapes[i].elementBuffer = gl.GenBuffer()
Shapes[i].elementBuffer.Bind(gl.ELEMENT_ARRAY_BUFFER)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, len(Shapes[i].elements)*2, Shapes[i].elements, gl.STATIC_DRAW)
Shapes[i].numElements = len(Shapes[i].elements)
vertexAttribArray := shaderProgram.GetAttribLocation("position")
vertexAttribArray.AttribPointer(2, gl.FLOAT, false, 0, uintptr(0))
vertexAttribArray.EnableArray()
}
}
func CleanUpShapes() {
// Fixme does this actually work or is it broken in the same way as above ?
for _, shape := range Shapes {
shape.vao.Delete()
shape.vbo.Delete()
shape.elementBuffer.Delete()
}
}
func NewBlock() {
var block Block
block.shape = &Shapes[rand.Intn(numShapes)]
// Initialise a random X position.
// The blocks must snap to the grid. Since shapes are defined around their COM, the centres are not what should be
// snapped. How do I deal with this ?
block.position = glam.Vec3{float32((rand.Intn(width/gridSize))*gridSize) - width/2, 200, 0}
// Pick a random orientation.
block.orientation = rand.Intn(4)
block.reflect = rand.Intn(2)
// Finally, a random color.
block.color = randomColor()
blocks = append(blocks, block)
}
func (block *Block) Draw() {
block.shape.vao.Bind()
position := glam.Translation(block.position)
rotation := glam.Rotation(float32(block.orientation)*math.Pi/2.0, glam.Vec3{0, 0, 1})
var model glam.Mat4
model.Multiply(&rotation, &position)
shaderProgram.GetUniformLocation("model").UniformMatrix4fv(false, model)
shaderProgram.GetUniformLocation("inColor").Uniform3fv(1, block.color)
// shaderProgram.GetUniformLocation("reflect").Uniform1i(block.reflect)
gl.DrawElements(gl.TRIANGLES, block.shape.numElements, gl.UNSIGNED_SHORT, uintptr(0))
}
func (block *Block) Move(direction int) {
// Do collision detection in here.
switch direction {
case down:
if block.position.Y >= -(height/2)+40 {
block.position.Y -= gridSize
} else {
// We've hit the bottom. Generate a new block.
// Since block creation involves OpenGL related calls, it needs to be done in the main thread.
// Since this function can be called from other threads, we need to signal to the OpenGL thread to make a block.
generateNewBlock <- true
}
case left:
if block.position.X >= -width/2+40 {
block.position.X -= gridSize
}
case right:
if block.position.X <= width/2-40 {
block.position.X += gridSize
}
}
}
func (block *Block) Rotate() {
block.orientation += 1
}