1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Licensed under the Apache License, Version 2.0 (the "License");
4 # you may not use this file except in compliance with the License.
5 # You may obtain a copy of the License at
7 # http://www.apache.org/licenses/LICENSE-2.0
9 # Unless required by applicable law or agreed to in writing, software
10 # distributed under the License is distributed on an "AS IS" BASIS,
11 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 # See the License for the specific language governing permissions and
13 # limitations under the License.
15 # Example of using `GamnitDisplay` to setup a screen for custom calls to OpenGL ES 2.0
17 # This example does not support the lifecycle of mobile platforms, as such it only works on desktop computers.
20 # * The book OpenGL ES 2.0 Programming Guide
21 # * https://code.google.com/p/opengles-book-samples/source/browse/trunk/LinuxX11/Chapter_2/Hello_Triangle/Hello_Triangle.c
22 module standalone_triangle
25 import gamnit
::display
27 if "NIT_TESTING".environ
== "true" then exit
(0)
30 var display
= new GamnitDisplay
33 var width
= display
.width
34 var height
= display
.height
35 print
"Width: {width}"
36 print
"Height: {height}"
38 # Custom calls to OpenGL ES 2.0
40 assert gl
.shader_compiler
else print
"Cannot compile shaders"
44 var program
= new GLProgram
45 if not program
.is_ok
then
46 print
"Program is not ok: {gl.error.to_s}\nLog:"
47 print program
.info_log
53 var vertex_shader
= new GLVertexShader
54 assert vertex_shader
.is_ok
else print
"Vertex shader is not ok: {gl.error}"
55 vertex_shader
.source
= """
56 attribute vec4 vPosition;
59 gl_Position = vPosition;
61 """@glsl_vertex_shader
.to_cstring
63 assert vertex_shader
.is_compiled
else print
"Vertex shader compilation failed with: {vertex_shader.info_log} {program.info_log}"
67 var fragment_shader
= new GLFragmentShader
68 assert fragment_shader
.is_ok
else print
"Fragment shader is not ok: {gl.error}"
69 fragment_shader
.source
= """
70 precision mediump float;
73 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
75 """@glsl_fragment_shader
.to_cstring
76 fragment_shader
.compile
77 assert fragment_shader
.is_compiled
else print
"Fragment shader compilation failed with: {fragment_shader.info_log}"
81 program
.attach_shader vertex_shader
82 program
.attach_shader fragment_shader
83 program
.bind_attrib_location
(0, "vPosition")
85 assert program
.is_linked
else print
"Linking failed: {program.info_log}"
89 var vertices
= [0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0]
90 var vertex_array
= new VertexArray(0, 3, vertices
)
91 vertex_array
.attrib_pointer
92 gl
.clear_color
(0.5, 0.0, 0.5, 1.0)
96 gl
.viewport
(0, 0, width
, height
)
97 gl
.clear
((new GLBuffer).color
)
101 glDrawArrays
(new GLDrawMode.triangles
, 0, 3)
109 fragment_shader
.delete