lib: move some examples/* into specific subdirectories of their lib
[nit.git] / lib / glesv2 / examples / opengles2_hello_triangle.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
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
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
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.
16
17 # Basic example of OpenGL ES 2.0 usage from the book OpenGL ES 2.0 Programming Guide.
18 #
19 # Code reference:
20 # https://code.google.com/p/opengles-book-samples/source/browse/trunk/LinuxX11/Chapter_2/Hello_Triangle/Hello_Triangle.c
21 module opengles2_hello_triangle
22
23 import glesv2
24 import egl
25 import mnit_linux # for sdl
26 import x11
27
28 if "NIT_TESTING".environ == "true" then exit(0)
29
30 var window_width = 800
31 var window_height = 600
32
33 #
34 ## SDL
35 #
36 var sdl_display = new SDLDisplay(window_width, window_height)
37 var sdl_wm_info = new SDLSystemWindowManagerInfo
38 var x11_window_handle = sdl_wm_info.x11_window_handle
39
40 #
41 ## X11
42 #
43 var x_display = x_open_default_display
44 assert x_display != 0 else print "x11 fail"
45
46 #
47 ## EGL
48 #
49 var egl_display = new EGLDisplay(x_display)
50 assert egl_display.is_valid else print "EGL display is not valid"
51 egl_display.initialize
52
53 print "EGL version: {egl_display.version}"
54 print "EGL vendor: {egl_display.vendor}"
55 print "EGL extensions: {egl_display.extensions.join(", ")}"
56 print "EGL client APIs: {egl_display.client_apis.join(", ")}"
57
58 assert egl_display.is_valid else print egl_display.error
59
60 var config_chooser = new EGLConfigChooser
61 #config_chooser.surface_type_egl
62 config_chooser.blue_size = 8
63 config_chooser.green_size = 8
64 config_chooser.red_size = 8
65 #config_chooser.alpha_size = 8
66 #config_chooser.depth_size = 8
67 #config_chooser.stencil_size = 8
68 #config_chooser.sample_buffers = 1
69 config_chooser.close
70
71 var configs = config_chooser.choose(egl_display)
72 assert configs != null else print "choosing config failed: {egl_display.error}"
73 assert not configs.is_empty else print "no EGL config"
74
75 print "{configs.length} EGL configs available"
76 for config in configs do
77 var attribs = config.attribs(egl_display)
78 print "* caveats: {attribs.caveat}"
79 print " conformant to: {attribs.conformant}"
80 print " size of RGBA: {attribs.red_size} {attribs.green_size} {attribs.blue_size} {attribs.alpha_size}"
81 print " buffer, depth, stencil: {attribs.buffer_size} {attribs.depth_size} {attribs.stencil_size}"
82 end
83
84 var config = configs.first
85
86 var format = config.attribs(egl_display).native_visual_id
87
88 # TODO android part
89 # Opengles1Display_midway_init(recv, format);
90
91 var surface = egl_display.create_window_surface(config, x11_window_handle, [0])
92 assert surface.is_ok else print egl_display.error
93
94 var context = egl_display.create_context(config)
95 assert context.is_ok else print egl_display.error
96
97 var make_current_res = egl_display.make_current(surface, surface, context)
98 assert make_current_res
99
100 var width = surface.attribs(egl_display).width
101 var height = surface.attribs(egl_display).height
102 print "Width: {width}"
103 print "Height: {height}"
104
105 assert egl_bind_opengl_es_api else print "eglBingAPI failed: {egl_display.error}"
106
107 #
108 ## GLESv2
109 #
110
111 print "Can compile shaders? {gl_shader_compiler}"
112 assert_no_gl_error
113
114 assert gl_shader_compiler else print "Cannot compile shaders"
115
116 # gl program
117 print gl_error.to_s
118 var program = new GLProgram
119 if not program.is_ok then
120 print "Program is not ok: {gl_error.to_s}\nLog:"
121 print program.info_log
122 abort
123 end
124 assert_no_gl_error
125
126 # vertex shader
127 var vertex_shader = new GLVertexShader
128 assert vertex_shader.is_ok else print "Vertex shader is not ok: {gl_error}"
129 vertex_shader.source = """
130 attribute vec4 vPosition;
131 void main()
132 {
133 gl_Position = vPosition;
134 } """
135 vertex_shader.compile
136 assert vertex_shader.is_compiled else print "Vertex shader compilation failed with: {vertex_shader.info_log} {program.info_log}"
137 assert_no_gl_error
138
139 # fragment shader
140 var fragment_shader = new GLFragmentShader
141 assert fragment_shader.is_ok else print "Fragment shader is not ok: {gl_error}"
142 fragment_shader.source = """
143 precision mediump float;
144 void main()
145 {
146 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
147 }
148 """
149 fragment_shader.compile
150 assert fragment_shader.is_compiled else print "Fragment shader compilation failed with: {fragment_shader.info_log}"
151 assert_no_gl_error
152
153 program.attach_shader vertex_shader
154 program.attach_shader fragment_shader
155 program.bind_attrib_location(0, "vPosition")
156 program.link
157 assert program.is_linked else print "Linking failed: {program.info_log}"
158 assert_no_gl_error
159
160 # draw!
161 var vertices = [0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0]
162 var vertex_array = new VertexArray(0, 3, vertices)
163 vertex_array.attrib_pointer
164 gl_clear_color(0.5, 0.0, 0.5, 1.0)
165 for i in [0..10000[ do
166 printn "."
167 assert_no_gl_error
168 gl_viewport(0, 0, width, height)
169 gl_clear_color_buffer
170 program.use
171 vertex_array.enable
172 vertex_array.draw_arrays_triangles
173 egl_display.swap_buffers(surface)
174 end
175
176 # delete
177 program.delete
178 vertex_shader.delete
179 fragment_shader.delete
180
181 #
182 ## EGL
183 #
184 # close
185 egl_display.make_current(new EGLSurface.none, new EGLSurface.none, new EGLContext.none)
186 egl_display.destroy_context(context)
187 egl_display.destroy_surface(surface)
188
189 #
190 ## SDL
191 #
192 # close
193 sdl_display.destroy