123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618 |
- //===========================================================================//
- // Copyright (C) Microsoft Corporation. All rights reserved. //
- //===========================================================================//
- #pragma once
- #define MLR_MLRPRIMITIVEBASE_HPP
- #if !defined(MLR_MLR_HPP)
- #include <MLR\MLR.hpp>
- #endif
- #if !defined(MLR_GOSVERTEXPOOL_HPP)
- #include <MLR\GOSVertexPool.hpp>
- #endif
- namespace MidLevelRenderer {
- struct ClipPolygon2
- {
- void Init(int);
- void Destroy();
- Stuff::DynamicArrayOf<Stuff::Vector4D> coords; // [Max_Number_Vertices_Per_Polygon]
- #if COLOR_AS_DWORD
- Stuff::DynamicArrayOf<DWORD> colors; //[Max_Number_Vertices_Per_Polygon];
- #else
- Stuff::DynamicArrayOf<Stuff::RGBAColor> colors; //[Max_Number_Vertices_Per_Polygon];
- #endif
- Stuff::DynamicArrayOf<Vector2DScalar> texCoords; //[2*Max_Number_Vertices_Per_Polygon];
- Stuff::DynamicArrayOf<MLRClippingState> clipPerVertex; //[Max_Number_Vertices_Per_Polygon];
- };
- class MLRPrimitiveBase__ClassData;
- //##########################################################################
- //#################### MLRPrimitiveBase ##############################
- //##########################################################################
- // this is the abstract base class for all geometry. it has contains geometry
- // with one and only one renderer state !!!
- typedef Stuff::Vector2DOf<Stuff::Scalar> Vector2DScalar;
- class _declspec(novtable) MLRPrimitiveBase:
- public Stuff::RegisteredClass
- {
- friend class MLRShape;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // Initialization
- //
- public:
- static void
- InitializeClass();
- static void
- TerminateClass();
- typedef MLRPrimitiveBase__ClassData ClassData;
- static ClassData
- *DefaultData;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // Constructors/Destructors
- //
- protected:
- MLRPrimitiveBase(
- ClassData *class_data,
- Stuff::MemoryStream *stream,
- int version
- );
- ~MLRPrimitiveBase();
- public:
- MLRPrimitiveBase(ClassData *class_data);
- typedef MLRPrimitiveBase*
- (*Factory)(
- Stuff::MemoryStream *stream,
- int version
- );
- static MLRPrimitiveBase*
- Make(
- Stuff::MemoryStream *stream,
- int version
- );
- virtual void
- Save(Stuff::MemoryStream *stream);
- // Subprimitves are units in which this geometry is split off
- // ie. nr of polygons in a polygon mesh or number of tripstrips
- // in a tristriped mesh, every of this subprimitves has another
- // number which is type specific
- // ie. number of vertices in a polygon or number of vertices in
- // a tristrip
- // the data for the coord/color/texcoord/normal or index
- // ARE IN THIS ORDER
- virtual int
- GetNumPrimitives()
- { Check_Object(this); return lengths.GetLength(); }
- virtual void
- SetSubprimitiveLengths(
- unsigned char *length_array,
- int subprimitive_count
- ) = 0;
- // returns the number of subprimitives
- void
- GetSubprimitiveLengths(unsigned char **length_array, int*);
- int
- GetSubprimitiveLength(int i) const;
- // ==============================================================
- virtual void SetReferenceState(const MLRState& _state, int=0)
- { Check_Object(this); referenceState = _state; };
- virtual const MLRState&
- GetReferenceState(int=0) const
- { Check_Object(this); return referenceState; };
- virtual const MLRState&
- GetCurrentState(int=0) const
- { Check_Object(this); return state; };
- virtual void
- CombineStates (const MLRState& master)
- { Check_Object(this); state.Combine(master, referenceState); };
- int
- GetNumVertices()
- { Check_Object(this); return coords.GetLength(); }
- virtual void
- SetCoordData(
- const Stuff::Point3D *array,
- int point_count
- );
- virtual void
- GetCoordData(
- Stuff::Point3D **array,
- int *point_count
- );
- virtual void
- SetTexCoordData(
- const Vector2DScalar *array,
- int point_count
- );
- virtual void
- GetTexCoordData(
- Vector2DScalar **array,
- int *point_count
- );
- // is to call befor clipping, parameter: camera point
- virtual int FindBackFace(const Stuff::Point3D&) = 0;
- virtual void Lighting(MLRLight* const*, int nrLights) = 0;
- static void InitializeDraw();
- virtual void InitializeDrawPrimitive(unsigned char, int=0);
- int GetVisible ()
- { Check_Object(this); return visible; }
- virtual GOSVertex*
- GetGOSVertices(int=0)
- { Check_Object(this); return gos_vertices; }
- int
- GetNumGOSVertices()
- { Check_Object(this); return numGOSVertices; }
- virtual GOSVertex2UV*
- GetGOSVertices2UV(int=0)
- { Check_Object(this); return NULL; }
- int
- GetSortDataMode()
- { Check_Object(this); return drawMode; }
- virtual bool
- CastRay(
- Stuff::Line3D *line,
- Stuff::Normal3D *normal
- );
- virtual void
- PaintMe(const Stuff::RGBAColor *paintMe) = 0;
- virtual int
- TransformAndClip(Stuff::Matrix4D*, MLRClippingState, GOSVertexPool*,bool=false) = 0;
- virtual void
- TransformNoClip(Stuff::Matrix4D*, GOSVertexPool*,bool=false) = 0;
- virtual int
- GetNumPasses()
- { Check_Object(this); return passes; }
- virtual void
- HurtMe(const Stuff::LinearMatrix4D&, Stuff::Scalar radius)
- { Check_Object(this); }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // This functions using the static buffers
- //
- void
- SetClipCoord(Stuff::Point3D &point, int index)
- {
- Check_Object(this); Verify(clipExtraCoords->GetLength() > index);
- (*clipExtraCoords)[index].x = point.x;
- (*clipExtraCoords)[index].y = point.y;
- (*clipExtraCoords)[index].z = point.z;
- }
- void
- FlashClipCoords(int num)
- {
- Check_Object(this); Verify(clipExtraCoords->GetLength() > num);
- coords.SetLength(num);
- for(int i=0;i<num;i++)
- {
- coords[i].x = (*clipExtraCoords)[i].x;
- coords[i].y = (*clipExtraCoords)[i].y;
- coords[i].z = (*clipExtraCoords)[i].z;
- }
- }
- void
- SetClipTexCoord(Vector2DScalar &uvs, int index)
- {
- Check_Object(this); Verify(clipExtraTexCoords->GetLength() > index);
- Verify( MLRState::GetHasMaxUVs() ? (uvs[0]>=-100.0 && uvs[0]<=100.0) : 1);
- Verify( MLRState::GetHasMaxUVs() ? (uvs[1]>=-100.0 && uvs[1]<=100.0) : 1);
- (*clipExtraTexCoords)[index] = uvs;
- }
- void
- FlashClipTexCoords(int num)
- {
- Check_Object(this); Verify(clipExtraTexCoords->GetLength() > num);
- texCoords.SetLength(num);
- Mem_Copy(texCoords.GetData(), clipExtraTexCoords->GetData(), sizeof(Vector2DScalar)*num, sizeof(Vector2DScalar)*num);
- }
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // Reference counting
- //
- public:
- void
- AttachReference()
- {Check_Object(this); ++referenceCount;}
- void
- DetachReference()
- {
- Check_Object(this); Verify(referenceCount > 0);
- if ((--referenceCount) == 0)
- {
- Unregister_Object(this);
- delete this;
- }
- }
- int
- GetReferenceCount()
- {return referenceCount;}
- protected:
- int
- referenceCount;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- // Testing
- //
- public:
- void
- TestInstance() const;
- virtual int
- GetSize()
- {
- Check_Object(this);
- int ret = 0;
- ret += coords.GetSize();
- ret += texCoords.GetSize();
- ret += lengths.GetSize();
- return ret;
- }
- void
- GetExtend(Stuff::ExtentBox *box);
- protected:
- virtual void
- Transform(Stuff::Matrix4D*);
- static ClipPolygon2 *clipBuffer;
- unsigned char visible; // primitive visibilty per frame
- unsigned char passes;
- // int numPrimitives; // Number of primitives, e.g. - num quads
- // Replaced by GetNumPrimitives
- // int numVertices; // number of verts for stats and vert arrays
- // Replaced by GetNumVertices
- Stuff::DynamicArrayOf<Stuff::Point3D> coords; // Base address of coordinate list
- Stuff::DynamicArrayOf<Vector2DScalar> texCoords; // Base address of texture coordinate list
- static Stuff::DynamicArrayOf<Stuff::Vector4D> *transformedCoords;
- Stuff::DynamicArrayOf<unsigned char> lengths; // List of strip lengths
- #if COLOR_AS_DWORD // clipExtraColors for the future generations !!!
- static Stuff::DynamicArrayOf<DWORD> *clipExtraColors; // , Max_Number_Vertices_Per_Mesh
- #else
- static Stuff::DynamicArrayOf<Stuff::RGBAColor> *clipExtraColors; // , Max_Number_Vertices_Per_Mesh
- #endif
- static Stuff::DynamicArrayOf<MLRClippingState> *clipPerVertex; // , Max_Number_Vertices_Per_Mesh
- static Stuff::DynamicArrayOf<Stuff::Vector4D> *clipExtraCoords; // , Max_Number_Vertices_Per_Mesh
- static Stuff::DynamicArrayOf<Vector2DScalar> *clipExtraTexCoords; // , Max_Number_Vertices_Per_Mesh
- static Stuff::DynamicArrayOf<unsigned short> *clipExtraLength; // , Max_Number_Primitives_Per_Frame
- MLRState state, referenceState;
- int drawMode;
- GOSVertex *gos_vertices;
- unsigned short numGOSVertices;
- };
- struct IcoInfo {
- int type;
- int depth;
- bool indexed;
- Stuff::Scalar radius;
- Stuff::Scalar all;
- bool onOff;
- const char *GetTypeName();
- };
- MLRShape*
- CreateIndexedIcosahedron(
- IcoInfo&,
- Stuff::DynamicArrayOf<MLRState>*
- );
- //##########################################################################
- //################### MLRPrimitiveBase__ClassData ####################
- //##########################################################################
- class MLRPrimitiveBase__ClassData:
- public Stuff::RegisteredClass::ClassData
- {
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- public:
- MLRPrimitiveBase__ClassData(
- Stuff::RegisteredClass::ClassID class_id,
- const char* class_name,
- Stuff::RegisteredClass::ClassData *parent_class,
- MLRPrimitiveBase::Factory primitive_factory
- ):
- RegisteredClass__ClassData(class_id, class_name, parent_class),
- primitiveFactory(primitive_factory)
- {}
- MLRPrimitiveBase::Factory
- primitiveFactory;
- //~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
- //
- public:
- void
- TestInstance();
- };
- struct ClipData2
- {
- Stuff::Vector4D *coords;
- #if COLOR_AS_DWORD
- DWORD *colors;
- #else
- Stuff::RGBAColor *colors;
- #endif
- Vector2DScalar *texCoords;
- MLRClippingState *clipPerVertex;
- unsigned short length;
- };
- #if 0 // still defined in "MLRPrimitive.hpp"
- inline Stuff::Scalar
- GetBC(int nr, const Stuff::Vector4D& v4d)
- {
- switch(nr)
- {
- case 0:
- return (v4d.w - v4d.y);
- case 1:
- return v4d.y;
- case 2:
- return (v4d.w - v4d.x);
- case 3:
- return v4d.x;
- case 4:
- return v4d.z;
- case 5:
- return (v4d.w - v4d.z);
- }
- return 0.0f;
- }
- inline void
- GetDoubleBC
- (
- int nr,
- Stuff::Scalar& result1,
- Stuff::Scalar& result2,
- const Stuff::Vector4D& v4d1,
- const Stuff::Vector4D& v4d2
- )
- {
- switch(nr)
- {
- case 0:
- result1 = (v4d1.w - v4d1.y);
- result2 = (v4d2.w - v4d2.y);
- break;
- case 1:
- result1 = v4d1.y;
- result2 = v4d2.y;
- break;
- case 2:
- result1 = (v4d1.w - v4d1.x);
- result2 = (v4d2.w - v4d2.x);
- break;
- case 3:
- result1 = v4d1.x;
- result2 = v4d2.x;
- break;
- case 4:
- result1 = v4d1.z;
- result2 = v4d2.z;
- break;
- case 5:
- result1 = (v4d1.w - v4d1.z);
- result2 = (v4d2.w - v4d2.z);
- break;
- }
- }
- inline Stuff::Scalar
- GetLerpFactor
- (
- int nr,
- const Stuff::Vector4D& v4d1,
- const Stuff::Vector4D& v4d2
- )
- {
- Stuff::Scalar result1, result2;
- switch(nr)
- {
- case 0:
- result1 = (v4d1.w - v4d1.y);
- result2 = (v4d2.w - v4d2.y);
- break;
- case 1:
- result1 = v4d1.y;
- result2 = v4d2.y;
- break;
- case 2:
- result1 = (v4d1.w - v4d1.x);
- result2 = (v4d2.w - v4d2.x);
- break;
- case 3:
- result1 = v4d1.x;
- result2 = v4d2.x;
- break;
- case 4:
- result1 = v4d1.z;
- result2 = v4d2.z;
- break;
- case 5:
- result1 = (v4d1.w - v4d1.z);
- result2 = (v4d2.w - v4d2.z);
- break;
- default:
- result1 = 0.0f;
- result2 = 0.0f;
- Abort_Program("Invalid plane number used !");
- break;
- }
- Verify(!Stuff::Close_Enough(result1, result2));
- return result1 / (result1 - result2);
- }
- #endif
- inline Stuff::Scalar
- GetBC(int nr, const Stuff::Vector4D& v4d)
- {
- switch(nr)
- {
- case 0:
- return (v4d.w - v4d.y);
- case 1:
- return v4d.y;
- case 2:
- return (v4d.w - v4d.x);
- case 3:
- return v4d.x;
- case 4:
- return v4d.z;
- case 5:
- return (v4d.w - v4d.z);
- }
- return 0.0f;
- }
- inline void
- GetDoubleBC
- (
- int nr,
- Stuff::Scalar& result1,
- Stuff::Scalar& result2,
- const Stuff::Vector4D& v4d1,
- const Stuff::Vector4D& v4d2
- )
- {
- switch(nr)
- {
- case 0:
- result1 = (v4d1.w - v4d1.y);
- result2 = (v4d2.w - v4d2.y);
- break;
- case 1:
- result1 = v4d1.y;
- result2 = v4d2.y;
- break;
- case 2:
- result1 = (v4d1.w - v4d1.x);
- result2 = (v4d2.w - v4d2.x);
- break;
- case 3:
- result1 = v4d1.x;
- result2 = v4d2.x;
- break;
- case 4:
- result1 = v4d1.z;
- result2 = v4d2.z;
- break;
- case 5:
- result1 = (v4d1.w - v4d1.z);
- result2 = (v4d2.w - v4d2.z);
- break;
- }
- }
- inline Stuff::Scalar
- GetLerpFactor
- (
- int nr,
- const Stuff::Vector4D& v4d1,
- const Stuff::Vector4D& v4d2
- )
- {
- Stuff::Scalar result1, result2;
- switch(nr)
- {
- case 0:
- result1 = (v4d1.w - v4d1.y);
- result2 = (v4d2.w - v4d2.y);
- break;
- case 1:
- result1 = v4d1.y;
- result2 = v4d2.y;
- break;
- case 2:
- result1 = (v4d1.w - v4d1.x);
- result2 = (v4d2.w - v4d2.x);
- break;
- case 3:
- result1 = v4d1.x;
- result2 = v4d2.x;
- break;
- case 4:
- result1 = v4d1.z;
- result2 = v4d2.z;
- break;
- case 5:
- result1 = (v4d1.w - v4d1.z);
- result2 = (v4d2.w - v4d2.z);
- break;
- default:
- result1 = 0.0f;
- result2 = 0.0f;
- STOP(("Invalid plane number used !"));
- break;
- }
- if(result1 == 0.0f)
- {
- return 0.0f;
- }
- Verify(!Stuff::Close_Enough(result1, result2, Stuff::SMALL*0.1f));
- return result1 / (result1 - result2);
- }
- }
|