Hi
Just a simple shader (1 point light, 1 directionnal light) for users who haven't used the shaders with agk yet.
The code for vertex and shader is from Paul
.
- you need to create 2 files : shader.ps, shader.vs, place it in your media folder. These files areused by your shader.
NOTE : please use notepad or equivalent appli on mac/linux. The file should be in unicode (I think).
Shader.ps
uniform sampler2D texture0;
uniform vec4 agk_MeshDiffuse;
uniform vec4 agk_PLightColor;
uniform vec4 agk_PLightPos; // x,y,z = position, w = light range
uniform vec3 ambient; // ambient light
uniform vec3 DLightColor;
uniform vec3 DLightDir;
varying vec3 normalVarying;
varying vec3 posVarying;
varying vec2 uv0Varying;
varying vec2 uvNormal;
void main()
{
vec3 dir = (agk_PLightPos.xyz - posVarying);
vec3 norm = normalize(normalVarying);
// calculate point light attenuation for this pixel
float atten = dot(dir,dir);
float lightRange = agk_PLightPos.w;
atten = clamp(lightRange/atten,0.0,1.0);
// calculate point light instensity for this pixel
float intensity = dot(normalize(dir),norm);
intensity = clamp(intensity,0.0,1.0);
// add point light color
vec3 color2 = ambient + agk_PLightColor.rgb * intensity * atten;
// add directional light color
color2 = color2 + clamp(dot(-DLightDir,norm)*DLightColor.rgb,0.0,1.0);
color2 = clamp(color2,0.0,1.0);
// multiply light with texture and object color
gl_FragColor = texture2D(texture0, uv0Varying) * vec4(color2,1.0) * agk_MeshDiffuse;
}
Shader.vs :
attribute vec3 position;
attribute vec3 normal;
attribute vec2 uv;
uniform mat3 agk_WorldNormal;
uniform mat4 agk_World;
uniform mat4 agk_ViewProj;
uniform vec4 uvBounds0;
varying vec3 normalVarying;
varying vec3 posVarying;
varying vec2 uv0Varying;
void main()
{
vec4 pos = agk_World * vec4(position,1);
gl_Position = agk_ViewProj * pos;
posVarying = pos.xyz;
vec3 norm = agk_WorldNormal * normal;
normalVarying = norm;
uv0Varying = uv * uvBounds0.xy + uvBounds0.zw;
}
The AppGameKit code :
// Project: 3D_shader_light
// Created: 2015-12-01
// set window properties
SetWindowTitle( "3D_shader_light" )
SetWindowSize( 1024, 768, 0 )
// set display properties
SetVirtualResolution( 1024, 768 )
SetOrientationAllowed( 1, 1, 1, 1 )
SetClearColor(150,150,150)
// create a sphere to see the texture
CreateObjectSphere(1,50,40,60)
// position and orientate the camera
SetCameraPosition(1,50,100,-200)
SetCameraLookAt(1,0,0,0,0)
SetGenerateMipmaps(1)
LoadImage(1,"texture.jpg")
LoadShader(1, "shader.vs", "shader.ps")
SetObjectShader(1,1)
SetObjectImage(1,1,0)
SetShaderConstantByName(1,"DLightDir",-0.3,-0.3,0.7,0.0) // direction
SetShaderConstantByName(1,"DLightColor",1,0.5,0,0.0) // rgb : 0 to 1.0
SetShaderConstantByName(1,"agk_PLightPos",10,10,10,50) // position of the point light (x,y,z)+ range
SetShaderConstantByName(1,"agk_PLightColor",0,0,1,0.0) // blue color
SetShaderConstantByName(1,"ambient",0.2,0.2,0.2,0.0) // ambient color
do
sync()
loop
http://www.dracaena-studio.com