123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695 |
- //===========================================================================//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- #include "MLRHeaders.hpp"
- //#############################################################################
- //############################### MLRShape ##################################
- //#############################################################################
- MLRShape::ClassData*
- MLRShape::DefaultData = NULL;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRShape::InitializeClass()
- {
- Verify(!DefaultData);
- Verify(gos_GetCurrentHeap() == StaticHeap);
- DefaultData =
- new ClassData(
- MLRShapeClassID,
- "MidLevelRenderer::MLRShape",
- Plug::DefaultData
- );
- Register_Object(DefaultData);
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRShape::TerminateClass()
- {
- Unregister_Object(DefaultData);
- delete DefaultData;
- DefaultData = NULL;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- MLRShape::MLRShape(
- MemoryStream *stream,
- int version
- ):
- Plug(DefaultData)
- {
- Check_Pointer(this);
- Check_Object(stream);
- Verify(gos_GetCurrentHeap() == Heap);
- *stream >> numPrimitives;
- int i;
- allPrimitives.SetLength(numPrimitives);
- for(i=0;i<numPrimitives;i++)
- {
- MLRPrimitiveBase *pt = NULL;
- switch(version)
- {
- case 1:
- case 2:
- {
- STOP(("Lower than version 3 is not supported anymore !"));
- /*
- MLRPrimitive *pt_old;
- RegisteredClass::ClassID class_id;
- *stream >> class_id;
- MLRPrimitive::ClassData* class_data =
- Cast_Object(MLRPrimitive::ClassData*, FindClassData(class_id));
- pt_old = (*class_data->primitiveFactory)(stream, version);
- Register_Object(pt_old);
- int i, nr, test;
- if(pt_old->GetReferenceState().GetLightingMode() == MLRState::LightingOffMode)
- {
- test = 0;
- }
- else
- {
- test = 2;
- }
- #if COLOR_AS_DWORD
- DWORD* colors;
- #else
- RGBAColor *colors, testColor(1.0f, 1.0f, 1.0f, 1.0f);
- #endif
- Cast_Pointer(MLRIndexedPolyMesh*, pt_old)->GetColorData(&colors, &nr);
- for(i=0;i<nr;i++)
- {
- #if COLOR_AS_DWORD
- if(colors[i] != 0xffffffff)
- #else
- if(colors[i] != testColor)
- #endif
- {
- break;
- }
- }
- if(i<nr)
- {
- test |= 1;
- }
- switch (test)
- {
- case 0:
- pt = new MLR_I_PMesh;
- Cast_Pointer(MLR_I_PMesh*, pt)->Copy(Cast_Pointer(MLRIndexedPolyMesh*, pt_old));
- break;
- case 1:
- pt = new MLR_I_C_PMesh;
- Cast_Pointer(MLR_I_C_PMesh*, pt)->Copy(Cast_Pointer(MLRIndexedPolyMesh*, pt_old));
- break;
- default:
- pt = new MLR_I_L_PMesh;
- Cast_Pointer(MLR_I_L_PMesh*, pt)->Copy(Cast_Pointer(MLRIndexedPolyMesh*, pt_old));
- break;
- }
- pt_old->DetachReference();
- */
- }
- break;
- default:
- {
- RegisteredClass::ClassID class_id;
- *stream >> class_id;
- MLRPrimitiveBase::ClassData* class_data =
- Cast_Pointer(MLRPrimitiveBase::ClassData*, FindClassData(class_id));
- Check_Object(class_data);
- pt = (*class_data->primitiveFactory)(stream, version);
- Register_Object(pt);
- if(
- ConvertToTriangleMeshes == true && (
- pt->IsDerivedFrom(MLR_I_PMesh::DefaultData) ||
- pt->IsDerivedFrom(MLR_I_C_PMesh::DefaultData) ||
- pt->IsDerivedFrom(MLR_I_L_PMesh::DefaultData)
- )
- )
- {
- unsigned char *length;
- int i, num, threes, nonThrees;
- pt->GetSubprimitiveLengths(&length, &num);
- for(i=0,threes=0,nonThrees=0;i<num;i++)
- {
- if(length[i]==3)
- {
- threes++;
- }
- else
- {
- nonThrees++;
- }
- }
- if(threes>0 && nonThrees==0) //threes > 9*nonThrees)
- {
- MLRPrimitiveBase *pt_old = pt;
-
- if(pt->GetClassData() == MLR_I_L_PMesh::DefaultData)
- {
- pt = new MLR_I_L_TMesh;
- Register_Object(pt);
- Cast_Pointer(MLR_I_L_TMesh*, pt)->Copy(Cast_Pointer(MLR_I_L_PMesh*, pt_old));
- }
- else if(pt->GetClassData() == MLR_I_C_PMesh::DefaultData)
- {
- pt = new MLR_I_C_TMesh;
- Register_Object(pt);
- Cast_Pointer(MLR_I_C_TMesh*, pt)->Copy(Cast_Pointer(MLR_I_C_PMesh*, pt_old));
- }
- else if(pt->GetClassData() == MLR_I_PMesh::DefaultData)
- {
- pt = new MLR_I_TMesh;
- Register_Object(pt);
- Cast_Pointer(MLR_I_TMesh*, pt)->Copy(Cast_Pointer(MLR_I_PMesh*, pt_old));
- }
- else if(pt->GetClassData() == MLR_I_L_DT_PMesh::DefaultData)
- {
- pt = new MLR_I_L_DT_TMesh;
- Register_Object(pt);
- Cast_Pointer(MLR_I_L_DT_TMesh*, pt)->Copy(Cast_Pointer(MLR_I_L_DT_PMesh*, pt_old));
- }
- else if(pt->GetClassData() == MLR_I_C_DT_PMesh::DefaultData)
- {
- pt = new MLR_I_C_DT_TMesh;
- Register_Object(pt);
- Cast_Pointer(MLR_I_C_DT_TMesh*, pt)->Copy(Cast_Pointer(MLR_I_C_DT_PMesh*, pt_old));
- }
- else if(pt->GetClassData() == MLR_I_DT_PMesh::DefaultData)
- {
- pt = new MLR_I_DT_TMesh;
- Register_Object(pt);
- Cast_Pointer(MLR_I_DT_TMesh*, pt)->Copy(Cast_Pointer(MLR_I_DT_PMesh*, pt_old));
- }
- else if(pt->GetClassData() == MLR_I_L_DeT_PMesh::DefaultData)
- {
- pt = new MLR_I_L_DeT_TMesh;
- Register_Object(pt);
- Cast_Pointer(MLR_I_L_DeT_TMesh*, pt)->Copy(Cast_Pointer(MLR_I_L_DeT_PMesh*, pt_old));
- }
- else if(pt->GetClassData() == MLR_I_C_DeT_PMesh::DefaultData)
- {
- pt = new MLR_I_C_DeT_TMesh;
- Register_Object(pt);
- Cast_Pointer(MLR_I_C_DeT_TMesh*, pt)->Copy(Cast_Pointer(MLR_I_C_DeT_PMesh*, pt_old));
- }
- else if(pt->GetClassData() == MLR_I_DeT_PMesh::DefaultData)
- {
- pt = new MLR_I_DeT_TMesh;
- Register_Object(pt);
- Cast_Pointer(MLR_I_DeT_TMesh*, pt)->Copy(Cast_Pointer(MLR_I_DeT_PMesh*, pt_old));
- }
- pt_old->DetachReference();
- }
- }
- }
- break;
- }
- allPrimitives[i] = pt;
- }
- referenceCount = 1;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- MLRShape::MLRShape(int nr):
- Plug(DefaultData), allPrimitives(nr ? nr : 4)
- {
- Verify(gos_GetCurrentHeap() == Heap);
- numPrimitives = 0;
- referenceCount = 1;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- MLRShape::~MLRShape()
- {
- int i;
- MLRPrimitiveBase *pt;
- for(i=numPrimitives-1;i>=0;i--)
- {
- pt = allPrimitives[i];
- allPrimitives[i] = NULL;
- pt->DetachReference();
- }
- Verify(referenceCount==0);
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- MLRShape*
- MLRShape::Make(
- MemoryStream *stream,
- int version
- )
- {
- Check_Object(stream);
- gos_PushCurrentHeap(Heap);
- MLRShape *shape = new MLRShape(stream, version);
- gos_PopCurrentHeap();
- return shape;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRShape::Save(MemoryStream *stream)
- {
- Check_Object(this);
- Check_Object(stream);
- *stream << numPrimitives;
- int i;
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- allPrimitives[i]->Save(stream);
- }
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRShape::Add (MLRPrimitiveBase *p)
- {
- Check_Object(this);
- if(numPrimitives >= allPrimitives.GetLength() || allPrimitives.GetLength() == 0)
- {
- gos_PushCurrentHeap(Heap);
- allPrimitives.SetLength(numPrimitives + 4);
- gos_PopCurrentHeap();
- }
- allPrimitives[numPrimitives] = p;
- p->AttachReference();
- numPrimitives++;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- int
- MLRShape::GetNumPrimitives()
- {
- int i, ret = 0;
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- ret += allPrimitives[i]->GetNumPrimitives();
- }
- return ret;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- int
- MLRShape::GetNumDrawnTriangles()
- {
- int i, j, ret = 0;
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- if(allPrimitives[i]->IsDerivedFrom(MLRIndexedPrimitiveBase::DefaultData))
- {
- j = (Cast_Pointer(MLRIndexedPrimitiveBase*, allPrimitives[i]))->GetNumGOSIndices();
- }
- else
- {
- j = allPrimitives[i]->GetNumGOSVertices();
- }
- if(j!=0xffff)
- {
- ret += j;
- }
- }
- return ret;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- MLRPrimitiveBase*
- MLRShape::Find (int i)
- {
- Check_Object(this);
- Verify(i<numPrimitives);
- return allPrimitives[i];
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- int
- MLRShape::Find (MLRPrimitiveBase *p)
- {
- Check_Object(this);
- Check_Object(p);
- int i;
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- if(allPrimitives[i] == p)
- {
- return i;
- }
- }
- return -1;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- bool
- MLRShape::Replace (MLRPrimitiveBase *pout, MLRPrimitiveBase *pin)
- {
- Check_Object(this);
- Check_Object(pout);
- Check_Object(pin);
- int num = Find (pout);
- if(num>=0)
- {
- pout->DetachReference();
- allPrimitives[num] = pin;
- pin->AttachReference();
- }
- else
- {
- return false;
- }
- return true;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- MLRPrimitiveBase*
- MLRShape::Remove(MLRPrimitiveBase *p)
- {
- Check_Object(this);
- Check_Object(p);
- int i, nr = Find(p);
- if(nr < 0)
- {
- return NULL;
- }
- for(i=nr;i<numPrimitives-1;i++)
- {
- allPrimitives[i] = allPrimitives[i+1];
- }
- allPrimitives[i] = NULL;
- numPrimitives--;
- p->DetachReference();
- return p;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- MLRPrimitiveBase*
- MLRShape::Remove(int nr)
- {
- Check_Object(this);
- int i;
- MLRPrimitiveBase *p = Find(nr);
- if(nr < 0 || nr >= numPrimitives)
- {
- return NULL;
- }
- for(i=nr;i<numPrimitives-1;i++)
- {
- allPrimitives[i] = allPrimitives[i+1];
- }
- allPrimitives[i] = NULL;
- numPrimitives--;
- p->DetachReference();
- return p;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- int
- MLRShape::Insert(MLRPrimitiveBase *p, int nr)
- {
- Check_Object(this);
- if(nr >= numPrimitives)
- {
- Add(p);
- return numPrimitives;
- }
- if(numPrimitives >= allPrimitives.GetLength() || allPrimitives.GetLength() == 0)
- {
- gos_PushCurrentHeap(Heap);
- allPrimitives.SetLength(numPrimitives + 4);
- gos_PopCurrentHeap();
- }
- int i;
- for(i=numPrimitives;i>nr;i--)
- {
- allPrimitives[i] = allPrimitives[i-1];
- }
- allPrimitives[i] = p;
- p->AttachReference();
- numPrimitives++;
- return numPrimitives;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRShape::InitializePrimitives(unsigned char vis, const MLRState& master, int parameter)
- {
- Check_Object(this);
- int i;
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- allPrimitives[i]->InitializeDrawPrimitive(vis, parameter);
-
- allPrimitives[i]->CombineStates(master);
- }
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // p is the eye point
- void
- MLRShape::HurtMe(const Stuff::LinearMatrix4D& pain, Stuff::Scalar radius)
- {
- for(int i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- allPrimitives[i]->HurtMe(pain, radius);
- }
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // p is the eye point
- int
- MLRShape::FindBackFace(const Point3D& p)
- {
- Check_Object(this);
- int i, ret = 0;
- Point3D sp;
-
- sp.Multiply(p, *worldToShape);
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- ret += allPrimitives[i]->FindBackFace(sp);
- }
- return ret;
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- /*
- void
- MLRShape::Transform(Matrix4D *mat)
- {
- Check_Object(this);
- int i;
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- allPrimitives[i]->Transform(mat);
- }
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRShape::Transform()
- {
- Check_Object(this);
- int i;
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- allPrimitives[i]->Transform(&shapeToClipMatrix);
- }
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- int
- MLRShape::Clip(MLRClippingState clippingFlags, GOSVertexPool *vp)
- {
- Check_Object(this);
- int i, ret = 0;
- gos_GetViewport( &ViewportScalars::MulX, &ViewportScalars::MulY, &ViewportScalars::AddX, &ViewportScalars::AddY );
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- ret += allPrimitives[i]->Clip(clippingFlags, vp);
- }
- return ret;
- }
- */
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- void
- MLRShape::Lighting (
- const LinearMatrix4D& WorldToShape,
- MLRLight* const* lights,
- int nrLights
- )
- {
- Check_Object(this);
- Check_Object(&WorldToShape);
- if(nrLights == 0)
- {
- return;
- }
- int i;
- for(i=0;i<nrLights;i++)
- {
- lights[i]->SetLightToShapeMatrix(WorldToShape);
- }
- for(i=0;i<numPrimitives;i++)
- {
- Check_Object(allPrimitives[i]);
- allPrimitives[i]->Lighting(lights, nrLights);
- }
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- bool
- MLRShape::CastRay (
- Line3D *line,
- Normal3D *normal
- )
- {
- Check_Object(this);
- Check_Object(line);
- Check_Pointer(normal);
- bool result = false;
- for(int i=0; i<numPrimitives; i++)
- {
- Check_Object(allPrimitives[i]);
- if (allPrimitives[i]->CastRay(line, normal))
- result = true;
- }
- return result;
- }
|