Skip to content

Commit

Permalink
finished spotlight, finished demo.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andor Salga committed Jun 24, 2011
1 parent 3fff626 commit 1348723
Show file tree
Hide file tree
Showing 3 changed files with 240 additions and 22 deletions.
161 changes: 161 additions & 0 deletions demos/fixed_function/demo.js
@@ -0,0 +1,161 @@
var ps, pointCloud;
var r1 = 0, r2 = 0, r3 = 0;

// Create an orbit camera halfway between the closest and farthest point
var cam = new OrbitCam({closest:10, farthest:350, distance: 300});
var isDragging = false;
var rotationStartCoords = [0, 0];

function baseLight(light){
var lightName = "lights" + light.id;
ps.uniformi( lightName + ".isOn", true);

ps.uniformf( lightName + ".position", light.position);

if(light.ambient){ps.uniformf( lightName + ".ambient", light.ambient);}
if(light.diffuse){ps.uniformf( lightName + ".diffuse", light.diffuse);}
if(light.specular){ps.uniformf( lightName + ".specular", light.specular);}
}

function dirLight(light){
baseLight(light);
ps.uniformi( "lights" + light.id + ".type", 1);
}

function pointLight(light){
baseLight(light);
var lightName = "lights" + light.id;
ps.uniformi( lightName + ".type", 2);
ps.uniformf( lightName + ".attenuation", light.attenuation);
}

function spotLight(light){
baseLight(light);
var lightName = "lights" + light.id;
ps.uniformi( lightName + ".type", 3);

ps.uniformf( lightName + ".angle", light.angle);
ps.uniformf( lightName + ".direction", light.direction);
ps.uniformf( lightName + ".concentration", light.concentration);
ps.uniformf( lightName + ".attenuation", light.attenuation);
}

function zoom(amt){
if(amt < 0){
cam.goCloser(-amt);
}
else{
cam.goFarther(amt);
}
}

function mousePressed(){
rotationStartCoords[0] = ps.mouseX;
rotationStartCoords[1] = ps.mouseY;

isDragging = true;
}

function mouseReleased(){
isDragging = false;
}

function render(){

if(isDragging === true){
// how much was the cursor moved compared to last time
// this function was called?
var deltaX = ps.mouseX - rotationStartCoords[0];
var deltaY = ps.mouseY - rotationStartCoords[1];

// now that the camera was updated, reset where the
// rotation will start for the next time this function is called.
rotationStartCoords = [ps.mouseX, ps.mouseY];

cam.yaw(-deltaX * 0.015);
cam.pitch(deltaY * 0.015);
}

var c = pointCloud.getCenter();
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();

r1 += 0.035;
r2 += 0.04;
r3 -= 0.05;

var dir;
var mat;

//
ps.loadMatrix([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]);
ps.rotateZ(r1);
dir = V3.$(0, 1, 0);
mat = ps.peekMatrix();
dir = V3.mul4x4(mat, dir);
dirLight({id:2, ambient:[0.2, 0.2, 0.2], diffuse:[0,0.7,0], position:dir});

// BLUE POINT LIGHT
ps.loadMatrix([1,0,0,0,0,1,0,0,0,0,1,0,0,0,0,1]);
ps.rotateY(r2);
dir = V3.$(0, 0, 100);
mat = ps.peekMatrix();
dir = V3.mul4x4(mat, dir);
pointLight({id:1, ambient:[0.2, 0.2, 0.2], diffuse:[0,0,1], attenuation:[1,0,0], position: dir});

// SPOT LIGHT
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.translate(-cam.position[0], -cam.position[1], -cam.position[2]);
ps.rotateY(r3);

var mvm = ps.peekMatrix();
var nx = 0;
var ny = 0;
var nz = -1;

var dir = [mvm[0] * nx + mvm[4] * ny + mvm[8] * nz,
mvm[1] * nx + mvm[5] * ny + mvm[9] * nz,
mvm[2] * nx + mvm[6] * ny + mvm[10] * nz
];

pos = V3.$(0, 0, 250);
mat = ps.peekMatrix();
newPos = V3.mul4x4(mat, pos);

spotLight({id:5, angle: .5, concentration:20, attenuation:[1,0,0], diffuse:[1,0,0], direction: dir, position: newPos});

ps.popMatrix();

ps.clear();
ps.render(pointCloud);
}

function start(){
ps = new PointStream();
ps.setup(document.getElementById('canvas'));
ps.background([0,0,0,1]);

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.uniformi("matOn", true);
ps.uniformf("matShininess", 250);
ps.uniformf("matAmbient", [0.5, 1, 0.5]);
ps.uniformf("matDiffuse", [1, 1, 1]);
ps.uniformf("matSpecular", [1, 1, 0.5]);

ps.pointSize(30.0);

pointCloud = ps.load("../../clouds/andor.ply");

ps.onRender = render;
ps.onMouseScroll = zoom;
ps.onMousePressed = mousePressed;
ps.onMouseReleased = mouseReleased;
}
15 changes: 15 additions & 0 deletions demos/fixed_function/index.html
@@ -0,0 +1,15 @@
<html>

<head>
<link rel="stylesheet" type="text/css" href="../style.css" />
<script src="../../xbps.js"></script>
<script src="../../libs/orbitcam.js"></script>
<script src="../../shaders/fixed_function.js"></script>
<script src="demo.js"></script>
</head>

<body onLoad="start();" bgcolor="#222222">
<canvas id="canvas" width="500" height="500"></canvas><span id="thumbnails"></span><br />
</body>

</html>
86 changes: 64 additions & 22 deletions shaders/fixed_function.vs
Expand Up @@ -23,8 +23,9 @@ struct Light {

vec3 position;
vec3 direction;

float angle;
vec3 halfVector;
vec3 attenuation;
float concentration;
};

Expand All @@ -38,6 +39,7 @@ uniform Light lights4;
uniform Light lights5;
uniform Light lights6;
uniform Light lights7;
const int MAX_LIGHTS = 8;

// GLSL does not support switch
Light getLight(int index){
Expand All @@ -60,13 +62,12 @@ 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){
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) )));
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 );
}
Expand All @@ -77,8 +78,7 @@ void DirectionalLight(inout vec3 ambient, inout vec3 diffuse, inout vec3 spec, i

/*
*/
void PointLight(inout vec3 ambient, inout vec3 diffuse, inout vec3 specular, in vec3 vertNormal,
in vec3 ecPos, in Light light){
void pointLight(inout vec3 ambient, inout vec3 diffuse, inout vec3 specular, in vec3 normal, in vec3 ecPos, in Light light){
float powerfactor;

// Get the vector from the light to the vertex
Expand All @@ -90,12 +90,11 @@ void PointLight(inout vec3 ambient, inout vec3 diffuse, inout vec3 specular, in
// Normalize the light ray so it can be used in the dot product operation.
VP = normalize( VP );

//float attenuation = 1.0 / ( falloff[0] + ( falloff[1] * d ) + ( falloff[2] * d * d ));
float attenuation = 1.0 / ( 1.0 + 0.0 + 0.0);
float attenuation = 1.0 / ( light.attenuation[0] + ( light.attenuation[1] * d ) + ( light.attenuation[2] * d * d ));

float nDotVP = max( 0.0, dot( vertNormal, VP ));
float nDotVP = max( 0.0, dot( normal, VP ));
vec3 halfVector = normalize( VP - normalize(ecPos) );
float nDotHV = max( 0.0, dot( vertNormal, halfVector ));
float nDotHV = max( 0.0, dot( normal, halfVector ));

if( nDotVP == 0.0) {
powerfactor = 0.0;
Expand All @@ -111,6 +110,49 @@ void PointLight(inout vec3 ambient, inout vec3 diffuse, inout vec3 specular, in

/*
*/
void spotLight( inout vec3 ambient, inout vec3 diffuse, inout vec3 spec, in vec3 normal, in vec3 ecPos, in Light light ) {
float spotAttenuation;
float powerfactor;

// calculate the vector from the current vertex to the light.
vec3 VP = light.position - ecPos;

// get the distance from the spotlight and the vertex
float d = length( VP );
VP = normalize( VP );

float attenuation = 1.0 / ( light.attenuation[0] + ( light.attenuation[1] * d ) + ( light.attenuation[2] * d * d ) );

// dot product of the vector from vertex to light and light direction.
float spotDot = dot( -VP, normalize( light.direction ) );

// if the vertex falls inside the cone
spotAttenuation = attenuation;
if( spotDot > cos( light.angle ) ){
spotAttenuation = pow( spotDot, light.concentration );
}
else{
spotAttenuation = 0.0;
}
attenuation *= spotAttenuation;

vec3 halfVector = normalize(VP + ecPos);
float nDotVP = max(0.0, dot(normal, VP));
float nDotHV = max(0.0, dot(normal, halfVector));

if( nDotVP == 0.0 ) {
powerfactor = 0.0;
}
else {
powerfactor = pow(nDotHV, matShininess);
}

ambient += light.ambient * attenuation;
diffuse += light.diffuse * nDotVP * attenuation;
spec += matSpecular * powerfactor * attenuation;
}


void main(void) {
vec3 transNorm = vec3(ps_NormalMatrix * vec4(ps_Normal, 0.0));

Expand All @@ -126,39 +168,39 @@ void main(void) {
frontColor = vec4(ps_Color, 1.0);
}
else{
for(int i = 0; i < 8; i++){
for(int i = 0; i < MAX_LIGHTS; i++){
Light light = getLight(i);

if(light.isOn){
if(light.type == 1){
DirectionalLight(finalAmbient, finalDiffuse, finalSpecular, transNorm, ecPos, light);
directionalLight(finalAmbient, finalDiffuse, finalSpecular, transNorm, ecPos, light);
}
else if(light.type == 2){
PointLight(finalAmbient, finalDiffuse, finalSpecular, transNorm, ecPos, light);
pointLight(finalAmbient, finalDiffuse, finalSpecular, transNorm, ecPos, light);
}
else if(light.type == 3){

spotLight(finalAmbient, finalDiffuse, finalSpecular, transNorm, ecPos, light);
}
}
}
}

if(matOn){
frontColor = vec4( (ps_Color * matAmbient * finalAmbient), 1.0);
//(ps_Color * matDiffuse * finalDiffuse) , 1.0);
// (ps_Color * matSpecular * finalSpecular), 1.0);
frontColor = vec4( (ps_Color * matAmbient * finalAmbient) +
(ps_Color * matDiffuse * finalDiffuse) +
(ps_Color * matSpecular * finalSpecular), 1.0);
}
else{
frontColor = vec4( (ps_Color * finalAmbient) +
(ps_Color * finalDiffuse) +
(ps_Color * finalSpecular), 1.0);
frontColor = vec4((ps_Color * finalAmbient) +
(ps_Color * finalDiffuse) +
(ps_Color * finalSpecular), 1.0);
}

float dist = length(ecPos4);
float attn = ps_Attenuation[0] +
(ps_Attenuation[1] * dist) +
(ps_Attenuation[2] * dist * dist);

gl_PointSize = ps_PointSize * sqrt(1.0/attn);
gl_Position = ps_ProjectionMatrix * ecPos4;
}

0 comments on commit 1348723

Please sign in to comment.