Skip to main content
GameDev.net gamedev.net
🔒 Locked

funky Normals

Started by m4gnus Apr 18, 2006 at 2:13 PM 2 replies 500+ views
Original Post
m4gnus
m4gnus
i've got a (hopefully) little problem with my normals. In my engine i calculate the normals after loading the mesh(i don't export them from maya,because it's always a lot of debugging until a new feature of the exporter works) and there are some problems when the mesh contains mutiple pieces(like 2 cubes next to each other). The first piece gets correct normals but the second one not. I did a screenshot to show what i mean: Edit:hmm dunno why but the image doesn't seem to work...so click the smybol to view it the left cube's normals are ok but the right one's are screwed up. Can anybody think of a reason for that? I'll post my code if somebody wants but maybe someone knows that problem. regards, m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."
acid2
acid2
At a guess, there's some funky adjancency info being produced, by the exporter. What's the code you're using to load the model?
bennyW
bennyW
An easy way to debug this is to just look at the values of the verts being used to generate the normals. Shouldn't be too much work since its just the two cubes. My guess is that you're using the wrong verts in your cross product calculations?
m4gnus
m4gnus
Quote:

At a guess, there's some funky adjancency info being produced, by the exporter. What's the code you're using to load the model?


the adjacency info is created by the engine after loading the mesh.

Quote:

An easy way to debug this is to just look at the values of the verts being used to generate the normals. Shouldn't be too much work since its just the two cubes. My guess is that you're using the wrong verts in your cross product calculations?



ok i'll check that.

anyway all that stuff isn't that much code so i'll post it here.
the adjacency info is created by my own class. At load time it just fills 2 arrays(in fact 2 std::vectors). 1 Array that stores all faces(i call the indices for this array faceindices from now on. to get a faceindex from a vertexindex you just need to do floor(float(i)/3.0f)[is that the same as i/3?]) and 1 2D array (std::vector< std::vector >) that stores all the faceindices of the faces around a vertex(adjacentFaces[vertexindex] <--this vector contains all faceindices of the adjacent faces)

that function looks like this:

void ngAdjacencyInfo::create(ngVertex *firstv,short *firsti,int numInd,int numVerts){	adjacentFaces.resize(numVerts);	faces.resize(numInd/3);	//int j=0;	for(int i=2;i<numInd;i+=3)	{		ngTriangle tri;		tri.create(&firstv[firsti[i-2]],&firstv[firsti[i-1]],&firstv[firsti]);		int faceindex=floor(float(i)/3.0f);		adjacentFaces.at(firsti[i-2]).push_back(faceindex);		adjacentFaces.at(firsti[i-1]).push_back(faceindex);		adjacentFaces.at(firsti).push_back(faceindex);		faces.at(faceindex)=tri;		//j++;	}	//int fsz=faces.size();}

ngTriangle::create looks like this:
void ngTriangle::create(ngVertex *vert1,ngVertex *vert2,ngVertex *vert3){		v1=vert1;	v2=vert2;	v3=vert3;	ngVector3 pos1,pos2,pos3;	pos1=ngVector3(v1->getAttribute(NG_POSITION,0));	pos2=ngVector3(v2->getAttribute(NG_POSITION,0));	pos3=ngVector3(v3->getAttribute(NG_POSITION,0));	n=crossVector3(pos2-pos1,pos3-pos1);}


and the (quite complex) code for getting all adajcent faces of a vertex(index):
	ngTriangle * getAdjacentFaces(short vertexindex,int *psize)	{		void *pOut;		int i=0;		int index=0;		std::vector<ngTriangle> res;		int size=adjacentFaces.size();		if(adjacentFaces.at(vertexindex).size()!=1);			res.resize(adjacentFaces.at(vertexindex).size()); 		std::vector<int> & adjFaces=adjacentFaces.at(vertexindex); 		for(std::vector<int>::iterator it=adjFaces.begin();it!=adjFaces.end();++it)		{			index=*it;			res.at(i)=faces.at(index);			i++;		}		pOut=malloc(res.size()*sizeof(ngTriangle));		memcpy(pOut,(void*)&res[0],res.size()*sizeof(ngTriangle));		int vsize=res.size();		memcpy((void*)psize,(void*)&vsize,sizeof(int));		return (ngTriangle*)pOut;	}



Edit: hmm ok at least the face normals seem correct. everytime i call ngTriangle::create the calculated nromal is pointing in the direction of one of the world axis.

so the problem is most probably in the function that calculates the vertexnormals:
void ngMesh::calculateNormals(){	//ok the follwing function is not really the perfect way of getting vertexnormals(does it all multiple times per vertex) but at least it (should) work(s)	int sz=Indices.size();	vertexnormals.resize(cVerts.size());	for(int i=0;i<Indices.size();i++)	{		short index=Indices.at(i);		std::vector<ngTriangle> adjFaces;		ngTriangle *tri;		int size;		tri=adjInfo.getAdjacentFaces(Indices.at(i),&size);		//std::vector<ngTriangle> & adjFaces=adjInfo.getAdjacentFaces(Indices.at(i));		ngVector3 avgNormal(0.0f,0.0f,0.0f);		for(int j=0;j<size;j++)		{			avgNormal=avgNormal+tri[j].getNormal();		}		avgNormal=avgNormal/size;		vertexnormals[Indices].x=avgNormal.x;		vertexnormals[Indices].y=avgNormal.y;		vertexnormals[Indices].z=avgNormal.z;	}	}

PS: i can't check if the vertex normals calculated by that function are correct as i don't know what the correct values for a cube are...


regards,
m4gnus
"There are 10 types of people in the world... those who understand binary and those who don't."

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.