Permalink
Browse files

added dir light.

  • Loading branch information...
1 parent b11e728 commit 3fff6261d0a58db036bed884c59ec2cbca20e7fc Andor Salga committed Jun 23, 2011
Showing with 131 additions and 26 deletions.
  1. +74 −11 demos/acorn/acorn.js
  2. +9 −0 libs/orbitcam.js
  3. +8 −0 psapi.js
  4. +40 −15 shaders/fixed_function.vs
View
85 demos/acorn/acorn.js
@@ -1,7 +1,8 @@
var ps, acorn;
-
+var r = 0;
+var r2 = 0;
// Create an orbit camera halfway between the closest and farthest point
-var cam = new OrbitCam({closest:10, farthest:100, distance: 40});
+var cam = new OrbitCam({closest:10, farthest:100, distance: 70});
var isDragging = false;
var rotationStartCoords = [0, 0];
@@ -48,6 +49,40 @@ function keyDown(){
}
function render(){
+
+ ps.pushMatrix();
+ ps.loadMatrix([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]);
+ ps.rotateY(r+=0.1);
+ ps.translate(0,0,1000);
+ var mat = ps.peekMatrix();
+
+ var vec = V3.$(0, 0, 0);
+ var newpos = V3.mul4x4(mat, vec);
+
+ ps.uniformi("lights0.isOn", false);
+ ps.uniformf("lights0.ambient", [0.1,0.1,0.1]);
+ ps.uniformf("lights0.position", newpos);
+ ps.uniformf("lights0.specular", [0, 1, 0]);
+
+ ps.loadMatrix([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]);
+ ps.rotateZ(r2+=0.1);
+ ps.translate(100,0,100);
+ mat = ps.peekMatrix();
+
+ vec = V3.$(0, 0, 0);
+ newpos = V3.mul4x4(mat, vec);
+
+ ps.uniformi("lights1.isOn", false);
+ ps.uniformi("lights1.type", 2);
+ ps.uniformf("lights1.position", newpos);
+ ps.uniformf("lights1.diffuse", [0.7, 0.7, 0.7]);
+ ps.uniformf("lights1.specular", [1, 0, 0]);
+
+
+
+ps.popMatrix();
+
+
if(isDragging === true){
// how much was the cursor moved compared to last time
@@ -67,6 +102,36 @@ function render(){
ps.multMatrix(M4x4.makeLookAt(cam.position, cam.direction, cam.up));
ps.translate(-cam.position[0]-c[0], -cam.position[1]-c[1], -cam.position[2]-c[2] );
+
+
+
+
+
+ ps.pushMatrix();
+ ps.loadMatrix([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]);
+ //ps.multMatrix(M4x4.makeLookAt(cam.position, cam.direction, cam.up));
+
+
+ ps.rotateY(r2+=0.1);//,cam.direction);
+ // ps.translate(0,0,1);
+ ps.translate(-cam.position[0], -cam.position[1], -cam.position[2] );
+
+ vec = V3.$(0, 0, 1);
+ mat = ps.peekMatrix();
+ newpos = V3.mul4x4(mat, vec);
+
+ ps.uniformi("lights2.isOn", true);
+ ps.uniformi("lights2.type", 1);
+ ps.uniformf("lights2.ambient", [0.1, 0.1, 0.1]);
+ ps.uniformf("lights2.diffuse", [0.3, 0.9, 0.3]);
+ ps.uniformf("lights2.position", newpos);
+ ps.popMatrix();
+
+
+
+
+
+
ps.clear();
ps.render(acorn);
@@ -100,22 +165,20 @@ function start(){
ps.setup(document.getElementById('canvas'));
ps.background([1, 1, 1, 1.0]);
- fixedFunctionProg = ps.createProgram(fixedFunctionVert, fixedFunctionFrag);
+ var vert = ps.getShaderStr("../../shaders/fixed_function.vs");
+ var frag = ps.getShaderStr("../../shaders/fixed_function.fs");
+
+ fixedFunctionProg = ps.createProgram(vert, frag);
ps.useProgram(fixedFunctionProg);
- ps.uniformf("lights0.on", 1);
- ps.uniformi("matOn", 1);
+ ps.uniformi("matOn", 0);
ps.uniformf("matShininess", 100);
ps.uniformf("matAmbient", [0.3, 0.3, 0.3]); // 204/255
ps.uniformf("matDiffuse", [1, 1, 1]); // 51/255
ps.uniformf("matSpecular", [0.7, 1, 1]);
- ps.uniformf("lights0.ambient", [0.5, 0.5, 0.5]);
- ps.uniformf("lights0.diffuse", [0.7, 0.7, 0.7]);
- ps.uniformf("lights0.specular", [0.6, 0.6, 0.6]);
- ps.uniformf("lights0.position", [0, 50, 50]);
-
+
/* ps.uniformi("matOn", 1);
ps.uniformf("matShininess", 18);
ps.uniformf("matAmbient", [0.8, 0.8, 0.8]); // 204/255
@@ -129,7 +192,7 @@ function start(){
ps.pointSize(3.0);
- acorn = ps.load("../../clouds/acorn.psi");
+ acorn = ps.load("../../clouds/Mickey_Mouse.psi");
ps.onRender = render;
ps.onMouseScroll = zoom;
View
9 libs/orbitcam.js
@@ -106,6 +106,15 @@ var OrbitCam = (function(){
});
/**
+ Get the camera's left vector.
+
+ @return {Array} Array of three values.
+ */
+ this.__defineGetter__("left", function(){
+ return left;
+ });
+
+ /**
Move the camera close to the orbit point. If it can't move 'distance'
closer, it will move as close to the orbit point as it can.
View
8 psapi.js
@@ -1430,6 +1430,13 @@ var PointStream = (function() {
this.loadMatrix(M4x4.mul(this.peekMatrix(), rotMat));
};
+ /**
+ */
+ this.rotate = function(radians, a){
+ var rotMat = M4x4.rotate(radians, a, M4x4.I);
+ this.loadMatrix(M4x4.mul(this.peekMatrix(), rotMat));
+ };
+
/*********************************************/
/********** Matrix Stack Operations **********/
/*********************************************/
@@ -1497,6 +1504,7 @@ var PointStream = (function() {
if (XHR.status !== 200 && XHR.status !== 0) {
throw ("XHR failed: " + XHR.status);
}
+
else{
shaderSrc = XHR.responseText;
}
View
55 shaders/fixed_function.vs
@@ -60,6 +60,21 @@ uniform vec3 matDiffuse;
uniform vec3 matSpecular;
uniform float matShininess;
+
+/*
+*/
+void DirectionalLight(inout vec3 ambient, inout vec3 diffuse, inout vec3 spec, in vec3 normal, in vec3 ecPos, in Light light){
+ float powerfactor = 0.0;
+ float nDotVP = max(0.0, dot( normal, normalize(-light.position) ));
+ float nDotVH = max(0.0, dot( normal, normalize(-light.position-normalize(ecPos) )));
+ if( nDotVP != 0.0 ){
+ powerfactor = pow( nDotVH, matShininess );
+ }
+ ambient += light.ambient;
+ diffuse += light.diffuse * nDotVP;
+ spec += matSpecular * powerfactor;
+}
+
/*
*/
void PointLight(inout vec3 ambient, inout vec3 diffuse, inout vec3 specular, in vec3 vertNormal,
@@ -103,31 +118,41 @@ void main(void) {
vec3 ecPos = (vec3(ecPos4))/ecPos4.w;
// calculate color
- vec3 finalAmbient = vec3(0.0, 0.0, 0.0);
- vec3 finalDiffuse = vec3(0.0, 0.0, 0.0);
- vec3 finalSpecular = vec3(0.0, 0.0, 0.0);
+ vec3 finalAmbient = vec3(0.0);
+ vec3 finalDiffuse = vec3(0.0);
+ vec3 finalSpecular = vec3(0.0);
if(ps_Normal == vec3(0.0, 0.0, 0.0)){
frontColor = vec4(ps_Color, 1.0);
}
else{
- if(lights0.isOn){
- PointLight(finalAmbient, finalDiffuse, finalSpecular, transNorm, ecPos, lights0);
+ for(int i = 0; i < 8; i++){
+ Light light = getLight(i);
+
+ if(light.isOn){
+ if(light.type == 1){
+ DirectionalLight(finalAmbient, finalDiffuse, finalSpecular, transNorm, ecPos, light);
+ }
+ else if(light.type == 2){
+ PointLight(finalAmbient, finalDiffuse, finalSpecular, transNorm, ecPos, light);
+ }
+ else if(light.type == 3){
+
+ }
+ }
}
}
-
-if(matOn){
- frontColor = vec4( (ps_Color * matAmbient * finalAmbient) +
- (ps_Color * matDiffuse * finalDiffuse) +
- (ps_Color * matSpecular * finalSpecular), 1.0);
-}
-else{
- frontColor = vec4( (ps_Color * finalAmbient) +
+ if(matOn){
+ frontColor = vec4( (ps_Color * matAmbient * finalAmbient), 1.0);
+ //(ps_Color * matDiffuse * finalDiffuse) , 1.0);
+ // (ps_Color * matSpecular * finalSpecular), 1.0);
+ }
+ else{
+ frontColor = vec4( (ps_Color * finalAmbient) +
(ps_Color * finalDiffuse) +
(ps_Color * finalSpecular), 1.0);
-}
-
+ }
float dist = length(ecPos4);
float attn = ps_Attenuation[0] +

0 comments on commit 3fff626

Please sign in to comment.