/
main.go
360 lines (305 loc) · 8.98 KB
/
main.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
package main
import "sdl"
import "gl"
import "flag"
import (
"math"
)
const (
PiOver100 float64 = 0.0174532925199433
cubeWidth = 0.5
)
var printInfo = flag.Bool("info", false, "print GL implementation information")
var T0 uint32 = 0
var Frames uint32 = 0
var (
yrot float32 // camera rotation
xpos, zpos float32 // camera position
walkbias, walkbiasangle float32 // head-bobbing....
lookupdown float32
whiteColor = []float32{1.0, 1.0, 1.0, 1.0}
lightAmbient = []float32{0.5, 0.5, 0.5, 1.0}
lightDiffuse = []float32{1.0, 1.0, 1.0, 1.0}
lightPosition = []float32{0.0, 0.0, 2.0, 1.0}
)
var cubes map[string]uint = make(map[string]uint)
var chunks []*ChunkInfo
func cube(color []float32) {
gl.Materialfv(gl.FRONT, gl.AMBIENT_AND_DIFFUSE, whiteColor)
gl.Color4f(color[0], color[1], color[2], color[3])
// TOP
gl.Begin(gl.TRIANGLE_FAN)
gl.Vertex3f(cubeWidth, cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, cubeWidth, cubeWidth)
gl.Vertex3f(cubeWidth, cubeWidth, cubeWidth)
gl.End()
// Bottom
gl.Begin(gl.TRIANGLE_FAN)
gl.Vertex3f(cubeWidth, -cubeWidth, cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, -cubeWidth)
gl.Vertex3f(cubeWidth, -cubeWidth, -cubeWidth)
gl.End()
// Front
gl.Begin(gl.TRIANGLE_FAN)
gl.Vertex3f(cubeWidth, cubeWidth, cubeWidth)
gl.Vertex3f(-cubeWidth, cubeWidth, cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, cubeWidth)
gl.Vertex3f(cubeWidth, -cubeWidth, cubeWidth)
gl.End()
// Back
gl.Begin(gl.TRIANGLE_FAN)
gl.Vertex3f(cubeWidth, -cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, cubeWidth, -cubeWidth)
gl.Vertex3f(cubeWidth, cubeWidth, -cubeWidth)
gl.End()
// Left
gl.Begin(gl.TRIANGLE_FAN)
gl.Vertex3f(-cubeWidth, cubeWidth, cubeWidth)
gl.Vertex3f(-cubeWidth, cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, cubeWidth)
gl.End()
// Right
gl.Begin(gl.TRIANGLE_FAN)
gl.Vertex3f(cubeWidth, cubeWidth, -cubeWidth)
gl.Vertex3f(cubeWidth, cubeWidth, cubeWidth)
gl.Vertex3f(cubeWidth, -cubeWidth, cubeWidth)
gl.Vertex3f(cubeWidth, -cubeWidth, -cubeWidth)
gl.End()
//*
gl.Color4f(color[0]/1.2, color[1]/1.2, color[2]/1.2, 1.0)
gl.Begin(gl.LINE_STRIP) // Start Drawing Our Player Using Lines
// TOP
gl.Vertex3f(cubeWidth, cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, cubeWidth, cubeWidth)
gl.Vertex3f(cubeWidth, cubeWidth, cubeWidth)
// Front
gl.Vertex3f(-cubeWidth, cubeWidth, cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, cubeWidth)
gl.Vertex3f(cubeWidth, -cubeWidth, cubeWidth)
// Bottom
gl.Vertex3f(-cubeWidth, -cubeWidth, cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, -cubeWidth)
gl.Vertex3f(cubeWidth, -cubeWidth, -cubeWidth)
// Back
gl.Vertex3f(-cubeWidth, -cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, cubeWidth, -cubeWidth)
gl.Vertex3f(cubeWidth, cubeWidth, -cubeWidth)
// Right
gl.Vertex3f(cubeWidth, cubeWidth, cubeWidth)
gl.Vertex3f(cubeWidth, -cubeWidth, cubeWidth)
gl.Vertex3f(cubeWidth, -cubeWidth, -cubeWidth)
// Left
gl.Vertex3f(-cubeWidth, cubeWidth, cubeWidth)
gl.Vertex3f(-cubeWidth, cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, -cubeWidth)
gl.Vertex3f(-cubeWidth, -cubeWidth, cubeWidth)
gl.End()
//*/
}
func draw() {
xtrans := -xpos
ztrans := -zpos
ytrans := -walkbias - 0.25
scenroty := 360.0 - yrot
// Clear the screen and depth buffer
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
// reset the view
gl.LoadIdentity()
// Rotate up and down to look up and down
gl.Rotatef(lookupdown, 1.0, 0.0, 0.0)
// Rotate depending on direction player is facing
gl.Rotatef(scenroty, 0.0, 1.0, 0.0)
// translate the scene based on player position
gl.Translatef(xtrans, ytrans-1.75, ztrans)
for _, chunk := range chunks {
for y := 0; y < 16; y++ {
for x := 0; x < 16; x++ {
for z := 0; z <= 16; z++ {
if len(chunk.Data[y][x]) > z && chunk.Data[y][x][z] != "" {
gl.PushMatrix()
gl.Translated(
float64(16*chunk.X+x),
float64(z),
float64(16*chunk.Y+y))
gl.CallList(cubes[chunk.Data[y][x][z]])
gl.PopMatrix()
}
}
}
}
}
gl.PopMatrix()
sdl.GL_SwapBuffers()
Frames++
{
t := sdl.GetTicks()
if t-T0 >= 5000 {
seconds := (t - T0) / 1000.0
fps := Frames / seconds
print(Frames, " frames in ", seconds, " seconds = ", fps, " FPS\n")
T0 = t
Frames = 0
}
}
}
func idle() {
}
/* new window size or exposure */
func reshape(width int, height int) {
// protect against a divide by zero
if height == 0 {
height = 1
}
// Setup our viewport
gl.Viewport(0, 0, width, height)
// change to the projection matrix and set our viewing volume.
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
// aspect ratio
aspect := float64(width) / float64(height)
// Set our perspective.
// This code is equivalent to using gluPerspective as in the original tutorial.
var fov, near, far float64
fov = 45.0
near = 0.1
far = 100.0
top := math.Tan(float64(fov*math.Pi/360.0)) * near
bottom := -top
left := aspect * bottom
right := aspect * top
gl.Frustum(left, right, bottom, top, near, far)
// Make sure we're changing the model view and not the projection
gl.MatrixMode(gl.MODELVIEW)
// Reset the view
gl.LoadIdentity()
}
func fetchChunks(chunkY, chunkX int) {
for y := -chunkY; y < chunkY; y++ {
for x := -chunkX; x < chunkX; x++ {
if chunk, err := GetChunk(y, x); err == nil {
chunks = append(chunks, chunk)
}
}
}
}
func init_() {
//pos := []float64{5.0, 5.0, 10.0, 0.0}
colors := map[string][]float32{
"0": {0.7, 0.7, 0.7, 1.0},
"1": {1.0, 0.0, 0.0, 1.0},
"2": {1.0, 0.6, 0.0, 1.0},
"3": {1.0, 1.0, 0.0, 1.0},
"4": {0.0, 1.0, 0.0, 1.0},
"5": {0.0, 0.0, 1.0, 1.0},
"6": {0.0, 0.6, 1.0, 1.0},
"7": {1.0, 0.0, 1.0, 1.0},
"8": {1.0, 1.0, 1.0, 1.0},
"9": {0.3, 0.3, 0.3, 1.0},
}
fetchChunks(2, 2)
gl.ShadeModel(gl.SMOOTH)
gl.ClearColor(0.2, 0.2, 0.6, 0.0)
gl.ClearDepth(1.0)
gl.Enable(gl.DEPTH_TEST)
gl.DepthFunc(gl.LEQUAL)
gl.Enable(gl.COLOR_MATERIAL)
gl.Enable(gl.CULL_FACE)
gl.Hint(gl.PERSPECTIVE_CORRECTION_HINT, gl.NICEST)
gl.Hint(gl.LINE_SMOOTH_HINT, gl.NICEST)
gl.Lightfv(gl.LIGHT0, gl.AMBIENT, lightAmbient)
gl.Lightfv(gl.LIGHT0, gl.DIFFUSE, lightDiffuse)
gl.Lightfv(gl.LIGHT0, gl.POSITION, lightPosition)
gl.Enable(gl.LIGHT0)
gl.Enable(gl.LIGHTING)
for name, color := range colors {
/* make a cube */
cubes[name] = gl.GenLists(1)
gl.NewList(cubes[name], gl.COMPILE)
cube(color)
gl.EndList()
}
if *printInfo {
print("GL_RENDERER = ", gl.GetString(gl.RENDERER), "\n")
print("GL_VERSION = ", gl.GetString(gl.VERSION), "\n")
print("GL_VENDOR = ", gl.GetString(gl.VENDOR), "\n")
print("GL_EXTENSIONS = ", gl.GetString(gl.EXTENSIONS), "\n")
}
}
// handle key press events
func handleKeyPress(keysym []uint8) {
keys := sdl.GetKeyState()
if keys[sdl.K_RIGHT] == 1 {
yrot -= 3.5
}
if keys[sdl.K_LEFT] == 1 {
yrot += 3.5
}
if keys[sdl.K_UP] == 1 {
xpos -= float32(math.Sin(float64(yrot)*PiOver100)) * 0.5
zpos -= float32(math.Cos(float64(yrot)*PiOver100)) * 0.5
if walkbiasangle >= 359.0 {
walkbiasangle = 0.0
} else {
walkbiasangle += 10.0
}
walkbias = float32(math.Sin(float64(walkbiasangle)*PiOver100)) / 20.0
}
if keys[sdl.K_DOWN] == 1 {
xpos += float32(math.Sin(float64(yrot)*PiOver100)) * 0.5
zpos += float32(math.Cos(float64(yrot)*PiOver100)) * 0.5
if walkbiasangle <= 1.0 {
walkbiasangle = 359.0
} else {
walkbiasangle -= 10.0
}
walkbias = float32(math.Sin(float64(walkbiasangle)*PiOver100)) / 20.0
}
}
func main() {
flag.Parse()
var done bool
var keys []uint8
sdl.Init(sdl.INIT_VIDEO)
var screen = sdl.SetVideoMode(640, 480, 16, sdl.OPENGL|sdl.RESIZABLE)
if screen == nil {
sdl.Quit()
panic("Couldn't set 300x300 GL video mode: " + sdl.GetError() + "\n")
}
sdl.WM_SetCaption("Gears", "gears")
init_()
reshape(int(screen.W), int(screen.H))
done = false
for !done {
var event sdl.Event
idle()
for event.Poll() {
switch event.Type {
case sdl.VIDEORESIZE:
screen = sdl.SetVideoMode(int(event.Resize().W), int(event.Resize().H), 16,
sdl.OPENGL|sdl.RESIZABLE)
if screen != nil {
reshape(int(screen.W), int(screen.H))
} else {
panic("we couldn't set the new video mode??")
}
break
case sdl.QUIT:
done = true
break
}
}
keys = sdl.GetKeyState()
handleKeyPress(keys)
if keys[sdl.K_ESCAPE] != 0 {
done = true
}
draw()
}
sdl.Quit()
return
}