From 8973175783f5338c85cda64d68e8ad6718a991ce Mon Sep 17 00:00:00 2001 From: "Sha..rd" Date: Sun, 25 Mar 2012 19:09:03 +0000 Subject: [PATCH] * Try to fix linkVertices in TangentBinormalGenerator (prevent assigning same tangent to verts with different texcoord) git-svn-id: https://jmonkeyengine.googlecode.com/svn/trunk@9263 75d07b2b-3a1a-0410-a2c5-0572b91ccdca --- .../jme3/util/TangentBinormalGenerator.java | 22 ++++++++++++++----- 1 file changed, 17 insertions(+), 5 deletions(-) diff --git a/engine/src/core/com/jme3/util/TangentBinormalGenerator.java b/engine/src/core/com/jme3/util/TangentBinormalGenerator.java index 8816b9159..36ab1cd19 100644 --- a/engine/src/core/com/jme3/util/TangentBinormalGenerator.java +++ b/engine/src/core/com/jme3/util/TangentBinormalGenerator.java @@ -67,11 +67,13 @@ public class TangentBinormalGenerator { private static class VertexInfo { public final Vector3f position; public final Vector3f normal; + public final Vector2f texCoord; public final ArrayList indices = new ArrayList(); - public VertexInfo(Vector3f position, Vector3f normal) { + public VertexInfo(Vector3f position, Vector3f normal, Vector2f texCoord) { this.position = position; this.normal = normal; + this.texCoord = texCoord; } } @@ -387,27 +389,37 @@ public class TangentBinormalGenerator { (FastMath.abs(u.z - v.z) < tolerance); } + private static boolean approxEqual(Vector2f u, Vector2f v) { + float tolerance = 1E-4f; + return (FastMath.abs(u.x - v.x) < tolerance) && + (FastMath.abs(u.y - v.y) < tolerance); + } + private static ArrayList linkVertices(Mesh mesh) { ArrayList vertexMap = new ArrayList(); - FloatBuffer vertexBuffer = (FloatBuffer) mesh.getBuffer(Type.Position).getData(); - FloatBuffer normalBuffer = (FloatBuffer) mesh.getBuffer(Type.Normal).getData(); + FloatBuffer vertexBuffer = mesh.getFloatBuffer(Type.Position); + FloatBuffer normalBuffer = mesh.getFloatBuffer(Type.Normal); + FloatBuffer texcoordBuffer = mesh.getFloatBuffer(Type.TexCoord); Vector3f position = new Vector3f(); Vector3f normal = new Vector3f(); + Vector2f texCoord = new Vector2f(); final int size = vertexBuffer.capacity() / 3; for (int i = 0; i < size; i++) { populateFromBuffer(position, vertexBuffer, i); populateFromBuffer(normal, normalBuffer, i); + populateFromBuffer(texCoord, texcoordBuffer, i); boolean found = false; for (int j = 0; j < vertexMap.size(); j++) { VertexInfo vertexInfo = vertexMap.get(j); if (approxEqual(vertexInfo.position, position) && - approxEqual(vertexInfo.normal, normal)) + approxEqual(vertexInfo.normal, normal) && + approxEqual(vertexInfo.texCoord, texCoord)) { vertexInfo.indices.add(i); found = true; @@ -416,7 +428,7 @@ public class TangentBinormalGenerator { } if (!found) { - VertexInfo vertexInfo = new VertexInfo(position.clone(), normal.clone()); + VertexInfo vertexInfo = new VertexInfo(position.clone(), normal.clone(), texCoord.clone()); vertexInfo.indices.add(i); vertexMap.add(vertexInfo); }