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