[2823] | 1 | /* |
---|
| 2 | orxonox - the future of 3D-vertical-scrollers |
---|
| 3 | |
---|
| 4 | Copyright (C) 2004 orx |
---|
| 5 | |
---|
| 6 | This program is free software; you can redistribute it and/or modify |
---|
| 7 | it under the terms of the GNU General Public License as published by |
---|
| 8 | the Free Software Foundation; either version 2, or (at your option) |
---|
| 9 | any later version. |
---|
| 10 | |
---|
| 11 | ### File Specific: |
---|
| 12 | main-programmer: Benjamin Grauer |
---|
| 13 | co-programmer: ... |
---|
[3140] | 14 | |
---|
| 15 | TGA-code: borrowed from nehe-Tutorials |
---|
| 16 | |
---|
[2823] | 17 | */ |
---|
| 18 | |
---|
[2776] | 19 | #include "material.h" |
---|
| 20 | |
---|
[3140] | 21 | // headers only for PathList |
---|
| 22 | #include <unistd.h> |
---|
| 23 | #include <sys/types.h> |
---|
| 24 | #include <sys/stat.h> |
---|
| 25 | #include <stdlib.h> |
---|
| 26 | #include <fstream> |
---|
| 27 | |
---|
| 28 | using namespace std; |
---|
| 29 | |
---|
[3186] | 30 | /** |
---|
| 31 | \brief creates a ned PathList. |
---|
| 32 | |
---|
| 33 | It is a good idea to use this as an initial List, |
---|
| 34 | because if you give on a name the Path will not be checked for its existence. |
---|
| 35 | */ |
---|
[3140] | 36 | PathList::PathList() |
---|
| 37 | { |
---|
[3195] | 38 | this->pathName = NULL; |
---|
| 39 | this->next = NULL; |
---|
[3140] | 40 | } |
---|
[3186] | 41 | |
---|
| 42 | /** |
---|
| 43 | \brief Creates a new PathList with a Name. |
---|
| 44 | \param pName the Name of The Path. |
---|
| 45 | |
---|
| 46 | This function just adds the Path without checking if it exists. |
---|
| 47 | */ |
---|
[3140] | 48 | PathList::PathList(char* pName) |
---|
| 49 | { |
---|
[3195] | 50 | this->pathName = new char [strlen(pName)+1]; |
---|
| 51 | strcpy (this->pathName, pName); |
---|
| 52 | this->next = NULL; |
---|
[3140] | 53 | } |
---|
| 54 | |
---|
[3186] | 55 | /** |
---|
| 56 | \brief destroys a PathList |
---|
| 57 | |
---|
| 58 | It does this by deleting the Name and then delete its preceding PathList. |
---|
| 59 | */ |
---|
[3140] | 60 | PathList::~PathList() |
---|
| 61 | { |
---|
[3195] | 62 | if (this->pathName) |
---|
| 63 | delete []this->pathName; |
---|
| 64 | if (this->next) |
---|
| 65 | delete this->next; |
---|
[3140] | 66 | } |
---|
| 67 | |
---|
[3186] | 68 | /** |
---|
| 69 | \brief Adds a new Pathlist Element. |
---|
| 70 | \param pName |
---|
| 71 | |
---|
| 72 | Adding a Path automatically checks if the Path exists, |
---|
| 73 | and if it does not it will not add it to the List. |
---|
| 74 | */ |
---|
[3140] | 75 | void PathList::addPath (char* pName) |
---|
| 76 | { |
---|
| 77 | if (pName[0] == '\0') |
---|
| 78 | { |
---|
[3206] | 79 | PRINTF(3)("not Adding empty Path to the List.\n"); |
---|
[3140] | 80 | return; |
---|
| 81 | } |
---|
| 82 | char* tmpPName = new char[strlen(pName)]; |
---|
| 83 | strncpy(tmpPName, pName, strlen(pName)-1); |
---|
| 84 | tmpPName[strlen(pName)-1] = '\0'; |
---|
| 85 | if (access (tmpPName, F_OK) == 0) |
---|
| 86 | { |
---|
| 87 | struct stat status; |
---|
| 88 | stat(tmpPName, &status); |
---|
| 89 | if (status.st_mode & S_IFDIR) |
---|
| 90 | { |
---|
[3206] | 91 | PRINTF(2)("Adding Path %s to the PathList.\n", pName); |
---|
[3140] | 92 | PathList* tmpPathList = this; |
---|
| 93 | while (tmpPathList->next) |
---|
| 94 | tmpPathList = tmpPathList->next; |
---|
| 95 | tmpPathList->next = new PathList(pName); |
---|
| 96 | } |
---|
| 97 | else |
---|
[3206] | 98 | PRINTF(2)("You tried to add non-folder %s to a PathList.\n", tmpPName); |
---|
[3140] | 99 | } |
---|
| 100 | else |
---|
[3206] | 101 | PRINTF(2)("You tried to add non-existing folder %s to a PathList.\n", tmpPName); |
---|
[3140] | 102 | delete []tmpPName; |
---|
| 103 | } |
---|
| 104 | |
---|
[2842] | 105 | /** |
---|
| 106 | \brief creates a default Material with no Name |
---|
| 107 | normally you call this to create a material List (for an obj-file) and then append with addMaterial() |
---|
| 108 | */ |
---|
[2776] | 109 | Material::Material() |
---|
| 110 | { |
---|
[3195] | 111 | this->init(); |
---|
[2778] | 112 | |
---|
[3195] | 113 | this->setName (""); |
---|
[2776] | 114 | } |
---|
| 115 | |
---|
[2842] | 116 | /** |
---|
| 117 | \brief creates a Material. |
---|
| 118 | \param mtlName Name of the Material to be added to the Material List |
---|
| 119 | */ |
---|
[2776] | 120 | Material::Material (char* mtlName) |
---|
| 121 | { |
---|
[3195] | 122 | this->init(); |
---|
[2776] | 123 | |
---|
[3195] | 124 | this->setName (mtlName); |
---|
[2776] | 125 | } |
---|
| 126 | |
---|
[2847] | 127 | /** |
---|
| 128 | \brief deletes a Material |
---|
| 129 | */ |
---|
| 130 | Material::~Material() |
---|
| 131 | { |
---|
[3206] | 132 | PRINTF(2)("delete Material %s.\n", this->name); |
---|
[3195] | 133 | if (this->name) |
---|
| 134 | delete []this->name; |
---|
| 135 | if (this->diffuseTextureSet) |
---|
| 136 | glDeleteTextures (1, &this->diffuseTexture); |
---|
| 137 | if (this->nextMat) |
---|
| 138 | delete this->nextMat; |
---|
[2847] | 139 | } |
---|
| 140 | |
---|
[2842] | 141 | /** |
---|
| 142 | \brief adds a new Material to the List. |
---|
| 143 | this Function will append a new Material to the end of a Material List. |
---|
| 144 | \param mtlName The name of the Material to be added. |
---|
| 145 | */ |
---|
[2778] | 146 | Material* Material::addMaterial(char* mtlName) |
---|
| 147 | { |
---|
[3206] | 148 | PRINTF(2)("adding Material %s.\n", mtlName); |
---|
[3140] | 149 | Material* tmpMat = this; |
---|
[2778] | 150 | while (tmpMat->nextMat != NULL) |
---|
| 151 | { |
---|
| 152 | tmpMat = tmpMat->nextMat; |
---|
| 153 | } |
---|
[3140] | 154 | tmpMat->nextMat = new Material(mtlName); |
---|
| 155 | return tmpMat->nextMat; |
---|
[2778] | 156 | |
---|
| 157 | } |
---|
| 158 | |
---|
[2842] | 159 | /** |
---|
| 160 | \brief initializes a new Material with its default Values |
---|
| 161 | */ |
---|
[2776] | 162 | void Material::init(void) |
---|
| 163 | { |
---|
[3206] | 164 | PRINTF(2)("initializing new Material.\n"); |
---|
[3195] | 165 | this->nextMat = NULL; |
---|
| 166 | this->name =""; |
---|
| 167 | this->setIllum(1); |
---|
| 168 | this->setDiffuse(0,0,0); |
---|
| 169 | this->setAmbient(0,0,0); |
---|
| 170 | this->setSpecular(.5,.5,.5); |
---|
| 171 | this->setShininess(2.0); |
---|
| 172 | this->setTransparency(0.0); |
---|
[3070] | 173 | |
---|
[3195] | 174 | if (!this->pathList) |
---|
| 175 | this->pathList = new PathList(""); |
---|
[3140] | 176 | |
---|
| 177 | |
---|
[3195] | 178 | this->diffuseTextureSet = false; |
---|
| 179 | this->ambientTextureSet = false; |
---|
| 180 | this->specularTextureSet = false; |
---|
[3070] | 181 | |
---|
[2836] | 182 | |
---|
[2776] | 183 | } |
---|
| 184 | |
---|
[3140] | 185 | PathList *Material::pathList = NULL; |
---|
| 186 | |
---|
[2842] | 187 | /** |
---|
[3140] | 188 | \brief Search for a Material called mtlName |
---|
| 189 | \param mtlName the Name of the Material to search for |
---|
| 190 | \returns Material named mtlName if it is found. NULL otherwise. |
---|
| 191 | */ |
---|
| 192 | Material* Material::search (char* mtlName) |
---|
| 193 | { |
---|
[3206] | 194 | PRINTF(2)("Searching for material %s", mtlName); |
---|
[3140] | 195 | Material* searcher = this; |
---|
| 196 | while (searcher != NULL) |
---|
| 197 | { |
---|
[3206] | 198 | PRINTF(2)("."); |
---|
[3140] | 199 | if (!strcmp (searcher->getName(), mtlName)) |
---|
| 200 | { |
---|
[3206] | 201 | PRINTF(2)("found.\n"); |
---|
[3140] | 202 | return searcher; |
---|
| 203 | } |
---|
| 204 | searcher = searcher->nextMat; |
---|
| 205 | } |
---|
[3206] | 206 | PRINTF(2)("not found\n"); |
---|
[3140] | 207 | return NULL; |
---|
| 208 | } |
---|
| 209 | |
---|
| 210 | /** |
---|
| 211 | \brief sets the material with which the following Faces will be painted |
---|
| 212 | */ |
---|
| 213 | bool Material::select (void) |
---|
| 214 | { |
---|
| 215 | // setting diffuse color |
---|
| 216 | // glColor3f (diffuse[0], diffuse[1], diffuse[2]); |
---|
[3195] | 217 | glMaterialfv(GL_FRONT, GL_DIFFUSE, this->diffuse); |
---|
[3140] | 218 | |
---|
| 219 | // setting ambient color |
---|
[3195] | 220 | glMaterialfv(GL_FRONT, GL_AMBIENT, this->ambient); |
---|
[3140] | 221 | |
---|
| 222 | // setting up Sprecular |
---|
[3195] | 223 | glMaterialfv(GL_FRONT, GL_SPECULAR, this->specular); |
---|
[3140] | 224 | |
---|
| 225 | // setting up Shininess |
---|
[3195] | 226 | glMaterialf(GL_FRONT, GL_SHININESS, this->shininess); |
---|
[3140] | 227 | |
---|
| 228 | // setting illumination Model |
---|
[3195] | 229 | if (this->illumModel == 1) //! \todo make this work, if no vertex-normals are read. |
---|
[3140] | 230 | glShadeModel(GL_FLAT); |
---|
[3195] | 231 | else if (this->illumModel >= 2) |
---|
[3140] | 232 | glShadeModel(GL_SMOOTH); |
---|
| 233 | |
---|
[3195] | 234 | if (this->diffuseTextureSet) |
---|
| 235 | glBindTexture(GL_TEXTURE_2D, this->diffuseTexture); |
---|
[3140] | 236 | else |
---|
| 237 | glBindTexture(GL_TEXTURE_2D, 0); |
---|
| 238 | |
---|
| 239 | } |
---|
| 240 | |
---|
| 241 | |
---|
| 242 | /** |
---|
[2842] | 243 | \brief Set the Name of the Material. (Important for searching) |
---|
| 244 | \param mtlName the Name of the Material to be set. |
---|
| 245 | */ |
---|
[2776] | 246 | void Material::setName (char* mtlName) |
---|
| 247 | { |
---|
[3206] | 248 | PRINTF(3)("setting Material Name to %s.\n", this->name); |
---|
[3195] | 249 | this->name = new char [strlen(mtlName)+1]; |
---|
| 250 | strcpy(this->name, mtlName); |
---|
[3206] | 251 | } |
---|
[3140] | 252 | |
---|
[2842] | 253 | /** |
---|
| 254 | \returns The Name of The Material |
---|
| 255 | */ |
---|
[2778] | 256 | char* Material::getName (void) |
---|
| 257 | { |
---|
[3195] | 258 | return this->name; |
---|
[2778] | 259 | } |
---|
[2776] | 260 | |
---|
[2842] | 261 | /** |
---|
| 262 | \brief Sets the Material Illumination Model. |
---|
| 263 | \brief illu illumination Model in int form |
---|
| 264 | */ |
---|
[2776] | 265 | void Material::setIllum (int illum) |
---|
| 266 | { |
---|
[3206] | 267 | PRINTF(3)("setting illumModel of Material %s to %i\n", this->name, illum); |
---|
[3195] | 268 | this->illumModel = illum; |
---|
[3208] | 269 | // PRINTF(3)("setting illumModel to: %i\n", illumModel); |
---|
[2776] | 270 | } |
---|
[2842] | 271 | /** |
---|
| 272 | \brief Sets the Material Illumination Model. |
---|
| 273 | \brief illu illumination Model in char* form |
---|
| 274 | */void Material::setIllum (char* illum) |
---|
[2776] | 275 | { |
---|
[3195] | 276 | this->setIllum (atoi(illum)); |
---|
[2776] | 277 | } |
---|
| 278 | |
---|
[2842] | 279 | /** |
---|
| 280 | \brief Sets the Material Diffuse Color. |
---|
| 281 | \param r Red Color Channel. |
---|
| 282 | \param g Green Color Channel. |
---|
| 283 | \param b Blue Color Channel. |
---|
| 284 | */ |
---|
[2776] | 285 | void Material::setDiffuse (float r, float g, float b) |
---|
| 286 | { |
---|
[3206] | 287 | PRINTF(3)("setting Diffuse Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); |
---|
[3195] | 288 | this->diffuse[0] = r; |
---|
| 289 | this->diffuse[1] = g; |
---|
| 290 | this->diffuse[2] = b; |
---|
| 291 | this->diffuse[3] = 1.0; |
---|
[2780] | 292 | |
---|
[2776] | 293 | } |
---|
[2842] | 294 | /** |
---|
| 295 | \brief Sets the Material Diffuse Color. |
---|
| 296 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
| 297 | */ |
---|
[2776] | 298 | void Material::setDiffuse (char* rgb) |
---|
| 299 | { |
---|
[3140] | 300 | float r,g,b; |
---|
| 301 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 302 | this->setDiffuse (r, g, b); |
---|
[2776] | 303 | } |
---|
| 304 | |
---|
[2842] | 305 | /** |
---|
| 306 | \brief Sets the Material Ambient Color. |
---|
| 307 | \param r Red Color Channel. |
---|
| 308 | \param g Green Color Channel. |
---|
| 309 | \param b Blue Color Channel. |
---|
| 310 | */ |
---|
[2776] | 311 | void Material::setAmbient (float r, float g, float b) |
---|
| 312 | { |
---|
[3206] | 313 | PRINTF(3)("setting Ambient Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); |
---|
[3195] | 314 | this->ambient[0] = r; |
---|
| 315 | this->ambient[1] = g; |
---|
| 316 | this->ambient[2] = b; |
---|
| 317 | this->ambient[3] = 1.0; |
---|
[2776] | 318 | } |
---|
[2842] | 319 | /** |
---|
| 320 | \brief Sets the Material Ambient Color. |
---|
| 321 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
| 322 | */ |
---|
[2776] | 323 | void Material::setAmbient (char* rgb) |
---|
| 324 | { |
---|
[3140] | 325 | float r,g,b; |
---|
| 326 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 327 | this->setAmbient (r, g, b); |
---|
[2776] | 328 | } |
---|
| 329 | |
---|
[2842] | 330 | /** |
---|
| 331 | \brief Sets the Material Specular Color. |
---|
| 332 | \param r Red Color Channel. |
---|
| 333 | \param g Green Color Channel. |
---|
| 334 | \param b Blue Color Channel. |
---|
| 335 | */ |
---|
[2776] | 336 | void Material::setSpecular (float r, float g, float b) |
---|
| 337 | { |
---|
[3206] | 338 | PRINTF(3)("setting Specular Color of Material %s to r=%f g=%f b=%f.\n", this->name, r, g, b); |
---|
[3195] | 339 | this->specular[0] = r; |
---|
| 340 | this->specular[1] = g; |
---|
| 341 | this->specular[2] = b; |
---|
| 342 | this->specular[3] = 1.0; |
---|
[2804] | 343 | } |
---|
[2842] | 344 | /** |
---|
| 345 | \brief Sets the Material Specular Color. |
---|
| 346 | \param rgb The red, green, blue channel in char format (with spaces between them) |
---|
| 347 | */ |
---|
[2776] | 348 | void Material::setSpecular (char* rgb) |
---|
| 349 | { |
---|
[3140] | 350 | float r,g,b; |
---|
| 351 | sscanf (rgb, "%f %f %f", &r, &g, &b); |
---|
[3195] | 352 | this->setSpecular (r, g, b); |
---|
[2776] | 353 | } |
---|
| 354 | |
---|
[2842] | 355 | /** |
---|
| 356 | \brief Sets the Material Shininess. |
---|
| 357 | \param shini stes the Shininess from float. |
---|
| 358 | */ |
---|
[2836] | 359 | void Material::setShininess (float shini) |
---|
| 360 | { |
---|
[3195] | 361 | this->shininess = shini; |
---|
[2836] | 362 | } |
---|
[2842] | 363 | /** |
---|
| 364 | \brief Sets the Material Shininess. |
---|
| 365 | \param shini stes the Shininess from char*. |
---|
| 366 | */ |
---|
[2836] | 367 | void Material::setShininess (char* shini) |
---|
| 368 | { |
---|
[3195] | 369 | this->setShininess (atof(shini)); |
---|
[2836] | 370 | } |
---|
[2776] | 371 | |
---|
[2842] | 372 | /** |
---|
| 373 | \brief Sets the Material Transparency. |
---|
| 374 | \param trans stes the Transparency from int. |
---|
| 375 | */ |
---|
[2776] | 376 | void Material::setTransparency (float trans) |
---|
| 377 | { |
---|
[3206] | 378 | PRINTF(3)("setting Transparency of Material %s to %f.\n", this->name, trans); |
---|
[3195] | 379 | this->transparency = trans; |
---|
[2776] | 380 | } |
---|
[2842] | 381 | /** |
---|
| 382 | \brief Sets the Material Transparency. |
---|
| 383 | \param trans stes the Transparency from char*. |
---|
| 384 | */ |
---|
[2776] | 385 | void Material::setTransparency (char* trans) |
---|
| 386 | { |
---|
[3195] | 387 | this->setTransparency (atof(trans)); |
---|
[2776] | 388 | } |
---|
[2778] | 389 | |
---|
[3140] | 390 | /** |
---|
| 391 | \brief Adds a Texture Path to the List of already existing Paths |
---|
| 392 | \param pathName The Path to add. |
---|
| 393 | */ |
---|
| 394 | void Material::addTexturePath(char* pathName) |
---|
| 395 | { |
---|
[3195] | 396 | this->pathList->addPath (pathName); |
---|
[3140] | 397 | } |
---|
| 398 | |
---|
| 399 | /** |
---|
| 400 | \brief Searches for a Texture inside one of the defined Paths |
---|
| 401 | \param texName The name of the texture o search for. |
---|
| 402 | \returns pathName+texName if texName was found in the pathList. NULL if the Texture is not found. |
---|
| 403 | */ |
---|
| 404 | char* Material::searchTextureInPaths(char* texName) const |
---|
| 405 | { |
---|
| 406 | char* tmpName = NULL; |
---|
| 407 | PathList* pList = pathList; |
---|
| 408 | while (pList) |
---|
| 409 | { |
---|
| 410 | if (pList->pathName) |
---|
| 411 | { |
---|
| 412 | tmpName = new char [strlen(pList->pathName)+strlen(texName)+1]; |
---|
| 413 | strcpy(tmpName, pList->pathName); |
---|
| 414 | } |
---|
| 415 | else |
---|
| 416 | { |
---|
| 417 | tmpName = new char [strlen(texName)+1]; |
---|
| 418 | tmpName[0]='\0'; |
---|
| 419 | } |
---|
| 420 | strcat(tmpName, texName); |
---|
| 421 | if (access (tmpName, F_OK) == 0) |
---|
| 422 | return tmpName; |
---|
| 423 | |
---|
| 424 | if (tmpName) |
---|
| 425 | delete []tmpName; |
---|
| 426 | tmpName = NULL; |
---|
| 427 | pList = pList->next; |
---|
| 428 | } |
---|
| 429 | return NULL; |
---|
| 430 | } |
---|
| 431 | |
---|
| 432 | |
---|
[3070] | 433 | // MAPPING // |
---|
| 434 | |
---|
[2842] | 435 | /** |
---|
[3070] | 436 | \brief Sets the Materials Diffuse Map |
---|
| 437 | \param dMap the Name of the Image to Use |
---|
| 438 | */ |
---|
| 439 | void Material::setDiffuseMap(char* dMap) |
---|
| 440 | { |
---|
[3206] | 441 | PRINTF(3)("setting Diffuse Map %s\n", dMap); |
---|
[3070] | 442 | |
---|
[3140] | 443 | // diffuseTextureSet = loadBMP(dMap, &diffuseTexture); |
---|
[3195] | 444 | this->diffuseTextureSet = this->loadImage(dMap, &this->diffuseTexture); |
---|
[3070] | 445 | |
---|
| 446 | } |
---|
| 447 | |
---|
| 448 | /** |
---|
| 449 | \brief Sets the Materials Ambient Map |
---|
| 450 | \param aMap the Name of the Image to Use |
---|
[3195] | 451 | \todo implement this |
---|
[3070] | 452 | */ |
---|
| 453 | void Material::setAmbientMap(char* aMap) |
---|
| 454 | { |
---|
| 455 | SDL_Surface* ambientMap; |
---|
| 456 | |
---|
| 457 | } |
---|
| 458 | |
---|
| 459 | /** |
---|
| 460 | \brief Sets the Materials Specular Map |
---|
| 461 | \param sMap the Name of the Image to Use |
---|
[3195] | 462 | \todo implement this |
---|
[3070] | 463 | */ |
---|
| 464 | void Material::setSpecularMap(char* sMap) |
---|
| 465 | { |
---|
| 466 | SDL_Surface* specularMap; |
---|
| 467 | |
---|
| 468 | } |
---|
| 469 | |
---|
| 470 | /** |
---|
| 471 | \brief Sets the Materials Bumpiness |
---|
| 472 | \param bump the Name of the Image to Use |
---|
[3195] | 473 | \todo implemet this |
---|
[3070] | 474 | */ |
---|
| 475 | void Material::setBump(char* bump) |
---|
| 476 | { |
---|
| 477 | |
---|
| 478 | } |
---|
| 479 | |
---|
[3186] | 480 | /** |
---|
| 481 | \brief Loads a Texture to the openGL-environment. |
---|
| 482 | \param pImage The Image to load to openGL |
---|
| 483 | \param texture The Texture to apply it to. |
---|
| 484 | */ |
---|
[3140] | 485 | bool Material::loadTexToGL (Image* pImage, GLuint* texture) |
---|
| 486 | { |
---|
[3206] | 487 | PRINTF(2)("Loading texture to OpenGL-Environment.\n"); |
---|
[3140] | 488 | glGenTextures(1, texture); |
---|
| 489 | glBindTexture(GL_TEXTURE_2D, *texture); |
---|
| 490 | /* not Working, and not needed. |
---|
| 491 | glTexImage2D( GL_TEXTURE_2D, 0, 3, width, |
---|
| 492 | height, 0, GL_BGR, |
---|
| 493 | GL_UNSIGNED_BYTE, map->pixels ); |
---|
| 494 | */ |
---|
| 495 | gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, GL_RGB, GL_UNSIGNED_BYTE, pImage->data); |
---|
| 496 | |
---|
| 497 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); |
---|
| 498 | glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR); |
---|
| 499 | } |
---|
| 500 | |
---|
| 501 | |
---|
[3178] | 502 | #ifdef HAVE_SDL_SDL_IMAGE_H |
---|
[3140] | 503 | bool Material::loadImage(char* imageName, GLuint* texture) |
---|
| 504 | { |
---|
| 505 | char* imgNameWithPath = searchTextureInPaths(imageName); |
---|
| 506 | if (imgNameWithPath) |
---|
| 507 | { |
---|
| 508 | SDL_Surface* map; |
---|
| 509 | Image* pImage = new Image; |
---|
| 510 | map=IMG_Load(imgNameWithPath); |
---|
| 511 | if(!map) |
---|
| 512 | { |
---|
[3206] | 513 | PRINTF(1)("IMG_Load: %s\n", IMG_GetError()); |
---|
[3140] | 514 | return false; |
---|
| 515 | } |
---|
| 516 | pImage->height = map->h; |
---|
| 517 | pImage->width = map->w; |
---|
| 518 | pImage->data = (GLubyte*)map->pixels; |
---|
| 519 | if( !IMG_isPNG(SDL_RWFromFile(imgNameWithPath, "rb")) && !IMG_isJPG(SDL_RWFromFile(imgNameWithPath, "rb"))) |
---|
| 520 | for (int i=0;i<map->h * map->w *3;i+=3) |
---|
| 521 | { |
---|
| 522 | GLuint temp = pImage->data[i]; |
---|
| 523 | pImage->data[i] = pImage->data[i+2]; |
---|
| 524 | pImage->data[i+2] = temp; |
---|
| 525 | } |
---|
[3195] | 526 | this->loadTexToGL (pImage, texture); |
---|
[3140] | 527 | } |
---|
| 528 | else |
---|
| 529 | { |
---|
[3206] | 530 | PRINTF(1)("Image not Found: %s\n", imgNameWithPath); |
---|
[3140] | 531 | return false; |
---|
| 532 | } |
---|
| 533 | } |
---|
| 534 | |
---|
| 535 | |
---|
[3178] | 536 | #else /* HAVE_SDL_SDL_IMAGE_H */ |
---|
[3070] | 537 | /** |
---|
[3140] | 538 | \brief Makes the Programm ready to Read-in a texture-File |
---|
| 539 | 1. Checks what type of Image should be imported |
---|
| 540 | 2. ToDO: Checks where to find the Image |
---|
| 541 | */ |
---|
| 542 | bool Material::loadImage(char* imageName, GLuint* texture) |
---|
| 543 | { |
---|
| 544 | char* imgNameWithPath = searchTextureInPaths(imageName); |
---|
| 545 | if (imgNameWithPath) |
---|
| 546 | { |
---|
| 547 | if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".bmp", 4)) |
---|
| 548 | { |
---|
[3206] | 549 | PRINTF(3)("Requested bmp-image. Trying to Import.\n"); |
---|
[3195] | 550 | return this->loadBMP(imgNameWithPath, texture); |
---|
[3140] | 551 | } |
---|
| 552 | |
---|
| 553 | else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".jpg", 4) || !strncmp(imgNameWithPath+strlen(imgNameWithPath)-5, ".jpg", 5)) |
---|
| 554 | { |
---|
[3206] | 555 | PRINTF(3)("Requested jpeg-image. Trying to Import\n"); |
---|
[3195] | 556 | return this->loadJPG(imgNameWithPath, texture); |
---|
[3140] | 557 | } |
---|
| 558 | else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".tga", 4)) |
---|
| 559 | { |
---|
[3206] | 560 | PRINTF(3)("Requested tga-image. Trying to Import\n"); |
---|
[3195] | 561 | return this->loadTGA(imgNameWithPath, texture); |
---|
[3140] | 562 | } |
---|
| 563 | else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".png", 4)) |
---|
| 564 | { |
---|
[3206] | 565 | PRINTF(3)("Requested png-image. Trying to Import\n"); |
---|
[3195] | 566 | return this->loadPNG(imgNameWithPath, texture); |
---|
[3140] | 567 | } |
---|
| 568 | else |
---|
| 569 | { |
---|
[3206] | 570 | PRINTF(1)("Requested Image was not recognized in its type. (Maybe a type-Cast-error.)\n FileName: %s", imgNameWithPath); |
---|
[3140] | 571 | return false; |
---|
| 572 | } |
---|
| 573 | } |
---|
| 574 | else |
---|
| 575 | { |
---|
[3206] | 576 | PRINTF(1)("Image not Found: %s\n", imgNameWithPath); |
---|
[3140] | 577 | return false; |
---|
| 578 | } |
---|
| 579 | } |
---|
| 580 | |
---|
| 581 | /** |
---|
[3070] | 582 | \brief reads in a Windows BMP-file, and imports it to openGL. |
---|
| 583 | \param bmpName The name of the Image to load. |
---|
| 584 | \param texture A pointer to the Texture which should be read to. |
---|
| 585 | */ |
---|
| 586 | bool Material::loadBMP (char* bmpName, GLuint* texture) |
---|
| 587 | { |
---|
[3140] | 588 | Image* pImage = new Image; |
---|
| 589 | FILE *file; |
---|
| 590 | unsigned long size; // size of the image in bytes. |
---|
| 591 | unsigned long i; // standard counter. |
---|
| 592 | unsigned short int planes; // number of planes in image (must be 1) |
---|
| 593 | unsigned short int bpp; // number of bits per pixel (must be 24) |
---|
| 594 | GLuint temp; // temporary color storage for bgr-rgb conversion. |
---|
| 595 | |
---|
| 596 | // make sure the file is there. |
---|
| 597 | if ((file = fopen(bmpName, "rb"))==NULL) |
---|
[3070] | 598 | { |
---|
[3206] | 599 | PRINTF(1)("File Not Found : %s\n",bmpName); |
---|
[3140] | 600 | return false; |
---|
| 601 | } |
---|
| 602 | // seek through the bmp header, up to the width/height: |
---|
| 603 | fseek(file, 18, SEEK_CUR); |
---|
| 604 | |
---|
| 605 | // read the width |
---|
| 606 | if ((i = fread(&pImage->width, 4, 1, file)) != 1) |
---|
| 607 | { |
---|
[3206] | 608 | PRINTF(1)("Error reading width from %s.\n", bmpName); |
---|
[3140] | 609 | return false; |
---|
| 610 | } |
---|
| 611 | // read the height |
---|
| 612 | if ((i = fread(&pImage->height, 4, 1, file)) != 1) |
---|
| 613 | { |
---|
[3206] | 614 | PRINTF(1)("Error reading height from %s.\n", bmpName); |
---|
[3140] | 615 | return false; |
---|
| 616 | } |
---|
| 617 | |
---|
| 618 | // calculate the size (assuming 24 bits or 3 bytes per pixel). |
---|
| 619 | size = pImage->width * pImage->height * 3; |
---|
| 620 | |
---|
| 621 | // read the planes |
---|
| 622 | if ((fread(&planes, 2, 1, file)) != 1) |
---|
| 623 | { |
---|
[3206] | 624 | PRINTF(1)("Error reading planes from %s.\n", bmpName); |
---|
[3140] | 625 | return false; |
---|
| 626 | } |
---|
| 627 | if (planes != 1) |
---|
| 628 | { |
---|
[3206] | 629 | PRINTF(1)("Planes from %s is not 1: %u\n", bmpName, planes); |
---|
[3140] | 630 | return false; |
---|
| 631 | } |
---|
| 632 | |
---|
| 633 | // read the bpp |
---|
| 634 | if ((i = fread(&bpp, 2, 1, file)) != 1) |
---|
| 635 | { |
---|
[3206] | 636 | PRINTF(1)("Error reading bpp from %s.\n", bmpName); |
---|
[3140] | 637 | return false; |
---|
| 638 | } |
---|
| 639 | if (bpp != 24) |
---|
| 640 | { |
---|
[3206] | 641 | PRINTF(1)("Bpp from %s is not 24: %u\n", bmpName, bpp); |
---|
[3140] | 642 | return false; |
---|
| 643 | } |
---|
| 644 | |
---|
| 645 | // seek past the rest of the bitmap header. |
---|
| 646 | fseek(file, 24, SEEK_CUR); |
---|
| 647 | |
---|
| 648 | // read the data. |
---|
| 649 | pImage->data = (GLubyte *) malloc(size); |
---|
| 650 | if (pImage->data == NULL) |
---|
| 651 | { |
---|
[3206] | 652 | PRINTF(1)("Error allocating memory for color-corrected image data"); |
---|
[3140] | 653 | return false; |
---|
| 654 | } |
---|
| 655 | |
---|
| 656 | if ((i = fread(pImage->data, size, 1, file)) != 1) |
---|
| 657 | { |
---|
[3206] | 658 | PRINTF(1)("Error reading image data from %s.\n", bmpName); |
---|
[3140] | 659 | return false; |
---|
| 660 | } |
---|
| 661 | fclose(file); |
---|
[3070] | 662 | |
---|
[3140] | 663 | // reverse all of the colors. (bgr -> rgb) |
---|
| 664 | for (i=0;i<size;i+=3) |
---|
| 665 | { |
---|
| 666 | temp = pImage->data[i]; |
---|
| 667 | pImage->data[i] = pImage->data[i+2]; |
---|
| 668 | pImage->data[i+2] = temp; |
---|
| 669 | } |
---|
[3195] | 670 | this->loadTexToGL (pImage, texture); |
---|
[3140] | 671 | |
---|
| 672 | |
---|
| 673 | if (pImage) |
---|
| 674 | { |
---|
| 675 | if (pImage->data) |
---|
| 676 | { |
---|
| 677 | free(pImage->data); |
---|
| 678 | } |
---|
[3070] | 679 | |
---|
[3140] | 680 | free(pImage); |
---|
| 681 | } |
---|
[3195] | 682 | return true; |
---|
[3070] | 683 | |
---|
| 684 | } |
---|
| 685 | |
---|
[3140] | 686 | /** |
---|
| 687 | \brief reads in a jpg-file |
---|
| 688 | \param jpgName the Name of the Image to load |
---|
| 689 | \param texture a reference to the Texture to write the image to |
---|
| 690 | */ |
---|
| 691 | bool Material::loadJPG (char* jpgName, GLuint* texture) |
---|
| 692 | { |
---|
| 693 | #ifdef HAVE_JPEGLIB_H |
---|
| 694 | struct jpeg_decompress_struct cinfo; |
---|
| 695 | Image *pImage = NULL; |
---|
| 696 | FILE *pFile; |
---|
| 697 | |
---|
| 698 | // Open a file pointer to the jpeg file and check if it was found and opened |
---|
| 699 | if((pFile = fopen(jpgName, "rb")) == NULL) |
---|
| 700 | { |
---|
| 701 | // Display an error message saying the file was not found, then return NULL |
---|
[3206] | 702 | PRINTF(1)("Unable to load JPG File %s.\n", jpgName); |
---|
[3140] | 703 | return false; |
---|
| 704 | } |
---|
| 705 | |
---|
| 706 | // Create an error handler |
---|
| 707 | jpeg_error_mgr jerr; |
---|
| 708 | |
---|
| 709 | // Have our compression info object point to the error handler address |
---|
| 710 | cinfo.err = jpeg_std_error(&jerr); |
---|
| 711 | |
---|
| 712 | // Initialize the decompression object |
---|
| 713 | jpeg_create_decompress(&cinfo); |
---|
| 714 | |
---|
| 715 | // Specify the data source (Our file pointer) |
---|
| 716 | jpeg_stdio_src(&cinfo, pFile); |
---|
| 717 | |
---|
| 718 | // Allocate the structure that will hold our eventual jpeg data (must free it!) |
---|
| 719 | pImage = (Image*)malloc(sizeof(Image)); |
---|
| 720 | |
---|
| 721 | // DECOFING |
---|
| 722 | // Read in the header of the jpeg file |
---|
| 723 | jpeg_read_header(&cinfo, TRUE); |
---|
| 724 | |
---|
| 725 | // Start to decompress the jpeg file with our compression info |
---|
| 726 | jpeg_start_decompress(&cinfo); |
---|
| 727 | |
---|
| 728 | // Get the image dimensions and row span to read in the pixel data |
---|
| 729 | pImage->rowSpan = cinfo.image_width * cinfo.num_components; |
---|
| 730 | pImage->width = cinfo.image_width; |
---|
| 731 | pImage->height = cinfo.image_height; |
---|
| 732 | |
---|
| 733 | // Allocate memory for the pixel buffer |
---|
| 734 | pImage->data = new unsigned char[pImage->rowSpan * pImage->height]; |
---|
| 735 | |
---|
| 736 | // Here we use the library's state variable cinfo.output_scanline as the |
---|
| 737 | // loop counter, so that we don't have to keep track ourselves. |
---|
| 738 | |
---|
| 739 | // Create an array of row pointers |
---|
| 740 | unsigned char** rowPtr = new unsigned char*[pImage->height]; |
---|
| 741 | for (int i = 0; i < pImage->height; i++) |
---|
| 742 | rowPtr[i] = &(pImage->data[i*pImage->rowSpan]); |
---|
| 743 | |
---|
| 744 | // Now comes the juice of our work, here we extract all the pixel data |
---|
| 745 | int rowsRead = 0; |
---|
| 746 | while (cinfo.output_scanline < cinfo.output_height) |
---|
| 747 | { |
---|
| 748 | // Read in the current row of pixels and increase the rowsRead count |
---|
| 749 | rowsRead += jpeg_read_scanlines(&cinfo, &rowPtr[rowsRead], cinfo.output_height - rowsRead); |
---|
| 750 | } |
---|
| 751 | |
---|
| 752 | // Delete the temporary row pointers |
---|
| 753 | delete [] rowPtr; |
---|
| 754 | |
---|
| 755 | // Finish decompressing the data |
---|
| 756 | jpeg_finish_decompress(&cinfo);// decodeJPG(&cinfo, pImage); |
---|
| 757 | |
---|
| 758 | // This releases all the stored memory for reading and decoding the jpeg |
---|
| 759 | jpeg_destroy_decompress(&cinfo); |
---|
| 760 | |
---|
| 761 | // Close the file pointer that opened the file |
---|
| 762 | fclose(pFile); |
---|
| 763 | |
---|
[3070] | 764 | |
---|
[3140] | 765 | if(pImage == NULL) |
---|
| 766 | exit(0); |
---|
| 767 | |
---|
[3195] | 768 | this->loadTexToGL (pImage, texture); |
---|
[3140] | 769 | if (pImage) |
---|
| 770 | { |
---|
| 771 | if (pImage->data) |
---|
| 772 | { |
---|
| 773 | free(pImage->data); |
---|
| 774 | } |
---|
| 775 | |
---|
| 776 | free(pImage); |
---|
| 777 | } |
---|
| 778 | return true; |
---|
| 779 | #else /* HAVE_JPEGLIB_H */ |
---|
[3206] | 780 | PRINTF(1)("sorry, but you did not compile with jpeg-support.\nEither install SDL_image or jpeglib, and recompile to see the image\n"); |
---|
[3140] | 781 | return false; |
---|
| 782 | #endif /* HAVE_JPEGLIB_H */ |
---|
[3070] | 783 | |
---|
[3140] | 784 | } |
---|
[3070] | 785 | |
---|
| 786 | /** |
---|
[3140] | 787 | \brief reads in a tga-file |
---|
| 788 | \param tgaName the Name of the Image to load |
---|
| 789 | \param texture a reference to the Texture to write the image to |
---|
[2842] | 790 | */ |
---|
[3140] | 791 | bool Material::loadTGA(const char * tgaName, GLuint* texture) |
---|
[2778] | 792 | { |
---|
[3140] | 793 | typedef struct |
---|
| 794 | { |
---|
| 795 | GLubyte Header[12]; |
---|
| 796 | } TGAHeader; |
---|
| 797 | TGAHeader tgaHeader; |
---|
| 798 | |
---|
| 799 | GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header |
---|
| 800 | GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; // Compressed TGA Header |
---|
| 801 | FILE * fTGA; |
---|
| 802 | fTGA = fopen(tgaName, "rb"); |
---|
| 803 | |
---|
| 804 | if(fTGA == NULL) |
---|
[2778] | 805 | { |
---|
[3206] | 806 | PRINTF(1)("Error could not open texture file: %s\n", tgaName); |
---|
[3140] | 807 | return false; |
---|
| 808 | } |
---|
| 809 | |
---|
| 810 | if(fread(&tgaHeader, sizeof(TGAHeader), 1, fTGA) == 0) |
---|
| 811 | { |
---|
[3206] | 812 | PRINTF(1)("Error could not read file header of %s\n", tgaName); |
---|
[3140] | 813 | if(fTGA != NULL) |
---|
[2804] | 814 | { |
---|
[3140] | 815 | fclose(fTGA); |
---|
[2804] | 816 | } |
---|
[3140] | 817 | return false; |
---|
[2778] | 818 | } |
---|
[3140] | 819 | |
---|
| 820 | if(memcmp(uTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0) |
---|
| 821 | { |
---|
| 822 | loadUncompressedTGA(tgaName, fTGA, texture); |
---|
| 823 | if (fTGA) |
---|
| 824 | fclose (fTGA); |
---|
| 825 | } |
---|
| 826 | else if(memcmp(cTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0) |
---|
| 827 | { |
---|
| 828 | loadCompressedTGA(tgaName, fTGA, texture); |
---|
| 829 | if (fTGA) |
---|
| 830 | fclose (fTGA); |
---|
| 831 | } |
---|
| 832 | else |
---|
| 833 | { |
---|
[3206] | 834 | PRINTF(1)("Error TGA file be type 2 or type 10\n"); |
---|
[3140] | 835 | if (fTGA) |
---|
| 836 | fclose(fTGA); |
---|
| 837 | return false; |
---|
| 838 | } |
---|
| 839 | return true; |
---|
[2778] | 840 | } |
---|
| 841 | |
---|
[2842] | 842 | /** |
---|
[3140] | 843 | \brief reads in an uncompressed tga-file |
---|
| 844 | \param filename the Name of the Image to load |
---|
| 845 | \param fTGA a Pointer to a File, that should be read |
---|
| 846 | \param texture a reference to the Texture to write the image to |
---|
[2842] | 847 | */ |
---|
[3140] | 848 | bool Material::loadUncompressedTGA(const char * filename, FILE * fTGA, GLuint* texture) |
---|
[2778] | 849 | { |
---|
[3140] | 850 | GLubyte header[6]; // First 6 Useful Bytes From The Header |
---|
| 851 | GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File |
---|
| 852 | GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram |
---|
| 853 | GLuint temp; // Temporary Variable |
---|
| 854 | GLuint type; |
---|
| 855 | GLuint Height; // Height of Image |
---|
| 856 | GLuint Width; // Width of Image |
---|
| 857 | GLuint Bpp; // Bits Per Pixel |
---|
[2780] | 858 | |
---|
[3140] | 859 | Image* pImage = new Image; |
---|
| 860 | GLuint cswap; |
---|
| 861 | if(fread(header, sizeof(header), 1, fTGA) == 0) |
---|
| 862 | { |
---|
[3206] | 863 | PRINTF(1)("Error could not read info header\n"); |
---|
[3140] | 864 | return false; |
---|
| 865 | } |
---|
| 866 | |
---|
| 867 | Width = pImage->width = header[1] * 256 + header[0]; |
---|
| 868 | Height = pImage->height = header[3] * 256 + header[2]; |
---|
| 869 | Bpp = pImage->bpp = header[4]; |
---|
| 870 | // Make sure all information is valid |
---|
| 871 | if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32))) |
---|
| 872 | { |
---|
[3206] | 873 | PRINTF(1)("Error invalid texture information\n"); |
---|
[3140] | 874 | return false; |
---|
| 875 | } |
---|
| 876 | |
---|
| 877 | if(pImage->bpp == 24) |
---|
| 878 | { |
---|
| 879 | pImage->type = GL_RGB; |
---|
| 880 | } |
---|
| 881 | else |
---|
| 882 | { |
---|
| 883 | pImage->type = GL_RGBA; |
---|
| 884 | } |
---|
| 885 | |
---|
| 886 | bytesPerPixel = (Bpp / 8); |
---|
| 887 | imageSize = (bytesPerPixel * Width * Height); |
---|
| 888 | pImage->data = (GLubyte*) malloc(imageSize); |
---|
| 889 | |
---|
| 890 | if(pImage->data == NULL) |
---|
| 891 | { |
---|
[3206] | 892 | PRINTF(1)("Error could not allocate memory for image\n"); |
---|
[3140] | 893 | return false; |
---|
| 894 | } |
---|
| 895 | |
---|
| 896 | if(fread(pImage->data, 1, imageSize, fTGA) != imageSize) |
---|
| 897 | { |
---|
[3206] | 898 | PRINTF(1)("Error could not read image data\n"); |
---|
[3140] | 899 | if(pImage->data != NULL) |
---|
| 900 | { |
---|
| 901 | free(pImage->data); |
---|
| 902 | } |
---|
| 903 | return false; |
---|
| 904 | } |
---|
| 905 | |
---|
| 906 | for(cswap = 0; cswap < (int)imageSize; cswap += bytesPerPixel) |
---|
| 907 | { |
---|
| 908 | pImage->data[cswap] ^= pImage->data[cswap+2] ^= |
---|
| 909 | pImage->data[cswap] ^= pImage->data[cswap+2]; |
---|
| 910 | } |
---|
| 911 | |
---|
[3195] | 912 | this->loadTexToGL (pImage, texture); |
---|
[2780] | 913 | |
---|
[3140] | 914 | return true; |
---|
| 915 | } |
---|
[2836] | 916 | |
---|
[3140] | 917 | /** |
---|
| 918 | \brief reads in a compressed tga-file |
---|
| 919 | \param filename the Name of the Image to load |
---|
| 920 | \param fTGA a Pointer to a File, that should be read |
---|
| 921 | \param texture a reference to the Texture to write the image to |
---|
| 922 | */ |
---|
| 923 | bool Material::loadCompressedTGA(const char * filename, FILE * fTGA, GLuint* texture) |
---|
| 924 | { |
---|
| 925 | GLubyte header[6]; // First 6 Useful Bytes From The Header |
---|
| 926 | GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File |
---|
| 927 | GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram |
---|
| 928 | GLuint temp; // Temporary Variable |
---|
| 929 | GLuint type; |
---|
| 930 | GLuint Height; // Height of Image |
---|
| 931 | GLuint Width; // Width of Image |
---|
| 932 | GLuint Bpp; // Bits Per Pixel |
---|
| 933 | |
---|
| 934 | Image* pImage = new Image; |
---|
| 935 | |
---|
[2780] | 936 | |
---|
[3140] | 937 | if(fread(header, sizeof(header), 1, fTGA) == 0) |
---|
| 938 | { |
---|
[3206] | 939 | PRINTF(1)("Error could not read info header\n"); |
---|
[3140] | 940 | return false; |
---|
| 941 | } |
---|
| 942 | |
---|
| 943 | Width = pImage->width = header[1] * 256 + header[0]; |
---|
| 944 | Height = pImage->height = header[3] * 256 + header[2]; |
---|
| 945 | Bpp = pImage->bpp = header[4]; |
---|
[3070] | 946 | |
---|
[3140] | 947 | GLuint pixelcount = Height * Width; |
---|
| 948 | GLuint currentpixel = 0; |
---|
| 949 | GLuint currentbyte = 0; |
---|
| 950 | GLubyte * colorbuffer = (GLubyte *)malloc(bytesPerPixel); |
---|
| 951 | |
---|
| 952 | //Make sure all pImage info is ok |
---|
| 953 | if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32))) |
---|
| 954 | { |
---|
[3206] | 955 | PRINTF(1)("Error Invalid pImage information\n"); |
---|
[3140] | 956 | return false; |
---|
| 957 | } |
---|
[3070] | 958 | |
---|
[3140] | 959 | bytesPerPixel = (Bpp / 8); |
---|
| 960 | imageSize = (bytesPerPixel * Width * Height); |
---|
| 961 | pImage->data = (GLubyte*) malloc(imageSize); |
---|
| 962 | |
---|
| 963 | if(pImage->data == NULL) |
---|
| 964 | { |
---|
[3206] | 965 | PRINTF(1)("Error could not allocate memory for image\n"); |
---|
[3140] | 966 | return false; |
---|
| 967 | } |
---|
| 968 | |
---|
| 969 | do |
---|
| 970 | { |
---|
| 971 | GLubyte chunkheader = 0; |
---|
| 972 | |
---|
| 973 | if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0) |
---|
| 974 | { |
---|
[3206] | 975 | PRINTF(1)("Error could not read RLE header\n"); |
---|
[3140] | 976 | if(pImage->data != NULL) |
---|
| 977 | { |
---|
| 978 | free(pImage->data); |
---|
| 979 | } |
---|
| 980 | return false; |
---|
| 981 | } |
---|
| 982 | // If the ehader is < 128, it means the that is the number of RAW color packets minus 1 |
---|
| 983 | if(chunkheader < 128) |
---|
| 984 | { |
---|
| 985 | short counter; |
---|
| 986 | chunkheader++; |
---|
| 987 | // Read RAW color values |
---|
| 988 | for(counter = 0; counter < chunkheader; counter++) |
---|
| 989 | { |
---|
| 990 | // Try to read 1 pixel |
---|
| 991 | if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel) |
---|
| 992 | { |
---|
[3206] | 993 | PRINTF(1)("Error could not read image data\n"); |
---|
[3140] | 994 | if(colorbuffer != NULL) |
---|
| 995 | { |
---|
| 996 | free(colorbuffer); |
---|
| 997 | } |
---|
| 998 | |
---|
| 999 | if(pImage->data != NULL) |
---|
| 1000 | { |
---|
| 1001 | free(pImage->data); |
---|
| 1002 | } |
---|
| 1003 | |
---|
| 1004 | return false; |
---|
| 1005 | } |
---|
| 1006 | // write to memory |
---|
| 1007 | // Flip R and B vcolor values around in the process |
---|
| 1008 | pImage->data[currentbyte ] = colorbuffer[2]; |
---|
| 1009 | pImage->data[currentbyte + 1] = colorbuffer[1]; |
---|
| 1010 | pImage->data[currentbyte + 2] = colorbuffer[0]; |
---|
| 1011 | |
---|
| 1012 | if(bytesPerPixel == 4) // if its a 32 bpp image |
---|
| 1013 | { |
---|
| 1014 | pImage->data[currentbyte + 3] = colorbuffer[3];// copy the 4th byte |
---|
| 1015 | } |
---|
| 1016 | |
---|
| 1017 | currentbyte += bytesPerPixel; |
---|
| 1018 | currentpixel++; |
---|
| 1019 | |
---|
| 1020 | // Make sure we haven't read too many pixels |
---|
| 1021 | if(currentpixel > pixelcount) |
---|
| 1022 | { |
---|
[3206] | 1023 | PRINTF(1)("Error too many pixels read\n"); |
---|
[3140] | 1024 | if(colorbuffer != NULL) |
---|
| 1025 | { |
---|
| 1026 | free(colorbuffer); |
---|
| 1027 | } |
---|
| 1028 | |
---|
| 1029 | if(pImage->data != NULL) |
---|
| 1030 | { |
---|
| 1031 | free(pImage->data); |
---|
| 1032 | } |
---|
| 1033 | |
---|
| 1034 | return false; |
---|
| 1035 | } |
---|
| 1036 | } |
---|
| 1037 | } |
---|
| 1038 | // chunkheader > 128 RLE data, next color reapeated chunkheader - 127 times |
---|
| 1039 | else |
---|
| 1040 | { |
---|
| 1041 | short counter; |
---|
| 1042 | chunkheader -= 127; // Subteact 127 to get rid of the ID bit |
---|
| 1043 | if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel) // Attempt to read following color values |
---|
| 1044 | { |
---|
[3206] | 1045 | PRINTF(1)("Error could not read from file"); |
---|
[3140] | 1046 | if(colorbuffer != NULL) |
---|
| 1047 | { |
---|
| 1048 | free(colorbuffer); |
---|
| 1049 | } |
---|
| 1050 | |
---|
| 1051 | if(pImage->data != NULL) |
---|
| 1052 | { |
---|
| 1053 | free(pImage->data); |
---|
| 1054 | } |
---|
| 1055 | |
---|
| 1056 | return false; |
---|
| 1057 | } |
---|
| 1058 | |
---|
| 1059 | for(counter = 0; counter < chunkheader; counter++) //copy the color into the image data as many times as dictated |
---|
| 1060 | { |
---|
| 1061 | // switch R and B bytes areound while copying |
---|
| 1062 | pImage->data[currentbyte ] = colorbuffer[2]; |
---|
| 1063 | pImage->data[currentbyte + 1] = colorbuffer[1]; |
---|
| 1064 | pImage->data[currentbyte + 2] = colorbuffer[0]; |
---|
| 1065 | |
---|
| 1066 | if(bytesPerPixel == 4) |
---|
| 1067 | { |
---|
| 1068 | pImage->data[currentbyte + 3] = colorbuffer[3]; |
---|
| 1069 | } |
---|
| 1070 | |
---|
| 1071 | currentbyte += bytesPerPixel; |
---|
| 1072 | currentpixel++; |
---|
| 1073 | |
---|
| 1074 | if(currentpixel > pixelcount) |
---|
| 1075 | { |
---|
[3206] | 1076 | PRINTF(1)("Error too many pixels read\n"); |
---|
[3140] | 1077 | if(colorbuffer != NULL) |
---|
| 1078 | { |
---|
| 1079 | free(colorbuffer); |
---|
| 1080 | } |
---|
| 1081 | |
---|
| 1082 | if(pImage->data != NULL) |
---|
| 1083 | { |
---|
| 1084 | free(pImage->data); |
---|
| 1085 | } |
---|
| 1086 | |
---|
| 1087 | return false; |
---|
| 1088 | } |
---|
| 1089 | } |
---|
| 1090 | } |
---|
| 1091 | } |
---|
| 1092 | |
---|
| 1093 | while(currentpixel < pixelcount); // Loop while there are still pixels left |
---|
| 1094 | |
---|
[3195] | 1095 | this->loadTexToGL (pImage, texture); |
---|
[3140] | 1096 | |
---|
| 1097 | return true; |
---|
[2778] | 1098 | } |
---|
[3140] | 1099 | |
---|
| 1100 | |
---|
| 1101 | /* |
---|
| 1102 | static int ST_is_power_of_two(unsigned int number) |
---|
| 1103 | { |
---|
| 1104 | return (number & (number - 1)) == 0; |
---|
| 1105 | } |
---|
| 1106 | */ |
---|
| 1107 | |
---|
| 1108 | /** |
---|
| 1109 | \brief reads in a png-file |
---|
| 1110 | \param pngName the Name of the Image to load |
---|
| 1111 | \param texture a reference to the Texture to write the image to |
---|
| 1112 | */ |
---|
| 1113 | bool Material::loadPNG(const char* pngName, GLuint* texture) |
---|
| 1114 | { |
---|
| 1115 | #ifdef HAVE_PNG_H |
---|
| 1116 | Image* pImage = new Image; |
---|
| 1117 | |
---|
| 1118 | FILE *PNG_file = fopen(pngName, "rb"); |
---|
| 1119 | if (PNG_file == NULL) |
---|
| 1120 | { |
---|
| 1121 | return 0; |
---|
| 1122 | } |
---|
| 1123 | |
---|
| 1124 | GLubyte PNG_header[8]; |
---|
| 1125 | |
---|
| 1126 | fread(PNG_header, 1, 8, PNG_file); |
---|
| 1127 | if (png_sig_cmp(PNG_header, 0, 8) != 0) |
---|
| 1128 | { |
---|
[3206] | 1129 | PRINTF(2)("Not Recognized as a pngFile\n"); |
---|
[3140] | 1130 | fclose (PNG_file); |
---|
| 1131 | return 0; |
---|
| 1132 | } |
---|
| 1133 | |
---|
| 1134 | png_structp PNG_reader = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL); |
---|
| 1135 | if (PNG_reader == NULL) |
---|
| 1136 | { |
---|
| 1137 | fclose(PNG_file); |
---|
| 1138 | return 0; |
---|
| 1139 | } |
---|
| 1140 | |
---|
| 1141 | png_infop PNG_info = png_create_info_struct(PNG_reader); |
---|
| 1142 | if (PNG_info == NULL) |
---|
| 1143 | { |
---|
| 1144 | png_destroy_read_struct(&PNG_reader, NULL, NULL); |
---|
| 1145 | fclose(PNG_file); |
---|
| 1146 | return 0; |
---|
| 1147 | } |
---|
| 1148 | |
---|
| 1149 | png_infop PNG_end_info = png_create_info_struct(PNG_reader); |
---|
| 1150 | if (PNG_end_info == NULL) |
---|
| 1151 | { |
---|
| 1152 | png_destroy_read_struct(&PNG_reader, &PNG_info, NULL); |
---|
| 1153 | fclose(PNG_file); |
---|
| 1154 | return 0; |
---|
| 1155 | } |
---|
| 1156 | |
---|
| 1157 | if (setjmp(png_jmpbuf(PNG_reader))) |
---|
| 1158 | { |
---|
| 1159 | png_destroy_read_struct(&PNG_reader, &PNG_info, &PNG_end_info); |
---|
| 1160 | fclose(PNG_file); |
---|
| 1161 | return (0); |
---|
| 1162 | } |
---|
| 1163 | |
---|
| 1164 | png_init_io(PNG_reader, PNG_file); |
---|
| 1165 | png_set_sig_bytes(PNG_reader, 8); |
---|
| 1166 | |
---|
| 1167 | png_read_info(PNG_reader, PNG_info); |
---|
| 1168 | |
---|
| 1169 | pImage->width = png_get_image_width(PNG_reader, PNG_info); |
---|
| 1170 | pImage->height = png_get_image_height(PNG_reader, PNG_info); |
---|
| 1171 | |
---|
| 1172 | png_uint_32 bit_depth, color_type; |
---|
| 1173 | bit_depth = png_get_bit_depth(PNG_reader, PNG_info); |
---|
| 1174 | color_type = png_get_color_type(PNG_reader, PNG_info); |
---|
| 1175 | |
---|
| 1176 | if (color_type == PNG_COLOR_TYPE_PALETTE) |
---|
| 1177 | { |
---|
| 1178 | png_set_palette_to_rgb(PNG_reader); |
---|
| 1179 | } |
---|
| 1180 | |
---|
| 1181 | if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8) |
---|
| 1182 | { |
---|
| 1183 | png_set_gray_1_2_4_to_8(PNG_reader); |
---|
| 1184 | } |
---|
| 1185 | |
---|
| 1186 | if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA) |
---|
| 1187 | { |
---|
| 1188 | png_set_gray_to_rgb(PNG_reader); |
---|
| 1189 | } |
---|
| 1190 | |
---|
| 1191 | if (png_get_valid(PNG_reader, PNG_info, PNG_INFO_tRNS)) |
---|
| 1192 | { |
---|
| 1193 | png_set_tRNS_to_alpha(PNG_reader); |
---|
| 1194 | } |
---|
| 1195 | else |
---|
| 1196 | { |
---|
| 1197 | png_set_filler(PNG_reader, 0xff, PNG_FILLER_AFTER); |
---|
| 1198 | } |
---|
| 1199 | |
---|
| 1200 | if (bit_depth == 16) |
---|
| 1201 | { |
---|
| 1202 | png_set_strip_16(PNG_reader); |
---|
| 1203 | } |
---|
| 1204 | |
---|
| 1205 | png_read_update_info(PNG_reader, PNG_info); |
---|
| 1206 | |
---|
| 1207 | pImage->data = (png_byte*)malloc(4 * pImage->width * pImage->height); |
---|
| 1208 | png_byte** PNG_rows = (png_byte**)malloc(pImage->height * sizeof(png_byte*)); |
---|
| 1209 | |
---|
| 1210 | unsigned int row; |
---|
| 1211 | for (row = 0; row < pImage->height; ++row) |
---|
| 1212 | { |
---|
| 1213 | PNG_rows[pImage->height - 1 - row] = pImage->data + (row * 4 * pImage->width); |
---|
| 1214 | } |
---|
| 1215 | |
---|
| 1216 | png_read_image(PNG_reader, PNG_rows); |
---|
| 1217 | |
---|
| 1218 | free(PNG_rows); |
---|
| 1219 | |
---|
| 1220 | png_destroy_read_struct(&PNG_reader, &PNG_info, &PNG_end_info); |
---|
| 1221 | fclose(PNG_file); |
---|
| 1222 | |
---|
| 1223 | /* if (!ST_is_power_of_two(pImage->width) || !ST_is_power_of_two(pImage->height)) |
---|
| 1224 | { |
---|
| 1225 | free(pImage->data); |
---|
| 1226 | return 0; |
---|
| 1227 | } |
---|
| 1228 | */ |
---|
[3195] | 1229 | this->loadTexToGL (pImage, texture); |
---|
[3140] | 1230 | |
---|
| 1231 | free(pImage->data); |
---|
| 1232 | |
---|
| 1233 | return true; |
---|
| 1234 | #else /* HAVE_PNG_H */ |
---|
[3206] | 1235 | PRINTF(1)("sorry, but you did not compile with png-support.\nEither install SDL_image or libpng, and recompile to see the image\n"); |
---|
[3140] | 1236 | return false; |
---|
| 1237 | #endif /* HAVE_PNG_H */ |
---|
| 1238 | |
---|
| 1239 | } |
---|
| 1240 | |
---|
[3178] | 1241 | #endif /* HAVE_SDL_SDL_IMAGE_H */ |
---|