problem of lighting computation with .3ds files

Started by
2 comments, last by TheSeb 18 years, 9 months ago
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 = normalsTemp ;	
	}
}

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 = normalsTemp ;	
	}
}




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[j]==nIndice)
				{
					myObjects[counterObj].perVertexNormals[nIndice][0] += myObjects[counterObj].perFaceNormals.x*angle[j] ;
					myObjects[counterObj].perVertexNormals[nIndice][1] += myObjects[counterObj].perFaceNormals.y*angle[j] ;
					myObjects[counterObj].perVertexNormals[nIndice][2] += myObjects[counterObj].perFaceNormals.z*angle[j] ;
				}
			}
		}
	}



[Edited by - TheSeb on June 29, 2005 3:59:06 AM]
Advertisement
Quote:Original post by TheSeb
-i swap z by -y for the normals and the vertices


*** Source Snippet Removed ***


I don't think you can do this. I got weird results when I did this. I had to apply a rotation matrix to get the vertices and normals right.

Hope this helps.
The more applications I write, more I find out how less I know
i have tried without swapping but i have the same problem with lighting and my model has moved at a place of the screen where it is not supposed to be.
I have forgotten something : 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 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

This topic is closed to new replies.

Advertisement