1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # Basic example of OpenGL ES 2.0 usage from the book OpenGL ES 2.0 Programming Guide.
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
28 if "NIT_TESTING".environ
== "true" then exit
(0)
30 var window_width
= 800
31 var window_height
= 600
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
43 var x_display
= x_open_default_display
44 assert x_display
!= 0 else print
"x11 fail"
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
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(", ")}"
58 assert egl_display
.is_valid
else print egl_display
.error
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
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"
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}"
84 var config
= configs
.first
87 # Opengles1Display_midway_init(self, format);
89 var surface
= egl_display
.create_window_surface
(config
, x11_window_handle
, [0])
90 assert surface
.is_ok
else print egl_display
.error
92 var context
= egl_display
.create_context
(config
)
93 assert context
.is_ok
else print egl_display
.error
95 var make_current_res
= egl_display
.make_current
(surface
, surface
, context
)
96 assert make_current_res
98 var width
= surface
.attribs
(egl_display
).width
99 var height
= surface
.attribs
(egl_display
).height
100 print
"Width: {width}"
101 print
"Height: {height}"
103 assert egl_bind_opengl_es_api
else print
"eglBingAPI failed: {egl_display.error}"
109 print
"Can compile shaders? {gl.shader_compiler}"
112 assert gl
.shader_compiler
else print
"Cannot compile shaders"
115 print glGetError
.to_s
116 var program
= new GLProgram
117 if not glIsProgram
(program
) then
118 print
"Program is not ok: {glGetError.to_s}\nLog:"
119 print glGetProgramInfoLog
(program
)
125 var vertex_shader
= new GLVertexShader
126 assert glIsShader
(vertex_shader
) else print
"Vertex shader is not ok: {glGetError}"
127 glShaderSource
(vertex_shader
, """
128 attribute vec4 vPosition;
131 gl_Position = vPosition;
134 glCompileShader vertex_shader
135 assert vertex_shader
.is_compiled
else print
"Vertex shader compilation failed with: {glGetShaderInfoLog(vertex_shader)} {glGetProgramInfoLog(program)}"
139 var fragment_shader
= new GLFragmentShader
140 assert glIsShader
(fragment_shader
) else print
"Fragment shader is not ok: {glGetError}"
141 glShaderSource
(fragment_shader
, """
142 precision mediump float;
145 gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0);
148 glCompileShader
(fragment_shader
)
149 assert fragment_shader
.is_compiled
else print
"Fragment shader compilation failed with: {glGetShaderInfoLog(fragment_shader)}"
152 glAttachShader
(program
, vertex_shader
)
153 glAttachShader
(program
, fragment_shader
)
154 program
.bind_attrib_location
(0, "vPosition")
155 glLinkProgram program
156 assert program
.is_linked
else print
"Linking failed: {glGetProgramInfoLog(program)}"
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 glClearColor
(0.5, 0.0, 0.5, 1.0)
164 for i
in [0..10000[ do
167 glViewport
(0, 0, width
, height
)
168 glClear gl_COLOR_BUFFER_BIT
171 glDrawArrays
(gl_TRIANGLES
, 0, 3)
172 egl_display
.swap_buffers
(surface
)
176 glDeleteProgram program
177 glDeleteShader vertex_shader
178 glDeleteShader fragment_shader
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