# Thread: OGLES2 shader

1. ## OGLES2 shader

http://en.wikipedia.org/wiki/Blinn–Phong_shading_model
http://www.arcsynthesis.org/gltut/Il...g%20Model.html
http://www.arcsynthesis.org/gltut/Il...g%20Model.html

vec3 E = normalize(-position);
vec3 HalfVector = normalize(L + E);
float NdotH = dot(normal, HalfVector);

and

vec3 E = normalize(-position);
vec3 R = reflect(-L, normal);
float rDotE = dot(R,E);
////

in file OGLES2xxxxx.vsh we use Blinn shading and which one you feel good for proton 3d sdk??

2. ## Blinn shading explain

http://stackoverflow.com/questions/3...in-modern-glsl
vec3 V = -out_Vertex;// really it is (eye - vertexPosition), so (0,0,0) - out_Vertex

//in our shading COGLES2Solid.vsh
vec3 E = normalize(-position);

another examples
http://en.wikipedia.org/wiki/Blinn%E..._shading_model
vec3 viewDir = normalize(-vertPos);

http://www.arcsynthesis.org/gltut/Il...g%20Model.html
vec3 viewDirection = normalize(-cameraSpacePosition);
//////////////////////////////////////////////////
void pointLight(in int index, in vec3 position, in vec3 normal, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular)
{
vec3 L = uLightPosition[index] - position;
float D = length(L);
L = normalize(L);

float Attenuation = 1.0 / (uLightAttenuation[index].x + uLightAttenuation[index].y * D +
uLightAttenuation[index].z * D * D);

ambient += uLightAmbient[index] * Attenuation;

//dot(objnormal and lightvector) = cos(theta)
float NdotL = dot(normal, L);

if (NdotL > 0.0)
{
diffuse += uLightDiffuse[index] * (NdotL * Attenuation);

//Blinn shading, in camera space which the camera position is (0,0,0)
vec3 E = normalize(-position);
vec3 HalfVector = normalize(L + E);
float NdotH = dot(normal, HalfVector);

if (NdotH > 0.0)
{
float SpecularFactor = pow(NdotH, uMaterialShininess);
specular += uLightSpecular[index] * (SpecularFactor * Attenuation);
}
}
}

3. ## PSP shader full examples

https://code.google.com/p/jpcsp/sour...er.vert?r=1639

attribute vec4 psp_weights1;
attribute vec4 psp_weights2;
uniform float psp_zPos;
uniform float psp_zScale;
uniform ivec3 psp_matFlags; // Ambient, Diffuse, Specular
uniform ivec4 psp_lightType;
uniform ivec4 psp_lightKind;
uniform ivec4 psp_lightEnabled;
uniform mat4 psp_boneMatrix[8];
uniform int psp_numberBones;
uniform bool texEnable;
uniform bool lightingEnable;

float calculateAttenuation(in int i, in float dist) {
return clamp(1.0 / (gl_LightSource[i].constantAttenuation +
gl_LightSource[i].linearAttenuation * dist +
gl_LightSource[i].quadraticAttenuation * dist * dist), 0.0, 1.0);
}

void directionalLight(in int i, in vec3 N, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular) {
vec3 L = normalize(gl_LightSource[i].position.xyz);
float nDotL = dot(N, L);
if (nDotL > 0.0) {
vec3 H = gl_LightSource[i].halfVector.xyz;
float nDotH = dot(N,H);
if (nDotH > 0.0) {
float pf = pow(nDotH, gl_FrontMaterial.shininess);
specular += gl_LightSource[i].specular * pf;
}
diffuse += gl_LightSource[i].diffuse * nDotL;
}

ambient += gl_LightSource[i].ambient;
}

void pointLight(in int i, in vec3 N, in vec3 V, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular) {
vec3 D = gl_LightSource[i].position.xyz - V;
vec3 L = normalize(D);

float dist = length(D);
float attenuation = calculateAttenuation(i, dist);

float nDotL = dot(N,L);

if (nDotL > 0.0) {
// TODO Which model is correct?
if (true) {
vec3 E = normalize(-V);
vec3 R = reflect(-L, N);

float rDotE = dot(R,E);
if (rDotE > 0.0) {
float pf = pow(rDotE, gl_FrontMaterial.shininess);
specular += gl_LightSource[i].specular * attenuation * pf;
}
} else {
vec3 H = normalize(L + vec3(0.0, 0.0, 1.0));
float nDotH = dot(N,H);
if (nDotH > 0.0) {
float pf = pow(nDotH, gl_FrontMaterial.shininess);
specular += gl_LightSource[i].specular * attenuation * pf;
}
}
diffuse += gl_LightSource[i].diffuse * attenuation * nDotL;
}

ambient += gl_LightSource[i].ambient * attenuation;
}

void spotLight(in int i, in vec3 N, in vec3 V, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular) {
vec3 D = gl_LightSource[i].position.xyz - V;
vec3 L = normalize(D);

// Check if point on surface is inside cone of illumination
float spotEffect = dot(normalize(gl_LightSource[i].spotDirection), -L);

if (spotEffect >= gl_LightSource[i].spotCosCutoff) {
float dist = length(D);
float attenuation = calculateAttenuation(i, dist);

attenuation *= pow(spotEffect, gl_LightSource[i].spotExponent);

float nDotL = dot(N,L);
if (nDotL > 0.0) {
// TODO Which model is correct?
if (true) {
vec3 E = normalize(-V);
vec3 R = reflect(-L, N);

float rDotE = dot(R,E);
if (rDotE > 0.0) {
float pf = pow(rDotE, gl_FrontMaterial.shininess);
specular += gl_LightSource[i].specular * attenuation * pf;
}
} else {
vec3 H = normalize(L + vec3(0.0, 0.0, 1.0));
float nDotH = dot(N,H);
if (nDotH > 0.0) {
float pf = pow(nDotH, gl_FrontMaterial.shininess);
specular += gl_LightSource[i].specular * attenuation * pf;
}
}
diffuse += gl_LightSource[i].diffuse * attenuation * nDotL;
}

ambient += gl_LightSource[i].ambient * attenuation;
}
}

void calculateLighting(in vec3 N, in vec3 V, inout vec4 ambient, inout vec4 diffuse, inout vec4 specular) {
for (int i = 0; i < 4; i++) {
if (psp_lightEnabled[i] != 0) {
if(psp_lightType[i] == 0)
directionalLight(i, N, ambient, diffuse, specular);
else if(psp_lightType[i] == 1)
pointLight(i, N, V, ambient, diffuse, specular);
else if(psp_lightType[i] == 2)
spotLight(i, N, V, ambient, diffuse, specular);
}
}
}

vec4 getEyeCoordinatePosition() {
return gl_ModelViewMatrix * gl_Vertex;
}

vec3 getEyeCoordinatePosition3(in vec4 eyeCoordinatePosition) {
return vec3(eyeCoordinatePosition) / eyeCoordinatePosition.w;
}

vec4 doLight(in vec4 eyeCoordinatePosition, in vec4 matAmbient, in vec4 matDiffuse, in vec4 matSpecular, in vec3 normal) {
vec4 ambient = vec4(0.0);
vec4 diffuse = vec4(0.0);
vec4 specular = vec4(0.0);
vec3 n = normalize(gl_NormalMatrix * normal);

calculateLighting(n, getEyeCoordinatePosition3(eyeCoordinatePosition), ambient, diffuse, specular);

ambient += gl_LightModel.ambient;
vec4 color = (ambient * matAmbient) +
(diffuse * matDiffuse) +
(specular * matSpecular) +
gl_FrontMaterial.emission;

return clamp(color, 0.0, 1.0);
}

vec4 getFrontColor(in vec4 eyeCoordinatePosition, in vec3 normal) {
if (!lightingEnable) {
return gl_Color;
}

vec4 matAmbient = psp_matFlags[0] != 0 ? gl_Color : gl_FrontMaterial.ambient;
vec4 matDiffuse = psp_matFlags[1] != 0 ? gl_Color : gl_FrontMaterial.diffuse;
vec4 matSpecular = psp_matFlags[2] != 0 ? gl_Color : gl_FrontMaterial.specular;

return doLight(eyeCoordinatePosition, matAmbient, matDiffuse, matSpecular, normal);
}

vec4 getPosition(inout vec3 normal) {
if (psp_numberBones == 0) {
return gl_Vertex;
}

vec4 position = vec4(0.0, 0.0, 0.0, gl_Vertex.w);
vec4 vertex = vec4(gl_Vertex.xyz, 1.0);
normal = vec3(0.0, 0.0, 0.0);
for (int i = 0; i < psp_numberBones; i++) {
float weight = (i <= 3 ? psp_weights1[i] : psp_weights2[i - 4]);
if (weight != 0.0) {
position.xyz += vec3(psp_boneMatrix[i] * vertex) * weight;

// Normals shouldn't be translated
normal += mat3(psp_boneMatrix[i]) * gl_Normal * weight;
}
}

return position;
}

float getFogFragCoord(vec4 eyeCoordinatePosition) {
return abs(eyeCoordinatePosition.z);
}

void main() {
vec4 eyeCoordinatePosition = getEyeCoordinatePosition();
vec3 normal = gl_Normal;

gl_Position = gl_ModelViewProjectionMatrix * getPosition(normal);
gl_FrontColor = getFrontColor(eyeCoordinatePosition, normal);
gl_FogFragCoord = getFogFragCoord(eyeCoordinatePosition);
gl_TexCoord[0] = gl_TextureMatrix[0] * gl_MultiTexCoord0;

// gl_Position = ftransform();
// gl_Position = gl_ModelViewProjectionMatrix * gl_Vertex;
// gl_Position.z = gl_Position.z * psp_zScale + psp_zPos * gl_Position.w;
}

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•