/
simple.go
162 lines (138 loc) · 4.33 KB
/
simple.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
// Ported from GLUT's samples. Original copyright below applies.
/* Copyright (c) Mark J. Kilgard, 1996. */
/* This program is freely distributable without licensing fees
and is provided without guarantee or warrantee expressed or
implied. This program is -not- in the public domain. */
/* This program is a response to a question posed by Gil Colgate
<gcolgate@sirius.com> about how lengthy a program is required using
OpenGL compared to using Direct3D immediate mode to "draw a
triangle at screen coordinates 0,0, to 200,200 to 20,200, and I
want it to be blue at the top vertex, red at the left vertex, and
green at the right vertex". I'm not sure how long the Direct3D
program is; Gil has used Direct3D and his guess is "about 3000
lines of code". */
package main
import (
"github.com/go-gl/gl"
"github.com/rhencke/glut"
"fmt"
"container/list"
)
type Entity struct {
rot_matrix [4]float32 // row major order
pos [2]float32 // pos offsets
id string // unique identifier for obj
entities *list.List // reference to the entity list
}
type GameState struct {
leftPos float32
units *list.List
main_unit *Entity
}
var t GameState = GameState{}
var currentWindow glut.Window;
func reshape(w, h int) {
/* Because Gil specified "screen coordinates" (presumably with an
upper-left origin), this short bit of code sets up the coordinate
system to correspond to actual window coodrinates. This code
wouldn't be required if you chose a (more typical in 3D) abstract
coordinate system. */
/* Establish viewing area to cover entire window. */
gl.Viewport(0, 0, w, h)
/* Start modifying the projection matrix. */
gl.MatrixMode(gl.PROJECTION)
/* Reset project matrix. */
gl.LoadIdentity()
/* Map abstract coords directly to window coords. */
gl.Ortho(0, float64(w), 0, float64(h), -1, 1)
/* Invert Y axis so increasing Y goes down. */
gl.Scalef(1, -1, 1)
/* Shift origin up to upper-left corner. */
gl.Translatef(0, float32(-h), 0)
}
func display() {
gl.Clear(gl.COLOR_BUFFER_BIT)
gl.Begin(gl.TRIANGLES)
l := t.units
for e := l.Front(); e != nil; e = e.Next() {
b := e.Value.(*Entity)
//r := b.rot_matrix
p := b.pos
gl.Color3f(0.0, 0.0, 1.0) /* blue */
gl.Vertex2f(p[0], p[1])
gl.Color3f(0.0, 1.0, 0.0) /* green */
gl.Vertex2f(20 + p[0], 20 + p[1])
gl.Color3f(1.0, 0.0, 0.0) /* red */
gl.Vertex2f(p[0], 20 + p[1])
}
gl.End()
gl.Flush() /* Single buffered, so needs a flush. */
glut.SwapBuffers()
}
func keyboardIn(key byte, x, y int) {
}
func specialIn(key, x, y int) {
// If they're pressing the left key
if (key == glut.KEY_LEFT) {
t.main_unit.pos[0] -= 5.0
}
if (key == glut.KEY_RIGHT) {
t.main_unit.pos[0] += 5.0
}
if (key == glut.KEY_UP) {
t.main_unit.pos[1] -= 5.0
}
if (key == glut.KEY_DOWN) {
t.main_unit.pos[1] += 5.0
}
}
// Abstract away our logging to change later
func Log(v ...interface{}) {
fmt.Println(v...)
}
func initWindow() {
// Function called to do the re-rendering
glut.DisplayFunc(display)
// Called when the visibility of the program changes
glut.VisibilityFunc(visible)
// Called when a regular ascii character is pressed
glut.KeyboardFunc(keyboardIn)
// Called when any non-ascii character is pressed
glut.SpecialFunc(specialIn)
// Called when the size of the window changes
glut.ReshapeFunc(reshape)
// affect our projection matrix
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity() // Load an identity matrix -> projection
// Specify the bounds of the of our scene
gl.Ortho(0, 40, 0, 40, 0, 40)
// Now affect our modelview matrix
gl.MatrixMode(gl.MODELVIEW)
// Make points be rendererd larger
gl.PointSize(3.0)
currentWindow = glut.GetWindow()
}
func idle() {
currentWindow.PostRedisplay()
}
func visible(vis int) {
//if vis == glut.visible {
//if !paused {
glut.IdleFunc(idle)
//}
//} else {
// glut.idlefunc(nil)
//}
}
func main() {
glut.CreateWindow("Triangle Demo")
t.units = list.New()
e := Entity{ [...]float32{1,0,0,1},
[...]float32{0, 0},
"test",
t.units }
t.units.PushBack(&e)
t.main_unit = &e
initWindow()
glut.MainLoop()
}