<?xml version="1.0" encoding="ISO-8859-1" standalone="yes"?>

<glsl_hacker>

  <window name="win3d01" title="GeeXLab Error Scene" width="800" height="400"
          gl_version_major="2" gl_version_minor="1" />
  
  
	<script name="init_scene" run_mode="INIT" >
		<raw_data><![CDATA[	
    
winW, winH = gh_window.getsize(0)


camera_ortho = gh_camera.create_ortho(-winW/2, winW/2, -winH/2, winH/2, 1.0, 10.0)
gh_camera.set_viewport(camera_ortho, 0, 0, winW, winH)
gh_camera.set_position(camera_ortho, 0, 0, 4)







local vertex_color_vs_gl2=" \
void main() \
{ \
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex; \
  gl_FrontColor = gl_Color; \
}"
  

local vertex_color_ps_gl2=" \
void main (void) \
{ \
  gl_FragColor = gl_Color; \
}"
  


local font_vs_gl2=" \
uniform mat4 gxl3d_ViewProjectionMatrix; \
uniform mat4 gxl3d_ModelMatrix; \
uniform vec4 gxl3d_Viewport; \
varying vec4 Vertex_UV; \
varying vec4 Vertex_Color; \
void main() \
{ \
  vec4 P = gl_Vertex; \
  vec4 Pw = gxl3d_ModelMatrix * P; \
  Pw.x = Pw.x - gxl3d_Viewport.z/2.0; \
  Pw.y = Pw.y + gxl3d_Viewport.w/2.0; \
  gl_Position = gxl3d_ViewProjectionMatrix * Pw; \
  Vertex_UV = gl_MultiTexCoord0; \
  Vertex_Color = gl_Color; \
}"
  

local font_ps_gl2=" \
uniform sampler2D tex0; \
varying vec4 Vertex_UV; \
varying vec4 Vertex_Color; \
void main (void) \
{ \
  vec2 uv = Vertex_UV.xy; \
  float t = texture2D(tex0,uv).r; \
  gl_FragColor = vec4(t * Vertex_Color.rgb, 1.0); \
}"







local vertex_color_vs_gles2=" \
attribute vec4 gxl3d_Position; \
attribute vec4 gxl3d_Color; \
uniform mat4 gxl3d_ModelViewProjectionMatrix; \
varying vec4 Vertex_Color; \
void main() \
{ \
  gl_Position = gxl3d_ModelViewProjectionMatrix * gxl3d_Position;\
  Vertex_Color = gxl3d_Color;\
}"
  

local vertex_color_ps_gles2=" \
varying highp vec4 Vertex_Color; \
void main() \
{ \
  gl_FragColor = Vertex_Color;\
}"
  




local font_vs_gles2=" \
attribute vec4 gxl3d_Position; \
attribute vec4 gxl3d_TexCoord0; \
attribute vec4 gxl3d_Color; \
uniform mat4 gxl3d_ViewProjectionMatrix; \
uniform mat4 gxl3d_ModelMatrix;\
uniform vec4 gxl3d_Viewport;\
varying vec4 Vertex_Color; \
varying vec4 Vertex_UV; \
void main() \
{ \
  vec4 P = gxl3d_Position;\
  vec4 Pw = gxl3d_ModelMatrix * P;\
  Pw.x = Pw.x - gxl3d_Viewport.z/2.0;\
  Pw.y = Pw.y + gxl3d_Viewport.w/2.0;\
  gl_Position = gxl3d_ViewProjectionMatrix * Pw;\
  Vertex_Color = gxl3d_Color;\
  Vertex_UV = gxl3d_TexCoord0;\
}"
  

local font_ps_gles2=" \
uniform sampler2D tex0; \
varying highp vec4 Vertex_UV; \
varying highp vec4 Vertex_Color; \
void main() \
{ \
  highp vec2 uv = Vertex_UV.xy;\
  highp float t = texture2D(tex0,uv).r;\
  gl_FragColor = vec4(t * Vertex_Color.rgb, 1.0);\
}"
  





is_opengl_es = gh_renderer.is_opengl_es()

if (is_opengl_es == 1) then

    vertex_color_prog = gh_gpu_program.create_v2("vertex_color_prog",vertex_color_vs_gles2, vertex_color_ps_gles2)
    font_prog = gh_gpu_program.create_v2("font_prog",font_vs_gles2, font_ps_gles2)

else

  local vs = "#version 120\n" .. vertex_color_vs_gl2
  local ps = "#version 120\n" .. vertex_color_ps_gl2
  vertex_color_prog = gh_gpu_program.create_v2("vertex_color_prog",vs, ps)
  
  vs = "#version 120\n" .. font_vs_gl2
  ps = "#version 120\n" .. font_ps_gl2
  font_prog = gh_gpu_program.create_v2("font_prog",vs, ps)
end











quad = gh_mesh.create_quad(winW, winH)
--gh_mesh.set_vertex_color(quad, 0, 0.3, 0.3, 0.35, 1) --bottom-left
--gh_mesh.set_vertex_color(quad, 1, 0.15, 0.15, 0.20, 1) -- top-left
--gh_mesh.set_vertex_color(quad, 2, 0.60, 0.60, 0.60, 1) --top-right
--gh_mesh.set_vertex_color(quad, 3, 0.60, 0.60, 0.60, 1) --bottom-right



quad_band_width = 12

quad2 = gh_mesh.create_quad(winW, quad_band_width)
gh_mesh.set_vertex_color(quad2, 0, 0.5, 0.2, 0.0, 1) --bottom-left
gh_mesh.set_vertex_color(quad2, 1, 0.5, 0.2, 0.0, 1) -- top-left
gh_mesh.set_vertex_color(quad2, 2, 0.5, 0.2, 0.0, 1) --top-right
gh_mesh.set_vertex_color(quad2, 3, 0.5, 0.2, 0.0, 1) --bottom-right

quad3 = gh_mesh.create_quad(quad_band_width, winH)
gh_mesh.set_vertex_color(quad3, 0, 0.5, 0.2, 0.0, 1) --bottom-left
gh_mesh.set_vertex_color(quad3, 1, 0.5, 0.2, 0.0, 1) -- top-left
gh_mesh.set_vertex_color(quad3, 2, 0.5, 0.2, 0.0, 1) --top-right
gh_mesh.set_vertex_color(quad3, 3, 0.5, 0.2, 0.0, 1) --bottom-right





font01 = gh_font.create_v2("hacked", 40, 512, 512)
gh_font.build_texture(font01, 0)
tex_font01 = gh_font.get_texture(font01)

font02 = gh_font.create_v2("roboto_medium", 20, 512, 512)
gh_font.build_texture(font02, 0)
tex_font02 = gh_font.get_texture(font02)

font03 = gh_font.create_v2("roboto_medium", 16, 512, 512)
gh_font.build_texture(font03, 0)
tex_font03 = gh_font.get_texture(font03)



r, g, b = gh_utils.hex_color_to_rgb("#ff9900")
gh_font.clear(font01)
gh_font.text_2d(font01, 20, 55, r, g, b, 1.0, "GeeXLab")
gh_font.update(font01, 0)



gh_font.clear(font03)
script_last_error = gh_app.get_data_str128("script_last_error")
if (script_last_error ~= "") then
  x_start = 20
  y_start = 180
  gh_font.text_2d(font03, x_start, y_start, 0.8, 0.8, 0.8, 1.0, script_last_error)
end
gh_font.update(font03, 0)



rpi_gles = 0
if ((is_opengl_es == 1) and (gh_utils.is_rpi() == 1)) then
  rpi_gles = 1
end  
  

last_time = gh_utils.get_elapsed_time()
frames = 0
fps = 0

gh_renderer.set_vsync(1)


		]]></raw_data>
	</script> 
  
	<script name="update_scene" run_mode="FRAME" >
		<raw_data><![CDATA[	

local elapsed_time = gh_utils.get_elapsed_time()
frames = frames + 1
local dt = elapsed_time - last_time
if (dt >= 1.0) then
  last_time = elapsed_time
  fps = frames
  frames = 0
end


gh_camera.bind(camera_ortho)
gh_renderer.set_depth_test_state(0)
gh_renderer.clear_color_depth_buffers(0.0, 0.0, 0.0, 1.0, 1.0)






gh_renderer.blending_on("") -- defaut is additive: BLEND_FACTOR_ONE + BLEND_FACTOR_ONE


local mx, my = gh_input.mouse_getpos()

local quad2_y = 0
local quad3_x = 0

if (rpi_gles == 1) then
  quad2_y = my
  quad3_x = mx
else  
  quad2_y = winH/2 - my
  quad3_x = mx - winW/2
end  


gh_gpu_program.bind(vertex_color_prog)
gh_mesh.set_vertices_color(quad2, 0.40, 0.15, 0.05, 1.0)
-- horizontal quad
gh_object.set_position(quad2, 0, quad2_y, 0)
gh_object.render(quad2)
gh_mesh.set_vertices_color(quad3, 0.40, 0.15, 0.05, 1.0)
-- vertical quad
gh_object.set_position(quad3, quad3_x, 0, 0)
gh_object.render(quad3)





gh_gpu_program.bind(font_prog)
gh_gpu_program.uniform1i(font_prog, "tex0", 0)

gh_texture.bind(tex_font01, 0)
gh_font.render(font01)


gh_font.clear(font02)
local x_start = 20
local y_start = 100
gh_font.text_2d(font02, x_start, y_start, 1, 0, 0, 1.0, "- demo error")
y_start = y_start + 20
gh_font.text_2d(font02, x_start, y_start, 1, 1, 0, 1.0, "- check the log file for more details. ")
y_start = y_start + 20
gh_font.text_2d(font02, x_start, y_start, 1.0, 1.0, 1.0, 1.0, string.format("- elapsed time: %.2f sec", elapsed_time))
if (script_last_error ~= "") then
  y_start = y_start + 20
  gh_font.text_2d(font02, x_start, y_start, 1.0, 1.0, 1.0, 1.0, "- error preview:")
end  

--y_start = y_start + 20
--gh_font.text_2d(font02, x_start, y_start, 1.0, 1.0, 1.0, 1.0, "mx=" .. mx .. " - my=" .. my)

gh_font.update(font02, 0)
gh_texture.bind(tex_font02, 0)
gh_font.render(font02)


gh_texture.bind(tex_font03, 0)
gh_font.render(font03)

gh_renderer.blending_off()


		]]></raw_data>
	</script> 
	

	<script name="resize_scene" run_mode="SIZE" >
		<raw_data><![CDATA[	

winW, winH = gh_window.getsize(0)


gh_camera.update_ortho(camera_ortho, -winW/2, winW/2, -winH/2, winH/2, 1.0, 10.0)
gh_camera.set_viewport(camera_ortho, 0, 0, winW, winH)

gh_mesh.update_quad_size(quad, winW, winH)
gh_mesh.update_quad_size(quad2, winW, quad_band_width)
gh_mesh.update_quad_size(quad3, quad_band_width, winH)

		]]></raw_data>
	</script> 

  
<!--  
<gpu_program name="vertex_color_prog" >
    <raw_data_vs><![CDATA[	 
#version 120    
void main()
{
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;		
  gl_FrontColor = gl_Color;
}
  ]]></raw_data_vs>
    <raw_data_ps><![CDATA[	 
#version 120    
void main (void)
{
  gl_FragColor = gl_Color;
}
    ]]></raw_data_ps>
  </gpu_program>

  
<gpu_program name="font_prog" >
    <raw_data_vs><![CDATA[	 
#version 120
uniform mat4 gxl3d_ViewProjectionMatrix;
uniform mat4 gxl3d_ModelMatrix;
uniform vec4 gxl3d_Viewport;
varying vec4 Vertex_UV;
varying vec4 Vertex_Color;
void main()
{
  vec4 P = gl_Vertex;
  vec4 Pw = gxl3d_ModelMatrix * P;
  Pw.x = Pw.x - gxl3d_Viewport.z/2;
  Pw.y = Pw.y + gxl3d_Viewport.w/2;
  gl_Position = gxl3d_ViewProjectionMatrix * Pw;		
  Vertex_UV = gl_MultiTexCoord0;
  Vertex_Color = gl_Color;
}
  ]]></raw_data_vs>
    <raw_data_ps><![CDATA[	 
#version 120
uniform sampler2D tex0;
varying vec4 Vertex_UV;
varying vec4 Vertex_Color;
void main (void)
{
  vec2 uv = Vertex_UV.xy;
  float t = texture2D(tex0,uv).r;
  gl_FragColor = vec4(t * Vertex_Color.rgb, 1.0); 
  //gl_FragColor = vec4(t, 1.0); 
  //gl_FragColor = vec4(1.0, 0.0, 0.0, 1.0); 
  //gl_FragColor = vec4(uv, 0.0, 1.0); 
}
    ]]></raw_data_ps>
  </gpu_program>
-->


   
</glsl_hacker>
