static int TextureCount(lua_State *L) { material_t *material = checkmaterial(L, 1); unsigned int type = checktexturetype(L, 2); unsigned int count = aiGetMaterialTextureCount(material, (enum aiTextureType)type); lua_pushinteger(L, count); return 1; }
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 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]); }