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

📄 mlrprimitive.cpp

📁 机甲指挥官2源代码
💻 CPP
📖 第 1 页 / 共 2 页
字号:
//===========================================================================//
// Copyright (C) Microsoft Corporation. All rights reserved.                 //
//===========================================================================//

#include "MLRHeaders.hpp"

//#############################################################################
//###############################    ClipPolygon    ##################################
//#############################################################################

ClipPolygon::ClipPolygon()
{
	coords.SetLength(Limits::Max_Number_Vertices_Per_Polygon);
	colors.SetLength(Limits::Max_Number_Vertices_Per_Polygon);
	texCoords.SetLength(Limits::Max_Number_Vertices_Per_Polygon);
	clipPerVertex.SetLength(Limits::Max_Number_Vertices_Per_Polygon);
}

//#############################################################################
//###############################    MLRPrimitive    ##################################
//#############################################################################

DWORD Statistics::MLR_TransformedVertices = 0;
DWORD Statistics::MLR_LitVertices = 0;
DWORD Statistics::MLR_Primitives = 0;
DWORD Statistics::MLR_NonClippedVertices = 0;
DWORD Statistics::MLR_ClippedVertices = 0;
DWORD Statistics::MLR_PrimitiveKB = 0;
DWORD Statistics::MLR_PolysClippedButOutside = 0;
DWORD Statistics::MLR_PolysClippedButInside = 0;
DWORD Statistics::MLR_PolysClippedButOnePlane = 0;
DWORD Statistics::MLR_PolysClippedButGOnePlane = 0;
gos_CycleData Statistics::MLR_ClipTime;
DWORD Statistics::MLR_NumAllIndices = 1;
DWORD Statistics::MLR_NumAllVertices = 1;
float Statistics::MLR_Index_Over_Vertex_Ratio = 1.0f;



MLRPrimitive::ClassData*
	MLRPrimitive::DefaultData = NULL;

DynamicArrayOf<MLRClippingState>
	MLRPrimitive::clipPerVertex;
DynamicArrayOf<Vector4D>
	MLRPrimitive::clipExtraCoords;

#if COLOR_AS_DWORD
DynamicArrayOf<DWORD>
#else
DynamicArrayOf<RGBAColor>
#endif
	MLRPrimitive::clipExtraColors;

DynamicArrayOf<Vector2DScalar>
	MLRPrimitive::clipExtraTexCoords;

DynamicArrayOf<int>
	MLRPrimitive::clipExtraIndex;

DynamicArrayOf<unsigned short>
	MLRPrimitive::clipExtraLength;

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRPrimitive::InitializeClass()
{
	Verify(!DefaultData);
	DefaultData =
		new ClassData(
			MLRPrimitiveClassID,
			"MidLevelRenderer::MLRPrimitive",
			Plug::DefaultData,
			NULL
		);
	Register_Object(DefaultData);
	
	clipPerVertex.SetLength(Limits::Max_Number_Vertices_Per_Mesh);
	clipExtraCoords.SetLength(Limits::Max_Number_Vertices_Per_Mesh);
	clipExtraColors.SetLength(Limits::Max_Number_Vertices_Per_Mesh);
	clipExtraTexCoords.SetLength(Limits::Max_Number_Vertices_Per_Mesh);
	clipExtraIndex.SetLength(Limits::Max_Number_Vertices_Per_Mesh);
	clipExtraLength.SetLength(Limits::Max_Number_Primitives_Per_Frame);

}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRPrimitive::TerminateClass()
{
	clipPerVertex.SetLength(0);
	clipExtraCoords.SetLength(0);
	clipExtraColors.SetLength(0);
	clipExtraTexCoords.SetLength(0);
	clipExtraIndex.SetLength(0);
	clipExtraLength.SetLength(0);

	Unregister_Object(DefaultData);
	delete DefaultData;
	DefaultData = NULL;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLRPrimitive::MLRPrimitive(
	ClassData *class_data,
	MemoryStream *stream,
	int version
):
	Plug(class_data)
{
	Check_Pointer(this);
	Check_Object(stream);


	switch(version)
	{
		case 1:
		{
			actualColors = &colors;

			MemoryStreamIO_Read(stream, &coords);
			numVertices = coords.GetLength();

			transformedCoords.SetLength(numVertices);
			litColors.SetLength(numVertices);

			int i, len;
#if COLOR_AS_DWORD
			Stuff::DynamicArrayOf<Stuff::RGBAColor> rgbaColors;
			MemoryStreamIO_Read(stream, &rgbaColors);

			len = rgbaColors.GetLength();

			const Stuff::RGBAColor *data = rgbaColors.GetData();

			colors.SetLength(len);

			for(i=0;i<len;i++)
			{
				colors[i] = GOSCopyColor(data+i);
			}
#else
			MemoryStreamIO_Read(stream, &colors);
#endif
			MemoryStreamIO_Read(stream, &normals);
			MemoryStreamIO_Read(stream, &texCoords);

			*stream >> visible;

			Stuff::DynamicArrayOf<int>	tempLengths;

			MemoryStreamIO_Read(stream, &tempLengths);

			len = tempLengths.GetLength();
			lengths.SetLength(len);

			for(i=0;i<len;i++)
			{
				lengths[i] = (unsigned char)(tempLengths[i] & 0xff);
			}

			*stream >> visible;

			*stream >> drawMode;

			referenceState.Load(stream, version);
		}
		break;
		case 2:
		{
			actualColors = &colors;

			MemoryStreamIO_Read(stream, &coords);
			numVertices = coords.GetLength();

			transformedCoords.SetLength(numVertices);
			litColors.SetLength(numVertices);

#if COLOR_AS_DWORD
			MemoryStreamIO_Read(stream, &colors);
#else
			Stuff::DynamicArrayOf<DWORD> smallColors;

			MemoryStreamIO_Read(stream, &smallColors);
		
			int i, len = smallColors.GetLength();

			colors.SetLength(len);

			DWORD theColor;

			for(i=0;i<len;i++)
			{
				theColor = smallColors[i];

				colors[i].blue = (theColor & 0xff) * One_Over_256;

				theColor = theColor>>8;

				colors[i].green = (theColor & 0xff) * One_Over_256;

				theColor = theColor>>8;

				colors[i].red = (theColor & 0xff) * One_Over_256;

				theColor = theColor>>8;

				colors[i].alpha = (theColor & 0xff) * One_Over_256;
			}
#endif

			MemoryStreamIO_Read(stream, &normals);
			MemoryStreamIO_Read(stream, &texCoords);

			MemoryStreamIO_Read(stream, &lengths);

			*stream >> drawMode;

			referenceState.Load(stream, version);
		}
		break;
		default:
			STOP(("ERF-Version is newer than loader !"));
		break;
	}

	referenceCount = 1;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRPrimitive::Save(MemoryStream *stream)
{
	Check_Object(this);
	Check_Object(stream);

	*stream << GetClassID();
	MemoryStreamIO_Write(stream, &coords);

#if COLOR_AS_DWORD
	MemoryStreamIO_Write(stream, &colors);
#else
	Stuff::DynamicArrayOf<DWORD> smallColors;
	int i, len = colors.GetLength();

	const Stuff::RGBAColor *data = colors.GetData();

	smallColors.SetLength(len);

	for(i=0;i<len;i++)
	{
		smallColors[i] = GOSCopyColor(data+i);
	}

	MemoryStreamIO_Write(stream, &smallColors);
#endif

	MemoryStreamIO_Write(stream, &normals);
	MemoryStreamIO_Write(stream, &texCoords);

//	*stream << visible; // changed from version 1 to 2

	MemoryStreamIO_Write(stream, &lengths);

//	*stream << visible; // changed from version 1 to 2

	*stream << static_cast<int>(drawMode);

	referenceState.Save(stream);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLRPrimitive::MLRPrimitive(ClassData *class_data):
	Plug(class_data), lengths(0),
	colors(0), normals(0), texCoords(0), coords(0)
{
	referenceState = 0;
	
	state = 0;

	numVertices = 0;	// number of verts for stats and vert arrays

	actualColors = &colors;

	referenceCount = 1;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLRPrimitive::~MLRPrimitive()
{
	Verify(referenceCount==0);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRPrimitive::TestInstance() const
{
	Verify(IsDerivedFrom(DefaultData));
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRPrimitive::InitializeDraw()
{
#ifdef LAB_ONLY
	Statistics::MLR_NumAllIndices = 1;
	Statistics::MLR_NumAllVertices = 1;
#endif
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRPrimitive::InitializeDrawPrimitive(int vis, int)
{
	gos_vertices = NULL;
	numGOSVertices = -1;

	visible = vis;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRPrimitive::SetSubprimitiveLengths(unsigned char *data, int l)
{
	Check_Object(this); 
	lengths.AssignData(data, l);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
int
	MLRPrimitive::GetSubprimitiveLengths(unsigned char **data)
{
	Check_Object(this); 
	*data = lengths.GetData();
	return lengths.GetLength();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
int
	MLRPrimitive::GetSubprimitiveLength (int i) const
{ 
	Check_Object(this); 
	return (lengths.GetLength() > 0 ? abs(lengths[i]) : 1);
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRPrimitive::SetCoordData(
		const Point3D *data,
		int dataSize
	)
{
	Check_Object(this); 
	Check_Pointer(data);

	Verify(colors.GetLength() == 0 || dataSize == colors.GetLength());
	Verify(normals.GetLength() == 0 || dataSize == normals.GetLength());
	Verify(texCoords.GetLength() == 0 || dataSize == texCoords.GetLength());

#if defined (MAX_NUMBER_VERTICES)
	Verify(dataSize <= MAX_NUMBER_VERTICES);
#endif
	coords.AssignData(data, dataSize);
	transformedCoords.SetLength(dataSize);
	numVertices = dataSize;
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
	MLRPrimitive::GetCoordData(
		Point3D **data,
		int *dataSize
	)
{
	Check_Object(this);

	*data = coords.GetData();
	*dataSize = coords.GetLength();
}

//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void

⌨️ 快捷键说明

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