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

<geexlab>
	
  <window name="win3d01" title="PhysX 4 - Rigid Body 01" 
          width="1280" height="720" 
          gl_version_major="3" gl_version_minor="2" />
  
   
  
	<script name="init_scene" run_mode="INIT" language="LUA" filename="init.lua" />
  
	<script name="update_scene" run_mode="FRAME" language="LUA" filename="frame.lua" />
	




	<script name="terminate_scene" run_mode="TERMINATE" >
		<raw_data><![CDATA[	

gh_physx3.fetch_results(px_scene)

gh_physx3.rb_free_actors(aaid)

gh_physx4.stop()
gh_utils.trace("PhysX 4 stopped ok.")

gh_imgui.terminate()
  
		]]></raw_data>
	</script> 





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

winW, winH = gh_window.getsize(0)

gx_camera.update_perspective(camera, 60, 1, 0, 0, winW, winH, 0.1, 1000)

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)

		]]></raw_data>
	</script> 
  







  <gpu_program name="texture_prog" >
    <raw_data_vs><![CDATA[	 
#version 120
uniform vec4 uvtiling;    
void main()
{
  gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;		
  gl_TexCoord[0] = gl_MultiTexCoord0 * uvtiling;
}
  ]]></raw_data_vs>
    <raw_data_ps><![CDATA[	 
#version 120
uniform sampler2D tex0;
void main (void)
{
  vec2 uv = gl_TexCoord[0].xy;
  uv.y *= -1.0;
  vec4 t = texture2D(tex0,uv).rgba;
  gl_FragColor = t;
}
    ]]></raw_data_ps>
  </gpu_program>






  <gpu_program name="gi_texture_prog" >
    <raw_data_vs><![CDATA[	 
#version 330
#extension GL_ARB_draw_instanced: enable

in vec4 gxl3d_Position;
in vec4 gxl3d_Normal;
in vec4 gxl3d_TexCoord0;

in vec4 gxl3d_Instance_Position;
in vec4 gxl3d_Instance_Rotation;

uniform mat4 gxl3d_ProjectionMatrix;
uniform mat4 gxl3d_ViewMatrix;
//uniform float time;

//out vec4 Vertex_Normal;
out vec4 Vertex_UV;

#define PI_OVER_180 0.01745329251994329576923690768489

mat3 quat_to_mat3(vec4 q) 
{
  mat3 m;
  float a1, a2, s;
  q = normalize (q);
  s = q.w * q.w - 0.5;
  m[0][0] = q.x * q.x + s;  m[1][1] = q.y * q.y + s;  m[2][2] = q.z * q.z + s;
  a1 = q.x * q.y;  a2 = q.z * q.w;  m[0][1] = a1 + a2;  m[1][0] = a1 - a2;
  a1 = q.x * q.z;  a2 = q.y * q.w;  m[2][0] = a1 + a2;  m[0][2] = a1 - a2;
  a1 = q.y * q.z;  a2 = q.x * q.w;  m[1][2] = a1 + a2;  m[2][1] = a1 - a2;
  return 2. * m;
}

mat4 build_instance_transform(vec4 pos, vec4 q)
{
  mat3 rot_mat = quat_to_mat3(q); 
  mat4 result;
  
  result[0][0]  = rot_mat[0][0];
	result[0][1]  = rot_mat[0][1];
	result[0][2]  = rot_mat[0][2];
	result[0][3]  = 0.0;

	result[1][0]  = rot_mat[1][0];
	result[1][1]  = rot_mat[1][1];
	result[1][2]  = rot_mat[1][2];
	result[1][3]  = 0.0;

  result[2][0]  = rot_mat[2][0];
  result[2][1]  = rot_mat[2][1];
  result[2][2]  = rot_mat[2][2];
  result[2][3]  = 0.0;

  result[3][0] = pos.x;
  result[3][1] = pos.y;
  result[3][2] = pos.z;
  result[3][3] = 1.0;
  
  return result;
}

void main()
{
  mat4 modelMatrix = build_instance_transform(gxl3d_Instance_Position, gxl3d_Instance_Rotation);
  //mat4 modelViewMatrix = gxl3d_ViewMatrix * modelMatrix;
  
  vec4 modelSpacePos = modelMatrix * gxl3d_Position;
  vec4 viewSpacePos = gxl3d_ViewMatrix * modelSpacePos;  
  gl_Position = gxl3d_ProjectionMatrix * viewSpacePos;  
  
  Vertex_UV = gxl3d_TexCoord0;
  
  //Vertex_Normal = modelViewMatrix * gxl3d_Normal;
  
  //vec4 LP = gxl3d_ViewMatrix * light_position;
  //Vertex_LightDir = LP - viewSpacePos;
  //Vertex_EyeVec = -viewSpacePos;
}
  ]]></raw_data_vs>
  
    <raw_data_ps><![CDATA[	 
#version 150
uniform sampler2D tex0;
//in vec4 Vertex_Normal;
in vec4 Vertex_UV;
out vec4 FragColor;

void main()
{
  vec2 uv = Vertex_UV.xy;
  uv.y *= -1.0;
  vec4 tex01_color = texture(tex0, uv).rgba;
  FragColor.rgb = tex01_color.rgb;
  FragColor.a = 1.0;
}
    ]]></raw_data_ps>
  </gpu_program>

  

  
</geexlab>
