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 # Portable example of using Gamnit with custom calls to OpenGL ES 2.0
18 # * The book OpenGL ES 2.0 Programming Guide
19 # * https://code.google.com/p/opengles-book-samples/source/browse/trunk/LinuxX11/Chapter_2/Hello_Triangle/Hello_Triangle.c
20 module portable_triangle
is
21 app_name
"gamnit Triangle"
22 app_namespace
"org.nitlanguage.triangle"
23 app_version
(1, 0, git_revision
)
30 # Our only program for the graphic card
31 var program
: GLProgram is noautoinit
33 # The only vertex sharder
34 var vertex_shader
: GLVertexShader is noautoinit
36 # The only fragment sharder
37 var fragment_shader
: GLFragmentShader is noautoinit
39 # Vertex data for the triangle
40 var vertex_array
: VertexArray is noautoinit
47 assert display
!= null
49 print
"Width: {display.width}"
50 print
"Height: {display.height}"
53 assert gl
.shader_compiler
else print
"Cannot compile shaders"
56 program
= new GLProgram
57 if not program
.is_ok
then
58 print
"Program is not ok: {gl.error.to_s}\nLog:"
59 print program
.info_log
65 vertex_shader
= new GLVertexShader
66 assert vertex_shader
.is_ok
else print
"Vertex shader is not ok: {gl.error}"
67 vertex_shader
.source
= """
68 attribute vec4 vPosition;
71 gl_Position = vPosition;
73 """@glsl_vertex_shader
.to_cstring
75 assert vertex_shader
.is_compiled
else print
"Vertex shader compilation failed with: {vertex_shader.info_log} {program.info_log}"
79 fragment_shader
= new GLFragmentShader
80 assert fragment_shader
.is_ok
else print
"Fragment shader is not ok: {gl.error}"
81 fragment_shader
.source
= """
82 precision mediump float;
85 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
87 """@glsl_fragment_shader
.to_cstring
88 fragment_shader
.compile
89 assert fragment_shader
.is_compiled
else print
"Fragment shader compilation failed with: {fragment_shader.info_log}"
93 program
.attach_shader vertex_shader
94 program
.attach_shader fragment_shader
95 program
.bind_attrib_location
(0, "vPosition")
97 assert program
.is_linked
else print
"Linking failed: {program.info_log}"
101 var vertices
= [0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0]
102 vertex_array
= new VertexArray(0, 3, vertices
)
103 vertex_array
.attrib_pointer
108 var display
= display
109 if display
!= null then
110 gl
.clear_color
(0.5, 0.0, 0.5, 1.0)
113 gl
.viewport
(0, 0, display
.width
, display
.height
)
114 gl
.clear
((new GLBuffer).color
)
118 glDrawArrays
(new GLDrawMode.triangles
, 0, 3)
129 fragment_shader
.delete
132 var display
= display
133 if display
!= null then display
.close
137 if "NIT_TESTING".environ
== "true" then exit
(0)