// <Material // DEF="" ID // USE="" IDREF // ambientIntensity="0.2" SFFloat [inputOutput] // diffuseColor="0.8 0.8 0.8" SFColor [inputOutput] // emissiveColor="0 0 0" SFColor [inputOutput] // shininess="0.2" SFFloat [inputOutput] // specularColor="0 0 0" SFColor [inputOutput] // transparency="0" SFFloat [inputOutput] // /> void X3DImporter::ParseNode_Shape_Material() { std::string use, def; float ambientIntensity = 0.2f; float shininess = 0.2f; float transparency = 0; aiColor3D diffuseColor(0.8f, 0.8f, 0.8f); aiColor3D emissiveColor(0, 0, 0); aiColor3D specularColor(0, 0, 0); CX3DImporter_NodeElement* ne( nullptr ); MACRO_ATTRREAD_LOOPBEG; MACRO_ATTRREAD_CHECKUSEDEF_RET(def, use); MACRO_ATTRREAD_CHECK_RET("ambientIntensity", ambientIntensity, XML_ReadNode_GetAttrVal_AsFloat); MACRO_ATTRREAD_CHECK_RET("shininess", shininess, XML_ReadNode_GetAttrVal_AsFloat); MACRO_ATTRREAD_CHECK_RET("transparency", transparency, XML_ReadNode_GetAttrVal_AsFloat); MACRO_ATTRREAD_CHECK_REF("diffuseColor", diffuseColor, XML_ReadNode_GetAttrVal_AsCol3f); MACRO_ATTRREAD_CHECK_REF("emissiveColor", emissiveColor, XML_ReadNode_GetAttrVal_AsCol3f); MACRO_ATTRREAD_CHECK_REF("specularColor", specularColor, XML_ReadNode_GetAttrVal_AsCol3f); MACRO_ATTRREAD_LOOPEND; // if "USE" defined then find already defined element. if(!use.empty()) { MACRO_USE_CHECKANDAPPLY(def, use, ENET_Material, ne); } else { // create and if needed - define new geometry object. ne = new CX3DImporter_NodeElement_Material(NodeElement_Cur); if(!def.empty()) ne->ID = def; ((CX3DImporter_NodeElement_Material*)ne)->AmbientIntensity = ambientIntensity; ((CX3DImporter_NodeElement_Material*)ne)->Shininess = shininess; ((CX3DImporter_NodeElement_Material*)ne)->Transparency = transparency; ((CX3DImporter_NodeElement_Material*)ne)->DiffuseColor = diffuseColor; ((CX3DImporter_NodeElement_Material*)ne)->EmissiveColor = emissiveColor; ((CX3DImporter_NodeElement_Material*)ne)->SpecularColor = specularColor; // check for child nodes if(!mReader->isEmptyElement()) ParseNode_Metadata(ne, "Material"); else NodeElement_Cur->Child.push_back(ne);// add made object as child to current element NodeElement_List.push_back(ne);// add element to node element list because its a new object in graph }// if(!use.empty()) else }
MaterialData ModelLoader::loadMaterial(unsigned int index, const aiMaterial* material) { Q_ASSERT(material != nullptr); MaterialData data = MaterialData(); data.name = "material_" + to_string(index); aiColor3D ambientColor(0.1f, 0.1f, 0.1f); aiColor3D diffuseColor(0.8f, 0.8f, 0.8f); aiColor3D specularColor(0.0f, 0.0f, 0.0f); aiColor3D emissiveColor(0.0f, 0.0f, 0.0f); int blendMode; int twoSided = 1; float opacity = 1.0f; float shininess = 0.0f; float shininessStrength = 1.0f; if(material->Get(AI_MATKEY_COLOR_AMBIENT, ambientColor) == AI_SUCCESS) { data.ambientColor.setX(ambientColor.r); data.ambientColor.setY(ambientColor.g); data.ambientColor.setZ(ambientColor.b); } if(material->Get(AI_MATKEY_COLOR_DIFFUSE, diffuseColor) == AI_SUCCESS) { data.diffuseColor.setX(diffuseColor.r); data.diffuseColor.setY(diffuseColor.g); data.diffuseColor.setZ(diffuseColor.b); } if(material->Get(AI_MATKEY_COLOR_SPECULAR, specularColor) == AI_SUCCESS) { data.specularColor.setX(specularColor.r); data.specularColor.setY(specularColor.g); data.specularColor.setZ(specularColor.b); } if(material->Get(AI_MATKEY_COLOR_EMISSIVE, emissiveColor) == AI_SUCCESS) { data.emissiveColor.setX(emissiveColor.r); data.emissiveColor.setY(emissiveColor.g); data.emissiveColor.setZ(emissiveColor.b); } if(material->Get(AI_MATKEY_TWOSIDED, twoSided) == AI_SUCCESS) { data.twoSided = twoSided; } if(material->Get(AI_MATKEY_OPACITY, opacity) == AI_SUCCESS) { data.ambientColor.setW(opacity); data.diffuseColor.setW(opacity); data.specularColor.setW(opacity); data.emissiveColor.setW(opacity); if(opacity < 1.0f) { data.alphaBlending = true; // Activate backface culling allows to avoid // cull artifacts when alpha blending is activated data.twoSided = 1; if(material->Get(AI_MATKEY_BLEND_FUNC, blendMode) == AI_SUCCESS) { if(blendMode == aiBlendMode_Additive) data.blendMode = aiBlendMode_Additive; else data.blendMode = aiBlendMode_Default; } } else { data.alphaBlending = false; data.blendMode = -1; } } if(material->Get(AI_MATKEY_SHININESS, shininess) == AI_SUCCESS) { data.shininess = shininess; } if(material->Get(AI_MATKEY_SHININESS_STRENGTH, shininessStrength) == AI_SUCCESS) { data.shininessStrength = shininessStrength; } return data; }
// Execute OpenGL Material void GLC_Material::glExecute(float overwriteTransparency) { GLfloat pAmbientColor[4]= {ambientColor().redF(), ambientColor().greenF(), ambientColor().blueF(), overwriteTransparency}; GLfloat pDiffuseColor[4]= {diffuseColor().redF(), diffuseColor().greenF(), diffuseColor().blueF(), overwriteTransparency}; GLfloat pSpecularColor[4]= {specularColor().redF(), specularColor().greenF(), specularColor().blueF(), overwriteTransparency}; GLfloat pLightEmission[4]= {emissiveColor().redF(), emissiveColor().greenF(), emissiveColor().blueF(), overwriteTransparency}; const bool textureIsEnable= glIsEnabled(GL_TEXTURE_2D); if (m_pTexture != NULL) { if (!textureIsEnable) glEnable(GL_TEXTURE_2D); m_pTexture->glcBindTexture(); if (GLC_State::glslUsed()) { if (GLC_Shader::hasActiveShader()) { GLC_Shader::currentShaderHandle()->programShaderHandle()->setUniformValue("tex", GLint(0)); GLC_Shader::currentShaderHandle()->programShaderHandle()->setUniformValue("useTexture", true); } } } else { if (textureIsEnable) glDisable(GL_TEXTURE_2D); if (GLC_State::glslUsed()) { if (GLC_Shader::hasActiveShader()) { GLC_Shader::currentShaderHandle()->programShaderHandle()->setUniformValue("tex", GLint(0)); GLC_Shader::currentShaderHandle()->programShaderHandle()->setUniformValue("useTexture", false); } } } glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, pAmbientColor); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, pDiffuseColor); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, pSpecularColor); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, pLightEmission); glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, &m_Shininess); glColor4fv(pDiffuseColor); // OpenGL Error handler GLenum error= glGetError(); if (error != GL_NO_ERROR) { GLC_OpenGlException OpenGlException("GLC_Material::glExecute(float overwriteTransparency) ", error); throw(OpenGlException); } }
void Material::ExportMaterials(FbxScene* scene, FbxMesh* mesh, const ImporterMesh& importedMesh, const ImporterMaterial* importedMaterials) { for (int i = 0; i < importedMesh.material_count; i++) { FbxNode* node = mesh->GetNode(); FbxString materialName = importedMaterials[importedMesh.material_Id[i]].name; FbxString shadingName; FbxDouble3 diffuseColor(importedMaterials[importedMesh.material_Id[i]].diffuse[0], importedMaterials[importedMesh.material_Id[i]].diffuse[1], importedMaterials[importedMesh.material_Id[i]].diffuse[2]); FbxDouble3 ambientColor(importedMaterials[importedMesh.material_Id[i]].ambient[0], importedMaterials[importedMesh.material_Id[i]].ambient[1], importedMaterials[importedMesh.material_Id[i]].ambient[2]); FbxDouble3 emissiveColor(importedMaterials[importedMesh.material_Id[i]].incandescence[0], importedMaterials[importedMesh.material_Id[i]].incandescence[1], importedMaterials[importedMesh.material_Id[i]].incandescence[2]); FbxDouble3 transparencyColor(importedMaterials[importedMesh.material_Id[i]].transparency_color[0], importedMaterials[importedMesh.material_Id[i]].transparency_color[1], importedMaterials[importedMesh.material_Id[i]].transparency_color[2]); FbxDouble3 specularColor(importedMaterials[importedMesh.material_Id[i]].specular[0], importedMaterials[importedMesh.material_Id[i]].specular[1] , importedMaterials[importedMesh.material_Id[i]].specular[2]); const char* pathName = "C://Users/Litet/Documents/GitHub/SmallGameProject/FBX Export/FBX Export/"; // Lambert if (importedMaterials[importedMesh.material_Id[i]].mtrl_type == 0) { shadingName = "Lambert"; FbxSurfaceLambert* material = NULL; material= node->GetSrcObject<FbxSurfaceLambert>(0); material = FbxSurfaceLambert::Create(scene, materialName.Buffer()); materialName += i; material->Emissive.Set(emissiveColor); material->Ambient.Set(ambientColor); material->Diffuse.Set(diffuseColor); material->DiffuseFactor.Set(importedMaterials[importedMesh.material_Id[i]].diffuse_factor); material->TransparentColor.Set(transparencyColor); FbxNode* node = mesh->GetNode(); if (node) { node->AddMaterial(material); } // Diffuse Texture FbxFileTexture* texture = FbxFileTexture::Create(scene, "Diffuse Texture"); std::cout << "DAFUSE MAP LENGTH: " << importedMaterials[importedMesh.material_Id[i]].duffuse_map_length << std::endl; if (importedMaterials[importedMesh.material_Id[i]].duffuse_map_length > 0) { std::string tmp(pathName); tmp += importedMaterials[importedMesh.material_Id[i]].diffuse_map; texture->SetFileName(tmp.c_str()); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->Diffuse.ConnectSrcObject(texture); } // Normal Texture texture = FbxFileTexture::Create(scene, "Normal Texture"); if (importedMaterials[importedMesh.material_Id[i]].normal_map_length > 0) { texture->SetFileName(importedMaterials[importedMesh.material_Id[i]].normal_map); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->NormalMap.ConnectSrcObject(texture); } } // Phong else { shadingName = "Phong"; FbxSurfacePhong* material = NULL; material = node->GetSrcObject<FbxSurfacePhong>(0); material = FbxSurfacePhong::Create(scene, materialName.Buffer()); materialName += i; material->Emissive.Set(emissiveColor); material->Ambient.Set(ambientColor); material->Diffuse.Set(diffuseColor); material->DiffuseFactor.Set(importedMaterials[importedMesh.material_Id[i]].diffuse_factor); material->TransparentColor.Set(transparencyColor); material->Specular.Set(specularColor); // No need... super boost? //material->SpecularFactor.Set(importedMaterials[importedMesh.material_Id[i]].specular_factor * 100); material->Shininess.Set(importedMaterials[importedMesh.material_Id[i]].specular_factor); material->Reflection.Set(FbxDouble3(importedMaterials[importedMesh.material_Id[i]].reflection[0], importedMaterials[importedMesh.material_Id[i]].reflection[1], importedMaterials[importedMesh.material_Id[i]].reflection[2])); // Bugged...? material->ReflectionFactor.Set(FbxDouble(importedMaterials[importedMesh.material_Id[i]].reflection_factor)); cout << importedMaterials[importedMesh.material_Id[i]].shininess << endl; FbxNode* node = mesh->GetNode(); if (node) { node->AddMaterial(material); } // Diffuse Texture FbxFileTexture* texture = FbxFileTexture::Create(scene, "Diffuse Texture"); if (importedMaterials[importedMesh.material_Id[i]].duffuse_map_length > 0) { texture->SetFileName(importedMaterials[importedMesh.material_Id[i]].diffuse_map); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->Diffuse.ConnectSrcObject(texture); } // Specular Texture texture = FbxFileTexture::Create(scene, "Specular Texture"); if (importedMaterials[importedMesh.material_Id[i]].specular_map_length > 0) { texture->SetFileName(importedMaterials[importedMesh.material_Id[i]].specular_map); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->Specular.ConnectSrcObject(texture); } // Normal Texture texture = FbxFileTexture::Create(scene, "Normal Texture"); if (importedMaterials[importedMesh.material_Id[i]].normal_map_length > 0) { texture->SetFileName(importedMaterials[importedMesh.material_Id[i]].normal_map); texture->SetTextureUse(FbxTexture::eStandard); texture->SetMappingType(FbxTexture::eUV); texture->SetMaterialUse(FbxFileTexture::eModelMaterial); texture->SetSwapUV(false); texture->SetTranslation(0.0, 0.0); texture->SetScale(1.0, 1.0); texture->SetRotation(0.0, 0.0); if (material) material->NormalMap.ConnectSrcObject(texture); } } } }