/
ngl.nim
345 lines (299 loc) · 11.3 KB
/
ngl.nim
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
from colors import Color, colRed
proc a(c:Color):float {.inline.} =
1.0 - (c.int shr 24 and 0xff).float / 255.0
proc r(c:Color):float {.inline.} =
(c.int shr 16 and 0xff).float / 255.0
proc g(c:Color):float {.inline.} =
(c.int shr 8 and 0xff).float / 255.0
proc b(c:Color):float {.inline.} =
(c.int shr 0 and 0xff).float / 255.0
#------------------------------------------------------------#
when defined js:
import webgl except flush
elif not defined android:
import opengl
when defined js:
type ShaderKind* = enum
skNone,
skFragment = seFragment_Shader,
skVertex = seVertex_Shader
else:
type ShaderKind* = enum
skNone, # FIXME: uninitialized warning
skFragment = GL_FRAGMENT_SHADER,
skVertex = GL_VERTEX_SHADER
when defined js:
type BufferKind* = enum
bkNone,
bkArray = beARRAY_BUFFER,
bkElement = beElement_ARRAY_BUFFER
else:
type BufferKind* = enum
bkNone, # FIXME: uninitialized warning
bkArray = GL_ARRAY_BUFFER
bkElement = GL_ELEMENT_ARRAY_BUFFER
when defined js:
type DrawMode* = enum
dmNone,
dmStatic = beStaticDraw,
dmDynamic = beDynamicDraw
else:
type DrawMode* = enum
dmNone,
dmStatic = GL_STATIC_DRAW,
dmDynamic = GL_DYNAMIC_DRAW
when defined js:
type DataKind* = enum
dkNone
dkByte = dtBYTE
dkUByte = dtUNSIGNED_BYTE
dkShort = dtSHORT
dkUShort = dtUNSIGNED_SHORT
dkInt = dtINT
dkUInt = dtUNSIGNED_INT
dkFloat = dtFLOAT
else:
type DataKind* = enum
dkNone
dkByte = cGL_BYTE
dkUByte = GL_UNSIGNED_BYTE
dkShort = cGL_SHORT
dkUShort = GL_UNSIGNED_SHORT
dkInt = cGL_INT
dkUInt = GL_UNSIGNED_INT
dkFloat = cGL_FLOAT
when defined js:
type PrimitiveKind* = enum
pkPoints = pmPoints
pkLines = pmLines
pkLineLoop = pmLineLoop
pkLineStrip = pmLineStrip
pkTriangles = pmTriangles
pkTriangleStrip = pmTriangleStrip
pkTriangleFan = pmTriangleFan
else:
type PrimitiveKind* = enum
pkPoints = GL_POINTS
pkLines = GL_LINES
pkLineLoop = GL_LINE_LOOP
pkLineStrip = GL_LINE_STRIP
pkTriangles = GL_TRIANGLES
pkTriangleStrip = GL_TRIANGLE_STRIP
pkTriangleFan = GL_TRIANGLE_FAN
type
Shader* = object
source: string
kind : ShaderKind
when defined js:
s: WebglShader
elif not defined android:
s:GLuint
else:
discard
Program* = object
vertex, fragment: Shader
when defined js:
p: WebglProgram
elif not defined android:
p: GLuint
else:
discard
Buffer* = object
when defined js:
b:WebglBuffer
else:
b:GLuint
when defined js:
type UniformLocation* = WebglUniformLocation
else:
type UniformLocation* = GLint
when defined js:
type AttribLocation* = uint
else:
type AttribLocation* = GLuint
when defined js:
type ContextGL* = WebglRenderingContext # shorthand
elif not defined android:
type ContextGL* = object
when defined js:
proc toJSA*(v:openarray[float32]) :Float32Array {.importcpp: "new Float32Array(#)".} # might be a lie
proc toJSA*(v:openarray[float]) :Float32Array {.importcpp: "new Float32Array(#)".} # might be a lie
proc toJSA*(v:openarray[uint16]) :Uint16Array {.importcpp: "new Uint16Array(#)".} # might be a lie
proc toJSA*(v:openarray[int32]) :Int32Array {.importcpp: "new Uint16Array(#)".} # might be a lie
proc toJSA*(v:openarray[int]) :Int32Array {.importcpp: "new Int32Array(#)".} # might be a lie
const VecSize {.intdefine.} : int = 4
proc viewport*(gl:ContextGL,x=0,y=0, w,h:int) =
when defined js:
webgl.viewport(gl, x,y, w,h)
else:
glViewport(x.GLint,y.GLint,w.GLsizei,h.GLsizei)
proc aspectRatio*(gl:ContextGL):float =
## Returns the viewport aspect ratio ( width / height )
when defined js:
result = gl.drawingBufferWidth/gl.drawingBufferHeight
proc clearColor*(gl:ContextGL, r,g,b,a:float) =
## Set clear color
when defined js:
webgl.clearColor(gl, r, g, b, a)
elif not defined android:
glClearColor(r, g, b, a)
proc clearColor*(gl:ContextGL, c:Color) = gl.clearColor(c.r,c.g,c.b,c.a)
proc clear*(gl:ContextGL, color=true,depth:bool=false) =
when defined js:
if color and depth:
webgl.clear(gl, ord(bbColor) or ord(bbDepth))
elif color:
webgl.clear(gl,bbColor)
elif depth:
webgl.clear(gl,bbDepth)
else:
if color and depth:
glclear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT)
elif color:
glclear(GL_COLOR_BUFFER_BIT)
elif depth:
glclear(GL_DEPTH_BUFFER_BIT)
proc createShader*(gl:ContextGL, kind:ShaderKind, src:string):Shader =
## Init and compile a shader
result.kind = kind
result.source = src
when defined js:
result.s = webgl.createShader(gl, kind.ShaderEnum)
webgl.shaderSource(gl, result.s,src)
webgl.compileShader(gl, result.s)
if not webgl.getStatus(gl, result.s): doassert(false,$webgl.getShaderInfoLog(gl, result.s))
elif not defined android:
result.s = glCreateShader(GLenum(kind))
glShaderSource(result.s, 1, [src].allocCStringArray, nil)
glCompileShader(result.s)
var status: GLint
glGetShaderiv(result.s, GL_COMPILE_STATUS, addr status)
if status == GL_FALSE.int:
var logLength: GLint
glGetShaderiv(result.s, GL_INFO_LOG_LENGTH, addr logLength)
var errorLog: cstring = cast[cstring](alloc(logLength))
glGetShaderInfoLog(result.s, logLength, addr logLength, errorLog)
echo(errorLog)
proc createProgram*(gl:ContextGL, useIt:bool=false ,vertex_src:string, fragment_src:string):Program =
## Init, compile and link a program.
## If `useIt` is true, also sets the program as current
let
vs = gl.createShader(skVertex, vertex_src) # vmsrc)
fs = gl.createShader(skFragment, fragment_src) # fsrc)
result.vertex = vs
result.fragment = fs
when defined js:
var program = webgl.createProgram(gl)
webgl.attachShader(gl,program, vs.s)
webgl.attachShader(gl,program, fs.s)
webgl.linkProgram(gl,program)
if not webgl.getStatus(gl,program):
doassert(false, $webgl.getProgramInfoLog(gl,program))
if useIt: webgl.useProgram(gl,program)
elif not defined android:
var program = glCreateProgram()
glAttachShader(program, vs.s)
glAttachShader(program, fs.s)
# mmh glBindFragDataLocation(program, 0, "outColor")
glLinkProgram(program)
var status: GLint
glGetProgramiv(program, GL_LINK_STATUS, addr status)
if status == GL_FALSE.int:
var logLength: GLint
glGetProgramiv(program, GL_INFO_LOG_LENGTH, addr logLength)
var errorLog: cstring = cast[cstring](alloc(logLength))
glGetProgramInfoLog(program, logLength, addr logLength, errorLog)
echo(errorLog)
if useit:
glUseProgram(program)
result.p = program
proc createBuffer*(gl:ContextGL):Buffer =
when defined js:
result.b = webgl.createBuffer(gl)
else:
glGenBuffers(1,addr result.b)
proc bindBuffer*(gl:ContextGL,kind:BufferKind, buffer:Buffer) =
when defined js:
webgl.bindBuffer(gl, kind.BufferEnum,buffer.b)
else:
glBindBuffer(GLenum(kind),buffer.b)
proc bufferData*(gl:ContextGL, kind:BufferKind, data:openArray[SomeNumber],usage:DrawMode) =
when defined js:
webgl.bufferData(gl, BufferEnum(kind), data.toJSA,BufferEnum(usage))
else:
glBufferData(GLenum(kind), sizeof(data[0])*data.len, unsafeAddr data, GLenum(usage))
proc bindVertices*(gl:ContextGL, vertices:openarray[float], drawMode:DrawMode=dmStatic) =
## Bind vertices to the context
## drawMode must be one of : # TODO: check this
## STATIC_DRAW: Contents of the buffer are likely to be used often and not change often. Contents are written to the buffer, but not read.
## DYNAMIC_DRAW: Contents of the buffer are likely to be used often and change often. Contents are written to the buffer, but not read.
## STREAM_DRAW: Contents of the buffer are likely to not be used often. Contents are written to the buffer, but not read.
gl.bindBuffer(bkArray, gl.createBuffer())
gl.bufferData(bkArray, vertices, drawMode)
proc getUniformLocation*(gl: ContextGL, program: Program, name: string): UniformLocation =
when defined js:
webgl.getUniformLocation(gl, program.p, name.cstring)
else:
glGetUniformLocation(program.p, name.cstring)
proc uniform4fv*(gl:ContextGL, loc:UniformLocation, val:openarray[float|float32]) =
when defined js:
webgl.uniform4fv(gl, loc,val.toJSA)
else:
glUniform4fv(GLInt(loc), GLSizei(1), cast[ptr GLfloat](unsafeAddr(val)))
proc uniformMatrix4fv*(gl:ContextGL, loc:UniformLocation,transp:bool, val:openarray[float|float32]) =
when defined js:
webgl.uniformMatrix4fv(gl, loc,transp,val.toJSA)
else:
glUniformMatrix4fv(GLInt(loc), 1, transp , cast[ptr GLfloat](unsafeAddr(val[0])))
proc bindColor*(gl:ContextGL,program:Program,colorname:string,color:Color) =
let uloc = gl.getUniformLocation(program, colorname)
gl.uniform4fv(uloc, @[color.r,color.g,color.b,color.a])
proc getAttribLocation*(gl: ContextGL, program: Program, name: string): AttribLocation =
when defined js:
webgl.getAttribLocation(gl, program.p, name.cstring)
else:
let unchckresult = glGetAttribLocation(program.p, name.cstring)
doassert(unchckresult>=0, "getAttribLocation:" & $name & "not found")
result = unchckresult.GLuint
proc enableVertexAttribArray*(gl: ContextGL, attr: AttribLocation) =
when defined js:
webgl.enableVertexAttribArray(gl,attr)
else:
glEnableVertexAttribArray(attr)
proc vertexAttribPointer*(gl:ContextGL,index:AttribLocation, size:int, typ: DataKind,
normalized: bool, stride: int, offset: int64) =
when defined js:
webgl.vertexAttribPointer(gl, index, size, typ.DataType, normalized, stride, offset)
else:
glVertexAttribPointer(index, size.GLint, typ.GLenum, normalized.GLboolean,
stride.GLSizei, cast[pointer](offset))
proc enableAttribute*(gl:ContextGL,program:Program,attribname:string,itemSize:int=VECSIZE) =
## Enable the attribute `attribname` with `itemsize`
let aloc = gl.getAttribLocation(program, attribname)
gl.enableVertexAttribArray(aloc)
gl.vertexAttribPointer(aloc, itemSize, dkFloat, false, 0, 0)
proc uploadVertices*(gl:ContextGL, buff:Buffer, vertices:seq[float], drawMode:DrawMode=dmStatic) =
## Bind vertices to the context
## drawMode must be one of : # TODO: check this
## STATIC_DRAW: Contents of the buffer are likely to be used often and not change often. Contents are written to the buffer, but not read.
## DYNAMIC_DRAW: Contents of the buffer are likely to be used often and change often. Contents are written to the buffer, but not read.
## STREAM_DRAW: Contents of the buffer are likely to not be used often. Contents are written to the buffer, but not read.
gl.bindBuffer(bkArray, buff)
gl.bufferData(bkArray, vertices, drawMode)
gl.bindBuffer(bkArray, buff)
proc drawArrays*(gl:ContextGL, mode:PrimitiveKind, first:int, count:int) =
when defined js:
webgl.drawArrays(gl, mode.PrimitiveMode,first,count)
else:
glDrawArrays(mode.GLenum, first.GLint, count.GLsizei)
proc drawElements*(gl:ContextGL, mode:PrimitiveKind, count:int, kind:DataKind,offset: int64) =
doAssert( kind in {dkUByte, dkUShort, dkUInt} )
when defined js:
webgl.drawElements(gl, mode.PrimitiveMode,count, kind.DataType, offset)
else:
glDrawElements(mode.GLenum, count.GLsizei, kind.GLenum, cast[pointer](offset))
proc flush*(gl:ContextGL) =
when defined js:
webgl.flush(gl)
else:
glFlush()