/
tr.go
210 lines (186 loc) · 6.36 KB
/
tr.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
// Copyright © 2013-2018 Galvanized Logic Inc.
// Use is governed by a BSD-style license found in the LICENSE file.
package main
import (
"fmt"
"math"
"os"
"time"
"github.com/gazed/vu/device"
"github.com/gazed/vu/load"
"github.com/gazed/vu/math/lin"
"github.com/gazed/vu/render"
"github.com/gazed/vu/render/gl"
)
// tr demonstrates basic OpenGL by drawing a triangle. If anything this example
// shows that basic OpenGL is not all that basic. Check out the following methods:
// • initData() creates a triangle mesh that includes verticies, faces, and colors.
// • initScene() makes the data available to the graphics card.
// • initShader() uses render.BindProgram to load and prepare the shader programs.
// • drawScene() is called to render and spin the triangle.
// Note that the OpenGL go bindings in vu.render/gl have been generated.
//
// CONTROLS: NA
func tr() {
device.Run(&trtag{}) // Does not return.
}
// Init is a one-time callback before rendering updates.
func (tag *trtag) Init(dev device.Device) {
dev.SetTitle("Triangle")
dev.SetSize(500, 100, 600, 600)
tag.initScene()
tag.resize(600, 600)
}
// Refresh application state and render a new frame.
func (tag *trtag) Refresh(dev device.Device) {
p := dev.Down()
if p.Resized {
_, _, ww, wh := dev.Size()
tag.resize(ww, wh)
}
tag.drawScene()
dev.SwapBuffers()
}
// Globally unique "tag" that encapsulates example specific data.
type trtag struct {
shaders uint32
vao uint32
rotateAngle float64
mvpRef int32
persp *lin.M4 // Perspective matrix.
mvp64 *lin.M4 // Scratch for transform calculations.
mvp []float32 // transform matrix uniform data.
lastTime time.Time // Controls triangle rotation speed.
// mesh information
verticies []float32
faces []uint8
color []float32
}
// A tetrahedron (triangular pyramid) with sides of length 2 and
// centered at the origin (in order to look nice), would have points:
// (0, 2/√3, −1/√6)
// (±1, −1/√3, −1/√6)
// (0, 0, 3/√6)
func (tag *trtag) initData() {
tag.verticies = []float32{
0, float32(2.0 / math.Sqrt(3)), float32(-1.0 / math.Sqrt(6)), // Red
-1, float32(-1.0 / math.Sqrt(3)), float32(-1.0 / math.Sqrt(6)), // Green
1, float32(-1.0 / math.Sqrt(3)), float32(-1.0 / math.Sqrt(6)), // Blue
0, 0, float32(3.0 / math.Sqrt(6)), // White
}
tag.faces = []uint8{
0, 2, 1, // front face RGB
2, 0, 3, // left face BRW
1, 3, 0, // right face GRW
1, 2, 3, // bottom face GBW
}
tag.color = []float32{
1, 0, 0, 1, // vertex 0 is red
0, 1, 0, 1, // vertex 1 is green
0, 0, 1, 1, // vertex 2 is blue
1, 1, 1, 1, // vertex 3 is white
}
}
// resize sets the view port size. User resizes are ignored.
func (tag *trtag) resize(width int, height int) {
gl.Viewport(0, 0, int32(width), int32(height))
tag.persp.Persp(60, float64(width)/float64(height), 0.1, 50)
}
// initScene is one time initialization that creates a single VAO
func (tag *trtag) initScene() {
tag.mvp64 = lin.NewM4()
tag.persp = lin.NewM4().Persp(60, float64(600)/float64(600), 0.1, 50)
tag.mvp = make([]float32, 16) // 4x4 matrix uniform data.
tag.initData()
// Bind the OpenGL calls and dump some version info.
gl.Init()
fmt.Printf("%s %s", gl.GetString(gl.RENDERER), gl.GetString(gl.VERSION))
fmt.Printf(" GLSL %s\n", gl.GetString(gl.SHADING_LANGUAGE_VERSION))
// Gather the one scene into this one vertex array object.
gl.GenVertexArrays(1, &tag.vao)
gl.BindVertexArray(tag.vao)
// create shaders
tag.initShader()
gl.UseProgram(tag.shaders)
// vertex data.
var vbuff uint32
gl.GenBuffers(1, &vbuff)
gl.BindBuffer(gl.ARRAY_BUFFER, vbuff)
gl.BufferData(gl.ARRAY_BUFFER, int64(len(tag.verticies)*4), gl.Pointer(&(tag.verticies[0])), gl.STATIC_DRAW)
gl.VertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0)
gl.EnableVertexAttribArray(0)
// color data.
var cbuff uint32
gl.GenBuffers(1, &cbuff)
gl.BindBuffer(gl.ARRAY_BUFFER, cbuff)
gl.BufferData(gl.ARRAY_BUFFER, int64(len(tag.color)*4), gl.Pointer(&(tag.color[0])), gl.STATIC_DRAW)
gl.VertexAttribPointer(3, 4, gl.FLOAT, false, 0, 0)
gl.EnableVertexAttribArray(3)
// faces data.
var ebuff uint32
gl.GenBuffers(1, &ebuff)
gl.BindBuffer(gl.ELEMENT_ARRAY_BUFFER, ebuff)
gl.BufferData(gl.ELEMENT_ARRAY_BUFFER, int64(len(tag.faces)), gl.Pointer(&(tag.faces[0])), gl.STATIC_DRAW)
// set some state that doesn't need to change during drawing.
gl.ClearColor(0.0, 0.0, 0.0, 1.0)
gl.Enable(gl.CULL_FACE)
gl.CullFace(gl.BACK)
}
// initShader compiles shaders and links them into a shader program.
func (tag *trtag) initShader() {
shader := &load.ShdData{}
if err := shader.Load("vcolor", load.NewLocator()); err == nil {
tag.shaders = gl.CreateProgram()
if err := gl.BindProgram(tag.shaders, shader.Vsh, shader.Fsh); err != nil {
fmt.Printf("Failed to create program: %s\n", err)
}
tag.mvpRef = gl.GetUniformLocation(tag.shaders, "mvpm")
if tag.mvpRef < 0 {
fmt.Printf("No model-view-projection matrix in vertex shader\n")
}
}
}
// drawScene renders the 3D models consisting of one VAO
func (tag *trtag) drawScene() {
gl.Clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT)
tag.checkError("gl.Clear")
gl.UseProgram(tag.shaders)
tag.checkError("gl.UseProgram")
gl.BindVertexArray(tag.vao)
tag.checkError("gl.BindVertexArray")
// Use a modelview matrix and quaternion to rotate the 3D object.
tag.mvp64.SetQ(lin.NewQ().SetAa(0, 1, 0, lin.Rad(-tag.rotateAngle)))
tag.mvp64.TranslateMT(0, 0, -4)
tag.mvp = render.M4ToData(tag.mvp64.Mult(tag.mvp64, tag.persp), tag.mvp)
gl.UniformMatrix4fv(tag.mvpRef, 1, false, &(tag.mvp[0]))
if err := gl.GetError(); err != 0 {
fmt.Printf("gl.UniformMatrix error %d\n", err)
}
gl.DrawElements(gl.TRIANGLES, int32(len(tag.faces)), gl.UNSIGNED_BYTE, 0)
if err := gl.GetError(); err != 0 {
fmt.Printf("gl.DrawElements error %d\n", err)
}
// cleanup
gl.UseProgram(0)
tag.checkError("gl.UseProgram-0")
gl.BindVertexArray(0)
tag.checkError("gl.BindVertexArray-0")
// rotate based on time... not on how fast the computer runs.
if time.Now().Sub(tag.lastTime).Seconds() > 0.01 {
tag.rotateAngle++
tag.lastTime = time.Now()
}
}
// checkError helps to debug OpenGL errors by printing out when the occur.
func (tag *trtag) checkError(txt string) {
cnt := 0
err := gl.GetError()
for err != 0 {
fmt.Printf("%s error %d::%d\n", txt, cnt, err)
err = gl.GetError()
cnt++
if cnt > 10 {
os.Exit(-1)
}
}
}