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
|
int Object::loadObj(std::string pathNameSansExt) {
std::string l, usemtl;
char c; int old_tex = 0;
std::string pathName = pathNameSansExt + ".obj";
std::string pathNameMtl = pathNameSansExt + ".mtl";
std::ifstream file(pathName.c_str(), std::ios::in);
if (!file.good()) {
std::cerr << "error: '" << pathName << "' isn't a file" << std::endl;
return BAD_NAME;
}
loadMTL(pathNameMtl);
while (!file.eof()) {
file.get(c);
if (c == '#')
while (c!='\n') file.get(c);
else if(c == 'u') {
file.get(c);
while (c != ' ')
file.get(c);
file >> usemtl;
}
else if(c == 'v') {
float x, y, z;
file.get(c);
if(c == ' ') {
file >> x >> y >> z;
_points.push_back(Point(x, y, z));
}
if(c == 'n')
file >> x >> y >> z;
if(c == 't') {
file >> x >> y;
_vt.push_back(Point(x, y));
}
}
else if(c == 'f') {
int x = 0, y = 0, z = 0;
int xt= 0, yt= 0, zt= 0;
std::vector<int> v;
std::vector<int> t;
std::string tmp;
file >> tmp;
int i;
t.push_back(_textures[usemtl]);
for (i=0; tmp[i]!='/' && tmp.size()>i; ++i)
x = (tmp[i] - '0') + x * 10;
for (i=i+1; tmp[i]!='/' && tmp.size()>i; ++i)
xt = (tmp[i] - '0') + xt * 10;
v.push_back(x-1);
t.push_back(xt-1);
file >> tmp;
for (i=0; tmp[i]!='/' && tmp.size()>i; ++i)
y = (tmp[i] - '0') + y * 10;
for (i = i+1; tmp[i]!='/' && tmp.size()>i; ++i)
yt = (tmp[i] - '0') + yt * 10;
v.push_back(y-1);
t.push_back(yt-1);
file >> tmp;
for (i=0; tmp[i]!='/' && tmp.size()>i; ++i)
z = (tmp[i] - '0') + z * 10;
for (i=i+1; tmp[i]!='/' && tmp.size()>i; ++i)
zt = (tmp[i] - '0') + zt * 10;
v.push_back(z-1);
t.push_back(zt-1);
_arrPoints.push_back(v);
_arrTexture.push_back(t);
if (old_tex != t.at(0)) {
std::cerr << t.at(0) << " "; //std::endl;
old_tex = t.at(0);
}
}
else
while (c!='\n')
file.get(c);
}
_good = true;
calculerBarycentre();
return SUCCESS;
}
int Object::loadMTL(std::string pathName) {
bool premier = true;
std::ifstream file(pathName.c_str(), std::ios::in);
if (!file.good()) {
std::cerr << "error: '" << pathName << "' isn't a file" << std::endl;
return BAD_NAME;
}
std::string tmp, nomMTL, chemin;
char c = (char)0;
while (!file.eof()) {
file >> tmp;
if (tmp == "newmtl") {
if(premier) {
file >> nomMTL;
premier = false;
}
else {
_textures[nomMTL] = loadTexture(chemin.c_str());
std::cerr << "nomMTL: " << nomMTL << ", contenu: " << _textures[nomMTL] << std::endl;
file >> nomMTL;
}
}
else if (tmp == "map_Kd") {
file >> chemin;
}
else
while (c!='\n')
file.get(c);
}
_textures[nomMTL] = loadTexture(chemin.c_str());
std::cerr << "nomMTL: " << nomMTL << ", contenu: " << _textures[nomMTL] << std::endl;
return SUCCESS;
}
int Object::loadTexture(const char * filename,bool useMipMap) {
GLuint glID;
SDL_Surface * picture_surface = NULL;
SDL_Surface *gl_surface = NULL;
SDL_Surface * gl_fliped_surface = NULL;
Uint32 rmask, gmask, bmask, amask;
picture_surface = IMG_Load(filename);
if (picture_surface == NULL)
return BAD_NAME;
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
SDL_PixelFormat format = *(picture_surface->format);
format.BitsPerPixel = 32;
format.BytesPerPixel = 4;
format.Rmask = rmask;
format.Gmask = gmask;
format.Bmask = bmask;
format.Amask = amask;
gl_surface = SDL_ConvertSurface(picture_surface,&format,SDL_SWSURFACE);
gl_fliped_surface = flipSurface(gl_surface);
glGenTextures(1, &glID);
glBindTexture(GL_TEXTURE_2D, glID);
if (useMipMap)
{
gluBuild2DMipmaps(GL_TEXTURE_2D, 4, gl_fliped_surface->w,
gl_fliped_surface->h, GL_RGBA,GL_UNSIGNED_BYTE,
gl_fliped_surface->pixels);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_LINEAR);
}
else
{
glTexImage2D(GL_TEXTURE_2D, 0, 4, gl_fliped_surface->w,
gl_fliped_surface->h, 0, GL_RGBA,GL_UNSIGNED_BYTE,
gl_fliped_surface->pixels);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
}
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
SDL_FreeSurface(gl_fliped_surface);
SDL_FreeSurface(gl_surface);
SDL_FreeSurface(picture_surface);
return glID;
} |
Partager