Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
improved freeflight demo and created ribbons demo
  • Loading branch information
Benjamin DeLillo committed Nov 19, 2010
1 parent 0d87544 commit e11fd70
Show file tree
Hide file tree
Showing 2 changed files with 194 additions and 65 deletions.
171 changes: 171 additions & 0 deletions examples/boids/ribbons.html
@@ -0,0 +1,171 @@
<html>
<head>
<link type="text/css" rel="stylesheet" href="../example.css">
<script type="text/javascript" src="../../src/webglu.complete.js">
</script>
<script type="text/javascript" src="../../external/Sylverster.src.js">
</script>
<script type="x-shader/x-fragment" id="ribbon_fs">

#ifdef GL_ES
precision highp float;
#endif
varying vec4 frontColor;
void main(void) {
gl_FragColor = vec4(frontColor.rgb, 0.5);
}
</script>
<script type="application/x-javascript">
function init() {
if (!$W.initialize()) return false;
$W.GL.clearColor(77/255, 77/255, 77/255, 1.0);
$W.camera.setPosition(-2,0,17);
$W.camera.setTarget(1,1,0);
$W.camera.yfov = 90;
$W.camera.aspectRatio = 1;
$W.GL.enable($W.GL.BLEND);
$W.GL.blendFunc($W.GL.SRC_ALPHA, $W.GL.ONE_MINUS_SRC_ALPHA);
return true;
}
function makeFlock() {
/* Passing false as the second (optional) parameter stops the object
* from being added to the primary object list which means it
* won't get rendered when we call $W.draw(), instead we'll
* have to call boid.draw() or boid.drawAt(pos,rot,scl) ourselves.
* We'll do this because we can just draw the same model a whole
* bunch of times (a la instancing) at different positions.
*/
var boid = new $W.Object($W.GL.TRIANGLES, false);
// Same model & colors as demo at http://www.red3d.com/cwr/boids/
var tip = [0,0,1];
var lft = [1/2,0,0];
var rgt = [-1/2,0,0];
var rdg = [0,1/4,1/10];
var yel = [220/255, 220/255, 40/255];
var blu = [ 40/255, 220/255, 220/255];
var grn = [ 40/255, 220/255, 40/255];
var gry = [120/255, 120/255, 120/255];
boid.vertexCount = 0;
boid.fillArray('vertex',
[tip,rdg,lft, tip,rdg,rgt, lft,rgt,rdg, lft,rgt,tip]);
boid.fillArray('color',
[yel,yel,yel, grn,grn,grn, blu,blu,blu, gry,gry,gry]);
boid.setScaleUniformly(.7);
$W.useCrazyGLU(); // OpenSteer is part of CrazyGLU (for now)
/* The Flock object (not part of the original Opensteer)
* handles creation, management, and drawing for a bunch
* boids, where the first argument is the number of boids and
* the second is the model to use for each.
*/
return new $W.OS.Flock(30, boid);
}
function start() {
var FPS = document.getElementById('overlay');
if (init()) {
var flock = makeFlock();
var ribbonMaterial = new $W.Material({
name: "ribbonMaterial",
program: {
name: "ribbonProgram",
shaders: [
{name:"ribbon_vs", path: $W.paths.shaders +
"default.vert"},
{id:"ribbon_fs"}]
}
});
for (var i = 0; i < flock.flock.length; i++) {
var boid = flock.flock[i];
boid.ribbon = $W.createObject({type: $W.GL.TRIANGLE_STRIP});
boid.color = [Math.random(), Math.random(), Math.random()];
boid.ribbon.fillArray('vertex', [.5,0,0, -.5,0,0]);
boid.ribbon.fillArray('color', boid.color.concat(boid.color));
boid.ribbon.setMaterial(ribbonMaterial);
boid.ribbon.vertexCount = 2;
var bindAndBuffer = function() {
$W.GL.bindBuffer($W.GL.ARRAY_BUFFER, this.glBuffer);
$W.GL.bufferData($W.GL.ARRAY_BUFFER, this.glData, $W.GL.STATIC_DRAW);
};
boid.ribbon.vertexBuffer = boid.ribbon.arrayBuffers.vertex;
boid.ribbon.colorBuffer = boid.ribbon.arrayBuffers.color;
boid.setPosition(V3.$(70 / i+1, 70 - (70 / i+1),0));
}
var updateRibbon = function(boid) {
$W.modelview.pushMatrix();
$W.modelview.loadIdentity();

$W.modelview.translate(V3.elements(
V3.scale(boid.smoothedPosition(), .2)));
$W.modelview.multiply(boid.rotationMatrix());
var lVtx = $W.modelview.matrix.multiply(
$V([0.5, 0, 0, 1])).elements;
var rVtx = $W.modelview.matrix.multiply(
$V([-0.5, 0, 0, 1])).elements;
lVtx.pop();
rVtx.pop();
if (boid.ribbon.vertexCount > 40) {
boid.ribbon.vertexBuffer.setData(
(boid.ribbon.vertexBuffer.data.concat(lVtx.concat(rVtx))).slice(6));
}else {
boid.ribbon.vertexBuffer.setData(
boid.ribbon.vertexBuffer.data.concat(lVtx.concat(rVtx)));
boid.ribbon.colorBuffer.setData(
boid.ribbon.colorBuffer.data.concat(boid.color.concat(boid.color)));
boid.ribbon.vertexCount += 2;
}
$W.modelview.popMatrix();
}
$W.update = function() {
FPS.innerHTML = $W.FPS;
flock.update($W.util.clip($W.timer.dt, null, 20) / 20);
for (var i = 0; i < flock.flock.length; i++) {
updateRibbon(flock.flock[i]);
}
};
$W.draw = function() {
flock.draw();
};
$W.start();
}
}
</script>

</head>
<body onload='start()'>
<h1>
<strong>
Boids
</strong>
</h1>
behavioral simulation in WebGL using WebGLU
<div id="glCanvas">

<div id="overlay">
</div>
<canvas id='canvas' width='500' height='500'>
</canvas>
<br>
</div>
</body>

</html>
<!--
Copyright (c) 2009 Benjamin P. DeLillo
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
-->
88 changes: 23 additions & 65 deletions examples/freeflight/freeflight.html
Expand Up @@ -31,6 +31,7 @@
var flyer = $W.createObject({type: $W.GL.LINES,
data: flyerData});
flyer.speed = .07;
flyer.turnSpeed = .1;
flyer.positions = [];
var ribbon = $W.createObject({type: $W.GL.TRIANGLE_STRIP});
ribbon.fillArray('vertex', [.5,0,0, -.5,0,0]);
Expand All @@ -56,89 +57,47 @@

$W.useGameGLU();
var st = $G.state;
st.turning = {
st = {
left: {active: false, time: 0, amount:0},
right:{active: false, time: 0, amount:0},
up: {active: false, time: 0, amount:0},
down: {active: false, time: 0, amount:0},
horiz: 0, vert: 0, hScale: 0, vScale: 0,
yaw: 0, pitch: 0
}
$G.bind("+left", function() { st.hScale = -1; });
$G.bind("-left", function() { st.hScale = 0; });
$G.bind("+right", function() { st.hScale = 1; });
$G.bind("-right", function() { st.hScale = 0; });
$G.bind("+up", function() { st.vScale = 1; });
$G.bind("-up", function() { st.vScale = 0; });
$G.bind("+down", function() { st.vScale = -1; });
$G.bind("-down", function() { st.vScale = 0; });

var horizScale = 0;
var vertScale = 0;
$G.bind("+left", function() {
horizScale = -1;
st.turning.left.active = true;
});
$G.bind("-left", function() {
horizScale = 0;
});

$G.bind("+right", function() {
horizScale = 1;
});
$G.bind("-right", function() {
horizScale = 0;
});

$G.bind("+up", function() {
vertScale = 1;
});
$G.bind("-up", function() {
vertScale = 0;
});

$G.bind("down", function() {
vertScale = -1;
});
$G.bind("-down", function() {
vertScale = 0;
});

var turnScale = .1;
var ribbonAppendDt = 0;
var overlay = document.getElementById('overlay');
var horizTurn = 0;
var vertTurn = 0;
var yaw = 0;
var pitch = 0;
var lastLeftVert = $V([0,0,0]);
var lastRightVert = $V([0,0,0]);
$W.update = function() {
overlay.innerHTML = $W.FPS;

yaw += $W.timer.dt * horizScale * turnScale;
document.getElementById('overlay').innerHTML = $W.FPS;
st.yaw += $W.timer.dt * st.hScale * flyer.turnSpeed;
st.pitch -= $W.timer.dt * st.vScale * flyer.turnSpeed;
flyer.setRotation(st.yaw, st.pitch, 0);

pitch -= $W.timer.dt * vertScale * turnScale;

flyer.setRotation(yaw, pitch, 0);

var direction;
$W.modelview.pushMatrix();
$W.modelview.loadIdentity();
$W.modelview.translate(flyer.position.elements);
$W.modelview.multiply(flyer.q.matrix());
var A = flyer.q.matrix().multiply($V([0,0,1,1]));
var B = flyer.q.matrix().multiply($V([0,0,0,1]));
direction = A.subtract(B);
var direction = A.subtract(B);
direction.elements.pop();

ribbonAppendDt = 0;
var vertices = vertexBuffer.data;
var colors = colorBuffer.data;

var leftVert = $W.modelview.matrix.multiply(
var lVtx = $W.modelview.matrix.multiply(
$V([0.5, 0, 0, 1])).elements;
var rightVert = $W.modelview.matrix.multiply(
var rVtx = $W.modelview.matrix.multiply(
$V([-0.5, 0, 0, 1])).elements;
lVtx.pop();
rVtx.pop();

leftVert.pop();
rightVert.pop();

var segment = [].concat(leftVert, rightVert);
vertexBuffer.setData(vertices.concat(segment));
var newColors = [0,1,0, 0,1,0];
colorBuffer.setData(colors.concat(newColors));

vertexBuffer.setData(vertexBuffer.data.concat(lVtx.concat(rVtx)));
colorBuffer.setData(colorBuffer.data.concat([0,1,0, 0,1,0]));
ribbon.vertexCount += 2;
$W.modelview.popMatrix();

Expand All @@ -164,7 +123,6 @@
<br>
</div>
</body>

</html>
<!--
Copyright (c) 2009 Benjamin P. DeLillo
Expand Down

0 comments on commit e11fd70

Please sign in to comment.