1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295
|
#include "RxAssimpLoader.h"
Assimp::Importer RxAssimpLoader::importer;
RxAssimpLoader::RxAssimpLoader()
{
scene = NULL;
}
RxAssimpLoader::RxAssimpLoader(const string &pFile, unsigned int flags)
{
scene = NULL;
Import3DFromFile(pFile,flags);
}
const aiScene* RxAssimpLoader::getaiScene()
{
}
bool RxAssimpLoader::Import3DFromFile(const string &pFile, unsigned int flags)
{
cfile = pFile;
//Vérification de l'existance du fichier
ifstream fin(cfile.c_str());
if(!fin.fail())
{
fin.close();
}
else
{
LOGGER<<"ERREUR LOADER ASSIMP : "<<importer.GetErrorString()<<endl;
return erreur=false;
}
//importation de la scène
scene = importer.ReadFile( pFile, flags);
// si erreur
if( !scene)
{
LOGGER<<"ERREUR LOADER ASSIMP : "<<importer.GetErrorString()<<endl;
return erreur=false;
}
// pas d'erreur
LOGGER<<"LOADER ASSIMP : Import of scene " + pFile + " succeeded.";
//on charge l'ensemble des textures
LoadGLTextures();
return erreur=true;
}
int RxAssimpLoader::LoadGLTextures()
{
if (scene->HasTextures()) return 0; //pas de texture
/* Relève le filename des textures */
for (unsigned int m=0; m<scene->mNumMaterials; m++) //pour chacune des texture
{
int texIndex = 0;
aiReturn texFound = AI_SUCCESS;
aiString path; // filename
while (texFound == AI_SUCCESS)
{
texFound = scene->mMaterials[m]->GetTexture(aiTextureType_DIFFUSE, texIndex, &path);
//enregiste le filename de la texture dans la structure map
//(clé sans valeur pr l'instant, le chargement ce fera ensuite)
textureFile[path.data] = 0;
texIndex++;
}
}
int numTextures = textureFile.size();
/* define texture path */
//std::string texturepath = "../../../test/models/Obj/";
map<string,RxTexture>::iterator itr = textureFile.begin();
for (; itr != textureFile.end(); itr++)
{
string filename = itr->first; // chemin d'accès de l'image
string fileloc = Dir(cfile)+"/" + filename; // chargement de l'image
if (itr->second = LoadTexture(fileloc.c_str())) //Si erreur de chargement texture
{
LOGGER<<"ERREUR : Chargement impossible : " + fileloc<<endl;
}
}
//retourne le nombre de texture
return numTextures;
}
// Can't send color down as a pointer to aiColor4D because AI colors are ABGR.
void RxAssimpLoader::Color4f(const struct aiColor4D *color)
{
glColor4f(color->r, color->g, color->b, color->a);
}
void RxAssimpLoader::set_float4(float f[4], float a, float b, float c, float d)
{
f[0] = a;
f[1] = b;
f[2] = c;
f[3] = d;
}
void RxAssimpLoader::color4_to_float4(const struct aiColor4D *c, float f[4])
{
f[0] = c->r;
f[1] = c->g;
f[2] = c->b;
f[3] = c->a;
}
void RxAssimpLoader::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;
unsigned int max; // changed: to unsigned*/
int texIndex = 0;
aiString texPath; //contains filename of texture
if(AI_SUCCESS == mtl->GetTexture(aiTextureType_DIFFUSE, texIndex, &texPath))
{
//bind texture
glBindTexture(GL_TEXTURE_2D, textureFile[texPath.data]);
}
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 RxAssimpLoader::recursive_render (const struct aiScene *sc, const struct aiNode* nd, float scale)
{
unsigned int i;
unsigned int n=0, t;
struct aiMatrix4x4 m = nd->mTransformation;
m.Scaling(aiVector3D(scale, scale, scale), m);
// update transform
m.Transpose();
glPushMatrix();
glMultMatrixf((float*)&m);
// draw all meshes assigned to this node
for (; n < nd->mNumMeshes; ++n)
{
const struct aiMesh* mesh = scene->mMeshes[nd->mMeshes[n]];
apply_material(sc->mMaterials[mesh->mMaterialIndex]);
if(mesh->mNormals == NULL)
{
glDisable(GL_LIGHTING);
}
else
{
glEnable(GL_LIGHTING);
}
if(mesh->mColors[0] != NULL)
{
glEnable(GL_COLOR_MATERIAL);
}
else
{
glDisable(GL_COLOR_MATERIAL);
}
for (t = 0; t < mesh->mNumFaces; ++t) {
const struct aiFace* face = &mesh->mFaces[t];
GLenum face_mode;
switch(face->mNumIndices)
{
case 1: face_mode = GL_POINTS; break;
case 2: face_mode = GL_LINES; break;
case 3: face_mode = GL_TRIANGLES; break;
default: face_mode = GL_POLYGON; break;
}
glBegin(face_mode);
for(i = 0; i < face->mNumIndices; i++) // go through all vertices in face
{
int vertexIndex = face->mIndices[i]; // get group index for current index
if(mesh->mColors[0] != NULL)
Color4f(&mesh->mColors[0][vertexIndex]);
if(mesh->mNormals != NULL)
if(mesh->HasTextureCoords(0)) //HasTextureCoords(texture_coordinates_set)
{
glTexCoord2f(mesh->mTextureCoords[0][vertexIndex].x, 1 - mesh->mTextureCoords[0][vertexIndex].y); //mTextureCoords[channel][vertex]
}
glNormal3fv(&mesh->mNormals[vertexIndex].x);
glVertex3fv(&mesh->mVertices[vertexIndex].x);
}
glEnd();
}
}
// draw all children
for (n = 0; n < nd->mNumChildren; ++n)
{
recursive_render(sc, nd->mChildren[n],scale);
}
glPopMatrix();
}
void RxAssimpLoader::Draw(float scale)
{
if(scene)
recursive_render(scene, scene->mRootNode,scale);
} |
Partager