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