forked from Thinkofname/steven-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gl.go
313 lines (270 loc) · 8.23 KB
/
gl.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
// Copyright 2015 Matthew Collins
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
// Package gl provides a more Go friendly OpenGL API
package gl
import (
"fmt"
"unsafe"
"github.com/thinkofdeath/gl/v3.2-core/gl"
"github.com/thinkofdeath/steven/console"
)
// ClearFlags is a set of flags to mark what should be cleared during
// a Clear call.
type ClearFlags uint32
const (
// ColorBufferBit marks the color buffer to be cleared
ColorBufferBit ClearFlags = gl.COLOR_BUFFER_BIT
// DepthBufferBit marks the depth buffer to be cleared
DepthBufferBit ClearFlags = gl.DEPTH_BUFFER_BIT
// StencilBufferBit marks the stencil buffer to be cleared
StencilBufferBit ClearFlags = gl.STENCIL_BUFFER_BIT
)
// Flag is a setting that can be enabled or disabled on the context.
type Flag uint32
// Valid flags
const (
DepthTest Flag = gl.DEPTH_TEST
CullFaceFlag Flag = gl.CULL_FACE
StencilTest Flag = gl.STENCIL_TEST
Blend Flag = gl.BLEND
DebugOutput Flag = gl.DEBUG_OUTPUT
Multisample Flag = gl.MULTISAMPLE
)
// Face specifies a face to act on.
type Face uint32
// Valid faces
const (
Back Face = gl.BACK
Front Face = gl.FRONT
)
// FaceDirection is used to specify an order of vertices, normally
// used to set which is considered to be the front face.
type FaceDirection uint32
// Valid directions for vertices for faces.
const (
ClockWise FaceDirection = gl.CW
CounterClockWise FaceDirection = gl.CCW
)
// DrawType is used to specify how the vertices will be handled
// to draw.
type DrawType uint32
const (
// Triangles treats each set of 3 vertices as a triangle
Triangles DrawType = gl.TRIANGLES
// LineStrip means the previous vertex connects to the next
// one in a continuous strip.
LineStrip DrawType = gl.LINE_STRIP
// Lines treats each set of 2 vertices as a line
Lines DrawType = gl.LINES
// Points treats each vertex as a point
Points DrawType = gl.POINTS
)
// Func is a function to be preformed on two values.
type Func uint32
// Functions
const (
Never Func = gl.NEVER
Less Func = gl.LESS
LessOrEqual Func = gl.LEQUAL
Greater Func = gl.GREATER
Always Func = gl.ALWAYS
Equal Func = gl.EQUAL
)
// Op is an operation to be applied (depending on the method used)
type Op uint32
// Valid operations
const (
Replace Op = gl.REPLACE
Keep Op = gl.KEEP
Zero Op = gl.ZERO
)
// Factor is used in blending
type Factor uint32
// Valid factors
const (
SrcAlpha Factor = gl.SRC_ALPHA
OneMinusSrcAlpha Factor = gl.ONE_MINUS_SRC_ALPHA
OneFactor Factor = gl.ONE
ZeroFactor Factor = gl.ZERO
)
// Init inits the gl library. This should be called once a context is ready.
func Init() {
if err := gl.Init(); err != nil {
panic(err)
}
}
// Viewport sets the size of the viewport of this context.
func Viewport(x, y, width, height int) {
gl.Viewport(int32(x), int32(y), int32(width), int32(height))
}
// ClearColor sets the color the color buffer should be cleared to
// when Clear is called with the color flag.
func ClearColor(r, g, b, a float32) {
gl.ClearColor(r, g, b, a)
}
// Clear clears the buffers specified by the passed flags.
func Clear(flags ClearFlags) {
gl.Clear(uint32(flags))
}
// ActiveTexture sets the texture slot with the passed id as the
// currently active one.
func ActiveTexture(id int) {
gl.ActiveTexture(gl.TEXTURE0 + uint32(id))
}
// Enable enables the passed flag.
func Enable(flag Flag) {
gl.Enable(uint32(flag))
}
// Disable disables the passed flag.
func Disable(flag Flag) {
gl.Disable(uint32(flag))
}
// CullFace sets the face to be culled by the gpu.
func CullFace(face Face) {
gl.CullFace(uint32(face))
}
// FrontFace sets the direction of vertices used to specify the
// front face (e.g. for culling).
func FrontFace(dir FaceDirection) {
gl.FrontFace(uint32(dir))
}
// DrawArrays draws the passed number of triangles starting at the
// passed offset using data from the currently bound buffer(s).
// The DrawType specifies how the shapes (triangles, lines etc)
// will be formed from the data.
func DrawArrays(ty DrawType, offset, count int) {
gl.DrawArrays(uint32(ty), int32(offset), int32(count))
}
func DrawElements(ty DrawType, count int, dty Type, offset int) {
gl.DrawElements(uint32(ty), int32(count), uint32(dty), uintptr(offset))
}
func MultiDrawElements(ty DrawType, count []int32, dty Type, offset []uintptr) {
gl.MultiDrawElements(uint32(ty), (*int32)(gl.Ptr(count)), uint32(dty), (*uintptr)(gl.Ptr(offset)), int32(len(count)))
}
// CheckError panics if there has been an error reported to the
// context. This is normally not a cheap call so shouldn't be
// used in production.
func CheckError() {
err := gl.GetError()
if err != 0 {
panic(fmt.Sprintf("gl error: %d", err))
}
}
// Flush flushes all commands in the queue and waits for completion.
func Flush() {
gl.Flush()
}
// DepthMask sets whether the depth buffer can be written too.
func DepthMask(f bool) {
gl.DepthMask(f)
}
// DepthFunc sets the function used to decide whether to cull a pixel
// when depth testing
func DepthFunc(f Func) {
gl.DepthFunc(uint32(f))
}
// ColorMask sets whether each color channel be the written too.
func ColorMask(r, g, b, a bool) {
gl.ColorMask(r, g, b, a)
}
// StencilFunc sets the function to be used when comparing with the
// stencil buffer.
func StencilFunc(f Func, ref, mask int) {
gl.StencilFunc(uint32(f), int32(ref), uint32(mask))
}
// StencilMask sets the value to be written to the stencil buffer on
// success.
func StencilMask(mask int) {
gl.StencilMask(uint32(mask))
}
// StencilOp sets the operation to be executed depending on the result
// of the stencil test.
func StencilOp(op, fail, pass Op) {
gl.StencilOp(uint32(op), uint32(fail), uint32(pass))
}
// ClearStencil clears the stencil buffer will the passed value.
func ClearStencil(i int) {
gl.ClearStencil(int32(i))
}
// BlendFunc sets the factors to be used when blending.
func BlendFunc(sFactor, dFactor Factor) {
gl.BlendFunc(uint32(sFactor), uint32(dFactor))
}
// BlendFuncSeparate sets the factors to be used when blending.
func BlendFuncSeparate(sFactorRGB, dFactorRGB, sFactorA, dFactorA Factor) {
gl.BlendFuncSeparate(uint32(sFactorRGB), uint32(dFactorRGB), uint32(sFactorA), uint32(dFactorA))
}
// DebugLog enables OpenGL's debug messages and logs them to stdout.
func DebugLog() {
gl.DebugMessageCallback(func(
source uint32,
gltype uint32,
id uint32,
severity uint32,
length int32,
message string,
userParam unsafe.Pointer) {
// Source
strSource := "unknown"
switch source {
case gl.DEBUG_SOURCE_API:
strSource = "api"
case gl.DEBUG_SOURCE_WINDOW_SYSTEM:
strSource = "windowSystem"
case gl.DEBUG_SOURCE_SHADER_COMPILER:
strSource = "shaderCompiler"
case gl.DEBUG_SOURCE_THIRD_PARTY:
strSource = "thirdParty"
case gl.DEBUG_SOURCE_APPLICATION:
strSource = "application"
case gl.DEBUG_SOURCE_OTHER:
strSource = "other"
}
// Type
strType := "unknown"
switch gltype {
case gl.DEBUG_TYPE_ERROR:
strType = "error"
case gl.DEBUG_TYPE_DEPRECATED_BEHAVIOR:
strType = "deprecatedBehavior"
case gl.DEBUG_TYPE_UNDEFINED_BEHAVIOR:
strType = "undefinedBehavior"
case gl.DEBUG_TYPE_PORTABILITY:
strType = "portability"
case gl.DEBUG_TYPE_PERFORMANCE:
strType = "performance"
case gl.DEBUG_TYPE_MARKER:
strType = "marker"
case gl.DEBUG_TYPE_PUSH_GROUP:
strType = "pushGroup"
case gl.DEBUG_TYPE_POP_GROUP:
strType = "popGroup"
case gl.DEBUG_TYPE_OTHER:
strType = "other"
}
// Severity
strSeverity := "unknown"
switch severity {
case gl.DEBUG_SEVERITY_HIGH:
strSeverity = "high"
case gl.DEBUG_SEVERITY_MEDIUM:
strSeverity = "medium"
case gl.DEBUG_SEVERITY_LOW:
strSeverity = "low"
case gl.DEBUG_SEVERITY_NOTIFICATION:
return
}
console.Text("[%s][%s][%s]: %s", strSource, strType, strSeverity, message)
}, nil)
}