Initial import of KREngine

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%404
This commit is contained in:
kearwood
2011-10-25 05:03:10 +00:00
parent 5937288f78
commit cece608881
86 changed files with 12190 additions and 0 deletions

View File

@@ -0,0 +1,168 @@
//
// Shader.fsh
// gldemo
//
// Based on http://www.fabiensanglard.net/bumpMapping/index.php
/*
// -- Per vertex lighting
uniform sampler2D diffuseTexture, specularTexture, normalTexture;
varying mediump vec2 texCoord;
varying mediump vec3 diffuse;
varying mediump vec3 specular;
void main (void)
{
mediump vec3 texColour = vec3(texture2D(diffuseTexture, texCoord));
mediump vec3 specColor = vec3(texture2D(specularTexture, texCoord));
mediump vec3 normalVal = vec3(texture2D(normalTexture, texCoord));
//mediump vec3 colour = (texColour * diffuse) + specular;
mediump vec3 colour = (texColour * diffuse) + (specColor * specular);
gl_FragColor = vec4(colour, 1.0);
}
*/
/*
// -- Per Pixel lighting, test 1 --
uniform sampler2D diffuseTexture, specularTexture, normalTexture;
uniform mediump vec3 material_ambient, material_diffuse, material_specular;
varying mediump vec2 texCoord;
varying mediump vec3 normal;
varying mediump vec3 ePos;
const mediump float shininess = 4.0;
const mediump vec3 LightPos = vec3(20.0, 00.0, 10.0);
const mediump vec3 LightCol = vec3(2.0,2.0,2.0);
//directional light function //
// spFlg flag using specular or not.
// nrml nrml vector in the eye coordinate.
// ePos vertex position in the eye coordinate.
void DirectionalLight(inout mediump vec3 diffuse, inout mediump vec3 specular, in mediump int spFlg, in mediump vec3 nrml, in mediump vec3 ePos){
// calculate the light direction vector.
mediump vec3 lightDir = normalize(LightPos);
// calculate the half vector between eye position and light position.
mediump vec3 halfV = normalize(-ePos + LightPos);
// calculate diffuse light intensity.
mediump float dVP = max(dot(nrml,lightDir), 0.0);
// calculate approximated specular light base intensity.
mediump float dHV = max(dot(nrml,halfV),0.0);
// if the diffuse is not zero and spFlg is On,
// calculate specular light intensity with shininess,
// or turn off the specular light.
mediump float pf;
if (dVP>.0 && spFlg==1) pf = pow(dHV, shininess);
else pf = 0.0;
diffuse += dVP*LightCol;
specular += pf*LightCol;
}
void main (void)
{
mediump vec3 texColour = vec3(texture2D(diffuseTexture, texCoord));
mediump vec3 specColor = vec3(texture2D(specularTexture, texCoord));
mediump vec3 normalVal = vec3(texture2D(normalTexture, texCoord));
// initalize light intensity parameter.
mediump vec3 diffuse = vec3(0.75);
mediump vec3 specular = vec3(0.0);
DirectionalLight(diffuse, specular, 1, normalize(normal + normalVal), ePos);
specular *= material_specular;
diffuse = diffuse * material_diffuse + material_ambient;
//mediump vec3 colour = (texColour * diffuse) + specular;
mediump vec3 colour = (texColour * diffuse) + (specColor * specular);
gl_FragColor = vec4(colour, 1.0);
}
*/
/*
// -- Per Pixel lighting, test 2 --
uniform sampler2D diffuseTexture, specularTexture, normalTexture;
varying mediump vec2 texCoord;
varying mediump vec3 lightVec;
varying mediump vec3 halfVec;
//varying mediump vec3 eyeVec;
uniform mediump vec3 material_ambient, material_diffuse, material_specular;
void main()
{
// lookup normal from normal map, move from [0,1] to [-1, 1] range, normalize
mediump vec3 normal = normalize(2.0 * texture2D(normalTexture,texCoord).rgb - 1.0);
mediump float lamberFactor= max (dot (lightVec, normal), 0.0);
gl_FragColor = vec4(0.0);
if (lamberFactor > 0.0)
{
// compute diffuse lighting
mediump vec4 diffuseMaterial = texture2D(diffuseTexture, texCoord);
mediump vec4 diffuseLight = vec4(material_diffuse, 1.0) + 1.0; // 1.0 added so there will not be complete darkness
// compute specular lighting
mediump vec4 specularLight = texture2D(specularTexture, texCoord); // Specular value comes from a texture
mediump float shininess = pow (max (dot (halfVec, normal), 0.0), 2.0);
gl_FragColor = diffuseMaterial * diffuseLight * lamberFactor ;
gl_FragColor += vec4(material_specular, 1.0) * specularLight * shininess;
}
// compute ambient
gl_FragColor += vec4(material_ambient, 1.0);;
}
*/
// -- Per Pixel lighting, test 3 --
uniform sampler2D diffuseTexture, specularTexture, normalTexture;
varying mediump vec2 texCoord;
varying mediump vec3 lightVec;
varying mediump vec3 halfVec;
uniform lowp vec3 material_ambient, material_diffuse, material_specular;
void main()
{
// compute diffuse lighting
mediump vec4 diffuseMaterial = texture2D(diffuseTexture, texCoord);
/*
// lookup normal from normal map, move from [0,1] to [-1, 1] range, normalize
mediump vec3 normal = normalize(2.0 * texture2D(normalTexture,texCoord).rgb - 1.0);
mediump float lamberFactor= max (dot (lightVec, normal), 0.0);
// Add ambient light and alpha component from diffuse texture map
gl_FragColor = vec4(material_ambient, diffuseMaterial.w);
// compute specular lighting
mediump vec3 specularLight = vec3(texture2D(specularTexture, texCoord)); // Specular value comes from a texture
mediump float shininess = pow (max (dot (halfVec, normal), 0.0), 2.0);
// Add diffuse light
gl_FragColor += vec4(vec3(diffuseMaterial) * material_diffuse * lamberFactor, 0.0);
// Add specular light
gl_FragColor += vec4(material_specular * specularLight * shininess, 0.0);
*/
//gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
gl_FragColor = diffuseMaterial;
}

View File

@@ -0,0 +1,171 @@
//
// Shader.vsh
// gldemo
//
// Created by Kearwood Gilbert on 10-09-16.
// Copyright (c) 2010 Kearwood Software. All rights reserved.
//
/*
// -- Per vertex lighting
attribute highp vec3 myVertex, myNormal;
attribute mediump vec2 myUV;
uniform mediump mat4 myMVPMatrix, myModelView; // mvpmatrix is the result of multiplying the model, view, and projection matrices
uniform mediump mat3 myModelViewIT;
uniform mediump vec3 material_ambient, material_diffuse, material_specular;
varying mediump vec2 texCoord;
varying mediump vec3 diffuse, specular;
varying mediump vec3 normal;
const mediump float shininess = 4.0;
const mediump float cutoff = 0.975, exp = 100.0;
const mediump vec3 LightPos = vec3(20.0, 00.0, 10.0);
const mediump vec3 LightCol = vec3(2.0,2.0,2.0);
//directional light function //
// spFlg flag using specular or not.
// nrml nrml vector in the eye coordinate.
// ePos vertex position in the eye coordinate.
void DirectionalLight(in mediump int spFlg, in mediump vec3 nrml, in mediump vec3 ePos){
// calculate the light direction vector.
mediump vec3 lightDir = normalize(LightPos);
// calculate the half vector between eye position and light position.
mediump vec3 halfV = normalize(-ePos + LightPos);
// calculate diffuse light intensity.
mediump float dVP = max(dot(nrml,lightDir), 0.0);
// calculate approximated specular light base intensity.
mediump float dHV = max(dot(nrml,halfV),0.0);
// if the diffuse is not zero and spFlg is On,
// calculate specular light intensity with shininess,
// or turn off the specular light.
mediump float pf;
if (dVP>.0 && spFlg==1) pf = pow(dHV, shininess);
else pf = 0.0;
diffuse += dVP*LightCol;
specular += pf*LightCol;
}
// main function //
void main(void){
// transform the normal vector from the model coordinate to the eye coordinate.
normal = normalize(myModelViewIT * myNormal);
// calculate the vertex position in the eye coordinate.
highp vec3 ePos = vec3(myModelView * vec4(myVertex,1.0));
// initalize light intensity parameter.
//diffuse = material_ambient;
diffuse = vec3(0.75);
specular = vec3(0.0);
DirectionalLight(1, normal, ePos);
specular *= material_specular;
diffuse = diffuse * material_diffuse + material_ambient;
// Transform position
gl_Position = myMVPMatrix * vec4(myVertex,1.0);
// Pass UV co-ordinates
texCoord = myUV.st;
}
*/
/*
// -- Per Pixel lighting, test 1 --
attribute highp vec3 myVertex, myNormal, myTangent;
attribute mediump vec2 myUV;
uniform mediump mat4 myMVPMatrix, myModelView; // mvpmatrix is the result of multiplying the model, view, and projection matrices
uniform mediump mat3 myModelViewIT;
uniform mediump vec3 material_ambient, material_diffuse, material_specular;
varying mediump vec2 texCoord;
varying mediump vec3 normal;
varying mediump vec3 ePos;
// main function //
void main(void){
// Transform position
gl_Position = myMVPMatrix * vec4(myVertex,1.0);
// Pass UV co-ordinates
texCoord = myUV.st;
// transform the normal vector from the model coordinate to the eye coordinate.
normal = normalize(myModelViewIT * myNormal);
// calculate the vertex position in the eye coordinate.
ePos = vec3(myModelView * vec4(myVertex,1.0));
//mat_ambient = material_ambient;
//mat_diffuse = material_diffuse;
//mat_specular = material_specular;
}
*/
// -- Per Pixel lighting, test 2 --
const mediump vec3 LightPos = vec3(40, 20.0, -90.0);
attribute highp vec3 myVertex, myNormal;
attribute highp vec3 myTangent;
attribute mediump vec2 myUV;
uniform highp mat4 myMVPMatrix, myModelView; // mvpmatrix is the result of multiplying the model, view, and projection matrices
uniform highp mat3 myModelViewIT;
uniform lowp vec3 material_ambient, material_diffuse, material_specular;
varying mediump vec2 texCoord;
/*
varying mediump vec3 lightVec;
varying mediump vec3 halfVec;
*/
//varying mediump vec3 eyeVec;
void main()
{
// Transform position
gl_Position = myMVPMatrix * vec4(myVertex,1.0);
// Pass UV co-ordinates
texCoord = myUV.st;
/*
// Building the matrix Eye Space -> Tangent Space
vec3 n = normalize(vec3(myModelView * vec4(myNormal, 1.0)));
vec3 t = normalize(vec3(myModelView * vec4(myTangent, 1.0)));
vec3 b = cross(n, t);
vec3 vertexPosition = vec3(myMVPMatrix * vec4(myVertex, 1.0));
vec3 lightDir = normalize(LightPos - vertexPosition);
// transform light and half angle vectors by tangent basis
vec3 v;
v.x = dot(lightDir, t);
v.y = dot(lightDir, b);
v.z = dot(lightDir, n);
lightVec = normalize(v);
// v.x = dot(vertexPosition, t);
// v.y = dot(vertexPosition, b);
// v.z = dot(vertexPosition, n);
// eyeVec = normalize(v);
vertexPosition = normalize(vertexPosition);
// Normalize the halfVector to pass it to the fragment shader
vec3 halfVector = normalize((vertexPosition + lightDir) / 2.0);
v.x = dot (halfVector, t);
v.y = dot (halfVector, b);
v.z = dot (halfVector, n);
halfVec = normalize (v);
*/
}

View File

@@ -0,0 +1,150 @@
#define DOF_QUALITY 0
#define ENABLE_VIDEO_BG 0
#define PIXEL_SHIFT_1 0.001
#define PIXEL_SHIFT_2 0.002
#define PIXEL_SHIFT_3 0.003
#define PIXEL_SHIFT_4 0.004
varying mediump vec2 textureCoordinate;
precision lowp float;
#if ENABLE_VIDEO_BG == 0
uniform lowp sampler2D videoFrame;
#endif
uniform lowp sampler2D renderFrame;
uniform lowp sampler2D depthFrame;
void main()
{
lowp vec4 renderColor = texture2D(renderFrame, textureCoordinate);
mediump float depth = texture2D(depthFrame, textureCoordinate).r;
mediump vec4 pixelColor = renderColor;
#if DOF_QUALITY == 2
// Render high quality circle of confusion
// __XXX__
// _XXXXX_
// _XXXXX_
// _XXXXX_
// __XXX__
mediump float cf1 = PIXEL_SHIFT_1;
mediump float cf2 = PIXEL_SHIFT_2;
mediump float bx1 = textureCoordinate.s + cf1;
mediump float bx2 = textureCoordinate.s + cf2;
mediump float bxm1 = textureCoordinate.s - cf1;
mediump float bxm2 = textureCoordinate.s - cf2;
mediump float by1 = textureCoordinate.t + cf1;
mediump float by2 = textureCoordinate.t + cf2;
mediump float bym1 = textureCoordinate.t - cf1;
mediump float bym2 = textureCoordinate.t - cf2;
pixelColor += texture2D(renderFrame, vec2(bx1, textureCoordinate.t));
pixelColor += texture2D(renderFrame, vec2(bxm1, textureCoordinate.t));
pixelColor += texture2D(renderFrame, vec2(bx2, textureCoordinate.t));
pixelColor += texture2D(renderFrame, vec2(bxm2, textureCoordinate.t));
pixelColor += texture2D(renderFrame, vec2(textureCoordinate.s, by1));
pixelColor += texture2D(renderFrame, vec2(bx1, by1));
pixelColor += texture2D(renderFrame, vec2(bxm1, by1));
pixelColor += texture2D(renderFrame, vec2(bx2, by1));
pixelColor += texture2D(renderFrame, vec2(bxm2, by1));
pixelColor += texture2D(renderFrame, vec2(textureCoordinate.s, by2));
pixelColor += texture2D(renderFrame, vec2(bx1, by2));
pixelColor += texture2D(renderFrame, vec2(bxm1, by2));
pixelColor += texture2D(renderFrame, vec2(textureCoordinate.s,bym1));
pixelColor += texture2D(renderFrame, vec2(bx1,bym1));
pixelColor += texture2D(renderFrame, vec2(bxm1,bym1));
pixelColor += texture2D(renderFrame, vec2(bx2,bym1));
pixelColor += texture2D(renderFrame, vec2(bxm2,bym1));
pixelColor += texture2D(renderFrame, vec2(bx1, bym2));
pixelColor += texture2D(renderFrame, vec2(bx1, bym2));
pixelColor += texture2D(renderFrame, vec2(bxm1, bym2));
pixelColor /= 21.0;
#endif
// DOF_QUALITY == 2
#if DOF_QUALITY == 1
// Render low quality circle of confusion
// ___X___
// __XXX__
// _XXXXX_
// __XXX__
// ___X___
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_2));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_1, -PIXEL_SHIFT_1));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_1));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_1, -PIXEL_SHIFT_1));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_2, 0));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_1, 0));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_1, 0));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_2, 0));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(-PIXEL_SHIFT_1, -PIXEL_SHIFT_1));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_1));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(+PIXEL_SHIFT_1, -PIXEL_SHIFT_1));
pixelColor += texture2D(renderFrame, textureCoordinate + vec2(0, -PIXEL_SHIFT_2));
pixelColor /= 13.0;
#endif
// DOF_QUALITY == 1
#if DOF_QUALITY > 0
mediump float focusDepth = texture2D(depthFrame, vec2(0.5, 0.5)).r;
mediump float blurAmount = clamp((depth - focusDepth) * 10.0, 0.0, 1.0);
pixelColor = pixelColor * blurAmount + renderColor * (1.0 - blurAmount);
#endif
// ---- DEPTH_OF_FIELD END ----
// ---- NIGHT_TIME START ----
// Un-comment to enable night time / flash effect
// pixelColor *= 1.0 - clamp((depth - 0.7) * 2.0, 0.0, 1.0);
// ---- NIGHT_TIME END ----
// ---- VIDEO_BG START ----
/*
// Overlay video background
if(depth == 1.0) {
//FILTER COLOR BY CALCULATING PER PIXEL DOT PRODUCT
pixelColor = vec4(dot(vec3(texture2D(videoFrame, textureCoordinate)), vec3(.222, .707, .071)));
}
*/
// ---- VIDEO_BG END ----
// ---- VIGNETTE START ----
// Render vignette effect
// pixelColor *= 1.0 - clamp((distance(textureCoordinate, vec2(0.5, 0.5)) - 0.4), 0.0, 1.0);
// ---- VIGNETTE END ----
gl_FragColor = pixelColor;
//PASSTHROUGH STATEMENT
// gl_FragColor = texture2D(depthFrame, textureCoordinate);
//gl_FragColor = vec4(vec3(blurAmount), 1.0);
}

View File

@@ -0,0 +1,10 @@
attribute vec4 position;
attribute lowp vec4 inputTextureCoordinate;
varying mediump vec2 textureCoordinate;
void main()
{
gl_Position = position;
textureCoordinate = inputTextureCoordinate.xy;
}

View File

@@ -0,0 +1,35 @@
// -- Per Pixel lighting, test 3 --
uniform sampler2D diffuseTexture, specularTexture, normalTexture;
varying mediump vec2 texCoord;
varying mediump vec3 lightVec;
varying mediump vec3 halfVec;
uniform lowp vec3 material_ambient, material_diffuse, material_specular;
void main()
{
// compute diffuse lighting
mediump vec4 diffuseMaterial = texture2D(diffuseTexture, texCoord);
/*
// lookup normal from normal map, move from [0,1] to [-1, 1] range, normalize
mediump vec3 normal = normalize(2.0 * texture2D(normalTexture,texCoord).rgb - 1.0);
mediump float lamberFactor= max (dot (lightVec, normal), 0.0);
// Add ambient light and alpha component from diffuse texture map
gl_FragColor = vec4(material_ambient, diffuseMaterial.w);
// compute specular lighting
mediump vec3 specularLight = vec3(texture2D(specularTexture, texCoord)); // Specular value comes from a texture
mediump float shininess = pow (max (dot (halfVec, normal), 0.0), 2.0);
// Add diffuse light
gl_FragColor += vec4(vec3(diffuseMaterial) * material_diffuse * lamberFactor, 0.0);
// Add specular light
gl_FragColor += vec4(material_specular * specularLight * shininess, 0.0);
*/
//gl_FragColor = vec4(1.0, 1.0, 1.0, 1.0);
gl_FragColor = diffuseMaterial;
}

View File

@@ -0,0 +1,58 @@
const mediump vec3 LightPos = vec3(40, 20.0, -90.0);
attribute highp vec3 myVertex, myNormal;
attribute highp vec3 myTangent;
attribute mediump vec2 myUV;
uniform highp mat4 myMVPMatrix, myModelView; // mvpmatrix is the result of multiplying the model, view, and projection matrices
uniform highp mat3 myModelViewIT;
uniform lowp vec3 material_ambient, material_diffuse, material_specular;
varying mediump vec2 texCoord;
/*
varying mediump vec3 lightVec;
varying mediump vec3 halfVec;
*/
//varying mediump vec3 eyeVec;
void main()
{
// Transform position
gl_Position = myMVPMatrix * vec4(myVertex,1.0);
// Pass UV co-ordinates
texCoord = myUV.st;
/*
// Building the matrix Eye Space -> Tangent Space
vec3 n = normalize(vec3(myModelView * vec4(myNormal, 1.0)));
vec3 t = normalize(vec3(myModelView * vec4(myTangent, 1.0)));
vec3 b = cross(n, t);
vec3 vertexPosition = vec3(myMVPMatrix * vec4(myVertex, 1.0));
vec3 lightDir = normalize(LightPos - vertexPosition);
// transform light and half angle vectors by tangent basis
vec3 v;
v.x = dot(lightDir, t);
v.y = dot(lightDir, b);
v.z = dot(lightDir, n);
lightVec = normalize(v);
// v.x = dot(vertexPosition, t);
// v.y = dot(vertexPosition, b);
// v.z = dot(vertexPosition, n);
// eyeVec = normalize(v);
vertexPosition = normalize(vertexPosition);
// Normalize the halfVector to pass it to the fragment shader
vec3 halfVector = normalize((vertexPosition + lightDir) / 2.0);
v.x = dot (halfVector, t);
v.y = dot (halfVector, b);
v.z = dot (halfVector, n);
halfVec = normalize (v);
*/
}