cvshapes.cpp

来自「Windows上的MUD客户端程序」· C++ 代码 · 共 2,206 行 · 第 1/5 页

CPP
2,206
字号
			v[facet].x() = -sintheta * r;
			v[facet].y() = -h2;
			v[facet].z() = -costheta * r;

			v[facet + numFacets].set(0,h2,0);  // top vertex

			n[facet*2].normalize();
			//n[facet*2].z() = -n[facet*2].z();
			if(!boolCull)
			{
				n[facet*2+normStep] = -n[facet*2];	// inside normal
			}

			theta = (facet +.5) * twoPi / numFacets;	// halfway to next point for top vert
			n[facet*2+1].x() = -sin(theta);
			n[facet*2+1].y() = ynorm;	// width / height, then mormalize
			n[facet*2+1].z() = -cos(theta);
			n[facet*2+1].normalize();
			if(!boolCull)
			{
				n[facet*2+1+normStep] = -n[facet*2+1];	// point's inside normal
			}
		}
		n[numFacets*2].set(0,-1,0);  // bottom's normal
		if(!boolCull)
		{
			n[numFacets*2+normStep] = -n[numFacets*2];	// bottom's inside normal
		}
		// Dupe the bottom verts, to use for sides, if we have texture
		if(boolUseTexture)
		{
			for( int i = 0; i < numFacets; i ++)
			{
				v[i+vertStep] = v[i];
			}
		}
		

		// Do the bottom
		if(pNode->parts.value & QvCone::BOTTOM)
		{
		 	 // TODO : Make sure this order is right when we do textures
			face_data[ifd++] = numFacets;	// count
			for( int j = 0; j < numFacets; j ++)
			{
					// Use the vertices, in mangled order

				int index = ( j % (numFacets));
				face_data[ifd++] = index;			// vertex
				face_data[ifd++] = numFacets * 2;		// normal
			}
			if(!boolCull)
			{
				face_data[ifd++] = numFacets;	// count
				for( int j = 0; j < numFacets; j ++)
				{
					// Use the vertices, in even more mangled order

					int index = (numFacets - j) % (numFacets);
					face_data[ifd++] = index;			// vertex
					face_data[ifd++] = numFacets * 2 + normStep;		// normal
				}
			}
		}

		// Do the sides
		if(pNode->parts.value & QvCone::SIDES)
		{
			for( int facet = 0; facet < numFacets; facet ++)
			{
				face_data[ifd++] = 3;	// face vertex count

				face_data[ifd++] = facet + vertStep;							  // v        
				face_data[ifd++] = facet * 2;						  // n, etc.  
				face_data[ifd++] = facet + numFacets;				  // top vertex
				face_data[ifd++] = facet * 2 + 1;
				face_data[ifd++] = ((facet + 1) % numFacets)  + vertStep;
				face_data[ifd++] = (facet * 2 + 2) % (2 * numFacets);
			}
			if(!boolCull)
			{
				for( int facet = 0; facet < numFacets; facet ++)
				{
					face_data[ifd++] = 3;	// face vertex count
											// change the order of pt[1] and [2]

	 				face_data[ifd++] = facet + vertStep;							// v
					face_data[ifd++] = facet * 2 + normStep;			// n, etc.
					face_data[ifd++] = ((facet + 1) % numFacets) + vertStep;
					face_data[ifd++] = (facet * 2 + 2) % (2 * numFacets) + normStep;
					face_data[ifd++] = facet + numFacets;				// top vertex
					face_data[ifd++] = facet * 2 + 1 + normStep;
				}
			}
		}

		face_data[ifd] = 0;	// terminate array

		// Load the verts and faces
		ChNrFaceArray facesAdded;
		int numFacesAdded;
		ChNrMeshAddFaces(mesh,
				       numVerts,
				       (ChNrVector*) v,
				       numNorms,
				       (ChNrVector*) n,
				       face_data,
				       &numFacesAdded,
				       &facesAdded);
		
		// Assign colors to faces
		int iface = 0;

		#if defined(CH_USE_D3D)
		numFacesAdded = facesAdded->GetSize();
		#endif

		ChNrFace elt;	// scratch space
		// Do the bottom
		if(pNode->parts.value & QvCone::BOTTOM)
		{
			int count = 1;	// count
			if(!boolCull) count *= 2;
			for( int j = 0; j < count; j ++)
			{
				GetElement(facesAdded, unsigned(iface), elt);
				if(boolUseTexture)
				{ 
					SetTextureCoordinates(pTxMap, mesh, elt, Bottom); 
				}
				materialMap.Set(mesh, elt, ChQvConeMaterials::Bottom);
				iface++;
				D3DRelease(elt);
			}
		}

		// Do the sides
		if(pNode->parts.value & QvCone::SIDES)
		{
			int count = numFacets;	// count
			if(!boolCull) count *= 2;
			for( int j = 0; j < count; j ++)
			{
				GetElement(facesAdded, unsigned(iface), elt);
				if(boolUseTexture && j < numFacets)
				{ 
					SetTextureCoordinates(pTxMap, mesh, elt, Sides, j); 
				}
				materialMap.Set(mesh, elt, ChQvConeMaterials::Sides);
				iface++;
				D3DRelease(elt);
			}
		}

		if(boolUseTexture)
		{
			ChTextureHandle hdl = pTextureData->GetTextureHandle();
			if(hdl)
			{
				// Texture guy got here first, apply it
				//ChNrMeshSetTexture(mesh, hdl);
				SetTexture(pTextureData);

			}
		}  
	
													  // Cleanup time
 		ChNrFree(facesAdded);							  
		delete [] v;
		delete [] n;
		delete [] face_data;
		delete [] pTxMap;

		pIterator->DidAShape();
		pRC->UnlockScene();		
		pRC->UnlockQv();		   // Unlock tree 
	}
#endif
	return true;
}


bool ChQvConeInstance::Draw(ChRenderContext *pRC, ChDrawIterator *pIterator)
{
#if defined( CH_USE_3DR )
	long On = 1;
	long Off = 0;

	G3dHandle_t hGC = pRC->GetGC();
	Dword_t  hRC = pRC->GetRC();
	QvCone *pNode = (QvCone *)GetNode();
	pRC->SetModelTransform(GetTransform());

	pRC->SetShading(this);


	Float_t		cull = 0;
	Fixed32_t wMattNum = 0;	  // always use material 0

	if(pNode->parts.value == QvCone::ALL) cull = 1;	// if all sides are present, then cull
	G3dSetMatt( hGC,  wMattNum, G3DM_BACK_CULL,	(Float_t *)&cull );
	
	int numFacets = CalcConeFacetCount(this, pRC);

	// Make a material mapping; we will use later for each facet and bottom
	ChQvConeMaterials materialMap( this);

	/* Do the texture stuff - find it, make the map, set up 3dr for it, */
	int boolUseTexture = false;
	
	ChQvTextureRenderData *pTextureData = 0;
	pTextureData = (ChQvTextureRenderData*)(GetTexture2()->GetRenderData());
	if (pTextureData->GetTextureHandle()) boolUseTexture = true;
	
	ChQvConeTextures	*pTxMap = 0;
	 
	if(boolUseTexture)
	{
		pRC->SetTexture(pTextureData->GetTextureHandle());
		pTxMap = new ChQvConeTextures( this, numFacets );
	}

	float h2 = pNode->height.value / 2.;
	float r = pNode->bottomRadius.value;

	Fixed32_t lCCW;
	G3dGetState( hGC, G3DL_FRONT_CCW, &lCCW); // save CCW state
	G3dSetState( hGC, G3DL_FRONT_CCW, &On);

	
	bool boolPureEmissive;
	float pi = atan(1.) * 4.;
	float twoPi = pi * 2;

	// Do the sides
	if(pNode->parts.value & QvCone::SIDES)
	{
		materialMap.Set(hRC, hGC, 0, &boolPureEmissive);
		pRC->SetModulation( boolUseTexture, boolPureEmissive);

		G3dBeginPrim(hGC, G3D_PRM_QUADSTRIP, numFacets * 2 + 2);
		for( int facet = 0; facet <= numFacets; facet ++)
		{
			PointF_t n, v1, v2;
			float theta = facet * twoPi / numFacets;

			// get better norm from RLab Code TODO!!!
			n.x = -sin(theta);
			n.y = 0;
			n.z = -cos(theta);
		 
			v1.x = 0;
			v1.y = h2;
			v1.z = 0;

			v2.x = n.x * r;
			v2.y = -h2;
			v2.z = n.z * r;

			if(boolUseTexture)
			{
				PointF_t textCoord = pTxMap->GetCoord3(0, facet, 0);
				G3dAddPrimVtxF(hGC, &v1, &n, &textCoord, 0);

				textCoord = pTxMap->GetCoord3(0, facet, 1);
				G3dAddPrimVtxF(hGC, &v2, &n, &textCoord, 0);
			}
			else
			{
				G3dAddPrimVtxF(hGC, &v1, &n, 0, 0);
				G3dAddPrimVtxF(hGC, &v2, &n, 0, 0);
			}
		}
		try
		{
			G3dEndPrim(hGC);
		}
		catch(...)
		{
			//TRACE("Exception in drawing cylinder caught\r\n");
		}
	}


	// Do the bottom
	G3dSetState( hGC, G3DL_FRONT_CCW, &Off);
	if(pNode->parts.value & QvCone::BOTTOM)
	{
		materialMap.Set(hRC, hGC, 1 /*bottom*/, &boolPureEmissive);
		pRC->SetModulation( boolUseTexture, boolPureEmissive);

		G3dBeginPrim(hGC, G3D_PRM_POLYGON, numFacets);
		PointF_t n, v1;
		n.x = 0;
		n.y = -1;
		n.z = 0;
		for( int j = 0; j <= numFacets; j ++)
		{
			float theta = j * twoPi / numFacets;
			v1.x = sin(theta + pi) * r;
			v1.y = -h2;
			v1.z = cos(theta + pi) * r;
		 
			if(boolUseTexture)
			{
				PointF_t textCoord = pTxMap->GetCoord3(1, 0, j);
				G3dAddPrimVtxF(hGC, &v1, &n, &textCoord, 0);
			}
			else
			{
				G3dAddPrimVtxF(hGC, &v1, &n, 0, 0);
			}
		}
		G3dEndPrim(hGC);
	}

	G3dSetState( hGC, G3DL_FRONT_CCW, &lCCW);
	if(boolUseTexture)
	{
		pRC->SetTexture(0);							// turn off texture
		delete pTxMap;
	}
#elif (defined(CH_USE_RLAB) || defined(CH_USE_D3D))
#endif //defined( CH_USE_3DR )
	return true;
}


#if (defined(CH_USE_RLAB) || defined(CH_USE_D3D))
void ChQvConeInstance::SetTextureCoordinates(ChQvConeTextures *pTxMap, ChNrMesh mesh, ChNrFace face, Parts part, int facet)
{

	int count = ChNrFaceGetVertexCount(face);
	for(int j = 0; j < count; j++)
	{
		GxVec3f textCoord = pTxMap->GetCoord3(part, facet, j);
		int index = ChNrFaceGetVertexIndex(face, j);
		ChNrMeshSetTextureCoordinates(mesh, index, textCoord.x(), textCoord.y());
	}
}
#endif


ChQvCubeInstance::ChQvCubeInstance() : ChQvShapeInstance()
{
}

bool ChQvCubeInstance::Construct(ChRenderContext *pRC, ChConstructionIterator *pIterator)
{
#if (defined(CH_USE_RLAB) || defined(CH_USE_D3D))
	{
		ASSERT(m_frame );
		pRC->LockQv();		   // Lock tree so other threads don't kill our data
		if(!pIterator->IsOKToProceed(this))	 // This locks scene if available
		{
			pRC->UnlockQv();		   // Unlock tree 
			return 0;
		}
		if(IsConstructed())
		{
			pRC->UnlockScene();
			pRC->UnlockQv();		   // Unlock tree 
			return 1;
		}
		m_boolConstructed = true;
		
		static long indices[6][4] =
		{
			{0, 1, 5, 4},	// front
			{2, 3, 7, 6},	// back
			{3, 0, 4, 7},	// left
			{1, 2, 6, 5},	// right
			{3, 2, 1, 0},	// top
			{4, 5, 6, 7}	// bottom
		};
		static  GxVec3f fSigns[8] =
		{
			GxVec3f(-1, 1, 1),	// top front left
			GxVec3f( 1, 1, 1),	// top right
			GxVec3f( 1, 1, -1),	// top right back
			GxVec3f(-1, 1, -1),	// top left back
			GxVec3f(-1, -1, 1),	// bottom left
			GxVec3f( 1, -1, 1),	// bottom right
			GxVec3f( 1, -1, -1),	// bottom right back
			GxVec3f(-1, -1, -1)	// bottom left back
		};
		#if 0
		static  GxVec3f fNorms[6] =		
		{									// indexed by face
			GxVec3f( 0, 0, 1),// 
			GxVec3f( 0, 0, -1),	// 
			GxVec3f(-1, 0, 0),	// 
			GxVec3f( 1, 0, 0),	// 
			GxVec3f( 0, 1, 0),	// 
			GxVec3f( 0, -1, 0)	// bottom 
		};
		#else
		static  GxVec3f fNorms[6] =		
		{									// indexed by face
			GxVec3f( 0, 0, -1),// 
			GxVec3f( 0, 0, 1),	// 
			GxVec3f(1, 0, 0),	// 
			GxVec3f( -1, 0, 0),	// 
			GxVec3f( 0, -1, 0),	// 
			GxVec3f( 0, 1, 0)	// bottom 
		};
		#endif
		QvCube *pNode = (QvCube *)GetNode();

		// Make a material mapping; we will use later for each face
		ChQvCubeMaterials materialMap( this );
		pRC->AdjustTransparency(materialMap);

		/* Do the texture stuff - find it, make the map, set up 3dr for it, */
		ChQvCubeTextures	*pTxMap =  new ChQvCubeTextures( this );
		ChQvTextureRenderData *pTextureData = (ChQvTextureRenderData*)(GetTexture2()->GetRenderData());
		bool boolUseTexture = !(pTextureData->IsEmpty());  // one -might- arrive


		float h2 = pNode->height.value / 2.;
		float w2 = pNode->width.value / 2.;
		float d2 = pNode->depth.value / 2.;

		ChNrMesh mesh = m_mesh = pRC->CreateMesh();
		ChNrFrameAddVisual(GetFrame(), mesh);				   
		D3DRelease(mesh);

		ChNrObjectSetAppData(mesh, (unsigned long)this);
		ChNrMeshSetColorSource(mesh,ColorFromFace); // alternative: ChNativeColorFromVertex 
		ChNrMeshSetPerspective(mesh,true);  

		// Need to reserve space for vertices, normals, and faces - 
		int numFaces = 6, numVerts = 8, numNorms = 6;
		int vertStep = 0;		// step to find verts for sides
		if (boolUseTexture)
		{
			vertStep = numVerts;
			numVerts *= 3;
		}

		ChNrMeshReserveSpace(mesh, numVerts, numNorms, numFaces);  
		ChNrFaceData * face_data = new ChNrFaceData[48 + 6 + 1];

⌨️ 快捷键说明

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