Skip to content

Instantly share code, notes, and snippets.

@notlion
Created October 6, 2011 11:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notlion/1267219 to your computer and use it in GitHub Desktop.
Save notlion/1267219 to your computer and use it in GitHub Desktop.
Test for Plask Vec3.rotate()
var plask = require("plask");
plask.simpleWindow({
settings: {
type: "3d",
width: 500,
height: 500,
multisample: true
},
rotateBuffer: function(axis, theta){
var data = new Float32Array(this.pnts.length * 3);
for(var di = 0, i = 0, n = this.pnts.length; i < n; ++i){
this.pnts[i].rotate(theta, axis.x, axis.y, axis.z);
data[di ] = this.pnts[i].x;
data[di + 1] = this.pnts[i].y;
data[di + 2] = this.pnts[i].z;
di += 3;
}
var gl = this.gl;
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
gl.bufferData(gl.ARRAY_BUFFER, data, gl.STREAM_DRAW);
},
init: function(){
var gl = this.gl;
this.pnts = [];
for(var i = 10000; --i >= 0;){
this.pnts[i] = new plask.Vec3(
Math.random() * 2 - 1,
Math.random() * 2 - 1,
Math.random() * 2 - 1
);
}
this.buffer = gl.createBuffer();
this.rotateBuffer(new plask.Vec3(0, 1, 0), 0);
this.shader = plask.gl.MagicProgram.createFromStrings(
gl,
[ "uniform mat4 u_mvp_matrix;"
, "attribute vec3 a_position;"
, "void main(){"
, "gl_PointSize = 3.0;"
, "gl_Position = u_mvp_matrix * vec4(a_position, 1.0);"
, "}"
].join("\n"),
[ "void main(){"
, "gl_FragColor = vec4(1.0);"
, "}"
].join("\n")
);
this.mv_matrix = new plask.Mat4().lookAt(0, 0, -3, 0, 0, 0, 0, 1, 0);
this.p_matrix = new plask.Mat4().perspective(60, this.width / this.height, 0.01, 10);
this.framerate(60);
},
draw: function(){
var gl = this.gl;
gl.clearColor(0, 0, 0, 1);
gl.clear(gl.COLOR_BUFFER_BIT | gl.DEPTH_BUFFER_BIT);
this.shader.use();
this.shader.set_u_mvp_matrix(this.p_matrix.dup().mul(this.mv_matrix));
gl.bindBuffer(gl.ARRAY_BUFFER, this.buffer);
gl.vertexAttribPointer(0, 3, gl.FLOAT, false, 0, 0);
gl.enableVertexAttribArray(0);
gl.drawArrays(gl.POINTS, 0, this.pnts.length);
this.rotateBuffer(new plask.Vec3(1, 1, 0).normalize(), 0.01);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment