MaterialPtr CMaterialManager::GetDefaultMaterial(const IInputLayout *pIL) { xst_assert( pIL != xst_null, "(CMaterialManager::GetDefaultMaterial) Input layout must be set" ); //Get or create default material for this input layout DefaultMatMap::iterator Itr; //If resource not found if( XST::MapUtils::FindPlace( m_mDefaultMaterials, pIL, &Itr ) ) { //Create new material u32 uILHandle = pIL->GetHandle(); Resources::CMaterial* pMat = xst_new Resources::CMaterial( m_pShaderMgr, DEFAULT_TECHNIQUE, this, uILHandle, XST::StringUtil::EmptyAString, XST::ResourceType::MATERIAL, XST::ResourceStates::CREATED/*, xst_null*/ ); if( pMat == xst_null ) { XST_LOG_ERR( "Default material creation failed. Memory error." ); return MaterialPtr(); } //Set defaults IPass* pPass = pMat->GetCurrentTechnique()->GetPass( 0 ); pPass->SetVertexShader( pIL->GetVertexShader() ); pPass->SetPixelShader( pIL->GetPixelShader() ); //Add material to the buffer MaterialPtr pMaterial( pMat ); XST::MapUtils::InsertOnPlace( m_mDefaultMaterials, pIL, pMaterial, Itr ); return pMaterial; } //If resource found returns it return Itr->second; }
bool CFont::InitModel() { static CStrAny sPlain(ST_CONST, "Plain"); static CStrAny sg_mWorld(ST_CONST, "g_mWorld"); static CStrAny sg_mView(ST_CONST, "g_mView"); static CStrAny sg_mProj(ST_CONST, "g_mProj"); static CStrAny sg_mTexTransform(ST_CONST, "g_mTexTransform"); static CStrAny sg_cMaterialDiffuse(ST_CONST, "g_cMaterialDiffuse"); static CStrAny sg_txDiffuse(ST_CONST, "g_txDiffuse"); static CStrAny sg_sDiffuse(ST_CONST, "g_sDiffuse"); ASSERT(!m_pTextModel); bool bRes; CTechnique *pTech = CGraphics::Get()->GetTechnique(sPlain); if (!pTech) return false; CSmartPtr<CGeometry> pGeom(new CGeometry()); bRes = pGeom->Init(pTech->m_pInputDesc, CGeometry::PT_TRIANGLELIST, INIT_BUFFER_CHARS * 4, INIT_BUFFER_CHARS * 6, 0, 0, CResource::RF_DYNAMIC | CResource::RF_KEEPSYSTEMCOPY, CResource::RF_DYNAMIC | CResource::RF_KEEPSYSTEMCOPY); if (!bRes) return false; CSmartPtr<CMaterial> pMaterial(new CMaterial()); bRes = pMaterial->Init(pTech, true, 0); if (!bRes) return false; CSmartPtr<CModel> pModel(new CModel()); bRes = pModel->Init(pGeom, pMaterial, 0, false); if (!bRes) return false; m_pTextModel = pModel; CMatrix<4, 4> mIdentity; mIdentity.SetDiagonal(); CMatrixVar vMat(4, 4, CMatrixVar::MVF_OWNVALUES, &mIdentity(0, 0)); m_pTextModel->SetVar(sg_mProj, vMat); m_pTextModel->SetVar(sg_mView, vMat); m_pTextModel->SetVar(sg_mWorld, vMat); CMatrix<3, 2> mTexIdentity; mTexIdentity.SetDiagonal(); CMatrixVar vTexMat(3, 2, CMatrixVar::MVF_OWNVALUES, &mTexIdentity(0, 0)); m_pTextModel->SetVar(sg_mTexTransform, vTexMat); CVar<CVector<4> > vVec4; vVec4.Val().Set(1, 1, 1, 1); m_pTextModel->SetVar(sg_cMaterialDiffuse, vVec4); CVar<CTexture *> vTexVar; vTexVar.Val() = m_pTexture; m_pTextModel->SetVar(sg_txDiffuse, vTexVar); CVar<CSampler *> vSampVar; vSampVar.Val() = CGraphics::Get()->GetSampler(CSampler::TDesc()); m_pTextModel->SetVar(sg_sDiffuse, vSampVar); ResetModel(); return true; }
void LoadMeshes(FbxNode* pFbxNode, packed_freelist<Mesh>& sceneMeshes) { // Material const uint32_t materialCount = pFbxNode->GetMaterialCount(); for (uint32_t i = 0; i < materialCount; ++i) { FbxSurfaceMaterial* pFbxMaterial = pFbxNode->GetMaterial(i); if (pFbxMaterial && !pFbxMaterial->GetUserDataPtr()) { FbxAutoPtr<Material> pMaterial(new Material); if (pMaterial->init(pFbxMaterial)) { pFbxMaterial->SetUserDataPtr(pMaterial.Release()); } } } FbxNodeAttribute* nodeAttribute = pFbxNode->GetNodeAttribute(); if (nodeAttribute) { // Mesh if (nodeAttribute->GetAttributeType() == FbxNodeAttribute::eMesh) { FbxMesh* pFbxMesh = pFbxNode->GetMesh(); if (pFbxMesh && !pFbxMesh->GetUserDataPtr()) { Mesh mesh; if (mesh.init(pFbxMesh)) { sceneMeshes.insert(mesh); } // TODO: FbxAutoPtr<Mesh> pMesh(new Mesh); if (pMesh->init(pFbxMesh)) { pFbxMesh->SetUserDataPtr(pMesh.Release()); } } } // Light else if (nodeAttribute->GetAttributeType() == FbxNodeAttribute::eLight) { FbxLight* pFbxLight = pFbxNode->GetLight(); if (pFbxLight && !pFbxLight->GetUserDataPtr()) { FbxAutoPtr<Light> pLight(new Light); if (pLight->init(pFbxLight)) { pFbxLight->SetUserDataPtr(pLight.Release()); } } } } const int childCount = pFbxNode->GetChildCount(); for (int i = 0; i < childCount; ++i) { LoadMeshes(pFbxNode->GetChild(i), sceneMeshes); } }
osg::ref_ptr<osg::Node> get(const CylinderVec_t& cylinders) { osg::ref_ptr<osg::Geode> geode( new osg::Geode() ); for ( const auto& cc : cylinders ) { osg::Vec3d pp( cc.begin - cc.end ); double height( pp.length() ); if ( height < 0.000001 ) continue; osg::Vec3d center( (cc.begin.x() + cc.end.x())/2.0, (cc.begin.y() + cc.end.y())/2.0, (cc.begin.z() + cc.end.z())/2.0 ); // This is the default direction for the cylinders to face in OpenGL static const osg::Vec3d ZZ( 0,0,1 ); // Get CROSS product (the axis of rotation) osg::Vec3d tt( ZZ^pp ); // Get angle. length is magnitude of the vector double angle( acos( (ZZ * pp) / height) ); // Create a cylinder between the two points with the given radius osg::ref_ptr<osg::Cylinder> cylinder( new osg::Cylinder(center, cc.radius, height) ); cylinder->setRotation(osg::Quat(angle, osg::Vec3d(tt.x(), tt.y(), tt.z()))); // A geode to hold the cylinder osg::ref_ptr<osg::ShapeDrawable> cylinderDrawable( new osg::ShapeDrawable(cylinder) ); geode->addDrawable(cylinderDrawable); // Set the color of the cylinder that extends between the two points. osg::ref_ptr<osg::Material> pMaterial( new osg::Material() ); pMaterial->setDiffuse( osg::Material::FRONT, cc.color); geode->getOrCreateStateSet()->setAttribute( pMaterial, osg::StateAttribute::OVERRIDE ); geode->getOrCreateStateSet()->setMode(GL_BLEND, osg::StateAttribute::ON); geode->getOrCreateStateSet()->setRenderingHint(osg::StateSet::TRANSPARENT_BIN); } osg::ref_ptr<osg::Group> pAddToThisGroup(new osg::Group()); pAddToThisGroup->addChild(geode); return pAddToThisGroup; }