examples: annotate examples
[nit.git] / lib / gamnit / examples / triangle / src / standalone_triangle.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
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
6 #
7 # http://www.apache.org/licenses/LICENSE-2.0
8 #
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.
14
15 # Example of using `GamnitDisplay` to setup a screen for custom calls to OpenGL ES 2.0
16 #
17 # This example does not support the lifecycle of mobile platforms, as such it only works on desktop computers.
18 #
19 # References:
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 is example
23
24 import app
25 import gamnit::display
26
27 if "NIT_TESTING".environ == "true" then exit(0)
28
29 # Setup gamnit
30 var display = new GamnitDisplay
31 display.setup
32
33 var width = display.width
34 var height = display.height
35 print "Width: {width}"
36 print "Height: {height}"
37
38 # Custom calls to OpenGL ES 2.0
39 assert glGetError == gl_NO_ERROR
40 assert gl.shader_compiler else print "Cannot compile shaders"
41
42 # GL program
43 print glGetError.to_s
44 var program = new GLProgram
45 if not glIsProgram(program) then
46 print "Program is not ok: {glGetError.to_s}\nLog:"
47 print glGetProgramInfoLog(program)
48 abort
49 end
50 assert glGetError == gl_NO_ERROR
51
52 # Vertex shader
53 var vertex_shader = new GLVertexShader
54 assert glIsShader(vertex_shader) else print "Vertex shader is not ok: {glGetError}"
55 glShaderSource(vertex_shader, """
56 attribute vec4 vPosition;
57 void main()
58 {
59 gl_Position = vPosition;
60 }
61 """@glsl_vertex_shader.to_cstring)
62 glCompileShader vertex_shader
63 assert vertex_shader.is_compiled else print "Vertex shader compilation failed with: {glGetShaderInfoLog(vertex_shader)} {glGetProgramInfoLog(program)}"
64 assert glGetError == gl_NO_ERROR
65
66 # Fragment shader
67 var fragment_shader = new GLFragmentShader
68 assert glIsShader(fragment_shader) else print "Fragment shader is not ok: {glGetError}"
69 glShaderSource(fragment_shader, """
70 precision mediump float;
71 void main()
72 {
73 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
74 }
75 """@glsl_fragment_shader.to_cstring)
76 glCompileShader fragment_shader
77 assert fragment_shader.is_compiled else print "Fragment shader compilation failed with: {glGetShaderInfoLog(fragment_shader)}"
78 assert glGetError == gl_NO_ERROR
79
80 # Attach to program
81 glAttachShader(program, vertex_shader)
82 glAttachShader(program, fragment_shader)
83 program.bind_attrib_location(0, "vPosition")
84 glLinkProgram program
85 assert program.is_linked else print "Linking failed: {glGetProgramInfoLog(program)}"
86 assert glGetError == gl_NO_ERROR
87
88 # Draw!
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 glClearColor(0.5, 0.0, 0.5, 1.0)
93 for i in [0..1000[ do
94 printn "."
95 assert glGetError == gl_NO_ERROR
96 glViewport(0, 0, width, height)
97 glClear gl_COLOR_BUFFER_BIT
98 glUseProgram program
99 vertex_array.enable
100
101 glDrawArrays(gl_TRIANGLES, 0, 3)
102
103 display.flip
104 end
105
106 # Clean up
107 glDeleteProgram program
108 glDeleteShader vertex_shader
109 glDeleteShader fragment_shader
110
111 # Close gamnit
112 display.close