📄 mlrtriangleclipping.hpp
字号:
for(k=j,l=0;k<j+3;k++,l++)
{
int indexK = index[k];
srcPolygon.coords[l] = (*transformedCoords)[indexK];
#ifdef I_SAY_YES_TO_COLOR
srcPolygon.colors[l] = (*actualColors)[indexK];
#endif // I_SAY_YES_TO_COLOR
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
srcPolygon.texCoords[2*l] = texCoords[indexK];
srcPolygon.texCoords[2*l+1] = texCoords[indexK + numVertices];
#else // I_SAY_YES_TO_DUAL_TEXTURES
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
for(m=0;m<currentNrOfPasses;m++)
{
srcPolygon.texCoords[currentNrOfPasses*l+m] = multiTexCoordsPointers[m][indexK];
}
#else // I_SAY_YES_TO_MULTI_TEXTURES
#ifdef I_SAY_YES_TO_TERRAIN
srcPolygon.texCoords[l] = (*clipTexCoords)[indexK];
#else // I_SAY_YES_TO_TERRAIN
srcPolygon.texCoords[l] = texCoords[indexK];
#endif // I_SAY_YES_TO_TERRAIN
#endif // I_SAY_YES_TO_MULTI_TEXTURES
#endif // I_SAY_YES_TO_DUAL_TEXTURES
srcPolygon.clipPerVertex[l] = (*clipPerVertex)[indexK];
}
srcPolygon.length = l;
//
//--------------------------------
// Point to the destination buffer
//--------------------------------
//
dstBuffer = 0;
dstPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
#ifdef I_SAY_YES_TO_COLOR
dstPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
#endif // I_SAY_YES_TO_COLOR
dstPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
dstPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();
dstPolygon.length = 0;
//
//-----------------------------------------------------------
// Spin through each plane that clipped the primitive and use
// it to actually clip the primitive
//-----------------------------------------------------------
//
mask = 1;
MLRClippingState theNewOr(0);
int loop = 4;
#if HUNT_CLIP_ERROR
for(k=0;k<srcPolygon.length;k++)
{
DEBUG_STREAM << setiosflags( ios::scientific) << setprecision(20)
<< srcPolygon.coords[k].x << " "
<< srcPolygon.coords[k].y << " "
<< srcPolygon.coords[k].z << " "
<< srcPolygon.coords[k].w << '\n';
}
DEBUG_STREAM << "TheOriginalOR: " << hex << theOr.GetClippingState() << dec << '\n';
#endif // HUNT_CLIP_ERROR
do
{
for(l=0; l<MLRClippingState::NextBit; l++)
{
if(theOr.IsClipped(mask))
{
//
//-----------------------------------
// Clip each vertex against the plane
//-----------------------------------
//
#if HUNT_CLIP_ERROR
DEBUG_STREAM << l << ": " << '\n';
#endif // HUNT_CLIP_ERROR
for(k=0;k<srcPolygon.length;k++)
{
k1 = (k+1) < srcPolygon.length ? k+1 : 0;
theTest = srcPolygon.clipPerVertex[k];
//
//----------------------------------------------------
// If this vertex is inside the viewing space, copy it
// directly to the clipping buffer
//----------------------------------------------------
//
if(theTest.IsClipped(mask) == 0)
{
firstIsIn = true;
dstPolygon.coords[dstPolygon.length] =
srcPolygon.coords[k];
#if HUNT_CLIP_ERROR
DEBUG_STREAM << k << " goes " << setiosflags( ios::scientific) << setprecision(20)
<< srcPolygon.coords[k].x << " "
<< srcPolygon.coords[k].y << " "
<< srcPolygon.coords[k].z << " "
<< srcPolygon.coords[k].w << '\n';
#endif // HUNT_CLIP_ERROR
dstPolygon.clipPerVertex[dstPolygon.length] =
srcPolygon.clipPerVertex[k];
#ifdef I_SAY_YES_TO_COLOR
dstPolygon.colors[dstPolygon.length] =
srcPolygon.colors[k];
#endif // I_SAY_YES_TO_COLOR
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
dstPolygon.texCoords[2*dstPolygon.length] =
srcPolygon.texCoords[2*k];
dstPolygon.texCoords[2*dstPolygon.length+1] =
srcPolygon.texCoords[2*k+1];
#else // I_SAY_YES_TO_DUAL_TEXTURES
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
for(m=0;m<currentNrOfPasses;m++)
{
dstPolygon.texCoords[currentNrOfPasses*dstPolygon.length+m] =
srcPolygon.texCoords[currentNrOfPasses*k+m];
}
#else // I_SAY_YES_TO_MULTI_TEXTURES
dstPolygon.texCoords[dstPolygon.length] =
srcPolygon.texCoords[k];
#endif // I_SAY_YES_TO_MULTI_TEXTURES
#endif // I_SAY_YES_TO_DUAL_TEXTURES
dstPolygon.length++;
//
//-------------------------------------------------------
// We don't need to clip this edge if the next vertex is
// also in the viewing space, so just move on to the next
// vertex
//-------------------------------------------------------
//
if(srcPolygon.clipPerVertex[k1].IsClipped(mask) == 0)
{
continue;
}
}
//
//---------------------------------------------------------
// This vertex is outside the viewing space, so if the next
// vertex is also outside the viewing space, no clipping is
// needed and we throw this vertex away. Since only one
// clipping plane is involved, it must be in the same space
// as the first vertex
//---------------------------------------------------------
//
else
{
firstIsIn = false;
if(srcPolygon.clipPerVertex[k1].IsClipped(mask) != 0)
{
Verify(
srcPolygon.clipPerVertex[k1].IsClipped(mask)
== srcPolygon.clipPerVertex[k].IsClipped(mask)
);
continue;
}
}
//
//-------------------------------------------
// Find the clipping interval from bc0 to bc1
//-------------------------------------------
//
if(firstIsIn == true)
{
a = GetLerpFactor (l, srcPolygon.coords[k], srcPolygon.coords[k1]);
Verify(a >= 0.0f && a <= 1.0f);
//
//------------------------------
// Lerp the homogeneous position
//------------------------------
//
dstPolygon.coords[dstPolygon.length].Lerp(
srcPolygon.coords[k],
srcPolygon.coords[k1],
a
);
#if HUNT_CLIP_ERROR
DEBUG_STREAM << "True " << a << " " << k << " " << k1 << " we get " << dstPolygon.length << '\n';
DEBUG_STREAM << setiosflags( ios::scientific) << setprecision(20)
<< dstPolygon.coords[dstPolygon.length].x << " "
<< dstPolygon.coords[dstPolygon.length].y << " "
<< dstPolygon.coords[dstPolygon.length].z << " "
<< dstPolygon.coords[dstPolygon.length].w << '\n';
#endif // HUNT_CLIP_ERROR
DoClipTrick(dstPolygon.coords[dstPolygon.length], l);
//
//----------------------------------------------------------
// If there are colors, lerp them in screen space for now as
// most cards do that anyway
//----------------------------------------------------------
//
#ifdef I_SAY_YES_TO_COLOR
#if COLOR_AS_DWORD
dstPolygon.colors[dstPolygon.length] = Color_DWORD_Lerp(
srcPolygon.colors[k],
srcPolygon.colors[k1],
a
);
#else // COLOR_AS_DWORD
dstPolygon.colors[dstPolygon.length].Lerp(
srcPolygon.colors[k],
srcPolygon.colors[k1],
a
);
#endif // COLOR_AS_DWORD
#endif // I_SAY_YES_TO_COLOR
//
//-----------------------------------------------------
// If there are texture uv's, we need to lerp them in a
// perspective correct manner
//-----------------------------------------------------
//
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
dstPolygon.texCoords[2*dstPolygon.length].Lerp
(
srcPolygon.texCoords[2*k],
srcPolygon.texCoords[2*k1],
a
);
dstPolygon.texCoords[2*dstPolygon.length+1].Lerp
(
srcPolygon.texCoords[2*k+1],
srcPolygon.texCoords[2*k1+1],
a
);
#else // I_SAY_YES_TO_DUAL_TEXTURES
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
for(m=0;m<currentNrOfPasses;m++)
{
dstPolygon.texCoords[currentNrOfPasses*dstPolygon.length+m].Lerp
(
srcPolygon.texCoords[currentNrOfPasses*k+m],
srcPolygon.texCoords[currentNrOfPasses*k1+m],
a
);
}
#else // I_SAY_YES_TO_MULTI_TEXTURES
dstPolygon.texCoords[dstPolygon.length].Lerp
(
srcPolygon.texCoords[k],
srcPolygon.texCoords[k1],
a
);
#endif // I_SAY_YES_TO_MULTI_TEXTURES
#endif // I_SAY_YES_TO_DUAL_TEXTURES
}
else
{
a = GetLerpFactor (l, srcPolygon.coords[k1], srcPolygon.coords[k]);
Verify(a >= 0.0f && a <= 1.0f);
//
//------------------------------
// Lerp the homogeneous position
//------------------------------
//
dstPolygon.coords[dstPolygon.length].Lerp(
srcPolygon.coords[k1],
srcPolygon.coords[k],
a
);
#if HUNT_CLIP_ERROR
DEBUG_STREAM << "False " << a << " " << k << " " << k1 << " we get " << dstPolygon.length << '\n';
DEBUG_STREAM << setiosflags( ios::scientific) << setprecision(20)
<< dstPolygon.coords[dstPolygon.length].x << " "
<< dstPolygon.coords[dstPolygon.length].y << " "
<< dstPolygon.coords[dstPolygon.length].z << " "
<< dstPolygon.coords[dstPolygon.length].w << '\n';
#endif // HUNT_CLIP_ERROR
DoClipTrick(dstPolygon.coords[dstPolygon.length], l);
//
//----------------------------------------------------------
// If there are colors, lerp them in screen space for now as
// most cards do that anyway
//----------------------------------------------------------
//
#ifdef I_SAY_YES_TO_COLOR
#if COLOR_AS_DWORD
dstPolygon.colors[dstPolygon.length] = Color_DWORD_Lerp(
srcPolygon.colors[k1],
srcPolygon.colors[k],
a
);
#else // COLOR_AS_DWORD
dstPolygon.colors[dstPolygon.length].Lerp(
srcPolygon.colors[k1],
srcPolygon.colors[k],
a
);
#endif // COLOR_AS_DWORD
#endif // I_SAY_YES_TO_COLOR
//
//-----------------------------------------------------
// If there are texture uv's, we need to lerp them in a
// perspective correct manner
//-----------------------------------------------------
//
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
dstPolygon.texCoords[2*dstPolygon.length].Lerp
(
srcPolygon.texCoords[2*k1],
srcPolygon.texCoords[2*k],
a
);
dstPolygon.texCoords[2*dstPolygon.length+1].Lerp
(
srcPolygon.texCoords[2*k1+1],
srcPolygon.texCoords[2*k+1],
a
);
#else // I_SAY_YES_TO_DUAL_TEXTURES
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
for(m=0;m<currentNrOfPasses;m++)
{
dstPolygon.texCoords[currentNrOfPasses*dstPolygon.length+m].Lerp
(
srcPolygon.texCoords[currentNrOfPasses*k1+m],
srcPolygon.texCoords[currentNrOfPasses*k+m],
a
);
}
#else // I_SAY_YES_TO_MULTI_TEXTURES
dstPolygon.texCoords[dstPolygon.length].Lerp
(
srcPolygon.texCoords[k1],
srcPolygon.texCoords[k],
a
);
#endif // I_SAY_YES_TO_MULTI_TEXTURES
#endif // I_SAY_YES_TO_DUAL_TEXTURES
}
//
//-------------------------------------
// We have to generate a new clip state
//-------------------------------------
//
dstPolygon.clipPerVertex[dstPolygon.length].Clip4dVertex(&dstPolygon.coords[dstPolygon.length]);
//
//----------------------------------
// Bump the new polygon vertex count
//----------------------------------
//
dstPolygon.length++;
}
//
//-----------------------------------------------
// Swap source and destination buffer pointers in
// preparation for the next plane test
//-----------------------------------------------
//
srcPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
#ifdef I_SAY_YES_TO_COLOR
srcPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
#endif // I_SAY_YES_TO_COLOR
srcPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
srcPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();
srcPolygon.length = dstPolygon.length;
dstBuffer = !dstBuffer;
dstPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
#ifdef I_SAY_YES_TO_COLOR
dstPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
#endif // I_SAY_YES_TO_COLOR
dstPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
dstPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();
dstPolygon.length = 0;
}
mask = mask << 1;
}
theNewOr = 0;
for(k=0;k<srcPolygon.length;k++)
{
theNewOr |= srcPolygon.clipPerVertex[k];
}
#if HUNT_CLIP_ERROR
DEBUG_STREAM << "TheOR: " << hex << theNewOr.GetClippingState() << dec << '\n';
#endif // HUNT_CLIP_ERROR
theOr = theNewOr;
} while (theNewOr != 0 && loop--);
//
//--------------------------------------------------
// could not clip this rare case, just ignore it
//--------------------------------------------------
//
if(theNewOr != 0)
{
testList[i] = 0;
continue;
}
//
//--------------------------------------------------
// Move the most recent polygon into the clip buffer
//--------------------------------------------------
//
#if HUNT_CLIP_ERROR
DEBUG_STREAM << "Final: " << srcPolygon.length << '\n';
#endif // HUNT_CLIP_ERROR
for(k=0;k<srcPolygon.length;k++)
{
int clipped_index = myNumberUsedClipVertex + k;
#if HUNT_CLIP_ERROR
DEBUG_STREAM << setiosflags( ios::scientific) << setprecision(20)
<< srcPolygon.coords[k].x << " "
<< srcPolygon.coords[k].y << " "
<< srcPolygon.coords[k].z << " "
<< srcPolygon.coords[k].w << '\n';
#endif // HUNT_CLIP_ERROR
(*clipExtraCoords)[clipped_index] = srcPolygon.coords[k];
#ifdef I_SAY_YES_TO_COLOR
(*clipExtraColors)[clipped_index] = srcPolygon.colors[k];
#endif // I_SAY_YES_TO_COLOR
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
(*clipExtraTexCoords)[clipped_index] = srcPolygon.texCoords[2*k];
(*clipExtraTexCoords2)[clipped_index] = srcPolygon.texCoords[2*k+1];
#else // I_SAY_YES_TO_DUAL_TEXTURES
#ifdef I_SAY_YES_TO_MULTI_TEXTURES
for(m=0;m<currentNrOfPasses;m++)
{
(*clipExtraMultiTexCoords)[m][clipped_index] = srcPolygon.texCoords[currentNrOfPasses*k+m];
}
#else // I_SAY_YES_TO_MULTI_TEXTURES
(*clipExtraTexCoords)[clipped_index] = srcPolygon.texCoords[k];
#endif // I_SAY_YES_TO_MULTI_TEXTURES
#endif // I_SAY_YES_TO_DUAL_TEXTURES
}
numberVerticesPerPolygon = srcPolygon.length;
#if HUNT_CLIP_ERROR
DEBUG_STREAM << "---" << '\n';
#endif // HUNT_CLIP_ERROR
(*clipExtraLength)[myNumberUsedClipLength] = numberVerticesPerPolygon;
#ifdef _ARMOR
(*clipExtraLength)[myNumberUsedClipLength] |= 0x8000;
#endif // _ARMOR
}
myNumberUsedClipVertex += numberVerticesPerPolygon;
myNumberUsedClipLength++;
ret++;
// clip
// dont draw the original
testList[i] = 0;
}
}
Check_Object(vt);
gos_vertices = vt->GetActualVertexPool(db);
numGOSVertices = 0;
gos_indices = vt->GetActualIndexPool(db);
numGOSIndices = 0;
k = visibleIndexedVertices.GetLength();
unsigned short strideIndex;
for(j=0,strideIndex=0;j<k;j++)
{
if(visibleIndexedVertices[j] == 0)
{
strideIndex++;
}
else
{
indexOffset[j] = static_cast<unsigned short>(j-strideIndex);
#ifdef I_SAY_YES_TO_DUAL_TEXTURES
if(MLRState::GetMultitextureLightMap() == true && state.GetMultiTextureMode()!=MLRState::MultiTextureOffMode)
{
GOSCopyData(
&gos_vertices2uv[numGOSVertices],
transformedCoords->GetData(),
#ifdef I_SAY_YES_TO_COLOR
actualColors->GetData(),
#endif // I_SAY_YES_TO_COLOR
#ifdef I_SAY_YES_TO_TERRAIN
clipTexCoords->GetData(),
#else // I_SAY_YES_TO_TERRAIN
texCoords.GetData(),
#endif // I_SAY_YES_TO_TERRAIN
texCoords.GetData() + numVertices,
j
#if FOG_HACK
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -