tests: Added test for basename and related methods
[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 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 # TODO android part
87 # Opengles1Display_midway_init(self, format);
88
89 var surface = egl_display.create_window_surface(config, x11_window_handle, [0])
90 assert surface.is_ok else print egl_display.error
91
92 var context = egl_display.create_context(config)
93 assert context.is_ok else print egl_display.error
94
95 var make_current_res = egl_display.make_current(surface, surface, context)
96 assert make_current_res
97
98 var width = surface.attribs(egl_display).width
99 var height = surface.attribs(egl_display).height
100 print "Width: {width}"
101 print "Height: {height}"
102
103 assert egl_bind_opengl_es_api else print "eglBingAPI failed: {egl_display.error}"
104
105 #
106 ## GLESv2
107 #
108
109 print "Can compile shaders? {gl.shader_compiler}"
110 assert_no_gl_error
111
112 assert gl.shader_compiler else print "Cannot compile shaders"
113
114 # gl program
115 print gl.error.to_s
116 var program = new GLProgram
117 if not program.is_ok then
118 print "Program is not ok: {gl.error.to_s}\nLog:"
119 print program.info_log
120 abort
121 end
122 assert_no_gl_error
123
124 # vertex shader
125 var vertex_shader = new GLVertexShader
126 assert vertex_shader.is_ok else print "Vertex shader is not ok: {gl.error}"
127 vertex_shader.source = """
128 attribute vec4 vPosition;
129 void main()
130 {
131 gl_Position = vPosition;
132 }
133 """.to_cstring
134 vertex_shader.compile
135 assert vertex_shader.is_compiled else print "Vertex shader compilation failed with: {vertex_shader.info_log} {program.info_log}"
136 assert_no_gl_error
137
138 # fragment shader
139 var fragment_shader = new GLFragmentShader
140 assert fragment_shader.is_ok else print "Fragment shader is not ok: {gl.error}"
141 fragment_shader.source = """
142 precision mediump float;
143 void main()
144 {
145 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
146 }
147 """.to_cstring
148 fragment_shader.compile
149 assert fragment_shader.is_compiled else print "Fragment shader compilation failed with: {fragment_shader.info_log}"
150 assert_no_gl_error
151
152 program.attach_shader vertex_shader
153 program.attach_shader fragment_shader
154 program.bind_attrib_location(0, "vPosition")
155 program.link
156 assert program.is_linked else print "Linking failed: {program.info_log}"
157 assert_no_gl_error
158
159 # draw!
160 var vertices = [0.0, 0.5, 0.0, -0.5, -0.5, 0.0, 0.5, -0.5, 0.0]
161 var vertex_array = new VertexArray(0, 3, vertices)
162 vertex_array.attrib_pointer
163 gl.clear_color(0.5, 0.0, 0.5, 1.0)
164 for i in [0..10000[ do
165 printn "."
166 assert_no_gl_error
167 gl.viewport(0, 0, width, height)
168 gl.clear((new GLBuffer).color)
169 program.use
170 vertex_array.enable
171 glDrawArrays(new GLDrawMode.triangles, 0, 3)
172 egl_display.swap_buffers(surface)
173 end
174
175 # delete
176 program.delete
177 vertex_shader.delete
178 fragment_shader.delete
179
180 #
181 ## EGL
182 #
183 # close
184 egl_display.make_current(new EGLSurface.none, new EGLSurface.none, new EGLContext.none)
185 egl_display.destroy_context(context)
186 egl_display.destroy_surface(surface)
187
188 #
189 ## SDL
190 #
191 # close
192 sdl_display.destroy