-
Notifications
You must be signed in to change notification settings - Fork 0
/
graphics.go
111 lines (87 loc) · 2.06 KB
/
graphics.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
package main
import gl "github.com/go-gl/gl"
import "sync"
type GraphicsQueue struct {
calls []func()
}
var graphicsQueue = NewGraphicsQueue()
var lock = new(sync.Mutex)
func NewGraphicsQueue() *GraphicsQueue {
queue := new(GraphicsQueue)
queue.calls = make([]func(), 0, 100)
return queue
}
func (gq *GraphicsQueue) Process() {
calls := make([]func(), len(gq.calls))
lock.Lock()
copy(calls, gq.calls)
gq.calls = make([]func(), 0, 100)
lock.Unlock()
for _, call := range calls {
call()
}
}
func (gq *GraphicsQueue) Enqueue(call func()) {
lock.Lock()
gq.calls = append(gq.calls, call)
lock.Unlock()
}
func fill_background(args List, context *Context) Data {
red := args.First().(Float)
green := args.Second().(Float)
blue := args.Third().(Float)
alpha := args.Get(3).(Float)
graphicsQueue.Enqueue(func() {
gl.ClearColor(gl.GLclampf(red.Value), gl.GLclampf(green.Value),
gl.GLclampf(blue.Value), gl.GLclampf(alpha.Value))
})
return Nothing{}
}
//--------------------------------
// Cube / World Rendering
type Vertex3D struct {
X, Y, Z float64
}
func (v Vertex3D) Similar(other Vertex3D) bool {
dx := other.X - v.X
dy := other.Y - v.Y
dz := other.Z - v.Z
return (dx*dx + dy*dy + dz*dz) < 0.5
}
type Vertex4D struct {
Vertex3D
W float64
}
func (cube *Cube) Render() {
x, y, z := cube.Position.X, cube.Position.Y, cube.Position.Z
gl.Begin(gl.QUADS)
gl.Color3d(0.5-(x/50), 0.5-(y/50), 0.5-(z/50))
// Front Side
gl.TexCoord2f(0, 0)
gl.Vertex3d(x-0.5, y-0.5, z+0.5)
gl.TexCoord2f(1, 0)
gl.Vertex3d(x+0.5, y-0.5, z+0.5)
gl.TexCoord2f(1, 1)
gl.Vertex3d(x+0.5, y+0.5, z+0.5)
gl.TexCoord2f(0, 1)
gl.Vertex3d(x-0.5, y+0.5, z+0.5)
// Left Side
gl.Color3d(0.5-(x/20), 0.5-(y/20), 0.5-(z/20))
gl.TexCoord2f(0, 0)
gl.Vertex3d(x-0.5, y-0.5, z-0.5)
gl.TexCoord2f(1, 0)
gl.Vertex3d(x-0.5, y-0.5, z+0.5)
gl.TexCoord2f(1, 1)
gl.Vertex3d(x-0.5, y+0.5, z+0.5)
gl.TexCoord2f(0, 1)
gl.Vertex3d(x-0.5, y+0.5, z-0.5)
gl.End()
}
func (world *World) Render() {
for _, cube := range world.cubePool {
if !cube.used {
continue
}
cube.Render()
}
}