Skip to content
This repository has been archived by the owner on Sep 16, 2023. It is now read-only.

Commit

Permalink
Wireframe rendering is now way faster.
Browse files Browse the repository at this point in the history
  • Loading branch information
stephomi committed Dec 28, 2013
1 parent 2796c12 commit efa86ea
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 56 deletions.
2 changes: 1 addition & 1 deletion object/mesh.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ Mesh.prototype = {
/** Update the rendering buffers */ /** Update the rendering buffers */
updateBuffers: function () updateBuffers: function ()
{ {
this.render_.updateBuffers(this.vertexArray_, this.normalArray_, this.colorArray_, this.indexArray_); this.render_.updateBuffers(this.vertexArray_, this.normalArray_, this.colorArray_, this.indexArray_, this.triangles_.length);
}, },


/** Update geometry */ /** Update geometry */
Expand Down
86 changes: 37 additions & 49 deletions object/render.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ function Render(gl)
this.vertexBuffer_ = null; //vertices buffer this.vertexBuffer_ = null; //vertices buffer
this.normalBuffer_ = null; //normals buffer this.normalBuffer_ = null; //normals buffer
this.colorBuffer_ = null; //colors buffer this.colorBuffer_ = null; //colors buffer
this.barycenterBuffer_ = null; //barycenter buffer
this.indexBuffer_ = null; //indexes buffer this.indexBuffer_ = null; //indexes buffer
this.reflectionLoc_ = null; //texture reflection this.reflectionLoc_ = null; //texture reflection


Expand All @@ -19,7 +18,6 @@ function Render(gl)
this.vertexAttrib_ = null; //vertex attribute location this.vertexAttrib_ = null; //vertex attribute location
this.colorAttrib_ = null; //color vertex attribute location this.colorAttrib_ = null; //color vertex attribute location
this.normalAttrib_ = null; //normal attribute location this.normalAttrib_ = null; //normal attribute location
this.barycenterAttrib_ = null; //barycenter attribute location


this.mvpMatrixUnif_ = null; //model view projection matrix uniform location this.mvpMatrixUnif_ = null; //model view projection matrix uniform location
this.mvMatrixUnif_ = null; //model view matrix uniform location this.mvMatrixUnif_ = null; //model view matrix uniform location
Expand All @@ -32,6 +30,9 @@ function Render(gl)
this.lineNormalUnif_ = null; //line normal uniform location this.lineNormalUnif_ = null; //line normal uniform location


this.reflectionTexUnif_ = null; //reflection texture uniform location this.reflectionTexUnif_ = null; //reflection texture uniform location

this.cacheDrawArraysV_ = null; //cache array for vertices
this.cacheDrawArraysN_ = null; //cache array for normals
} }


//the rendering mode //the rendering mode
Expand Down Expand Up @@ -87,9 +88,7 @@ Render.prototype = {


this.vertexAttrib_ = gl.getAttribLocation(this.shaderProgram_, 'vertex'); this.vertexAttrib_ = gl.getAttribLocation(this.shaderProgram_, 'vertex');
this.normalAttrib_ = gl.getAttribLocation(this.shaderProgram_, 'normal'); this.normalAttrib_ = gl.getAttribLocation(this.shaderProgram_, 'normal');
if (this.shaderType_ === Render.mode.WIREFRAME) if (this.shaderType_ !== Render.mode.NORMAL && this.shaderType_ !== Render.mode.WIREFRAME)
this.barycenterAttrib_ = gl.getAttribLocation(this.shaderProgram_, 'barycenter');
else if (this.shaderType_ !== Render.mode.NORMAL)
this.colorAttrib_ = gl.getAttribLocation(this.shaderProgram_, 'color'); this.colorAttrib_ = gl.getAttribLocation(this.shaderProgram_, 'color');


this.mvpMatrixUnif_ = gl.getUniformLocation(shaderProgram, 'mvpMat'); this.mvpMatrixUnif_ = gl.getUniformLocation(shaderProgram, 'mvpMat');
Expand All @@ -115,7 +114,7 @@ Render.prototype = {
{ {
var gl = this.gl_; var gl = this.gl_;
this.vertexShader_ = gl.createShader(gl.VERTEX_SHADER); this.vertexShader_ = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(this.vertexShader_, '\n'+vertex+'\n'); gl.shaderSource(this.vertexShader_, '\n' + vertex + '\n');
gl.compileShader(this.vertexShader_); gl.compileShader(this.vertexShader_);
this.fragmentShader_ = gl.createShader(gl.FRAGMENT_SHADER); this.fragmentShader_ = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(this.fragmentShader_, fragment); gl.shaderSource(this.fragmentShader_, fragment);
Expand Down Expand Up @@ -162,7 +161,10 @@ Render.prototype = {


gl.enableVertexAttribArray(this.normalAttrib_); gl.enableVertexAttribArray(this.normalAttrib_);
gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer_); gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer_);
gl.vertexAttribPointer(this.normalAttrib_, 3, gl.FLOAT, false, 0, 0); if (this.shaderType_ === Render.mode.WIREFRAME)
gl.vertexAttribPointer(this.normalAttrib_, 4, gl.FLOAT, false, 0, 0);
else
gl.vertexAttribPointer(this.normalAttrib_, 3, gl.FLOAT, false, 0, 0);


if (this.shaderType_ !== Render.mode.WIREFRAME && this.shaderType_ !== Render.mode.NORMAL) if (this.shaderType_ !== Render.mode.WIREFRAME && this.shaderType_ !== Render.mode.NORMAL)
{ {
Expand Down Expand Up @@ -193,9 +195,6 @@ Render.prototype = {
gl.depthMask(true); gl.depthMask(true);
break; break;
case Render.mode.WIREFRAME: case Render.mode.WIREFRAME:
gl.enableVertexAttribArray(this.barycenterAttrib_);
gl.bindBuffer(gl.ARRAY_BUFFER, this.barycenterBuffer_);
gl.vertexAttribPointer(this.barycenterAttrib_, 3, gl.FLOAT, false, 0, 0);
gl.drawArrays(gl.TRIANGLES, 0, lengthIndexArray); gl.drawArrays(gl.TRIANGLES, 0, lengthIndexArray);
break; break;
case Render.mode.NORMAL: case Render.mode.NORMAL:
Expand All @@ -219,11 +218,11 @@ Render.prototype = {
}, },


/** Update buffers */ /** Update buffers */
updateBuffers: function (vAr, nAr, cAr, iAr) updateBuffers: function (vAr, nAr, cAr, iAr, nbTriangles)
{ {
if (this.shaderType_ === Render.mode.WIREFRAME) if (this.shaderType_ === Render.mode.WIREFRAME)
{ {
this.makeWireframeBuffers(vAr, nAr, iAr); this.makeWireframeBuffers(vAr, nAr, iAr, nbTriangles);
return; return;
} }
var gl = this.gl_; var gl = this.gl_;
Expand All @@ -241,53 +240,42 @@ Render.prototype = {
}, },


/** Create arrays for the drawArrays function */ /** Create arrays for the drawArrays function */
makeWireframeBuffers: function (vAr, nAr, iAr) makeWireframeBuffers: function (vAr, nAr, iAr, nbTriangles)
{ {
var gl = this.gl_; var gl = this.gl_;
var vArray = new Float32Array(iAr.length * 3);
var cdv = this.cacheDrawArraysV_;
var cdn = this.cacheDrawArraysN_;
if (cdv === null || cdv.length <= nbTriangles * 9)
{
this.cacheDrawArraysV_ = new Float32Array(nbTriangles * 9 * 1.5);
this.cacheDrawArraysN_ = new Float32Array(nbTriangles * 12 * 1.5);
cdv = this.cacheDrawArraysV_;
cdn = this.cacheDrawArraysN_;
}

var i = 0, var i = 0,
j = 0, j = 0,
id = 0; id = 0;
for (i = 0; i < iAr.length; ++i) var len = nbTriangles * 9;
for (i = 0; i < len; ++i)
{ {
j = i * 3; j = i * 3;
id = iAr[i] * 3; id = iAr[i] * 3;
vArray[j] = vAr[id]; cdv[j] = vAr[id];
vArray[j + 1] = vAr[id + 1]; cdv[j + 1] = vAr[id + 1];
vArray[j + 2] = vAr[id + 2]; cdv[j + 2] = vAr[id + 2];

j = i * 4;
cdn[j] = nAr[id];
cdn[j + 1] = nAr[id + 1];
cdn[j + 2] = nAr[id + 2];
cdn[j + 3] = i % 3;
} }
gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer_); gl.bindBuffer(gl.ARRAY_BUFFER, this.vertexBuffer_);
gl.bufferData(gl.ARRAY_BUFFER, vArray, gl.DYNAMIC_DRAW); gl.bufferData(gl.ARRAY_BUFFER, cdv, gl.DYNAMIC_DRAW);


var nArray = new Float32Array(iAr.length * 3);
for (i = 0; i < iAr.length; ++i)
{
j = i * 3;
id = iAr[i] * 3;
nArray[j] = nAr[id];
nArray[j + 1] = nAr[id + 1];
nArray[j + 2] = nAr[id + 2];
}
gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer_); gl.bindBuffer(gl.ARRAY_BUFFER, this.normalBuffer_);
gl.bufferData(gl.ARRAY_BUFFER, nArray, gl.DYNAMIC_DRAW); gl.bufferData(gl.ARRAY_BUFFER, cdn, gl.DYNAMIC_DRAW);

var baryArray = new Float32Array(iAr.length * 3);
for (i = 0; i < iAr.length / 3; ++i)
{
j = i * 9;
baryArray[j] = 1;
baryArray[j + 1] = 0;
baryArray[j + 2] = 0;
baryArray[j + 3] = 0;
baryArray[j + 4] = 1;
baryArray[j + 5] = 0;
baryArray[j + 6] = 0;
baryArray[j + 7] = 0;
baryArray[j + 8] = 1;
}
if (!this.barycenterBuffer_)
this.barycenterBuffer_ = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, this.barycenterBuffer_);
gl.bufferData(gl.ARRAY_BUFFER, baryArray, gl.DYNAMIC_DRAW);
} }
}; };
1 change: 0 additions & 1 deletion shaders/wireframe.frag
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ varying vec3 vNormal;
varying vec3 vBarycenter; varying vec3 vBarycenter;
const vec3 colorBackface = vec3(0.81, 0.71, 0.23); const vec3 colorBackface = vec3(0.81, 0.71, 0.23);
const vec4 colorCutPlane = vec4(0.81, 0.31, 0.23, 1.0); const vec4 colorCutPlane = vec4(0.81, 0.31, 0.23, 1.0);

const vec3 vecLight = vec3(0.06189844605901729, 0.12379689211803457, 0.9903751369442766); const vec3 vecLight = vec3(0.06189844605901729, 0.12379689211803457, 0.9903751369442766);
const float shininess = 100.0; const float shininess = 100.0;
void main() void main()
Expand Down
14 changes: 9 additions & 5 deletions shaders/wireframe.vert
Original file line number Original file line Diff line number Diff line change
@@ -1,17 +1,21 @@
attribute vec3 vertex; attribute vec3 vertex;
attribute vec3 normal; attribute vec4 normal;
attribute vec3 barycenter;
uniform mat4 mvMat, mvpMat; uniform mat4 mvMat, mvpMat;
uniform mat3 nMat; uniform mat3 nMat;
varying vec3 vVertex; varying vec3 vVertex;
varying vec3 vNormal; varying vec3 vNormal;
varying vec3 vBarycenter; varying vec3 vBarycenter;
varying vec2 vTexCoord;
void main() void main()
{ {
vBarycenter = barycenter;
vec4 vertex4 = vec4(vertex, 1.0); vec4 vertex4 = vec4(vertex, 1.0);
vNormal = nMat * normal; float bVal = normal[3];
if(bVal > 1.5)
vBarycenter = vec3(0,0,1);
else if(bVal < 0.5)
vBarycenter = vec3(0,1,0);
else
vBarycenter = vec3(1,0,0);
vNormal = nMat * vec3(normal);
vVertex = vec3(mvMat * vertex4); vVertex = vec3(mvMat * vertex4);
gl_Position = mvpMat * vertex4; gl_Position = mvpMat * vertex4;
} }

0 comments on commit efa86ea

Please sign in to comment.