Permalink
Browse files

Revert last commit (position model)

Final coordinate stand in a 3 matrix multiplication : perspective * camera * object
With this model, i dont need :
- to hold a mvmatrix stack
- un-necessary binding to the shader (i could only send the camera when needed)
I also compute normal matrix on the shader side, but is not the best idea ever :
This add a lot of (repetiting) operation in the vertex-shader side, maybe i should revert this
  • Loading branch information...
1 parent 2abc29b commit b596f0054a246bea60a12084d0ebf1c358d92cf6 @131 committed Aug 18, 2011
Showing with 9 additions and 34 deletions.
  1. +4 −3 index.htm
  2. +5 −31 libs/webgl.js
View
@@ -37,8 +37,9 @@
attribute vec2 aTextureCoord;
uniform mat4 uMVMatrix;
+ uniform mat4 uElementMatrix;
uniform mat4 uPMatrix;
- uniform mat3 uNMatrix;
+
uniform vec3 uAmbientColor;
@@ -51,13 +52,13 @@
varying vec3 vLightWeighting;
void main(void) {
- gl_Position = uPMatrix * uMVMatrix * vec4(aVertexPosition, 1.0);
+ gl_Position = uPMatrix * uMVMatrix * uElementMatrix * vec4(aVertexPosition, 1.0);
vTextureCoord = aTextureCoord;
if (!uUseLighting) {
vLightWeighting = vec3(1.0, 1.0, 1.0);
} else {
- vec3 transformedNormal = uNMatrix * aVertexNormal;
+ vec3 transformedNormal = mat3(uMVMatrix * uElementMatrix) * aVertexNormal;
float directionalLightWeighting = max(dot(transformedNormal, uLightingDirection), 0.0);
vLightWeighting = uAmbientColor + uDirectionalColor * directionalLightWeighting;
}
View
@@ -67,33 +67,10 @@ WebGL = new Class({
this.gl.clear(this.gl.COLOR_BUFFER_BIT | this.gl.DEPTH_BUFFER_BIT);
},
- mvMatrixStack: [],
- initMVMatrix:function(src){
- this.mvMatrixStack = [];
- this.mvMatrix = this.storeMVMatrix(src);
- },
-
- storeMVMatrix:function(src) {
- var copy = mat4.create();
- mat4.set(src || this.mvMatrix, copy);
- this.mvMatrixStack.push(copy);
- return copy;
- },
-
- restoreMVMatrix:function() {
- if (this.mvMatrixStack.length == 0)
- throw "Invalid popMatrix!";
- this.mvMatrix = this.mvMatrixStack.pop();
- },
-
- sendMVMatrix:function(matrix){
+ bindCamera:function(){
- var normalMatrix = mat3.create();
- mat4.toInverseMat3(this.mvMatrix, normalMatrix);
- mat3.transpose(normalMatrix);
+ this.shader.bindVar("uMVMatrix", this.camera.matrix);
- this.shader.bindVar("uMVMatrix", this.mvMatrix);
- this.shader.bindVar("uNMatrix", normalMatrix);
},
setPerspective:function(angle, min, max){
@@ -108,7 +85,8 @@ WebGL = new Class({
},
draw:function(cube){
- this.initMVMatrix(this.camera.matrix);
+
+ this.bindCamera();
//apply camera matrix to lighting vector
var ld = vec3.create(this.lightingDirection);
@@ -121,11 +99,7 @@ WebGL = new Class({
Array.each(this.elements, function(form){
- this.storeMVMatrix();
- mat4.multiply(this.mvMatrix, form.coordinates);
- this.sendMVMatrix();
- this.restoreMVMatrix();
-
+ this.shader.bindVar("uElementMatrix", form.coordinates);
form.draw();
}.bind(this));

0 comments on commit b596f00

Please sign in to comment.