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 AssetMaterialSetTextures(ASSET_MATERIAL* material) { struct aiString texture_path; if(aiGetMaterialTexture(material->material, aiTextureType_DIFFUSE, 0, &texture_path, NULL, NULL, NULL, NULL, NULL, NULL) == aiReturn_SUCCESS) { const char *path = (const char*)texture_path.data; const char *separator = "*"; const char *sph = ".sph"; const char *spa = ".spa"; char *texture = Locale2UTF8(path); if(strstr(texture, separator) != NULL) { int num_token; char **tokens = SplitString(texture, separator, &num_token); if(num_token > 0) { char *main_texture = tokens[0]; if(StringCompareIgnoreCase(&main_texture[strlen(main_texture)-4], sph) == 0) { material->sphere_texture = MEM_STRDUP_FUNC(main_texture); material->sphere_texture_render_mode = MATERIAL_MULTI_TEXTURE; } else { material->main_texture = MEM_STRDUP_FUNC(main_texture); } } if(num_token >= 2) { char *sub_texture = tokens[1]; if(StringCompareIgnoreCase(&sub_texture[strlen(sub_texture)-4], sph) == 0) { MEM_FREE_FUNC(material->sphere_texture); material->sphere_texture = MEM_STRDUP_FUNC(sub_texture); material->sphere_texture_render_mode = MATERIAL_MULTI_TEXTURE; } else if(StringCompareIgnoreCase(&sub_texture[strlen(sub_texture)-4], spa) == 0) { MEM_FREE_FUNC(material->sphere_texture); material->sphere_texture = MEM_STRDUP_FUNC(sub_texture); material->sphere_texture_render_mode = MATERIAL_ADD_TEXTURE; } } } else if(StringCompareIgnoreCase(&texture[strlen(texture)-4], sph) == 0) { material->sphere_texture = MEM_STRDUP_FUNC(texture); material->sphere_texture_render_mode = MATERIAL_MULTI_TEXTURE; } MEM_FREE_FUNC(texture); } }
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); } }
glhckTexture* textureFromMaterial(const char *file, const struct aiMaterial *mtl) { glhckTexture *texture; struct aiString textureName; enum aiTextureMapping textureMapping; enum aiTextureOp op; enum aiTextureMapMode textureMapMode[3] = {0,0,0}; unsigned int uvwIndex, flags; float blend; char *texturePath; glhckTextureParameters params; assert(file && mtl); if (!aiGetMaterialTextureCount(mtl, aiTextureType_DIFFUSE)) return NULL; if (aiGetMaterialTexture(mtl, aiTextureType_DIFFUSE, 0, &textureName, &textureMapping, &uvwIndex, &blend, &op, textureMapMode, &flags) != AI_SUCCESS) return NULL; memcpy(¶ms, glhckTextureDefaultParameters(), sizeof(glhckTextureParameters)); switch (textureMapMode[0]) { case aiTextureMapMode_Clamp: case aiTextureMapMode_Decal: params.wrapR = GLHCK_WRAP_CLAMP_TO_EDGE; params.wrapS = GLHCK_WRAP_CLAMP_TO_EDGE; params.wrapT = GLHCK_WRAP_CLAMP_TO_EDGE; break; case aiTextureMapMode_Mirror: params.wrapR = GLHCK_WRAP_MIRRORED_REPEAT; params.wrapS = GLHCK_WRAP_MIRRORED_REPEAT; params.wrapT = GLHCK_WRAP_MIRRORED_REPEAT; break; default: break; } if (!(texturePath = _glhckImportTexturePath(textureName.data, file))) return NULL; DEBUG(0, "%s", texturePath); texture = glhckTextureNewFromFile(texturePath, NULL, ¶ms); _glhckFree(texturePath); return texture; }
// basado en http://www.gamedev.net/topic/582240-assimp-drawing-textured-model/ void Model::recursiveTextureLoad(const struct aiScene *sc, const struct aiNode* nd) { int i; unsigned int n = 0, t; aiMatrix4x4 m = nd->mTransformation; // update transform aiTransposeMatrix4(&m); glPushMatrix(); glMultMatrixf((float*)&m); // draw all meshes assigned to this node for (; n < nd->mNumMeshes; ++n) { const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]]; unsigned int cont = aiGetMaterialTextureCount(sc->mMaterials[mesh->mMaterialIndex], aiTextureType_DIFFUSE); struct aiString* str = (aiString*)malloc(sizeof(struct aiString)); if(cont > 0) { //aiGetMaterialString(sc->mMaterials[mesh->mMaterialIndex],AI_MATKEY_TEXTURE_DIFFUSE(0),str); aiGetMaterialTexture(sc->mMaterials[mesh->mMaterialIndex],aiTextureType_DIFFUSE,0,str,0,0,0,0,0,0); // See if another mesh is already using this texture, if so, just copy GLuint instead of remaking entire texture bool newTextureToBeLoaded = true; for(int x = 0; x < texturesAndPaths.size(); x++) { if(texturesAndPaths[x].pathName == *str) { TextureAndPath reusedTexture; reusedTexture.hTexture = texturesAndPaths[x].hTexture; reusedTexture.pathName = *str; texturesAndPaths.push_back(reusedTexture); newTextureToBeLoaded = false; std::cout << "Texture reused." << std::endl; break; } } if(newTextureToBeLoaded) { FREE_IMAGE_FORMAT formato = FreeImage_GetFileType(str->data,0); //Automatocally detects the format(from over 20 formats!) FIBITMAP* imagen = FreeImage_Load(formato, str->data); FIBITMAP* temp = imagen; imagen = FreeImage_ConvertTo32Bits(imagen); FreeImage_Unload(temp); int w = FreeImage_GetWidth(imagen); int h = FreeImage_GetHeight(imagen); //Some debugging code char* pixeles = (char*)FreeImage_GetBits(imagen); //FreeImage loads in BGR format, so you need to swap some bytes(Or use GL_BGR). //Now generate the OpenGL texture object TextureAndPath newTexture; newTexture.pathName = *str; glGenTextures(1, &newTexture.hTexture); glBindTexture(GL_TEXTURE_2D, newTexture.hTexture); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, GL_BGRA_EXT,GL_UNSIGNED_BYTE,(GLvoid*)pixeles ); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR ); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, newTexture.hTexture); GLenum huboError = glGetError(); if(huboError) { std::cout<<"There was an error loading the texture"<<std::endl; } std::cout << "texture loaded." << std::endl; texturesAndPaths.push_back(newTexture); } } } // Get textures from all children for (n = 0; n < nd->mNumChildren; ++n) recursiveTextureLoad(sc, nd->mChildren[n]); }
void Model::init_model(UString filePath) { std::string _path; #if defined(VIX_SYS_WINDOWS) && defined(UNICODE) UConverter cv; _path = cv.to_bytes(filePath); #else _path = filePath; #endif Assimp::Importer imp; const aiScene* scene = imp.ReadFile(_path, aiProcess_CalcTangentSpace | aiProcess_Triangulate | aiProcess_JoinIdenticalVertices | aiProcess_SortByPType | aiProcess_GenUVCoords); if(!scene) { // DebugPrintF(VTEXT("ASSIMP: Error could not read file [%s]"), _path); return; } size_t numMeshes = scene->mNumMeshes; aiString diffuse; aiString bump; for(size_t i = 0; i < numMeshes; i++) { aiMesh* mesh = scene->mMeshes[i]; size_t matIndex = mesh->mMaterialIndex; aiMaterial* mat = scene->mMaterials[matIndex]; aiGetMaterialTexture(mat, aiTextureType_DIFFUSE, 0, &diffuse); aiGetMaterialTexture(mat, aiTextureType_HEIGHT, 0, &bump); aiColor4D color; aiGetMaterialColor(mat, AI_MATKEY_COLOR_DIFFUSE, &color); printf("MATCOLOR [%f, %f, %f, %f]\n", color.r, color.g, color.b, color.a); printf("MATTEXT [DIFFUSE: %s]\n", diffuse.data); printf("MATTEXT [BUMP: %s]\n", bump.data); /*INIT MODEL CENTROID, SIZE, MIN, AND MAX*/ /*USED FOR COLLIDER*/ aiVector3D min, max, center; FindMeshCenter(mesh, center, min, max); m_min = Vec3(min.x, min.y, min.z); m_max = Vec3(max.x, max.y, max.z); m_size = m_max - m_min; m_centroid = Vec3(center.x, center.y, center.z); InitMesh(mesh); } if(diffuse.C_Str()) { UString texPath = Vixen::UStringFromCharArray(diffuse.data); m_texture = new GLTexture(texPath); } if(bump.C_Str()) { UString texPath = Vixen::UStringFromCharArray(bump.data); m_bump = new GLTexture(texPath); } m_initialized = true; }
void Model3D::recursiveTextureLoad(const struct aiScene *sc, const struct aiNode* nd) { int i; unsigned int n = 0, t; struct aiMatrix4x4 m = nd->mTransformation; // update transform aiTransposeMatrix4(&m); glPushMatrix(); glMultMatrixf((float*) &m); // draw all meshes assigned to this node for (; n < nd->mNumMeshes; ++n) { const struct aiMesh* mesh = sc->mMeshes[nd->mMeshes[n]]; unsigned int cont = aiGetMaterialTextureCount(sc->mMaterials[mesh->mMaterialIndex], aiTextureType_AMBIENT); struct aiString* str = (aiString*) malloc(sizeof(struct aiString)); if (cont > 0) { //aiGetMaterialString(sc->mMaterials[mesh->mMaterialIndex],AI_MATKEY_TEXTURE_DIFFUSE(0),str); aiGetMaterialTexture(sc->mMaterials[mesh->mMaterialIndex], aiTextureType_AMBIENT, 0, str, 0, 0, 0, 0, 0, 0); // See if another mesh is already using this texture, if so, just copy GLuint instead of remaking entire texture bool newTextureToBeLoaded = true; for (int x = 0; x < texturesAndPaths.size(); x++) { if (texturesAndPaths[x].pathName == *str) { TextureAndPath reusedTexture; reusedTexture.hTexture = texturesAndPaths[x].hTexture; reusedTexture.pathName = *str; //printf("%s pathnm \n", reusedTexture.pathName.data); texturesAndPaths.push_back(reusedTexture); newTextureToBeLoaded = false; std::cout << "Texture reused." << std::endl; break; } } if (newTextureToBeLoaded) { texturename.clear(); texturename.append(foldername.c_str()); texturename.append(str->data); std::cout << texturename.c_str() << " strname \n"; Mat imagen=imread(texturename.c_str()); int w = imagen.cols; int h = imagen.rows; int dataSize=imagen.cols*imagen.rows*3; unsigned char* data=new unsigned char[dataSize]; int j=0; for( int y = 0; y < imagen.rows; y++ ) { for( int x = 0; x < imagen.cols; x++ ) { for( int c = 0; c < 3; c++ ) { data[j] = imagen.at<Vec3b>(y,x)[c]; j++; } } } //Now generate the OpenGL texture object TextureAndPath newTexture; newTexture.pathName = *str; glGenTextures(1, &newTexture.hTexture); glBindTexture(GL_TEXTURE_2D, newTexture.hTexture); //glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA, w, h, 0, GL_BGR,GL_UNSIGNED_BYTE,(GLvoid*)pixeles ); //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, 3, imagen.cols, imagen.rows, GL_BGR_EXT, GL_UNSIGNED_BYTE, data); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glBindTexture(GL_TEXTURE_2D, newTexture.hTexture); GLenum huboError = glGetError(); if (huboError) { std::cout << "There was an error loading the texture" << std::endl; } std::cout << "texture loaded." << std::endl; texturesAndPaths.push_back(newTexture); } } } // Get textures from all children for (n = 0; n < nd->mNumChildren; ++n) recursiveTextureLoad(sc, nd->mChildren[n]); }
static Mesh* process_mesh(struct aiMesh* mesh, struct aiScene* scene) { Vertex **vertices = malloc(sizeof(Vertex *) * mesh->mNumVertices); for (int i = 0; i < mesh->mNumVertices; i++) { vertices[i] = malloc(sizeof(Vertex)); } // Process vertices if (mesh->mVertices && mesh->mNumVertices > 0) { for (int i = 0; i < mesh->mNumVertices; i++) { struct aiVector3D position = mesh->mVertices[i]; vertices[i]->position = create_vec(position.x, position.y, position.z, 1.0); } } else { gl_log(INFO, "A mesh was processed with no vertices."); } // Process normals if (mesh->mNormals) { for (int i = 0; i < mesh->mNumVertices; i++) { struct aiVector3D normal = mesh->mNormals[i]; vertices[i]->normal = create_vec(normal.x, normal.y, normal.z, 1.0); } } else { gl_log(INFO, "A mesh was processed with no normals."); } // Process texture coords if (mesh->mTextureCoords[0]) { for (int i = 0; i < mesh->mNumVertices; i++) { struct aiVector3D texture = mesh->mTextureCoords[0][i]; vertices[i]->texture_coords = create_vec(texture.x, texture.y, texture.z, 1.0); } } else { for (int i = 0; i < mesh->mNumVertices; i++) { vertices[i]->texture_coords = 0; } gl_log(INFO, "A mesh was processed with no texture coordinates."); } // Process material Texture* mesh_texture = malloc(sizeof(Texture)); Material* mesh_material = malloc(sizeof(Material)); if (mesh->mMaterialIndex >= 0) { struct aiString path; struct aiMaterial *material = scene->mMaterials[mesh->mMaterialIndex]; aiGetMaterialTexture(material, aiTextureType_DIFFUSE, 0, &path, 0, 0, 0, 0, 0, 0); mesh_texture->id = create_texture(path.data); struct aiColor4D color; aiGetMaterialColor(material, AI_MATKEY_COLOR_DIFFUSE, &color); mesh_material->diffuse_color = create_vec(color.r, color.g, color.b, color.a); aiGetMaterialColor(material, AI_MATKEY_COLOR_AMBIENT, &color); mesh_material->ambient_color = create_vec(color.r, color.g, color.b, color.a); aiGetMaterialColor(material, AI_MATKEY_COLOR_SPECULAR, &color); mesh_material->specular_color = create_vec(color.r, color.g, color.b, color.a); } else { gl_log(INFO, "A mesh was processed with no material."); } return create_mesh(vertices, mesh->mNumVertices, mesh_texture, mesh_material); }