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