#### Archived

This topic is now archived and is closed to further replies.

# Dot3 calculations

This topic is 6037 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

Would any like examing this and telling me if it is correct or not because its not working right and this is my poorest area on the topic of Dot3. I hope you can under stand the structs I that were used by the names.


// The following two functions take the model and find the tangent and binormals for all vertexs

void tangent_basis(float v0[3], float v1[3], float v2[3], float t0[2], float t1[2], float t2[2], float *tangent, float *binormal)
{
float cp[3];
float e0[3]={ v1[0]-v0[0], t1[0]-t0[0], t1[1]-t0[1] };
float e1[3]={ v2[0]-v0[0], t2[0]-t0[0], t2[1]-t0[1] };

cp[0]=e0[1]*e1[2]-e0[2]*e1[1];
cp[1]=e0[2]*e1[0]-e0[0]*e1[2];
cp[2]=e0[0]*e1[1]-e0[1]*e1[0];

if(fabs(cp[0])>0.00001f)
{
tangent[0]=-cp[1]/cp[0];
binormal[0]=-cp[2]/cp[0];
}

e0[0]=v1[1]-v0[1];
e1[0]=v2[1]-v0[1];

cp[0]=e0[1]*e1[2]-e0[2]*e1[1];
cp[1]=e0[2]*e1[0]-e0[0]*e1[2];
cp[2]=e0[0]*e1[1]-e0[1]*e1[0];

if(fabs(cp[0])>0.00001f)
{
tangent[1]=-cp[1]/cp[0];
binormal[1]=-cp[2]/cp[0];
}

e0[0]=v1[2]-v0[2];
e1[0]=v2[2]-v0[2];

cp[0]=e0[1]*e1[2]-e0[2]*e1[1];
cp[1]=e0[2]*e1[0]-e0[0]*e1[2];
cp[2]=e0[0]*e1[1]-e0[1]*e1[0];

if(fabs(cp[0])>0.00001f)
{
tangent[2]=-cp[1]/cp[0];
binormal[2]=-cp[2]/cp[0];
}
}

{
int current_DOT3=0;
tVector3 vVector1, vVector2, vNormal, vPoly[3];

// If there are no objects, we can skip this part

if(pModel->numOfObjects <= 0)
return;

// Go through each of the objects to calculate their normals

for(int index = 0; index < pModel->numOfObjects; index++)
{
// Get the current object

t3DObject *pObject = &(pModel->pObject[index]);

// Here we allocate all the memory we need to calculate the normals

pObject->pBinormal		= new tVector3 [pObject->numOfVerts];
pObject->pTangent		= new tVector3 [pObject->numOfVerts];

// Go though all of the faces of this object

for(int i=0; i < pObject->numOfFaces; i++)
{
float v0[3], v1[3], v2[3], t0[2], t1[2], t2[2], tangent[3], binormal[3];

v0[0]= pObject->pVerts[  pObject->pFaces[i].vertIndex[0] ].x;
v0[1]= pObject->pVerts[  pObject->pFaces[i].vertIndex[0] ].y;
v0[2]= pObject->pVerts[  pObject->pFaces[i].vertIndex[0] ].z;

v1[0]= pObject->pVerts[  pObject->pFaces[i].vertIndex[1] ].x;
v1[1]= pObject->pVerts[  pObject->pFaces[i].vertIndex[1] ].y;
v1[2]= pObject->pVerts[  pObject->pFaces[i].vertIndex[1] ].z;

v2[0]= pObject->pVerts[  pObject->pFaces[i].vertIndex[2] ].x;
v2[1]= pObject->pVerts[  pObject->pFaces[i].vertIndex[2] ].y;
v2[2]= pObject->pVerts[  pObject->pFaces[i].vertIndex[2] ].z;

t0[0]=pObject->pTexVerts[ pObject->pFaces[i].vertIndex[0] ].x ;
t0[1]=pObject->pTexVerts[ pObject->pFaces[i].vertIndex[0] ].y ;

t1[0]=pObject->pTexVerts[ pObject->pFaces[i].vertIndex[1] ].x ;
t1[1]=pObject->pTexVerts[ pObject->pFaces[i].vertIndex[1] ].y ;

t2[0]=pObject->pTexVerts[ pObject->pFaces[i].vertIndex[2] ].x ;
t2[1]=pObject->pTexVerts[ pObject->pFaces[i].vertIndex[2] ].y ;

tangent_basis(v0, v1, v2, t0, t1, t2, tangent, binormal);

for(int k=0;k<3;k++)
{
pObject->pFaces[i].DOT3Index[k]=current_DOT3;
pObject->pTangent[  current_DOT3 ].x=tangent[0];
pObject->pTangent[  current_DOT3 ].y=tangent[1];
pObject->pTangent[  current_DOT3 ].z=tangent[2];

pObject->pBinormal[  current_DOT3 ].x=binormal[0];
pObject->pBinormal[  current_DOT3 ].y=binormal[1];
pObject->pBinormal[  current_DOT3 ].z=binormal[2];
current_DOT3++;
}

}
}
}


This is a snippit from the drawing code this makes it ready to be passed.

...
float m[9], lv[3], tc[3], len;

tVector3 NewPoint;
NewPoint=RotateAnything(Rotation,LightLoc-pObjectdata->pVerts[ index ]);

lv[0]=(NewPoint.x);
lv[1]=(NewPoint.y);
lv[2]=(NewPoint.z);

len=sqrt((lv[0]*lv[0])+(lv[1]*lv[1])+(lv[2]*lv[2]));
if(len!=0.0f)
{
lv[0]/=len;
lv[1]/=len;
lv[2]/=len;
}

//float modelview[16];

//glGetFloatv(GL_MODELVIEW_MATRIX , modelview);

tVector3 Tan=pObjectdata->pTangent[ pObjectdata->pFaces[j].DOT3Index[whichVertex] ];
tVector3 Bin=pObjectdata->pBinormal[ pObjectdata->pFaces[j].DOT3Index[whichVertex] ];
tVector3 Nor=pObjectdata->pNormals[ pObjectdata->pFaces[j].vertIndex[whichVertex] ];

m[0]=Tan.x;
m[1]=Bin.x;
m[2]=Nor.x;
m[3]=Tan.y;
m[4]=Bin.y;
m[5]=Nor.y;
m[6]=Tan.z;
m[7]=Bin.z;
m[8]=Nor.z;

tc[0]=(lv[0]*m[0])+(lv[1]*m[3])+(lv[2]*m[6]);
tc[1]=(lv[0]*m[1])+(lv[1]*m[4])+(lv[2]*m[7]);
tc[2]=(lv[0]*m[2])+(lv[1]*m[5])+(lv[2]*m[8]);

tc[0]=(tc[0]*0.5f)+0.5f;
tc[1]=(tc[1]*0.5f)+0.5f;
tc[2]=(tc[2]*0.5f)+0.5f;

glColor3f(tc[0], tc[1], tc[2]);
...

the main problem is that the tangents and binormals are not gernateing properly they are of all diffrent lenghts some are more then 100 units long! and they go out in strange directions and they are not parrel to each other, not even on a flat side. well I hope I can get an answer although I am asking a lot.

1. 1
2. 2
Rutin
16
3. 3
4. 4
5. 5

• 26
• 9
• 11
• 9
• 9
• ### Forum Statistics

• Total Topics
633710
• Total Posts
3013486
×