Permalink
Browse files

* added compute normals method in O3D

 * added lighting options in Options.Canvas
 * added shader code to handle lighting
  • Loading branch information...
1 parent 7100dba commit 98b7547ebcec31b11dbd1bc25dec45aae7896b1b @philogb committed with Sep 8, 2010
View
20 Source/Core/Canvas3D.js
@@ -132,24 +132,42 @@ Canvas.Base['3D'].FragmentShader = [
"#endif",
"varying vec4 vcolor;",
+ "varying vec3 lightWeighting;",
"void main(){",
- "gl_FragColor = vcolor;",
+ "gl_FragColor = vec4(vcolor.rgb * lightWeighting, vcolor.a);",
"}"
].join("\n");
Canvas.Base['3D'].VertexShader = [
"attribute vec3 position;",
+ "attribute vec3 normal;",
"uniform vec4 color;",
"uniform mat4 viewMatrix;",
"uniform mat4 projectionMatrix;",
+ "uniform mat4 normalMatrix;",
+
+ "uniform bool enableLighting;",
+ "uniform vec3 ambientColor;",
+ "uniform vec3 directionalColor;",
+ "uniform vec3 lightingDirection;",
+
"varying vec4 vcolor;",
+ "varying vec3 lightWeighting;",
"void main(void) {",
+ "if(!enableLighting) {",
+ "lightWeighting = vec3(1.0, 1.0, 1.0);",
+ "} else {",
+ "vec4 transformedNormal = normalMatrix * vec4(normal, 1.0);",
+ "float directionalLightWeighting = max(dot(transformedNormal.xyz, lightingDirection), 0.0);",
+ "lightWeighting = ambientColor + directionalColor * directionalLightWeighting;"
+ "}"
+
"vcolor = color;",
"gl_Position = projectionMatrix * viewMatrix * vec4( position, 1.0 );",
View
12 Source/Geometry/Matrix4.js
@@ -211,6 +211,18 @@ Matrix4.prototype = {
},
+ $transpose: function () {
+
+ this.n21 = this.n12;
+ this.n31 = this.n13;
+ this.n32 = this.n23;
+ this.n41 = this.n14;
+ this.n42 = this.n24;
+ this.n43 = this.n34;
+ return this;
+
+ },
+
clone: function () {
var m = new Matrix4();
View
21 Source/Geometry/O3D.js
@@ -32,6 +32,25 @@ O3D.base = new Class({
matrix.$multiply( Matrix4.rotationYMatrix( rot.y ) );
matrix.$multiply( Matrix4.rotationZMatrix( rot.z ) );
matrix.$multiply( Matrix4.scaleMatrix( scale.x, scale.y, scale.z ) );
+ },
+ //compute faces normals
+ computeNormals: function () {
+ for (var f=0, vs=this.vertices, fs=this.faces, len=fs.length; f < len; f++) {
+ var va = vs[fs[f].a],
+ vb = vs[fs[f].b],
+ vc = vs[fs[f].c],
+ cb = new Vector3,
+ ab = new Vector3,
+ normal = new Vector3;
+
+ cb.sub(vc, vb);
+ ab.sub(va, vb);
+ cb.$cross(ab);
+
+ if (!cb.isZero()) cb.normalize();
+
+ fs[f].normal = cb;
+ }
}
});
@@ -65,6 +84,7 @@ O3D.cube = new Class({
initialize: function() {
IsoCube.call(this);
+ this.computeNormals();
},
update: function(obj) {
@@ -131,6 +151,7 @@ O3D.tube = new Class({
2 * numSegs + 1);
}
}
+ this.computeNormals();
},
update: function(obj) {
View
22 Source/Graph/Graph.Plot.js
@@ -756,21 +756,28 @@ Graph.Plot3D = $.merge(Graph.Plot, {
if(!elem.webGLVertexBuffer) {
var vertices = [],
faces = [],
+ normals = [],
vertexIndex = 0,
geom = elem.geometry;
- for(var i=0, vs = geom.vertices, fs=geom.faces, fsl=fs.length; i<fsl; i++) {
+ for(var i=0, vs=geom.vertices, fs=geom.faces, fsl=fs.length; i<fsl; i++) {
var face = fs[i],
v1 = vs[face.a],
v2 = vs[face.b],
v3 = vs[face.c],
- v4 = face.d? vs[face.d] : false;
+ v4 = face.d? vs[face.d] : false,
+ n = face.normal;
vertices.push(v1.x, v1.y, v1.z);
vertices.push(v2.x, v2.y, v2.z);
vertices.push(v3.x, v3.y, v3.z);
if(v4) vertices.push(v4.x, v4.y, v4.z);
+ normals.push(n.x, n.y, n.z);
+ normals.push(n.x, n.y, n.z);
+ normals.push(n.x, n.y, n.z);
+ if(v4) normals.push(n.x, n.y, n.z);
+
faces.push(vertexIndex, vertexIndex +1, vertexIndex +2);
if(v4) {
faces.push(vertexIndex, vertexIndex +2, vertexIndex +3);
@@ -788,18 +795,29 @@ Graph.Plot3D = $.merge(Graph.Plot, {
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elem.webGLFaceBuffer);
gl.bufferData(gl.ELEMENT_ARRAY_BUFFER, new Uint16Array(faces), gl.STATIC_DRAW);
elem.webGLFaceCount = faces.length;
+ //calculate vertex normals and store them
+ elem.webGLNormalBuffer = gl.createBuffer();
+ gl.bindBuffer(gl.ARRAY_BUFFER, elem.webGLNormalBuffer);
+ gl.bufferData(gl.ARRAY_BUFFER, new Float32Array(normals), gl.STATIC_DRAW);
}
viewMatrix.multiply(camera.matrix, elem.geometry.matrix);
//send matrix data
gl.uniformMatrix4fv(program.viewMatrix, false, viewMatrix.flatten());
gl.uniformMatrix4fv(program.projectionMatrix, false, camera.projectionMatrix.flatten());
+ //send normal matrix for lighting
+ var normalMatrix = viewMatrix.inverse();
+ normalMatrix.$transpose();
+ gl.uniformMatrix4fv(program.normalMatrix, false, normalMatrix.flatten());
//send color data
var color = $.hexToRgb(elem.getData('color'));
color.push(opt.getAlpha());
gl.uniform4f(program.color, color[0] / 255, color[1] / 255, color[2] / 255, color[3]);
//send vertices data
gl.bindBuffer(gl.ARRAY_BUFFER, elem.webGLVertexBuffer);
gl.vertexAttribPointer(program.position, 3, gl.FLOAT, false, 0, 0);
+ //send normals data
+ gl.bindBuffer(gl.ARRAY_BUFFER, elem.webGLNormalBuffer);
+ gl.vertexAttribPointer(program.normal, 3, gl.FLOAT, false, 0, 0);
//draw!
gl.bindBuffer(gl.ELEMENT_ARRAY_BUFFER, elem.webGLFaceBuffer );
gl.drawElements(gl.TRIANGLES, elem.webGLFaceCount, gl.UNSIGNED_SHORT, 0);
View
17 Source/Options/Options.Canvas.js
@@ -54,5 +54,20 @@ Options.Canvas = {
height: false,
useCanvas: false,
withLabels: true,
- background: false
+ background: false,
+
+ Scene: {
+ Lighting: {
+ enable: false,
+ Ambient: {
+ color: '#ffccdd'
+ },
+ Directional: [
+ {
+ position: { x: 100, y: 100, z: 100 },
+ color: '#ffddcc'
+ }
+ ]
+ }
+ },
};
View
3 Tests/ForceDirected3D/test1.js
@@ -437,7 +437,10 @@ function init(){
"name": "graphnode20"
}
];
+<<<<<<< HEAD
+=======
+>>>>>>> 83dd65b... * added compute normals method in O3D
// end
// init ForceDirected3D
var fd = new $jit.ForceDirected3D({

0 comments on commit 98b7547

Please sign in to comment.