ModelShape::ModelShape(IGraphicsDevice *device, Model *model) : Shape(device, "model"), mModel(model) { TMeshList &meshes = model->GetMeshes(); for(int i = 0; i < meshes.size(); i++) { ModelMesh *mesh = meshes[i]; VERTEX_BUFFER_DESC desc; desc.Stride = sizeof(ModelVertex); IVertexBuffer *meshBuffer = device->CreateVertexBuffer(desc); meshBuffer->SetData(mesh->GetBuffer(), sizeof(ModelVertex) * mesh->GetTotalFaces()); mMeshVertexBuffers.push_back(meshBuffer); ModelAnimationMaterialProperties *properties = new ModelAnimationMaterialProperties(); mMeshMaterials.push_back(new Material(device, properties)); } TMaterialList &materials = mModel->GetMaterials(); for(int i = 0; i < materials.size(); i++) { ModelMaterial *material = materials[i]; Image *image = material->GetImage(MaterialTextureDiffuse, 0); ITexture *texture = device->CreateTexture(); texture->Initialize(image->GetBits(), CSize(image->GetWidth(), image->GetHeight())); mTextures.push_back(texture); } }
//LAYOUT BINOBJSB --------------------------------------------------------------------------------> //Word: nameLength //buffer: name //DWord : #vert //vector3: pos, vector3: norm, vector2: tex, DWORD tetra, Vector3 barycentricCoords : vertex //DWord : #triangles //DWord, DWord, DWord : triangle //... bool BinCooker::CookSB() { UserStream stream(m_OutputName.c_str(), false); ModelMesh<VertexPNTSoftbody>* mesh = m_pModel->GetSoftbodyDrawMesh(); //store Name stream.storeWord(static_cast<WORD>(mesh->GetName().size())); const char* name = mesh->GetName().c_str(); stream.storeBuffer(name, mesh->GetName().size()); //store Vertices stream.storeDword(mesh->GetNumVertices()); const vector<VertexPNTSoftbody>& v = mesh->GetVertices(); for (unsigned int i = 0; i < mesh->GetNumVertices(); ++i) { stream.storeVector3(v[i].position); stream.storeVector3(v[i].normal); stream.storeVector2(v[i].tex); stream.storeDword(v[i].tetra); stream.storeVector3(v[i].barycentricCoords); } //store Indices stream.storeDword(mesh->GetNumIndices() / 3); const vector<DWORD>& ind = mesh->GetIndices(); for (DWORD i = 0; i < mesh->GetNumIndices(); ++i) { stream.storeDword(ind[i]); } return true; }
void ModelShape::Draw(IGraphicsDevice *device, RenderState &state) { int animationFrame = mModel->GetCurrentAnimationFrame(); TMeshList &meshes = mModel->GetMeshes(); TBoneList &bones = mModel->GetBones(); for(int i = 0; i < meshes.size(); i++) { ModelMesh *mesh = meshes[i]; Material *material = mMeshMaterials[i]; ModelAnimationMaterialProperties *properties = dynamic_cast<ModelAnimationMaterialProperties *>(material->GetProperties()); if(properties) { int i = 0; for(int i = 0; i < bones.size(); i++) { ModelBone *bone = bones[i]; properties->SetBoneMatrix(i, bone->GetKeyframeTransform(animationFrame)); } ITexture *texture = mTextures[mesh->GetMaterial()]; properties->SetTexture(texture); } state.SetMaterial(material); ITechnique *technique = material->GetTechnique(0); technique->Begin(); while(technique->HasNextPass()) { technique->ProcessNextPass(device, state); mMeshVertexBuffers[i]->Activate(); device->Draw(PrimitiveTypeTriangleList, mesh->GetTotalFaces(), 0); } } }
void IndexedModel::initVao(){ for(unsigned int i = 0;i<meshes.size();i++){ ModelMesh* m = meshes.at(i); m->initVao(); } }
void Entity::addModel(Model *model, Material *mat) { ASSERT(model); for (int i = 0; i < model->getMeshCount(); i++) { ModelMesh *mesh = model->getMesh(i); expandLocalAABB(mesh->getBoundingBox()); _renderables.push_back(new Renderable( mesh->getRenderOperation(), mat ? mat : mesh->getDefaultMaterial())); } }
////////////////////////////////////////////////////////////////// /// 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; }
void ModelImporter_Impl::_CalculateModelCenter(ModelScene* scene) { vec3f min(9999.9f, 9999.9f, 9999.9f); vec3f max(-9999.9f, -9999.9f, -9999.9f); size_t j = 0, c; for (unsigned int i = 0; i < scene->numMeshes; ++i) { ModelMesh* mesh = &scene->Meshes[i]; auto indices = mesh->GetIndices(); vertex_attribute* vAttribs = new vertex_attribute[4]; vAttribs[0] = { LOC_POSITION, 3, GL_FLOAT, GL_FALSE, sizeof(float) * 3, 0 }; vAttribs[1] = { LOC_NORMAL, 3, GL_FLOAT, GL_TRUE, sizeof(float) * 3, sizeof(float) * mesh->numVertices * 3 }; vAttribs[2] = { LOC_TEXCOORD0, 2, GL_FLOAT, GL_TRUE, sizeof(float) * 2, sizeof(float) * mesh->numVertices * 6 }; vAttribs[3] = { LOC_TEXCOORD1, 3, GL_FLOAT, GL_TRUE, sizeof(float) * 3, sizeof(float) * mesh->numVertices * 8 }; float* verts = new float[mesh->numVertices * 11]; unsigned int idx = 0; unsigned int nVerts = mesh->numVertices; for (j = 0; j < nVerts; ++j)// FML { vec3f vert = mesh->Vertices[j]; verts[j * 3 + 0] = vert.x; verts[j * 3 + 1] = vert.y; verts[j * 3 + 2] = vert.z; vert *= scene->Scale; if (vert.x < min.x) min.x = vert.x; if (vert.y < min.y) min.y = vert.y; if (vert.z < min.z) min.z = vert.z; if (vert.x > max.x) max.x = vert.x; if (vert.y > max.y) max.y = vert.y; if (vert.z > max.z) max.z = vert.z; idx += 3; } for (c = 0; c < (int)mesh->numNormals; ++c) { verts[j * 3 + 0] = mesh->Normals[c].x; verts[j * 3 + 1] = mesh->Normals[c].y; verts[j * 3 + 2] = mesh->Normals[c].z; ++j; idx += 3; } uint32_t nTexCoordsPV = mesh->numTexCoordsPerVertex; uint32_t offs = j * 3; for(c = 0; c < nVerts; ++c) { if(nTexCoordsPV > 0) { verts[offs + (c * 2)] = mesh->TexCoords[0][c].x; verts[offs + (c * 2 + 1)] = mesh->TexCoords[0][c].y; } else { verts[offs + (c * 2)] = 0.0f; verts[offs + (c * 2 + 1)] = 0.0f; } idx += 2; ++j; } for( c = 0; c < mesh->numTangents; ++c ) { verts[idx++] = mesh->Tangents[c].x; verts[idx++] = mesh->Tangents[c].y; verts[idx++] = mesh->Tangents[c].z; ++j; } mesh->GeometryBufferId = createIndexedInterleavedArray(vAttribs, 4, verts, sizeof(float) * mesh->numVertices * 11, BUFFER_STATIC, \ indices, mesh->numIndices); delete[] verts; delete[] vAttribs; } float xSpan = max.x - min.x; xSpan *= 0.5; float centerX = min.x + xSpan; float ySpan = max.y - min.y; ySpan *= 0.5; float centerY = min.y + ySpan; float zSpan = max.z - min.z; zSpan *= 0.5; float centerZ = min.z + zSpan; scene->ModelCenter.set(centerX, centerY, centerZ); }