Skip to content

Commit

Permalink
added gooch.
Browse files Browse the repository at this point in the history
  • Loading branch information
Andor authored and Andor committed May 5, 2011
1 parent 358665f commit 76a7f31
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 42 deletions.
74 changes: 32 additions & 42 deletions demos/all_shaders/demo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ var canvasCounter = 1;
var pointCloud;
var ps;

var progCartoon;
var progGooch;

var zoom = -65;
var left = 3;
var top = 5;
Expand Down Expand Up @@ -35,47 +38,29 @@ function render(){
}

if(canvasCounter === 4 ){
/* var ctx = ps.getContext();
ctx.enable(ctx.BLEND);
ctx.blendFunc(ctx.SRC_ALPHA, ctx.ONE_MINUS_SRC_ALPHA);

ps.uniformi("reflection", true);
ps.uniformf("lightPos", [0, 50, 0]);
ps.uniformf("uReflection", [.15, .15, .3, .8]);
ps.uniformf("mirrorPos", [0, 100, 0]);
ps.pushMatrix();
ps.rotateZ(Math.PI/2);
ps.translate(-c[0], -c[1], -c[2]);
ps.translate(left, top, zoom);
ps.render(pointCloud);
ps.popMatrix();
ps.pushMatrix();
ps.translate(-c[0], -c[1], -c[2]);
/* ps.useProgram(progCartoon);
ps.rotateZ(Math.PI/2);
ps.translate(-c[0], -c[1], -c[2]);
ps.translate(left, top, zoom);
ps.uniformi("uOutline", true);
var ctx = ps.getContext();
ctx.disable(ctx.DEPTH_TEST);
// ps.render(pointCloud);*/

ps.translate(0, -10, -60);
ps.rotateX(0);
ps.translate(0, -0, 0);
ps.scale(1, -1, 1);
ps.rotateY(0);
ps.render(pointCloud);
ps.popMatrix();
// Draw object
ps.uniformi("reflection", false);
ps.uniformf("lightPos", [0, 50, 50]);
ps.uniformf("uReflection", [1, 1, 1, 1]);
ps.pushMatrix();
ps.translate(0, -0, -30);
ps.rotateX(0);
ps.rotateY(0);
ps.render(pointCloud);
ps.popMatrix();*/
ps.useProgram(progGooch);
ps.rotateZ(Math.PI/2);
ps.translate(-c[0], -c[1], -c[2]);
ps.translate(left, top, zoom);

ps.uniformf("warmColor", [0.5, 0.5, 0.0]);
ps.uniformf("coolColor", [0, 0, 1]);
ps.uniformf("surfaceColor", [0.1, 0.1, 0.1]);

ps.render(pointCloud);
}

if(canvasCounter === 5){
ps.background([0,0,0,1]);
ps.clear();

var ctx = ps.getContext();
Expand Down Expand Up @@ -115,15 +100,20 @@ function start(cvs){

case 3:
pointCloud = ps.load(POINT_CLOUD_PATH);
var progObj = ps.createProgram(cartoonVert, cartoonFrag);
ps.useProgram(progObj);
progCartoon = ps.createProgram(cartoonVert, cartoonFrag);
ps.useProgram(progCartoon);
break;

case 4:
pointCloud = ps.load(POINT_CLOUD_PATH);
var progObj = ps.createProgram(reflectionVert, reflectionFrag);
// var progObj = ps.createProgram(cartoonVert, cartoonFrag);
ps.useProgram(progObj);

progCartoon = ps.createProgram(cartoonVert, cartoonFrag);
progGooch = ps.createProgram(gooch_vs, gooch_fs);
ps.useProgram(progGooch);

ps.uniformf("warmColor", [0.5, 0.5, 0.0]);
ps.uniformf("coolColor", [0, 0, 1]);
ps.uniformf("surfaceColor", [0.1, 0.1, 0.1]);
break;

case 5:
Expand All @@ -135,6 +125,6 @@ function start(cvs){
default:break;
}

ps.pointSize(5);
ps.pointSize(6);
ps.onRender = render;
}
1 change: 1 addition & 0 deletions demos/all_shaders/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
<script src="../../shaders/fixed_function.js"></script>
<script src="../../shaders/cartoon.js"></script>
<script src="../../shaders/xray.js"></script>
<script src="../../shaders/gooch.js"></script>
<script src="../../xbps.js"></script>
<script src="demo.js"></script>
<style>
Expand Down
103 changes: 103 additions & 0 deletions shaders/gooch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
var gooch_vs =

"varying vec3 ViewVec;" +
"varying vec3 ecPos1;" +
"varying vec3 tnorm;" +

"varying vec4 frontColor;" +

"attribute vec3 ps_Vertex;" +
"attribute vec3 ps_Normal;" +
"attribute vec4 ps_Color;" +

"uniform float ps_PointSize;" +
"uniform vec3 ps_Attenuation;" +

"uniform vec3 lightPos;" +

"uniform mat4 ps_ModelViewMatrix;" +
"uniform mat4 ps_ProjectionMatrix;" +
"uniform mat4 ps_NormalMatrix;" +

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

" vec4 ecPos4 = ps_ModelViewMatrix * vec4(ps_Vertex, 1.0);" +

" frontColor = ps_Color;" +

" if(ps_Normal == vec3(0.0, 0.0, 0.0)){" +
" frontColor = ps_Color; " +
" }" +

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

" tnorm = vec3(normalize(ps_NormalMatrix * vec4(ps_Normal, 0.0)));" +
" ecPos1 = vec3(ecPos4); "+
" ViewVec = normalize(-ecPos1);" +

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

var gooch_fs =
"#ifdef GL_ES\n" +
" precision highp float;\n" +
"#endif\n" +

"float DiffuseWarm = 0.5;" +
"float DiffuseCool = 0.5; " +

// parameters
"uniform vec3 surfaceColor;" +
"uniform vec3 warmColor;" +
"uniform vec3 coolColor;" +

"varying vec3 ViewVec; " +
"varying vec3 ecPos1; "+
"varying vec3 tnorm;" +

"void pointLight(in vec3 pos, in vec3 nviewVec, in vec3 ntnorm, inout float NdotL, inout float spec)" +
"{" +
" vec3 lightVec = normalize(pos - ecPos1);" +
" vec3 ReflectVec = normalize(reflect(lightVec, ntnorm));" +
" NdotL = (dot(lightVec, ntnorm) + 1.0) * 0.5;" +
" spec += max(dot(ReflectVec, -nviewVec), 0.0);" +
"}" +

"void c3dl_goochDirLight(in vec3 pos, in vec3 nviewVec, in vec3 ntnorm, inout float NdotL, inout float spec)" +
"{"+
// when the user specifies the the direction of the light, they are
// specifying the direction the light is going towards.
" vec3 lightVec = vec3(-pos);" +

// calculate how intense the light is. NdotL is added for each light.
" NdotL = (dot(lightVec, ntnorm) + 1.0) * 0.5;" +
" vec3 ReflectVec = normalize(reflect(lightVec, ntnorm));" +
" spec += max(dot(ReflectVec, -nviewVec), 0.0);" +
"}"+

/*
*/
"void main(void) {" +

" vec3 kcool = min(coolColor + DiffuseCool * surfaceColor, 1.0);"+
" vec3 kwarm = min(warmColor + DiffuseWarm * surfaceColor, 1.0);" +

" vec3 nviewVec = normalize(ViewVec);" +
" vec3 ntnorm = normalize(tnorm);" +

" float NdotL = 0.0;" +
" float spec = 0.0;" +

" pointLight(vec3(0.0, 10.0, -40.0), nviewVec, ntnorm, NdotL, spec); "+

" NdotL = clamp(NdotL, 0.0, 1.0);"+

" vec3 kfinal = mix(kcool, kwarm, NdotL);" +
" spec = pow(spec, 17.0);" +
" gl_FragColor = vec4(min(kfinal + spec, 1.0), 1.0);" +
"}";

0 comments on commit 76a7f31

Please sign in to comment.