Jump to content

  • Log In with Google      Sign In   
  • Create Account


marshmarsh

Member Since 30 Oct 2012
Offline Last Active Nov 02 2012 09:53 AM

Topics I've Started

Tangent Space compute fail

30 October 2012 - 06:56 AM

Hi guys, in advance sorry for my bad english.

Now I'm implementing bump(normal) mapping in my program, but there is a fail: http://img51.imageshack.us/img51/8934/tangent2.jpg
You can see triangles on surface..

Can you see my function to compute tangent space, i can't find fail :(

landScape.verts - vertex mass landScape.verts[0] = x of first vert, landScape.verts[1] = y of first vert, landScape.verts[2] = z of frist vert and etc.
landScape.normals - same for normals,
landScape.texCoord - same for texture coords, but not xyz, only UV;

[source lang="cpp"]void GLW::computeLandScapeTBN() { QVector3D p1, p2, p3; QVector3D vec1, vec2; QVector3D tangent; QVector3D normal; QVector3D binormal; QVector3D tangentOrth; float u1, u2, u3, v1, v2, v3; float divider; float NdotT; float NdotN; bool rightHanded; for (int i = 0, k = 0; i < landScape.verts.size(); i += 9, k += 6) { u1 = landScape.texCoord[k]; v1 = landScape.texCoord[k+1]; u2 = landScape.texCoord[k+2]; v2 = landScape.texCoord[k+3]; u3 = landScape.texCoord[k+4]; v3 = landScape.texCoord[k+5]; p1.setX(landScape.verts[i]), p1.setY(landScape.verts[i+1]), p1.setZ(landScape.verts[i+2]); p2.setX(landScape.verts[i+3]), p2.setY(landScape.verts[i+4]), p2.setZ(landScape.verts[i+5]); p3.setX(landScape.verts[i+6]), p3.setY(landScape.verts[i+7]), p3.setZ(landScape.verts[i+8]); vec1 = QVector3D((p2.x()-p1.x())*(v3-v1), (p2.y()-p1.y())*(v3-v1), (p2.z()-p1.z())*(v3-v1)); vec2 = QVector3D((p3.x()-p1.x())*(v2-v1), (p3.y()-p1.y())*(v2-v1), (p3.z()-p1.z())*(v2-v1)); divider = (u2-u1)*(v3-v1) - (v2-v1)*(u3-u1); tangent = QVector3D((vec1.x()-vec2.x())/divider, (vec1.y()-vec2.y())/divider, (vec1.z()-vec2.z())/divider); normal = QVector3D(landScape.normals[i], landScape.normals[i+1], landScape.normals[i+2]); NdotT = normal.x()*tangent.x() + normal.y()*tangent.y() + normal.z()*tangent.z(); NdotN = normal.x()*normal.x() + normal.y()*normal.y() + normal.z()*normal.z(); tangentOrth = QVector3D(tangent.x() - normal.x()*NdotT/NdotN, tangent.y() - normal.y()*NdotT/NdotN, tangent.z() - normal.z()*NdotT/NdotN); tangentOrth.normalize(); binormal = QVector3D::crossProduct(normal, tangentOrth); rightHanded = QVector3D::dotProduct(QVector3D::crossProduct(tangentOrth, binormal), normal) >= 0; if(!rightHanded) binormal = QVector3D(binormal.x()*(-1), binormal.y()*(-1), binormal.z()*(-1)); landScape.tangents.push_back(tangentOrth.x()), landScape.tangents.push_back(tangentOrth.y()), landScape.tangents.push_back(tangentOrth.z()); landScape.binormals.push_back(binormal.x()), landScape.binormals.push_back(binormal.y()), landScape.binormals.push_back(binormal.z()); normal = QVector3D(landScape.normals[i+3], landScape.normals[i+4], landScape.normals[i+5]); NdotT = normal.x()*tangent.x() + normal.y()*tangent.y() + normal.z()*tangent.z(); NdotN = normal.x()*normal.x() + normal.y()*normal.y() + normal.z()*normal.z(); tangentOrth = QVector3D(tangent.x() - normal.x()*NdotT/NdotN, tangent.y() - normal.y()*NdotT/NdotN, tangent.z() - normal.z()*NdotT/NdotN); tangentOrth.normalize(); binormal = QVector3D::crossProduct(normal, tangentOrth); rightHanded = QVector3D::dotProduct(QVector3D::crossProduct(tangentOrth, binormal), normal) >= 0; if(!rightHanded) binormal = QVector3D(binormal.x()*(-1), binormal.y()*(-1), binormal.z()*(-1)); landScape.tangents.push_back(tangentOrth.x()), landScape.tangents.push_back(tangentOrth.y()), landScape.tangents.push_back(tangentOrth.z()); landScape.binormals.push_back(binormal.x()), landScape.binormals.push_back(binormal.y()), landScape.binormals.push_back(binormal.z()); normal = QVector3D(landScape.normals[i+6], landScape.normals[i+7], landScape.normals[i+8]); NdotT = normal.x()*tangent.x() + normal.y()*tangent.y() + normal.z()*tangent.z(); NdotN = normal.x()*normal.x() + normal.y()*normal.y() + normal.z()*normal.z(); tangentOrth = QVector3D(tangent.x() - normal.x()*NdotT/NdotN, tangent.y() - normal.y()*NdotT/NdotN, tangent.z() - normal.z()*NdotT/NdotN); tangentOrth.normalize(); binormal = QVector3D::crossProduct(normal, tangentOrth); rightHanded = QVector3D::dotProduct(QVector3D::crossProduct(tangentOrth, binormal), normal) >= 0; if(!rightHanded) binormal = QVector3D(binormal.x()*(-1), binormal.y()*(-1), binormal.z()*(-1)); landScape.tangents.push_back(tangentOrth.x()), landScape.tangents.push_back(tangentOrth.y()), landScape.tangents.push_back(tangentOrth.z()); landScape.binormals.push_back(binormal.x()), landScape.binormals.push_back(binormal.y()), landScape.binormals.push_back(binormal.z()); }}[/source]
In advance, thanks :)

PARTNERS