char *find_material(struct aiMaterial *material) { struct aiString str; char shader[500], *p; char *name; int i; for (i = 0; i < nummats; i++) if (matlist[i].material == material) return matlist[i].shader; aiGetMaterialString(material, AI_MATKEY_NAME, &str); name = str.data; strcpy(shader, clean_material_name(name)); strcat(shader, "+"); if (!aiGetMaterialString(material, AI_MATKEY_TEXTURE_DIFFUSE(0), &str)) strcat(shader, get_base_name(str.data)); else strcat(shader, "unknown"); p = strrchr(shader, '.'); if (p) *p = 0; p = shader; while (*p) { *p = tolower(*p); p++; } matlist[nummats].name = name; matlist[nummats].material = material; matlist[nummats].shader = strdup(shader); return matlist[nummats++].shader; }
Material::Material(aiMaterial* mat) { aiColor4D _color_diffuse(0.f,0.f,0.f,0.0f); aiColor4D _color_specular(0.f,0.f,0.f,0.0f); aiColor4D _color_ambient(0.f,0.f,0.f,0.0f); aiColor4D _color_emissive(0.f,0.f,0.f,0.0f); aiColor4D _color_transparent(1.f,1.f,1.f,1.0f); float _opacity=1.0f; float _shininess=0.0f; aiString _name; aiGetMaterialColor(mat,AI_MATKEY_COLOR_DIFFUSE,&_color_diffuse); aiGetMaterialColor(mat,AI_MATKEY_COLOR_SPECULAR,&_color_specular); aiGetMaterialColor(mat,AI_MATKEY_COLOR_AMBIENT,&_color_ambient); aiGetMaterialColor(mat,AI_MATKEY_COLOR_EMISSIVE,&_color_emissive); aiGetMaterialColor(mat,AI_MATKEY_COLOR_TRANSPARENT,&_color_transparent); aiGetMaterialFloat(mat,AI_MATKEY_OPACITY,&_opacity); aiGetMaterialFloat(mat,AI_MATKEY_SHININESS,&_shininess); aiGetMaterialString(mat,AI_MATKEY_NAME,&_name); name=_name.C_Str(); color_diffuse=vec4(_color_diffuse.r,_color_diffuse.g,_color_diffuse.b,_color_diffuse.a); color_specular=vec4(_color_specular.r,_color_specular.g,_color_specular.b,_color_specular.a); color_ambient=vec4(_color_ambient.r,_color_ambient.g,_color_ambient.b,_color_ambient.a); color_emissive=vec4(_color_emissive.r,_color_emissive.g,_color_emissive.b,_color_emissive.a); color_transparent=vec4(_color_transparent.r,_color_transparent.g,_color_transparent.b,_color_transparent.a); opacity=_opacity; shininess=_shininess; }
//////////////////////////////////////////////////////////////////////// /// /// @fn void Modele3D::chargerTextures() /// /// Cette fonction charge les textures OpenGL associées au modèle /// 'assimp' préalablement chargé. /// /// @return Aucune. /// //////////////////////////////////////////////////////////////////////// void Modele3D::chargerTextures() { if (scene_->HasTextures()) { utilitaire::afficherErreur("Les textures intégrées ne sont pas prises en charge pour le moment."); } // Itérer parmi l'ensemble des matériaux for (unsigned int i=0; i<scene_->mNumMaterials; i++) { aiMaterial* materiau = scene_->mMaterials[i]; aiString matName; aiGetMaterialString(materiau,AI_MATKEY_NAME,&matName); mapMaterial_[matName.data] = materiau; // POINT D'EXTENSION : il vous serait possible de prendre en charge d'autres types de textures que celle // en diffusion (DIFFUSE)... // Parcourir l'ensemble des textures pour ce matériau et stocker les noms de fichiers // Par la suite, nous initialiserons la mémoire de textures OpenGL et procéderons // au chargement des fichiers graphiques. int idTexture = 0; while (true) { // Chaîne UTF-8 (fort possiblement compatible ASCII) qui contient le nom de fichier associé à la texture // On peut l'utiliser comme une chaîne C standard (char *) sans réel risque (selon la doc. assimp !) aiString cheminTexture = ""; // Récupérer le chemin vers la texture... aiReturn retour = materiau->GetTexture(aiTextureType_DIFFUSE, idTexture, &cheminTexture); // Vérifie que la texture est valide... if (retour != AI_SUCCESS) break; // Stocker le nom de texture dans le tableau associatif mapTextures_[cheminTexture.data] = NULL; idTexture++; } } // Pour chaque texture trouvée, procéder au chargement // Nombre de textures GLsizei nombreTextures = (GLsizei)mapTextures_.size(); // Générer les textures OpenGL identificateursTextures_ = new GLuint[nombreTextures]; glGenTextures(nombreTextures, identificateursTextures_); // Itérateur STL std::map<std::string, GLuint*>::iterator itr = mapTextures_.begin(); for (int j=0; j<nombreTextures; j++) { std::string nomFichier = itr->first; itr->second = &(identificateursTextures_[j]); itr++; // Charger la texture aidegl::glLoadTexture( RazerGameUtilities::NOM_DOSSIER_MEDIA + nomFichier, identificateursTextures_[j], false); } }
static int GetString(lua_State *L, material_t *material, const char* pKey, unsigned int type, unsigned int index) { aistring_t s; if(!material) material = checkmaterial(L, 1); s.data[0]='\0'; s.length = 0; if(aiGetMaterialString(material, pKey, type, index, &s) != AI_SUCCESS) return 0; lua_pushlstring(L, s.data, s.length); return 1; }
void process_material(struct goat3d_material *mtl, struct aiMaterial *aimtl) { struct aiString aistr; struct aiColor4D color; float val; if(aiGetMaterialString(aimtl, AI_MATKEY_NAME, &aistr) == aiReturn_SUCCESS) { goat3d_set_mtl_name(mtl, aistr.data); } if(aiGetMaterialColor(aimtl, AI_MATKEY_COLOR_DIFFUSE, &color) == aiReturn_SUCCESS) { goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_DIFFUSE, color.r, color.g, color.b); } if(aiGetMaterialColor(aimtl, AI_MATKEY_COLOR_SPECULAR, &color) == aiReturn_SUCCESS) { float sstr = 1.0; aiGetMaterialFloatArray(aimtl, AI_MATKEY_SHININESS_STRENGTH, &sstr, 0); goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_SPECULAR, color.r * sstr, color.g * sstr, color.b * sstr); } if(aiGetMaterialFloatArray(aimtl, AI_MATKEY_BUMPSCALING, &val, 0) == aiReturn_SUCCESS) { goat3d_set_mtl_attrib3f(mtl, GOAT3D_MAT_ATTR_BUMP, val, val, val); } if(aiGetMaterialFloatArray(aimtl, AI_MATKEY_REFLECTIVITY, &val, 0) == aiReturn_SUCCESS) { goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_REFLECTION, val); } if(aiGetMaterialFloatArray(aimtl, AI_MATKEY_OPACITY, &val, 0) == aiReturn_SUCCESS) { goat3d_set_mtl_attrib1f(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, 1.0 - val); } if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_DIFFUSE(0), &aistr) == aiReturn_SUCCESS) { goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_DIFFUSE, aistr.data); } if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_SPECULAR(0), &aistr) == aiReturn_SUCCESS) { goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SPECULAR, aistr.data); } if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_SHININESS(0), &aistr) == aiReturn_SUCCESS) { goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_SHININESS, aistr.data); } if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_NORMALS(0), &aistr) == aiReturn_SUCCESS) { goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_NORMAL, aistr.data); } if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_REFLECTION(0), &aistr) == aiReturn_SUCCESS) { goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_REFLECTION, aistr.data); } if(aiGetMaterialString(aimtl, AI_MATKEY_TEXTURE_OPACITY(0), &aistr) == aiReturn_SUCCESS) { /* TODO this is semantically inverted... maybe add an alpha attribute? */ goat3d_set_mtl_attrib_map(mtl, GOAT3D_MAT_ATTR_TRANSMISSION, aistr.data); } }
void load_mesh(char* fname, state_t* state, transform_data_t t, int bsdf_index, char* matname) { printf("Loading mesh %s\n",fname); //parse file const aiScene* scene = aiImportFile(fname, aiProcess_Triangulate | aiProcess_JoinIdenticalVertices); qr_assert(scene, "parser", "Could not open mesh file: %s (%s)",fname, aiGetErrorString()); for(int i=0; i<scene->mNumMeshes; i++) { bool contin = false; if(matname == NULL) { contin = true; } else { aiString name; aiGetMaterialString(scene->mMaterials[scene->mMeshes[i]->mMaterialIndex], AI_MATKEY_NAME, &name); contin = strcmp(matname, &name.data[0]) == 0; } if(contin) { int pind = state->n_primitives; state->n_primitives += scene->mMeshes[i]->mNumFaces; state->primitives = (primitive_t*)realloc(state->primitives, sizeof(primitive_t)*(state->n_primitives)); int vind = state->n_verts; int vstart = state->n_verts; state->n_verts += scene->mMeshes[i]->mNumVertices; state->verts = (vec3*)realloc(state->verts, sizeof(vec3)*(state->n_verts)); for(int j=0; j<scene->mMeshes[i]->mNumVertices; j++) { state->verts[vind++] = transform_point(make_vec3(scene->mMeshes[i]->mVertices[j].x, scene->mMeshes[i]->mVertices[j].y, scene->mMeshes[i]->mVertices[j].z), state->transforms[t.index], false); } for(int j=0; j<scene->mMeshes[i]->mNumFaces; j++) { qr_assert(scene->mMeshes[i]->mFaces[j].mNumIndices==3, "parser", "Only triangles are supported (%s)",fname); state->primitives[pind].type = PRIMITIVE_TRIANGLE; state->primitives[pind].t = 0; state->primitives[pind].bsdf = bsdf_index; state->primitives[pind++].data = make_primitive_triangle(vstart+scene->mMeshes[i]->mFaces[j].mIndices[0], vstart+scene->mMeshes[i]->mFaces[j].mIndices[1], vstart+scene->mMeshes[i]->mFaces[j].mIndices[2]); } } } aiReleaseImport(scene); }
//------------------------------------------------------------------------------- int CMaterialManager::CreateMaterial( AssetHelper::MeshHelper* pcMesh,const aiMesh* pcSource) { #if 1//??? ai_assert(0 != pcMesh); ai_assert(0 != pcSource); // ID3DXFROMWINEBuffer* piBuffer; // extract all properties from the ASSIMP material structure const aiMaterial* pcMat = mr->GetAsset()->pcScene->mMaterials[pcSource->mMaterialIndex]; // // DIFFUSE COLOR -------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_DIFFUSE, (aiColor4D*)&pcMesh->vDiffuseColor)) { pcMesh->vDiffuseColor.x = 1.0f; pcMesh->vDiffuseColor.y = 1.0f; pcMesh->vDiffuseColor.z = 1.0f; pcMesh->vDiffuseColor.w = 1.0f; } // // SPECULAR COLOR -------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_SPECULAR, (aiColor4D*)&pcMesh->vSpecularColor)) { pcMesh->vSpecularColor.x = 1.0f; pcMesh->vSpecularColor.y = 1.0f; pcMesh->vSpecularColor.z = 1.0f; pcMesh->vSpecularColor.w = 1.0f; } // // AMBIENT COLOR -------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_AMBIENT, (aiColor4D*)&pcMesh->vAmbientColor)) { pcMesh->vAmbientColor.x = 0.0f; pcMesh->vAmbientColor.y = 0.0f; pcMesh->vAmbientColor.z = 0.0f; pcMesh->vAmbientColor.w = 1.0f; } // // EMISSIVE COLOR ------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_EMISSIVE, (aiColor4D*)&pcMesh->vEmissiveColor)) { pcMesh->vEmissiveColor.x = 0.0f; pcMesh->vEmissiveColor.y = 0.0f; pcMesh->vEmissiveColor.z = 0.0f; pcMesh->vEmissiveColor.w = 1.0f; } // // Opacity -------------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialFloat(pcMat,AI_MATKEY_OPACITY,&pcMesh->fOpacity)) { pcMesh->fOpacity = 1.0f; } // // Shading Model -------------------------------------------------- // bool bDefault = false; if(AI_SUCCESS != aiGetMaterialInteger(pcMat,AI_MATKEY_SHADING_MODEL,(int*)&pcMesh->eShadingMode )) { bDefault = true; pcMesh->eShadingMode = aiShadingMode_Gouraud; } // // Shininess ------------------------------------------------------ // if(AI_SUCCESS != aiGetMaterialFloat(pcMat,AI_MATKEY_SHININESS,&pcMesh->fShininess)) { // assume 15 as default shininess pcMesh->fShininess = 15.0f; } else if (bDefault)pcMesh->eShadingMode = aiShadingMode_Phong; // // Shininess strength ------------------------------------------------------ // if(AI_SUCCESS != aiGetMaterialFloat(pcMat,AI_MATKEY_SHININESS_STRENGTH,&pcMesh->fSpecularStrength)) { // assume 1.0 as default shininess strength pcMesh->fSpecularStrength = 1.0f; } aiString szPath; aiTextureMapMode mapU(aiTextureMapMode_Wrap),mapV(aiTextureMapMode_Wrap); bool bib =false; if (pcSource->mTextureCoords[0]) { // // DIFFUSE TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_DIFFUSE(0),&szPath)) { LoadTexture(&pcMesh->piDiffuseTexture,&szPath); aiGetMaterialInteger(pcMat,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0),(int*)&mapU); aiGetMaterialInteger(pcMat,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0),(int*)&mapV); } // // SPECULAR TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_SPECULAR(0),&szPath)) { LoadTexture(&pcMesh->piSpecularTexture,&szPath); } // // OPACITY TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_OPACITY(0),&szPath)) { LoadTexture(&pcMesh->piOpacityTexture,&szPath); } else { int flags = aiTextureFlags_IgnoreAlpha;//???0; aiGetMaterialInteger(pcMat,AI_MATKEY_TEXFLAGS_DIFFUSE(0),&flags); // try to find out whether the diffuse texture has any // non-opaque pixels. If we find a few, use it as opacity texture if ((pcMesh->piDiffuseTexture!=-1) && !(flags & aiTextureFlags_IgnoreAlpha) && HasAlphaPixels(pcMesh->piDiffuseTexture)) { int iVal; // NOTE: This special value is set by the tree view if the user // manually removes the alpha texture from the view ... if (AI_SUCCESS != aiGetMaterialInteger(pcMat,"no_a_from_d",0,0,&iVal)) { pcMesh->piOpacityTexture = pcMesh->piDiffuseTexture; // pcMesh->piOpacityTexture->AddRef(); } } } // // AMBIENT TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_AMBIENT(0),&szPath)) { LoadTexture(&pcMesh->piAmbientTexture,&szPath); } // // EMISSIVE TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_EMISSIVE(0),&szPath)) { LoadTexture(&pcMesh->piEmissiveTexture,&szPath); } // // Shininess TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_SHININESS(0),&szPath)) { LoadTexture(&pcMesh->piShininessTexture,&szPath); } // // Lightmap TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_LIGHTMAP(0),&szPath)) { LoadTexture(&pcMesh->piLightmapTexture,&szPath); } // // NORMAL/HEIGHT MAP ------------------------------------------------ // bool bHM = false; if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_NORMALS(0),&szPath)) { LoadTexture(&pcMesh->piNormalTexture,&szPath); } else { if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_HEIGHT(0),&szPath)) { LoadTexture(&pcMesh->piNormalTexture,&szPath); } else bib = true; bHM = true; } // normal/height maps are sometimes mixed up. Try to detect the type // of the texture automatically if (pcMesh->piNormalTexture!=-1) { HMtoNMIfNecessary(pcMesh->piNormalTexture, &pcMesh->piNormalTexture,bHM); } } // check whether a global background texture is contained // in this material. Some loaders set this value ... if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_GLOBAL_BACKGROUND_IMAGE,&szPath)) { // CBackgroundPainter::Instance().SetTextureBG(szPath.data); } // BUGFIX: If the shininess is 0.0f disable phong lighting // This is a workaround for some meshes in the DX SDK (e.g. tiny.x) // FIX: Added this check to the x-loader, but the line remains to // catch other loader doing the same ... if (0.0f == pcMesh->fShininess){ pcMesh->eShadingMode = aiShadingMode_Gouraud; } int two_sided = 0; aiGetMaterialInteger(pcMat,AI_MATKEY_TWOSIDED,&two_sided); pcMesh->twosided = (two_sided != 0); // check whether we have already a material using the same // shader. This will decrease loading time rapidly ... for (unsigned int i = 0; i < mr->GetAsset()->pcScene->mNumMeshes;++i) { if (mr->GetAsset()->pcScene->mMeshes[i] == pcSource) { break; } AssetHelper::MeshHelper* pc = mr->GetAsset()->apcMeshes[i]; if ((pcMesh->piDiffuseTexture !=-1 ? true : false) != (pc->piDiffuseTexture !=-1 ? true : false)) continue; if ((pcMesh->piSpecularTexture !=-1 ? true : false) != (pc->piSpecularTexture !=-1 ? true : false)) continue; if ((pcMesh->piAmbientTexture !=-1 ? true : false) != (pc->piAmbientTexture !=-1 ? true : false)) continue; if ((pcMesh->piEmissiveTexture !=-1 ? true : false) != (pc->piEmissiveTexture !=-1 ? true : false)) continue; if ((pcMesh->piNormalTexture !=-1 ? true : false) != (pc->piNormalTexture !=-1 ? true : false)) continue; if ((pcMesh->piOpacityTexture !=-1 ? true : false) != (pc->piOpacityTexture !=-1 ? true : false)) continue; if ((pcMesh->piShininessTexture !=-1 ? true : false) != (pc->piShininessTexture !=-1 ? true : false)) continue; if ((pcMesh->piLightmapTexture !=-1 ? true : false) != (pc->piLightmapTexture !=-1 ? true : false)) continue; if ((pcMesh->eShadingMode != aiShadingMode_Gouraud ? true : false) != (pc->eShadingMode != aiShadingMode_Gouraud ? true : false)) continue; if ((pcMesh->fOpacity != 1.0f ? true : false) != (pc->fOpacity != 1.0f ? true : false)) continue; if (pcSource->HasBones() != mr->GetAsset()->pcScene->mMeshes[i]->HasBones()) continue; // we can reuse this material if (pc->piEffect!=-1) { pcMesh->piEffect = pc->piEffect; pc->bSharedFX = pcMesh->bSharedFX = true; // pcMesh->piEffect->AddRef(); return 2; } } m_iShaderCount++; //if(mr->m_piDefaultEffect==-1) if(0) { typedef struct _D3DXFROMWINEMACRO { LPCSTR Name; LPCSTR Definition; } D3DXFROMWINEMACRO, *LPD3DXFROMWINEMACRO; D3DXFROMWINEMACRO sMacro[64]; // build macros for the HLSL compiler unsigned int iCurrent = 0; if (pcMesh->piDiffuseTexture!=-1) { sMacro[iCurrent].Name = "AV_DIFFUSE_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; if (mapU == aiTextureMapMode_Wrap) sMacro[iCurrent].Name = "AV_WRAPU"; else if (mapU == aiTextureMapMode_Mirror) sMacro[iCurrent].Name = "AV_MIRRORU"; else // if (mapU == aiTextureMapMode_Clamp) sMacro[iCurrent].Name = "AV_CLAMPU"; sMacro[iCurrent].Definition = "1"; ++iCurrent; if (mapV == aiTextureMapMode_Wrap) sMacro[iCurrent].Name = "AV_WRAPV"; else if (mapV == aiTextureMapMode_Mirror) sMacro[iCurrent].Name = "AV_MIRRORV"; else // if (mapV == aiTextureMapMode_Clamp) sMacro[iCurrent].Name = "AV_CLAMPV"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if (pcMesh->piSpecularTexture!=-1) { sMacro[iCurrent].Name = "AV_SPECULAR_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if (pcMesh->piAmbientTexture!=-1) { sMacro[iCurrent].Name = "AV_AMBIENT_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if (pcMesh->piEmissiveTexture!=-1) { sMacro[iCurrent].Name = "AV_EMISSIVE_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } char buff[32]; if (pcMesh->piLightmapTexture!=-1) { sMacro[iCurrent].Name = "AV_LIGHTMAP_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; int idx; if(AI_SUCCESS == aiGetMaterialInteger(pcMat,AI_MATKEY_UVWSRC_LIGHTMAP(0),&idx) && idx >= 1 && pcSource->mTextureCoords[idx]) { sMacro[iCurrent].Name = "AV_TWO_UV"; sMacro[iCurrent].Definition = "1"; ++iCurrent; sMacro[iCurrent].Definition = "IN.TexCoord1"; } else sMacro[iCurrent].Definition = "IN.TexCoord0"; sMacro[iCurrent].Name = "AV_LIGHTMAP_TEXTURE_UV_COORD"; ++iCurrent;float f= 1.f; aiGetMaterialFloat(pcMat,AI_MATKEY_TEXBLEND_LIGHTMAP(0),&f); stx_snprintf(buff,32,"%f",f); sMacro[iCurrent].Name = "LM_STRENGTH"; sMacro[iCurrent].Definition = buff; ++iCurrent; } if ((pcMesh->piNormalTexture!=-1) && !bib) { sMacro[iCurrent].Name = "AV_NORMAL_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if (pcMesh->piOpacityTexture!=-1) { sMacro[iCurrent].Name = "AV_OPACITY_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; if (pcMesh->piOpacityTexture == pcMesh->piDiffuseTexture) { sMacro[iCurrent].Name = "AV_OPACITY_TEXTURE_REGISTER_MASK"; sMacro[iCurrent].Definition = "a"; ++iCurrent; } else { sMacro[iCurrent].Name = "AV_OPACITY_TEXTURE_REGISTER_MASK"; sMacro[iCurrent].Definition = "r"; ++iCurrent; } } if (pcMesh->eShadingMode != aiShadingMode_Gouraud && !mr->m_sOptions.bNoSpecular) { sMacro[iCurrent].Name = "AV_SPECULAR_COMPONENT"; sMacro[iCurrent].Definition = "1"; ++iCurrent; if (pcMesh->piShininessTexture!=-1) { sMacro[iCurrent].Name = "AV_SHININESS_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } } if (1.0f != pcMesh->fOpacity) { sMacro[iCurrent].Name = "AV_OPACITY"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if( pcSource->HasBones()) { sMacro[iCurrent].Name = "AV_SKINNING"; sMacro[iCurrent].Definition = "0";//???"1"; ++iCurrent; } /* // If a cubemap is active, we'll need to lookup it for calculating // a physically correct reflection if (CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode()) { sMacro[iCurrent].Name = "AV_SKYBOX_LOOKUP"; sMacro[iCurrent].Definition = "1"; ++iCurrent; }*/ sMacro[iCurrent].Name = 0; sMacro[iCurrent].Definition = 0; //Construct defines from sMacro and compine with mr->m_szMaterialShader string std::string extra; unsigned int iiCurrent = 0; while ((sMacro[iiCurrent].Name != 0)&& (sMacro[iiCurrent].Definition != 0)) { extra.append("#define "); extra.append(sMacro[iiCurrent].Name); extra.append(" "); extra.append(sMacro[iiCurrent].Definition); extra.append(";\n"); iiCurrent++; } // compile the shader #if 1 const char *ShaderName = mr->m_szShaderName.c_str(); //LOG_PRINT("ShaderName=%s\n", ShaderName); ShaderID shd=MeshRendererShadersFactory::GetShader(ShaderName, "main", "main"); //LOG_PRINT("m_SimpleShader=%s\n", m_SimpleShader); #elif 0 const char *m_SimpleShader = MeshRendererShadersFactory::GetShader("SimpleShader"); mr->m_piDefaultEffect=IRenderer::GetRendererInstance()->addHLSLShader( m_SimpleShader, "main", "main");//D1??? #elif 0 mr->m_piDefaultEffect=IRenderer::GetRendererInstance()->addHLSLShader( g_szMaterialShader.c_str(), "MaterialVShader_D1", "MaterialPShaderSpecular_D1",0,0,extra.c_str());//D1??? #elif 0 mr->m_piDefaultEffect=IRenderer::GetRendererInstance()->addHLSLShader( g_szDefaultShader.c_str(), "DefaultVShader", "DefaultPShaderSpecular_D1",0,0,extra.c_str());//D1??? #endif //mr->m_piMaterialEffect=mr->m_piDefaultEffect; #if 1 FormatDesc Fmt[] = { #if 0 { 0, 0, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_POSITION, 0 }, { 0, 12, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_NORMAL, 0 }, { 0, 24, D3DDECLTYPE_D3DCOLOR, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_COLOR, 0 }, { 0, 28, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TANGENT, 0 }, { 0, 40, D3DDECLTYPE_FLOAT3, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BINORMAL, 0 }, { 0, 52, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 0 }, { 0, 60, D3DDECLTYPE_FLOAT2, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_TEXCOORD, 1 }, { 0, 68, D3DDECLTYPE_UBYTE4, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDINDICES, 0 }, { 0, 72, D3DDECLTYPE_UBYTE4N, D3DDECLMETHOD_DEFAULT, D3DDECLUSAGE_BLENDWEIGHT, 0 }, #else {0, TYPE_VERTEX, FORMAT_FLOAT, 3}, {0, TYPE_TEXCOORD, FORMAT_FLOAT, 2}, /* {0, TYPE_NORMAL, FORMAT_FLOAT, 3}, {0, TYPE_TEXCOORD, FORMAT_FLOAT, 3},//??? {0, TYPE_TEXCOORD, FORMAT_FLOAT, 2}, {0, TYPE_TEXCOORD, FORMAT_FLOAT, 4},//??? {0, TYPE_TEXCOORD, FORMAT_FLOAT, 4}//??? */ #endif }; //mr->m_DefaultVertexDecl = IRenderer::GetRendererInstance()->addVertexFormat(Fmt, elementsOf(Fmt), mr->m_piDefaultEffect);//??? #endif } if(0)//-1== mr->m_piMaterialEffect) { // get the name of the material and use it in the log message if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_NAME,&szPath) && '\0' != szPath.data[0]) { std::string sz = "[ERROR] Unable to load material: "; sz.append(szPath.data); //CLogDisplay::Instance().AddEntry(sz); } else { //CLogDisplay::Instance().AddEntry("Unable to load material: UNNAMED"); } return 0; } else { /* // use Fixed Function effect when working with shaderless cards if( mr->m_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0)) { ////LOG_PRINT("mr->m_piDefaultEffect=%d\n",mr->m_piDefaultEffect); IRenderer::GetRendererInstance()->SetTechnique( MaterialFX_FFh); }*/ } // if( piBuffer) piBuffer->Release(); LOG_FNLN; LOG_PRINT("ShaderName=%d\n",mr->m_szShaderName.c_str()); ShaderID shd = MeshRendererShadersFactory::GetShader(mr->m_szShaderName.c_str(), "main", "main"); IRenderer::GetRendererInstance()->setShader(shd); // now commit all constants to the shader // // This is not necessary for shared shader. Shader constants for // shared shaders are automatically recommited before the shader // is being used for a particular mesh if (1.0f != pcMesh->fOpacity) { //???IRenderer::GetRendererInstance()->SetFloat("TRANSPARENCY",pcMesh->fOpacity); } if (pcMesh->eShadingMode != aiShadingMode_Gouraud && !mr->m_sOptions.bNoSpecular) { IRenderer::GetRendererInstance()->SetFloat("SPECULARITY",pcMesh->fShininess);//??? IRenderer::GetRendererInstance()->SetFloat("SPECULAR_STRENGTH",pcMesh->fSpecularStrength); } IRenderer::GetRendererInstance()->SetVector("DIFFUSE_COLOR",&pcMesh->vDiffuseColor); //IRenderer::GetRendererInstance()->SetVector("SPECULAR_COLOR",&pcMesh->vSpecularColor); IRenderer::GetRendererInstance()->SetVector("AMBIENT_COLOR",&pcMesh->vAmbientColor); IRenderer::GetRendererInstance()->SetVector("EMISSIVE_COLOR",&pcMesh->vEmissiveColor); if (pcMesh->piDiffuseTexture!=-1) { IRenderer::GetRendererInstance()->SetTexture("DIFFUSE_SAMPLER",pcMesh->piDiffuseTexture); } if (pcMesh->piOpacityTexture!=-1) { IRenderer::GetRendererInstance()->SetTexture("OPACITY_SAMPLER",pcMesh->piOpacityTexture); } if (pcMesh->piSpecularTexture!=-1) { IRenderer::GetRendererInstance()->SetTexture("SPECULAR_SAMPLER",pcMesh->piSpecularTexture); } if (pcMesh->piAmbientTexture!=-1) { IRenderer::GetRendererInstance()->SetTexture("AMBIENT_SAMPLER",pcMesh->piAmbientTexture); } if (pcMesh->piEmissiveTexture!=-1) { IRenderer::GetRendererInstance()->SetTexture("EMISSIVE_SAMPLER",pcMesh->piEmissiveTexture); } if (pcMesh->piNormalTexture!=-1) { IRenderer::GetRendererInstance()->SetTexture("NORMAL_SAMPLER",pcMesh->piNormalTexture); } if (pcMesh->piShininessTexture!=-1) { IRenderer::GetRendererInstance()->SetTexture("SHININESS_SAMPLER",pcMesh->piShininessTexture); } if (pcMesh->piLightmapTexture!=-1) { IRenderer::GetRendererInstance()->SetTexture("LIGHTMAP_SAMPLER",pcMesh->piLightmapTexture); } /* if (CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode()){ IRenderer::GetRendererInstance()->SetTexture("lw_tex_envmap",CBackgroundPainter::Instance().GetTexture()); }*/ #endif return 1; }
//------------------------------------------------------------------------------- int CMaterialManager::CreateMaterial( AssetHelper::MeshHelper* pcMesh,const aiMesh* pcSource) { ai_assert(NULL != pcMesh); ai_assert(NULL != pcSource); ID3DXBuffer* piBuffer; D3DXMACRO sMacro[64]; // extract all properties from the ASSIMP material structure const aiMaterial* pcMat = g_pcAsset->pcScene->mMaterials[pcSource->mMaterialIndex]; // // DIFFUSE COLOR -------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_DIFFUSE, (aiColor4D*)&pcMesh->vDiffuseColor)) { pcMesh->vDiffuseColor.x = 1.0f; pcMesh->vDiffuseColor.y = 1.0f; pcMesh->vDiffuseColor.z = 1.0f; pcMesh->vDiffuseColor.w = 1.0f; } // // SPECULAR COLOR -------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_SPECULAR, (aiColor4D*)&pcMesh->vSpecularColor)) { pcMesh->vSpecularColor.x = 1.0f; pcMesh->vSpecularColor.y = 1.0f; pcMesh->vSpecularColor.z = 1.0f; pcMesh->vSpecularColor.w = 1.0f; } // // AMBIENT COLOR -------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_AMBIENT, (aiColor4D*)&pcMesh->vAmbientColor)) { pcMesh->vAmbientColor.x = 0.0f; pcMesh->vAmbientColor.y = 0.0f; pcMesh->vAmbientColor.z = 0.0f; pcMesh->vAmbientColor.w = 1.0f; } // // EMISSIVE COLOR ------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialColor(pcMat,AI_MATKEY_COLOR_EMISSIVE, (aiColor4D*)&pcMesh->vEmissiveColor)) { pcMesh->vEmissiveColor.x = 0.0f; pcMesh->vEmissiveColor.y = 0.0f; pcMesh->vEmissiveColor.z = 0.0f; pcMesh->vEmissiveColor.w = 1.0f; } // // Opacity -------------------------------------------------------- // if(AI_SUCCESS != aiGetMaterialFloat(pcMat,AI_MATKEY_OPACITY,&pcMesh->fOpacity)) { pcMesh->fOpacity = 1.0f; } // // Shading Model -------------------------------------------------- // bool bDefault = false; if(AI_SUCCESS != aiGetMaterialInteger(pcMat,AI_MATKEY_SHADING_MODEL,(int*)&pcMesh->eShadingMode )) { bDefault = true; pcMesh->eShadingMode = aiShadingMode_Gouraud; } // // Shininess ------------------------------------------------------ // if(AI_SUCCESS != aiGetMaterialFloat(pcMat,AI_MATKEY_SHININESS,&pcMesh->fShininess)) { // assume 15 as default shininess pcMesh->fShininess = 15.0f; } else if (bDefault)pcMesh->eShadingMode = aiShadingMode_Phong; // // Shininess strength ------------------------------------------------------ // if(AI_SUCCESS != aiGetMaterialFloat(pcMat,AI_MATKEY_SHININESS_STRENGTH,&pcMesh->fSpecularStrength)) { // assume 1.0 as default shininess strength pcMesh->fSpecularStrength = 1.0f; } aiString szPath; aiTextureMapMode mapU(aiTextureMapMode_Wrap),mapV(aiTextureMapMode_Wrap); bool bib =false; if (pcSource->mTextureCoords[0]) { // // DIFFUSE TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_DIFFUSE(0),&szPath)) { LoadTexture(&pcMesh->piDiffuseTexture,&szPath); aiGetMaterialInteger(pcMat,AI_MATKEY_MAPPINGMODE_U_DIFFUSE(0),(int*)&mapU); aiGetMaterialInteger(pcMat,AI_MATKEY_MAPPINGMODE_V_DIFFUSE(0),(int*)&mapV); } // // SPECULAR TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_SPECULAR(0),&szPath)) { LoadTexture(&pcMesh->piSpecularTexture,&szPath); } // // OPACITY TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_OPACITY(0),&szPath)) { LoadTexture(&pcMesh->piOpacityTexture,&szPath); } else { int flags = 0; aiGetMaterialInteger(pcMat,AI_MATKEY_TEXFLAGS_DIFFUSE(0),&flags); // try to find out whether the diffuse texture has any // non-opaque pixels. If we find a few, use it as opacity texture if (pcMesh->piDiffuseTexture && !(flags & aiTextureFlags_IgnoreAlpha) && HasAlphaPixels(pcMesh->piDiffuseTexture)) { int iVal; // NOTE: This special value is set by the tree view if the user // manually removes the alpha texture from the view ... if (AI_SUCCESS != aiGetMaterialInteger(pcMat,"no_a_from_d",0,0,&iVal)) { pcMesh->piOpacityTexture = pcMesh->piDiffuseTexture; pcMesh->piOpacityTexture->AddRef(); } } } // // AMBIENT TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_AMBIENT(0),&szPath)) { LoadTexture(&pcMesh->piAmbientTexture,&szPath); } // // EMISSIVE TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_EMISSIVE(0),&szPath)) { LoadTexture(&pcMesh->piEmissiveTexture,&szPath); } // // Shininess TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_SHININESS(0),&szPath)) { LoadTexture(&pcMesh->piShininessTexture,&szPath); } // // Lightmap TEXTURE ------------------------------------------------ // if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_LIGHTMAP(0),&szPath)) { LoadTexture(&pcMesh->piLightmapTexture,&szPath); } // // NORMAL/HEIGHT MAP ------------------------------------------------ // bool bHM = false; if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_NORMALS(0),&szPath)) { LoadTexture(&pcMesh->piNormalTexture,&szPath); } else { if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_TEXTURE_HEIGHT(0),&szPath)) { LoadTexture(&pcMesh->piNormalTexture,&szPath); } else bib = true; bHM = true; } // normal/height maps are sometimes mixed up. Try to detect the type // of the texture automatically if (pcMesh->piNormalTexture) { HMtoNMIfNecessary(pcMesh->piNormalTexture, &pcMesh->piNormalTexture,bHM); } } // check whether a global background texture is contained // in this material. Some loaders set this value ... if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_GLOBAL_BACKGROUND_IMAGE,&szPath)) { CBackgroundPainter::Instance().SetTextureBG(szPath.data); } // BUGFIX: If the shininess is 0.0f disable phong lighting // This is a workaround for some meshes in the DX SDK (e.g. tiny.x) // FIX: Added this check to the x-loader, but the line remains to // catch other loader doing the same ... if (0.0f == pcMesh->fShininess){ pcMesh->eShadingMode = aiShadingMode_Gouraud; } int two_sided = 0; aiGetMaterialInteger(pcMat,AI_MATKEY_TWOSIDED,&two_sided); pcMesh->twosided = (two_sided != 0); // check whether we have already a material using the same // shader. This will decrease loading time rapidly ... for (unsigned int i = 0; i < g_pcAsset->pcScene->mNumMeshes;++i) { if (g_pcAsset->pcScene->mMeshes[i] == pcSource) { break; } AssetHelper::MeshHelper* pc = g_pcAsset->apcMeshes[i]; if ((pcMesh->piDiffuseTexture != NULL ? true : false) != (pc->piDiffuseTexture != NULL ? true : false)) continue; if ((pcMesh->piSpecularTexture != NULL ? true : false) != (pc->piSpecularTexture != NULL ? true : false)) continue; if ((pcMesh->piAmbientTexture != NULL ? true : false) != (pc->piAmbientTexture != NULL ? true : false)) continue; if ((pcMesh->piEmissiveTexture != NULL ? true : false) != (pc->piEmissiveTexture != NULL ? true : false)) continue; if ((pcMesh->piNormalTexture != NULL ? true : false) != (pc->piNormalTexture != NULL ? true : false)) continue; if ((pcMesh->piOpacityTexture != NULL ? true : false) != (pc->piOpacityTexture != NULL ? true : false)) continue; if ((pcMesh->piShininessTexture != NULL ? true : false) != (pc->piShininessTexture != NULL ? true : false)) continue; if ((pcMesh->piLightmapTexture != NULL ? true : false) != (pc->piLightmapTexture != NULL ? true : false)) continue; if ((pcMesh->eShadingMode != aiShadingMode_Gouraud ? true : false) != (pc->eShadingMode != aiShadingMode_Gouraud ? true : false)) continue; if ((pcMesh->fOpacity != 1.0f ? true : false) != (pc->fOpacity != 1.0f ? true : false)) continue; if (pcSource->HasBones() != g_pcAsset->pcScene->mMeshes[i]->HasBones()) continue; // we can reuse this material if (pc->piEffect) { pcMesh->piEffect = pc->piEffect; pc->bSharedFX = pcMesh->bSharedFX = true; pcMesh->piEffect->AddRef(); return 2; } } m_iShaderCount++; // build macros for the HLSL compiler unsigned int iCurrent = 0; if (pcMesh->piDiffuseTexture) { sMacro[iCurrent].Name = "AV_DIFFUSE_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; if (mapU == aiTextureMapMode_Wrap) sMacro[iCurrent].Name = "AV_WRAPU"; else if (mapU == aiTextureMapMode_Mirror) sMacro[iCurrent].Name = "AV_MIRRORU"; else // if (mapU == aiTextureMapMode_Clamp) sMacro[iCurrent].Name = "AV_CLAMPU"; sMacro[iCurrent].Definition = "1"; ++iCurrent; if (mapV == aiTextureMapMode_Wrap) sMacro[iCurrent].Name = "AV_WRAPV"; else if (mapV == aiTextureMapMode_Mirror) sMacro[iCurrent].Name = "AV_MIRRORV"; else // if (mapV == aiTextureMapMode_Clamp) sMacro[iCurrent].Name = "AV_CLAMPV"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if (pcMesh->piSpecularTexture) { sMacro[iCurrent].Name = "AV_SPECULAR_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if (pcMesh->piAmbientTexture) { sMacro[iCurrent].Name = "AV_AMBIENT_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if (pcMesh->piEmissiveTexture) { sMacro[iCurrent].Name = "AV_EMISSIVE_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } char buff[32]; if (pcMesh->piLightmapTexture) { sMacro[iCurrent].Name = "AV_LIGHTMAP_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; int idx; if(AI_SUCCESS == aiGetMaterialInteger(pcMat,AI_MATKEY_UVWSRC_LIGHTMAP(0),&idx) && idx >= 1 && pcSource->mTextureCoords[idx]) { sMacro[iCurrent].Name = "AV_TWO_UV"; sMacro[iCurrent].Definition = "1"; ++iCurrent; sMacro[iCurrent].Definition = "IN.TexCoord1"; } else sMacro[iCurrent].Definition = "IN.TexCoord0"; sMacro[iCurrent].Name = "AV_LIGHTMAP_TEXTURE_UV_COORD"; ++iCurrent;float f= 1.f; aiGetMaterialFloat(pcMat,AI_MATKEY_TEXBLEND_LIGHTMAP(0),&f); sprintf(buff,"%f",f); sMacro[iCurrent].Name = "LM_STRENGTH"; sMacro[iCurrent].Definition = buff; ++iCurrent; } if (pcMesh->piNormalTexture && !bib) { sMacro[iCurrent].Name = "AV_NORMAL_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if (pcMesh->piOpacityTexture) { sMacro[iCurrent].Name = "AV_OPACITY_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; if (pcMesh->piOpacityTexture == pcMesh->piDiffuseTexture) { sMacro[iCurrent].Name = "AV_OPACITY_TEXTURE_REGISTER_MASK"; sMacro[iCurrent].Definition = "a"; ++iCurrent; } else { sMacro[iCurrent].Name = "AV_OPACITY_TEXTURE_REGISTER_MASK"; sMacro[iCurrent].Definition = "r"; ++iCurrent; } } if (pcMesh->eShadingMode != aiShadingMode_Gouraud && !g_sOptions.bNoSpecular) { sMacro[iCurrent].Name = "AV_SPECULAR_COMPONENT"; sMacro[iCurrent].Definition = "1"; ++iCurrent; if (pcMesh->piShininessTexture) { sMacro[iCurrent].Name = "AV_SHININESS_TEXTURE"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } } if (1.0f != pcMesh->fOpacity) { sMacro[iCurrent].Name = "AV_OPACITY"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } if( pcSource->HasBones()) { sMacro[iCurrent].Name = "AV_SKINNING"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } // If a cubemap is active, we'll need to lookup it for calculating // a physically correct reflection if (CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode()) { sMacro[iCurrent].Name = "AV_SKYBOX_LOOKUP"; sMacro[iCurrent].Definition = "1"; ++iCurrent; } sMacro[iCurrent].Name = NULL; sMacro[iCurrent].Definition = NULL; // compile the shader if(FAILED( D3DXCreateEffect(g_piDevice, g_szMaterialShader.c_str(),(UINT)g_szMaterialShader.length(), (const D3DXMACRO*)sMacro,NULL,0,NULL,&pcMesh->piEffect,&piBuffer))) { // failed to compile the shader if( piBuffer) { MessageBox(g_hDlg,(LPCSTR)piBuffer->GetBufferPointer(),"HLSL",MB_OK); piBuffer->Release(); } // use the default material instead if (g_piDefaultEffect) { pcMesh->piEffect = g_piDefaultEffect; g_piDefaultEffect->AddRef(); } // get the name of the material and use it in the log message if(AI_SUCCESS == aiGetMaterialString(pcMat,AI_MATKEY_NAME,&szPath) && '\0' != szPath.data[0]) { std::string sz = "[ERROR] Unable to load material: "; sz.append(szPath.data); CLogDisplay::Instance().AddEntry(sz); } else { CLogDisplay::Instance().AddEntry("Unable to load material: UNNAMED"); } return 0; } else { // use Fixed Function effect when working with shaderless cards if( g_sCaps.PixelShaderVersion < D3DPS_VERSION(2,0)) pcMesh->piEffect->SetTechnique( "MaterialFX_FF"); } if( piBuffer) piBuffer->Release(); // now commit all constants to the shader // // This is not necessary for shared shader. Shader constants for // shared shaders are automatically recommited before the shader // is being used for a particular mesh if (1.0f != pcMesh->fOpacity) pcMesh->piEffect->SetFloat("TRANSPARENCY",pcMesh->fOpacity); if (pcMesh->eShadingMode != aiShadingMode_Gouraud && !g_sOptions.bNoSpecular) { pcMesh->piEffect->SetFloat("SPECULARITY",pcMesh->fShininess); pcMesh->piEffect->SetFloat("SPECULAR_STRENGTH",pcMesh->fSpecularStrength); } pcMesh->piEffect->SetVector("DIFFUSE_COLOR",&pcMesh->vDiffuseColor); pcMesh->piEffect->SetVector("SPECULAR_COLOR",&pcMesh->vSpecularColor); pcMesh->piEffect->SetVector("AMBIENT_COLOR",&pcMesh->vAmbientColor); pcMesh->piEffect->SetVector("EMISSIVE_COLOR",&pcMesh->vEmissiveColor); if (pcMesh->piDiffuseTexture) pcMesh->piEffect->SetTexture("DIFFUSE_TEXTURE",pcMesh->piDiffuseTexture); if (pcMesh->piOpacityTexture) pcMesh->piEffect->SetTexture("OPACITY_TEXTURE",pcMesh->piOpacityTexture); if (pcMesh->piSpecularTexture) pcMesh->piEffect->SetTexture("SPECULAR_TEXTURE",pcMesh->piSpecularTexture); if (pcMesh->piAmbientTexture) pcMesh->piEffect->SetTexture("AMBIENT_TEXTURE",pcMesh->piAmbientTexture); if (pcMesh->piEmissiveTexture) pcMesh->piEffect->SetTexture("EMISSIVE_TEXTURE",pcMesh->piEmissiveTexture); if (pcMesh->piNormalTexture) pcMesh->piEffect->SetTexture("NORMAL_TEXTURE",pcMesh->piNormalTexture); if (pcMesh->piShininessTexture) pcMesh->piEffect->SetTexture("SHININESS_TEXTURE",pcMesh->piShininessTexture); if (pcMesh->piLightmapTexture) pcMesh->piEffect->SetTexture("LIGHTMAP_TEXTURE",pcMesh->piLightmapTexture); if (CBackgroundPainter::TEXTURE_CUBE == CBackgroundPainter::Instance().GetMode()){ pcMesh->piEffect->SetTexture("lw_tex_envmap",CBackgroundPainter::Instance().GetTexture()); } return 1; }
// ----------------------------------------------------------------------------------- // Implementation of the assimp info utility to print basic file info int Assimp_Info (const char* const* params, unsigned int num) { if (num < 1) { printf("assimp info: Invalid number of arguments. " "See \'assimp info --help\'\n"); return 1; } // --help if (!strcmp( params[0],"-h")||!strcmp( params[0],"--help")||!strcmp( params[0],"-?") ) { printf("%s",AICMD_MSG_INFO_HELP_E); return 0; } // asssimp info <file> [-r] if (num < 1) { printf("assimp info: Invalid number of arguments. " "See \'assimp info --help\'\n"); return 1; } const std::string in = std::string(params[0]); // do maximum post-processing unless -r was specified ImportData import; import.ppFlags = num>1&&(!strcmp(params[1],"--raw")||!strcmp(params[1],"-r")) ? 0 : aiProcessPreset_TargetRealtime_MaxQuality; // import the main model import.log = true; import.showLog = true; const aiScene* scene = ImportModel(import,in); if (!scene) { printf("assimp info: Unable to load input file %s\n", in.c_str()); return 5; } aiMemoryInfo mem; globalImporter->GetMemoryRequirements(mem); static const char* format_string = "Memory consumption: %i B\n" "Nodes: %i\n" "Maximum depth %i\n" "Meshes: %i\n" "Animations: %i\n" "Textures (embed.): %i\n" "Materials: %i\n" "Cameras: %i\n" "Lights: %i\n" "Vertices: %i\n" "Faces: %i\n" "Bones: %i\n" "Animation Channels: %i\n" "Primitive Types: %s\n" "Average faces/mesh %i\n" "Average verts/mesh %i\n" "Minimum point (%f %f %f)\n" "Maximum point (%f %f %f)\n" "Center point (%f %f %f)\n" ; aiVector3D special_points[3]; FindSpecialPoints(scene,special_points); printf(format_string, mem.total, CountNodes(scene->mRootNode), GetMaxDepth(scene->mRootNode), scene->mNumMeshes, scene->mNumAnimations, scene->mNumTextures, scene->mNumMaterials, scene->mNumCameras, scene->mNumLights, CountVertices(scene), CountFaces(scene), CountBones(scene), CountAnimChannels(scene), FindPTypes(scene).c_str(), GetAvgFacePerMesh(scene), GetAvgVertsPerMesh(scene), special_points[0][0],special_points[0][1],special_points[0][2], special_points[1][0],special_points[1][1],special_points[1][2], special_points[2][0],special_points[2][1],special_points[2][2] ) ; unsigned int total=0; for(unsigned int i = 0;i < scene->mNumMaterials; ++i) { aiString name; if (AI_SUCCESS==aiGetMaterialString(scene->mMaterials[i],AI_MATKEY_NAME,&name)) { printf("%s\n \'%s\'",(total++?"":"\nNamed Materials:" ),name.data); } } if(total) { printf("\n"); } total=0; for(unsigned int i = 0;i < scene->mNumMaterials; ++i) { aiString name; static const aiTextureType types[] = { aiTextureType_NONE, aiTextureType_DIFFUSE, aiTextureType_SPECULAR, aiTextureType_AMBIENT, aiTextureType_EMISSIVE, aiTextureType_HEIGHT, aiTextureType_NORMALS, aiTextureType_SHININESS, aiTextureType_OPACITY, aiTextureType_DISPLACEMENT, aiTextureType_LIGHTMAP, aiTextureType_REFLECTION, aiTextureType_UNKNOWN }; for(unsigned int type = 0; type < sizeof(types)/sizeof(types[0]); ++type) { for(unsigned int idx = 0;AI_SUCCESS==aiGetMaterialString(scene->mMaterials[i], AI_MATKEY_TEXTURE(types[type],idx),&name); ++idx) { printf("%s\n \'%s\'",(total++?"":"\nTexture Refs:" ),name.data); } } } if(total) { printf("\n"); } total=0; for(unsigned int i = 0;i < scene->mNumAnimations; ++i) { if (scene->mAnimations[i]->mName.length) { printf("%s\n \'%s\'",(total++?"":"\nNamed Animations:" ),scene->mAnimations[i]->mName.data); } } if(total) { printf("\n"); } printf("\nNode hierarchy:\n"); unsigned int cline=0; PrintHierarchy(scene->mRootNode,20,1000,cline); printf("\n"); return 0; }
// ----------------------------------------------------------------------------------- // Implementation of the assimp info utility to print basic file info int Assimp_Info(const aiScene* scene, Assimp::Importer *globalImporter) { aiMemoryInfo mem; globalImporter->GetMemoryRequirements(mem); static const char* format_string = "Memory consumption: %i B\n" "Nodes: %i\n" "Maximum depth %i\n" "Meshes: %i\n" "Animations: %i\n" "Textures (embed.): %i\n" "Materials: %i\n" "Cameras: %i\n" "Lights: %i\n" "Vertices: %i\n" "Faces: %i\n" "Bones: %i\n" "Animation Channels: %i\n" "Primitive Types: %s\n" "Average faces/mesh %i\n" "Average verts/mesh %i\n" "Minimum point (%f %f %f)\n" "Maximum point (%f %f %f)\n" "Center point (%f %f %f)\n" ; aiVector3D special_points[3]; FindSpecialPoints(scene, special_points); fprintf(stderr, format_string, mem.total, CountNodes(scene->mRootNode), GetMaxDepth(scene->mRootNode), scene->mNumMeshes, scene->mNumAnimations, scene->mNumTextures, scene->mNumMaterials, scene->mNumCameras, scene->mNumLights, CountVertices(scene), CountFaces(scene), CountBones(scene), CountAnimChannels(scene), FindPTypes(scene).c_str(), GetAvgFacePerMesh(scene), GetAvgVertsPerMesh(scene), special_points[0][0], special_points[0][1], special_points[0][2], special_points[1][0], special_points[1][1], special_points[1][2], special_points[2][0], special_points[2][1], special_points[2][2] ) ; #define FULLLOG #ifdef FULLLOG unsigned int total = 0; for (unsigned int i = 0; i < scene->mNumMaterials; ++i) { aiString name; if (AI_SUCCESS == aiGetMaterialString(scene->mMaterials[i], AI_MATKEY_NAME, &name)) { fprintf(stderr, "%s\n \'%s\'", (total++ ? "" : "\nNamed Materials:"), name.data); } } if (total) { fprintf(stderr, "\n"); } total = 0; for (unsigned int i = 0; i < scene->mNumMaterials; ++i) { aiString name; static const aiTextureType types[] = { aiTextureType_NONE, aiTextureType_DIFFUSE, aiTextureType_SPECULAR, aiTextureType_AMBIENT, aiTextureType_EMISSIVE, aiTextureType_HEIGHT, aiTextureType_NORMALS, aiTextureType_SHININESS, aiTextureType_OPACITY, aiTextureType_DISPLACEMENT, aiTextureType_LIGHTMAP, aiTextureType_REFLECTION, aiTextureType_UNKNOWN }; for (unsigned int type = 0; type < sizeof(types) / sizeof(types[0]); ++type) { for (unsigned int idx = 0; AI_SUCCESS == aiGetMaterialString(scene->mMaterials[i], AI_MATKEY_TEXTURE(types[type], idx), &name); ++idx) { fprintf(stderr, "%s\n \'%s\'", (total++ ? "" : "\nTexture Refs:"), name.data); } } } if (total) { fprintf(stderr, "\n"); } total = 0; for (unsigned int i = 0; i < scene->mNumAnimations; ++i) { if (scene->mAnimations[i]->mName.length) { fprintf(stderr, "%s\n \'%s\'", (total++ ? "" : "\nNamed Animations:"), scene->mAnimations[i]->mName.data); } } if (total) { fprintf(stderr, "\n"); } /* fprintf(stderr, "\nNode hierarchy:\n"); unsigned int cline = 0; PrintHierarchy(scene->mRootNode, 20, 1000, cline); */ #endif fprintf(stderr, "\n"); return 0; }