# problem of lighting with .3ds files

## Recommended Posts

TheSeb    144
Hi, I have a problem with my models : they aren't lighted completly. Actually, the model which i test is lighted correctly excepted on several parts. I have checked some normals which i compute and there are right. I describe what my program does : -i make the computations for the weight normals -i compute the normals -i multiply the vertices by the transformation matrix -i multiply the normals computed by the 3x3 part of the transformation matrix -i swap z by -y for the normals and the vertices when i multiply the vertices by the transformation matrix my model appears at a place which is not the place where it is supposed to appear. i put the code of the multiplication by the transformation matrix :
void loader3ds::multiplyMatrixVertices(float *tMatrix, float *vertices, unsigned short *nbVertices)
{
float normalsTemp[MAX_VERTICES*3] ;

for(i=0 ; i < *nbVertices ; i++)//la 4ème coordonnée d'un point est 1
{
for(j=0 ; j<4 ; j++)
{
normalsTemp[i*3+j] = tMatrix[j*3+0] * vertices[i*3+0] +
tMatrix[j*3+1] * vertices[i*3+1] +
tMatrix[j*3+2] * vertices[i*3+2] +
tMatrix[j*3+3] * 1;
}
}

for(i=0 ; i < *nbVertices*3 ; i++)
{
vertices[i] = normalsTemp[i] ;
}
}

void loader3ds::multiplyMatrixNormals(float *tMatrix, float *normals, unsigned short *nbVertices)
{

float normalsTemp[MAX_VERTICES*3] ;
for(i=0 ; i < *nbVertices ; i++)
{
for(j=0 ; j<3 ; j++)
{
normalsTemp[i*3+j] = tMatrix[j*3+0] * normals[i*3+0] +
tMatrix[j*3+1] * normals[i*3+1] +
tMatrix[j*3+2] * normals[i*3+2] ;
}
}

for(i=0 ; i < *nbVertices*3 ; i++)
{
normals[i] = normalsTemp[i] ;
}
}


the part which compute the per vertex normals :
//nIndice is the number of the normal from 0 to nbVertices
for(nIndice=0 ; nIndice < myObjects[counterObj].nbVertices ; nIndice++)
{
for(i=0 ; i < myObjects[counterObj].nbPoly ; i++)//polygon per polygon
{
for(j=0 ; j< 3; j++)//the three vertices of each polygon
{
if(myObjects[counterObj].myIndices[i][j]==nIndice)
{
myObjects[counterObj].perVertexNormals[nIndice][0] += myObjects[counterObj].perFaceNormals[i].x*angle[i][j] ;
myObjects[counterObj].perVertexNormals[nIndice][1] += myObjects[counterObj].perFaceNormals[i].y*angle[i][j] ;
myObjects[counterObj].perVertexNormals[nIndice][2] += myObjects[counterObj].perFaceNormals[i].z*angle[i][j] ;
}
}
}
}



##### Share on other sites
TheSeb    144
i put the code which compute the normals' weight which is followed by the initialisation and the computation of the per vertex normals(in the previous post)
file.read((char*) &(myObjects[counterObj].nbPoly), sizeof (unsigned short int));		for (polyCounter = 0 ; polyCounter< myObjects[counterObj].nbPoly ; polyCounter++)	{			file.read((char*) &(myObjects[counterObj].myIndices[polyCounter][0]), sizeof (unsigned short int));		file.read((char*) &(myObjects[counterObj].myIndices[polyCounter][1]), sizeof (unsigned short int));		file.read((char*) &(myObjects[counterObj].myIndices[polyCounter][2]), sizeof (unsigned short int));		file.read((char*) &(faceInfo), sizeof (short int));	//	cout<<"face info "<<faceInfo<<endl ;		int Va = myObjects[counterObj].myIndices[polyCounter][0] ;		int Vb = myObjects[counterObj].myIndices[polyCounter][1] ;		int Vc = myObjects[counterObj].myIndices[polyCounter][2] ;                                                                       				float Vab[] = {(myObjects[counterObj].myVertices[Vb][0] - myObjects[counterObj].myVertices[Va][0]),					(myObjects[counterObj].myVertices[Vb][1] - myObjects[counterObj].myVertices[Va][1]),					(myObjects[counterObj].myVertices[Vb][2] - myObjects[counterObj].myVertices[Va][2])};		float Vac[] = {(myObjects[counterObj].myVertices[Vc][0] - myObjects[counterObj].myVertices[Va][0]),					(myObjects[counterObj].myVertices[Vc][1] - myObjects[counterObj].myVertices[Va][1]),					(myObjects[counterObj].myVertices[Vc][2] - myObjects[counterObj].myVertices[Va][2])};				float Vba[] = {(myObjects[counterObj].myVertices[Va][0] - myObjects[counterObj].myVertices[Vb][0]),					(myObjects[counterObj].myVertices[Va][1] - myObjects[counterObj].myVertices[Vb][1]),					(myObjects[counterObj].myVertices[Va][2] - myObjects[counterObj].myVertices[Vb][2])};		float Vbc[] = {(myObjects[counterObj].myVertices[Vc][0] - myObjects[counterObj].myVertices[Vb][0]),					(myObjects[counterObj].myVertices[Vc][1] - myObjects[counterObj].myVertices[Vb][1]),					(myObjects[counterObj].myVertices[Vc][2] - myObjects[counterObj].myVertices[Vb][2])};		float Vca[] = {(myObjects[counterObj].myVertices[Va][0] - myObjects[counterObj].myVertices[Vc][0]),					(myObjects[counterObj].myVertices[Va][1] - myObjects[counterObj].myVertices[Vc][1]),					(myObjects[counterObj].myVertices[Va][2] - myObjects[counterObj].myVertices[Vc][2])};		float Vcb[] = {(myObjects[counterObj].myVertices[Vb][0] - myObjects[counterObj].myVertices[Vc][0]),					(myObjects[counterObj].myVertices[Vb][1] - myObjects[counterObj].myVertices[Vc][1]),					(myObjects[counterObj].myVertices[Vb][2] - myObjects[counterObj].myVertices[Vc][2])};				normalize(Vab) ;		normalize(Vac) ;		dotProduct(Vab, Vac, &resultDp) ; 		//oFile2<<dec<<"resultDp "<<resultDp<<endl ;		angle[polyCounter][0]=acosf(resultDp) ;		//oFile2<<"angle "<<dec<<angle[polyCounter][0]<<endl ;		normalize(Vba) ;		normalize(Vbc) ;		dotProduct(Vba, Vbc, &resultDp) ;		//cout<<"resultDp "<<resultDp<<endl ;		angle[polyCounter][1]=acosf(resultDp) ;		normalize(Vca) ;		normalize(Vcb) ;		dotProduct(Vca, Vcb, &resultDp) ;		angle[polyCounter][2]=acosf(resultDp) ;				switch (faceInfo)		{			case 0x3 :crossProduct(&myObjects[counterObj].perFaceNormals[polyCounter], Vac, Vbc) ;				      break ;			case 0x7 :crossProduct(&myObjects[counterObj].perFaceNormals[polyCounter], Vab, Vbc) ;				      break ;			case 0x6 :crossProduct(&myObjects[counterObj].perFaceNormals[polyCounter], Vab, Vbc) ;				      break ;			case 0x5 :crossProduct(&myObjects[counterObj].perFaceNormals[polyCounter], Vac, Vab) ;				      break ;			case 0x4 :crossProduct(&myObjects[counterObj].perFaceNormals[polyCounter], Vab, Vcb) ;				      break ;			case 0x2 :crossProduct(&myObjects[counterObj].perFaceNormals[polyCounter], Vca, Vbc) ;				      break ;			case 0x1 :crossProduct(&myObjects[counterObj].perFaceNormals[polyCounter], Vac, Vcb) ;				      break ;			case 0x0 :crossProduct(&myObjects[counterObj].perFaceNormals[polyCounter], Vba, Vcb) ;				      break ;		}		normalize(&myObjects[counterObj].perFaceNormals[polyCounter]) ;				//oFile2<<dec<<"angle "<<angle[polyCounter*3]<<" "<<angle[polyCounter*3+1]<<" "<<angle[polyCounter*3+2]<<endl ;			}//end of the for loop