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, 1, 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 glIsProgram
(program
) then
58 print
"Program is not ok: {glGetError.to_s}\nLog:"
59 print program
.info_log
65 vertex_shader
= new GLVertexShader
66 assert glIsShader
(vertex_shader
) else print
"Vertex shader is not ok: {glGetError}"
67 glShaderSource
(vertex_shader
, """
68 attribute vec4 vPosition;
71 gl_Position = vPosition;
73 """@glsl_vertex_shader
.to_cstring
)
74 glCompileShader
(vertex_shader
)
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 glIsShader
(fragment_shader
) else print
"Fragment shader is not ok: {glGetError}"
81 glShaderSource
(fragment_shader
, """
82 precision mediump float;
85 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
87 """@glsl_fragment_shader
.to_cstring
)
88 glCompileShader
(fragment_shader
)
89 assert fragment_shader
.is_compiled
else print
"Fragment shader compilation failed with: {fragment_shader.info_log}"
93 glAttachShader
(program
, vertex_shader
)
94 glAttachShader
(program
, 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
110 var display
= display
111 if display
!= null then
112 glClearColor
(t
, t
, t
, 1.0)
115 glViewport
(0, 0, display
.width
, display
.height
)
116 glClear gl_COLOR_BUFFER_BIT
120 glDrawArrays
(gl_TRIANGLES
, 0, 3)
125 if t
> 1.0 then t
= 0.0
132 glDeleteProgram program
133 glDeleteShader vertex_shader
134 glDeleteShader fragment_shader
137 var display
= display
138 if display
!= null then display
.close
142 if "NIT_TESTING".environ
== "true" then exit
(0)