home *** CD-ROM | disk | FTP | other *** search
- // Copyright (c)1995 Ray Dream, Inc. All Rights Reserved.
- /* $Id: COMStar.cpp 1.13 1997/06/20 22:34:19 damien Exp $ */
-
- ////////////////////////////////////////////////////////////////////////
- // Geometric Primitive Example : Facets //
- //--------------------------------------------------------------------//
- // Implementation of the Facets Interface //
- ////////////////////////////////////////////////////////////////////////
-
- #include "math.h"
-
- #ifndef __COMSTAR__
- #include "COMSTAR.h"
- #endif
-
- #ifndef __I3DSHUTI__
- #include "I3DShUti.h"
- #endif
-
- #ifndef __ISHFMESH__
- #include "IShFMesh.h"
- #endif
-
- #ifndef __3DCOFAIL__
- #include "3DCoFail.h"
- #endif
-
- // Constructor / Destructor of the C++ Object :
- Facets::Facets() {
- fCRef=0; // Reference Counter
- fData.fNbBranches=5;
- }
-
- Facets::~Facets() {
- global_count_Obj--;
- }
-
- // IUnknown Interface :
- HRESULT Facets::QueryInterface(THIS_ REFIID riid,LPVOID FAR* ppvObj) {
- *ppvObj=NULL;
-
- // The Facets knows the interfaces of the parent Objects
- if (IsEqualIID(riid, IID_IUnknown))
- *ppvObj=(IUnknown*)(I3DExGeometricPrimitive*)this;
- else if (IsEqualIID(riid, IID_I3DExGeometricPrimitive))
- *ppvObj=(I3DExGeometricPrimitive*)this;
- else if (IsEqualIID(riid, IID_I3DExDataExchanger))
- *ppvObj=(I3DExDataExchanger*)this;
- else if (IsEqualIID(riid, IID_I3DExtension))
- *ppvObj=(I3DExtension*)this;
-
- // we must add reference if we return an interface
- if (*ppvObj!=NULL) {
- ((LPUNKNOWN)*ppvObj)->AddRef();
- return NOERROR;
- }
- else {
- return ResultFromScode(E_NOINTERFACE);
- }
- }
-
- ULONG Facets::AddRef() {
- return this->fCRef++;
- }
-
- ULONG Facets::Release() {
- ULONG UnreleaseObject=fCRef--;
-
- if (fCRef==0)
- delete this; // No reference left, so destroy the object
-
- return UnreleaseObject;
- // local variable used, because fCRef can be destroyed before.
- }
-
- // I3DExtension methods :
- I3DExtension* Facets::Clone() {
- Facets* theClone = new Facets;
- if (theClone) {
- theClone->AddRef();
- theClone->fData=fData;
- }
- return (I3DExtension*)theClone;
- }
-
- HRESULT Facets::ShellUtilitiesInit(THIS_ IShUtilities* shellUtilities) {
- return NOERROR;
- }
-
- // I3DExDataExchanger methods :
- ExtensionDataMap* Facets::GetExtensionDataMap() {
- return NULL;
- }
-
- void* Facets::GetExtensionDataBuffer() {
- return &fData;
- }
-
- HRESULT Facets::ExtensionDataChanged() {
- return NOERROR;
- }
-
- HRESULT Facets::HandleEvent(THIS_ ULONG SourceID) {
- return ResultFromScode(E_NOTIMPL);
- }
-
- short Facets::GetResID() {
- return 136; // always return -1, if you do not have a view
- // Here, we have a view to change the number of branches of the Star
- }
-
- // I3DExGeometricPrimitive methods
- // -- Geometry calls
- HRESULT Facets::EnumPatches(THIS_ EnumPatchesCallback callback, void* privData) {
- return ResultFromScode(E_NOTIMPL);
- }
-
- HRESULT Facets::EnumFacets(THIS_ EnumFacetsCallback callback, void* privData, NUM3D fidelity) {
- short i, j;
- FACET3D StarFacet;
- VECTOR3D v1,v2,normal;
- NUM3D angle,anglestep,radius1,radius2,radiusswap,sinus,cosinus;
- NUM3D k360=3.1415926535897932384626233 * 2;
-
- angle=0.0;
- anglestep=k360/((NUM3D)fData.fNbBranches)/2;
- radius1=10.0;
- radius2=4.0;
-
- // Inferior Facets
- // -- Common Vertex of each inferior facets
- StarFacet.fVertices[0].fVertex[0]=0.0;
- StarFacet.fVertices[0].fVertex[1]=0.0;
-
- for (i=0;i<(fData.fNbBranches*2);i++) {
- // Variable information of the common vertex
- StarFacet.fUVSpace=0;
- StarFacet.fVertices[0].fUV[1]=(((float)i)+0.5)/((float)fData.fNbBranches*2);
- //(angle + anglestep/2.0)/k360;
- // Second Vertex information
- sinus=sin(angle);
- cosinus=cos(angle); //QuickSinCos(angle,sinus,cosinus);
- StarFacet.fVertices[1].fVertex[0]=cosinus*radius1;
- StarFacet.fVertices[1].fVertex[1]=sinus*radius1;
- StarFacet.fVertices[1].fVertex[2]=0.0;
- StarFacet.fVertices[1].fUV[0]=0.5;
- StarFacet.fVertices[1].fUV[1]=((float)i)/((float)fData.fNbBranches*2); //angle/k360;
- // Last Vertex information
- angle += anglestep;
- sinus=sin(angle);
- cosinus=cos(angle); //QuickSinCos(angle,sinus,cosinus);
- StarFacet.fVertices[2].fVertex[0]=cosinus*radius2;
- StarFacet.fVertices[2].fVertex[1]=sinus*radius2;
- StarFacet.fVertices[2].fVertex[2]=0.0;
- StarFacet.fVertices[2].fUV[0]=0.5;
- StarFacet.fVertices[2].fUV[1]=angle/k360;
- // Swap the radius to "alternate" the point
- radiusswap=radius1;radius1=radius2;radius2=radiusswap;
- // Normal vector calculation
- for (j=0; j<3; j++) {
- v1[j]=StarFacet.fVertices[1].fVertex[j]-StarFacet.fVertices[0].fVertex[j];
- v2[j]=StarFacet.fVertices[2].fVertex[j]-StarFacet.fVertices[0].fVertex[j];
- normal[j]=v2[(j+1)%3]*v1[(j+2)%3] - v2[(j+2)%3]*v1[(j+1)%3];
- }
- /*normal[0]=v2[1]*v1[2] - v2[2]*v1[1];
- normal[1]=v2[2]*v1[0] - v2[0]*v1[2];
- normal[2]=v2[0]*v1[1] - v2[1]*v1[0]; // Vectorial product*/
- NUM3D norm=sqrt(SQR(normal[0]) + SQR(normal[1]) + SQR(normal[2]));
- normal[0] /= norm;
- normal[1] /= norm;
- normal[2] /= norm; // Normalization
- for (j=0; j<3; j++) {
- StarFacet.fVertices[0].fNormal[j]=normal[j]; // the 3 points have the same normal vector
- StarFacet.fVertices[1].fNormal[j]=normal[j];
- StarFacet.fVertices[2].fNormal[j]=normal[j];
- }
- // callback is used to give a facet to the shell
- // Inferior Facets
- StarFacet.fVertices[0].fVertex[2]=-4.0;
- StarFacet.fVertices[0].fUV[0]=1.0;
- callback(&StarFacet,privData);
- // Superior Facets
- for (j=0; j<3; j++) {
- // reverse normals
- StarFacet.fVertices[j].fNormal[2]=-StarFacet.fVertices[j].fNormal[2];
- }
- StarFacet.fVertices[0].fVertex[2]=4.0;
- StarFacet.fVertices[0].fUV[0]=0.0; // changed
- callback(&StarFacet,privData);
- }
-
- return NOERROR;
- }
-
- void AccuFacet(FACET3D* facet, void* accu) {
- ((IShFacetMeshAccumulator*)accu)->AddFacet(facet);
- }
-
- HRESULT Facets::GetNbrLOD(short &nbrLod) {
- return ResultFromScode(E_NOTIMPL);
- }
-
- HRESULT Facets::GetLOD(short lodIndex, NUM3D &lod) {
- return ResultFromScode(E_NOTIMPL);
- }
-
- HRESULT Facets::MakeFacetMesh(short index, FacetMesh &amesh) {
- IShFacetMeshAccumulator* accu;
- gShellUtilities->CoCreateInstance(CLSID_StandardFacetMeshAccumulator, NULL, CLSCTX_INPROC_SERVER, IID_IShFacetMeshAccumulator, (LPVOID*)&accu);
- EnumFacets(AccuFacet, accu, 0);
- accu->MakeFacetMesh(amesh);
- accu->Release();
- return S_OK;
- }
-
- HRESULT Facets::MakeFacetMesh(NUM3D lod, FacetMesh &amesh) {
- return MakeFacetMesh((short)0, amesh);
- }
-
- // Give the boundary Box
- HRESULT Facets::GetBBox(THIS_ BOX3D* bbox) {
- bbox->fMin[0]=-10.0;
- bbox->fMax[0]=10.0;
- bbox->fMin[1]=-10.0;
- bbox->fMax[1]=10.0;
- bbox->fMin[2]=-4.0;
- bbox->fMax[2]=4.0;
- return NOERROR;
- }
-
- // -- Shading calls
- ULONG Facets::GetUVSpaceCount()
- {
- return 1; // the star is describe with only 1 UV-Space
- }
-
- HRESULT Facets::GetUVSpace(THIS_ ULONG uvSpaceID, UVSpaceInfo* uvSpaceInfo)
- {
- if (uvSpaceID == 0)
- {
- uvSpaceInfo->fMin[0] = 0.0; // u coordinate goes from 0 to 1
- uvSpaceInfo->fMax[0] = 1.0;
- uvSpaceInfo->fMin[1] = 0.0; // v coordinate goes from 0 to 1
- uvSpaceInfo->fMax[1] = 1.0;
- uvSpaceInfo->fWraparound[0] = FALSE; // No Wrap around
- uvSpaceInfo->fWraparound[1] = FALSE;
- // uvSpaceInfo->fIsFlatSurface = FALSE; // the surface is not flat
- }
- return NOERROR;
- }
-
- // We use the default interpolation method to get all the coordinate of a point in UV Coordinates
- HRESULT Facets::UV2XYZ(THIS_ VECTOR2D* uv, ULONG uvSpaceID, VECTOR3D* resultPosition, BOOLEAN* inUVSpace) {
- return ResultFromScode(E_NOTIMPL);
- }
-
- // -- Ray Tracing calls
- HRESULT Facets::RayHit(THIS_ BOOLEAN* didHit, Ray3D* aR, RayHitParameters* RayHitParams, RayHit3D* hit) {
- return ResultFromScode(E_NOTIMPL);
- }
-
- HRESULT Facets::GetRayHitDetails(THIS_ RayHit3D* hit) {
- return ResultFromScode(E_NOTIMPL);
- }
-
- HRESULT Facets::RayAllHits(THIS_ Ray3D* aR, NUM3D tmin, NUM3D tmax, RayHit3D* hit, RayHitCallback callback, void* privData) {
- return ResultFromScode(E_NOTIMPL);
- }
-
-
-
-
-