[osg-users] Smooth shading

Zahraa Yasseen zmy01 at aub.edu.lb
Tue Oct 30 03:53:31 PDT 2007


Hello,

I'm working on a project in which we need to output 3D objects  
constructed by the code. We generate vertices and then triangles. When  
the normal binding is per_primitive, every thing works fine. When it  
is per_vertex, either the osgProducer::Viewer crashes or the result is  
incorrect (badly shaded triangles). Here is the code:

	if(geo->getNormalBinding() == osg::Geometry::BIND_PER_PRIMITIVE)
	{
		for(unsigned int i=0; i<maxFaceId; i++)
		{
			unsigned int A = GetFaceVertex(i, 0);
			unsigned int B = GetFaceVertex(i, 1);
			unsigned int C = GetFaceVertex(i, 2);

			osg::Vec3f BA = vertices->at(A) - vertices->at(B);
			osg::Vec3f CB = vertices->at(B) - vertices->at(C);
			osg::Vec3f N = BA ^ CB;
			N.normalize();
			normals->push_back(N);
		}
	}

	else if(geo->getNormalBinding() == osg::Geometry::BIND_PER_VERTEX)
	{

		for(unsigned int i=0; i<vertices->getNumElements(); i++)
			normals->push_back(osg::Vec3f(0.0, 0.0, 0.0));

		for(unsigned int i=0; i<maxFaceId; i++)
		{
			unsigned int A = GetFaceVertex(i, 0);
			unsigned int B = GetFaceVertex(i, 1);
			unsigned int C = GetFaceVertex(i, 2);

			osg::Vec3f BA = vertices->at(A) - vertices->at(B);
			osg::Vec3f CB = vertices->at(B) - vertices->at(C);
			osg::Vec3f N = BA ^ CB;
			(*normals)[A] += N;
			(*normals)[B] += N;
			(*normals)[C] += N;
		}

		for(unsigned int i=0; i<normals->getNumElements(); i++)
			normals->at(i).normalize();
	}

thanks

----------------------------------------------------------------




More information about the osg-users mailing list