예제 #1
0
    void fillSubModelArary(const ModelContainer* pModelContainer, const TreeNode *root, Array<SubModel>& array, Vector3& pLo, Vector3& pHi) {
        Vector3 lo = Vector3(inf(), inf(), inf());
        Vector3 hi = Vector3(-inf(), -inf(), -inf());

        for(int i=0; i<	root->getNValues(); i++) {
            SubModel sm = pModelContainer->getSubModel(root->getStartPosition() + i);
            lo = lo.min(sm.getAABoxBounds().low());
            hi = hi.max(sm.getAABoxBounds().high());
            array.append(sm);
        }

        if(root->getChild((TreeNode *) &pModelContainer->getTreeNode(0), 0)) {
            fillSubModelArary(pModelContainer, root->getChild((TreeNode *)&pModelContainer->getTreeNode(0), 0), array, lo, hi);
        }
        if(root->getChild((TreeNode *)&pModelContainer->getTreeNode(0), 1)) {
            fillSubModelArary(pModelContainer, root->getChild((TreeNode *)&pModelContainer->getTreeNode(0), 1), array, lo, hi);
        }

        float dist1 = (hi -lo).magnitude();
        AABox b;
        root->getBounds(b);
        float dist2 = (b.high() -b.low()).magnitude();
        if(dist1 > dist2) {
            // error
            int xxx = 0;
        }

    }
예제 #2
0
 //===================================================
 Vector3 getViewPos(const ModelContainer* mc, unsigned int pModelNr = 0) {
     if(mc->getNSubModel() < pModelNr) {
         pModelNr = mc->getNSubModel();
     }
     const SubModel sm = mc->getSubModel(pModelNr);
     return (sm.getAABoxBounds().low());
 }
예제 #3
0
    void ModelContainerView::fillVertexAndIndexArrays(const SubModel& pSm, Array<Vector3>& vArray, Array<int>& iArray) {
        Array<TriangleBox> tbarray;

        fillRenderArray(pSm, tbarray, &pSm.getTreeNode(0));
        MeshBuilder builder;
        int len = tbarray.size();
        int count = 0;
        for(int i=0;i<len;++i) {
            Triangle t = Triangle(tbarray[i].vertex(0).getVector3() + pSm.getBasePosition(),
                tbarray[i].vertex(1).getVector3() + pSm.getBasePosition(),
                tbarray[i].vertex(2).getVector3() + pSm.getBasePosition());

            vArray.append(t.vertex(0));
            vArray.append(t.vertex(1));
            vArray.append(t.vertex(2));

            iArray.append(count+0);
            iArray.append(count+1);
            iArray.append(count+1);
            iArray.append(count+2);
            iArray.append(count+2);
            iArray.append(count+0);
            count+=3;
        }
    }
예제 #4
0
  void
  fillSubModelArary (const ModelContainer* pModelContainer, const TreeNode *root, Array<SubModel>& array, Vector3& pLo, Vector3& pHi)
  {
    Vector3 lo = Vector3 (inf (), inf (), inf ());
    Vector3 hi = Vector3 (-inf (), -inf (), -inf ());
    //printf("[VMAP] %i Submodels\n", root->getNValues ());
    for (int i = 0; i < root->getNValues (); i++)
      {
        SubModel sm = pModelContainer->getSubModel (root->getStartPosition () + i);
        lo = lo.min (sm.getAABoxBounds ().low ());
        hi = hi.max (sm.getAABoxBounds ().high ());
        array.append (sm);
      }

    if (root->getChild ((TreeNode *) & pModelContainer->getTreeNode (0), 0))
      {
        fillSubModelArary (pModelContainer, root->getChild ((TreeNode *) & pModelContainer->getTreeNode (0), 0), array, lo, hi);
      }
    if (root->getChild ((TreeNode *) & pModelContainer->getTreeNode (0), 1))
      {
        fillSubModelArary (pModelContainer, root->getChild ((TreeNode *) & pModelContainer->getTreeNode (0), 1), array, lo, hi);
      }

    float dist1 = (hi - lo).magnitude ();
    AABox b;
    root->getBounds (b);
    float dist2 = (b.high () - b.low ()).magnitude ();
    if (dist1 > dist2)
      {
        // error
        //printf("error in boundingbox size vs. hi-lo size.");
        // this error seems to happen alot! maybe need to do something against it?
        int xxx = 0;
      }
  }
예제 #5
0
    RealTime ModelContainer::getIntersectionTime(const Ray& pRay, bool pExitAtFirst, float pMaxDist) const
    {
        #ifdef _DEBUG_VMAPS
        for(unsigned int i=0; i<getNSubModel(); i++)
        {
            SubModel model = getSubModel(i);
            bool insiteOk;
            Vector3 mynormal;
            Vector3 location;
            bool hitval = MyCollisionDetection::collisionLocationForMovingPointFixedAABox(
                pRay.origin, pRay.direction,
                model.getAABoxBounds(),
                location,insiteOk, mynormal);
            if(hitval)
            {
                float len2 = (location - pRay.origin).squaredLength();
                int a = 0;                                  // just to be able to set a breakpoint
            }
        }
        TreeNode tn = getTreeNode(0);
        for(int i=0; i<tn.getNValues(); i++)
        {
            SubModel mysm = getSubModel(tn.getStartPosition() + i);
            AABox testbox = mysm.getAABoxBounds();
            gBoxArray.append(AABox(testbox.low(), testbox.high()));
        }
        #endif

        double  firstDistance = inf();
        Ray relativeRay = Ray::fromOriginAndDirection(pRay.origin - getBasePosition(), pRay.direction);
        const IT end = endRayIntersection();
        IT obj = beginRayIntersection(pRay, pMaxDist);
        for ( ;obj != end; ++obj)                           // (preincrement is *much* faster than postincrement!)
        {
            /*
            Call your accurate intersection test here.  It is guaranteed
            that the ray hits the bounding box of obj.  (*obj) has type T,
            so you can call methods directly using the "->" operator.
            */
            SubModel const *model =  &(*obj);

            RealTime t = model->getIntersectionTime(pRay, pExitAtFirst, pMaxDist);
            if(t > 0 && t < inf())
            {
                obj.markBreakNode();
                if(firstDistance > t && pMaxDist >= t)
                {
                    firstDistance = t;
                    if(pExitAtFirst) { break; }
                }
            }
        }
        return(firstDistance);
    }
예제 #6
0
    void ModelContainerView::fillRenderArray(const SubModel& pSm, Array<TriangleBox> &pArray, const TreeNode* pTreeNode) {
        for(int i=0;i<pTreeNode->getNValues(); i++) {
            pArray.append(pSm.getTriangles()[i+pTreeNode->getStartPosition()]);
        }

        if(pTreeNode->getChild(pSm.getTreeNodes(), 0) != 0) {
            fillRenderArray(pSm, pArray, pTreeNode->getChild(pSm.getTreeNodes(), 0));
        }

        if(pTreeNode->getChild(pSm.getTreeNodes(), 1) != 0) {
            fillRenderArray(pSm, pArray, pTreeNode->getChild(pSm.getTreeNodes(), 1));
        }
    }
예제 #7
0
파일: SubModel.cpp 프로젝트: FLaM666/evo-X
    bool SubModel::operator==(const SubModel& pSm2) const
    {
        bool result = false;

        if(getNNodes() == pSm2.getNNodes() &&
            getNTriangles() == pSm2.getNTriangles() &&
            getBasePosition() == pSm2.getBasePosition() &&
            getNodesPos() == pSm2.getNodesPos() &&
            getTrianglesPos() == pSm2.getTrianglesPos())
        {
            result = true;
        }
        return result;
    }
예제 #8
0
 void
 ModelContainerView::fillVertexArray (const SubModel& pSm, Array<Vector3>& vArray)
 {
   //printf("fillVertexArray for %i-%i\n",VMAP::hashCode(SubModel),5);
   //printf("fillVertexArray for %i\n",VMAP::hashCode(pSm));
   Array<TriangleBox> tbarray;
   fillRenderArray (pSm, tbarray, &pSm.getTreeNode (0));
   int len = tbarray.size ();
   Vector3 toconvert[3];
   for (int i = 0; i < len; ++i)
     {
       // TODO: Get correct Conversion of position values.
       // The system between vmaps and MaNGOS differs in that:
       /* from iinternal(xyz) -> MaNGOS(xyz)
       float pos[3];
       pos[0] = z;
       pos[1] = x;
       pos[2] = y;
       double full = 64.0*533.33333333;
       double mid = full/2.0;
       pos[0] = -((mid+pos[0])-full); -> -mid -pos[0] + full -> mid - z
       pos[1] = -((mid+pos[1])-full); -> -mid -pos[0] + full -> mid - x
       */
       TriangleBox old = tbarray[i];
       toconvert[0] = old.vertex(0).getVector3 () + pSm.getBasePosition ();
       toconvert[1] = old.vertex(1).getVector3 () + pSm.getBasePosition ();
       toconvert[2] = old.vertex(2).getVector3 () + pSm.getBasePosition ();
       for (int j = 0; j < 3; j++) {
           toconvert[j] = toconvert[j].zxy();
           toconvert[j].x = MID - toconvert[j].x;
           toconvert[j].y = MID - toconvert[j].y;
       }
       //TODO: playing around with correct range of triangles for detour
       Triangle t = Triangle(toconvert[0],toconvert[1],toconvert[2]);
       Triangle t1 = Triangle(toconvert[2],toconvert[1],toconvert[0]);
       globalTriangleArray.append(t,t1);
       /*
       for (int j = 2; j>= 0; j--) {
           Vector3 vertex = tbarray[i].vertex(j).getVector3() + pSm.getBasePosition();
           // x = [expr {-(($::mid+$x)-$::full)}]
           //printf("x[%.2f] y[%.2f] z[%.2f]",vertex.x,vertex.y,vertex.z);
           // v 152.333 60.0304 -9561.67
           vertex = Vector3(-((64*533.333333333/2 + vertex.z) - 64*533.333333333),
                           -((64*533.333333333/2 + vertex.x) - 64*533.333333333),
                           vertex.y);
           vArray.append(vertex);
       }
       */
     }
 }
예제 #9
0
    void ModelContainer::countSubModelsAndNodesAndTriangles(AABSPTree<SubModel *>::Node& pNode, int& nSubModels, int& nNodes, int& nTriangles)
    {
        // For this node we will need a TreeNode as well as for the internal nodes
        ++nNodes;

        nSubModels += pNode.valueArray.size();
        for (int i=0; i<pNode.valueArray.size(); i++)
        {
        G3D::_AABSPTree::Handle<SubModel*>* h= pNode.valueArray[i];
            SubModel *m = h->value;
            // add the internal nodes as well
            nNodes += m->getNNodes();
            nTriangles += m->getNTriangles();
        }

        if(pNode.child[0] != 0)
        {
            countSubModelsAndNodesAndTriangles(*pNode.child[0], nSubModels, nNodes, nTriangles);
        }
        if(pNode.child[1] != 0)
        {
            countSubModelsAndNodesAndTriangles(*pNode.child[1],  nSubModels, nNodes, nTriangles);
        }
    }
예제 #10
0
	bool MapTree::isOutDoors(const Vector3& pos)
	{
		Vector3 dir = Vector3(0,-1,0);
		Ray ray = Ray::fromOriginAndDirection(pos, dir);   // direction with length of 1
		unsigned int flags;

		AABSPTree<ModelContainer*>::RayIntersectionIterator MITR;
		AABSPTree<ModelContainer*>::RayIntersectionIterator MITREND;
		RayIntersectionIterator<TreeNode, SubModel> SMITR;
		RayIntersectionIterator<TreeNode, SubModel> SMITREND;
		ModelContainer * mc;
		SubModel * sm;

		MITR = iTree->beginRayIntersection( ray, MAX_CAN_FALL_DISTANCE );
		MITREND = iTree->endRayIntersection();

		for(; MITR != MITREND; ++MITR)
		{
			mc = ((ModelContainer*)*MITR);
			
			Ray relativeRay = Ray::fromOriginAndDirection(ray.origin - mc->getBasePosition(), ray.direction);
			SMITR = mc->beginRayIntersection( relativeRay, MAX_CAN_FALL_DISTANCE );
			SMITREND = mc->endRayIntersection();

			for( ; SMITR != SMITREND; ++SMITR )
			{
				sm = ((SubModel*)&(*SMITR));
#if 0
				if( sm->getIndoorFlag() != 0 )
				{
					unsigned int i = sm->getIndoorFlag();
					printf("=========================================================\n");
					printf("Model indoor flags: %u\n", sm->getIndoorFlag());
					unsigned int z, b;
					for(z = 1, b = 1; b < 32;)
					{
						if(i & z)
							printf("   Bit %u (0x%.8X or %u) is set!\n", b, z, z);

						z <<= 1;
						b+=1;
					}
					printf("=========================================================\n");
				}
#else
				flags = sm->getIndoorFlag();
				if( flags != 0 )
				{
					/* From WoWdev:
					 Flag 	 Meaning
					0x1 	Always set
					0x4 	Has vertex colors (MOCV chunk)
					0x8 	Outdoor
					0x200 	Has lights (MOLR chunk)
					0x800 	Has doodads (MODR chunk)
					0x1000 	Has water (MLIQ chunk)
					0x2000 	Indoor
					0x40000 	Show skybox

					**********************

					0x8000 seems to be set in the areas in citys (while it has the indoor flag, its not
					an indoor area

					*/

					if( !(flags & 0x8) )
						return false;
				}
#endif
			}
		}

		return true;
	}
예제 #11
0
파일: SubModel.cpp 프로젝트: FLaM666/evo-X
 void getBounds(const SubModel& pSm, G3D::AABox& pAABox)
 {
     ShortBox box = pSm.getReletiveBounds();
     pAABox.set(box.getLo().getVector3()+pSm.getBasePosition(), box.getHi().getVector3()+pSm.getBasePosition());
 }
예제 #12
0
파일: SubModel.cpp 프로젝트: FLaM666/evo-X
 /**
 Functions to use ModelContainer with a AABSPTree
 */
 unsigned int hashCode(const SubModel& pSm)
 {
     return pSm.getNTriangles();
 }
예제 #13
0
    void ModelContainer::fillContainer(const AABSPTree<SubModel *>::Node& pNode, int &pSubModelPos, int &pTreeNodePos, int &pTrianglePos, Vector3& pLo, Vector3& pHi, Vector3& pFinalLo, Vector3& pFinalHi)
    {
        // TreeNode for the SubModel
        TreeNode treeNode = TreeNode(pNode.valueArray.size(), pSubModelPos);
        treeNode.setSplitAxis(pNode.splitAxis);
        treeNode.setSplitLocation(pNode.splitLocation);
        int currentTreeNodePos = pTreeNodePos++;

        Vector3 lo = Vector3(inf(),inf(),inf());
        Vector3 hi = Vector3(-inf(),-inf(),-inf());

        for (int i=0; i<pNode.valueArray.size(); i++)
        {
            G3D::_AABSPTree::Handle<SubModel*>* h= pNode.valueArray[i];
            SubModel *m = h->value;

            memcpy(&getTreeNodes()[pTreeNodePos], &m->getTreeNode(0), sizeof(TreeNode) * m->getNNodes());
            memcpy(&getTriangles()[pTrianglePos], &m->getTriangle(0), sizeof(TriangleBox) * m->getNTriangles());

            SubModel newModel = SubModel(m->getNTriangles(), getTriangles(), pTrianglePos, m->getNNodes(), getTreeNodes(), pTreeNodePos);
            newModel.setReletiveBounds(m->getReletiveBounds().getLo(), m->getReletiveBounds().getHi());
            newModel.setBasePosition(m->getBasePosition());
            iSubModel[pSubModelPos++] = newModel;

            pTreeNodePos += m->getNNodes();
            pTrianglePos += m->getNTriangles();

            AABox box = m->getAABoxBounds();
            lo = lo.min(box.low());
            hi = hi.max(box.high());
            pFinalLo = pFinalLo.min(lo);
            pFinalHi = pFinalHi.max(hi);
        }
        /*
        if(pNode.valueArray.size() == 0) {
        int xxx = 0; // just for the breakpoint
        }
        */
        // get absolute bounds

        if(pNode.child[0] != 0)
        {
            treeNode.setChildPos(0, pTreeNodePos);
            fillContainer(*pNode.child[0], pSubModelPos, pTreeNodePos, pTrianglePos, lo, hi,pFinalLo,pFinalHi);
        }
        if(pNode.child[1] != 0)
        {
            treeNode.setChildPos(1, pTreeNodePos);
            fillContainer(*pNode.child[1], pSubModelPos, pTreeNodePos, pTrianglePos, lo, hi,pFinalLo,pFinalHi);
        }

        pLo = pLo.min(lo);
        pHi = pHi.max(hi);

        treeNode.setBounds(lo,hi);

        setTreeNode(treeNode, currentTreeNodePos);

    }
예제 #14
0
				//=================================================================
				bool TileAssembler::readRawFile(std::string& pModelFilename,  ModelPosition& pModelPosition, AABSPTree<SubModel *> *pMainTree)
				{
					bool result = false;

					std::string filename = iSrcDir;
					if(filename.length() >0)
						filename.append("/");
					filename.append(pModelFilename);
					FILE *rf = fopen(filename.c_str(), "rb");
					if(!rf)
					{
						// depending on the extractor version, the data could be located in the root dir
						std::string baseModelFilename = pModelFilename.substr((pModelFilename.find_first_of("/")+1),pModelFilename.length());
						filename = iSrcDir;
						if(filename.length() >0)
							filename.append("/");
						filename.append(baseModelFilename);
						rf = fopen(filename.c_str(), "rb");
					}
					char ident[8];

					int trianglecount =0;

					#ifdef _ASSEMBLER_DEBUG
					int startgroup = 0;					 //2;
					int endgroup = INT_MAX;				 //2;
					fprintf(::g_df,"-------------------------------------------------\n");
					fprintf(::g_df,"%s\n", pModelFilename.c_str());
					fprintf(::g_df,"-------------------------------------------------\n");
					#else
					int startgroup = 0;
					int endgroup = INT_MAX;
					#endif

					if(rf)
					{
						if(fread(&ident, 8, 1, rf) != 1) { fclose(rf); return(false); }
						if(strcmp(ident, "VMAP001") == 0)
						{
							// OK, do nothing
						}
						else if(strcmp(ident, "VMAP002") == 0)
						{
							// we have to read one int. This is needed during the export and we have to skip it here
							int tempNVectors;
							if(fread(&tempNVectors, sizeof(int), 1, rf) != 1) { fclose(rf); return(false); }

						}
						else
						{
							// wrong version
							fclose(rf);
							return(false);
						}
						uint32 groups;
						char blockId[5];
						blockId[4] = 0;
						int blocksize;

						if(fread(&groups, sizeof(uint32), 1, rf) != 1) { fclose(rf); return(false); }

						for(int g=0;g<(int)groups;g++)
						{
							// group MUST NOT have more then 65536 indexes !! Array will have a problem with that !! (strange ...)
							Array<int> tempIndexArray;
							Array<Vector3> tempVertexArray;

							AABSPTree<Triangle> *gtree = new AABSPTree<Triangle>();

							uint32 flags;
							uint32 isindoor = 0;
							uint32 branches;
							if(fread(&flags, sizeof(uint32), 1, rf) != 1)
							{
								fclose(rf);
								return(false);
							}

							if(flags & 1) {
								if(fread(&isindoor, sizeof(uint32), 1, rf) != 1)
								{
									fclose(rf);
									return(false);
								}
							}

							if(fread(&blockId, 4, 1, rf) != 1) { fclose(rf); return(false); }
							if(strcmp(blockId, "GRP ") != 0)
							{
								if(fread(&blockId, 4, 1, rf) != 1) { fclose(rf); return(false); }
							}
							if(strcmp(blockId, "GRP ") != 0) { printf("BUG: (%s)\n",blockId); fclose(rf); return(false); }
							if(fread(&blocksize, sizeof(int), 1, rf) != 1) { fclose(rf); return(false); }
							if(fread(&branches, sizeof(uint32), 1, rf) != 1) { fclose(rf); return(false); }
							for(int b=0;b<(int)branches; b++)
							{
								uint32 indexes;
								// indexes for each branch (not used jet)
								if(fread(&indexes, sizeof(uint32), 1, rf) != 1) { fclose(rf); return(false); }
							}

							// ---- indexes
							if(fread(&blockId, 4, 1, rf) != 1) { fclose(rf); return(false); }
							if(strcmp(blockId, "INDX") != 0) { fclose(rf); return(false); }
							if(fread(&blocksize, sizeof(int), 1, rf) != 1) { fclose(rf); return(false); }
							unsigned int nindexes;
							if(fread(&nindexes, sizeof(uint32), 1, rf) != 1) { fclose(rf); return(false); }
							if(nindexes >0)
							{
								unsigned short *indexarray = new unsigned short[nindexes*sizeof(unsigned short)];
								if(fread(indexarray, sizeof(unsigned short), nindexes, rf) != nindexes) { fclose(rf); return(false); }
								for(int i=0;i<(int)nindexes; i++)
								{
									unsigned short val = indexarray[i];
									tempIndexArray.append(val);
								}
								delete indexarray;
							}

							// ---- vectors
							if(fread(&blockId, 4, 1, rf) != 1) {fclose(rf); return(false); }
							if(strcmp(blockId, "VERT") != 0) { fclose(rf); return(false); }
							if(fread(&blocksize, sizeof(int), 1, rf) != 1) { fclose(rf); return(false); }
							unsigned int nvectors;
							if(fread(&nvectors, sizeof(int), 1, rf) != 1) { fclose(rf); return(false); }
							float *vectorarray = 0;
							if(nvectors >0)
							{
								vectorarray = new float[nvectors*sizeof(float)*3];
								if(fread(vectorarray, sizeof(float)*3, nvectors, rf) != nvectors) { fclose(rf); return(false); }
							}
							// ----- liquit
							if(flags & 0x00001000)
							{
								// we have liquit -> not handled yet ... skip
								if(fread(&blockId, 4, 1, rf) != 1) { fclose(rf); return(false); }
								if(strcmp(blockId, "LIQU") != 0) { fclose(rf); return(false); }
								if(fread(&blocksize, sizeof(int), 1, rf) != 1) { fclose(rf); return(false); }
								fseek(rf, blocksize, SEEK_CUR);
							}

							for(unsigned int i=0, indexNo=0; indexNo<nvectors; indexNo++)
							{
								Vector3 v = Vector3(vectorarray[i+2], vectorarray[i+1], vectorarray[i+0]);
								i+=3;
								v = pModelPosition.transform(v);

								float swapy = v.y;
								v.y = v.x;
								v.x = swapy;

								tempVertexArray.append(v);
							}

							// ---- calculate triangles
							int rest = nindexes%3;
							if(rest != 0)
							{
								nindexes -= rest;
							}

							for(unsigned int i=0;i<(nindexes);)
							{
								Triangle t = Triangle(tempVertexArray[tempIndexArray[i+2]], tempVertexArray[tempIndexArray[i+1]], tempVertexArray[tempIndexArray[i+0]] );
								i+=3;
								++trianglecount;
								if(g>= startgroup && g <= endgroup)
								{
									gtree->insert(t);
								}
							}

							if(vectorarray != 0)
							{
								delete vectorarray;
							}

							if(gtree->size() >0)
							{
								gtree->balance();
								SubModel *sm = new SubModel(gtree);
								sm->setIndoorFlag( isindoor );
								#ifdef _ASSEMBLER_DEBUG
								if(::g_df) fprintf(::g_df,"group trianglies: %d, Tris: %d, Nodes: %d, gtree.triangles: %d\n", g, sm->getNTriangles(), sm->getNNodes(), gtree->memberTable.size());
								if(sm->getNTriangles() !=  gtree->memberTable.size())
								{
									if(::g_df) fprintf(::g_df,"ERROR !!!! group trianglies: %d, Tris: %d, Nodes: %d, gtree.triangles: %d\n", g, sm->getNTriangles(), sm->getNNodes(), gtree->memberTable.size());
								}
								#endif
								sm->setBasePosition(pModelPosition.iPos);
								pMainTree->insert(sm);
							}
							delete gtree;
						}
						fclose(rf);
						result = true;
					}
					return(result);
				}
예제 #15
0
    //=================================================================
    bool TileAssembler::readRawFile(std::string& pModelFilename,  ModelPosition& pModelPosition, AABSPTree<SubModel *> *pMainTree)
    {
        std::string filename = iSrcDir;
        if (filename.length() >0)
            filename.append("/");
        filename.append(pModelFilename);
        FILE *rf = fopen(filename.c_str(), "rb");
        if (!rf)
        {
            // depending on the extractor version, the data could be located in the root dir
            std::string baseModelFilename = pModelFilename.substr((pModelFilename.find_first_of("/")+1),pModelFilename.length());
            filename = iSrcDir;
            if (filename.length() >0)
                filename.append("/");
            filename.append(baseModelFilename);
            rf = fopen(filename.c_str(), "rb");
        }

        if (!rf)
        {
            printf("ERROR: Can't open model file in form: %s",pModelFilename.c_str());
            printf("...                          or form: %s",filename.c_str() );
            return false;
        }

        char ident[8];

        int trianglecount =0;

        #ifdef _ASSEMBLER_DEBUG
        int startgroup = 0;                     //2;
        int endgroup = INT_MAX;                 //2;
        fprintf(::g_df,"-------------------------------------------------\n");
        fprintf(::g_df,"%s\n", pModelFilename.c_str());
        fprintf(::g_df,"-------------------------------------------------\n");
        #else
        int startgroup = 0;
        int endgroup = INT_MAX;
        #endif

        // temporary use defines to simplify read/check code (close file and return at fail)
        #define READ_OR_RETURN(V,S) if (fread((V), (S), 1, rf) != 1) { \
                                        fclose(rf); return(false); }
        #define CMP_OR_RETURN(V,S)  if (strcmp((V),(S)) != 0)        { \
                                        fclose(rf); return(false); }

        READ_OR_RETURN(&ident, 8);
        if (strcmp(ident, "VMAP001") == 0)
        {
            // OK, do nothing
        }
        else if (strcmp(ident, "VMAP002") == 0)
        {
            // we have to read one int. This is needed during the export and we have to skip it here
            int tempNVectors;
            READ_OR_RETURN(&tempNVectors, sizeof(int));

        }
        else
        {
            // wrong version
            fclose(rf);
            return(false);
        }
        G3D::uint32 groups;
        char blockId[5];
        blockId[4] = 0;
        int blocksize;

        READ_OR_RETURN(&groups, sizeof(G3D::uint32));

        for (int g=0;g<(int)groups;g++)
        {
            // group MUST NOT have more then 65536 indexes !! Array will have a problem with that !! (strange ...)
            Array<int> tempIndexArray;
            Array<Vector3> tempVertexArray;

            AABSPTree<Triangle> *gtree = new AABSPTree<Triangle>();

            // add free gtree at fail
            #undef READ_OR_RETURN
            #undef CMP_OR_RETURN
            #define READ_OR_RETURN(V,S) if (fread((V), (S), 1, rf) != 1) { \
                                            fclose(rf); delete gtree; return(false); }
            #define CMP_OR_RETURN(V,S)  if (strcmp((V),(S)) != 0)        { \
                                            fclose(rf); delete gtree; return(false); }

            G3D::uint32 flags;
            READ_OR_RETURN(&flags, sizeof(G3D::uint32));

            G3D::uint32 branches;
            READ_OR_RETURN(&blockId, 4);
            CMP_OR_RETURN(blockId, "GRP ");
            READ_OR_RETURN(&blocksize, sizeof(int));
            READ_OR_RETURN(&branches, sizeof(G3D::uint32));
            for (int b=0;b<(int)branches; b++)
            {
                G3D::uint32 indexes;
                // indexes for each branch (not used jet)
                READ_OR_RETURN(&indexes, sizeof(G3D::uint32));
            }

            // ---- indexes
            READ_OR_RETURN(&blockId, 4);
            CMP_OR_RETURN(blockId, "INDX");
            READ_OR_RETURN(&blocksize, sizeof(int));
            unsigned int nindexes;
            READ_OR_RETURN(&nindexes, sizeof(G3D::uint32));
            if (nindexes >0)
            {
                unsigned short *indexarray = new unsigned short[nindexes*sizeof(unsigned short)];
                READ_OR_RETURN(indexarray, nindexes*sizeof(unsigned short));
                for (int i=0;i<(int)nindexes; i++)
                {
                    unsigned short val = indexarray[i];
                    tempIndexArray.append(val);
                }
                delete[] indexarray;
            }

            // ---- vectors
            READ_OR_RETURN(&blockId, 4);
            CMP_OR_RETURN(blockId, "VERT");
            READ_OR_RETURN(&blocksize, sizeof(int));
            unsigned int nvectors;
            READ_OR_RETURN(&nvectors, sizeof(int));

            float *vectorarray = 0;

            // add vectorarray free
            #undef READ_OR_RETURN
            #undef CMP_OR_RETURN
            #define READ_OR_RETURN(V,S) if (fread((V), (S), 1, rf) != 1) { \
                                            fclose(rf); delete gtree; delete[] vectorarray; return(false); }
            #define CMP_OR_RETURN(V,S)  if (strcmp((V),(S)) != 0)        { \
                                            fclose(rf); delete gtree; delete[] vectorarray; return(false); }

            if (nvectors >0)
            {
                vectorarray = new float[nvectors*sizeof(float)*3];
                READ_OR_RETURN(vectorarray, nvectors*sizeof(float)*3);
            }
            // ----- liquit
            if (flags & 1)
            {
                // we have liquit -> not handled yet ... skip
                READ_OR_RETURN(&blockId, 4);
                CMP_OR_RETURN(blockId, "LIQU");
                READ_OR_RETURN(&blocksize, sizeof(int));
                fseek(rf, blocksize, SEEK_CUR);
            }


            for (unsigned int i=0, indexNo=0; indexNo<nvectors; indexNo++)
            {
                Vector3 v = Vector3(vectorarray[i+2], vectorarray[i+1], vectorarray[i+0]);
                i+=3;
                v = pModelPosition.transform(v);

                float swapy = v.y;
                v.y = v.x;
                v.x = swapy;

                tempVertexArray.append(v);
            }

            // ---- calculate triangles
            int rest = nindexes%3;
            if (rest != 0)
            {
                nindexes -= rest;
            }

            for (unsigned int i=0;i<(nindexes);)
            {
                Triangle t = Triangle(tempVertexArray[tempIndexArray[i+2]], tempVertexArray[tempIndexArray[i+1]], tempVertexArray[tempIndexArray[i+0]] );
                i+=3;
                ++trianglecount;
                if (g>= startgroup && g <= endgroup)
                {
                    gtree->insert(t);
                }
            }

            // drop of temporary use defines
            #undef READ_OR_RETURN
            #undef CMP_OR_RETURN

            if (vectorarray != 0)
            {
                delete vectorarray;
            }

            if (gtree->size() >0)
            {
                gtree->balance();
                SubModel *sm = new SubModel(gtree);
                #ifdef _ASSEMBLER_DEBUG
                if (::g_df) fprintf(::g_df,"group trianglies: %d, Tris: %d, Nodes: %d, gtree.triangles: %d\n", g, sm->getNTriangles(), sm->getNNodes(), gtree->memberTable.size());
                if (sm->getNTriangles() !=  gtree->memberTable.size())
                {
                    if (::g_df) fprintf(::g_df,"ERROR !!!! group trianglies: %d, Tris: %d, Nodes: %d, gtree.triangles: %d\n", g, sm->getNTriangles(), sm->getNNodes(), gtree->memberTable.size());
                }
                #endif
                sm->setBasePosition(pModelPosition.iPos);
                pMainTree->insert(sm);
            }
            delete gtree;
        }
        fclose(rf);
        return true;
    }
예제 #16
0
//---------------------------------------------------------------------------//
  bool SubModel::operator==(const SubModel& anOther) const
  {
    return GetDescription() == anOther.GetDescription();
  }