📄 mlr_i_pmesh.cpp
字号:
//===========================================================================//
// Copyright (C) Microsoft Corporation. All rights reserved. //
//===========================================================================//
#include "MLRHeaders.hpp"
#if defined(TRACE_ENABLED) && defined(MLR_TRACE)
BitTrace *MLR_I_PMesh_Clip;
#endif
#define UV_TEST 0
extern DWORD gEnableLightMaps;
//#############################################################################
//###### MLRIndexedPolyMesh with no color no lighting one texture layer ######
//#############################################################################
MLR_I_PMesh::ClassData*
MLR_I_PMesh::DefaultData = NULL;
DynamicArrayOf<Vector2DScalar> *lightMapUVs;
DynamicArrayOf<Scalar> *lightMapSqFalloffs;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::InitializeClass()
{
Verify(!DefaultData);
Verify(gos_GetCurrentHeap() == StaticHeap);
DefaultData =
new ClassData(
MLR_I_PMeshClassID,
"MidLevelRenderer::MLR_I_PMesh",
MLRIndexedPrimitiveBase::DefaultData,
(MLRPrimitiveBase::Factory)&Make
);
Register_Object(DefaultData);
#if defined(TRACE_ENABLED) && defined(MLR_TRACE)
MLR_I_PMesh_Clip = new BitTrace("MLR_I_PMesh_Clip");
Register_Object(MLR_I_PMesh_Clip);
#endif
lightMapUVs = new DynamicArrayOf<Vector2DScalar>(Limits::Max_Number_Vertices_Per_Polygon);
Register_Object(lightMapUVs);
lightMapSqFalloffs = new DynamicArrayOf<Scalar> (Limits::Max_Number_Vertices_Per_Polygon);
Register_Object(lightMapSqFalloffs);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::TerminateClass()
{
Unregister_Object(lightMapSqFalloffs);
delete lightMapSqFalloffs;
Unregister_Object(lightMapUVs);
delete lightMapUVs;
Unregister_Object(DefaultData);
delete DefaultData;
DefaultData = NULL;
#if defined(TRACE_ENABLED) && defined(MLR_TRACE)
Unregister_Object(MLR_I_PMesh_Clip);
delete MLR_I_PMesh_Clip;
#endif
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLR_I_PMesh::MLR_I_PMesh(
ClassData *class_data,
MemoryStream *stream,
int version
):
MLRIndexedPrimitiveBase(class_data, stream, version)
{
Check_Pointer(this);
Check_Pointer(stream);
Verify(gos_GetCurrentHeap() == Heap);
facePlanes.SetLength(GetNumPrimitives());
testList.SetLength(GetNumPrimitives());
FindFacePlanes();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLR_I_PMesh::MLR_I_PMesh(ClassData *class_data):
MLRIndexedPrimitiveBase(class_data)
{
Check_Pointer(this);
Verify(gos_GetCurrentHeap() == Heap);
drawMode = SortData::TriIndexedList;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
/*
void
MLR_I_PMesh::Copy(MLRIndexedPolyMesh *polyMesh)
{
Check_Pointer(this);
Verify(gos_GetCurrentHeap() == Heap);
int len;
unsigned short *_index;
Point3D *_coords;
Vector2DScalar *_texCoords;
unsigned char *_lengths;
polyMesh->GetCoordData(&_coords, &len);
SetCoordData(_coords, len);
polyMesh->GetIndexData(&_index, &len);
SetIndexData(_index, len);
polyMesh->GetSubprimitiveLengths(&_lengths);
SetSubprimitiveLengths(_lengths, polyMesh->GetNumPrimitives());
facePlanes.SetLength(GetNumPrimitives());
testList.SetLength(GetNumPrimitives());
FindFacePlanes();
polyMesh->GetTexCoordData(&_texCoords, &len);
SetTexCoordData(_texCoords, len);
referenceState = polyMesh->GetReferenceState();
visibleIndexedVerticesKey = false;
visibleIndexedVertices.SetLength(coords.GetLength());
}
*/
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLR_I_PMesh::~MLR_I_PMesh()
{
Check_Object(this);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
MLR_I_PMesh*
MLR_I_PMesh::Make(
MemoryStream *stream,
int version
)
{
Check_Object(stream);
gos_PushCurrentHeap(Heap);
MLR_I_PMesh *mesh = new MLR_I_PMesh(DefaultData, stream, version);
gos_PopCurrentHeap();
return mesh;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::Save(MemoryStream *stream)
{
Check_Object(this);
Check_Object(stream);
MLRIndexedPrimitiveBase::Save(stream);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::TestInstance() const
{
Verify(IsDerivedFrom(DefaultData));
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::InitializeDrawPrimitive(unsigned char vis, int parameter)
{
MLRIndexedPrimitiveBase::InitializeDrawPrimitive(vis, parameter);
if(parameter & 1)
{
ResetTestList();
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::SetSubprimitiveLengths (unsigned char *data, int numPrimitives)
{
Check_Object(this);
Verify(gos_GetCurrentHeap() == Heap);
lengths.AssignData(data, numPrimitives);
facePlanes.SetLength(numPrimitives);
testList.SetLength(numPrimitives);
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::GetSubprimitiveLengths (unsigned char **data, int *l)
{
Check_Object(this);
*l = lengths.GetLength();
*data = lengths.GetData();
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::FindFacePlanes()
{
Check_Object(this);
int i, j, stride, numPrimitives = GetNumPrimitives();
Vector3D v;
Verify(index.GetLength() > 0);
for(i=0,j=0;i<numPrimitives;i++)
{
stride = lengths[i];
facePlanes[i].BuildPlane(
coords[index[j]],
coords[index[j+1]],
coords[index[j+2]]
);
j += stride;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
int
MLR_I_PMesh::FindBackFace(const Point3D& u)
{
Check_Object(this);
int i, numPrimitives = GetNumPrimitives();
int ret = 0, len = lengths.GetLength();
unsigned char *iPtr;
Plane *p;
if(len <= 0)
{
visible = 0;
return 0;
}
p = &facePlanes[0];
iPtr = &testList[0];
if(state.GetBackFaceMode() == MLRState::BackFaceOffMode)
{
ResetTestList();
ret = 1;
}
else
{
for(i=0;i<numPrimitives;i++,p++,iPtr++)
{
*iPtr = (p->GetDistanceTo(u) >= 0.0f) ? (unsigned char)0xff : (unsigned char)0;
ret += *iPtr;
}
visible = ret ? (unsigned char)1 : (unsigned char)0;
}
visible = ret ? (unsigned char)1 : (unsigned char)0;
FindVisibleVertices();
return ret;
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::ResetTestList()
{
int i, numPrimitives = GetNumPrimitives();
unsigned char *iPtr = &testList[0];
for(i=0;i<numPrimitives;i++,iPtr++)
{
*iPtr = 1;
}
}
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
int
MLR_I_PMesh::FindVisibleVertices()
{
Check_Object(this);
Verify(index.GetLength() > 0);
int ret, i, j, k, stride, len = lengths.GetLength();
Verify(index.GetLength() > 0);
for(i=0,j=0,ret=0;i<len;i++)
{
stride = lengths[i];
Verify(stride >= 3);
if(testList[i] == 0)
{
j += stride;
continue;
}
for(k=j;k<j+stride;k++)
{
visibleIndexedVertices[index[k]] = 1;
ret++;
}
j += stride;
}
visibleIndexedVerticesKey = true;
return ret;
}
extern DWORD gEnableTextureSort, gEnableAlphaSort;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
MLR_I_PMesh::Transform(Matrix4D *mat)
{
Check_Object(this);
Start_Timer(Transform_Time);
Verify(index.GetLength() > 0);
int i, len = coords.GetLength();
if(visibleIndexedVerticesKey == false)
{
FindVisibleVertices();
}
Stuff::Vector4D *v4d = transformedCoords->GetData();
Stuff::Point3D *p3d = coords.GetData();
MLRClippingState *cs = clipPerVertex->GetData();
unsigned char *viv = visibleIndexedVertices.GetData();
for(i=0;i<len;i++,p3d++,v4d++,cs++,viv++)
{
if(*viv == 0)
{
continue;
}
v4d->Multiply(*p3d, *mat);
#ifdef LAB_ONLY
Set_Statistic(TransformedVertices, TransformedVertices+1);
#endif
cs->Clip4dVertex(v4d);
//
//--------------------------------------------------------
// I claims all vertices are in. lets check it. who knows
//--------------------------------------------------------
//
#ifdef LAB_ONLY
if( (*cs)==0)
{
#if defined(_ARMOR)
if(ArmorLevel > 3)
{
//
//--------------------------------------------------------
// I claims all vertices are in. lets check it. who knows
//--------------------------------------------------------
//
Verify(v4d->x >= 0.0f && v4d->x <= v4d->w );
Verify(v4d->y >= 0.0f && v4d->y <= v4d->w );
Verify(v4d->z >= 0.0f && v4d->z <= v4d->w );
}
#endif
Set_Statistic(NonClippedVertices, NonClippedVertices+1);
}
else
{
Set_Statistic(ClippedVertices, ClippedVertices+1);
}
#endif
}
Stop_Timer(Transform_Time);
}
#undef I_SAY_YES_TO_DUAL_TEXTURES
#undef I_SAY_YES_TO_COLOR
#undef I_SAY_YES_TO_LIGHTING
#define CLASSNAME MLR_I_PMesh
#if defined(TRACE_ENABLED) && defined(MLR_TRACE)
#define SET_MLR_PMESH_CLIP() MLR_I_PMesh_Clip->Set()
#define CLEAR_MLR_PMESH_CLIP() MLR_I_PMesh_Clip->Clear()
#else
#define SET_MLR_PMESH_CLIP()
#define CLEAR_MLR_PMESH_CLIP()
#endif
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// This include contains follwing functions:
// void MLR_I_PMesh::TransformNoClip(Matrix4D*, GOSVertexPool*);
// int MLR_I_PMesh::Clip(MLRClippingState, GOSVertexPool*);
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#include <MLR\MLRPrimitiveClipping.hpp>
#undef CLASSNAME
//---------------------------------------------------------------------------
//
void
MLR_I_PMesh::Lighting(MLRLight* const* lights, int nrLights)
{
int i;
MLRLightMap *lightMap;
for(i=0;i<nrLights;i++)
{
lightMap = lights[i]->GetLightMap();
if(lightMap!=NULL)
{
LightMapLighting(lights[i]);
}
}
}
RGBAColor errorColor;
//---------------------------------------------------------------------------
//
bool
CheckForBigTriangles(DynamicArrayOf<Vector2DScalar> *lightMapUVs, int stride)
{
int k1, k0;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -