123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014 |
- //===========================================================================//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- #include "MLRHeaders.hpp"
- #if !defined(MLR_MLRCLIPTRICK_HPP)
- #include <MLR\MLRClipTrick.hpp>
- #endif
- #if !defined(MLR_MLRINDEXEDTRIANGLECLOUD_HPP)
- #include <MLR\MLRIndexedTriangleCloud.hpp>
- #endif
- extern DWORD gShowClippedPolys;
- extern unsigned short *indexOffset; // [MidLevelRenderer::Max_Number_Vertices_Per_Mesh]
- //#############################################################################
- //#################### MLRIndexedTriangleCloud ##########################
- //#############################################################################
- DynamicArrayOf<unsigned short>
- *MLRIndexedTriangleCloud::clipExtraIndex;
- DynamicArrayOf<Vector2DScalar>
- *MLRIndexedTriangleCloud::clipExtraTexCoords;
- DynamicArrayOf<unsigned char>
- *MLRIndexedTriangleCloud::visibleIndexedVertices;
- MLRIndexedTriangleCloud::ClassData*
- MLRIndexedTriangleCloud::DefaultData = NULL;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRIndexedTriangleCloud::InitializeClass()
- {
- Verify(!DefaultData);
- Verify(gos_GetCurrentHeap() == StaticHeap);
- DefaultData =
- new ClassData(
- MLRIndexedTriangleCloudClassID,
- "MidLevelRenderer::MLRIndexedTriangleCloud",
- MLRTriangleCloud::DefaultData
- );
- Register_Object(DefaultData);
-
- clipExtraIndex = new DynamicArrayOf<unsigned short> (Limits::Max_Number_Vertices_Per_Mesh);
- Register_Pointer(clipExtraIndex);
- clipExtraTexCoords = new DynamicArrayOf<Vector2DScalar> (Limits::Max_Number_Vertices_Per_Mesh);
- Register_Pointer(clipExtraTexCoords);
- visibleIndexedVertices = new DynamicArrayOf<unsigned char> (Limits::Max_Number_Vertices_Per_Mesh);
- Register_Pointer(visibleIndexedVertices);
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRIndexedTriangleCloud::TerminateClass()
- {
- Unregister_Pointer(clipExtraIndex);
- delete clipExtraIndex;
- Unregister_Pointer(clipExtraTexCoords);
- delete clipExtraTexCoords;
- Unregister_Pointer(visibleIndexedVertices);
- delete visibleIndexedVertices;
- Unregister_Object(DefaultData);
- delete DefaultData;
- DefaultData = NULL;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- MLRIndexedTriangleCloud::MLRIndexedTriangleCloud(int nr) :
- MLRTriangleCloud(nr)
- {
- Verify(gos_GetCurrentHeap() == Heap);
- usedNrOfPoints = NULL;
- Check_Pointer(this);
-
- drawMode = SortData::TriIndexedList;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- MLRIndexedTriangleCloud::~MLRIndexedTriangleCloud()
- {
- Check_Object(this);
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRIndexedTriangleCloud::SetData
- (
- const int *tri_count,
- const int *point_count,
- const unsigned short *index_data,
- const Stuff::Point3D *point_data,
- const Stuff::RGBAColor *color_data,
- const Vector2DScalar *uv_data
- )
- {
- Check_Pointer(this);
- usedNrOfTriangles = tri_count;
- usedNrOfPoints = point_count;
- Verify(*usedNrOfTriangles <= maxNrOf);
- index = index_data;
- points = point_data;
- colors = color_data;
- texCoords = uv_data;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRIndexedTriangleCloud::Draw (DrawEffectInformation *dInfo, GOSVertexPool *allVerticesToDraw, MLRSorter *sorter)
- {
- Check_Object(this);
- worldToEffect.Invert(*dInfo->effectToWorld);
- Vector4D *v4 = transformedCoords->GetData();
- for(int k=0;k<*usedNrOfPoints;k++, v4++)
- {
- v4->Multiply(points[k], effectToClipMatrix);
- (*clipPerVertex)[k].Clip4dVertex(v4);
- }
- #if 0
- Lighting(*shape->worldToShape, dInfo->activeLights, dInfo->nrOfActiveLights);
- #endif
- if( Clip(dInfo->clippingFlags, allVerticesToDraw) )
- {
- sorter->AddEffect(this, dInfo->state);
- }
- }
- static MLRClippingState theAnd, theOr, theTest;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- int
- MLRIndexedTriangleCloud::Clip(MLRClippingState clippingFlags, GOSVertexPool *vt)
- {
- int myNumberUsedClipVertex, myNumberUsedClipIndex, myNumberUsedClipLength;
- myNumberUsedClipVertex = 0;
- myNumberUsedClipIndex = 0;
- myNumberUsedClipLength = 0;
- Verify(*usedNrOfTriangles > 0);
-
- //
- //------------------------
- // Handle the indexed case
- //------------------------
- //
- memset(visibleIndexedVertices->GetData(), 0, *usedNrOfPoints);
- //
- //-----------------------------------------------------------------
- // Step through each polygon, making sure that we don't try to clip
- // backfaced polygons
- //-----------------------------------------------------------------
- //
- int i, j, k, k0, k1, *cs = (int *)clipPerVertex->GetData();
- unsigned short l;
- int index0, index1, index2, ret = 0;
- int mask;
- Scalar a = 0.0f;
- for(i=0,j=0;i<*usedNrOfTriangles;j+=3,++i)
- {
- index0 = index[j];
- index1 = index[j+1];
- index2 = index[j+2];
- //
- //---------------------------------------------------------------
- // 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
- //---------------------------------------------------------------
- //
- theAnd = cs[index0];
- theAnd &= (cs[index1] & cs[index2]);
- theOr |= (cs[index1] | cs[index2]);
- theAnd = theOr = 0; //ASSUME NO CLIPPING NEEDED FOR MC2. Its just not done here!
-
- //
- //-------------------------------------------------------------------
- // If any bit is set for all vertices, then the polygon is completely
- // outside the viewing space and we don't have to draw it. On the
- // other hand, if no bits at all were ever set, we can do a trivial
- // accept of the polygon
- //-------------------------------------------------------------------
- //
- if (theAnd != 0)
- {
- testList[i] = 0;
- #ifdef LAB_ONLY
- Set_Statistic(PolysClippedButOutside, PolysClippedButOutside+1);
- #endif
- }
- else if (theOr == 0)
- {
- testList[i] = 1;
- ret++;
- (*visibleIndexedVertices)[index0] = 1;
- (*visibleIndexedVertices)[index1] = 1;
- (*visibleIndexedVertices)[index2] = 1;
- #ifdef LAB_ONLY
- Set_Statistic(PolysClippedButInside, PolysClippedButInside+1);
- #endif
- }
- //
- //-----------------------------------------------------------------
- // It is not a trivial case, so we must now do real clipping on the
- // polygon
- //-----------------------------------------------------------------
- //
- else
- {
- unsigned short numberVerticesPerPolygon = 0;
- //
- //---------------------------------------------------------------
- // Handle the case of a single clipping plane by stepping through
- // the vertices and finding the edge it originates
- //---------------------------------------------------------------
- //
- bool firstIsIn;
- if (theOr.GetNumberOfSetBits() == 1)
- {
- #ifdef LAB_ONLY
- Set_Statistic(PolysClippedButOnePlane, PolysClippedButOnePlane+1);
- #endif
- for(k=j;k<j+3;k++)
- {
- k0 = index[k];
- k1 = index[(k+1) < j+3 ? k+1 : j];
- //
- //----------------------------------------------------
- // If this vertex is inside the viewing space, copy it
- // directly to the clipping buffer
- //----------------------------------------------------
- //
- int clipped_index =
- myNumberUsedClipVertex + numberVerticesPerPolygon;
- theTest = cs[k0];
- if(theTest == 0)
- {
- firstIsIn = true;
- (*clipExtraCoords)[clipped_index] = (*transformedCoords)[k0];
- (*clipExtraColors)[clipped_index] = colors[k0];
- (*clipExtraTexCoords)[clipped_index] = texCoords[k0];
- numberVerticesPerPolygon++;
- clipped_index++;
- //
- //-------------------------------------------------------
- // 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(cs[k1] == 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(cs[k1] != 0)
- {
- Verify(cs[k1] == cs[k0]);
- continue;
- }
- }
- //
- //--------------------------------------------------
- // We now find the distance along the edge where the
- // clipping plane will intersect
- //--------------------------------------------------
- //
- int ct = 0;
- mask = 1;
- theTest |= cs[k1];
- //
- //-----------------------------------------------------
- // Find the boundary conditions that match our clipping
- // plane
- //-----------------------------------------------------
- //
- for (l=0; l<MLRClippingState::NextBit; l++)
- {
- if(theTest.IsClipped(mask))
- {
- // GetDoubleBC(l, bc0, bc1, transformedCoords[k0], transformedCoords[k1]);
- //
- //-------------------------------------------
- // Find the clipping interval from bc0 to bc1
- //-------------------------------------------
- //
- if(firstIsIn==true)
- {
- a = GetLerpFactor(l, (*transformedCoords)[k0], (*transformedCoords)[k1]);
- }
- else
- {
- a = GetLerpFactor(l, (*transformedCoords)[k1], (*transformedCoords)[k0]);
- }
- Verify(a >= 0.0f && a <= 1.0f);
- ct = l;
- break;
- }
- mask <<= 1;
- }
- //
- //------------------------------
- // Lerp the homogeneous position
- //------------------------------
- //
- if(firstIsIn==true)
- {
- (*clipExtraCoords)[clipped_index].Lerp(
- (*transformedCoords)[k0],
- (*transformedCoords)[k1],
- a
- );
- DoClipTrick((*clipExtraCoords)[clipped_index], ct);
- //
- //----------------------------------------------------------
- // If there are colors, lerp them in screen space for now as
- // most cards do that anyway
- //----------------------------------------------------------
- //
- #if COLOR_AS_DWORD
- (*clipExtraColors)[clipped_index] = Color_DWORD_Lerp (
- colors[k0],
- colors[k1],
- a
- );
- #else
- (*clipExtraColors)[clipped_index].Lerp(
- colors[k0],
- colors[k1],
- a
- );
- #endif
- //
- //-----------------------------------------------------
- // If there are texture uv's, we need to lerp them in a
- // perspective correct manner
- //-----------------------------------------------------
- //
- (*clipExtraTexCoords)[clipped_index].Lerp
- (
- texCoords[k0],
- texCoords[k1],
- a
- );
- }
- else
- {
- (*clipExtraCoords)[clipped_index].Lerp(
- (*transformedCoords)[k1],
- (*transformedCoords)[k0],
- a
- );
- DoClipTrick((*clipExtraCoords)[clipped_index], ct);
- //
- //----------------------------------------------------------
- // If there are colors, lerp them in screen space for now as
- // most cards do that anyway
- //----------------------------------------------------------
- //
- #if COLOR_AS_DWORD
- (*clipExtraColors)[clipped_index] = Color_DWORD_Lerp (
- colors[k1],
- colors[k0],
- a
- );
- #else
- (*clipExtraColors)[clipped_index].Lerp(
- colors[k1],
- colors[k0],
- a
- );
- #endif
- //
- //-----------------------------------------------------
- // If there are texture uv's, we need to lerp them in a
- // perspective correct manner
- //-----------------------------------------------------
- //
- (*clipExtraTexCoords)[clipped_index].Lerp
- (
- texCoords[k1],
- texCoords[k0],
- a
- );
- }
- //
- //--------------------------------
- // Bump the polygon's vertex count
- //--------------------------------
- //
- numberVerticesPerPolygon++;
- }
- (*clipExtraLength)[myNumberUsedClipLength] = numberVerticesPerPolygon;
- #ifdef _ARMOR
- (*clipExtraLength)[myNumberUsedClipLength] &= ~0x8000;
- #endif
- }
- //
- //---------------------------------------------------------------
- // We have to handle multiple planes. We do this by creating two
- // buffers and we switch between them as we clip plane by plane
- //---------------------------------------------------------------
- //
- else
- {
- #ifdef LAB_ONLY
- Set_Statistic(PolysClippedButGOnePlane, PolysClippedButGOnePlane+1);
- #endif
- ClipData2 srcPolygon, dstPolygon;
- int dstBuffer = 1;
- srcPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
- srcPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
- srcPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
- srcPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();
- //
- //----------------------------------------------------------
- // unravel and copy the original data into the source buffer
- //----------------------------------------------------------
- //
- for(k=j,l=0;k<j+3;k++,l++)
- {
- int indexK = index[k];
- srcPolygon.coords[l] = (*transformedCoords)[indexK];
- srcPolygon.colors[l] = colors[indexK];
- srcPolygon.texCoords[l] = texCoords[indexK];
- srcPolygon.clipPerVertex[l] = (*clipPerVertex)[indexK];
- }
- srcPolygon.length = l;
- //
- //--------------------------------
- // Point to the destination buffer
- //--------------------------------
- //
- dstBuffer = 0;
- dstPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
- dstPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
- 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';
- }
- #endif
- #if HUNT_CLIP_ERROR
- DEBUG_STREAM << "TheOriginalOR: " << hex << theOr.GetClippingState() << dec << '\n';
- #endif
- 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
- 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
- dstPolygon.clipPerVertex[dstPolygon.length] =
- srcPolygon.clipPerVertex[k];
- dstPolygon.colors[dstPolygon.length] =
- srcPolygon.colors[k];
- dstPolygon.texCoords[dstPolygon.length] =
- srcPolygon.texCoords[k];
- 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
- DoClipTrick(dstPolygon.coords[dstPolygon.length], l);
- //
- //----------------------------------------------------------
- // If there are colors, lerp them in screen space for now as
- // most cards do that anyway
- //----------------------------------------------------------
- //
- #if COLOR_AS_DWORD
- dstPolygon.colors[dstPolygon.length] = Color_DWORD_Lerp(
- srcPolygon.colors[k],
- srcPolygon.colors[k1],
- a
- );
- #else
- dstPolygon.colors[dstPolygon.length].Lerp(
- srcPolygon.colors[k],
- srcPolygon.colors[k1],
- a
- );
- #endif
- //
- //-----------------------------------------------------
- // If there are texture uv's, we need to lerp them in a
- // perspective correct manner
- //-----------------------------------------------------
- //
- dstPolygon.texCoords[dstPolygon.length].Lerp
- (
- srcPolygon.texCoords[k],
- srcPolygon.texCoords[k1],
- a
- );
- }
- 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
- DoClipTrick(dstPolygon.coords[dstPolygon.length], l);
- //
- //----------------------------------------------------------
- // If there are colors, lerp them in screen space for now as
- // most cards do that anyway
- //----------------------------------------------------------
- //
- #if COLOR_AS_DWORD
- dstPolygon.colors[dstPolygon.length] = Color_DWORD_Lerp(
- srcPolygon.colors[k1],
- srcPolygon.colors[k],
- a
- );
- #else
- dstPolygon.colors[dstPolygon.length].Lerp(
- srcPolygon.colors[k1],
- srcPolygon.colors[k],
- a
- );
- #endif
- //
- //-----------------------------------------------------
- // If there are texture uv's, we need to lerp them in a
- // perspective correct manner
- //-----------------------------------------------------
- //
- dstPolygon.texCoords[dstPolygon.length].Lerp
- (
- srcPolygon.texCoords[k1],
- srcPolygon.texCoords[k],
- a
- );
- }
- //
- //-------------------------------------
- // 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();
- srcPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
- srcPolygon.texCoords = clipBuffer[dstBuffer].texCoords.GetData();
- srcPolygon.clipPerVertex = clipBuffer[dstBuffer].clipPerVertex.GetData();
- srcPolygon.length = dstPolygon.length;
- dstBuffer = !dstBuffer;
- dstPolygon.coords = clipBuffer[dstBuffer].coords.GetData();
- dstPolygon.colors = clipBuffer[dstBuffer].colors.GetData();
- 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
- 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
- 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
- (*clipExtraCoords)[clipped_index] = srcPolygon.coords[k];
- (*clipExtraColors)[clipped_index] = srcPolygon.colors[k];
- (*clipExtraTexCoords)[clipped_index] = srcPolygon.texCoords[k];
- }
- numberVerticesPerPolygon = srcPolygon.length;
- #if HUNT_CLIP_ERROR
- DEBUG_STREAM << "---" << '\n';
- #endif
-
- (*clipExtraLength)[myNumberUsedClipLength] = numberVerticesPerPolygon;
- #ifdef _ARMOR
- (*clipExtraLength)[myNumberUsedClipLength] |= 0x8000;
- #endif
- }
- myNumberUsedClipVertex += numberVerticesPerPolygon;
- myNumberUsedClipLength++;
- ret++;
- // clip
- // dont draw the original
- testList[i] = 0;
- }
- }
- Check_Object(vt);
- gos_vertices = vt->GetActualVertexPool();
- numGOSVertices = 0;
- gos_indices = vt->GetActualIndexPool();
- numGOSIndices = 0;
- unsigned short strideIndex;
- for(j=0,strideIndex=0;j<*usedNrOfPoints;j++)
- {
- if((*visibleIndexedVertices)[j] == 0)
- {
- strideIndex++;
- }
- else
- {
- indexOffset[j] = static_cast<unsigned short>(j-strideIndex);
- GOSCopyData(
- &gos_vertices[numGOSVertices],
- transformedCoords->GetData(),
- colors,
- texCoords,
- j
- );
- #ifdef LAB_ONLY
- if(gShowClippedPolys)
- {
- gos_vertices[numGOSVertices].argb = 0xff0000ff;
- gos_vertices[numGOSVertices].u = 0.0f;
- gos_vertices[numGOSVertices].v = 0.0f;
- }
- #endif
- numGOSVertices++;
- }
- }
- for(i=0,j=0;i<*usedNrOfTriangles;j+=3,++i)
- {
- if(testList[i] == 0)
- {
- continue;
- }
- Verify((vt->GetLastIndex() + 3 + numGOSIndices) < vt->GetLength());
- Verify(indexOffset[index[j]] < numGOSVertices);
- gos_indices[numGOSIndices] = indexOffset[index[j]];
- Verify(indexOffset[index[j+2]] < numGOSVertices);
- gos_indices[numGOSIndices+1] = indexOffset[index[j+2]];
- Verify(indexOffset[index[j+1]] < numGOSVertices);
- gos_indices[numGOSIndices+2] = indexOffset[index[j+1]];
- numGOSIndices += 3;
- }
- unsigned short stride;
- if(myNumberUsedClipLength > 0)
- {
- for(i=0,j=0;i<myNumberUsedClipLength;i++)
- {
- #ifdef _ARMOR
- stride = static_cast<unsigned short>((*clipExtraLength)[i] & 0x7fff);
- #else
- stride = static_cast<unsigned short>((*clipExtraLength)[i]);
- #endif
- for(k=1;k<stride-1;k++)
- {
- Verify((vt->GetLast() + 3 + numGOSVertices) < vt->GetLength());
- Verify((vt->GetLastIndex() + 3 + numGOSIndices) < vt->GetLength());
- GOSCopyTriangleData(
- &gos_vertices[numGOSVertices],
- clipExtraCoords->GetData(),
- clipExtraColors->GetData(),
- clipExtraTexCoords->GetData(),
- j, j+k+1, j+k
- );
- #ifdef LAB_ONLY
- if(gShowClippedPolys)
- {
- if((*clipExtraLength)[i] & 0x8000)
- {
- gos_vertices[numGOSVertices].argb = 0xffff0000;
- gos_vertices[numGOSVertices].u = 0.0f;
- gos_vertices[numGOSVertices].v = 0.0f;
- gos_vertices[numGOSVertices+1].argb = 0xffff0000;
- gos_vertices[numGOSVertices+1].u = 0.0f;
- gos_vertices[numGOSVertices+1].v = 0.0f;
- gos_vertices[numGOSVertices+2].argb = 0xffff0000;
- gos_vertices[numGOSVertices+2].u = 0.0f;
- gos_vertices[numGOSVertices+2].v = 0.0f;
- }
- else
- {
- gos_vertices[numGOSVertices].argb = 0xffff9999;
- gos_vertices[numGOSVertices].u = 0.0f;
- gos_vertices[numGOSVertices].v = 0.0f;
- gos_vertices[numGOSVertices+1].argb = 0xffff9999;
- gos_vertices[numGOSVertices+1].u = 0.0f;
- gos_vertices[numGOSVertices+1].v = 0.0f;
- gos_vertices[numGOSVertices+2].argb = 0xffff9999;
- gos_vertices[numGOSVertices+2].u = 0.0f;
- gos_vertices[numGOSVertices+2].v = 0.0f;
- }
- }
- #endif
- Verify((vt->GetLastIndex() + 3 + numGOSIndices) < vt->GetLength());
- Verify(numGOSIndices%3 == 0);
- gos_indices[numGOSIndices] = (unsigned short)numGOSVertices;
- gos_indices[numGOSIndices+1] = (unsigned short)(numGOSVertices + 1);
- gos_indices[numGOSIndices+2] = (unsigned short)(numGOSVertices + 2);
- numGOSVertices += 3;
- numGOSIndices += 3;
- }
- j += stride;
- }
- #if HUNT_CLIP_ERROR
- DEBUG_STREAM << "***" << endl << endl;
- #endif
- }
- vt->Increase(numGOSVertices);
- vt->IncreaseIndex(numGOSIndices);
- return numGOSIndices;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRIndexedTriangleCloud::TestInstance() const
- {
- if (usedNrOfTriangles)
- {
- Check_Pointer(usedNrOfTriangles);
- Verify(*usedNrOfTriangles >= 0);
- Verify(*usedNrOfTriangles <= maxNrOf);
- }
- }
|