std::vector<std::shared_ptr<Material>> ModelLoader::loadMaterials(const aiScene* scene, std::map<std::string, std::shared_ptr<Texture>> textures) { std::vector<std::shared_ptr<Material>> output; for (unsigned int materialIndex = 0; materialIndex < scene->mNumMaterials; ++materialIndex) { std::shared_ptr<BasicLightingMaterial> currentGeneratedMaterial(std::make_shared<BasicLightingMaterial>()); const aiMaterial *material = scene->mMaterials[materialIndex]; aiColor4D aiDiffuse; aiColor4D aiSpecular; aiColor4D aiAmbient; aiColor4D aiEmission; glm::vec4 diffuse(1.0f, 1.0f, 1.0f, 1.0f); glm::vec4 specular(0.0f, 0.0f, 0.0f, 0.0f); glm::vec4 ambient(0.0f, 0.0f, 0.0f, 1.0f); glm::vec4 emission(0.0f, 0.0f, 0.0f, 0.0f); float shininess; float shininessStrength; unsigned int max = 1; aiString texturePath; std::shared_ptr<Texture> texture(std::make_shared<Texture>()); if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &aiDiffuse)) { diffuse = aiColor4DToGlmVec4(aiDiffuse); } if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, &aiSpecular)) { specular = aiColor4DToGlmVec4(aiSpecular); } if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &aiAmbient)) { ambient = aiColor4DToGlmVec4(aiAmbient); } if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_EMISSIVE, &aiEmission)) { emission = aiColor4DToGlmVec4(aiEmission); } aiGetMaterialFloatArray(material, AI_MATKEY_SHININESS, &shininess, &max); aiGetMaterialFloatArray(material, AI_MATKEY_SHININESS_STRENGTH, &shininessStrength, &max); shininess *= shininessStrength; if (AI_SUCCESS == material->GetTexture(aiTextureType_DIFFUSE, 0, &texturePath)) { std::string path(texturePath.data); texture = textures[path]; } currentGeneratedMaterial->setDiffuse(diffuse); currentGeneratedMaterial->setSpecular(specular); currentGeneratedMaterial->setAmbient(ambient); currentGeneratedMaterial->setEmission(emission); currentGeneratedMaterial->setShininess(shininess); currentGeneratedMaterial->setTexture(texture); output.push_back(currentGeneratedMaterial); } return output; }
void InitializeAssetMaterial( ASSET_MATERIAL* material, ASSET_MODEL* model, struct aiMaterial* material_ref, int num_indices, int index, void* application_context ) { struct aiColor4D color; aiGetMaterialColor(material_ref, AI_MATKEY_COLOR_AMBIENT, &color); material->ambient[0] = color.r; material->ambient[1] = color.g; material->ambient[2] = color.b; material->ambient[3] = 1; aiGetMaterialColor(material_ref, AI_MATKEY_COLOR_DIFFUSE, &color); material->diffuse[0] = color.r; material->diffuse[1] = color.g; material->diffuse[2] = color.b; material->diffuse[3] = 1; aiGetMaterialColor(material_ref, AI_MATKEY_COLOR_SPECULAR, &color); material->specular[0] = color.r; material->specular[1] = color.g; material->specular[2] = color.b; material->specular[3] = 1; aiGetMaterialFloatArray(material_ref, AI_MATKEY_SHININESS, &material->shininess, NULL); material->material = material_ref; material->num_indices = num_indices; material->interface_data.index = index; AssetMaterialSetTextures(material); }
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; }
static CMaterial *cmaterial( const aiMaterial *mat,CShader *shader ){ CMaterial *cmat=new CMaterial; cmat->SetShader( shader ); aiColor4D color; aiString path; if( aiGetMaterialTexture( mat,aiTextureType_DIFFUSE,0,&path,0,0,0,0,0 )==AI_SUCCESS ){ cmat->SetTexture( "DiffuseMap",App.TextureUtil()->LoadTexture( cstring( path ) ) ); }else if( aiGetMaterialColor( mat,AI_MATKEY_COLOR_DIFFUSE,&color )==AI_SUCCESS ){ cmat->SetColor( "DiffuseColor",ccolor( color ) ); } if( aiGetMaterialTexture( mat,aiTextureType_SPECULAR,0,&path,0,0,0,0,0 )==AI_SUCCESS ){ cmat->SetTexture( "SpecularMap",App.TextureUtil()->LoadTexture( cstring( path ) ) ); }else if( aiGetMaterialColor( mat,AI_MATKEY_COLOR_SPECULAR,&color )==AI_SUCCESS ){ cmat->SetColor( "SpecularColor",ccolor( color ) ); } if( aiGetMaterialTexture( mat,aiTextureType_EMISSIVE,0,&path,0,0,0,0,0 )==AI_SUCCESS ){ cmat->SetTexture( "EmissiveMap",App.TextureUtil()->LoadTexture( cstring( path ) ) ); }else if( aiGetMaterialColor( mat,AI_MATKEY_COLOR_EMISSIVE,&color )==AI_SUCCESS ){ cmat->SetColor( "EmissiveColor",ccolor( color ) ); } if( aiGetMaterialTexture( mat,aiTextureType_NORMALS,0,&path,0,0,0,0,0 )==AI_SUCCESS ){ cmat->SetTexture( "NormalMap",App.TextureUtil()->LoadTexture( cstring( path ) ) ); } return cmat; }
void apply_material(const aiMaterial *mtl) { float c[4]; GLenum fill_mode; int ret1, ret2; aiColor4D diffuse; aiColor4D specular; aiColor4D ambient; aiColor4D emission; float shininess, strength; int two_sided; int wireframe; unsigned int max; set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse)) color4_to_float4(&diffuse, c); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, c); set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular)) color4_to_float4(&specular, c); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c); set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient)) color4_to_float4(&ambient, c); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, c); set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission)) color4_to_float4(&emission, c); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, c); max = 1; ret1 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max); max = 1; ret2 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS_STRENGTH, &strength, &max); if((ret1 == AI_SUCCESS) && (ret2 == AI_SUCCESS)) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess * strength); else { glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f); set_float4(c, 0.0f, 0.0f, 0.0f, 0.0f); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c); } max = 1; if(AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_ENABLE_WIREFRAME, &wireframe, &max)) fill_mode = wireframe ? GL_LINE : GL_FILL; else fill_mode = GL_FILL; glPolygonMode(GL_FRONT_AND_BACK, fill_mode); max = 1; if((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)) && two_sided) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); }
void Model::processMaterial(const aiScene* scene) { aiColor4D ka, kd, ks; GLuint texDiffuse, texNormal, texSpecular; aiString str; for(GLuint i = 0; i < scene->mNumMaterials; i++) { if(AI_SUCCESS == scene->mMaterials[i]->GetTexture(aiTextureType_DIFFUSE, 0, &str)) texDiffuse = TextureFromFile(str.C_Str(), this->directory); else texDiffuse = 0; if(AI_SUCCESS == scene->mMaterials[i]->GetTexture(aiTextureType_HEIGHT, 0, &str)) texNormal = TextureFromFile(str.C_Str(), this->directory); else texNormal = 0; if(AI_SUCCESS == scene->mMaterials[i]->GetTexture(aiTextureType_SPECULAR, 0, &str)) texSpecular = TextureFromFile(str.C_Str(), this->directory); else texSpecular = 0; aiGetMaterialColor(scene->mMaterials[i], AI_MATKEY_COLOR_AMBIENT, &ka); aiGetMaterialColor(scene->mMaterials[i], AI_MATKEY_COLOR_DIFFUSE, &kd); aiGetMaterialColor(scene->mMaterials[i], AI_MATKEY_COLOR_SPECULAR, &ks); m_material.push_back(Material(ka, kd, ks, texDiffuse, texNormal, texSpecular)); } }
bool Mesh::AssimpLoader::LoadAssimpMesh(Ptr<Mesh> mesh, const aiMesh* aimesh, const aiScene* scene) { auto assetManager = AssetManager::GetInstance(); for (size_t i = 0; i < aimesh->mNumVertices; i++) { mesh->vertices.push_back(Vector3f(aimesh->mVertices[i].x, aimesh->mVertices[i].y, aimesh->mVertices[i].z)); mesh->normals.push_back(Vector3f(aimesh->mNormals[i].x, aimesh->mNormals[i].y, aimesh->mNormals[i].z)); mesh->tangents.push_back(Vector3f(aimesh->mTangents[i].x, aimesh->mTangents[i].y, aimesh->mTangents[i].z)); for (size_t j = 0; j < aimesh->GetNumUVChannels() && j < 4; j++) { mesh->uv[j].push_back(Vector2f(aimesh->mTextureCoords[j][i].x, aimesh->mTextureCoords[j][i].y)); } } for (size_t i = 0; i < aimesh->mNumFaces; i++) { for (size_t j = 0; j < aimesh->mFaces[i].mNumIndices; j++) mesh->triangles.push_back(aimesh->mFaces[i].mIndices[j]); } // TODO : 支持更多材质样式 if (aimesh->mMaterialIndex >= 0) { aiMaterial* material = scene->mMaterials[aimesh->mMaterialIndex]; aiColor4D diffuse, specular, ambient, emissive; ai_real shininess; unsigned int max = 1; if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &diffuse)) mesh->material.diffuse = Vector4f(diffuse.r, diffuse.g, diffuse.b, diffuse.a); if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, &specular)) mesh->material.specular = Vector4f(specular.r, specular.g, specular.b, specular.a); if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &ambient)) mesh->material.ambient = Vector4f(ambient.r, ambient.g, ambient.b, ambient.a); if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_EMISSIVE, &emissive)) mesh->material.emissive = Vector4f(emissive.r, emissive.g, emissive.b, emissive.a); if (AI_SUCCESS == aiGetMaterialFloatArray(material, AI_MATKEY_SHININESS, &shininess, &max)) mesh->material.shininess = shininess; for (size_t i = 0; i < material->GetTextureCount(aiTextureType_DIFFUSE); i++) { aiString path; material->GetTexture(aiTextureType_DIFFUSE, i, &path); String fullPath = this->dir + "/" + path.data; Ptr<Texture> tex = assetManager->CreateTextureFromFile(fullPath); if (tex == nullptr) return false; mesh->material.texture = tex; break; // 目前只支持一张diffuse texture } } return true; }
void Model::applyMateriale(const aiMaterial* materiale, Materiale& meshMat) { std::cout << "Applying mesh material" << std::endl; aiString texPath; if(AI_SUCCESS == materiale->GetTexture(aiTextureType_DIFFUSE, 0, &texPath)){ std::string fullPath = folderPath + texPath.C_Str(); // Check if this texture was already loaded. std::map<std::string, unsigned int>::iterator it = textureID_map.find(fullPath); if(it == textureID_map.end()){ Image img; loadImage(img, fullPath.c_str()); meshMat.textureID = loadTexture(img); }else{ meshMat.textureID = it->second; } std::cout << "loaded texture: " << meshMat.textureID << std::endl; }else{ Image img; // generate white dummy 16x16 texture so we don't have to // switch shaders for untextured meshes img.components = 3; img.height = 1; img.width = 1; img.data.resize(1 * 1 * 3); for(int i = 0; i < 1 * 1 * 3; i++){ img.data[i] = (unsigned char) 255; } meshMat.textureID = loadTexture(img); std::cout << "Created a dummy texture: " << meshMat.textureID << std::endl; } // Fetch material specs aiColor4D diffuse; if(AI_SUCCESS == aiGetMaterialColor(materiale, AI_MATKEY_COLOR_DIFFUSE, &diffuse)){ meshMat.diffuse = glm::vec3(diffuse.r, diffuse.g, diffuse.b); } aiColor4D ambient; if(AI_SUCCESS == aiGetMaterialColor(materiale, AI_MATKEY_COLOR_AMBIENT, &ambient)){ meshMat.ambient = glm::vec3(ambient.r, ambient.g, ambient.b); } aiColor4D specular; if(AI_SUCCESS == aiGetMaterialColor(materiale, AI_MATKEY_COLOR_SPECULAR, &specular)){ meshMat.specular = glm::vec3(specular.r, specular.g, specular.b); } float shininess = 0.0f; unsigned int max; aiGetMaterialFloatArray(materiale, AI_MATKEY_SHININESS, &shininess, &max); meshMat.shininess = shininess; }
bool CAssimpMesh::ExtractMaterials(const aiScene* pScene) { CCacheResourceManager& res = CCacheResourceManager::Instance(); // Initialize the materials for (uint32 i = 0 ; i < pScene->mNumMaterials ; i++) { CMaterial* material = new CMaterial(); const aiMaterial* pMaterial = pScene->mMaterials[i]; material->diffuseTexture = NULL; if (pMaterial->GetTextureCount(aiTextureType_DIFFUSE) > 0) { aiString path; if (pMaterial->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) { material->diffuseTexture = (CTexture*)res.LoadTexture2D(path.data); } } struct aiColor4D specular, diffuse, ambient; vec4f zero; //diffuse if((AI_SUCCESS == aiGetMaterialColor(pMaterial, AI_MATKEY_COLOR_DIFFUSE, &diffuse))) material->diffuse = vec4f(diffuse.r, diffuse.g, diffuse.b, diffuse.a); else material->diffuse = zero; //ambiant if((AI_SUCCESS == aiGetMaterialColor(pMaterial, AI_MATKEY_COLOR_AMBIENT, &ambient))) material->ambient = vec4f(ambient.r, ambient.g, ambient.b, ambient.a); else material->ambient = zero; //specular if((AI_SUCCESS == aiGetMaterialColor(pMaterial, AI_MATKEY_COLOR_SPECULAR, &specular))) material->specular = vec4f(specular.r, specular.g, specular.b, specular.a); else material->specular = zero; //shininess aiGetMaterialFloat(pMaterial,AI_MATKEY_SHININESS,&material->shininess); if(material->shininess <1.0f) material->shininess = 15; aiGetMaterialFloat(pMaterial,AI_MATKEY_OPACITY,&material->opacity); if(material->opacity< 1.f) material->isTransparent = true; aiGetMaterialInteger(pMaterial,AI_MATKEY_TWOSIDED,&material->twoSided); m_pMeshBuffer->AddMaterial(material); } return true; }
MeshGL::MeshGL(aiScene* p_scene, aiMesh* p_mesh) { vao = vbo_positions = vbo_texcoords = vbo_normals = 0; aiMaterial *p_mtl = p_scene->mMaterials[p_mesh->mMaterialIndex]; aiGetMaterialColor(p_mtl, AI_MATKEY_COLOR_DIFFUSE, &material.diffuse); aiGetMaterialColor(p_mtl, AI_MATKEY_COLOR_SPECULAR, &material.specular); aiGetMaterialColor(p_mtl, AI_MATKEY_COLOR_AMBIENT, &material.ambient); aiGetMaterialColor(p_mtl, AI_MATKEY_COLOR_EMISSIVE, &material.emission); unsigned int max; aiGetMaterialFloatArray(p_mtl, AI_MATKEY_SHININESS, &material.shininess, &max); aiGetMaterialFloatArray(p_mtl, AI_MATKEY_SHININESS_STRENGTH, &material.strength, &max); glGenVertexArrays(1, &vao); glBindVertexArray(vao); if(p_mesh->HasPositions()) { nVertices = p_mesh->mNumVertices; glGenBuffers(1, &vbo_positions); glBindBuffer(GL_ARRAY_BUFFER, vbo_positions); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*p_mesh->mNumVertices, &p_mesh->mVertices[0], GL_STATIC_DRAW); glEnableVertexAttribArray(0); glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), BUFFER_OFFSET(0)); } if(p_mesh->HasNormals()) { glGenBuffers(1, &vbo_normals); glBindBuffer(GL_ARRAY_BUFFER, vbo_normals); glBufferData(GL_ARRAY_BUFFER, sizeof(GLfloat)*3*p_mesh->mNumVertices, &p_mesh->mNormals[0], GL_STATIC_DRAW); glEnableVertexAttribArray(1); glVertexAttribPointer(1, 3, GL_FLOAT, GL_FALSE, 3*sizeof(GLfloat), BUFFER_OFFSET(0)); } if(p_mesh->HasFaces()) { nTriangles = p_mesh->mNumFaces; std::vector<GLuint> indices; for(GLuint i=0 ; i<p_mesh->mNumFaces ; i++) { for(GLuint j=0 ; j<p_mesh->mFaces[i].mNumIndices ; j++) { indices.push_back(p_mesh->mFaces[i].mIndices[j]); } } GLuint n = indices.size(); glGenBuffers(1, &vbo_indices); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, vbo_indices); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLuint)*3*nTriangles, indices.data(), GL_STATIC_DRAW); } }
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 GLAssimpMeshBinding::loadMaterials(aiMaterial *material) { aiColor4D aiCol; if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, (aiColor4D*)&aiCol)) { matDiffuse.x = aiCol.r; matDiffuse.y = aiCol.g; matDiffuse.z = aiCol.b; matDiffuse.w = aiCol.a; } if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, (aiColor4D*)&aiCol)) { matSpecular.x = aiCol.r; matSpecular.y = aiCol.g; matSpecular.z = aiCol.b; matSpecular.w = aiCol.a; } if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, (aiColor4D*)&aiCol)) { matAmbient.x = aiCol.r; matAmbient.y = aiCol.g; matAmbient.z = aiCol.b; matAmbient.w = aiCol.a; } if (AI_SUCCESS == aiGetMaterialColor(material, AI_MATKEY_COLOR_EMISSIVE, (aiColor4D*)&aiCol)) { matEmissive.x = aiCol.r; matEmissive.y = aiCol.g; matEmissive.z = aiCol.b; matEmissive.w = aiCol.a; } if (AI_SUCCESS != aiGetMaterialFloat(material, AI_MATKEY_OPACITY, &matOpacity)) { matOpacity = 1.0f; } if (AI_SUCCESS != aiGetMaterialFloat(material, AI_MATKEY_SHININESS, &matShininess)) { matShininess = 1.0f; } aiString path; F32 blend; aiGetMaterialTexture(material, aiTextureType_DIFFUSE, 0, &path, NULL, NULL, &blend, NULL, NULL, NULL); matDiffuseTexture = strDuplicate(path.data); if (debugAssimp) { char buf[1024]; sprintf(buf, "Diffuse: %f %f %f %f", matDiffuse.x, matDiffuse.y, matDiffuse.z, matDiffuse.w); logmsg(buf); sprintf(buf, "Specular: %f %f %f %f", matSpecular.x, matSpecular.y, matSpecular.z, matSpecular.w); logmsg(buf); sprintf(buf, "Ambient: %f %f %f %f", matAmbient.x, matAmbient.y, matAmbient.z, matAmbient.w); logmsg(buf); sprintf(buf, "Emissive: %f %f %f %f", matEmissive.x, matEmissive.y, matEmissive.z, matEmissive.w); logmsg(buf); sprintf(buf, "Opacity: %f Shininess: %f", matOpacity, matShininess); logmsg(buf); } }
static int GetColor(lua_State *L, material_t *material, const char* pKey, unsigned int type, unsigned int index) { color4_t color; if(!material) material = checkmaterial(L, 1); if(aiGetMaterialColor(material, pKey, type, index, &color) != AI_SUCCESS) return 0; return pushcolor4(L, &color, 0); }
inline void AssimpScene::retrieveColor( const aiMaterial * mtl , const char * pKey , const unsigned int type , const unsigned int index , QVector4D & color , const float r , const float g , const float b , const float a) { aiColor4D acolor; if (AI_SUCCESS == aiGetMaterialColor(mtl, pKey, type, index, &acolor)) { color.setX(acolor.r); color.setY(acolor.g); color.setZ(acolor.b); color.setW(acolor.a); } else { color.setX(r); color.setY(g); color.setZ(b); color.setW(a); } }
int AssimpScene::ai_get_mat_color(const aiMaterial * mat, const char * key, int type, int index, float * color) { aiColor4D colorAI; hpr_set_float4(color, 1.0f, 0.0f, 0.0f, 1.0f); // default color red if (AI_SUCCESS == aiGetMaterialColor(mat,key,type,index,&colorAI)) { } return 0; }
Ogre::MaterialPtr AssetLoader::createMaterial(Ogre::String name, int index, aiMaterial* mat) { Ogre::MaterialManager* omatMgr = Ogre::MaterialManager::getSingletonPtr(); std::ostringstream matname; matname << name.data() << "_mat"; matname.widen(4); matname.fill('0'); matname << index; Ogre::String matName = matname.str(); Ogre::ResourceManager::ResourceCreateOrRetrieveResult status = omatMgr->createOrRetrieve(matName, Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME, true); Ogre::MaterialPtr omat = status.first; aiColor4D clr(1.0, 1.0, 1.0, 1.0); aiGetMaterialColor(mat, AI_MATKEY_COLOR_DIFFUSE, &clr); omat->getTechnique(0)->getPass(0)->setDiffuse(clr.r, clr.g, clr.b, clr.a); aiGetMaterialColor(mat, AI_MATKEY_COLOR_SPECULAR, &clr); omat->getTechnique(0)->getPass(0)->setSpecular(clr.r, clr.g, clr.b, clr.a); aiGetMaterialColor(mat, AI_MATKEY_COLOR_AMBIENT, &clr); omat->getTechnique(0)->getPass(0)->setAmbient(clr.r, clr.g, clr.b); aiGetMaterialColor(mat, AI_MATKEY_COLOR_EMISSIVE, &clr); omat->getTechnique(0)->getPass(0)->setSelfIllumination(clr.r, clr.g, clr.b); omat->getTechnique(0)->getPass(0)->setShadingMode(Ogre::SO_GOURAUD); omat->getTechnique(0)->getPass(0)->setSceneBlending(Ogre::SBT_TRANSPARENT_ALPHA); omat->getTechnique(0)->getPass(0)->setLightingEnabled(true); omat->getTechnique(0)->getPass(0)->setDepthCheckEnabled(true); //omat->getTechnique(0)->getPass(0)->setVertexColourTracking(Ogre::TVC_DIFFUSE); aiString path; aiReturn res = mat->GetTexture(aiTextureType_DIFFUSE, 0, &path); if (res == AI_SUCCESS) { Ogre::TextureUnitState* stateBase = omat->getTechnique(0)->getPass(0)->createTextureUnitState(); stateBase->setColourOperation(Ogre::LBO_MODULATE); stateBase->setTextureName(path.data); } return omat; }
void createMaterialData( osg::StateSet* ss, const aiMaterial* aiMtl ) const { aiColor4D c; osg::Material* material = new osg::Material; if ( aiGetMaterialColor(aiMtl, AI_MATKEY_COLOR_AMBIENT, &c)==AI_SUCCESS ) material->setAmbient( osg::Material::FRONT_AND_BACK, osg::Vec4(c.r, c.g, c.b, c.a) ); if ( aiGetMaterialColor(aiMtl, AI_MATKEY_COLOR_DIFFUSE, &c)==AI_SUCCESS ) material->setDiffuse( osg::Material::FRONT_AND_BACK, osg::Vec4(c.r, c.g, c.b, c.a) ); if ( aiGetMaterialColor(aiMtl, AI_MATKEY_COLOR_SPECULAR, &c)==AI_SUCCESS ) material->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4(c.r, c.g, c.b, c.a) ); if ( aiGetMaterialColor(aiMtl, AI_MATKEY_COLOR_EMISSIVE, &c)==AI_SUCCESS ) material->setEmission( osg::Material::FRONT_AND_BACK, osg::Vec4(c.r, c.g, c.b, c.a) ); unsigned int maxValue = 1; float shininess = 0.0f, strength = 1.0f; if ( aiGetMaterialFloatArray(aiMtl, AI_MATKEY_SHININESS, &shininess, &maxValue)==AI_SUCCESS ) { maxValue = 1; if ( aiGetMaterialFloatArray( aiMtl, AI_MATKEY_SHININESS_STRENGTH, &strength, &maxValue )==AI_SUCCESS ) shininess *= strength; material->setShininess( osg::Material::FRONT_AND_BACK, shininess ); } else { material->setShininess( osg::Material::FRONT_AND_BACK, 0.0f ); material->setSpecular( osg::Material::FRONT_AND_BACK, osg::Vec4() ); } ss->setAttributeAndModes( material ); int wireframe = 0; maxValue = 1; if ( aiGetMaterialIntegerArray(aiMtl, AI_MATKEY_ENABLE_WIREFRAME, &wireframe, &maxValue)==AI_SUCCESS ) { ss->setAttributeAndModes( new osg::PolygonMode( osg::PolygonMode::FRONT_AND_BACK, wireframe ? osg::PolygonMode::LINE : osg::PolygonMode::FILL) ); } }
VType *CopyMesh(INDICES &indices, LptaSkin::ID &skinId, const aiScene *scene, MANAGERS &managers) { VType *vertices = new VType(); vector<LptaSkin::ID> skinIds; for (unsigned int matIdx = 0; matIdx < scene->mNumMaterials; ++matIdx) { aiMaterial *mat = scene->mMaterials[matIdx]; aiColor4D diffuse(0.0f, 0.0f, 0.0f, 0.0f); aiColor4D ambient(0.0f, 0.0f, 0.0f, 0.0f); aiColor4D specular(0.0f, 0.0f, 0.0f, 0.0f); aiColor4D emissive(0.0f, 0.0f, 0.0f, 0.0f); float specularPower = 0.0f; mat->Get(AI_MATKEY_COLOR_DIFFUSE, diffuse); mat->Get(AI_MATKEY_COLOR_AMBIENT, ambient); mat->Get(AI_MATKEY_COLOR_SPECULAR, specular); mat->Get(AI_MATKEY_COLOR_EMISSIVE, emissive); mat->Get(AI_MATKEY_SHININESS, specularPower); aiGetMaterialColor(mat, AI_MATKEY_COLOR_EMISSIVE, &emissive); LptaMaterial::ID materialId = managers.material.AddOrRetrieveMaterial( LptaColor(diffuse.r, diffuse.g, diffuse.b, diffuse.a), LptaColor(ambient.r, ambient.g, ambient.b, diffuse.a), LptaColor(specular.r, specular.g, specular.b, specular.a), LptaColor(emissive.r, emissive.g, emissive.b, emissive.a), specularPower ); LptaSkin::TEXTURE_IDS textureIds; // only diffuse texture is supported for now for (unsigned int texIdx = 0; texIdx < mat->GetTextureCount(aiTextureType_DIFFUSE) && texIdx < LptaSkin::MAX_TEXTURES; ++texIdx) { aiString filepath; mat->GetTexture(aiTextureType_DIFFUSE, texIdx, &filepath); LptaTexture::ID textureId = managers.texture.AddOrRetrieveTexture(filepath.C_Str()); textureIds.push_back(textureId); } LptaSkin::ID skinId = managers.skin.AddSkin(materialId, textureIds, false); skinIds.push_back(skinId); } for (unsigned int meshIdx = 0; meshIdx < scene->mNumMeshes; ++meshIdx) { const aiMesh *mesh = scene->mMeshes[meshIdx]; CopyAlgorithm::Copy(*vertices, indices, *mesh, vertices->GetNumVertices()); skinId = skinIds.at(mesh->mMaterialIndex); } return vertices; }
void derp (void) { const struct aiScene *csc; struct aiScene *sc; struct aiMesh *me; GLuint vb, nb, tb; GLuint ctex; GLuint dtex; GLuint fbo; GLuint dpp; GLint dpp_cat0_loc; csc = aiImportFile ("../data/n1.dae", aiProcess_Triangulate | aiProcess_PreTransformVertices); sc = (struct aiScene *)csc; g_xassert (sc); g_xassert (sc->mNumMeshes >= 1); g_xassert (sc->mNumMaterials >= 1); me = sc->mMeshes[0]; g_xassert (aiPrimitiveType_TRIANGLE == me->mPrimitiveTypes); mesh_buffers_extract (me, &vb, &nb, &tb); /* Save Viewport, Modelview, Projection. * Then set them to sane defaults. * * Empirical test indicates projection set to Ortho, * Modelview set to Identity, Viewport set to 0,0,w,h. * * ModelView and Viewport are fine but Projection has to go, * as I plan setting it myself. */ allegro_ffp_restore (RESTORE_MODE_SAVE, RESTORE_SUB_ALLEGRO); /* * Do not do this... * While it is useful to play it safe and save & restore viewport/mst, * I am not actually supposed to set over allegro's stuff, right? * Actually: * Need to use allegro's original settings when using the * drawing routines targeting framebuffer 0. (eg debug_draw_tex_quad) * It is correct to LoadIdentity the projection stack, * but fix said routines to use allegro state. */ glMatrixMode (GL_PROJECTION); glLoadIdentity (); glMatrixMode (GL_MODELVIEW); /* Clear the default framebuffer (Optional) */ glBindFramebufferEXT (GL_FRAMEBUFFER_EXT, 0); glClearColor (0, 0, 1, 1); glClear (GL_COLOR_BUFFER_BIT); /* Create two textures, a color and a depth. */ glGenTextures (1, &ctex); glActiveTexture (GL_TEXTURE0); glBindTexture (GL_TEXTURE_2D, ctex); glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D (GL_TEXTURE_2D, 0, GL_RGBA8, 640, 480, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL); glGenTextures (1, &dtex); glActiveTexture (GL_TEXTURE0); glBindTexture (GL_TEXTURE_2D, dtex); glTexParameterf (GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexImage2D (GL_TEXTURE_2D, 0, GL_DEPTH_COMPONENT24_ARB, 640, 480, 0, GL_DEPTH_COMPONENT, GL_UNSIGNED_INT, NULL); check_gl_error (); /* Create a FBO and attach the newly created textures */ glGenFramebuffersEXT (1, &fbo); glBindFramebufferEXT (GL_FRAMEBUFFER, fbo); glFramebufferTexture2DEXT (GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D, ctex, 0); glFramebufferTexture2DEXT (GL_FRAMEBUFFER, GL_DEPTH_ATTACHMENT, GL_TEXTURE_2D, dtex, 0); g_xassert (GL_FRAMEBUFFER_COMPLETE == glCheckFramebufferStatusEXT (GL_FRAMEBUFFER)); check_gl_error (); /* Create the depth pass program. (Vertex shader only, do nothing) */ dpp = depth_pass_program (); check_gl_error (); struct DepthData ds; ds.fbo = fbo; ds.ab = vb; ds.dpp = dpp; depth_bind (&ds); { glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity (); //glOrtho (-2.5, 2.5, -2.5, 2.5, -5, 5); gluPerspective (90.0f, 1.0f, 1.0f, 5.0f); glMatrixMode (GL_MODELVIEW); glPushMatrix (); glLoadIdentity (); glTranslatef (0.0f, 0.0f, -2.5f); glDrawBuffer (GL_COLOR_ATTACHMENT0); glClear (GL_DEPTH_BUFFER_BIT); glBindBuffer (GL_ARRAY_BUFFER, ds.ab); glVertexAttribPointer (ds._cat0_loc, 3, GL_FLOAT, GL_FALSE, 0, 0); glUseProgram (ds.dpp); glDrawArrays (GL_TRIANGLES, 0, me->mNumFaces * 3); glUseProgram (0); glMatrixMode (GL_PROJECTION); glPopMatrix (); glMatrixMode (GL_MODELVIEW); glPopMatrix (); } depth_unbind (&ds); debug_draw_tex_quad (ctex, 100, 0, 100, 100); debug_draw_tex_quad (dtex, 100, 150, 100, 100); /** * Material stuff, move elsewhere or something. */ struct aiMaterial *mat; struct aiColor4D col_diff; struct aiColor4D col_spec; g_xassert (sc->mNumMaterials >= 1); mat = sc->mMaterials[me->mMaterialIndex]; g_xassert (AI_SUCCESS == aiGetMaterialColor (mat, AI_MATKEY_COLOR_DIFFUSE, &col_diff)); g_xassert (AI_SUCCESS == aiGetMaterialColor (mat, AI_MATKEY_COLOR_SPECULAR, &col_spec)); struct MaterialFbo mf = {0}; mf.tdep = dtex; material_create_fbo (&mf); /** * Warning: * Remember to unbind the depth texture (dtex) from depth_stage fbo, * Rebind it as texture for rendering. (Don't need to unbind?) * (4.4.2) ""A single framebuffer-attachable image may be attached * to multiple framebuffer objects"" * Not sure about whether unbind required. * (4.4.3) Warns about Feedback loops, which do apply. */ struct MaterialData ms = {0}; ms.fbo = mf._fbo; ms.ctex = blender_tex; ms.dtex = dtex; ms.mpp = material_pass_program (); ms.vb = vb; ms.nb = nb; ms.tb = tb; material_bind (&ms); { glMatrixMode (GL_PROJECTION); glPushMatrix (); glLoadIdentity (); //glOrtho (-2.5, 2.5, -2.5, 2.5, -5, 5); gluPerspective (90.0f, 1.0f, 1.0f, 5.0f); glMatrixMode (GL_MODELVIEW); glPushMatrix (); glLoadIdentity (); glTranslatef (0.0f, 0.0f, -2.5f); GLenum material_draw_buffers[] = {GL_COLOR_ATTACHMENT0, GL_COLOR_ATTACHMENT1, GL_COLOR_ATTACHMENT2}; glDrawBuffers (3, material_draw_buffers); glBindBuffer (GL_ARRAY_BUFFER, ms.vb); glVertexAttribPointer (ms._cat0_loc, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer (GL_ARRAY_BUFFER, ms.nb); glVertexAttribPointer (ms._cat1_nor, 3, GL_FLOAT, GL_FALSE, 0, 0); glBindBuffer (GL_ARRAY_BUFFER, ms.tb); glVertexAttribPointer (ms._cat2_tex, 3, GL_FLOAT, GL_FALSE, 0, 0); /* Program needs to be active to load Uniforms */ glUseProgram (ms.mpp); /** * Texture unit 0, * make sure diffuse / color texture is glBindTexture'd * while glActiveTexture is zero. */ glUniform1i (ms._tex0, 0); /* Also go load diffuse, specular out of the aiColor4Ds */ glUniform4f (ms._diffuse, col_diff.r, col_diff.g, col_diff.b, col_diff.a); glUniform4f (ms._specular, col_spec.r, col_spec.g, col_spec.b, col_spec.a); glActiveTexture (GL_TEXTURE0); glEnable (GL_TEXTURE_2D); glBindTexture(GL_TEXTURE_2D, ms.ctex); glDrawArrays (GL_TRIANGLES, 0, me->mNumFaces * 3); glUseProgram (0); glMatrixMode (GL_PROJECTION); glPopMatrix (); glMatrixMode (GL_MODELVIEW); glPopMatrix (); } material_unbind (&ms); /** * I believe I don't have the right COLOR_ATTACHMENTs during material stage. * In any case ctex probably ends up as normals. * Creating a new FBO for the material stage is probably ok. * * Warning: * For unknown reason the model, automaticall unwrapped by Blender * (Select all faces -> U -> Unwrap) had its UV coordinates screwed up. * Re-exporting with a proper UV map somehow fixed the problem. * * Blender settings: (Texure Properties) * Image/Source: Single Image * Mapping/Coordinates: UV * Mapping Layer: (Empty) * Mapping/Projection: Flat */ //debug_draw_tex_quad (ctex, 200, 0, 100, 100); debug_draw_tex_quad (mf._tnor, 200, 0, 100, 100); debug_draw_tex_quad (mf._tdiff, 200, 110, 100, 100); debug_draw_tex_quad (mf._tspec, 200, 220, 100, 100); /* 3.4 Lighting Stage */ struct LightingFbo lf = {0}; lf.tdep = dtex; lighting_create_fbo (&lf); struct LightingData ls = {0}; ls.fbo = lf._fbo; ls.lpp = lighting_ambient_pass_program (); ls.dtex = dtex; ls.tnor = mf._tnor; ls.tdiff = mf._tdiff; ls.tspec = mf._tspec; lighting_bind (&ls); { } lighting_unbind (&ls); /* Restore the saved allegro settings */ allegro_ffp_restore (RESTORE_MODE_RESTORE, RESTORE_SUB_ALLEGRO); }
//------------------------------------------------------------------------------- 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; }
//------------------------------------------- void ofxAssimpModelLoader::loadGLResources(){ ofLogVerbose("ofxAssimpModelLoader") << "loadGLResources(): starting"; // create new mesh helpers for each mesh, will populate their data later. modelMeshes.resize(scene->mNumMeshes,ofxAssimpMeshHelper()); // create OpenGL buffers and populate them based on each meshes pertinant info. for (unsigned int i = 0; i < scene->mNumMeshes; ++i){ ofLogVerbose("ofxAssimpModelLoader") << "loadGLResources(): loading mesh " << i; // current mesh we are introspecting aiMesh* mesh = scene->mMeshes[i]; // the current meshHelper we will be populating data into. ofxAssimpMeshHelper & meshHelper = modelMeshes[i]; //ofxAssimpMeshHelper meshHelper; //meshHelper.texture = NULL; // Handle material info aiMaterial* mtl = scene->mMaterials[mesh->mMaterialIndex]; aiColor4D dcolor, scolor, acolor, ecolor; if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &dcolor)){ meshHelper.material.setDiffuseColor(aiColorToOfColor(dcolor)); } if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &scolor)){ meshHelper.material.setSpecularColor(aiColorToOfColor(scolor)); } if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &acolor)){ meshHelper.material.setAmbientColor(aiColorToOfColor(acolor)); } if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &ecolor)){ meshHelper.material.setEmissiveColor(aiColorToOfColor(ecolor)); } float shininess; if(AI_SUCCESS == aiGetMaterialFloat(mtl, AI_MATKEY_SHININESS, &shininess)){ meshHelper.material.setShininess(shininess); } int blendMode; if(AI_SUCCESS == aiGetMaterialInteger(mtl, AI_MATKEY_BLEND_FUNC, &blendMode)){ if(blendMode==aiBlendMode_Default){ meshHelper.blendMode=OF_BLENDMODE_ALPHA; }else{ meshHelper.blendMode=OF_BLENDMODE_ADD; } } // Culling unsigned int max = 1; int two_sided; if((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, &max)) && two_sided) meshHelper.twoSided = true; else meshHelper.twoSided = false; // Load Textures int texIndex = 0; aiString texPath; // TODO: handle other aiTextureTypes if(AI_SUCCESS == mtl->GetTexture(aiTextureType_DIFFUSE, texIndex, &texPath)){ ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource(): loading image from \"" << texPath.data << "\""; string modelFolder = file.getEnclosingDirectory(); string relTexPath = ofFilePath::getEnclosingDirectory(texPath.data,false); string texFile = ofFilePath::getFileName(texPath.data); string realPath = ofFilePath::join(ofFilePath::join(modelFolder, relTexPath), texFile); if(ofFile::doesFileExist(realPath) == false) { ofLogError("ofxAssimpModelLoader") << "loadGLResource(): texture doesn't exist: \"" << file.getFileName() + "\" in \"" << realPath << "\""; } ofxAssimpTexture assimpTexture; bool bTextureAlreadyExists = false; for(int j=0; j<textures.size(); j++) { assimpTexture = textures[j]; if(assimpTexture.getTexturePath() == realPath) { bTextureAlreadyExists = true; break; } } if(bTextureAlreadyExists) { meshHelper.assimpTexture = assimpTexture; ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource(): texture already loaded: \"" << file.getFileName() + "\" from \"" << realPath << "\""; } else { ofTexture texture; bool bTextureLoadedOk = ofLoadImage(texture, realPath); if(bTextureLoadedOk) { textures.push_back(ofxAssimpTexture(texture, realPath)); assimpTexture = textures.back(); meshHelper.assimpTexture = assimpTexture; ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource(): texture loaded, dimensions: " << texture.getWidth() << "x" << texture.getHeight(); } else { ofLogError("ofxAssimpModelLoader") << "loadGLResource(): couldn't load texture: \"" << file.getFileName() + "\" from \"" << realPath << "\""; } } } meshHelper.mesh = mesh; aiMeshToOfMesh(mesh, meshHelper.cachedMesh, &meshHelper); meshHelper.cachedMesh.setMode(OF_PRIMITIVE_TRIANGLES); meshHelper.validCache = true; meshHelper.hasChanged = false; meshHelper.animatedPos.resize(mesh->mNumVertices); if(mesh->HasNormals()){ meshHelper.animatedNorm.resize(mesh->mNumVertices); } int usage; if(getAnimationCount()){ #ifndef TARGET_OPENGLES if(!ofIsGLProgrammableRenderer()){ usage = GL_STATIC_DRAW; }else{ usage = GL_STREAM_DRAW; } #else usage = GL_DYNAMIC_DRAW; #endif }else{ usage = GL_STATIC_DRAW; } meshHelper.vbo.setVertexData(&mesh->mVertices[0].x,3,mesh->mNumVertices,usage,sizeof(aiVector3D)); if(mesh->HasVertexColors(0)){ meshHelper.vbo.setColorData(&mesh->mColors[0][0].r,mesh->mNumVertices,GL_STATIC_DRAW,sizeof(aiColor4D)); } if(mesh->HasNormals()){ meshHelper.vbo.setNormalData(&mesh->mNormals[0].x,mesh->mNumVertices,usage,sizeof(aiVector3D)); } if (meshHelper.cachedMesh.hasTexCoords()){ meshHelper.vbo.setTexCoordData(meshHelper.cachedMesh.getTexCoordsPointer()[0].getPtr(),mesh->mNumVertices,GL_STATIC_DRAW,sizeof(ofVec2f)); } meshHelper.indices.resize(mesh->mNumFaces * 3); int j=0; for (unsigned int x = 0; x < mesh->mNumFaces; ++x){ for (unsigned int a = 0; a < mesh->mFaces[x].mNumIndices; ++a){ meshHelper.indices[j++]=mesh->mFaces[x].mIndices[a]; } } meshHelper.vbo.setIndexData(&meshHelper.indices[0],meshHelper.indices.size(),GL_STATIC_DRAW); //modelMeshes.push_back(meshHelper); } int numOfAnimations = scene->mNumAnimations; for(int i=0; i<numOfAnimations; i++) { aiAnimation * animation = scene->mAnimations[i]; animations.push_back(ofxAssimpAnimation(scene, animation)); } ofLogVerbose("ofxAssimpModelLoader") << "loadGLResource(): finished"; }
//------------------------------------------------------------------------------- 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; }
void RenderObject::pre_render() { recursive_pre_render(scene->mRootNode); //Init materials: for(unsigned int i= 0; i < scene->mNumMaterials; ++i) { const aiMaterial * mtl = scene->mMaterials[i]; Material mtl_data; aiString path; if(mtl->GetTextureCount(aiTextureType_DIFFUSE) > 0 && mtl->GetTexture(aiTextureType_DIFFUSE, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) { std::string p(path.data); mtl_data.texture = load_texture(p); } else if(mtl->GetTextureCount(aiTextureType_AMBIENT) > 0 && mtl->GetTexture(aiTextureType_AMBIENT, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) { std::string p(path.data); mtl_data.texture = load_texture(p); } if ( !mtl_data.texture ){ fprintf(stderr, "RenderObject `%s' texture failed to load.\n", name.c_str()); util_abort(); } //Check for normalmap: if(mtl->GetTextureCount(aiTextureType_HEIGHT) > 0 && mtl->GetTexture(aiTextureType_HEIGHT, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) { const std::string p(path.data); mtl_data.normal_map = load_texture(p); } if(mtl->GetTextureCount(aiTextureType_SHININESS) > 0 && mtl->GetTexture(aiTextureType_SHININESS, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) { const std::string p(path.data); mtl_data.specular_map = load_texture(p); } if(mtl->GetTextureCount(aiTextureType_OPACITY) > 0 && mtl->GetTexture(aiTextureType_OPACITY, 0, &path, NULL, NULL, NULL, NULL, NULL) == AI_SUCCESS) { const std::string p(path.data); mtl_data.alpha_map = load_texture(p); } aiString name; if(AI_SUCCESS == mtl->Get(AI_MATKEY_NAME, name)) fprintf(verbose, "Loaded material %d %s\n", i, name.data); aiColor4D value; if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &value)) color4_to_vec4(&value, mtl_data.diffuse); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &value)) color4_to_vec4(&value, mtl_data.specular); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &value)) color4_to_vec4(&value, mtl_data.ambient); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &value)) color4_to_vec4(&value, mtl_data.emission); unsigned int max = 1; float strength; int ret1 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &mtl_data.shininess, &max); if(ret1 == AI_SUCCESS) { max = 1; int ret2 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS_STRENGTH, &strength, &max); if(ret2 == AI_SUCCESS) mtl_data.shininess *= strength; } else { mtl_data.shininess = 0.0f; } if ( mtl_data.shininess < 0.001f ){ /* arbitrary small value */ mtl_data.shininess = 0.001f; /* in glsl pow(x,0) is undefined */ mtl_data.specular = glm::vec4(0.f, 0.f, 0.f, 0.f); } max = 1; materials.push_back(mtl_data); } }
void apply_material(const struct aiMaterial *mtl) { float c[4]; GLenum fill_mode; int ret1, ret2; struct aiColor4D diffuse; struct aiColor4D specular; struct aiColor4D ambient; struct aiColor4D emission; float shininess, strength; int two_sided; int wireframe; int max; int texIndex = 0; aiString texPath; //contains filename of texture if(AI_SUCCESS == mtl->GetTexture(aiTextureType_DIFFUSE, texIndex, &texPath)) { //bind texture unsigned int texId = *textureIdMap[texPath.data]; glBindTexture(GL_TEXTURE_2D, texId); } set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse)) color4_to_float4(&diffuse, c); glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, c); set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular)) color4_to_float4(&specular, c); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c); set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient)) color4_to_float4(&ambient, c); glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, c); set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f); if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission)) color4_to_float4(&emission, c); glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, c); max = 1; ret1 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, (unsigned int *)&max); max = 1; ret2 = aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS_STRENGTH, &strength, (unsigned int *)&max); if((ret1 == AI_SUCCESS) && (ret2 == AI_SUCCESS)) glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, shininess * strength); else { glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0f); set_float4(c, 0.0f, 0.0f, 0.0f, 0.0f); glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, c); } max = 1; if(AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_ENABLE_WIREFRAME, &wireframe, (unsigned int *)&max)) fill_mode = wireframe ? GL_LINE : GL_FILL; else fill_mode = GL_FILL; glPolygonMode(GL_FRONT_AND_BACK, fill_mode); max = 1; if((AI_SUCCESS == aiGetMaterialIntegerArray(mtl, AI_MATKEY_TWOSIDED, &two_sided, (unsigned int *)&max)) && two_sided) glEnable(GL_CULL_FACE); else glDisable(GL_CULL_FACE); }
void sceneLoader::processMesh(aiMesh* cmesh, const aiScene* scene) { vector<vertexData> data; vector<unsigned int> indices; vector<textureData> textures; aiColor4D col; aiMaterial* mat = scene->mMaterials[cmesh->mMaterialIndex]; aiGetMaterialColor(mat, AI_MATKEY_COLOR_DIFFUSE, &col); glm::vec3 defaultColor(col.r, col.g, col.b); for(std::size_t i=0; i<cmesh->mNumVertices; i++) { vertexData tmp; //position tmp.position = glm::vec3(cmesh->mVertices[i].x, cmesh->mVertices[i].y, cmesh->mVertices[i].z); //normals tmp.normal = glm::vec3(cmesh->mNormals[i].x, cmesh->mNormals[i].y, cmesh->mNormals[i].z); //tangents if(cmesh->mTangents) tmp.tangent = glm::vec3(cmesh->mTangents[i].x, cmesh->mTangents[i].y, cmesh->mTangents[i].z); else tmp.tangent = glm::vec3(1,0,0); //colors if(cmesh->mColors[0]) tmp.color = glm::vec3(cmesh->mColors[0][i].r, cmesh->mColors[0][i].g, cmesh->mColors[0][i].b); else tmp.color = defaultColor; //UVs if(cmesh->mTextureCoords[0]) tmp.UV = glm::vec2(cmesh->mTextureCoords[0][i].x, cmesh->mTextureCoords[0][i].y); else tmp.UV = glm::vec2(0,0); data.push_back(tmp); } for(std::size_t i=0; i<cmesh->mNumFaces; i++) { aiFace face = cmesh->mFaces[i]; for(std::size_t j=0; j<face.mNumIndices;j++) //0...2 { indices.push_back(face.mIndices[j]); } } //handle textures for(std::size_t i=0; i<mat->GetTextureCount(aiTextureType_DIFFUSE); i++) { aiString str; mat->GetTexture(aiTextureType_DIFFUSE, i, &str); textureData tmp; tmp.id = loadTexture(str.C_Str()); tmp.type = 0; //Index for Diffuse Map textures.push_back(tmp); } meshes.push_back(new mesh(&data, &indices, &textures)); }
void Model::genVAOsAndUniformBuffer(const aiScene *sc) { checkForErrors(); struct MyMesh aMesh; struct MyMaterial aMat; GLuint buffer; // For each mesh for (unsigned int n = 0; n < sc->mNumMeshes; ++n) { const aiMesh* mesh = sc->mMeshes[n]; // create array with faces // have to convert from Assimp format to array unsigned int *faceArray; faceArray = (unsigned int *)malloc(sizeof(unsigned int) * mesh->mNumFaces * 3); unsigned int faceIndex = 0; for (unsigned int t = 0; t < mesh->mNumFaces; ++t) { const aiFace* face = &mesh->mFaces[t]; memcpy(&faceArray[faceIndex], face->mIndices,3 * sizeof(unsigned int)); faceIndex += 3; } aMesh.numFaces = sc->mMeshes[n]->mNumFaces; // generate Vertex Array for mesh glGenVertexArrays(1,&(aMesh.vao)); glBindVertexArray(aMesh.vao); // buffer for faces glGenBuffers(1, &buffer); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, buffer); glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(unsigned int) * mesh->mNumFaces * 3, faceArray, GL_STATIC_DRAW); free(faceArray); // buffer for vertex positions if (mesh->HasPositions()) { glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->mNumVertices, mesh->mVertices, GL_STATIC_DRAW); glEnableVertexAttribArray(vertexLoc); glVertexAttribPointer(vertexLoc, 3, GL_FLOAT, 0, 0, 0); } // buffer for vertex normals if (mesh->HasNormals()) { glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*3*mesh->mNumVertices, mesh->mNormals, GL_STATIC_DRAW); glEnableVertexAttribArray(normalLoc); glVertexAttribPointer(normalLoc, 3, GL_FLOAT, 0, 0, 0); } // buffer for vertex texture coordinates if (mesh->HasTextureCoords(0)) { float *texCoords = (float *)malloc(sizeof(float)*2*mesh->mNumVertices); for (unsigned int k = 0; k < mesh->mNumVertices; ++k) { texCoords[k*2] = mesh->mTextureCoords[0][k].x; texCoords[k*2+1] = mesh->mTextureCoords[0][k].y; } glGenBuffers(1, &buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer); glBufferData(GL_ARRAY_BUFFER, sizeof(float)*2*mesh->mNumVertices, texCoords, GL_STATIC_DRAW); glEnableVertexAttribArray(texCoordLoc); glVertexAttribPointer(texCoordLoc, 2, GL_FLOAT, 0, 0, 0); } // unbind buffers glBindVertexArray(0); glBindBuffer(GL_ARRAY_BUFFER,0); glBindBuffer(GL_ELEMENT_ARRAY_BUFFER,0); // create material uniform buffer aiMaterial *mtl = sc->mMaterials[mesh->mMaterialIndex]; aiString texPath; //contains filename of texture if(AI_SUCCESS == mtl->GetTexture(aiTextureType_DIFFUSE, 0, &texPath)){ //bind texture unsigned int texId = textureIdMap[texPath.data]; aMesh.texIndex = texId; aMat.texCount = 1; } else aMat.texCount = 0; float c[4]; set_float4(c, 0.8f, 0.8f, 0.8f, 1.0f); aiColor4D diffuse; if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuse)) color4_to_float4(&diffuse, c); memcpy(aMat.diffuse, c, sizeof(c)); set_float4(c, 0.2f, 0.2f, 0.2f, 1.0f); aiColor4D ambient; if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambient)) color4_to_float4(&ambient, c); memcpy(aMat.ambient, c, sizeof(c)); set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f); aiColor4D specular; if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specular)) color4_to_float4(&specular, c); memcpy(aMat.specular, c, sizeof(c)); set_float4(c, 0.0f, 0.0f, 0.0f, 1.0f); aiColor4D emission; if(AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emission)) color4_to_float4(&emission, c); memcpy(aMat.emissive, c, sizeof(c)); float shininess = 0.0; unsigned int max; aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max); aMat.shininess = shininess; glGenBuffers(1,&(aMesh.uniformBlockIndex)); glBindBuffer(GL_UNIFORM_BUFFER,aMesh.uniformBlockIndex); glBufferData(GL_UNIFORM_BUFFER, sizeof(aMat), (void *)(&aMat), GL_STATIC_DRAW); myMeshes.push_back(aMesh); } }
Material::Material(aiMaterial * pMaterial, std::string dir) { aiString name; pMaterial->Get(AI_MATKEY_NAME,name); // TODO:must be reimplemented to include all textures std::string n(name.C_Str()); //Engine::getLog()->log("Material",n); m_name = n; m_base_texture_set = false; unsigned int num_diffuse_textures = pMaterial->GetTextureCount(aiTextureType_DIFFUSE); unsigned int num_ambient_textures = pMaterial->GetTextureCount(aiTextureType_AMBIENT); unsigned int num_specular_textures = pMaterial->GetTextureCount(aiTextureType_SPECULAR); unsigned int num_normal_textures = pMaterial->GetTextureCount(aiTextureType_NORMALS); unsigned int num_unkown_textures = pMaterial->GetTextureCount(aiTextureType_HEIGHT); unsigned int num_none_textures = pMaterial->GetTextureCount(aiTextureType_NONE); unsigned int num_opacity_textures = pMaterial->GetTextureCount(aiTextureType_OPACITY); std::cout << "Loading "<< num_diffuse_textures <<" diffuse textures"<<std::endl; std::cout << "Listed " << num_ambient_textures << " ambient textures" << std::endl; std::cout << "Listed " << num_specular_textures << " specular textures" << std::endl; std::cout << "Listed " << num_normal_textures << " normal textures" << std::endl; std::cout << "Listed " << num_unkown_textures << " height textures" << std::endl; std::cout << "Listed " << num_opacity_textures << " opacity textures" << std::endl; std::cout << "Listed " << num_opacity_textures << " none textures" << std::endl; // load diffuse texturess for(int x=0; x< num_diffuse_textures;x++) { aiString Path; float strength; // if a texture can be found if (pMaterial->GetTexture(aiTextureType_DIFFUSE, x, &Path, NULL, NULL, &strength, NULL, NULL) == AI_SUCCESS) { std::string fi(Path.data); std::string::size_type slashpos2 = fi.find_last_of("/"); std::string p2 ; if (slashpos2 != std::string::npos) p2 = fi.substr(slashpos2+1, fi.size()); else p2 = fi; std::string path = dir + "/" + p2; std::string t("C:"); if (p2.compare(0, t.length(), t) == 0) { path = p2; } std::cout << path << std::endl; // load it and store it in our mapping Texture * tex = RessourceManager::loadTexture(path); m_diffuse_textures.insert(m_diffuse_textures.end(),std::pair<int ,Texture *>(x,tex)); m_diffuse_texture_strengths.insert(m_diffuse_texture_strengths.end(),std::pair<int ,int>(x,strength)); m_base_texture_set = true; } } aiString Path, Path2; float strength, strength2; if (pMaterial->GetTexture(aiTextureType_AMBIENT, 0, &Path, NULL, NULL, &strength, NULL, NULL) == AI_SUCCESS) { std::string fi(Path.data); std::string::size_type slashpos2 = fi.find_last_of("/"); std::string p2; if (slashpos2 != std::string::npos) p2 = fi.substr(slashpos2 + 1, fi.size()); else p2 = fi; std::string path = dir + "/" + p2; // load it and store it in our mapping m_ambient_texture = RessourceManager::loadTexture(path); } if (pMaterial->GetTexture(aiTextureType_SPECULAR, 0, &Path, NULL, NULL, &strength, NULL, NULL) == AI_SUCCESS) { std::string fi(Path.data); std::string::size_type slashpos2 = fi.find_last_of("/"); std::string p2; if (slashpos2 != std::string::npos) p2 = fi.substr(slashpos2 + 1, fi.size()); else p2 = fi; std::string path = dir + "/" + p2; // load it and store it in our mapping m_specular_texture = RessourceManager::loadTexture(path); } if (pMaterial->GetTexture(aiTextureType_NORMALS, 0, &Path, NULL, NULL, &strength, NULL, NULL) == AI_SUCCESS) { std::string fi(Path.data); std::string::size_type slashpos2 = fi.find_last_of("/"); std::string p2; if (slashpos2 != std::string::npos) p2 = fi.substr(slashpos2 + 1, fi.size()); else p2 = fi; std::string path = dir + "/" + p2; // load it and store it in our mapping m_normal_texture = RessourceManager::loadTexture(path); } if (pMaterial->GetTexture(aiTextureType_HEIGHT, 0, &Path, NULL, NULL, &strength, NULL, NULL) == AI_SUCCESS) { std::string fi(Path.data); std::string::size_type slashpos2 = fi.find_last_of("/"); std::string p2; if (slashpos2 != std::string::npos) p2 = fi.substr(slashpos2 + 1, fi.size()); else p2 = fi; std::string path = dir + "/" + p2; // load it and store it in our mapping m_normal_texture = RessourceManager::loadTexture(path); // because assimp programmers think HEIGHT == NORMALS ... } if (pMaterial->GetTexture(aiTextureType_OPACITY, 0, &Path2, NULL, NULL, &strength2, NULL, NULL) == AI_SUCCESS) { std::string fi(Path2.data); std::string::size_type slashpos2 = fi.find_last_of("/"); std::string p2; if (slashpos2 != std::string::npos) p2 = fi.substr(slashpos2 + 1, fi.size()); else p2 = fi; std::string path = dir + "/" + p2; // load it and store it in our mapping m_opacity_texture = RessourceManager::loadTexture(path); m_base_texture_set = true; } aiColor4D diffuse,specular,ambient,emmissive; float opacity,shininess; aiGetMaterialColor(pMaterial, AI_MATKEY_COLOR_DIFFUSE, &diffuse); aiGetMaterialColor(pMaterial, AI_MATKEY_COLOR_SPECULAR, &specular); aiGetMaterialColor(pMaterial, AI_MATKEY_COLOR_AMBIENT, &ambient); aiGetMaterialColor(pMaterial, AI_MATKEY_COLOR_EMISSIVE, &emmissive); aiGetMaterialFloat(pMaterial, AI_MATKEY_OPACITY, &opacity); aiGetMaterialFloat(pMaterial, AI_MATKEY_SHININESS, &shininess); m_material_specs.m_opacity = opacity; m_material_specs.m_shininess = shininess; m_material_specs.m_diffuse_color = glm::vec3(diffuse.r,diffuse.g,diffuse.b); m_material_specs.m_ambient_color = glm::vec3(ambient.r,ambient.g,ambient.b); m_material_specs.m_specular_color = glm::vec3(specular.r,specular.g,specular.b); // if ambient color is not set use the diffuse color if( (m_material_specs.m_ambient_color .x == 0) && (m_material_specs.m_ambient_color .y == 0) && (m_material_specs.m_ambient_color .z == 0) ) { m_material_specs.m_ambient_color = m_material_specs.m_diffuse_color ; } m_material_specs.m_emmissive = 0.0f; if( (emmissive.r != 0.0f) || (emmissive.g != 0.0f) || (emmissive.b != 0.0f) ) { std::cout <<"emissive material"<<std::endl; m_material_specs.m_emmissive = 1.0f; } }
void AssimpLoader::GetMaterialPropreties(RenderableObject* assimpMesh, const struct aiMaterial *mtl) { Color color; // Manage the diffuse color struct aiColor4D diffuseColor; if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_DIFFUSE, &diffuseColor)) { color = Color(diffuseColor.r, diffuseColor.g, diffuseColor.b, diffuseColor.a); assimpMesh->AddMaterial(DIFFUSE_MATERIAL, color); } else { color = Color(1, 1, 1, 1); assimpMesh->AddMaterial(DIFFUSE_MATERIAL, color); } // Manage the specular color struct aiColor4D specularColor; if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_SPECULAR, &specularColor)) { color = Color(specularColor.r, specularColor.g, specularColor.b, 0); // Try to extract Specular shininess float shininess, strength; unsigned int max = 1; if (aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS, &shininess, &max) == AI_SUCCESS && aiGetMaterialFloatArray(mtl, AI_MATKEY_SHININESS_STRENGTH, &strength, &max) == AI_SUCCESS) { color.A = strength * shininess; } assimpMesh->AddMaterial(SPECULAR_MATERIAL, color); } else { color = Color(1, 1, 1, 0); assimpMesh->AddMaterial(SPECULAR_MATERIAL, color); } // Manage the Ambiant color struct aiColor4D ambiantColor; if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_AMBIENT, &ambiantColor)) { color = Color(ambiantColor.r, ambiantColor.g, ambiantColor.b, ambiantColor.a); assimpMesh->AddMaterial(AMBIANT_MATERIAL, color); } else { color = Color(1, 1, 1, 1); assimpMesh->AddMaterial(AMBIANT_MATERIAL, color); } // Manage emission color struct aiColor4D emissionColor; if (AI_SUCCESS == aiGetMaterialColor(mtl, AI_MATKEY_COLOR_EMISSIVE, &emissionColor)) { color = Color(emissionColor.r, emissionColor.g, emissionColor.b, emissionColor.a); assimpMesh->AddMaterial(EMISSION_MATERIAL, color); } else { color = Color(1, 1, 1, 1); assimpMesh->AddMaterial(EMISSION_MATERIAL, color); } }
Mesh Model::processMesh(aiMesh* ai_mesh, const aiScene* scene ) { // Data to fill TVecCoord vertices; vector<GLuint> indices; vector<GLuint> adjacent_indices; vector<Texture> textures; //vector<VertexWeight> boneWeights; vector<glm::vec2> textCoordVert; Material material; glm::uint numBones = 0; #pragma region [ Process Vertices ] // Walk through each of the mesh's vertices for(GLuint i = 0; i < ai_mesh->mNumVertices; i++) { Vertex vertex; glm::vec3 vector; TCoord vectorVert; // We declare a placeholder vector since Assimp uses its own vector class that doesn't directly convert to glm's vec3 class so we transfer the data to this placeholder glm::vec3 first. // Positions vectorVert[0] = ai_mesh->mVertices[i].x; vectorVert[1] = ai_mesh->mVertices[i].y; vectorVert[2] = ai_mesh->mVertices[i].z; //vertex.Position = vector; vertices.push_back(vectorVert); // Normals if (ai_mesh->HasNormals()) { vector.x = ai_mesh->mNormals[i].x; vector.y = ai_mesh->mNormals[i].y; vector.z = ai_mesh->mNormals[i].z; vertex.Normal = vector; } // Texture Coordinates if(ai_mesh->mTextureCoords[0]) // Does the mesh contain texture coordinates? { glm::vec2 vec; // A vertex can contain up to 8 different texture coordinates. We thus make the assumption that we won't // use models where a vertex can have multiple texture coordinates so we always take the first set (0). vec.x = ai_mesh->mTextureCoords[0][i].x; vec.y = ai_mesh->mTextureCoords[0][i].y; vertex.TexCoords = vec; textCoordVert.push_back(vec); } else { vertex.TexCoords = glm::vec2(0.0f, 0.0f); textCoordVert.push_back(glm::vec2(0.0f)); } if (ai_mesh->HasTangentsAndBitangents()) { glm::vec3 tangent; tangent.x = ai_mesh->mTangents[i].x; tangent.y = ai_mesh->mTangents[i].y; tangent.z = ai_mesh->mTangents[i].z; vertex.Tangent = tangent; } //vertices.push_back(vertex); } #pragma endregion #pragma region [ Process Faces ] //if (d_withAdjacencies) //FindAdjacencies(ai_mesh, adjacent_indices); // else //Now walk through each of the mesh's faces (a face is a mesh its triangle) and retrieve the corresponding vertex indices. for(GLuint i = 0; i < ai_mesh->mNumFaces; i++) { aiFace face = ai_mesh->mFaces[i]; // Retrieve all indices of the face and store them in the indices vector for(GLuint j = 0; j < face.mNumIndices; j++) indices.push_back(face.mIndices[j]); } #pragma endregion #pragma region [ Process Materials ] // Process materials if(ai_mesh->mMaterialIndex >= 0) { aiMaterial* aiMaterial = scene->mMaterials[ai_mesh->mMaterialIndex]; aiColor4D ambient; glm::vec3 glmAmbient; aiGetMaterialColor(aiMaterial, AI_MATKEY_COLOR_AMBIENT, &ambient); glmAmbient = glm::vec3(0.5f,0.5f,0.5f);// aiColor4DToGlm(ambient); aiColor4D diffuse; glm::vec3 glmDiffuse; aiGetMaterialColor(aiMaterial, AI_MATKEY_COLOR_DIFFUSE, &diffuse); glmDiffuse = aiColor4DToGlm(diffuse); aiColor4D specular; glm::vec3 glmSpecular; aiGetMaterialColor(aiMaterial, AI_MATKEY_COLOR_SPECULAR, &specular); glmSpecular = aiColor4DToGlm(specular); float shininess = 0.0f; aiGetMaterialFloat(aiMaterial, AI_MATKEY_SHININESS, &shininess); material = Material(glmAmbient,glmDiffuse,glmSpecular,32.0f); // We assume a convention for sampler names in the Shaders. Each diffuse texture should be named // as 'texture_diffuseN' where N is a sequential number ranging from 1 to MAX_SAMPLER_NUMBER. // Same applies to other texture as the following list summarizes: // Diffuse: texture_diffuseN // Specular: texture_specularN // Normal: texture_normalN // 1. Diffuse maps vector<Texture> diffuseMaps = this->loadMaterialTextures(aiMaterial, aiTextureType_DIFFUSE, TextureType_DIFFUSE); textures.insert(textures.end(), diffuseMaps.begin(), diffuseMaps.end()); // 2. Specular maps vector<Texture> specularMaps = this->loadMaterialTextures(aiMaterial, aiTextureType_SPECULAR, TextureType_SPECULAR); textures.insert(textures.end(), specularMaps.begin(), specularMaps.end()); // 3. Normal maps vector<Texture> normalMaps = this->loadMaterialTextures(aiMaterial, aiTextureType_NORMALS, TextureType_NORMAL); textures.insert(textures.end(), normalMaps.begin(), normalMaps.end()); // 4. Ref maps vector<Texture> reflMaps = this->loadMaterialTextures(aiMaterial, aiTextureType_AMBIENT, TextureType_REFLECTION); textures.insert(textures.end(), reflMaps.begin(), reflMaps.end()); } #pragma endregion #pragma region [ Process Bones ] //if(ai_mesh->HasBones()) //{ // int bone_count = ai_mesh->mNumBones; // boneWeights.resize(ai_mesh->mNumVertices); // for (GLuint i = 0 ; i < ai_mesh->mNumBones ; i++) { // int BoneIndex = 0; // string BoneName(ai_mesh->mBones[i]->mName.data); // if ( m_skeleton->boneMapping.find(BoneName) == m_skeleton->boneMapping.end()) { // // Allocate an index for a new bone // BoneInfo info; // BoneIndex = d_numberOfBone++; // info.offset = aiMatrix4x4ToGlm(&ai_mesh->mBones[i]->mOffsetMatrix); // info.index = BoneIndex; // m_skeleton->boneMapping[BoneName] = info; // } // else // BoneIndex = m_skeleton->boneMapping[BoneName].index; // for (GLuint j = 0 ; j < ai_mesh->mBones[i]->mNumWeights ; j++) { // int VertexID = ai_mesh->mBones[i]->mWeights[j].mVertexId; // float Weight = ai_mesh->mBones[i]->mWeights[j].mWeight; // for (glm::uint i = 0 ; i < 4 ; i++) { // if (boneWeights[VertexID].Weights[i] == 0.0) { // boneWeights[VertexID].IDs[i] = BoneIndex; // boneWeights[VertexID].Weights[i] = Weight; // break; // } // } // } // } //} #pragma endregion // Return a mesh object created from the extracted mesh data return Mesh(vertices, indices, textures ,material, textCoordVert); }
void Model::materials_parse(Scene &scene) { Assets &assets = scene.assets_get(); if (!assimp_scene->HasMaterials()) { std::cout << "Fragmic warning: model '" << this << "'has no materials" << std::endl; return; } //std::cout << "Number of materials: " << assimp_scene->mNumMaterials << std::endl; for (unsigned int i = 0; i < assimp_scene->mNumMaterials; i++) { aiMaterial &assimpMaterial = *assimp_scene->mMaterials[i]; std::unique_ptr<Material> materialPtr(new Material()); Material &material = *materialPtr; /* std::cout << "\tProperties: " << assimpMaterial.mNumProperties <<std::endl; std::cout << "\tAllocated: " << assimpMaterial.mNumAllocated <<std::endl; std::cout << "\tNone textures: " << assimpMaterial.GetTextureCount(aiTextureType_NONE) <<std::endl; std::cout << "\tAmbient textures: " << assimpMaterial.GetTextureCount(aiTextureType_AMBIENT) <<std::endl; std::cout << "\tDiffuse textures: " << assimpMaterial.GetTextureCount(aiTextureType_DIFFUSE) <<std::endl; std::cout << "\tSpecular textures: " << assimpMaterial.GetTextureCount(aiTextureType_SPECULAR) <<std::endl; std::cout << "\tEmissive textures: " << assimpMaterial.GetTextureCount(aiTextureType_EMISSIVE) <<std::endl; std::cout << "\tNormals textures: " << assimpMaterial.GetTextureCount(aiTextureType_NORMALS) <<std::endl; std::cout << "\tLightmap textures: " << assimpMaterial.GetTextureCount(aiTextureType_LIGHTMAP) <<std::endl; */ { aiColor4D color; aiReturn ret; ret = aiGetMaterialColor(&assimpMaterial, AI_MATKEY_COLOR_AMBIENT, &color); if (ret == AI_SUCCESS) { /* std::cout << "\tAmbient (r,g,b,a) = (" << color.r << "," << color.g << "," << color.b << "," << color.a << ")" <<std::endl; */ material.properties.Ka.x = color.r; material.properties.Ka.y = color.g; material.properties.Ka.z = color.b; } ret = aiGetMaterialColor(&assimpMaterial, AI_MATKEY_COLOR_DIFFUSE, &color); if (ret == AI_SUCCESS) { /* std::cout << "\tDiffuse (r,g,b,a) = (" << color.r << "," << color.g << "," << color.b << "," << color.a << ")" << std::endl; */ material.properties.Kd.x = color.r; material.properties.Kd.y = color.g; material.properties.Kd.z = color.b; } ret = aiGetMaterialColor(&assimpMaterial, AI_MATKEY_COLOR_SPECULAR, &color); if (ret == AI_SUCCESS) { /* std::cout << "\tSpecular (r,g,b,a) = (" << color.r << "," << color.g << "," << color.b << "," << color.a << ")" << std::endl; */ material.properties.Ks.x = color.r; material.properties.Ks.y = color.g; material.properties.Ks.z = color.b; } ret = aiGetMaterialColor(&assimpMaterial, AI_MATKEY_COLOR_EMISSIVE, &color); if (ret == AI_SUCCESS) { /* std::cout << "\tEmissive (r,g,b,a) = (" << color.r << "," << color.g << "," << color.b << "," << color.a << ")" <<std::endl; */ } ret = aiGetMaterialColor(&assimpMaterial, AI_MATKEY_COLOR_REFLECTIVE, &color); if (ret == AI_SUCCESS) { /* std::cout << "\tReflective (r,g,b,a) = (" << color.r << "," << color.g << "," << color.b << "," << color.a << ")" <<std::endl; */ } ret = aiGetMaterialColor(&assimpMaterial, AI_MATKEY_SHININESS, &color); if (ret == AI_SUCCESS) { /* std::cout << "\tShininess (r,g,b,a) = (" << color.r << "," << color.g << "," << color.b << "," << color.a << ")" <<std::endl; */ material.properties.shininess = color.r; } ret = aiGetMaterialColor(&assimpMaterial, AI_MATKEY_SHININESS_STRENGTH, &color); if (ret == AI_SUCCESS) { /* std::cout << "\tShininess strength (r,g,b,a) = (" << color.r << "," << color.g << "," << color.b << "," << color.a << ")" << std::endl; */ } } assimp_material_add_texture(material, assimpMaterial, MODEL_TEXTURE_DIFFUSE); assimp_material_add_texture(material, assimpMaterial, MODEL_TEXTURE_NORMAL); assimp_material_add_texture(material, assimpMaterial, MODEL_TEXTURE_HEIGHT); assimp_material_add_texture(material, assimpMaterial, MODEL_TEXTURE_ALPHA); assimp_material_add_texture(material, assimpMaterial, MODEL_TEXTURE_SPECULAR); materials.push_back(materialPtr.get()); assets.material_add(std::move(materialPtr)); } }