I tried flipping the normals, but everything goes black.
Here’s how i calculate the centroid and the midpoint of the edges for each triangle
centroid[j]=(el->vertTab[v[0]+j]+el->vertTab
for (j=0;j<3;j++) //calculate the coordinates of the centroid of the triangle
{// and new midpoint vertexes
[v[1]+j]+el->vertTab[v[2]+j])/3.0;
mid0[j]=(el->vertTab[v[0]+j]+el->vertTab[v[1]+j])/2.0;//midpoint of edge 0-1
mid1[j]=(el->vertTab[v[1]+j]+el->vertTab[v[2]+j])/2.0;//midpoint of edge 1-2
mid2[j]=(el->vertTab[v[2]+j]+el->vertTab[v[0]+j])/2.0;//midpoint of edge 2-0
}
Now i add the new vertexes v(centroid,mid0, mid1, mid2) to el->vertTab, and put in pos[0],pos[1], pos[2],pos[3] the position where these vertexes are in in el->vertTab
i then add the indexes of the vertexes of the new triangles
addMeshIndexes(el,v[0],pos[0],pos[3]);
addMeshIndexes(el,pos[0],v[1],pos[3]);
addMeshIndexes(el,pos[3],v[1],pos[1]);
addMeshIndexes(el,pos[3],pos[1],v[2]);
addMeshIndexes(el,v[2],pos[2],pos[3]);
addMeshIndexes(el,pos[2],v[0],pos[3]);
and calculate the normals like this:
GLfloat v[3];
GLfloat *ptr;
int count;
v[0]=v[1]=v[2]=0;
findFaces(el,vertNum);//finds the faces incident on the vertex vertNum and put them in a list in el->faces
f=el->faceList;
count=0;
while(f)
{
calcFaceNormals(el,f->face,v);//calculates the normal of the face pointed byt f->face and add its values to v
count++;
f=f->next;
}
if (count)
{//mediates the values of the normals
v[0]/=count;
v[1]/=count;
v[2]/=count;
}
ptr=&el->normalTab[el->normalTabCur];
//adds the coordinates of the normal to che normal vector
*ptr++=v[0];*ptr++=v[1];
*ptr++=v[2];
el->normalTabCur+=3;
This is the function calcFaceNormals:
void Models::calcFaceNormals(listModels *el,int face, GLfloat v[3])
{
GLfloat vA[3],vB[3];
int vert[3];
float x,y,z;
int j;
findVertexesOfFace(el,face,vert);
for (j=0;j<3;j++)
{
vA[j]=el->vertTab[vert[0]+j]-el->vertTab[vert[1]+j];
vB[j]=el->vertTab[vert[1]+j]-el->vertTab[vert[2]+j];
}
calculateNormalComponents(vA,vB,x,y,z);
v[0]+=x;
v[1]+=y;
v[2]+=z;
}
And this is calculateNormalComponents
void Models::calculateNormalComponents(GLfloat vA[3],GLfloat vB[3],float &x,float &y,float &z)
{
float mod;
float tolerance=0.00001f;
x=vA[1]*vB[2]-vA[2]*vB[1];
y=vA[2]*vB[0]-vA[0]*vB[2];
z=vA[0]*vB[1]-vA[1]*vB[0];
mod=sqrt(x*x+y*y+z*z);
x/=mod;
y/=mod;
z/=mod;
if ((x>0 && x<tolerance) | |(x<0 && x>-tolerance))
x=0;
if ((y>0 && y<tolerance) | |(y<0 && y>-tolerance))
y=0;
if ((z>0 && z<tolerance) | |(z<0 && z>-tolerance))
z=0;
}