⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cvnormal.cpp

📁 Windows上的MUD客户端程序
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	}

	for ( int i = 0 ; i < m_pSubDivision ; i++ )
	{
		float theta = -i*M_PI/m_pSubDivision ;
		m_pVertexNormals[i].x() = cos( theta ) ;
		m_pVertexNormals[i].y() = 0 ;
		m_pVertexNormals[i].z() = sin( theta ) ;

		theta *= 1.5 ;

		m_pFaceNormals[i].x() = cos( theta ) ;
		m_pFaceNormals[i].y() = 0 ;
		m_pFaceNormals[i].z() = sin( theta ) ;
	}
}

ChQvCylinderNormals::~ChQvCylinderNormals()
{
	delete[] m_pFaceNormals ;
	delete[] m_pVertexNormals ;
}

chint32 ChQvCylinderNormals::GetNumParts()
{
	//
	// The cylinder lies along y-axis and is centered on the origin
	//		Part 0 - the vertical walls of the cylinder
	//		Part 1 - the base of the cylinder on +ve y-axis
	//		Part 2 - the base of the cylinder on -ve y-axis
	//
	return 3 ;
}

chint32 ChQvCylinderNormals::GetNumFaces( chint32 lPart )
{
	//
	// All parts subdiv faces
	//
	return m_pSubDivision ;
}

chint32 ChQvCylinderNormals::GetNumVertices( chint32, chint32 lPart )
{
	//
	// The vertical walls of the cylinder are tessellated into rectangles
	// whereas the circular bases are tessellated into triangles similar
	// to the cone.
	//
	if ( lPart == 0 )
		return 4 ;
	else
		return 3 ;
}

GxVec3f ChQvCylinderNormals::GetPartNormal( chint32 lPart )
{
	//
	// Simply return the first face normal for vertical part
	//
	if ( lPart == 0 )
		return m_pFaceNormals[0] ;
	else if ( lPart == 1 )
		return m_pBaseNormals[0] ;
	else
		return m_pBaseNormals[1] ;
}

GxVec3f ChQvCylinderNormals::GetFaceNormal( chint32 lFace, chint32 lPart )
{
	//
	// All the faces in a base have the same normal
	//
	if ( lPart == 0 )
		return m_pFaceNormals[lFace] ;
	else if ( lPart == 1 )
		return m_pBaseNormals[0] ;
	else
		return m_pBaseNormals[1] ;
}

GxVec3f ChQvCylinderNormals::GetVertexNormal( chint32 lVertex,
											   chint32 lFace, chint32 lPart )
{
	if ( lPart == 0 )
	{
		lVertex = lVertex >> 2 ;
		if ( lVertex == 0 )
			return m_pVertexNormals[lFace] ;
		else
			return m_pVertexNormals[(lFace+1)%m_pSubDivision] ;
	}
	else if ( lPart == 1 )
		return m_pBaseNormals[0] ;
	else
		return m_pBaseNormals[1] ;
}

#ifdef MY_NOTYET

//
// I have not yet decided upon the tessellation scheme for the sphere.
// However it will have one part and f(subdiv) faces and g(subdiv) vertices.
//
ChQvSphereNormals::ChQvSphereNormals( QvSphere* sphere, QvState* state )
{
}

ChQvSphereNormals::~ChQvSphereNormals()
{
}

chint32 ChQvSphereNormals::GetNumParts()
{
}

chint32 ChQvSphereNormals::GetNumFaces( chint32 lPart )
{
}

chint32 ChQvSphereNormals::GetNumVertices( chint32 lFace, chint32 lPart )
{
}

GxVec3f ChQvSphereNormals::GetPartNormal( chint32 lPart )
{
}

GxVec3f ChQvSphereNormals::GetFaceNormal( chint32 lFace, chint32 lPart )
{
}

GxVec3f ChQvSphereNormals::GetVertexNormal( chint32 lVertex,
											 chint32 lFace, chint32 lPart )
{
}

#endif

bool ChQvIndexedFaceSetNormals::Set( QvIndexedFaceSet* ifs )
{
   if ( !ifs ) return false ;

   Destroy() ;
   m_pIFS = ifs ;

   // find how many faces we have, currently we do not need this
   // however if we change the logic for default normal generation
   // we will need the number of faces to make sure we have enough
   // normals for PER_FACE and PER_FACE_INDEXED bindings
   int numCoordIndices = ifs->coordIndex.num ;
   long* coordIndices = ifs->coordIndex.values ;
   for( int i = 0 ; i < numCoordIndices ; i++ )
   {
      if ( coordIndices[i] == -1 )
	 continue ;
      if ( i < numCoordIndices-1 && coordIndices[i+1] == -1 ) m_numFaces++ ;
   }

   return true ;
}

bool ChQvIndexedFaceSetNormals::Set( QvState* state )
{
	if ( !m_pIFS || !state ) return false ;

	m_pState = state ;

	// Get the necessary elements
	QvNormalBinding* nbelt =	0;
	if(state->getTopElement(QvState::NormalBindingIndex))
	{
		nbelt = ( (QvNormalBinding*)
			(state->getTopElement(QvState::NormalBindingIndex)->data) );
	}

	QvCoordinate3* c3elt = 0;
	if(state->getTopElement(QvState::Coordinate3Index))
	{
		c3elt =   ( (QvCoordinate3*)
			(state->getTopElement(QvState::Coordinate3Index)->data) ) ;
	}

	QvNormal* n3elt = 0;
	if(state->getTopElement(QvState::NormalIndex))
	{
		n3elt =  ( (QvNormal*)
			(state->getTopElement(QvState::NormalIndex)->data) ) ;
	}

	// Pull the information from the elements
	QvNormalBinding::Binding nbinding = QvNormalBinding::DEFAULT ;
	if ( nbelt ) nbinding = (QvNormalBinding::Binding) (nbelt->value.value) ;
	float* normalList = ( n3elt ? n3elt->vector.values : 0 ) ;
	float* coordList = ( c3elt ? c3elt->point.values : 0 ) ;

	// We really can't do without coordinates
	if ( !c3elt || !coordList ) return false ;

	//
	// Decide if we need to generate/use default normals (see Mentor p132)
	//
	//if ( !normalList || n3elt->vector.num != c3elt->point.num )
	if ( !normalList || m_pIFS->coordIndex.num > m_pIFS->normalIndex.num )
		nbinding = QvNormalBinding::DEFAULT ;

	long* normalIndex = m_pIFS->normalIndex.values ;

	switch( nbinding )
	{

		case QvNormalBinding::NONE:
			break ;

		case QvNormalBinding::DEFAULT:
		case QvNormalBinding::OVERALL:
		case QvNormalBinding::PER_PART:
		case QvNormalBinding::PER_PART_INDEXED:
			m_default = true ;
			break ;

		case QvNormalBinding::PER_FACE_INDEXED:
			if ( normalIndex )
				m_indexed = true ;	 
		case QvNormalBinding::PER_FACE:
			m_perFace = true ;
			break ;
		
		case QvNormalBinding::PER_VERTEX_INDEXED:
			if ( normalIndex && normalIndex[0] != -1 )
				m_indexed = true ;
		case QvNormalBinding::PER_VERTEX:
			m_perVertex = true ;
			break ;
	}

   if ( m_indexed ) m_pIndices = normalIndex ;
   if ( m_perFace || m_perVertex ) m_pNormals = (GxVec3f*)normalList ;

   // Generate default normals if necessary
   if ( m_default && !m_pDefaultNormals )
   {
   	  QvElement *pElt =  state->getTopElement(QvState::ShapeHintsIndex);

      QvShapeHints	defaultHints;	  
      QvShapeHints* shelt = &defaultHints;
	  if(pElt)
	  {
	  	shelt =  ( (QvShapeHints*)(pElt->data) ) ;
	  }
	  
	
      float creaseAngle = ( shelt ? shelt->creaseAngle.value : 0 ) ;

      int numNormals ;
      float* normals ;
      chuint32* pFlags ;
      if ( GxGenIFSNormals( c3elt->point.num, coordList,
			    m_pIFS->coordIndex.num, m_pIFS->coordIndex.values,
			    creaseAngle, numNormals, normals, &pFlags ) )
      {
	 m_numDefaultNormals = numNormals ;
	 m_pDefaultNormals = (GxVec3f*) normals ;
	 m_pFlags = pFlags;
      }
      else
      {
	 m_default = false ;
	 return false ;
      }
   }

   if ( m_default ) m_pNormals = m_pDefaultNormals ;

   return true ;
}

#endif	// !defined( _CHQVNORMALMAP_CPP )

// Local Variables: ***
// tab-width:4 ***
// End: ***

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -