further updates because i know i've talked to people who will be looking here later but i've made significant changes to my code:
got tons of progress now, i can pretty much see the whole level through the geometry drawer (which i changed from lines to solid triangles, lets me see more clearly what is happening) but its still not quite right. Either the for each poly function misses polies or the Add Face command is failing to add some of my faces because a few choice faces are missing in the geomtry. i cant see a pattern, there are just some that arnt drawn. If, instead of a loaded model, I run this on a simple cube made with dbCreateObjectCube() then it only draws half the cube! It draws it all if i draw directly from the face add code though so i know i'm parsing all the faces correctly.. anyway, updated code (all of it this time):
#include "DarkGDK.h"
#include "Newton.h"
#include "CNewton.h"
#include "Vector.h"
#include "Matrix.h"
//#define dbNewtMalloc(n) malloc(n)
//#define dbNewtFree(n) free(n)
void *dbNewtMalloc(int n) { return malloc(n); }
void dbNewtFree(void *p, int s) { free(p); }
int lines = 100;
int thisline = 0;
int numpoly = 0;
CNewton::CNewton() {
newtonWorld = NULL;
newtonTreeCol = NULL;
newtonWorld = NewtonCreate(dbNewtMalloc, dbNewtFree);
int groupID = NewtonMaterialGetDefaultGroupID(newtonWorld);
NewtonMaterialSetDefaultFriction(newtonWorld, groupID, groupID, 1.0f, 0.9f);
bInitialised = true;
}
CNewton::~CNewton() {
NewtonDestroy(newtonWorld);
}
int facesAdded = 0;
int CNewton::LoadStaticWorld(int nObjectID, char *szObjectFile) {
nStaticWorldID = nObjectID;
dbLoadObject(szObjectFile, nObjectID);
//dbMakeObjectCube(nObjectID, 100);
dbMakeMeshFromObject(1, nObjectID);
newtonTreeCol = NewtonCreateTreeCollision(newtonWorld, NULL);
NewtonTreeCollisionBeginBuild(newtonTreeCol);
dbLockVertexDataForMesh(1);
int nIndices = dbGetVertexDataIndexCount();
dVector facePoints[3];
for (int i = 0; i < nIndices; i += 3) {
facePoints[0].m_x = dbGetVertexDataPositionX(dbGetIndexData(i));
facePoints[0].m_y = dbGetVertexDataPositionY(dbGetIndexData(i));
facePoints[0].m_z = dbGetVertexDataPositionZ(dbGetIndexData(i));
facePoints[1].m_x = dbGetVertexDataPositionX(dbGetIndexData(i + 1));
facePoints[1].m_y = dbGetVertexDataPositionY(dbGetIndexData(i + 1));
facePoints[1].m_z = dbGetVertexDataPositionZ(dbGetIndexData(i + 1));
facePoints[2].m_x = dbGetVertexDataPositionX(dbGetIndexData(i + 2));
facePoints[2].m_y = dbGetVertexDataPositionY(dbGetIndexData(i + 2));
facePoints[2].m_z = dbGetVertexDataPositionZ(dbGetIndexData(i + 2));
//dbMakeObjectTriangle(lines + thisline, facePoints[0].m_x, facePoints[0].m_y, facePoints[0].m_z, facePoints[1].m_x, facePoints[1].m_y, facePoints[1].m_z, facePoints[2].m_x, facePoints[2].m_y, facePoints[2].m_z);
//dbColorObject(lines+thisline, dbRgb(dbRnd(255), dbRnd(255), dbRnd(255)));
//thisline++;
facesAdded++;
NewtonTreeCollisionAddFace(newtonTreeCol, 3, &facePoints[0].m_x, sizeof(dVector), 0);
}
dbUnlockVertexData();
dbDeleteMesh(1);
NewtonTreeCollisionEndBuild(newtonTreeCol, 0);
newtonWorldBody = NewtonCreateBody(newtonWorld, newtonTreeCol);
dMatrix m(GetIdentityMatrix());
NewtonBodySetMatrix(newtonWorldBody, &m[0][0]);
dVector boxP0;
dVector boxP1;
NewtonCollisionCalculateAABB(newtonTreeCol, &m[0][0], &boxP0.m_x, &boxP1.m_x);
// add some extra padding the world size
boxP0.m_x -= 10.0f;
boxP0.m_y -= 10.0f;
boxP0.m_z -= 10.0f;
boxP1.m_x += 10.0f;
boxP1.m_y += 10.0f;
boxP1.m_z += 10.0f;
// set the world size
NewtonSetWorldSize(newtonWorld, &boxP0.m_x, &boxP1.m_x);
NewtonReleaseCollision(newtonWorld, newtonTreeCol);
return 0;
}
int CNewton::LoadDynamicObject(int nObjectID, char *szObjectFile) {
dbMakeObjectCube(nObjectID, 100);
}
int getlines() {
return thisline;
}
void DebugShowGeometryCollision (const NewtonBody* body, int vertexCount, const dFloat* faceVertec, int id);
void DebugShowBodyCollision (const NewtonBody* body);
void DebugShowCollision (NewtonWorld* nWorld) {
NewtonWorldForEachBodyDo (nWorld, DebugShowBodyCollision);
}
void DebugShowBodyCollision (const NewtonBody* body) {
NewtonBodyForEachPolygonDo (body, DebugShowGeometryCollision);
}
void DebugShowGeometryCollision (const NewtonBody* body, int vertexCount, const dFloat* faceVertec, int id) {
int i;
numpoly++; //just use this to try and keep track of how many polies it thinks it has
for (i = 0; i < vertexCount; i+=8) {
dbMakeObjectTriangle(lines + thisline, faceVertec[i], faceVertec[i+1], faceVertec[i+2], faceVertec[i+3], faceVertec[i+4], faceVertec[i+5], faceVertec[i+6], faceVertec[i+7], faceVertec[i+8]);
dbColorObject(lines+thisline, dbRgb(dbRnd(255), dbRnd(255), dbRnd(255)));
thisline++;
}
}