Archived

This topic is now archived and is closed to further replies.

CD3DFrame - expansion to array of bounding boxes

This topic is 5539 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi. I am having trouble getting bounding boxes out of CD3DFrame object-hierarchy. I got boxes out, but they are not correct. Beside all, I have a feeling that "getboundingbox" doesnt work right. How to do it? Also, how to extract vertex points from any type of vertex buffer, no matter what fvf it has? Please please help. ivan ---- code ---- #ifndef MESHHANDLER_H #define MESHHANDLER_H #define D3D_OVERLOADS #include <d3d8.h> #include <D3dx8math.h> #include "D3DFile.h" //#include "coldet.h" #include <ode/ode.h> #include <string> #include <opcode.h> #include <OPC_model.h> using namespace std; using namespace Opcode; using namespace IceMaths; #define D3DFVF_VERTEXSL (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1) #define D3DFVF_VERTEXSL1 (D3DFVF_XYZ|D3DFVF_NORMAL) struct MODELVERTEX { D3DXVECTOR3 p; D3DXVECTOR3 n; FLOAT tu, tv; }; struct MODELVERTEXSIMPLE { D3DXVECTOR3 p; D3DXVECTOR3 n; }; struct MODELVERTEXREALSIMPLE { D3DXVECTOR3 p; }; class CollisionModel { public: CollisionModel(); ~CollisionModel(); OPCODE_Model * opm; IndexedTriangle * faces; Point * vertes; }; struct PosBox { D3DXVECTOR3 kot1; D3DXVECTOR3 kot2; }; struct PosBoxes { PosBox box[1000]; int boxcnt; }; class MeshHolder { public: string name; string xfiledefault; string xfilesmall; double size; CD3DMesh * defaultMesh; CD3DMesh * smallMesh; double line; D3DXVECTOR3 box; PosBoxes boxes; CollisionModel * collModel; }; class MashHandler { int boxcount; PosBox boxes[1000]; D3DXVECTOR3 absmin; D3DXVECTOR3 absmax; public: IDirect3DDevice8 * device; BOOL EnumerateMeshes (CD3DMesh* mesh,D3DXMATRIX mat); void EnumMeshes(CD3DFrame *frame,D3DXMATRIX mat); PosBoxes getBoxes(CD3DFrame * frame,float radius); void fixSize(ID3DXMesh * mesh,float radious,DWORD fvf,D3DXVECTOR3 &box); CollisionModel * Build_Opcode_Model_From_Mesh(ID3DXMesh * mesh); //CollisionModel3D * Build_ColDet_Model_From_Mesh(ID3DXMesh * mesh); }; #endif and #include "MashHandler.h" CollisionModel::CollisionModel() { opm=NULL; faces=NULL; vertes=NULL; } CollisionModel::~CollisionModel() { if (opm) delete opm; if (faces) delete faces; if (vertes) delete vertes; } BOOL MashHandler::EnumerateMeshes (CD3DMesh* cm,D3DXMATRIX mat) { MashHandler * mashh=this; D3DXVECTOR3 min,max,mid; // cm->SetFVF(mashh->device,D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1); ID3DXMesh * m_pMesh=cm->GetSysMemMesh(); // DWORD flag=mesh->GetFVF(); BYTE *pVertex; m_pMesh->LockVertexBuffer(D3DLOCK_READONLY, (BYTE**)&pVertex); D3DXComputeBoundingBox((VOID*)pVertex, m_pMesh->GetNumVertices(),D3DFVF_XYZ|D3DFVF_NORMAL/* m_pMesh->GetFVF()*/, &min, &max); m_pMesh->UnlockVertexBuffer(); D3DXVec3TransformCoord(&min,&min,&mat); D3DXVec3TransformCoord(&max,&max,&mat); /* //DWORD dwNumVertices = mesh->GetNumVertices(); //LPDIRECT3DVERTEXBUFFER8 pVB; //VOID * data; //mesh->GetVertexBuffer( &pVB ); // pVB->Lock( 0, 0, (BYTE**)&data,0 ); // D3DXComputeBoundingBox(data,dwNumVertices,flag,&min,&max); // D3DXVec3TransformCoord(&min,&min,&mat); // D3DXVec3TransformCoord(&max,&max,&mat); // D3DVERTEXBUFFER_DESC desc; struct MESHVERTEX { D3DXVECTOR3 p; D3DXVECTOR3 n; //FLOAT tu, tv; }; DWORD dwNumVertices = mesh->GetNumVertices(); LPDIRECT3DVERTEXBUFFER8 pVB; MESHVERTEX* pVertices; mesh->GetVertexBuffer( &pVB ); // pVB->GetDesc(&desc); // int k=desc.Size/dwNumVertices; // BYTE * data; pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ); //pVB->Lock( 0, 0, &data, D3DLOCK_READONLY ); FILE * dg=fopen("meshh.txt","a"); fprintf(dg,"frame =%\n",mashh->boxcount); for( DWORD i=0; i.p; //D3DXVECTOR3 tmp=*((D3DXVECTOR3 *) (nk)); D3DXVec3TransformCoord(&tmp,&tmp,&mat); // D3DXVec3TransformCoord(&max,&max,&mat); if (fabs(tmp.x)<0.01f) tmp.x=0.01f; if (fabs(tmp.y)<0.01f) tmp.y=0.01f; if (fabs(tmp.z)<0.01f) tmp.z=0.01f; if (tmp.x>max.x || i==0) max.x=tmp.x; if (tmp.xmax.y || i==0) max.y=tmp.y; if (tmp.ymax.z || i==0) max.z=tmp.z; if (tmp.zmashh->absmax.x || (i==0 && mashh->boxcount==0)) mashh->absmax.x=tmp.x; if (tmp.xabsmin.x || (i==0 && mashh->boxcount==0)) mashh->absmin.x=tmp.x; if (tmp.y>mashh->absmax.y || (i==0 && mashh->boxcount==0)) mashh->absmax.y=tmp.y; if (tmp.yabsmin.y || (i==0 && mashh->boxcount==0)) mashh->absmin.y=tmp.y; if (tmp.z>mashh->absmax.z || (i==0 && mashh->boxcount==0)) mashh->absmax.z=tmp.z; if (tmp.zabsmin.z || (i==0 && mashh->boxcount==0)) mashh->absmin.z=tmp.z; } fclose(dg); pVB->Unlock(); pVB->Release(); pVB->Unlock(); pVB->Release(); */ if (max.x>mashh->absmax.x || (mashh->boxcount==0)) mashh->absmax.x=max.x; if (min.xabsmin.x || (mashh->boxcount==0)) mashh->absmin.x=min.x; if (max.y>mashh->absmax.y || (mashh->boxcount==0)) mashh->absmax.y=max.y; if (min.yabsmin.y || (mashh->boxcount==0)) mashh->absmin.y=min.y; if (max.z>mashh->absmax.z || (mashh->boxcount==0)) mashh->absmax.z=max.z; if (min.zabsmin.z || (mashh->boxcount==0)) mashh->absmin.z=min.z; mashh->boxes[mashh->boxcount].kot1.x=min.x; mashh->boxes[mashh->boxcount].kot1.y=min.y; mashh->boxes[mashh->boxcount].kot1.z=min.z; mashh->boxes[mashh->boxcount].kot2.x=max.x; mashh->boxes[mashh->boxcount].kot2.y=max.y; mashh->boxes[mashh->boxcount].kot2.z=max.z; mashh->boxcount++; return TRUE; } void MashHandler::EnumMeshes(CD3DFrame *frame,D3DXMATRIX mat) { //D3DXMatrixMultiply(&mat,&mat,(frame->GetMatrix())); D3DXMATRIX mat2=(*(frame->GetMatrix())); //D3DXMatrixMultiply(&mat2,&mat,(frame->GetMatrix())); if( frame->m_pMesh ) EnumerateMeshes( frame->m_pMesh,mat); if( frame->m_pNext ) EnumMeshes(frame->m_pNext,mat ); //D3DXMatrixIdentity(&mat); //D3DXMatrixMultiply(&mat,&mat,(frame->GetMatrix())); //mat=(*(frame->GetMatrix())); if( frame->m_pChild ) EnumMeshes(frame->m_pChild ,mat2); } PosBoxes MashHandler::getBoxes(CD3DFrame * frame,float radius) { FILE * dg=fopen("meshh.txt","a"); fprintf(dg,"prihaja ...... \n"); boxcount=0; absmin=D3DXVECTOR3(0,0,0); absmax=D3DXVECTOR3(0,0,0); fclose(dg); //frame->EnumMeshes(MashHandler::EnumerateMeshes,this); D3DXMATRIX mat;//=*(frame->GetMatrix()); D3DXMatrixIdentity(&mat); EnumMeshes(frame,mat); dg=fopen("meshh.txt","a"); D3DXVECTOR3 mid=(absmin+absmax)*0.5f; D3DXVECTOR3 box=absmax-absmin; D3DXVECTOR3 rs=box*0.5f; float fRadius=D3DXVec3Length(&rs); float factor=0.0f; if (fRadius!=0.0f) factor=radius/fRadius; PosBoxes ven; ven.boxcnt=boxcount; fprintf(dg,"prihaja ...... izmerjeni rad=%f zeljeni=%f faktor=%f\n",fRadius,radius,factor); fprintf(dg,"absmin absmax kot1.x=%f kot1.y=%f kot1.z=%f kot2.x=%f kot2.y=%f kot3.z=%f\n", absmin.x, absmin.y, absmin.z, absmax.x, absmax.y, absmax.z); fprintf(dg,"odstevam kot1.x=%f kot1.y=%f kot1.z=%f\n", mid.x, mid.y, mid.z ); for (int i=0;iGetVertexBuffer( &pVB ); dwNumVertices = mesh->GetNumVertices(); pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ); //get min and max for( DWORD i=0; imax.x || i==0) max.x=pVertices[i].p.x; if (pVertices[i].p.xmax.y || i==0) max.y=pVertices[i].p.y; if (pVertices[i].p.ymax.z || i==0) max.z=pVertices[i].p.z; if (pVertices[i].p.zGetFVF(); //get center mid=(min+max)*0.5f; //get box box=max-min; //and "radius" of box D3DXVECTOR3 rs=box*0.5f; //get factor float fRadius=D3DXVec3Length(&rs); float factor=0.0f; if (fRadius!=0.0f) factor=radious/fRadius; //now hadle vericles for( DWORD i=0; iUnlock(); pVB->Release(); /* D3DXVECTOR3 min,max; FLOAT fRadius; LPDIRECT3DVERTEXBUFFER8 pVB; DWORD dwNumVertices; MESHVERTEX* pVertices; D3DXVECTOR3 vCenter; mesh->GetVertexBuffer( &pVB ); dwNumVertices = mesh->GetNumVertices(); pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ); D3DXComputeBoundingSphere( pVertices, dwNumVertices, fvf, &vCenter, &fRadius ); float fac=radious/fRadius; for( DWORD i=0; imax.x) max.x=pVertices[i].p.x; if (pVertices[i].p.xmax.y) max.y=pVertices[i].p.y; if (pVertices[i].p.ymax.z) max.z=pVertices[i].p.z; if (pVertices[i].p.zUnlock(); pVB->Release(); */ //*radOut=fRadius; } CollisionModel * MashHandler::Build_Opcode_Model_From_Mesh(ID3DXMesh * mesh) { OPCODECREATE OPCC; CollisionModel *out=new CollisionModel(); DWORD dwFVF = mesh->GetFVF(); struct MESHVERTEX { D3DXVECTOR3 p, n; FLOAT tu, tv; }; MESHVERTEX* pVertices; WORD* pIndices; // Lock the geometry buffers mesh->LockVertexBuffer( 0L, (BYTE**)&pVertices ); mesh->LockIndexBuffer( 0L, (BYTE**)&pIndices ); DWORD dwNumVertices = mesh->GetNumVertices(); DWORD dwNumFaces = mesh->GetNumFaces(); // For each face // Surface data //Construct tree model for collision OPCC.NbTris = dwNumFaces; OPCC.NbVerts = dwNumVertices; udword *ind=(udword*)calloc(dwNumFaces*3,sizeof(udword)); for (unsigned int i=0;iBuild(OPCC); if (!Status) exit(0); free(ind); free(pnt); //forget now everything temporary //triangles /* Point * trs=(Point *) calloc(dwNumFaces*3,sizeof(VertexPointers)); for (unsigned int i=0;iopm=mdl; out->triangles=trs; */ //points out->opm=mdl; Point *pnts=(Point *) calloc(dwNumVertices,sizeof(Point)); for (unsigned int i=0;ivertes=pnts; //triangles IndexedTriangle * ita=(IndexedTriangle*) calloc(dwNumFaces,sizeof(IndexedTriangle)); for (unsigned int i=0;ifaces=ita; mesh->UnlockVertexBuffer(); mesh->UnlockIndexBuffer(); return out; } /* CollisionModel3D * MashHandler::Build_ColDet_Model_From_Mesh(ID3DXMesh * mesh) { CollisionModel3D * m_CollModel = newCollisionModel3D(false); DWORD dwFVF = mesh->GetFVF(); if (dwFVF & D3DFVF_TEX1) { struct MESHVERTEX { D3DXVECTOR3 p, n; FLOAT tu, tv; }; MESHVERTEX* pVertices; WORD* pIndices; // Lock the geometry buffers mesh->LockVertexBuffer( 0L, (BYTE**)&pVertices ); mesh->LockIndexBuffer( 0L, (BYTE**)&pIndices ); DWORD dwNumVertices = mesh->GetNumVertices(); DWORD dwNumFaces = mesh->GetNumFaces(); // For each face for( DWORD i=0; iaddTriangle(va,vb,vc); } // Unlock the geometry buffers mesh->UnlockVertexBuffer(); mesh->UnlockIndexBuffer(); } else { struct MESHVERTEX { D3DXVECTOR3 p, n; }; MESHVERTEX* pVertices; WORD* pIndices; // Lock the geometry buffers mesh->LockVertexBuffer( 0L, (BYTE**)&pVertices ); mesh->LockIndexBuffer( 0L, (BYTE**)&pIndices ); DWORD dwNumVertices = mesh->GetNumVertices(); DWORD dwNumFaces = mesh->GetNumFaces(); // For each face for( DWORD i=0; iaddTriangle(va,vb,vc); } // Unlock the geometry buffers mesh->UnlockVertexBuffer(); mesh->UnlockIndexBuffer(); } m_CollModel->finalize(); return m_CollModel; }*/

Share this post


Link to post
Share on other sites
here it comes


  
#ifndef MESHHANDLER_H
#define MESHHANDLER_H

#define D3D_OVERLOADS
#include <d3d8.h>
#include <D3dx8math.h>
#include "D3DFile.h"
//#include "coldet.h"
#include <ode/ode.h>
#include <string>
#include <opcode.h>
#include <OPC_model.h>


using namespace std;
using namespace Opcode;
using namespace IceMaths;

#define D3DFVF_VERTEXSL (D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1)
#define D3DFVF_VERTEXSL1 (D3DFVF_XYZ|D3DFVF_NORMAL)



struct MODELVERTEX
{
D3DXVECTOR3 p;
D3DXVECTOR3 n;
FLOAT tu, tv;
};

struct MODELVERTEXSIMPLE
{
D3DXVECTOR3 p;
D3DXVECTOR3 n;
};

struct MODELVERTEXREALSIMPLE
{
D3DXVECTOR3 p;
};

class CollisionModel
{
public:
CollisionModel();
~CollisionModel();
OPCODE_Model * opm;
IndexedTriangle * faces;
Point * vertes;
};


struct PosBox
{
D3DXVECTOR3 kot1;
D3DXVECTOR3 kot2;
};

struct PosBoxes
{
PosBox box[1000];
int boxcnt;
};

class MeshHolder
{
public:
string name;
string xfiledefault;
string xfilesmall;
double size;
CD3DMesh * defaultMesh;
CD3DMesh * smallMesh;
double line;
D3DXVECTOR3 box;
PosBoxes boxes;
CollisionModel * collModel;
};


class MashHandler
{
int boxcount;
PosBox boxes[1000];
D3DXVECTOR3 absmin;
D3DXVECTOR3 absmax;
public:
IDirect3DDevice8 * device;
BOOL EnumerateMeshes (CD3DMesh* mesh,D3DXMATRIX mat);
BOOL EnumerateMeshes2 (CD3DMesh* mesh,D3DXMATRIX mat);
void EnumMeshes(CD3DFrame *frame,D3DXMATRIX mat);
PosBoxes getBoxes(CD3DFrame * frame,float radius);
void fixSize(ID3DXMesh * mesh,float radious,DWORD fvf,D3DXVECTOR3 &box);
CollisionModel * Build_Opcode_Model_From_Mesh(ID3DXMesh * mesh);
};


#endif



and cpp


  

#include "MashHandler.h"

CollisionModel::CollisionModel()
{
opm=NULL;
faces=NULL;
vertes=NULL;
}
CollisionModel::~CollisionModel()
{
if (opm) delete opm;
if (faces) delete faces;
if (vertes) delete vertes;
}

BOOL MashHandler::EnumerateMeshes (CD3DMesh* cm,D3DXMATRIX mat)
{
MashHandler * mashh=this;
D3DXVECTOR3 min,max,mid;
ID3DXMesh * mesh=cm->GetSysMemMesh();
DWORD fvfsize = D3DXGetFVFVertexSize(mesh->GetFVF());
BYTE *pVertex;
DWORD dwNumVertices = mesh->GetNumVertices();
mesh->LockVertexBuffer(D3DLOCK_READONLY, (BYTE**)&pVertex);
for( DWORD i=0; i<dwNumVertices; i++ )
{
DWORD off=i*fvfsize;

D3DXVECTOR3 tmp=*((D3DXVECTOR3*)(pVertex+off));
D3DXVec3TransformCoord(&tmp,&tmp,&mat);
if (fabs(tmp.x)<0.01f) tmp.x=0.00f;
if (fabs(tmp.y)<0.01f) tmp.y=0.00f;
if (fabs(tmp.z)<0.01f) tmp.z=0.00f;
if (tmp.x>max.x || i==0) max.x=tmp.x;
if (tmp.x<min.x || i==0) min.x=tmp.x;
if (tmp.y>max.y || i==0) max.y=tmp.y;
if (tmp.y<min.y || i==0) min.y=tmp.y;
if (tmp.z>max.z || i==0) max.z=tmp.z;
if (tmp.z<min.z || i==0) min.z=tmp.z;

if (tmp.x>mashh->absmax.x || (i==0 && mashh->boxcount==0)) mashh->absmax.x=tmp.x;
if (tmp.x<mashh->absmin.x || (i==0 && mashh->boxcount==0)) mashh->absmin.x=tmp.x;
if (tmp.y>mashh->absmax.y || (i==0 && mashh->boxcount==0)) mashh->absmax.y=tmp.y;
if (tmp.y<mashh->absmin.y || (i==0 && mashh->boxcount==0)) mashh->absmin.y=tmp.y;
if (tmp.z>mashh->absmax.z || (i==0 && mashh->boxcount==0)) mashh->absmax.z=tmp.z;
if (tmp.z<mashh->absmin.z || (i==0 && mashh->boxcount==0)) mashh->absmin.z=tmp.z;

}
if (max.x>mashh->absmax.x || (mashh->boxcount==0)) mashh->absmax.x=max.x;
if (min.x<mashh->absmin.x || (mashh->boxcount==0)) mashh->absmin.x=min.x;
if (max.y>mashh->absmax.y || (mashh->boxcount==0)) mashh->absmax.y=max.y;
if (min.y<mashh->absmin.y || (mashh->boxcount==0)) mashh->absmin.y=min.y;
if (max.z>mashh->absmax.z || (mashh->boxcount==0)) mashh->absmax.z=max.z;
if (min.z<mashh->absmin.z || (mashh->boxcount==0)) mashh->absmin.z=min.z;

mashh->boxes[mashh->boxcount].kot1.x=min.x;
mashh->boxes[mashh->boxcount].kot1.y=min.y;
mashh->boxes[mashh->boxcount].kot1.z=min.z;
mashh->boxes[mashh->boxcount].kot2.x=max.x;
mashh->boxes[mashh->boxcount].kot2.y=max.y;
mashh->boxes[mashh->boxcount].kot2.z=max.z;
mashh->boxcount++;

mesh->UnlockVertexBuffer();
return TRUE;
}

BOOL MashHandler::EnumerateMeshes2 (CD3DMesh* cm,D3DXMATRIX mat)
{

MashHandler * mashh=this;
D3DXVECTOR3 min,max,mid;
// cm->SetFVF(mashh->device,D3DFVF_XYZ|D3DFVF_NORMAL|D3DFVF_TEX1);

ID3DXMesh * m_pMesh=cm->GetSysMemMesh();
// DWORD flag=mesh->GetFVF();

BYTE *pVertex;
m_pMesh->LockVertexBuffer(D3DLOCK_READONLY, (BYTE**)&pVertex);
D3DXComputeBoundingBox((VOID*)pVertex, m_pMesh->GetNumVertices(),D3DFVF_XYZ|D3DFVF_NORMAL/* m_pMesh->GetFVF()*/, &min, &max);
m_pMesh->UnlockVertexBuffer();
D3DXVec3TransformCoord(&min,&min,&mat);
D3DXVec3TransformCoord(&max,&max,&mat);
if (max.x>mashh->absmax.x || (mashh->boxcount==0)) mashh->absmax.x=max.x;
if (min.x<mashh->absmin.x || (mashh->boxcount==0)) mashh->absmin.x=min.x;
if (max.y>mashh->absmax.y || (mashh->boxcount==0)) mashh->absmax.y=max.y;
if (min.y<mashh->absmin.y || (mashh->boxcount==0)) mashh->absmin.y=min.y;
if (max.z>mashh->absmax.z || (mashh->boxcount==0)) mashh->absmax.z=max.z;
if (min.z<mashh->absmin.z || (mashh->boxcount==0)) mashh->absmin.z=min.z;

mashh->boxes[mashh->boxcount].kot1.x=min.x;
mashh->boxes[mashh->boxcount].kot1.y=min.y;
mashh->boxes[mashh->boxcount].kot1.z=min.z;
mashh->boxes[mashh->boxcount].kot2.x=max.x;
mashh->boxes[mashh->boxcount].kot2.y=max.y;
mashh->boxes[mashh->boxcount].kot2.z=max.z;
mashh->boxcount++;

return TRUE;
}

void MashHandler::EnumMeshes(CD3DFrame *frame,D3DXMATRIX mat)
{

D3DXMATRIX matSavedWorld, matWorld;
matSavedWorld = mat;
D3DXMatrixMultiply( &matWorld,&matSavedWorld, (frame->GetMatrix()) );


if( frame->m_pMesh ) EnumerateMeshes( frame->m_pMesh,matWorld);
if( frame->m_pChild ) EnumMeshes(frame->m_pChild ,matWorld);
if( frame->m_pNext ) EnumMeshes(frame->m_pNext,matSavedWorld );



}
PosBoxes MashHandler::getBoxes(CD3DFrame * frame,float radius)
{
FILE * dg=fopen("meshh.txt","a");
fprintf(dg,"prihaja ...... \n");
boxcount=0;
absmin=D3DXVECTOR3(0,0,0);
absmax=D3DXVECTOR3(0,0,0);
fclose(dg);


//frame->EnumMeshes(MashHandler::EnumerateMeshes,this);

D3DXMATRIX mat;//=*(frame->GetMatrix());

D3DXMatrixIdentity(&mat);
EnumMeshes(frame,mat);
dg=fopen("meshh.txt","a");
D3DXVECTOR3 mid=(absmin+absmax)*0.5f;
D3DXVECTOR3 box=absmax-absmin;
D3DXVECTOR3 rs=box*0.5f;
float fRadius=D3DXVec3Length(&rs);
float factor=0.0f;
if (fRadius!=0.0f) factor=radius/fRadius;
PosBoxes ven;
ven.boxcnt=boxcount;
fprintf(dg,"prihaja ...... izmerjeni rad=%f zeljeni=%f faktor=%f\n",fRadius,radius,factor);
fprintf(dg,"absmin absmax kot1.x=%f kot1.y=%f kot1.z=%f kot2.x=%f kot2.y=%f kot3.z=%f\n",
absmin.x,
absmin.y,
absmin.z,
absmax.x,
absmax.y,
absmax.z);
fprintf(dg,"odstevam kot1.x=%f kot1.y=%f kot1.z=%f\n",
mid.x,
mid.y,
mid.z
);

for (int i=0;i<boxcount;i++)
{


boxes[i].kot1-=mid;
boxes[i].kot1*=factor;
boxes[i].kot2-=mid;
boxes[i].kot2*=factor;


ven.box[i].kot1=boxes[i].kot1;
ven.box[i].kot2=boxes[i].kot2;
}

fclose(dg);
return ven;
}

void MashHandler::fixSize(ID3DXMesh * mesh,float radious,DWORD fvf,D3DXVECTOR3 &box)
{
struct MESHVERTEX
{
D3DXVECTOR3 p;
D3DXVECTOR3 n;
FLOAT tu, tv;
};
D3DXVECTOR3 min,max,mid;
//FLOAT fRadius;


LPDIRECT3DVERTEXBUFFER8 pVB;
DWORD dwNumVertices;
MESHVERTEX* pVertices;
mesh->GetVertexBuffer( &pVB );
dwNumVertices = mesh->GetNumVertices();
pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 );
//get min and max

for( DWORD i=0; i<dwNumVertices; i++ )
{
if (pVertices[i].p.x>max.x || i==0) max.x=pVertices[i].p.x;
if (pVertices[i].p.x<min.x || i==0) min.x=pVertices[i].p.x;
if (pVertices[i].p.y>max.y || i==0) max.y=pVertices[i].p.y;
if (pVertices[i].p.y<min.y || i==0) min.y=pVertices[i].p.y;
if (pVertices[i].p.z>max.z || i==0) max.z=pVertices[i].p.z;
if (pVertices[i].p.z<min.z || i==0) min.z=pVertices[i].p.z;
}


DWORD flag=mesh->GetFVF();





//get center

mid=(min+max)*0.5f;
//get box

box=max-min;
//and "radius" of box

D3DXVECTOR3 rs=box*0.5f;
//get factor

float fRadius=D3DXVec3Length(&rs);
float factor=0.0f;
if (fRadius!=0.0f) factor=radious/fRadius;
//now hadle vericles

for( DWORD i=0; i<dwNumVertices; i++ )
{
pVertices[i].p-=mid;
pVertices[i].p*=factor;
}
box*=factor;
pVB->Unlock();
pVB->Release();

}

CollisionModel * MashHandler::Build_Opcode_Model_From_Mesh(ID3DXMesh * mesh)
{
OPCODECREATE OPCC;
CollisionModel *out=new CollisionModel();
DWORD dwFVF = mesh->GetFVF();
struct MESHVERTEX { D3DXVECTOR3 p, n; FLOAT tu, tv; };
MESHVERTEX* pVertices;
WORD* pIndices;
// Lock the geometry buffers

mesh->LockVertexBuffer( 0L, (BYTE**)&pVertices );
mesh->LockIndexBuffer( 0L, (BYTE**)&pIndices );
DWORD dwNumVertices = mesh->GetNumVertices();
DWORD dwNumFaces = mesh->GetNumFaces();
// For each face

// Surface data


//Construct tree model for collision

OPCC.NbTris = dwNumFaces;
OPCC.NbVerts = dwNumVertices;
udword *ind=(udword*)calloc(dwNumFaces*3,sizeof(udword));
for (unsigned int i=0;i<dwNumFaces*3;i++) ind[i]=pIndices[i];
Point * pnt=(Point *) calloc(dwNumVertices,sizeof(Point));
for (unsigned int i=0;i<dwNumVertices;i++)
{
pnt[i].x=pVertices[i].p.x;
pnt[i].z=pVertices[i].p.z;
pnt[i].y=pVertices[i].p.y;
}
OPCC.Tris = ind;
OPCC.Verts = pnt;
OPCC.Rules = SPLIT_COMPLETE | SPLIT_SPLATTERPOINTS | SPLIT_GEOMCENTER;;
OPCC.NoLeaf = true;
OPCC.Quantized = true;
OPCC.KeepOriginal = false;
OPCODE_Model * mdl=new OPCODE_Model();
bool Status = mdl->Build(OPCC);
if (!Status) exit(0);
free(ind);
free(pnt);
//forget now everything temporary

//triangles

//points

out->opm=mdl;
Point *pnts=(Point *) calloc(dwNumVertices,sizeof(Point));
for (unsigned int i=0;i<dwNumVertices;i++)
{
pnts[i].x=pVertices[i].p.x;
pnts[i].z=pVertices[i].p.z;
pnts[i].y=pVertices[i].p.y;
}
out->vertes=pnts;
//triangles

IndexedTriangle * ita=(IndexedTriangle*) calloc(dwNumFaces,sizeof(IndexedTriangle));
for (unsigned int i=0;i<dwNumFaces;i++)
{
//IndexedTriangle it(pIndices[i*3+0],pIndices[i*3+1],pIndices[i*3+2]);

ita[i].mVRef[0]=pIndices[i*3+0];
ita[i].mVRef[1]=pIndices[i*3+1];
ita[i].mVRef[2]=pIndices[i*3+2];
}
out->faces=ita;
mesh->UnlockVertexBuffer();
mesh->UnlockIndexBuffer();
return out;
}

Share this post


Link to post
Share on other sites