-
Notifications
You must be signed in to change notification settings - Fork 60
/
Copy pathcube.go
228 lines (194 loc) · 4.99 KB
/
cube.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
// Copyright 2014 The go-gl Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Renders a textured spinning cube using GLFW 3 and OpenGL 2.1.
package main // import "github.com/go-gl/example/gl21-cube"
import (
"go/build"
"image"
"image/draw"
_ "image/png"
"log"
"os"
"runtime"
"github.com/go-gl/gl/v2.1/gl"
"github.com/go-gl/glfw/v3.3/glfw"
)
var (
texture uint32
rotationX float32
rotationY float32
)
const width, height = 800, 600
func init() {
// GLFW event handling must run on the main OS thread
runtime.LockOSThread()
}
func main() {
if err := glfw.Init(); err != nil {
log.Fatalln("failed to initialize glfw:", err)
}
defer glfw.Terminate()
glfw.WindowHint(glfw.Resizable, glfw.False)
glfw.WindowHint(glfw.ContextVersionMajor, 2)
glfw.WindowHint(glfw.ContextVersionMinor, 1)
window, err := glfw.CreateWindow(width, height, "Cube", nil, nil)
if err != nil {
panic(err)
}
window.MakeContextCurrent()
if err := gl.Init(); err != nil {
panic(err)
}
texture = newTexture("square.png")
defer gl.DeleteTextures(1, &texture)
setupScene()
for !window.ShouldClose() {
drawScene()
window.SwapBuffers()
glfw.PollEvents()
}
}
func newTexture(file string) uint32 {
imgFile, err := os.Open(file)
if err != nil {
log.Fatalf("texture %q not found on disk: %v\n", file, err)
}
img, _, err := image.Decode(imgFile)
if err != nil {
panic(err)
}
rgba := image.NewRGBA(img.Bounds())
if rgba.Stride != rgba.Rect.Size().X*4 {
panic("unsupported stride")
}
draw.Draw(rgba, rgba.Bounds(), img, image.Point{0, 0}, draw.Src)
var texture uint32
gl.Enable(gl.TEXTURE_2D)
gl.GenTextures(1, &texture)
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.CLAMP_TO_EDGE)
gl.TexParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.CLAMP_TO_EDGE)
gl.TexImage2D(
gl.TEXTURE_2D,
0,
gl.RGBA,
int32(rgba.Rect.Size().X),
int32(rgba.Rect.Size().Y),
0,
gl.RGBA,
gl.UNSIGNED_BYTE,
gl.Ptr(rgba.Pix))
return texture
}
func setupScene() {
gl.Enable(gl.DEPTH_TEST)
gl.Enable(gl.LIGHTING)
gl.ClearColor(0.5, 0.5, 0.5, 0.0)
gl.ClearDepth(1)
gl.DepthFunc(gl.LEQUAL)
ambient := []float32{0.5, 0.5, 0.5, 1}
diffuse := []float32{1, 1, 1, 1}
lightPosition := []float32{-5, 5, 10, 0}
gl.Lightfv(gl.LIGHT0, gl.AMBIENT, &ambient[0])
gl.Lightfv(gl.LIGHT0, gl.DIFFUSE, &diffuse[0])
gl.Lightfv(gl.LIGHT0, gl.POSITION, &lightPosition[0])
gl.Enable(gl.LIGHT0)
gl.MatrixMode(gl.PROJECTION)
gl.LoadIdentity()
f := float64(width)/height - 1
gl.Frustum(-1-f, 1+f, -1, 1, 1.0, 10.0)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
}
func drawScene() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
gl.MatrixMode(gl.MODELVIEW)
gl.LoadIdentity()
gl.Translatef(0, 0, -3.0)
gl.Rotatef(rotationX, 1, 0, 0)
gl.Rotatef(rotationY, 0, 1, 0)
rotationX += 0.5
rotationY += 0.5
gl.BindTexture(gl.TEXTURE_2D, texture)
gl.Color4f(1, 1, 1, 1)
gl.Begin(gl.QUADS)
gl.Normal3f(0, 0, 1)
gl.TexCoord2f(0, 0)
gl.Vertex3f(-1, -1, 1)
gl.TexCoord2f(1, 0)
gl.Vertex3f(1, -1, 1)
gl.TexCoord2f(1, 1)
gl.Vertex3f(1, 1, 1)
gl.TexCoord2f(0, 1)
gl.Vertex3f(-1, 1, 1)
gl.Normal3f(0, 0, -1)
gl.TexCoord2f(1, 0)
gl.Vertex3f(-1, -1, -1)
gl.TexCoord2f(1, 1)
gl.Vertex3f(-1, 1, -1)
gl.TexCoord2f(0, 1)
gl.Vertex3f(1, 1, -1)
gl.TexCoord2f(0, 0)
gl.Vertex3f(1, -1, -1)
gl.Normal3f(0, 1, 0)
gl.TexCoord2f(0, 1)
gl.Vertex3f(-1, 1, -1)
gl.TexCoord2f(0, 0)
gl.Vertex3f(-1, 1, 1)
gl.TexCoord2f(1, 0)
gl.Vertex3f(1, 1, 1)
gl.TexCoord2f(1, 1)
gl.Vertex3f(1, 1, -1)
gl.Normal3f(0, -1, 0)
gl.TexCoord2f(1, 1)
gl.Vertex3f(-1, -1, -1)
gl.TexCoord2f(0, 1)
gl.Vertex3f(1, -1, -1)
gl.TexCoord2f(0, 0)
gl.Vertex3f(1, -1, 1)
gl.TexCoord2f(1, 0)
gl.Vertex3f(-1, -1, 1)
gl.Normal3f(1, 0, 0)
gl.TexCoord2f(1, 0)
gl.Vertex3f(1, -1, -1)
gl.TexCoord2f(1, 1)
gl.Vertex3f(1, 1, -1)
gl.TexCoord2f(0, 1)
gl.Vertex3f(1, 1, 1)
gl.TexCoord2f(0, 0)
gl.Vertex3f(1, -1, 1)
gl.Normal3f(-1, 0, 0)
gl.TexCoord2f(0, 0)
gl.Vertex3f(-1, -1, -1)
gl.TexCoord2f(1, 0)
gl.Vertex3f(-1, -1, 1)
gl.TexCoord2f(1, 1)
gl.Vertex3f(-1, 1, 1)
gl.TexCoord2f(0, 1)
gl.Vertex3f(-1, 1, -1)
gl.End()
}
// Set the working directory to the root of Go package, so that its assets can be accessed.
func init() {
dir, err := importPathToDir("github.com/go-gl/example/gl21-cube")
if err != nil {
log.Fatalln("Unable to find Go package in your GOPATH, it's needed to load assets:", err)
}
err = os.Chdir(dir)
if err != nil {
log.Panicln("os.Chdir:", err)
}
}
// importPathToDir resolves the absolute path from importPath.
// There doesn't need to be a valid Go package inside that import path,
// but the directory must exist.
func importPathToDir(importPath string) (string, error) {
p, err := build.Import(importPath, "", build.FindOnly)
if err != nil {
return "", err
}
return p.Dir, nil
}