📄 mlrprimitiveclipping.hpp
字号:
//===========================================================================//
// Copyright (C) Microsoft Corporation. All rights reserved. //
//===========================================================================//
#if !defined(MLR_MLRCLIPTRICK_HPP)
#include <MLR\MLRClipTrick.hpp>
#endif
extern DWORD gEnableTextureSort, gShowClippedPolys;
extern unsigned short *indexOffset; // [MidLevelRenderer::Max_Number_Vertices_Per_Mesh]
#define HUNT_CLIP_ERROR 0
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
//
void
CLASSNAME::TransformNoClip(Matrix4D *mat, GOSVertexPool *vt,bool db)
{
Check_Object(this);
Check_Object(vt);
Start_Timer(Transform_Time);
Verify(index.GetLength() > 0);
unsigned short stride;
bool textureAnimation = false;
Scalar deltaU=0.0f, deltaV=0.0f;
if(state.GetTextureHandle())
{
MLRTexture *texture = (*MLRTexturePool::Instance)[state.GetTextureHandle()];
textureAnimation = texture->GetAnimateTexture();
if(textureAnimation)
{
Stuff::AffineMatrix4D &textureMatrix = texture->GetTextureMatrix();
deltaU = textureMatrix(3, 0);
deltaV = textureMatrix(3, 1);
}
}
int i, j, k, len = lengths.GetLength();
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
int tex2count = 0;
#endif
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
int m;
// gos_PushCurrentHeap(StaticHeap);
// static DynamicArrayOf<int> *tex2count = new DynamicArrayOf<int>(Limits::Max_Number_Of_Multitextures);
int tex2count[Limits::Max_Number_Of_Multitextures];
// SPEW(("micgaert", "Michael!!! The new() on line 42 can cause memory leaks!"));
// gos_PopCurrentHeap();
for(m=0;m<currentNrOfPasses;m++)
{
tex2count[m] = 0;
}
#endif
int numVertices = GetNumVertices();
gos_vertices = vt->GetActualVertexPool(db);
numGOSVertices = 0;
Verify(index.GetLength() > 0);
Verify(coords.GetLength() == visibleIndexedVertices.GetLength());
if(visibleIndexedVerticesKey == false)
{
FindVisibleVertices();
}
for(j=0,stride=0;j<numVertices;j++)
{
if(visibleIndexedVertices[j] == 0)
{
stride++;
}
else
{
#ifdef LAB_ONLY
TransformedVertices++;
#endif
if(db==false)
{
Verify (vt->GetLast() + numGOSVertices < Limits::Max_Number_Vertices_Per_Frame);
}
else
{
Verify (numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
}
indexOffset[j] = (unsigned short)(j - stride);
gos_vertices[numGOSVertices].GOSTransformNoClip(
coords[j],
*mat,
&texCoords[j][0]
#if FOG_HACK
, (state.GetFogMode() != 0)
#endif
);
// gos_vertices[numGOSVertices].u = texCoords[j][0];
// gos_vertices[numGOSVertices].v = texCoords[j][1];
#ifdef I_SAY_YES_TO_COLOR
#ifdef I_SAY_YES_TO_LIGHTING
#if COLOR_AS_DWORD
gos_vertices[numGOSVertices].argb = (*actualColors)[j];
#else
gos_vertices[numGOSVertices].argb = GOSCopyColor(&(*actualColors)[j]);
#endif
#else
#if COLOR_AS_DWORD
gos_vertices[numGOSVertices].argb = colors[j];
#else
gos_vertices[numGOSVertices].argb = GOSCopyColor(&colors[j]);
#endif
#endif
#else
gos_vertices[numGOSVertices].argb = 0xffffffff;
#endif
if(textureAnimation)
{
gos_vertices[numGOSVertices].u += deltaU;
gos_vertices[numGOSVertices].v += deltaV;
}
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
(*texCoords2)[tex2count++] = texCoords[numVertices + j];
#endif
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
for(m=1;m<currentNrOfPasses;m++)
{
(*extraMultiTexCoords)[m][tex2count[m]++] = multiTexCoordsPointers[m][j];
}
#endif
numGOSVertices++;
}
}
#ifdef LAB_ONLY
Set_Statistic(NonClippedVertices, NonClippedVertices+numGOSVertices);
#endif
Check_Object(vt);
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
Verify (tex2count == numGOSVertices);
if(db==false)
{
Verify (vt->GetLast() + 2*numGOSVertices < vt->GetLength());
}
else
{
Verify (2*numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
}
if(state2.GetTextureHandle())
{
MLRTexture *texture = (*MLRTexturePool::Instance)[state.GetTextureHandle()];
textureAnimation = texture->GetAnimateTexture();
if(textureAnimation)
{
Stuff::AffineMatrix4D &textureMatrix = texture->GetTextureMatrix();
deltaU = textureMatrix(3, 0);
deltaV = textureMatrix(3, 1);
}
}
memcpy(gos_vertices + numGOSVertices, gos_vertices, numGOSVertices * sizeof(GOSVertex));
if(textureAnimation)
{
for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
{
gos_vertices[j].u = (*texCoords2)[i][0] + deltaU;
gos_vertices[j].v = (*texCoords2)[i][1] + deltaV;
}
}
else
{
for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
{
gos_vertices[j].u = (*texCoords2)[i][0];
gos_vertices[j].v = (*texCoords2)[i][1];
}
}
if(db==false)
{
vt->Increase(2*numGOSVertices);
}
#else
#ifdef I_SAY_YES_TO_DETAIL_TEXTURES
if(db==false)
{
Verify (vt->GetLast() + 2*numGOSVertices < vt->GetLength());
}
else
{
Verify (2*numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
}
memcpy(gos_vertices + numGOSVertices, gos_vertices, numGOSVertices * sizeof(GOSVertex));
for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
{
gos_vertices[j].u = gos_vertices[i].u*xScale + xOffset;
gos_vertices[j].v = gos_vertices[i].v*yScale + yOffset;
}
if(db==false)
{
vt->Increase(2*numGOSVertices);
}
#else
if(db==false)
{
vt->Increase(numGOSVertices);
}
#endif
#endif
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
for(m=1;m<currentNrOfPasses;m++)
{
Verify (tex2count[m] == numGOSVertices);
if(db==false)
{
Verify (vt->GetLast() + numGOSVertices < vt->GetLength());
}
else
{
Verify (numGOSVertices < 2*Limits::Max_Number_Vertices_Per_Mesh);
}
memcpy(gos_vertices + numGOSVertices, gos_vertices, numGOSVertices * sizeof(GOSVertex));
for(i=0,j=numGOSVertices;i<numGOSVertices;i++,j++)
{
gos_vertices[j].u = (*extraMultiTexCoords)[m][i][0];
gos_vertices[j].v = (*extraMultiTexCoords)[m][i][1];
}
if(db==false)
{
vt->Increase(numGOSVertices);
}
}
#endif
gos_indices = vt->GetActualIndexPool(db);
numGOSIndices = 0;
int ngi = 0;
for(i=0,j=0;i<len;j+=stride,i++)
{
stride = lengths[i];
Verify(stride >= 3);
if(testList[i] == 0)
{
continue;
}
for(k=1;k<stride-1;k++)
{
if(db==false)
{
Verify((vt->GetLastIndex() + 3 + numGOSIndices) < vt->GetLength());
}
else
{
Verify(3 + numGOSIndices < 2*Limits::Max_Number_Vertices_Per_Mesh);
}
gos_indices[ngi++] = indexOffset[index[j]];
gos_indices[ngi++] = indexOffset[index[j+1+k]];
gos_indices[ngi++] = indexOffset[index[j+k]];
}
}
numGOSIndices = (unsigned short)ngi;
Check_Object(vt);
if(db==false)
{
vt->IncreaseIndex(numGOSIndices);
}
Stop_Timer(Transform_Time);
visible = numGOSVertices ? (unsigned char)1 : (unsigned char)0;
}
static MLRClippingState theAnd, theOr, theTest;
//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
// Now it gets serious
int
CLASSNAME::TransformAndClip(Matrix4D *mat, MLRClippingState clippingFlags, GOSVertexPool *vt, bool db)
{
Transform(mat);
Start_Timer(Clipping_Time);
Check_Object(this);
unsigned short l, stride;
int i, j, k, ret = 0;
int len = lengths.GetLength();
Verify(len == testList.GetLength());
Verify(clippingFlags.GetClippingState() != 0);
//
//--------------------------------------
// See if we don't have to draw anything
//--------------------------------------
//
if(len <= 0)
{
visible = 0;
Stop_Timer(Clipping_Time);
return visible;
}
int mask, end, k0, k1, ct=0;
Scalar a=0.0f;
// Scalar bc0=0.0f, bc1=0.0f;
bool textureAnimation = false;
Scalar deltaU=0.0f, deltaV=0.0f;
if(state.GetTextureHandle())
{
MLRTexture *texture = (*MLRTexturePool::Instance)[state.GetTextureHandle()];
textureAnimation = texture->GetAnimateTexture();
if(textureAnimation)
{
Stuff::AffineMatrix4D &textureMatrix = texture->GetTextureMatrix();
deltaU = textureMatrix(3, 0);
deltaV = textureMatrix(3, 1);
}
}
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
bool textureAnimation2 = false;
Scalar deltaU2=0.0f, deltaV2=0.0f;
if(state2.GetTextureHandle())
{
MLRTexture *texture = (*MLRTexturePool::Instance)[state.GetTextureHandle()];
textureAnimation2 = texture->GetAnimateTexture();
if(textureAnimation2)
{
Stuff::AffineMatrix4D &textureMatrix = texture->GetTextureMatrix();
deltaU2 = textureMatrix(3, 0);
deltaV2 = textureMatrix(3, 1);
}
}
#endif
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
int tex2count = 0;
int numVertices = GetNumVertices();
#endif
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
int m;
gos_PushCurrentHeap(StaticHeap);
static DynamicArrayOf<int> *tex2count = new DynamicArrayOf<int>(Limits::Max_Number_Of_Multitextures);
SPEW(("micgaert", "Michael!!! The new() on line 316 can cause memory leaks!"));
gos_PopCurrentHeap();
for(m=0;m<currentNrOfPasses;m++)
{
tex2count[m] = 0;
}
#endif
int myNumberUsedClipVertex, myNumberUsedClipIndex, myNumberUsedClipLength;
myNumberUsedClipVertex = 0;
myNumberUsedClipIndex = 0;
myNumberUsedClipLength = 0;
Verify(index.GetLength() > 0);
if(visibleIndexedVerticesKey == false)
{
FindVisibleVertices();
}
//
//------------------------
// Handle the indexed case
//------------------------
//
// initialize visibleIndexedVertices
memset(visibleIndexedVertices.GetData(), 0, visibleIndexedVertices.GetSize());
//
//-----------------------------------------------------------------
// Step through each polygon, making sure that we don't try to clip
// backfaced polygons
//-----------------------------------------------------------------
//
for(i=0,j=0;i<len;i++)
{
stride = lengths[i];
if(testList[i] == 0)
{
j += stride;
continue;
}
//
//---------------------------------------------------------------
// Test each vertex of the polygon against the allowed clipping
// planes, and accumulate status for which planes always clip and
// which planes clipped at least once
//---------------------------------------------------------------
//
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -