static void OnReconstructMainMeshCallBack (NewtonBody* const body, NewtonFracturedCompoundMeshPart* const mainMesh, const NewtonCollision* const fracturedCompoundCollision) { DemoEntity* const entity = (DemoEntity*)NewtonBodyGetUserData(body); DemoMesh* const visualMesh = (DemoMesh*)entity->GetMesh(); dAssert (visualMesh->IsType(DemoMesh::GetRttiType())); dAssert (NewtonCollisionGetType(fracturedCompoundCollision) == SERIALIZE_ID_FRACTURED_COMPOUND); DemoEntityManager* const scene = (DemoEntityManager*)NewtonWorldGetUserData(NewtonBodyGetWorld(body)); dAssert (scene); visualMesh->RemoveAll(); for (void* segment = NewtonFracturedCompoundMeshPartGetFirstSegment(mainMesh); segment; segment = NewtonFracturedCompoundMeshPartGetNextSegment (segment)) { DemoSubMesh* const subMesh = visualMesh->AddSubMesh(); int material = NewtonFracturedCompoundMeshPartGetMaterial (segment); int indexCount = NewtonFracturedCompoundMeshPartGetIndexCount (segment); subMesh->m_textureHandle = AddTextureRef ((GLuint)material); subMesh->m_shader = scene->GetShaderCache().m_diffuseEffect; subMesh->AllocIndexData (indexCount); subMesh->m_indexCount = NewtonFracturedCompoundMeshPartGetIndexStream (fracturedCompoundCollision, mainMesh, segment, (int*)subMesh->m_indexes); } visualMesh->OptimizeForRender(); }
DemoMesh::DemoMesh(const DemoMesh& mesh) :DemoMeshInterface() ,dList<DemoSubMesh>() ,m_uv(NULL) ,m_vertex(NULL) ,m_normal(NULL) ,m_optimizedOpaqueDiplayList(0) ,m_optimizedTransparentDiplayList(0) { AllocVertexData(mesh.m_vertexCount); memcpy (m_vertex, mesh.m_vertex, 3 * m_vertexCount * sizeof (dFloat)); memcpy (m_normal, mesh.m_normal, 3 * m_vertexCount * sizeof (dFloat)); memcpy (m_uv, mesh.m_uv, 2 * m_vertexCount * sizeof (dFloat)); for (dListNode* nodes = mesh.GetFirst(); nodes; nodes = nodes->GetNext()) { DemoSubMesh* const segment = AddSubMesh(); DemoSubMesh& srcSegment = nodes->GetInfo(); segment->AllocIndexData (srcSegment.m_indexCount); memcpy (segment->m_indexes, srcSegment.m_indexes, srcSegment.m_indexCount * sizeof (unsigned)); segment->m_shiness = srcSegment.m_shiness; segment->m_ambient = srcSegment.m_ambient; segment->m_diffuse = srcSegment.m_diffuse; segment->m_specular = srcSegment.m_specular; segment->m_textureHandle = srcSegment.m_textureHandle; segment->m_textureName = srcSegment.m_textureName; if (segment->m_textureHandle) { AddTextureRef (srcSegment.m_textureHandle); } } // see if this mesh can be optimized OptimizeForRender (); }
DTexture* GManager::TextureAcquire( SFullName Filename, bool bAddRef, bool bMipGenerate ) { if( Filename.Name==NONE ) { return NULL; } SString str = (const char*)*Filename.Name; str = str.LowerCase(); Filename.Name = SName( (const char*)str ); DTexture *pTexture; dword dwIndex=0; if( (dwIndex=FindTextureFileName( Filename ))==0 ) { // neo creation DFile *pFile = DStdFileCreate(); if( !pFile->Open( Filename )) { DLogWriteSystem( "XBaseResourceAcquire::TEXTURE can't found [%s] file", (lpctstr)(*Filename) ); pFile->Close(); DStdFileRelease( pFile ); return NULL; } pTexture = CreateTexture( &dwIndex ); m_Texture.SetObjectName( dwIndex, Filename ); if( !pTexture->Load( pFile, bMipGenerate ) ) { DLogWriteSystem( "XBaseResourceAcquire::TEXTURE can't load [%s] file", (lpctstr)(*Filename) ); pFile->Close(); DStdFileRelease( pFile ); return NULL; } pFile->Close(); DStdFileRelease( pFile ); } else { // just get pointer and return pTexture = GetTexture( dwIndex ); if( bAddRef ) AddTextureRef( dwIndex ); } return pTexture; }
DTexture* GManager::TextureAcquire( SString strFilename, bool bAddRef, bool bMipGenerate ) { if( strFilename.IsEmpty() ) { return NULL; } strFilename = strFilename.LowerCase(); DTexture *pTexture; dword dwIndex=0; if( (dwIndex=FindTextureName( DCutPath(strFilename) ))==0 ) { // neo creation DFile File; if(!File.Open(strFilename)) { SString strFileOnly = DCutPath( strFilename ); strFilename = DGetParentDir( strFilename, 2 ); strFilename += "png\\"; strFilename += strFileOnly; } if( !File.Open( strFilename )) { File.Close(); return NULL; } pTexture = CreateTexture(); pTexture->SetName( DCutPath(strFilename) ); if( !pTexture->Load( &File, bMipGenerate ) ) { DLogWriteSystem( "XBaseResourceAcquire::TEXTURE can't load [%s] file", strFilename ); File.Close(); return NULL; } File.Close(); } else { // just get pointer and return pTexture = GetTexture( dwIndex ); if( bAddRef ) AddTextureRef( dwIndex ); } //DLogWriteSystem( "Out(1) XBaseResourceAcquire" ); return pTexture; }
void DemoMesh::SpliteSegment(dListNode* const node, int maxIndexCount) { const DemoSubMesh& segment = node->GetInfo(); if (segment.m_indexCount > maxIndexCount) { dVector minBox (1.0e10f, 1.0e10f, 1.0e10f, 0.0f); dVector maxBox (-1.0e10f, -1.0e10f, -1.0e10f, 0.0f); for (int i = 0; i < segment.m_indexCount; i ++) { int index = segment.m_indexes[i]; for (int j = 0; j < 3; j ++) { minBox[j] = (m_vertex[index * 3 + j] < minBox[j]) ? m_vertex[index * 3 + j] : minBox[j]; maxBox[j] = (m_vertex[index * 3 + j] > maxBox[j]) ? m_vertex[index * 3 + j] : maxBox[j]; } } int index = 0; dFloat maxExtend = -1.0e10f; for (int j = 0; j < 3; j ++) { dFloat ext = maxBox[j] - minBox[j]; if (ext > maxExtend ) { index = j; maxExtend = ext; } } int leftCount = 0; int rightCount = 0; dFloat spliteDist = (maxBox[index ] + minBox[index]) * 0.5f; for (int i = 0; i < segment.m_indexCount; i += 3) { bool isleft = true; for (int j = 0; j < 3; j ++) { int vertexIndex = segment.m_indexes[i + j]; isleft &= (m_vertex[vertexIndex * 3 + index] < spliteDist); } if (isleft) { leftCount += 3; } else { rightCount += 3; } } dAssert (leftCount); dAssert (rightCount); dListNode* const leftNode = Append(); dListNode* const rightNode = Append(); DemoSubMesh* const leftSubMesh = &leftNode->GetInfo(); DemoSubMesh* const rightSubMesh = &rightNode->GetInfo(); leftSubMesh->AllocIndexData (leftCount); rightSubMesh->AllocIndexData (rightCount); leftSubMesh->m_textureHandle = AddTextureRef (segment.m_textureHandle); rightSubMesh->m_textureHandle = AddTextureRef (segment.m_textureHandle); leftSubMesh->m_textureName = segment.m_textureName; rightSubMesh->m_textureName = segment.m_textureName; leftCount = 0; rightCount = 0; for (int i = 0; i < segment.m_indexCount; i += 3) { bool isleft = true; for (int j = 0; j < 3; j ++) { int vertexIndex = segment.m_indexes[i + j]; isleft &= (m_vertex[vertexIndex * 3 + index] < spliteDist); } if (isleft) { leftSubMesh->m_indexes[leftCount + 0] = segment.m_indexes[i + 0]; leftSubMesh->m_indexes[leftCount + 1] = segment.m_indexes[i + 1]; leftSubMesh->m_indexes[leftCount + 2] = segment.m_indexes[i + 2]; leftCount += 3; } else { rightSubMesh->m_indexes[rightCount + 0] = segment.m_indexes[i + 0]; rightSubMesh->m_indexes[rightCount + 1] = segment.m_indexes[i + 1]; rightSubMesh->m_indexes[rightCount + 2] = segment.m_indexes[i + 2]; rightCount += 3; } } SpliteSegment(leftNode, maxIndexCount); SpliteSegment(rightNode, maxIndexCount); Remove(node); } }