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

📄 cvshapes.cpp

📁 Windows上的MUD客户端程序
💻 CPP
📖 第 1 页 / 共 5 页
字号:
/*----------------------------------------------------------------------------
                        _                              _ _       
        /\             | |                            | (_)      
       /  \   _ __   __| |_ __ ___  _ __ ___   ___  __| |_  __ _ 
      / /\ \ | '_ \ / _` | '__/ _ \| '_ ` _ \ / _ \/ _` | |/ _` |
     / ____ \| | | | (_| | | | (_) | | | | | |  __/ (_| | | (_| |
    /_/    \_\_| |_|\__,_|_|  \___/|_| |_| |_|\___|\__,_|_|\__,_|

    The contents of this file are subject to the Andromedia Public
	License Version 1.0 (the "License"); you may not use this file
	except in compliance with the License. You may obtain a copy of
	the License at http://www.andromedia.com/APL/

    Software distributed under the License is distributed on an
	"AS IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
	implied. See the License for the specific language governing
	rights and limitations under the License.

    The Original Code is Pueblo client code, released November 4, 1998.

    The Initial Developer of the Original Code is Andromedia Incorporated.
	Portions created by Andromedia are Copyright (C) 1998 Andromedia
	Incorporated.  All Rights Reserved.

	Andromedia Incorporated                         415.365.6700
	818 Mission Street - 2nd Floor                  415.365.6701 fax
	San Francisco, CA 94103

    Contributor(s):
	--------------------------------------------------------------------------
	   Chaco team:  Dan Greening, Glenn Crocker, Jim Doubek,
	                Coyote Lussier, Pritham Shetty.

					Wrote and designed original codebase.

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

	Implementation for the ChQvInstance class for  Intel 3DR.

----------------------------------------------------------------------------*/

// $Header: /home/cvs/chaco/modules/client/msw/ChGraphx/CvShapes.cpp,v 2.50 1996/10/08 02:04:06 jimd Exp $

#include "grheader.h"

#include "CvInstnc.h"
#include <QvInfo.h>
#include <QvState.h>
#include "ChMaze.h"
#include "ChRenderData.h"
#include "ChMazDep.h"
#include "CvTrnsfm.h"
#include "CvNormal.h"
#include "CvBound.h"
#include "CvMaterl.h"
#include "CvTextur.h"
#include "GxSphere.h"
#include "ChSphere.h"
#include <ChTriangle.h>
#include "ChRenderBuffer.h"

#if defined(CH_IMMED_MODE)
#include "imode.h"
#endif

//#define FLIP_D3D_NORMALS	1

#define CH_SPHERE_LOD	1

// Binary transparency only for now
static bool IsTransparent(float transparency)
{	// 0 is opaque, 1 is totally transparent
	//return alpha <= 0.5;	
	return transparency >= 0.5;	
}

#if 0
inline int VertCount(long *values)
{
	int i = 0;
	while(values[i] != QV_END_FACE_INDEX) i++;
	return i;
}
#endif

inline int VertCount(long *values, int numPoints)
{
	int i = 0;
	while(i < numPoints && values[i] != QV_END_FACE_INDEX) i++;
	return i;
};

inline int VertCount(QvMFLong &coords, int start)
{
	return VertCount(coords.values + start, coords.num - start);
}

inline int GetFaceCount(long *values, int numPoints)
{
	int count = 0;
	for(int i = 0; i < numPoints ;i++)
	{
		if(values[i] == QV_END_FACE_INDEX || i == numPoints - 1) count++;
	}
	return count;
}


#if 0
bool ChQvInstance::ComputeBounds()
{
	// Every class needs to do their own. Maybe this should be pure virtual
	return false;
}

#endif

void ChQvInstanceHasProps::Init(ChQvBuildState *pState, ChQvInstance *pInstance)
{
	ChRenderContext *pRC = pState->GetView()->GetRenderContext();

	m_pMaterial =             (QvMaterial 			 *)(pRC->GetCurrentProperty(pState, QvState::MaterialIndex));   
	m_pMaterialBinding =      (QvMaterialBinding 	 *)(pRC->GetCurrentProperty(pState, QvState::MaterialBindingIndex));   
	m_pNormal =               (QvNormal 			 *)(pRC->GetCurrentProperty(pState, QvState::NormalIndex));   
	m_pNormalBinding =        (QvNormalBinding 	 *)(pRC->GetCurrentProperty(pState, QvState::NormalBindingIndex));   
	m_pCoordinate3 =          (QvCoordinate3 		 *)(pRC->GetCurrentProperty(pState, QvState::Coordinate3Index));   
	m_pTexture2 =             (QvTexture2 			 *)(pRC->GetCurrentProperty(pState, QvState::Texture2Index));   
	m_pTexture2Transform =    (QvTexture2Transform  *)(pRC->GetCurrentProperty(pState, QvState::Texture2TransformationIndex));   
	m_pTextureCoordinate2 =   (QvTextureCoordinate2 *)(pRC->GetCurrentProperty(pState, QvState::TextureCoordinate2Index));   
	m_pShapeHints =           (QvShapeHints 		 *)(pRC->GetCurrentProperty(pState, QvState::ShapeHintsIndex));   
	m_pInfo =           	  (QvInfo 				 *)(pRC->GetCurrentProperty(pState, QvState::InfoIndex));   
	m_pFontStyle =            (QvFontStyle 			 *)(pRC->GetCurrentProperty(pState, QvState::FontStyleIndex));   

	((ChQvRenderBaseData *)(m_pMaterial->GetRenderData()))->AddDependent(pInstance);             
	((ChQvRenderBaseData *)(m_pMaterialBinding->GetRenderData()))->AddDependent(pInstance);      
	((ChQvRenderBaseData *)(m_pNormal->GetRenderData()))->AddDependent(pInstance);               
	((ChQvRenderBaseData *)(m_pNormalBinding->GetRenderData()))->AddDependent(pInstance);        
	((ChQvRenderBaseData *)(m_pCoordinate3->GetRenderData()))->AddDependent(pInstance);          
	((ChQvRenderBaseData *)(m_pTexture2->GetRenderData()))->AddDependent(pInstance);             
	((ChQvRenderBaseData *)(m_pTexture2Transform->GetRenderData()))->AddDependent(pInstance);    
	((ChQvRenderBaseData *)(m_pTextureCoordinate2->GetRenderData()))->AddDependent(pInstance);   
	((ChQvRenderBaseData *)(m_pShapeHints->GetRenderData()))->AddDependent(pInstance);           
	((ChQvRenderBaseData *)(m_pInfo->GetRenderData()))->AddDependent(pInstance);           
	((ChQvRenderBaseData *)(m_pFontStyle->GetRenderData()))->AddDependent(pInstance);           

}

void ChQvInstanceHasProps::Term( ChQvInstance *pInstance)
{
	if(m_pMaterial) ((ChQvRenderBaseData *)(m_pMaterial->GetRenderData()))->RemoveDependent(pInstance);             
	if(m_pMaterialBinding) ((ChQvRenderBaseData *)(m_pMaterialBinding->GetRenderData()))->RemoveDependent(pInstance);      
	if(m_pNormal) ((ChQvRenderBaseData *)(m_pNormal->GetRenderData()))->RemoveDependent(pInstance);               
	if(m_pNormalBinding) ((ChQvRenderBaseData *)(m_pNormalBinding->GetRenderData()))->RemoveDependent(pInstance);        
	if(m_pCoordinate3) ((ChQvRenderBaseData *)(m_pCoordinate3->GetRenderData()))->RemoveDependent(pInstance);          
	if(m_pTexture2) ((ChQvRenderBaseData *)(m_pTexture2->GetRenderData()))->RemoveDependent(pInstance);             
	if(m_pTexture2Transform) ((ChQvRenderBaseData *)(m_pTexture2Transform->GetRenderData()))->RemoveDependent(pInstance);    
	if(m_pTextureCoordinate2) ((ChQvRenderBaseData *)(m_pTextureCoordinate2->GetRenderData()))->RemoveDependent(pInstance);   
	if(m_pShapeHints) ((ChQvRenderBaseData *)(m_pShapeHints->GetRenderData()))->RemoveDependent(pInstance);           
	if(m_pInfo) ((ChQvRenderBaseData *)(m_pInfo->GetRenderData()))->RemoveDependent(pInstance);           
	if(m_pFontStyle) ((ChQvRenderBaseData *)(m_pFontStyle->GetRenderData()))->RemoveDependent(pInstance);           
	m_pMaterial =             0;
	m_pMaterialBinding =      0;
	m_pNormal =               0;
	m_pNormalBinding =        0;
	m_pCoordinate3 =          0;
	m_pTexture2 =             0;
	m_pTexture2Transform =    0;
	m_pTextureCoordinate2 =   0;
	m_pShapeHints =           0;
	m_pInfo =           	  0;
	m_pFontStyle =            0;
}

void ChQvInstanceHasProps::RemoveProperty(ChQvRenderBaseData *pData)
{
	RemoveProperty(pData, (QvNode**)&m_pMaterial);
	RemoveProperty(pData, (QvNode**)&m_pMaterialBinding);
	RemoveProperty(pData, (QvNode**)&m_pNormal);
	RemoveProperty(pData, (QvNode**)&m_pNormalBinding);
	RemoveProperty(pData, (QvNode**)&m_pCoordinate3);
	RemoveProperty(pData, (QvNode**)&m_pTexture2);
	RemoveProperty(pData, (QvNode**)&m_pTexture2Transform);
	RemoveProperty(pData, (QvNode**)&m_pTextureCoordinate2);
	RemoveProperty(pData, (QvNode**)&m_pShapeHints);
	RemoveProperty(pData, (QvNode**)&m_pInfo);
	RemoveProperty(pData, (QvNode**)&m_pFontStyle);
}

void ChQvInstanceHasProps::RemoveProperty(ChQvRenderBaseData *pData, QvNode **ppNode)
{
	if(ppNode && *ppNode)
	{
		if(((ChQvRenderBaseData *)((*ppNode)->GetRenderData())) == pData) *ppNode = 0;          
	}
}

/////////// Shapes - first the shapeInstance abc

void ChQvShapeInstance::Init(ChQvBuildState *pState)
{
	ChQvInstance::Init(pState);
	ChQvInstanceHasProps::Init(pState, this);
	pState->AddShape();
}

ChQvShapeInstance::ChQvShapeInstance() : ChQvInstance()
#if (defined(CH_USE_RLAB) || defined(CH_USE_D3D))
	, m_mesh(0)
	, m_boolTransformDirty(false)
#if defined(CH_IMMED_MODE)
	, m_hTexture(0)
#endif
#endif
{
}

ChQvShapeInstance::~ChQvShapeInstance()
{
	ChQvInstanceHasProps::Term(this);
#if defined(CH_IMMED_MODE)
	if(m_hTexture)
	{
		ChNrObjectDestroy(m_hTexture);
	}
	m_hTexture = 0;
#endif
}

#if (defined(CH_USE_RLAB) || defined(CH_USE_D3D))

#if (defined(CH_USE_RLAB))
void ShapeTransformCallback(ChNrFrame frame, void * arg)
#else
void ShapeTransformCallback(ChNrFrame frame, void * arg, float delta)
#endif
{
	#if 1 || defined(CH_VRML_EVENTS)
	ChQvShapeInstance *pInst = (ChQvShapeInstance *)arg;
	if(pInst->IsTransformDirty())
	{
		ChNrFrameAddTransform(frame, CombineReplace, *(pInst->GetTransform().GetMatrix()));
		ChQvBounds bounds;
		pInst->GetBounds(bounds);	// ??????????? force the bounds transform to update

		pInst->SetTransformDirty( false );
	}
	#endif
}

void ChQvShapeInstance::CreateFrame()
{
	ChQvInstance::CreateFrame();
	// Install callback for event processing on transforms
	#if 1 || defined(CH_VRML_EVENTS)
	ChNrFrameAddCallback(m_frame, ShapeTransformCallback, this);
	#endif
};

#endif
ChQvInstance* ChQvShapeInstance::SetTransformDirty(bool boolDirty)
{
	m_boolTransformDirty = boolDirty;
	if(boolDirty && GetRC()) GetRC()->SetDirty();
	return this;
};

bool ChQvShapeInstance::SetupTexture(ChRenderContext *pRC)
{	
				// This is for procedural renderers like 3dr, OGL and D3d
	bool boolUseTexture = false;
	ChQvTextureRenderData *pTextureData = 0;
	pTextureData = (ChQvTextureRenderData*)(GetTexture2()->GetRenderData());
	if (pTextureData->GetTextureHandle()) boolUseTexture = true;
	 
	if(boolUseTexture)
	{
		pRC->SetTexture(pTextureData->GetTextureHandle());
	}
	else
	{
		pRC->SetTexture(0);
	}
	return boolUseTexture;
}

#if (defined(CH_USE_RLAB) || defined(CH_USE_D3D))
bool ChQvShapeInstance::SetTexture(ChQvTextureRenderData *pTextureData, ChNrMesh mesh /* = 0 */)
{
	if(!pTextureData) pTextureData = (ChQvTextureRenderData*)(GetTexture2()->GetRenderData());
	if(!mesh) mesh = m_mesh;

				// This is for retained mode renderers like RealityLab
	bool boolDidIt = false;


	if (mesh)
	{
		if(pTextureData)
		{
			ChTextureHandle texture = pTextureData->GetTextureHandle();
			if(texture)
			{
				ChNrMeshSetTexture(mesh, texture);
				boolDidIt = true;

				#if (defined(CH_USE_D3D))
				// D3D RM doesn't support alpha transparency for textures, 
				// so use chromaKey instead.
				if(pTextureData->IsChromaKey())
				{
					texture->SetDecalTransparency(true);
					texture->SetDecalTransparentColor(*(pTextureData->GetChromaKey().GetNative()));
				}
				
				// Bleach color out - sometimes D3D insists on modulating
				SetColor(ChColorA(1., 1., 1.), mesh);	 
				#endif
			}
		}
		else
		{
			// remove existing texture
			ChNrMeshSetTexture(mesh, 0);
		}
	}
	return boolDidIt;	// false says nothing happened
}

void ChQvShapeInstance::SetColor(const ChColorA &color, ChNrMesh mesh /* = 0 */)
{
#if (defined(CH_USE_D3D))
	if(!mesh) mesh = m_mesh;
	if(mesh)
	{
		mesh->SetColor(*(color.GetNative()));	 // set faces as asked
		mesh->SetColorSource(ColorFromFace);
	}
#endif
}

#endif

// Shapes


// arbitrary for now; later make it depend on image size
#define CalcCylinderFacetCount(cyl, pRC)	(50)
#define CalcConeFacetCount(cyl, pRC)	(50)


ChQvConeInstance::ChQvConeInstance() : ChQvShapeInstance()
{
}


bool ChQvConeInstance::Construct(ChRenderContext *pRC, ChConstructionIterator *pIterator)
{
#if (defined(CH_USE_RLAB) || defined(CH_USE_D3D))
	{
		QvCone *pNode = (QvCone *)GetNode();

		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;
		
		ChNrMesh mesh = m_mesh = pRC->CreateMesh();
		ChNrFrameAddVisual(GetFrame(), mesh);				   
		D3DRelease(mesh);

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

		ChNrMeshSetTextureTopology(mesh,true, false); // uwrap, vwrap 

		bool boolCull = (pNode->parts.value == QvCone::ALL);	// if all sides are present, then cull
		   //boolCull = true;
		int numFacets = CalcConeFacetCount(this, state);

		// Make a material mapping; we will use later for each facet, top and bottom
		ChQvConeMaterials materialMap( this );
		pRC->AdjustTransparency(materialMap);

		/* Do the texture stuff -  make the map, assign to all the verts, */
		ChQvConeTextures	*pTxMap =  new ChQvConeTextures( this, numFacets );
		ChQvTextureRenderData *pTextureData = (ChQvTextureRenderData*)(GetTexture2()->GetRenderData());
		bool boolUseTexture = !(pTextureData->IsEmpty());  // one -might- arrive

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

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

		// Build the display list: verts, normals, and faces
		// We replicate the apex vertex for each facet, since it has a different 
		// texture u for each facet.
		int numFaces = numFacets + 1, numVerts = numFacets * 2, numNorms = numFacets * 2 + 1;
		if (!boolCull) numNorms *= 2;
		int vertStep = 0;		// step to find verts for sides
		if (boolUseTexture)
		{
			vertStep = numVerts;
			numVerts += numFacets;
		}

		// alloc face_data conservatively; might be a touch too big in some cases
		int face_data_size = numFacets * 3 * 2 		// for side faces - triangles with normals
							+ numFacets 			// side counts
							+ 2 * numFacets 		// bottom
							+ 1 					// bottom counts		
							+ 1;					// terminator
		if (!boolCull) face_data_size *= 2;
		ChNrFaceData * face_data = new ChNrFaceData[face_data_size];

		int 	ifd = 0; // index into face data
		GxVec3f *n = new GxVec3f[numNorms];
		GxVec3f *v = new GxVec3f[numVerts];

		int normStep = numNorms / 2;
		float ynorm = (h2 > 1e-3) ? (r  / (h2 * 2)) : 0;
		for( int facet = 0; facet < numFacets; facet ++)
		{
			float theta = facet * twoPi / numFacets;
			float sintheta = sin(theta);
			float costheta = cos(theta);

			n[facet*2].x() = -sintheta;
			n[facet*2].y() = ynorm;	//  width / height, then mormalize
			n[facet*2].z() = -costheta;
	 

⌨️ 快捷键说明

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