1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
9 # http://www.apache.org/licenses/LICENSE-2.0
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
17 # OpenGL graphics rendering library for embedded systems, version 2.0
19 # This is a low-level wrapper, it can be useful for developers already familiar
20 # with the C API of OpenGL. Most developers will prefer to use higher level
21 # wrappers such as `mnit` and `gammit`.
23 # Defines the annotations `glsl_vertex_shader` and `glsl_fragment_shader`
24 # applicable on string literals to check shader code using `glslangValidator`.
25 # The tool must be in PATH. It can be downloaded from
26 # https://www.khronos.org/opengles/sdk/tools/Reference-Compiler/
28 # Most services of this module are a direct wrapper of the underlying
29 # C library. If a method or class is not documented in Nit, refer to
30 # the official documentation by the Khronos Group at:
31 # http://www.khronos.org/opengles/sdk/docs/man/
34 new_annotation glsl_vertex_shader
35 new_annotation glsl_fragment_shader
36 ldflags
("-lGLESv2")@android
43 #include <GLES2/gl2.h>
46 # OpenGL ES program to which we attach shaders
47 extern class GLProgram `{GLuint`}
48 # Create a new program
50 # The newly created instance should be checked using `is_ok
`.
51 new `{ return glCreateProgram(); `}
53 # Set the location for the attribute by `name`
54 fun bind_attrib_location
(index
: Int, name
: String) import String.to_cstring
`{
55 GLchar *c_name = String_to_cstring(name);
56 glBindAttribLocation(self, index, c_name);
59 # Get the location of the attribute by `name`
61 # Returns `-1` if there is no active attribute named `name`.
62 fun attrib_location
(name
: String): Int import String.to_cstring
`{
63 GLchar *c_name = String_to_cstring(name);
64 return glGetAttribLocation(self, c_name);
67 # Get the location of the uniform by `name`
69 # Returns `-1` if there is no active uniform named `name`.
70 fun uniform_location
(name
: String): Int import String.to_cstring
`{
71 GLchar *c_name = String_to_cstring(name);
72 return glGetUniformLocation(self, c_name);
75 # Query information on this program
76 fun query
(pname
: Int): Int `{
78 glGetProgramiv(self, pname, &val);
82 # Is this program linked?
83 fun is_linked
: Bool do return query
(0x8B82) != 0
85 # Has this program been deleted?
86 fun is_deleted
: Bool do return query
(0x8B80) != 0
88 # Boolean result of `validate`, must be called after `validate`
89 fun is_validated
: Bool do return query
(0x8B83) != 0
91 # Retrieve the information log of this program
93 # Useful with `link` and `validate`
94 fun info_log
: String import NativeString.to_s
`{
96 glGetProgramiv(self, GL_INFO_LOG_LENGTH, &size);
97 GLchar *msg = malloc(size);
98 glGetProgramInfoLog(self, size, NULL, msg);
99 return NativeString_to_s(msg);
102 # Number of active uniform in this program
104 # This should be the number of uniforms declared in all shader, except
105 # unused uniforms which may have been optimized out.
106 fun n_active_uniforms
: Int do return query
(0x8B86)
108 # Length of the longest uniform name in this program, including `\n`
109 fun active_uniform_max_length
: Int do return query
(0x8B87)
111 # Number of active attributes in this program
113 # This should be the number of uniforms declared in all shader, except
114 # unused uniforms which may have been optimized out.
115 fun n_active_attributes
: Int do return query
(0x8B89)
117 # Length of the longest uniform name in this program, including `\n`
118 fun active_attribute_max_length
: Int do return query
(0x8B8A)
120 # Number of shaders attached to this program
121 fun n_attached_shaders
: Int do return query
(0x8B85)
123 # Name of the active attribute at `index`
124 fun active_attrib_name
(index
: Int): String
126 var max_size
= active_attribute_max_length
127 return active_attrib_name_native
(index
, max_size
).to_s
129 private fun active_attrib_name_native
(index
, max_size
: Int): NativeString `{
130 // We get more values than we need, for compatibility. At least the
131 // NVidia driver tries to fill them even if NULL.
133 char *name = malloc(max_size);
136 glGetActiveAttrib(self, index, max_size, NULL, &size, &type, name);
140 # Size of the active attribute at `index`
141 fun active_attrib_size
(index
: Int): Int `{
144 glGetActiveAttrib(self, index, 0, NULL, &size, &type, NULL);
148 # Type of the active attribute at `index`
150 # May only be float related data types (single float, vectors and matrix).
151 fun active_attrib_type
(index
: Int): GLFloatDataType `{
154 glGetActiveAttrib(self, index, 0, NULL, &size, &type, NULL);
158 # Name of the active uniform at `index`
159 fun active_uniform_name
(index
: Int): String
161 var max_size
= active_attribute_max_length
162 return active_uniform_name_native
(index
, max_size
).to_s
164 private fun active_uniform_name_native
(index
, max_size
: Int): NativeString `{
165 char *name = malloc(max_size);
168 glGetActiveUniform(self, index, max_size, NULL, &size, &type, name);
172 # Size of the active uniform at `index`
173 fun active_uniform_size
(index
: Int): Int `{
176 glGetActiveUniform(self, index, 0, NULL, &size, &type, NULL);
180 # Type of the active uniform at `index`
182 # May be any data type supported by OpenGL ES 2.0 shaders.
183 fun active_uniform_type
(index
: Int): GLDataType `{
186 glGetActiveUniform(self, index, 0, NULL, &size, &type, NULL);
191 # Create a program object
192 fun glCreateProgram
: GLProgram `{ return glCreateProgram(); `}
194 # Install the `program
` as part of current rendering state
195 fun glUseProgram(program: GLProgram) `{ glUseProgram(program); `}
197 # Link the `program` object
198 fun glLinkProgram
(program
: GLProgram) `{ glLinkProgram(program); `}
200 # Validate the `program
` object
201 fun glValidateProgram(program: GLProgram) `{ glValidateProgram(program); `}
203 # Delete the `program` object
204 fun glDeleteProgram
(program
: GLProgram) `{ glDeleteProgram(program); `}
206 # Determine if `name
` corresponds to a program object
207 fun glIsProgram(name: GLProgram): Bool `{ return glIsProgram(name); `}
209 # Attach a `shader` to `program`
210 fun glAttachShader
(program
: GLProgram, shader
: GLShader) `{ glAttachShader(program, shader); `}
212 # Detach `shader
` from `program
`
213 fun glDetachShader(program: GLProgram, shader: GLShader) `{ glDetachShader(program, shader); `}
215 # Abstract OpenGL ES shader object, implemented by `GLFragmentShader` and `GLVertexShader`
216 extern class GLShader `{GLuint`}
218 # Source of the shader, if available
220 # Returns `null` if the source is not available, usually when the shader
221 # was created from a binary file.
222 fun source: nullable String
224 var size = query(0x8B88)
225 if size == 0 then return null
226 return source_native(size).to_s
229 private fun source_native(size: Int): NativeString `{
230 GLchar *code
= malloc
(size
);
231 glGetShaderSource
(self, size
, NULL, code
);
235 # Query information on this shader
236 protected fun query(pname: Int): Int `{
238 glGetShaderiv
(self, pname
, &val
);
242 # Has this shader been compiled?
243 fun is_compiled: Bool do return query(0x8B81) != 0
245 # Has this shader been deleted?
246 fun is_deleted: Bool do return query(0x8B80) != 0
248 # Retrieve the information log of this shader
250 # Useful with `link
` and `validate
`
251 fun info_log: String import NativeString.to_s `{
253 glGetShaderiv
(self, GL_INFO_LOG_LENGTH, &size
);
254 GLchar *msg
= malloc
(size
);
255 glGetShaderInfoLog
(self, size
, NULL, msg
);
256 return NativeString_to_s(msg
);
261 extern class GLShaderType
265 fun gl_VERTEX_SHADER: GLShaderType `{ return GL_VERTEX_SHADER; `}
266 fun gl_FRAGMENT_SHADER
: GLShaderType `{ return GL_FRAGMENT_SHADER; `}
268 # Create a shader object of the `shader_type
`
269 fun glCreateShader(shader_type: GLShaderType): GLShader `{
270 return glCreateShader
(shader_type
);
273 # Replace the source code in the `shader
` object with `code
`
274 fun glShaderSource(shader: GLShader, code: NativeString) `{
275 glShaderSource
(shader
, 1, (GLchar const
**)&code
, NULL);
278 # Compile the `shader
` object
279 fun glCompileShader(shader: GLShader) `{ glCompileShader(shader); `}
281 # Delete the `shader` object
282 fun glDeleteShader
(shader
: GLShader) `{ glDeleteShader(shader); `}
284 # Determine if `name
` corresponds to a shader object
285 fun glIsShader(name: GLShader): Bool `{ return glIsShader(name); `}
287 # An OpenGL ES 2.0 fragment shader
288 extern class GLFragmentShader
291 # Create a new fragment shader
293 # The newly created instance should be checked using `is_ok`.
294 new `{ return glCreateShader(GL_FRAGMENT_SHADER); `}
297 # An OpenGL ES 2.0 vertex shader
298 extern class GLVertexShader
301 # Create a new fragment shader
303 # The newly created instance should be checked using `is_ok
`.
304 new `{ return glCreateShader(GL_VERTEX_SHADER); `}
307 # An array of `Float` associated to a program variable
311 # Number of data per vertex
314 protected var glfloat_array
: NativeGLfloatArray
316 init(index
, count
: Int, array
: Array[Float])
320 self.glfloat_array
= new NativeGLfloatArray(array
.length
)
321 for k
in [0..array
.length
[ do
322 glfloat_array
[k
] = array
[k
]
326 fun attrib_pointer
do attrib_pointer_intern
(index
, count
, glfloat_array
)
327 private fun attrib_pointer_intern
(index
, count
: Int, array
: NativeGLfloatArray) `{
328 glVertexAttribPointer(index, count, GL_FLOAT, GL_FALSE, 0, array);
331 # Enable this vertex attribute array
332 fun enable
do glEnableVertexAttribArray
(index
)
334 # Disable this vertex attribute array
335 fun disable
do glDisableVertexAttribArray
(index
)
338 # Enable the generic vertex attribute array at `index`
339 fun glEnableVertexAttribArray
(index
: Int) `{ glEnableVertexAttribArray(index); `}
341 # Disable the generic vertex attribute array at `index
`
342 fun glDisableVertexAttribArray(index: Int) `{ glDisableVertexAttribArray(index); `}
344 # Render primitives from array data
345 fun glDrawArrays
(mode
: GLDrawMode, from
, count
: Int) `{ glDrawArrays(mode, from, count); `}
347 # Define an array of generic vertex attribute data
348 fun glVertexAttribPointer(index, size: Int, typ: GLDataType, normalized: Bool, stride: Int, array: NativeGLfloatArray) `{
349 glVertexAttribPointer
(index
, size
, typ
, normalized
, stride
, array
);
352 # Specify the value of a generic vertex attribute
353 fun glVertexAttrib1f(index: Int, x: Float) `{ glVertexAttrib1f(index, x); `}
355 # Specify the value of a generic vertex attribute
356 fun glVertexAttrib2f
(index
: Int, x
, y
: Float) `{ glVertexAttrib2f(index, x, y); `}
358 # Specify the value of a generic vertex attribute
359 fun glVertexAttrib3f(index: Int, x, y, z: Float) `{ glVertexAttrib3f(index, x, y, z); `}
361 # Specify the value of a generic vertex attribute
362 fun glVertexAttrib4f
(index
: Int, x
, y
, z
, w
: Float) `{ glVertexAttrib4f(index, x, y, z, w); `}
364 # Specify the value of a uniform variable for the current program object
365 fun glUniform1i(index, x: Int) `{ glUniform1i(index, x); `}
367 # Specify the value of a uniform variable for the current program object
368 fun glUniform2i
(index
, x
, y
: Int) `{ glUniform2i(index, x, y); `}
370 # Specify the value of a uniform variable for the current program object
371 fun glUniform3i(index, x, y, z: Int) `{ glUniform3i(index, x, y, z); `}
373 # Specify the value of a uniform variable for the current program object
374 fun glUniform4i
(index
, x
, y
, z
, w
: Int) `{ glUniform4i(index, x, y, z, w); `}
378 # Low level array of `Float`
381 redef type NATIVE: NativeGLfloatArray
383 init do native_array = new NativeGLfloatArray(length)
385 # Create with the content of `array
`
386 new from(array: Array[Float])
388 var arr = new GLfloatArray(array.length)
393 # Fill with the content of `array
`
394 fun fill_from(array: Array[Float])
396 assert length >= array.length
397 for k in [0..array.length[ do
403 # An array of `GLfloat` in C (`GLfloat*`)
404 extern class NativeGLfloatArray `{ GLfloat* `}
408 new(size
: Int) `{ return calloc(size, sizeof(GLfloat)); `}
410 redef fun [](index) `{ return self[index]; `}
411 redef fun []=(index
, val
) `{ self[index] = val; `}
413 redef fun +(offset) `{ return self + offset; `}
416 # General type for OpenGL enumerations
417 extern class GLEnum `{ GLenum `}
419 redef fun hash `{ return self; `}
421 redef fun ==(o
) do return o
!= null and is_same_type
(o
) and o
.hash
== self.hash
425 fun glGetError
: GLError `{ return glGetError(); `}
427 # An OpenGL ES 2.0 error code
433 if self == gl_NO_ERROR then return "No error"
434 if self == gl_INVALID_ENUM then return "Invalid enum"
435 if self == gl_INVALID_VALUE then return "Invalid value"
436 if self == gl_INVALID_OPERATION then return "Invalid operation"
437 if self == gl_INVALID_FRAMEBUFFER_OPERATION then return "invalid framebuffer operation"
438 if self == gl_OUT_OF_MEMORY then return "Out of memory"
439 return "Unknown error"
443 fun gl_NO_ERROR: GLError `{ return GL_NO_ERROR; `}
444 fun gl_INVALID_ENUM
: GLError `{ return GL_INVALID_ENUM; `}
445 fun gl_INVALID_VALUE: GLError `{ return GL_INVALID_VALUE; `}
446 fun gl_INVALID_OPERATION
: GLError `{ return GL_INVALID_OPERATION; `}
447 fun gl_INVALID_FRAMEBUFFER_OPERATION: GLError `{ return GL_INVALID_FRAMEBUFFER_OPERATION; `}
448 fun gl_OUT_OF_MEMORY
: GLError `{ return GL_OUT_OF_MEMORY; `}
450 fun assert_no_gl_error
452 var error = glGetError
453 if not error == gl_NO_ERROR then
454 print "GL error: {error}"
459 # Texture unit, the number of texture units is implementation dependent
460 extern class GLTextureUnit
464 fun gl_TEXTURE0: GLTextureUnit `{ return GL_TEXTURE0; `}
465 fun gl_TEXTURE1
: GLTextureUnit `{ return GL_TEXTURE1; `}
466 fun gl_TEXTURE2: GLTextureUnit `{ return GL_TEXTURE2; `}
467 fun gl_TEXTURE3
: GLTextureUnit `{ return GL_TEXTURE3; `}
468 fun gl_TEXTURE4: GLTextureUnit `{ return GL_TEXTURE4; `}
469 fun gl_TEXTURE5
: GLTextureUnit `{ return GL_TEXTURE5; `}
470 fun gl_TEXTURE6: GLTextureUnit `{ return GL_TEXTURE6; `}
471 fun gl_TEXTURE7
: GLTextureUnit `{ return GL_TEXTURE7; `}
472 fun gl_TEXTURE8: GLTextureUnit `{ return GL_TEXTURE8; `}
473 fun gl_TEXTURE9
: GLTextureUnit `{ return GL_TEXTURE9; `}
474 fun gl_TEXTURE10: GLTextureUnit `{ return GL_TEXTURE10; `}
475 fun gl_TEXTURE11
: GLTextureUnit `{ return GL_TEXTURE11; `}
476 fun gl_TEXTURE12: GLTextureUnit `{ return GL_TEXTURE12; `}
477 fun gl_TEXTURE13
: GLTextureUnit `{ return GL_TEXTURE13; `}
478 fun gl_TEXTURE14: GLTextureUnit `{ return GL_TEXTURE14; `}
479 fun gl_TEXTURE15
: GLTextureUnit `{ return GL_TEXTURE15; `}
480 fun gl_TEXTURE16: GLTextureUnit `{ return GL_TEXTURE16; `}
481 fun gl_TEXTURE17
: GLTextureUnit `{ return GL_TEXTURE17; `}
482 fun gl_TEXTURE18: GLTextureUnit `{ return GL_TEXTURE18; `}
483 fun gl_TEXTURE19
: GLTextureUnit `{ return GL_TEXTURE19; `}
484 fun gl_TEXTURE20: GLTextureUnit `{ return GL_TEXTURE20; `}
485 fun gl_TEXTURE21
: GLTextureUnit `{ return GL_TEXTURE21; `}
486 fun gl_TEXTURE22: GLTextureUnit `{ return GL_TEXTURE22; `}
487 fun gl_TEXTURE23
: GLTextureUnit `{ return GL_TEXTURE23; `}
488 fun gl_TEXTURE24: GLTextureUnit `{ return GL_TEXTURE24; `}
489 fun gl_TEXTURE25
: GLTextureUnit `{ return GL_TEXTURE25; `}
490 fun gl_TEXTURE26: GLTextureUnit `{ return GL_TEXTURE26; `}
491 fun gl_TEXTURE27
: GLTextureUnit `{ return GL_TEXTURE27; `}
492 fun gl_TEXTURE28: GLTextureUnit `{ return GL_TEXTURE28; `}
493 fun gl_TEXTURE29
: GLTextureUnit `{ return GL_TEXTURE29; `}
494 fun gl_TEXTURE30: GLTextureUnit `{ return GL_TEXTURE30; `}
495 fun gl_TEXTURE31
: GLTextureUnit `{ return GL_TEXTURE31; `}
497 # Texture unit at `offset
` after `gl_TEXTURE0
`
498 fun gl_TEXTURE(offset: Int): GLTextureUnit `{ return GL_TEXTURE0 + offset; `}
500 # Generate `n` texture names
501 fun glGenTextures
(n
: Int): Array[Int]
503 var array
= new CIntArray(n
)
504 native_glGenTextures
(n
, array
.native_array
)
510 private fun native_glGenTextures
(n
: Int, textures
: NativeCIntArray) `{
511 glGenTextures(n, (GLuint*)textures);
514 # Select server-side active texture unit
515 fun glActiveTexture
(texture
: GLTextureUnit) `{ glActiveTexture(texture); `}
517 # Bind the named `texture
` to a `target
`
518 fun glBindTexture(target: GLTextureTarget, texture: Int) `{
519 glBindTexture
(target
, texture
);
522 # Delete named textures
523 fun glDeleteTextures(textures: SequenceRead[Int])
525 var n = textures.length
526 var array = new CIntArray.from(textures)
527 native_glDeleteTextures(n, array.native_array)
531 private fun native_glDeleteTextures(n: Int, textures: NativeCIntArray) `{
532 glDeleteTextures
(n
, (const
GLuint *)textures
);
535 # Does `name
` corresponds to a texture?
536 fun glIsTexture(name: Int): Bool `{ return glIsTexture(name); `}
538 # Set pixel storage modes
539 fun glPixelStorei
(parameter
: GLPack, val
: Int) `{ glPixelStorei(parameter, val); `}
541 # Symbolic name of the parameter to be set with `glPixelStorei
`
546 # Parameter to specify the alignment requirements for the start of each pixel row in memory
547 fun gl_PACK_ALIGNEMENT: GLPack `{ return GL_PACK_ALIGNMENT; `}
549 # Parameter to specify the alignment requirements for the start of each pixel row in memory
550 fun gl_UNPACK_ALIGNEMENT
: GLPack `{ return GL_UNPACK_ALIGNMENT; `}
552 # TODO GL_PACK_ROW_LENGTH, GL_PACK_IMAGE_HEIGHT, GL_PACK_SKIP_PIXELS, GL_PACK_SKIP_ROWS, GL_PACK_SKIP_IMAGES
553 # GL_UNPACK_ROW_LENGTH, GL_UNPACK_IMAGE_HEIGHT, GL_UNPACK_SKIP_PIXELS, GL_UNPACK_SKIP_ROWS, GL_UNPACK_SKIP_IMAGES
555 # Specify a two-dimensional texture image
556 fun glTexImage2D(target: GLTextureTarget, level: Int, internalformat: GLPixelFormat,
557 width, height, border: Int,
558 format: GLPixelFormat, typ: GLPixelType, data: Pointer) `{
559 glTexImage2D
(target
, level
, internalformat
, width
, height
, border
, format
, typ
, data
);
562 # Specify a two-dimensional texture subimage
563 fun glTexSubImage2D(target: GLTextureTarget,
564 level, xoffset, yoffset, width, height, border: Int,
565 format: GLPixelFormat, typ: GLPixelType, data: Pointer) `{
566 glTexSubImage2D
(target
, level
, xoffset
, yoffset
, width
, height
, format
, typ
, data
);
569 # Copy pixels into a 2D texture image
570 fun glCopyTexImage2D(target: GLTextureTarget, level: Int, internalformat: GLPixelFormat,
571 x, y, width, height, border: Int) `{
572 glCopyTexImage2D
(target
, level
, internalformat
, x
, y
, width
, height
, border
);
575 # Copy a two-dimensional texture subimage
576 fun glCopyTexSubImage2D(target: GLTextureTarget, level, xoffset, yoffset, x, y, width, height: Int) `{
577 glCopyTexSubImage2D
(target
, level
, xoffset
, yoffset
, x
, y
, width
, height
);
580 # Copy a block of pixels from the framebuffer of `fomat
` and `typ
` at `data
`
581 fun glReadPixels(x, y, width, height: Int, format: GLPixelFormat, typ: GLPixelType, data: Pointer) `{
582 glReadPixels
(x
, y
, width
, height
, format
, typ
, data
);
585 # Texture minifying and magnifying function
586 extern class GLTexParameteri
590 fun gl_NEAREST: GLTexParameteri `{ return GL_NEAREST; `}
591 fun gl_LINEAR
: GLTexParameteri `{ return GL_LINEAR; `}
592 fun gl_NEAREST_MIPMAP_NEAREST: GLTexParameteri `{ return GL_NEAREST_MIPMAP_NEAREST; `}
593 fun gl_LINEAR_MIPMAP_NEAREST
: GLTexParameteri `{ return GL_LINEAR_MIPMAP_NEAREST; `}
594 fun gl_NEAREST_MIPMAP_NINEAR: GLTexParameteri `{ return GL_NEAREST_MIPMAP_LINEAR; `}
595 fun gl_LINEAR_MIPMAP_LINEAR
: GLTexParameteri `{ return GL_LINEAR_MIPMAP_LINEAR; `}
596 fun gl_CLAMP_TO_EDGE: GLTexParameteri `{ return GL_CLAMP_TO_EDGE; `}
597 fun gl_MIRRORED_REPEAT
: GLTexParameteri `{ return GL_MIRRORED_REPEAT; `}
598 fun gl_REPEAT: GLTexParameteri `{ return GL_REPEAT; `}
601 extern class GLTextureTarget
605 # Two-dimensional texture
606 fun gl_TEXTURE_2D
: GLTextureTarget `{ return GL_TEXTURE_2D; `}
609 fun gl_TEXTURE_CUBE_MAP: GLTextureTarget `{ return GL_TEXTURE_CUBE_MAP; `}
611 # TODO GL_TEXTURE_CUBE_MAP_POSITIVE_X, GL_TEXTURE_CUBE_MAP_NEGATIVE_X, GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
612 # GL_TEXTURE_CUBE_MAP_NEGATIVE_Y, GL_TEXTURE_CUBE_MAP_POSITIVE_Z, GL_TEXTURE_CUBE_MAP_NEGATIVE_Z
614 # A server-side capability
619 # Internal OpenGL integer for this capability
622 # Enable this server-side capability
623 fun enable
do enable_native
(val
)
624 private fun enable_native
(cap
: Int) `{ glEnable(cap); `}
626 # Disable this server-side capability
627 fun disable do disable_native(val)
628 private fun disable_native(cap: Int) `{ glDisable(cap); `}
630 redef fun hash
do return val
631 redef fun ==(o
) do return o
!= null and is_same_type
(o
) and o
.hash
== self.hash
634 # Generate `n` renderbuffer object names
635 fun glGenRenderbuffers
(n
: Int): Array[Int]
637 var array
= new CIntArray(n
)
638 native_glGenRenderbuffers
(n
, array
.native_array
)
644 private fun native_glGenRenderbuffers
(n
: Int, renderbuffers
: NativeCIntArray) `{
645 glGenRenderbuffers(n, (GLuint *)renderbuffers);
648 # Determine if `name` corresponds to a renderbuffer object
649 fun glIsRenderbuffer
(name
: Int): Bool `{
650 return glIsRenderbuffer(name);
653 # Delete named renderbuffer objects
654 fun glDeleteRenderbuffers
(renderbuffers
: SequenceRead[Int])
656 var n
= renderbuffers
.length
657 var array
= new CIntArray.from
(renderbuffers
)
658 native_glDeleteRenderbuffers
(n
, array
.native_array
)
662 private fun native_glDeleteRenderbuffers
(n
: Int, renderbuffers
: NativeCIntArray) `{
663 return glDeleteRenderbuffers(n, (const GLuint *)renderbuffers);
666 # Attach a renderbuffer object to a framebuffer object
667 fun glFramebufferRenderbuffer
(target
: GLFramebufferTarget, attachment
: GLAttachment,
668 renderbuffertarget
: GLRenderbufferTarget, renderbuffer
: Int) `{
669 glFramebufferRenderbuffer(target, attachment, renderbuffertarget, renderbuffer);
672 # Establish data storage, `format` and dimensions of the `target` renderbuffer object's image
673 fun glRenderbufferStorage
(target
: GLRenderbufferTarget, format
: GLRenderbufferFormat, width
, height
: Int) `{
674 glRenderbufferStorage(GL_RENDERBUFFER, format, width, height);
677 # Format for a renderbuffer
678 extern class GLRenderbufferFormat
682 # 4 red, 4 green, 4 blue, 4 alpha bits format
683 fun gl_RGBA4
: GLRenderbufferFormat `{ return GL_RGBA4; `}
685 # 5 red, 6 green, 5 blue bits format
686 fun gl_RGB565: GLRenderbufferFormat `{ return GL_RGB565; `}
688 # 5 red, 5 green, 5 blue, 1 alpha bits format
689 fun gl_RGB_A1
: GLRenderbufferFormat `{ return GL_RGB5_A1; `}
691 # 16 depth bits format
692 fun gl_DEPTH_COMPNENT16: GLRenderbufferFormat `{ return GL_DEPTH_COMPONENT16; `}
694 # 8 stencil bits format
695 fun gl_STENCIL_INDEX8
: GLRenderbufferFormat `{ return GL_STENCIL_INDEX8; `}
697 # Renderbuffer attachment point to a framebuffer
698 extern class GLAttachment
702 # First color attachment point
703 fun gl_COLOR_ATTACHMENT0: GLAttachment `{ return GL_COLOR_ATTACHMENT0; `}
705 # Depth attachment point
706 fun gl_DEPTH_ATTACHMENT
: GLAttachment `{ return GL_DEPTH_ATTACHMENT; `}
709 fun gl_STENCIL_ATTACHMENT: GLAttachment `{ return GL_STENCIL_ATTACHMENT; `}
712 private var gles
= new GLES is lazy
715 # Entry points to OpenGL ES 2.0 services
716 fun gl
: GLES do return sys
.gles
718 # OpenGL ES 2.0 services
721 # Query the boolean value at `key`
722 private fun get_bool
(key
: Int): Bool `{
724 glGetBooleanv(key, &val);
725 return val == GL_TRUE;
728 # Query the floating point value at `key`
729 private fun get_float
(key
: Int): Float `{
731 glGetFloatv(key, &val);
735 # Query the integer value at `key`
736 private fun get_int
(key
: Int): Int `{
738 glGetIntegerv(key, &val);
742 # Does this driver support shader compilation?
744 # Should always return `true` in OpenGL ES 2.0 and 3.0.
745 fun shader_compiler
: Bool do return get_bool
(0x8DFA)
747 # OpenGL server-side capabilities
748 var capabilities
= new GLCapabilities is lazy
751 # Specify the clear values for the color buffer, default values are at 0.0
752 fun glClearColor
(red
, green
, blue
, alpha
: Float) `{
753 glClearColor(red, green, blue, alpha);
756 # Specify the clear `value` for the depth buffer, default at 1.0
757 fun glClearDepthf
(value
: Float) `{ glClearDepthf(value); `}
759 # Specify the clear `value
` for the stencil buffer, default at 0
760 fun glClearStencil(value: Int) `{ glClearStencil(value); `}
763 fun glClear
(buffer
: GLBuffer) `{ glClear(buffer); `}
765 # Enable and disable writing of frame buffer color components
766 fun glColorMask(red, green, blue, alpha: Bool) `{ glColorMask(red, green, blue, alpha); `}
769 fun glViewport
(x
, y
, width
, height
: Int) `{ glViewport(x, y, width, height); `}
771 # Block until all GL execution is complete
772 fun glFinish `{ glFinish(); `}
774 # Force execution of GL commands in finite time
775 fun glFlush
`{ glFlush(); `}
777 # Set texture parameters
778 fun glTexParameteri(target: GLTextureTarget, pname: GLTexParameteriName, param: GLTexParameteri) `{
779 glTexParameteri
(target
, pname
, param
);
782 # Name of parameters of textures
783 extern class GLTexParameteriName
787 fun gl_TEXTURE_MIN_FILTER: GLTexParameteriName `{ return GL_TEXTURE_MIN_FILTER; `}
788 fun gl_TEXTURE_MAG_FILTER
: GLTexParameteriName `{ return GL_TEXTURE_MAG_FILTER; `}
789 fun gl_TEXTURE_WRAP_S: GLTexParameteriName `{ return GL_TEXTURE_WRAP_S; `}
790 fun gl_TEXTURE_WRAP_T
: GLTexParameteriName `{ return GL_TEXTURE_WRAP_T; `}
792 # Bind `framebuffer
` to a framebuffer target
794 # In OpenGL ES 2.0, `target
` must be `gl_FRAMEBUFFER
`.
795 fun glBindFramebuffer(target: GLFramebufferTarget, framebuffer: Int) `{
796 glBindFramebuffer
(target
, framebuffer
);
799 # Target of `glBindFramebuffer
`
800 extern class GLFramebufferTarget
804 # Target both reading and writing on the framebuffer with `glBindFramebuffer
`
805 fun gl_FRAMEBUFFER: GLFramebufferTarget `{ return GL_FRAMEBUFFER; `}
807 # Bind `renderbuffer` to a renderbuffer target
809 # In OpenGL ES 2.0, `target` must be `gl_RENDERBUFFER`.
810 fun glBindRenderbuffer
(target
: GLRenderbufferTarget, renderbuffer
: Int) `{
811 glBindRenderbuffer(target, renderbuffer);
814 # Target of `glBindRenderbuffer`
815 extern class GLRenderbufferTarget
819 # Target a renderbuffer with `glBindRenderbuffer`
820 fun gl_RENDERBUFFER
: GLRenderbufferTarget `{ return GL_RENDERBUFFER; `}
822 # Specify implementation specific hints
823 fun glHint(target: GLHintTarget, mode: GLHintMode) `{
824 glHint
(target
, mode
);
827 # Generate and fill set of mipmaps for the texture object `target
`
828 fun glGenerateMipmap(target: GLTextureTarget) `{ glGenerateMipmap(target); `}
830 # Bind the named `buffer` object
831 fun glBindBuffer
(target
: GLArrayBuffer, buffer
: Int) `{ glBindBuffer(target, buffer); `}
833 # Target to which bind the buffer with `glBindBuffer
`
834 extern class GLArrayBuffer
838 # Array buffer target
839 fun gl_ARRAY_BUFFER: GLArrayBuffer `{ return GL_ARRAY_BUFFER; `}
841 # Element array buffer
842 fun gl_ELEMENT_ARRAY_BUFFER
: GLArrayBuffer `{ return GL_ELEMENT_ARRAY_BUFFER; `}
844 # Completeness status of a framebuffer object
845 fun glCheckFramebufferStatus(target: GLFramebufferTarget): GLFramebufferStatus `{
846 return glCheckFramebufferStatus
(target
);
849 # Return value of `glCheckFramebufferStatus
`
850 extern class GLFramebufferStatus
855 if self == gl_FRAMEBUFFER_COMPLETE then return "complete"
856 if self == gl_FRAMEBUFFER_INCOMPLETE_ATTACHMENT then return "incomplete attachment"
857 if self == gl_FRAMEBUFFER_INCOMPLETE_DIMENSIONS then return "incomplete dimension"
858 if self == gl_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT then return "incomplete missing attachment"
859 if self == gl_FRAMEBUFFER_UNSUPPORTED then return "unsupported"
864 # The framebuffer is complete
865 fun gl_FRAMEBUFFER_COMPLETE: GLFramebufferStatus `{
866 return GL_FRAMEBUFFER_COMPLETE;
869 # Not all framebuffer attachment points are framebuffer attachment complete
870 fun gl_FRAMEBUFFER_INCOMPLETE_ATTACHMENT: GLFramebufferStatus `{
871 return GL_FRAMEBUFFER_INCOMPLETE_ATTACHMENT;
874 # Not all attached images have the same width and height
875 fun gl_FRAMEBUFFER_INCOMPLETE_DIMENSIONS: GLFramebufferStatus `{
876 return GL_FRAMEBUFFER_INCOMPLETE_DIMENSIONS;
879 # No images are attached to the framebuffer
880 fun gl_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT: GLFramebufferStatus `{
881 return GL_FRAMEBUFFER_INCOMPLETE_MISSING_ATTACHMENT;
884 # The combination of internal formats of the attached images violates an implementation-dependent set of restrictions
885 fun gl_FRAMEBUFFER_UNSUPPORTED: GLFramebufferStatus `{
886 return GL_FRAMEBUFFER_UNSUPPORTED;
889 # Hint target for `glHint
`
890 extern class GLHintTarget
894 # Indicates the quality of filtering when generating mipmap images
895 fun gl_GENERATE_MIPMAP_HINT: GLHintTarget `{ return GL_GENERATE_MIPMAP_HINT; `}
897 # Hint mode for `glHint`
898 extern class GLHintMode
902 # The most efficient option should be chosen
903 fun gl_FASTEST
: GLHintMode `{ return GL_FASTEST; `}
905 # The most correct, or highest quality, option should be chosen
906 fun gl_NICEST: GLHintMode `{ return GL_NICEST; `}
909 fun gl_DONT_CARE
: GLHintMode `{ return GL_DONT_CARE; `}
911 # Generate `n
` framebuffer object names
912 fun glGenFramebuffers(n: Int): Array[Int]
914 var array = new CIntArray(n)
915 native_glGenFramebuffers(n, array.native_array)
921 private fun native_glGenFramebuffers(n: Int, textures: NativeCIntArray) `{
922 glGenFramebuffers
(n
, (GLuint *)textures
);
925 # Determine if `name
` corresponds to a framebuffer object
926 fun glIsFramebuffer(name: Int): Bool `{
927 return glIsFramebuffer
(name
);
930 # Delete named framebuffer objects
931 fun glDeleteFramebuffers(framebuffers: SequenceRead[Int])
933 var n = framebuffers.length
934 var array = new CIntArray.from(framebuffers)
935 native_glDeleteFramebuffers(n, array.native_array)
939 private fun native_glDeleteFramebuffers(n: Int, framebuffers: NativeCIntArray) `{
940 return glDeleteFramebuffers
(n
, (const
GLuint *)framebuffers
);
943 # Attach a level of a texture object as a logical buffer to the currently bound framebuffer object
944 fun glFramebufferTexture2D(target: GLFramebufferTarget, attachment: GLAttachment,
945 texture_target: GLTextureTarget, texture, level: Int) `{
946 glFramebufferTexture2D
(target
, attachment
, texture_target
, texture
, level
);
949 # Entry point to OpenGL server-side capabilities
952 # GL capability: blend the computed fragment color values
955 var blend: GLCap is lazy do return new GLCap(0x0BE2)
957 # GL capability: cull polygons based of their winding in window coordinates
959 # Foreign: GL_CULL_FACE
960 var cull_face: GLCap is lazy do return new GLCap(0x0B44)
962 # GL capability: do depth comparisons and update the depth buffer
964 # Foreign: GL_DEPTH_TEST
965 var depth_test: GLCap is lazy do return new GLCap(0x0B71)
967 # GL capability: dither color components or indices before they are written to the color buffer
970 var dither: GLCap is lazy do return new GLCap(0x0BE2)
972 # GL capability: add an offset to depth values of a polygon fragment before depth test
974 # Foreign: GL_POLYGON_OFFSET_FILL
975 var polygon_offset_fill: GLCap is lazy do return new GLCap(0x8037)
977 # GL capability: compute a temporary coverage value where each bit is determined by the alpha value at the corresponding location
979 # Foreign: GL_SAMPLE_ALPHA_TO_COVERAGE
980 var sample_alpha_to_coverage: GLCap is lazy do return new GLCap(0x809E)
982 # GL capability: AND the fragment coverage with the temporary coverage value
984 # Foreign: GL_SAMPLE_COVERAGE
985 var sample_coverage: GLCap is lazy do return new GLCap(0x80A0)
987 # GL capability: discard fragments that are outside the scissor rectangle
989 # Foreign: GL_SCISSOR_TEST
990 var scissor_test: GLCap is lazy do return new GLCap(0x0C11)
992 # GL capability: do stencil testing and update the stencil buffer
994 # Foreign: GL_STENCIL_TEST
995 var stencil_test: GLCap is lazy do return new GLCap(0x0B90)
998 # Float related data types of OpenGL ES 2.0 shaders
1000 # Only data types supported by shader attributes, as seen with
1001 # `GLProgram::active_attrib_type
`.
1002 extern class GLFloatDataType
1005 fun is_float: Bool `{ return self == GL_FLOAT; `}
1006 fun is_float_vec2
: Bool `{ return self == GL_FLOAT_VEC2; `}
1007 fun is_float_vec3: Bool `{ return self == GL_FLOAT_VEC3; `}
1008 fun is_float_vec4
: Bool `{ return self == GL_FLOAT_VEC4; `}
1009 fun is_float_mat2: Bool `{ return self == GL_FLOAT_MAT2; `}
1010 fun is_float_mat3
: Bool `{ return self == GL_FLOAT_MAT3; `}
1011 fun is_float_mat4: Bool `{ return self == GL_FLOAT_MAT4; `}
1013 # Instances of `GLFloatDataType` can be equal to instances of `GLDataType`
1016 return o
!= null and o
isa GLFloatDataType and o
.hash
== self.hash
1020 # All data types of OpenGL ES 2.0 shaders
1022 # These types can be used by shader uniforms, as seen with
1023 # `GLProgram::active_uniform_type`.
1024 extern class GLDataType
1025 super GLFloatDataType
1027 fun is_int
: Bool `{ return self == GL_INT; `}
1028 fun is_int_vec2: Bool `{ return self == GL_INT_VEC2; `}
1029 fun is_int_vec3
: Bool `{ return self == GL_INT_VEC3; `}
1030 fun is_int_vec4: Bool `{ return self == GL_INT_VEC4; `}
1031 fun is_bool
: Bool `{ return self == GL_BOOL; `}
1032 fun is_bool_vec2: Bool `{ return self == GL_BOOL_VEC2; `}
1033 fun is_bool_vec3
: Bool `{ return self == GL_BOOL_VEC3; `}
1034 fun is_bool_vec4: Bool `{ return self == GL_BOOL_VEC4; `}
1035 fun is_sampler_2d
: Bool `{ return self == GL_SAMPLER_2D; `}
1036 fun is_sampler_cube: Bool `{ return self == GL_SAMPLER_CUBE; `}
1039 # Kind of primitives to render
1040 extern class GLDrawMode
1044 fun gl_POINTS
: GLDrawMode `{ return GL_POINTS; `}
1045 fun gl_LINES: GLDrawMode `{ return GL_LINES; `}
1046 fun gl_LINE_LOOP
: GLDrawMode `{ return GL_LINE_LOOP; `}
1047 fun gl_LINE_STRIP: GLDrawMode `{ return GL_LINE_STRIP; `}
1048 fun gl_TRIANGLES
: GLDrawMode `{ return GL_TRIANGLES; `}
1049 fun gl_TRIANGLE_STRIP: GLDrawMode `{ return GL_TRIANGLE_STRIP; `}
1050 fun gl_TRIANGLE_FAN
: GLDrawMode `{ return GL_TRIANGLE_FAN; `}
1052 # Pixel arithmetic for blending operations
1053 extern class GLBlendFactor
1057 fun gl_ZERO: GLBlendFactor `{ return GL_ZERO; `}
1058 fun gl_ONE
: GLBlendFactor `{ return GL_ONE; `}
1059 fun gl_SRC_COLOR: GLBlendFactor `{ return GL_SRC_COLOR; `}
1060 fun gl_ONE_MINUS_SRC_COLOR
: GLBlendFactor `{ return GL_ONE_MINUS_SRC_COLOR; `}
1061 fun gl_SRC_ALPHA: GLBlendFactor `{ return GL_SRC_ALPHA; `}
1062 fun gl_ONE_MINUS_SRC_ALPHA
: GLBlendFactor `{ return GL_ONE_MINUS_SRC_ALPHA; `}
1063 fun gl_DST_ALPHA: GLBlendFactor `{ return GL_DST_ALPHA; `}
1064 fun gl_ONE_MINUS_DST_ALPHA
: GLBlendFactor `{ return GL_ONE_MINUS_DST_ALPHA; `}
1065 fun gl_DST_COLOR: GLBlendFactor `{ return GL_DST_COLOR; `}
1066 fun gl_ONE_MINUS_DST_COLOR
: GLBlendFactor `{ return GL_ONE_MINUS_DST_COLOR; `}
1067 fun gl_SRC_ALPHA_SATURATE: GLBlendFactor `{ return GL_SRC_ALPHA_SATURATE; `}
1069 # Condition under which a pixel will be drawn
1070 extern class GLDepthFunc
1074 fun gl_NEVER
: GLDepthFunc `{ return GL_NEVER; `}
1075 fun gl_LESS: GLDepthFunc `{ return GL_LESS; `}
1076 fun gl_EQUAL
: GLDepthFunc `{ return GL_EQUAL; `}
1077 fun gl_LEQUAL: GLDepthFunc `{ return GL_LEQUAL; `}
1078 fun gl_GREATER
: GLDepthFunc `{ return GL_GREATER; `}
1079 fun gl_NOTEQUAL: GLDepthFunc `{ return GL_NOTEQUAL; `}
1080 fun gl_GEQUAL
: GLDepthFunc `{ return GL_GEQUAL; `}
1081 fun gl_ALWAYS: GLDepthFunc `{ return GL_ALWAYS; `}
1083 # Format of pixel data
1084 extern class GLPixelFormat
1088 fun gl_ALPHA
: GLPixelFormat `{ return GL_ALPHA; `}
1089 fun gl_RGB: GLPixelFormat `{ return GL_RGB; `}
1090 fun gl_RGBA
: GLPixelFormat `{ return GL_RGBA; `}
1092 # Data type of pixel data
1093 extern class GLPixelType
1097 fun gl_UNSIGNED_BYTE: GLPixelType `{ return GL_UNSIGNED_BYTE; `}
1098 fun gl_UNSIGNED_SHORT_5_6_5
: GLPixelType `{ return GL_UNSIGNED_SHORT_5_6_5; `}
1099 fun gl_UNSIGNED_SHORT_4_4_4_4: GLPixelType `{ return GL_UNSIGNED_SHORT_4_4_4_4; `}
1100 fun gl_UNSIGNED_SHORT_5_5_5_1
: GLPixelType `{ return GL_UNSIGNED_SHORT_5_5_5_1; `}
1102 # Set of buffers as a bitwise OR mask
1103 extern class GLBuffer `{ GLbitfield `}
1104 # Bitwise OR with `other`
1105 fun |(other
: GLBuffer): GLBuffer `{ return self | other; `}
1108 fun gl_DEPTH_BUFFER_BIT: GLBuffer `{ return GL_DEPTH_BUFFER_BIT; `}
1109 fun gl_STENCIL_BUFFER_BIT
: GLBuffer `{ return GL_STENCIL_BUFFER_BIT; `}
1110 fun gl_COLOR_BUFFER_BIT: GLBuffer `{ return GL_COLOR_BUFFER_BIT; `}
1112 # Define front- and back-facing polygons, `gc_CCW` by default
1113 fun glFrontFace
(mode
: GLFrontFaceMode) `{ glFrontFace(mode); `}
1115 # Orientation of front-facing polygons
1116 extern class GLFrontFaceMode
1120 fun gl_CW: GLFrontFaceMode `{ return GL_CW; `}
1121 fun gl_CCW
: GLFrontFaceMode `{ return GL_CCW; `}
1123 # Specify whether front- or back-facing polygons can be culled, default is `back
` only
1124 fun glCullFace(mode: GLCullFaceMode) `{ glCullFace(mode); `}
1126 # Candidates for culling
1127 extern class GLCullFaceMode
1131 fun gl_FRONT
: GLCullFaceMode `{ return GL_FRONT; `}
1132 fun gl_BACK: GLCullFaceMode `{ return GL_BACK; `}
1133 fun gl_FRONT_AND_BACK
: GLCullFaceMode `{ return GL_FRONT_AND_BACK; `}
1135 # Specify mapping of depth values from normalized device coordinates to window coordinates
1137 # Default at 0.0, 1.0.
1138 fun glDepthRangef(near, far: Float) `{ glDepthRangef(near, far); `}
1140 # Enable or disable writing into the depth buffer
1141 fun glDepthMask
(value
: Bool) `{ glDepthMask(value); `}
1143 # Specify the value used for depth buffer comparisons
1145 # Default value is `gl_LESS
`
1146 fun glDepthFunc(func: GLDepthFunc) `{ glDepthFunc(func); `}
1148 # Set the pixel arithmetic for the blending operations
1151 # * `src_factor`: `gl_ONE`
1152 # * `dst_factor`: `gl_ZERO`
1153 fun glBlendFunc
(src_factor
, dst_factor
: GLBlendFactor) `{
1154 glBlendFunc(src_factor, dst_factor);
1157 # Set the scale and units used to calculate depth values
1158 fun glPolygonOffset
(factor
, units
: Float) `{ glPolygonOffset(factor, units); `}
1160 # Specify the width of rasterized lines
1161 fun glLineWidth(width: Float) `{ glLineWidth(width); `}