ModelPtr AssetLoader::LoadAnimatedModel(std::string fileName) { FILE * f = nullptr; fopen_s(&f,fileName.c_str(),"rb"); if(f != nullptr) { unsigned int number; fread_s(&number,4,4,1,f); if(number != 50) { fclose(f); return ModelPtr(true); } unsigned int meshesCount; fread_s(&meshesCount,4,4,1,f); ModelPtr ptr(Heap::GetPtr()->Models.New()); Model * model = ptr.Get(); for(unsigned int iMesh = 0; iMesh < meshesCount; iMesh++) { fread_s(&animatedMeshData.numOfVerts,4,4,1,f); fread_s(animatedMeshData.verts,sizeof(AnimatedVert) * animatedMeshData.numOfVerts,sizeof(AnimatedVert),animatedMeshData.numOfVerts,f); fread_s(&animatedMeshData.numOfIndecies,4,4,1,f); fread_s(animatedMeshData.indecies,sizeof(unsigned int) * animatedMeshData.numOfIndecies, 4 ,animatedMeshData.numOfIndecies,f); MeshPtr meshPtr = factory->CreateMesh(); Mesh * mesh = meshPtr.Get(); mesh->Init(); mesh->SetDrawMethod(DM_DRAW_INDEXED); mesh->InitVertexBuffer(animatedMeshData.numOfVerts * sizeof(AnimatedVert),sizeof(AnimatedVert),animatedMeshData.verts); mesh->InitIndexBuffer(animatedMeshData.numOfIndecies,animatedMeshData.indecies); model->AddMesh(meshPtr); } fclose(f); return ptr; } else return nullptr; }
ModelPtr AssetLoader::LoadStaticModel(std::string fileName) { std::ifstream file(fileName.c_str(),std::ios::binary); if(file.is_open()) { unsigned int number; file.read((char*)&number,sizeof(unsigned int)); if(number != 0) return ModelPtr(true); unsigned int numberOfMeshes; file.read((char*)&numberOfMeshes,4); ModelPtr ptr(Heap::GetPtr()->Models.New()); Model * model = ptr.Get(); for(unsigned int iMesh = 0; iMesh < numberOfMeshes; iMesh++) { file.read((char*)&staticMeshData.numberOfVerts,sizeof(unsigned int)); file.read((char*)staticMeshData.vertecies,sizeof(StaticVert) * staticMeshData.numberOfVerts); file.read((char*)&staticMeshData.numberOfIndecies,sizeof(unsigned int)); file.read((char*)staticMeshData.indecies,sizeof(unsigned int) * staticMeshData.numberOfIndecies); MeshPtr meshPtr = factory->CreateMesh(); Mesh * mesh = meshPtr.Get(); mesh->Init(); mesh->SetDrawMethod(DM_DRAW_INDEXED); mesh->InitVertexBuffer(staticMeshData.numberOfVerts * sizeof(StaticVert),sizeof(StaticVert),staticMeshData.vertecies); mesh->InitIndexBuffer(staticMeshData.numberOfIndecies,staticMeshData.indecies); model->AddMesh(meshPtr); } file.close(); return ptr; } else return ModelPtr(true); }
////////////////////////////////////////////////////////////////// /// methods for DebugState: /// ////////////////////////////////////////////////////////////////// DebugState::DebugState(Engine* engine) : State(engine) { mEngine->SetUpdateFrequency(30.0f); count = 0; if(mPhysics.Initialize()) { std::cout << "physics initialized\n"; } std::cout << sizeof(physx::PxVec3) << std::endl; mRootNode = new SceneObjectNode(); Engine3d::Light* light = new Light(mEngine->GetShader()->GetProgramID()); light->SetPosition(2.1f,5.0f,-18.0f); light->SetDiffuse(10,10,10); light->SetSpecular(10,10,10); light->SetConstAttenuation(.01); light->SetAmbient(0.2f,0.2f,0.2f); mGimble = new SceneObjectNode(); mRootNode->AddChild(mGimble); mGimble->AddAsset(light); CameraLocationUpdater* cam = new CameraLocationUpdater(mEngine->GetShader()->GetProgramID()); mGimble->AddDrawInterface(cam); SceneObjectNode* node = new SceneObjectNode(); ModelMesh<SimpleVertex>* mesh = Primitives::MakeBox(40,1,40); mesh->SetShader(mEngine->GetShader()); Material* mat = new Material(mEngine->GetShader()->GetProgramID()); mesh->AddAsset(mat); Model* model = new Model("plane"); model->AddMesh(mesh); DrawModel* drawmodel = new DrawModel(model); node->AddDrawInterface(drawmodel); mGimble->AddChild(node); node->SetLocalPosition(glm::vec3(2.1f,0,-18)); PhysicsCallback* physicscbplane = new PhysicsCallback("plane", node, mesh); physicscbplane->SetIsStatic(true); physicscbplane->Initialize(&mPhysics); // Model* ship = new Model("models/Armadillo/armadillo.3DS"); // ModelLoader::Load(ship, mEngine->GetShader(), &mPhysics); // Texture* texture = new Texture("models/Armadillo/armadillotex.bmp"); // texture->SetProgramID(mEngine->GetShader()->GetProgramID()); // texture->Load(); // ship->GetMesh(2)->AddAsset(mat); // ship->GetMesh(2)->AddAsset(texture); // SceneObjectNode* shipnode = new SceneObjectNode(); // DrawModel* drawship = new DrawModel(ship); // shipnode->AddDrawInterface(drawship); // mGimble->AddChild(shipnode); // shipnode->Translate(glm::vec3(5,5,-20)); // PhysicsCallback* physicscbship1 = new PhysicsCallback("ship1", shipnode, ship->GetMesh(2)); // physicscbship1->Initialize(&mPhysics); // Model* ship2 = new Model("models/Armadillo/armadillo.3DS"); // ModelLoader::Load(ship2, mEngine->GetShader(), &mPhysics); // Texture* texture2 = new Texture("models/Armadillo/armadillotex.bmp"); // texture2->SetProgramID(mEngine->GetShader()->GetProgramID()); // texture2->Load(); // ship2->GetMesh(2)->AddAsset(mat); // ship2->GetMesh(2)->AddAsset(texture2); // SceneObjectNode* shipnode2 = new SceneObjectNode(); // DrawModel* drawship2 = new DrawModel(ship2); // shipnode2->AddDrawInterface(drawship2); // mGimble->AddChild(shipnode2); // shipnode2->Translate(glm::vec3(3,5,-20)); // PhysicsCallback* physicscbship = new PhysicsCallback("ship2", shipnode2, ship2->GetMesh(2)); // physicscbship->Initialize(&mPhysics); // CreateRandomObject(); SceneObjectNode* node1 = new SceneObjectNode(); ModelMesh<SimpleVertex>* box = Primitives::MakeBox(1,1,1); Material* mat1 = new Engine3d::Material(mEngine->GetShader()->GetProgramID()); mat1->SetDiffuse(1.0f,0.0f,0.0f); mat1->SetShininess(200); box->AddAsset(mat1); box->SetShader(mEngine->GetShader()); DrawMesh* drawmesh = new DrawMesh(box); node1->AddDrawInterface(drawmesh); node1->SetLocalPosition(glm::vec3(3.0f,20.0f,-20.0f)); PhysicsCallback* phys = new PhysicsCallback("box", node1, box); phys->Initialize(&mPhysics); mGimble->AddChild(node1); for(int i = 0; i < 255; i++) mKeysDown[i] = 0; mCameraTransform = new UniformMat4("cameratransform", mEngine->GetShader()->GetProgramID()); glClearColor(0.0, 1.0f, 1.0f, 1.0f); // Clear the background of our window to whitex }
Model<VertexPosNormTanTex>* TerrainLoader::Load(ID3D10Device* pDXDevice, const tstring& key) { Model<VertexPosNormTanTex>* ret = 0; if ( m_pAssets->IsAssetPresent(key)) { ret = m_pAssets->GetAsset(key); } else { vector<VertexPosNormTanTex> vertices; vector<DWORD> indices; Texture2D* heightMap = Content->LoadTexture2D(key, true); ID3D10Texture2D* pTex2D = heightMap->GetTextureResource(); vertices.reserve(heightMap->GetWidth() * heightMap->GetHeight()); indices.reserve((heightMap->GetWidth()-1) * (heightMap->GetHeight()-1) * 6); D3D10_MAPPED_TEXTURE2D pMappedTex2D; pTex2D->Map(0, D3D10_MAP_READ, 0, &pMappedTex2D); float heightMult = 1.0f; float planarMult = 1.0f / max(heightMap->GetWidth(), heightMap->GetHeight()); BYTE* pData = static_cast<BYTE*>(pMappedTex2D.pData); for (UINT x = 0; x < heightMap->GetHeight(); ++x) { for (UINT z = 0; z < heightMap->GetWidth(); ++z) { float height = pData[z * 4 + x * pMappedTex2D.RowPitch + 0] / 255.0f; //get red vertices.push_back(VertexPosNormTanTex( static_cast<float>(x) * planarMult, height * heightMult, static_cast<float>(z) * planarMult, 0, 0, 0, 0, 0, 0, static_cast<float>(z) / heightMap->GetWidth(), static_cast<float>(x) / heightMap->GetHeight())); if (z != heightMap->GetWidth() - 1 && x != heightMap->GetHeight() - 1) { indices.push_back(z + x * heightMap->GetWidth()); indices.push_back(z + x * heightMap->GetWidth() + 1); indices.push_back(z + (x + 1) * heightMap->GetWidth()); indices.push_back(z + x * heightMap->GetWidth() + 1); indices.push_back(z + (x + 1) * heightMap->GetWidth() + 1); indices.push_back(z + (x + 1) * heightMap->GetWidth()); } } } pTex2D->Unmap(0); CalculateNormals(vertices, indices); CalculateTangents(vertices, indices); Model<VertexPosNormTanTex>* pModel = new Model<VertexPosNormTanTex>(pDXDevice); ModelMesh<VertexPosNormTanTex>* pMesh = pModel->AddMesh(_T("")); pMesh->SetVertices(vertices); pMesh->SetIndices(indices); m_pAssets->AddAsset(key, pModel); ret = pModel; } return ret; }
// public, static Model::Handle Model::Register( const char *path ) { //HACK: assume null path means raw model data if ( !path ) { Model *model = new Model(); model->handle = numRegistered++; return model->handle; } //TODO: check for duplicates? or only for mesh info etc? // for now, just add a new entry Model *model = models[path]; if ( model ) { console.Print( PrintLevel::Debug, "Loading model \"%s\" using existing model (loaded %i times)\n", XS_FUNCTION, path, model->refCount ); model->refCount++; return model->handle; } model = models[path] = new Model(); console.Print( PrintLevel::Debug, "%s loaded \"%s\" for the first time\n", XS_FUNCTION, path ); model->modelPath = path; model->handle = numRegistered++; const File f( path, FileMode::ReadBinary ); if ( !f.isOpen ) { console.Print( PrintLevel::Normal, "Failed to load model \"%s\", returning invalid handle\n", model->modelPath.c_str() ); return invalidHandle; } char *buffer = new char[f.length + 1]; { f.Read( reinterpret_cast<uint8_t *>( buffer ) ); //FIXME: dispatch to correct file reader based on extension Common::FileXMF xmf( buffer, f.length ); size_t numMeshes = xmf.GetMeshCount(); SDL_assert( numMeshes > 0u ); for ( size_t i = 0u; i < numMeshes; i++ ) { Common::XMFMesh *xmfMesh = xmf.GetMeshFromIndex( i ); if ( !xmfMesh ) { break; } /*bool computedNormals = */xmfMesh->ValidateNormals(); Mesh *rdMesh = new Mesh(); //TODO: profile copying, optimise rdMesh->vertices = xmfMesh->vertices; rdMesh->normals = xmfMesh->normals; rdMesh->UVs = xmfMesh->UVs; rdMesh->indices = xmfMesh->indices; rdMesh->CreateMaterial(); model->AddMesh( rdMesh ); } } delete[] buffer; //model->LoadMeshes(); return model->handle; }