Skip to content

Commit

Permalink
Mesh.computeTangetns fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
tamat committed Feb 19, 2014
1 parent 124e95c commit 7e2ce14
Show file tree
Hide file tree
Showing 21 changed files with 2,087 additions and 334 deletions.
50 changes: 28 additions & 22 deletions build/litegl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1427,13 +1427,22 @@ Mesh.prototype.computeTangents = function() {

if(!vertices || !normals || !uvs) return;

var tangents = new Float32Array(vertices.length);
//temporary
var tan1 = new Float32Array(vertices.length);
var tan2 = new Float32Array(vertices.length);
var num_vertices = vertices.length / 3;

var a;
for (a = 0; a < triangles.length; a+=3)
var tangents = new Float32Array(num_vertices * 4);

//temporary (shared)
var tan1 = new Float32Array(num_vertices*3*2);
var tan2 = tan1.subarray(num_vertices*3);

var a,l;
var sdir = vec3.create();
var tdir = vec3.create();
var temp = vec3.create();
var temp2 = vec3.create();


for (a = 0, l = triangles.length; a < l; a+=3)
{
var i1 = triangles[a];
var i2 = triangles[a+1];
Expand Down Expand Up @@ -1466,33 +1475,30 @@ Mesh.prototype.computeTangents = function() {
else
r = 1.0 / den;

var sdir = vec3.fromValues( (t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r,
(t2 * z1 - t1 * z2) * r);
var tdir = vec3.fromValues( (s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r,
(s1 * z2 - s2 * z1) * r);
vec3.copy(sdir, [(t2 * x1 - t1 * x2) * r, (t2 * y1 - t1 * y2) * r, (t2 * z1 - t1 * z2) * r] );
vec3.copy(tdir, [(s1 * x2 - s2 * x1) * r, (s1 * y2 - s2 * y1) * r, (s1 * z2 - s2 * z1) * r] );

vec3.add( vertices.subarray(i1*3,i1*3+3), sdir);
vec3.add( vertices.subarray(i2*3,i2*3+3), sdir);
vec3.add( vertices.subarray(i3*3,i3*3+3), sdir);
vec3.add( tan1.subarray( i1*3, i1*3+3), tan1.subarray( i1*3, i1*3+3), sdir);
vec3.add( tan1.subarray( i2*3, i2*3+3), tan1.subarray( i2*3, i2*3+3), sdir);
vec3.add( tan1.subarray( i3*3, i3*3+3), tan1.subarray( i3*3, i3*3+3), sdir);

vec3.add( vertices.subarray(i1*3,i1*3+3), tdir);
vec3.add( vertices.subarray(i2*3,i2*3+3), tdir);
vec3.add( vertices.subarray(i3*3,i3*3+3), tdir);
vec3.add( tan2.subarray( i1*3, i1*3+3), tan2.subarray( i1*3, i1*3+3), tdir);
vec3.add( tan2.subarray( i2*3, i2*3+3), tan2.subarray( i2*3, i2*3+3), tdir);
vec3.add( tan2.subarray( i3*3, i3*3+3), tan2.subarray( i3*3, i3*3+3), tdir);
}

var temp = vec3.create();
for (a = 0; a < positions.length; a+=3)
for (a = 0, l = vertices.length; a < l; a+=3)
{
var n = normals.subarray(a,a+3);
var t = tan1.subarray(a,a+3);

// Gram-Schmidt orthogonalize
vec3.subtract(t, vec3.scale(n, vec3.dot(n, t, temp), temp), temp);
vec3.normalize(temp);
vec3.subtract(temp, t, vec3.scale(temp, n, vec3.dot(n, t) ) );
vec3.normalize(temp,temp);

// Calculate handedness
var w = ( vec3.dot( vec3.cross(n, t, vec3.create()), tan2.subarray(a,a+3)) < 0.0) ? -1.0 : 1.0;
tangents.set([temp[0], temp[1], temp[2], w],a);
var w = ( vec3.dot( vec3.cross(temp2, n, t), tan2.subarray(a,a+3) ) < 0.0) ? -1.0 : 1.0;
tangents.set([temp[0], temp[1], temp[2], w],(a/3)*4);
}

this.addVertexBuffer('tangents', Mesh.common_buffers["tangents"].attribute, 4, tangents );
Expand Down
58 changes: 29 additions & 29 deletions build/litegl.min.js

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions doc/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ YUI.add("yuidoc-meta", function(Y) {
"GL",
"Indexer",
"Mesh",
"Octree",
"Shader",
"Texture"
],
Expand Down
34 changes: 29 additions & 5 deletions doc/classes/Buffer.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ <h2 class="off-left">APIs</h2>

<li><a href="../classes/Mesh.html">Mesh</a></li>

<li><a href="../classes/Octree.html">Octree</a></li>

<li><a href="../classes/Shader.html">Shader</a></li>

<li><a href="../classes/Texture.html">Texture</a></li>
Expand Down Expand Up @@ -100,7 +102,7 @@ <h1>Buffer Class</h1>


<div class="foundat">
Defined in: <a href="../files/.._src_parsers.js.html#l2"><code>..&#x2F;src&#x2F;parsers.js:2</code></a>
Defined in: <a href="../files/.._src_mesh.js.html#l22"><code>..&#x2F;src&#x2F;mesh.js:22</code></a>
</div>


Expand Down Expand Up @@ -144,6 +146,12 @@ <h3 class="name"><code>Buffer</code></h3>

</li>

<li class="arg">

<code>stream_type</code>

</li>

</ul><span class="paren">)</span>
</div>

Expand Down Expand Up @@ -173,7 +181,7 @@ <h3 class="name"><code>Buffer</code></h3>



<a href="../files/.._src_parsers.js.html#l2"><code>..&#x2F;src&#x2F;parsers.js:2</code></a>
<a href="../files/.._src_mesh.js.html#l22"><code>..&#x2F;src&#x2F;mesh.js:22</code></a>

</p>

Expand Down Expand Up @@ -241,6 +249,22 @@ <h4>Parameters:</h4>

</li>

<li class="param">

<code class="param-name">stream_type</code>
<span class="type">Enum</span>




<div class="param-description">
<p>default gl.STATIC_DRAW (other: gl.DYNAMIC_DRAW, gl.STREAM_DRAW</p>

</div>


</li>

</ul>
</div>

Expand Down Expand Up @@ -307,7 +331,7 @@ <h3 class="name"><code>compile</code></h3>

<li class="arg">

<code>buffer_type</code>
<code>stream_type</code>

</li>

Expand Down Expand Up @@ -340,7 +364,7 @@ <h3 class="name"><code>compile</code></h3>



<a href="../files/.._src_mesh.js.html#l39"><code>..&#x2F;src&#x2F;mesh.js:39</code></a>
<a href="../files/.._src_mesh.js.html#l43"><code>..&#x2F;src&#x2F;mesh.js:43</code></a>

</p>

Expand All @@ -363,7 +387,7 @@ <h4>Parameters:</h4>

<li class="param">

<code class="param-name">buffer_type</code>
<code class="param-name">stream_type</code>
<span class="type">Number</span>


Expand Down
8 changes: 5 additions & 3 deletions doc/classes/GL.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ <h2 class="off-left">APIs</h2>

<li><a href="../classes/Mesh.html">Mesh</a></li>

<li><a href="../classes/Octree.html">Octree</a></li>

<li><a href="../classes/Shader.html">Shader</a></li>

<li><a href="../classes/Texture.html">Texture</a></li>
Expand Down Expand Up @@ -330,7 +332,7 @@ <h3 class="name"><code>gl.captureGamepads</code></h3>



<a href="../files/.._src_litegl.js.html#l170"><code>..&#x2F;src&#x2F;litegl.js:170</code></a>
<a href="../files/.._src_litegl.js.html#l199"><code>..&#x2F;src&#x2F;litegl.js:199</code></a>

</p>

Expand Down Expand Up @@ -395,7 +397,7 @@ <h3 class="name"><code>gl.captureKeys</code></h3>



<a href="../files/.._src_litegl.js.html#l126"><code>..&#x2F;src&#x2F;litegl.js:126</code></a>
<a href="../files/.._src_litegl.js.html#l155"><code>..&#x2F;src&#x2F;litegl.js:155</code></a>

</p>

Expand Down Expand Up @@ -563,7 +565,7 @@ <h3 class="name"><code>gl.getGamepads</code></h3>



<a href="../files/.._src_litegl.js.html#l189"><code>..&#x2F;src&#x2F;litegl.js:189</code></a>
<a href="../files/.._src_litegl.js.html#l218"><code>..&#x2F;src&#x2F;litegl.js:218</code></a>

</p>

Expand Down
2 changes: 2 additions & 0 deletions doc/classes/Indexer.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,8 @@ <h2 class="off-left">APIs</h2>

<li><a href="../classes/Mesh.html">Mesh</a></li>

<li><a href="../classes/Octree.html">Octree</a></li>

<li><a href="../classes/Shader.html">Shader</a></li>

<li><a href="../classes/Texture.html">Texture</a></li>
Expand Down
Loading

0 comments on commit 7e2ce14

Please sign in to comment.