-
Notifications
You must be signed in to change notification settings - Fork 0
/
window.go
269 lines (216 loc) · 6.08 KB
/
window.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
package p5go
import (
"math"
"github.com/go-gl/gl/v4.1-core/gl"
"github.com/go-gl/glfw/v3.3/glfw"
"github.com/go-gl/mathgl/mgl32"
)
// Windows struct
type Window struct {
window *glfw.Window
width int
height int
depth int
camera Camera
shape Shape
mouseX float64
mouseY float64
color mgl32.Mat3
}
// Canvas2D returns a Window with width and height.
//
// Parameters should be in pixel.
func Canvas2D(width, height int) *Window {
window, err := glfw.CreateWindow(width, height, "", nil, nil)
if err != nil {
panic(err)
}
standardColor := mgl32.Mat3{1.0, 1.0, 1.0}
return &Window{window: window, width: width, height: height, color: standardColor}
}
// Canvas3D returns a Window with width, height and depth.
//
// Parameters should be in pixel.
func Canvas3D(width, height, depth int) *Window {
window, err := glfw.CreateWindow(width, height, "", nil, nil)
if err != nil {
panic(err)
}
camera := centeredCamera(width, height)
shape := Shape{
width: width,
height: height,
depth: depth,
}
standardColor := mgl32.Mat3{1.0, 1.0, 1.0}
return &Window{window: window, width: width, height: height, depth: depth, camera: camera, shape: shape, color: standardColor}
}
// Returns Camera object with default values:
//
// position: {0, 0, 2}
// center: {0, 0, 0}
// up: {0, 1, 0}
// FOV: 45.0
// nearPlane: 0.1
// farPlane: 100.0
func (w *Window) Camera() *Camera {
return &w.camera
}
func (w *Window) Shape() Shape {
return w.shape
}
// Returns height of window
func (w *Window) Height() float32 {
return float32(w.height)
}
// Returns width of window
func (w *Window) Width() float32 {
return float32(w.width)
}
// Returns current x-axis position of mouse
func (w *Window) MouseX() float32 {
return float32(w.mouseX)
}
// Returns current y-axis position of mouse
func (w *Window) MouseY() float32 {
return float32(w.mouseY)
}
// Background change color of window.
//
// Parameters accepted are RGB values: 0-255
func (w *Window) Background(red, green, blue int) {
gl.Clear(gl.COLOR_BUFFER_BIT)
r_float := (1.0 / 255) * float32(red)
g_float := (1.0 / 255) * float32(green)
b_float := (1.0 / 255) * float32(blue)
gl.ClearColor(r_float, g_float, b_float, 1.0)
}
// Fill change color of shapes.
//
// Parameters accepted are RGB values: 0-255
func (w *Window) Fill(red, green, blue int) {
r_float := (1.0 / 255) * float32(red)
g_float := (1.0 / 255) * float32(green)
b_float := (1.0 / 255) * float32(blue)
w.color = mgl32.Mat3{r_float, g_float, b_float}
}
func (w *Window) Line(x1, y1, x2, y2 float32) {
x1f := fromWorldToLocalSpace(x1, w.width)
y1f := fromWorldToLocalSpace(y1, w.height)
x2f := fromWorldToLocalSpace(x2, w.width)
y2f := fromWorldToLocalSpace(y2, w.height)
line := []float32{
x1f, y1f,
x2f, y2f,
}
vbo := generatevbo(line)
vao := generate2Dvao(vbo)
gl.BindVertexArray(vao)
gl.DrawArrays(gl.LINES, 0, 2)
}
func (w *Window) Triangle(x1, y1, x2, y2, x3, y3 float32) {
x1f := fromWorldToLocalSpace(x1, w.width)
y1f := fromWorldToLocalSpace(y1, w.height)
x2f := fromWorldToLocalSpace(x2, w.width)
y2f := fromWorldToLocalSpace(y2, w.height)
x3f := fromWorldToLocalSpace(x3, w.width)
y3f := fromWorldToLocalSpace(y3, w.height)
triangle := []float32{
x1f, y1f,
x2f, y2f,
x3f, y3f,
}
vbo := generatevbo(triangle)
vao := generate2Dvao(vbo)
gl.BindVertexArray(vao)
gl.DrawArrays(gl.TRIANGLES, 0, 3)
}
func (w *Window) Quad(x1, y1, x2, y2, x3, y3, x4, y4 float32) {
x1f := fromWorldToLocalSpace(x1, w.width)
y1f := fromWorldToLocalSpace(y1, w.height)
x2f := fromWorldToLocalSpace(x2, w.width)
y2f := fromWorldToLocalSpace(y2, w.height)
x3f := fromWorldToLocalSpace(x3, w.width)
y3f := fromWorldToLocalSpace(y3, w.height)
x4f := fromWorldToLocalSpace(x4, w.width)
y4f := fromWorldToLocalSpace(y4, w.height)
quad := []float32{
x1f, y1f,
x2f, y2f,
x3f, y3f,
x4f, y4f,
}
vbo := generatevbo(quad)
vao := generate2Dvao(vbo)
gl.BindVertexArray(vao)
gl.DrawArrays(gl.QUADS, 0, 4)
}
func (w *Window) Rect(x1, y1, width, height float32) {
w.Triangle(x1, y1, x1+width, y1, x1, y1+height)
w.Triangle(x1+width, y1, x1, y1+height, x1+width, y1+height)
}
func (w *Window) Square(x1, y1, size float32) {
w.Rect(x1, y1, size, size)
}
func (w *Window) Circle(x1, y1, radius float32) {
w.Ellipse(x1, y1, radius, radius)
}
func (w *Window) Ellipse(x1, y1, width, height float32) {
triangles := 360
angle := float32(360 / triangles)
prevX := x1
prevY := y1
for i := 0; i <= triangles; i++ {
newX := float64(x1) + (float64(width) * math.Cos(float64(toRadians(angle*float32(i)))))
newY := float64(y1) + (float64(height) * math.Sin(float64(toRadians(angle*float32(i)))))
w.Triangle(x1, y1, prevX, prevY, float32(newX), float32(newY))
prevX = float32(newX)
prevY = float32(newY)
}
}
func (w *Window) Arc(x1, y1, width, height, start, stop float32) {
triangles := 360
angle := float32(360 / triangles)
prevX := x1
prevY := y1
for i := start; i <= stop; i++ {
newX := float64(x1) + (float64(width) * math.Cos(float64(toRadians(angle*float32(i)))))
newY := float64(y1) + (float64(height) * math.Sin(float64(toRadians(angle*float32(i)))))
w.Triangle(x1, y1, prevX, prevY, float32(newX), float32(newY))
prevX = float32(newX)
prevY = float32(newY)
}
}
func toRadians(degrees float32) float32 {
return degrees * math.Pi / 180
}
func (w *Window) Point(x1, y1 float32) {
x1f := fromWorldToLocalSpace(x1, w.width)
y1f := fromWorldToLocalSpace(y1, w.height)
quad := []float32{
x1f, y1f,
}
vbo := generatevbo(quad)
vao := generate2Dvao(vbo)
gl.BindVertexArray(vao)
gl.DrawArrays(gl.POINTS, 0, 1)
}
func fromWorldToLocalSpace(world float32, axis int) float32 {
return ((world / float32(axis)) * 2) - 1.0
}
func generatevbo(array []float32) uint32 {
var vbo uint32
gl.GenBuffers(1, &vbo)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.BufferData(gl.ARRAY_BUFFER, 4*len(array), gl.Ptr(array), gl.STATIC_DRAW)
return vbo
}
func generate2Dvao(vbo uint32) uint32 {
var vao uint32
gl.GenVertexArrays(1, &vao)
gl.BindVertexArray(vao)
gl.EnableVertexAttribArray(0)
gl.BindBuffer(gl.ARRAY_BUFFER, vbo)
gl.VertexAttribPointer(0, 2, gl.FLOAT, false, 0, nil)
return vao
}