Skip to content

Commit

Permalink
added tutorial notes, cleaned asc parser, cleaned demos, fixed shader…
Browse files Browse the repository at this point in the history
… demos
  • Loading branch information
Andor authored and Andor committed Feb 8, 2011
1 parent 85f92f1 commit e3c7fe3
Show file tree
Hide file tree
Showing 19 changed files with 677 additions and 275 deletions.
2 changes: 1 addition & 1 deletion demos/many_clouds/index.html
Expand Up @@ -10,7 +10,7 @@
<script src="../../xbps.js"></script>
<script src="shaders/acorn_shader.js"></script>
<script src="shaders/mickey_shader.js"></script>
<script src="many_clouds.js"></script>
<script src="js/many_clouds.js"></script>
</head>

<body onload="start();" bgcolor="#222222" text="#FFFFFF">
Expand Down
Expand Up @@ -128,9 +128,7 @@ function render() {
function start(){
ps = new PointStream();
ps.setup(document.getElementById('canvas'));
ps.background([.3,.6, .9,1]);
ps.background([1,1,1,1]);
//ps.background([1,1,1,0.1]);
ps.background([1,1,1,1]);

ps.onRender = render
ps.onMouseScroll = zoom;
Expand Down
43 changes: 13 additions & 30 deletions demos/many_clouds/shaders/acorn_shader.js
Expand Up @@ -16,52 +16,35 @@ var acornVertShader =
"attribute vec4 ps_Color;" +

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

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

"void PointLight(inout vec3 col, in vec3 ecPos, in vec3 vertNormal, in vec3 eye ) {" +
// Get the vector from the light to the vertex
" vec3 VP = vec3(0.0, 150.0, 150.0) - ecPos;" +

// Get the distance from the current vector to the light position
" float d = length( VP ); " +

// Normalize the light ray so it can be used in the dot product operation.
" VP = normalize( VP );" +

" float attenuation = 1.0 / ( 1.0 + ( d ) + ( d * d ));" +
" float nDotVP = max( 0.0, dot( vertNormal, VP ));" +
" vec3 halfVector = normalize( VP + eye );" +
" float nDotHV = max( 0.0, dot( vertNormal, halfVector ));" +
" col += vec3(0.7, 0.7, 1.0) * nDotVP;" +
"void PointLight(inout vec3 col, in vec3 ecPos, in vec3 vertNormal) {" +
" vec3 VP = -ecPos;" +
" VP = normalize(VP);" +
" float nDotVP = max(0.0, dot(vertNormal, VP));" +
" col = vec3(1.0, 1.0, 1.0) * nDotVP;" +
"}" +

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

" vec4 ecPos4 = ps_ModelViewMatrix * vec4(ps_Vertex, 1.0);" +
" vec3 ecPos = (vec3(ecPos4))/ecPos4.w;" +
" vec3 eye = vec3( 0.0, 0.0, 1.0 );" +

" vec3 col = vec3(0.0, 0.0, 0.0);" +
" PointLight(col, ecPos, transNorm, eye);" +
" vec3 col;" +
" PointLight(col, ecPos, transNorm);" +

" frontColor = ps_Color * vec4(col, 1.0);" +

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

" if(attn > 0.0){" +
" gl_PointSize = ps_PointSize * sqrt(1.0/attn);" +
" }" +
" else{" +
" gl_PointSize = 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;" +
"}";
8 changes: 4 additions & 4 deletions demos/many_clouds/shaders/mickey_shader.js
Expand Up @@ -17,7 +17,7 @@ var mickeyVertShader =
"attribute vec4 ps_Color;" +

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

"uniform mat4 ps_ModelViewMatrix;" +
"uniform mat4 ps_ProjectionMatrix;" +
Expand All @@ -36,9 +36,9 @@ var mickeyVertShader =
" vec3 ecPos = (vec3(ecPos4))/ecPos4.w;" +

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

" gl_PointSize = attn > 0.0 ? ps_PointSize * sqrt(1.0/attn) : 1.0;" +

Expand Down
34 changes: 0 additions & 34 deletions notes.txt

This file was deleted.

62 changes: 31 additions & 31 deletions psapi.js
Expand Up @@ -122,7 +122,7 @@ var PointStream = (function() {
"attribute vec4 ps_Color;" +

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

"uniform mat4 ps_ModelViewMatrix;" +
"uniform mat4 ps_ProjectionMatrix;" +
Expand All @@ -131,9 +131,9 @@ var PointStream = (function() {
" frontColor = ps_Color;" +
" vec4 ecPos4 = ps_ModelViewMatrix * vec4(ps_Vertex, 1.0);" +
" float dist = length(ecPos4);" +
" float attn = XBPS_attenuation[0] + " +
" (XBPS_attenuation[1] * dist) + " +
" (XBPS_attenuation[2] * dist * dist);" +
" float attn = ps_Attenuation[0] + " +
" (ps_Attenuation[1] * dist) + " +
" (ps_Attenuation[2] * dist * dist);" +

" if(attn > 0.0){" +
" gl_PointSize = ps_PointSize * sqrt(1.0/attn);" +
Expand All @@ -146,14 +146,14 @@ var PointStream = (function() {
"}";

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

"varying vec4 frontColor;" +
"void main(void){" +
" gl_FragColor = frontColor;" +
"}";
'#ifdef GL_ES \n\
precision highp float; \n\
#endif \n\
\n\
varying vec4 frontColor; \n\
void main(void){ \n\
gl_FragColor = frontColor; \n\
}';

/**
set a uniform integer
Expand Down Expand Up @@ -788,6 +788,23 @@ var PointStream = (function() {
}
}

// !! fix (remove from global ns)
function getAverage(arr){
var objCenter = [0, 0, 0];

for(var i = 0; i < arr.length; i += 3){
objCenter[0] += arr[i];
objCenter[1] += arr[i+1];
objCenter[2] += arr[i+2];
}

objCenter[0] /= arr.length/3;
objCenter[1] /= arr.length/3;
objCenter[2] /= arr.length/3;

return objCenter;
}

/**
Sets variables to default values.
*/
Expand Down Expand Up @@ -1171,7 +1188,7 @@ var PointStream = (function() {
*/
this.setDefaultUniforms = function(){
uniformf(currProgram, "ps_PointSize", 1);
uniformf(currProgram, "XBPS_attenuation", [attn[0], attn[1], attn[2]]);
uniformf(currProgram, "ps_Attenuation", [attn[0], attn[1], attn[2]]);
uniformMatrix(currProgram, "ps_ProjectionMatrix", false, projectionMatrix);
};

Expand Down Expand Up @@ -1262,7 +1279,7 @@ var PointStream = (function() {
@param {Number} quadratic -
*/
this.attenuation = function(constant, linear, quadratic){
uniformf(currProgram, "attenuation", [constant, linear, quadratic]);
uniformf(currProgram, "ps_Attenuation", [constant, linear, quadratic]);
};

/**
Expand Down Expand Up @@ -1338,20 +1355,3 @@ var PointStream = (function() {

return PointStream;
}());

// !! fix (remove from global ns)
var getAverage = function(arr){
var objCenter = [0, 0, 0];

for(var i = 0; i < arr.length; i += 3){
objCenter[0] += arr[i];
objCenter[1] += arr[i+1];
objCenter[2] += arr[i+2];
}

objCenter[0] /= arr.length/3;
objCenter[1] /= arr.length/3;
objCenter[2] /= arr.length/3;

return objCenter;
}
45 changes: 14 additions & 31 deletions tests/user_parser/user_asc_parser.js
Expand Up @@ -10,7 +10,12 @@
Notes:
This parser parses .ASC filetypes. These files are ASCII
files which have their data stored in one of the following ways:
This particular parser is meant to be used with the built-in XB
PointStream shader, therefore it only reads in vertex positions
and colors.
.ASC structure can be one of:
X, Y, Z
X, Y, Z, R, G, B
X, Y, Z, I, J, K
Expand Down Expand Up @@ -53,12 +58,7 @@ var User_ASC_Parser = (function() {
var normalsPresent = false;
var colorsPresent = false;
var layoutCode = UNKNOWN;

//
var parsedVerts = [];
var parsedCols = [];
var parsedNorms = [];


// keep track if onprogress event handler was called to
// handle Chrome/WebKit vs. Minefield differences.
//
Expand All @@ -79,9 +79,9 @@ var User_ASC_Parser = (function() {
ASC files can either contain
X, Y, Z
X, Y, Z, R, G, B
X, Y, Z, NX, NY, NZ
X, Y, Z, R, G, B, NX, NY, NZ
X, Y, Z, R, G, B
X, Y, Z, I, J, K
X, Y, Z, R, G, B, I, J, K
@returns {Number}
0 first case
Expand Down Expand Up @@ -299,16 +299,15 @@ var User_ASC_Parser = (function() {

var verts = new Float32Array(numVerts * 3);
var cols = colorsPresent ? new Float32Array(numVerts * 3) : null;
var norms = normalsPresent ? new Float32Array(numVerts * 3) : null;

// depending if there are colors,
// depending if there are colors,
// we'll need to read different indices.
// if there aren't:
// x y z r g b nx ny nz
// x y z r g b i j k
// 0 1 2 3 4 5 6 7 8 <- normals start at index 6
//
// if there are:
// x y z nx ny nz
// x y z i j k
// 0 1 2 3 4 5 <- normals start at index 3
var valueOffset = 0;
if(colorsPresent){
Expand All @@ -327,31 +326,15 @@ var User_ASC_Parser = (function() {
cols[j+1] = parseInt(chunk[i+4])/255;
cols[j+2] = parseInt(chunk[i+5])/255;
}

if(norms){
norms[j] = parseFloat(chunk[i + 3 + valueOffset]);
norms[j+1] = parseFloat(chunk[i + 4 + valueOffset]);
norms[j+2] = parseFloat(chunk[i + 5 + valueOffset]);
}
}

// !! pushing on null?
parsedVerts.push(verts);
parsedCols.push(cols);
parsedNorms.push(norms);


// XB PointStream expects an object with named/value pairs
// which contain the attribute arrays. These must match attribute
// names found in the shader

// attributes {
// "VERTEX" : [...],
// "COLOR" : [...]
// }
var attributes = {};
if(verts){attributes["ps_Vertex"] = verts;}
if(cols){attributes["ps_Color"] = cols;}
if(norms){attributes["ps_Normal"] = norms;}

parse(AJAX.parser, attributes);
}
Expand Down
3 changes: 1 addition & 2 deletions tests/user_parser/user_parser.js
Expand Up @@ -18,12 +18,11 @@ function render() {
function start(){
ps = new PointStream();
ps.setup(document.getElementById('canvas'));
ps.registerParser("asc", User_ASC_Parser);
ps.onRender = render;

ps.background([1, 1, 1, 1]);
ps.pointSize(5);

ps.registerParser("asc", User_ASC_Parser);

acorn = ps.load("../../clouds/acorn.asc");
}
23 changes: 23 additions & 0 deletions tests/user_parser_and_shader/index.html
@@ -0,0 +1,23 @@
<html>

<head>
<link rel="stylesheet" type="text/css" href="../style.css" />
<script src="user_asc_parser.js"></script>
<script src="../../xbps.js"></script>
<script src="user_parser.js"></script>
</head>

<body onLoad="start();">

<h1><a href="http://zenit.senecac.on.ca/wiki/index.php/XB_PointStream">XB PointStream</a> User-defined Parser</h1>

<p>
This test makes sure the user can specify their own parser and hook it into
the library.
<br />
<br />
<canvas id="canvas" width="300" height="300"></canvas><br />
</p>

</body>
</html>

0 comments on commit e3c7fe3

Please sign in to comment.