Changeset 3399 in orxonox.OLD for orxonox/branches/nico/src/importer
- Timestamp:
- Feb 6, 2005, 11:27:25 PM (20 years ago)
- Location:
- orxonox/branches/nico
- Files:
-
- 2 deleted
- 7 edited
- 6 copied
Legend:
- Unmodified
- Added
- Removed
-
orxonox/branches/nico
- Property svn:externals set to
-
orxonox/branches/nico/src/importer/Makefile.am
r3390 r3399 8 8 importer_SOURCES= framework.cc \ 9 9 windowHandler.cc \ 10 object.cc \ 10 model.cc \ 11 objModel.cc \ 11 12 array.cc \ 12 13 material.cc \ 13 vector.cc 14 texture.cc \ 15 vector.cc 14 16 15 17 heightmap_SOURCES= main.cc \ … … 21 23 noinst_HEADERS= framework.h \ 22 24 windowHandler.h \ 23 object.h \ 25 model.h \ 26 objModel.h \ 24 27 array.h \ 25 material.h 28 material.h \ 29 texture.h 26 30 27 31 -
orxonox/branches/nico/src/importer/Makefile.in
r3391 r3399 42 42 subdir = src/importer 43 43 DIST_COMMON = $(noinst_HEADERS) $(srcdir)/Makefile.am \ 44 $(srcdir)/Makefile.in TODO44 $(srcdir)/Makefile.in 45 45 ACLOCAL_M4 = $(top_srcdir)/aclocal.m4 46 46 am__aclocal_m4_deps = $(top_srcdir)/configure.ac … … 59 59 heightmap_LDADD = $(LDADD) 60 60 am_importer_OBJECTS = framework.$(OBJEXT) windowHandler.$(OBJEXT) \ 61 object.$(OBJEXT) array.$(OBJEXT) material.$(OBJEXT) \62 vector.$(OBJEXT)61 model.$(OBJEXT) objModel.$(OBJEXT) array.$(OBJEXT) \ 62 material.$(OBJEXT) texture.$(OBJEXT) vector.$(OBJEXT) 63 63 importer_OBJECTS = $(am_importer_OBJECTS) 64 64 importer_LDADD = $(LDADD) … … 69 69 @AMDEP_TRUE@ ./$(DEPDIR)/heightMapTerrain.Po \ 70 70 @AMDEP_TRUE@ ./$(DEPDIR)/heightMapViewer.Po ./$(DEPDIR)/main.Po \ 71 @AMDEP_TRUE@ ./$(DEPDIR)/material.Po ./$(DEPDIR)/object.Po \ 71 @AMDEP_TRUE@ ./$(DEPDIR)/material.Po ./$(DEPDIR)/model.Po \ 72 @AMDEP_TRUE@ ./$(DEPDIR)/objModel.Po ./$(DEPDIR)/texture.Po \ 72 73 @AMDEP_TRUE@ ./$(DEPDIR)/vector.Po ./$(DEPDIR)/windowHandler.Po 73 74 CXXCOMPILE = $(CXX) $(DEFS) $(DEFAULT_INCLUDES) $(INCLUDES) \ … … 135 136 SHELL = @SHELL@ 136 137 STRIP = @STRIP@ 138 SUB_PROJECTS_FALSE = @SUB_PROJECTS_FALSE@ 139 SUB_PROJECTS_TRUE = @SUB_PROJECTS_TRUE@ 137 140 VERSION = @VERSION@ 138 141 ac_ct_CC = @ac_ct_CC@ … … 183 186 importer_SOURCES = framework.cc \ 184 187 windowHandler.cc \ 185 object.cc \ 188 model.cc \ 189 objModel.cc \ 186 190 array.cc \ 187 191 material.cc \ 188 vector.cc 192 texture.cc \ 193 vector.cc 189 194 190 195 heightmap_SOURCES = main.cc \ … … 196 201 noinst_HEADERS = framework.h \ 197 202 windowHandler.h \ 198 object.h \ 203 model.h \ 204 objModel.h \ 199 205 array.h \ 200 material.h 206 material.h \ 207 texture.h 201 208 202 209 all: all-am … … 213 220 esac; \ 214 221 done; \ 215 echo ' cd $(top_srcdir) && $(AUTOMAKE) -- gnusrc/importer/Makefile'; \222 echo ' cd $(top_srcdir) && $(AUTOMAKE) --foreign src/importer/Makefile'; \ 216 223 cd $(top_srcdir) && \ 217 $(AUTOMAKE) -- gnusrc/importer/Makefile224 $(AUTOMAKE) --foreign src/importer/Makefile 218 225 .PRECIOUS: Makefile 219 226 Makefile: $(srcdir)/Makefile.in $(top_builddir)/config.status … … 275 282 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/main.Po@am__quote@ 276 283 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/material.Po@am__quote@ 277 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/object.Po@am__quote@ 284 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/model.Po@am__quote@ 285 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/objModel.Po@am__quote@ 286 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/texture.Po@am__quote@ 278 287 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/vector.Po@am__quote@ 279 288 @AMDEP_TRUE@@am__include@ @am__quote@./$(DEPDIR)/windowHandler.Po@am__quote@ -
orxonox/branches/nico/src/importer/framework.cc
r3238 r3399 15 15 16 16 #include "framework.h" 17 17 int verbose; 18 18 void DrawGLScene() 19 19 { … … 52 52 int main(int argc, char *argv[]) 53 53 { 54 int verbose = 3; 55 56 PRINTF(2)("This is The big verbose-Test %i, %s\n", 1, "cool"); 54 verbose = 2; 57 55 58 56 Uint8* keys; // This variable will be used in the keyboard routine … … 71 69 PRINTF(2)("screensize: %i, %i\n", wHandler.screen->w, wHandler.screen->h); 72 70 if (argc>=3) 73 obj = new O bject(argv[1], atof(argv[2]));71 obj = new OBJModel (argv[1], atof(argv[2])); 74 72 else if (argc>=2) 75 obj = new O bject(argv[1]);73 obj = new OBJModel(argv[1]); 76 74 else 77 obj = new Object(); 78 75 { 76 // This is an example, of how it is possible, to create a new Model, and adding some vertex-information. 77 // This also packs everything into a DisplayList, and can be handled exactly as any other model. 78 // This is an example of a cube with Texture-Coordinates, but without explicite Vertex-Normals. (they are soft-created). 79 80 obj = (OBJModel*) new Model(); 81 obj->setName("CUBE"); 82 obj->addVertex ("-0.5 -0.5 0.5"); 83 obj->addVertex ("0.5 -0.5 0.5"); 84 obj->addVertex ("-0.5 0.5 0.5"); 85 obj->addVertex ("0.5 0.5 0.5"); 86 obj->addVertex ("-0.5 0.5 -0.5"); 87 obj->addVertex ("0.5 0.5 -0.5"); 88 obj->addVertex ("-0.5 -0.5 -0.5"); 89 obj->addVertex ("0.5 -0.5 -0.5"); 90 91 obj->addVertexTexture ("0.0 0.0"); 92 obj->addVertexTexture ("1.0 0.0"); 93 obj->addVertexTexture ("0.0 1.0"); 94 obj->addVertexTexture ("1.0 1.0"); 95 obj->addVertexTexture ("0.0 2.0"); 96 obj->addVertexTexture ("1.0 2.0"); 97 obj->addVertexTexture ("0.0 3.0"); 98 obj->addVertexTexture ("1.0 3.0"); 99 obj->addVertexTexture ("0.0 4.0"); 100 obj->addVertexTexture ("1.0 4.0"); 101 obj->addVertexTexture ("2.0 0.0"); 102 obj->addVertexTexture ("2.0 1.0"); 103 obj->addVertexTexture ("-1.0 0.0"); 104 obj->addVertexTexture ("-1.0 1.0"); 105 106 obj->addFace ("1 2 4 3"); 107 obj->addFace ("3 4 6 5"); 108 obj->addFace ("5 6 8 7"); 109 obj->addFace ("7 8 2 1"); 110 obj->addFace ("2 8 6 4"); 111 obj->addFace ("7 1 3 5"); 112 obj->finalize(); 113 } 79 114 M = Vector(wHandler.screen->w/2, wHandler.screen->h/2, 0); 80 115 rotAxis = Vector (0.0,1.0,0.0); … … 215 250 case SDLK_c: 216 251 if (!obj) 217 obj = new O bject(argv[1]);252 obj = new OBJModel(argv[1]); 218 253 break; 219 254 } -
orxonox/branches/nico/src/importer/framework.h
r3238 r3399 3 3 4 4 #include "windowHandler.h" // Include the Whandler Basecode 5 #include "obj ect.h"5 #include "objModel.h" 6 6 #include "vector.h" 7 7 … … 33 33 34 34 WindowHandler wHandler; // Create an instance of the whandler basecode class 35 O bject* obj;35 OBJModel* obj; 36 36 37 37 -
orxonox/branches/nico/src/importer/material.cc
r3238 r3399 13 13 co-programmer: ... 14 14 15 TGA-code: borrowed from nehe-Tutorials16 17 15 */ 18 16 19 17 #include "material.h" 20 18 21 // headers only for PathList22 #include <unistd.h>23 #include <sys/types.h>24 #include <sys/stat.h>25 #include <stdlib.h>26 #include <fstream>27 28 19 using namespace std; 29 20 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 */36 PathList::PathList()37 {38 this->pathName = NULL;39 this->next = NULL;40 }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 */48 PathList::PathList(char* pName)49 {50 this->pathName = new char [strlen(pName)+1];51 strcpy (this->pathName, pName);52 this->next = NULL;53 }54 55 /**56 \brief destroys a PathList57 58 It does this by deleting the Name and then delete its preceding PathList.59 */60 PathList::~PathList()61 {62 if (this->pathName)63 delete []this->pathName;64 if (this->next)65 delete this->next;66 }67 68 /**69 \brief Adds a new Pathlist Element.70 \param pName71 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 */75 void PathList::addPath (char* pName)76 {77 if (pName[0] == '\0')78 {79 PRINTF(3)("not Adding empty Path to the List.\n");80 return;81 }82 char* tmpPName = new char[strlen(pName)];83 strncpy(tmpPName, pName, strlen(pName)-1);84 tmpPName[strlen(pName)-1] = '\0';85 if (access (tmpPName, F_OK) == 0)86 {87 struct stat status;88 stat(tmpPName, &status);89 if (status.st_mode & S_IFDIR)90 {91 PRINTF(2)("Adding Path %s to the PathList.\n", pName);92 PathList* tmpPathList = this;93 while (tmpPathList->next)94 tmpPathList = tmpPathList->next;95 tmpPathList->next = new PathList(pName);96 }97 else98 PRINTF(2)("You tried to add non-folder %s to a PathList.\n", tmpPName);99 }100 else101 PRINTF(2)("You tried to add non-existing folder %s to a PathList.\n", tmpPName);102 delete []tmpPName;103 }104 21 105 22 /** … … 133 50 if (this->name) 134 51 delete []this->name; 135 if (this->diffuseTexture Set)136 glDeleteTextures (1, &this->diffuseTexture);52 if (this->diffuseTexture) 53 this->diffuseTexture; 137 54 if (this->nextMat) 138 55 delete this->nextMat; … … 172 89 this->setTransparency(0.0); 173 90 174 if (!this->pathList) 175 this->pathList = new PathList(""); 176 91 92 this->diffuseTexture = NULL; 93 this->ambientTexture = NULL; 94 this->specularTexture = NULL; 177 95 178 96 this->diffuseTextureSet = false; … … 182 100 183 101 } 184 185 PathList *Material::pathList = NULL;186 102 187 103 /** … … 196 112 while (searcher != NULL) 197 113 { 198 PRINT F(2)(".");114 PRINT(2)("."); 199 115 if (!strcmp (searcher->getName(), mtlName)) 200 116 { 201 PRINT F(2)("found.\n");117 PRINT(2)("found.\n"); 202 118 return searcher; 203 119 } 204 120 searcher = searcher->nextMat; 205 121 } 206 PRINT F(2)("not found\n");122 PRINT(2)("not found\n"); 207 123 return NULL; 208 124 } … … 233 149 234 150 if (this->diffuseTextureSet) 235 glBindTexture(GL_TEXTURE_2D, this->diffuseTexture );151 glBindTexture(GL_TEXTURE_2D, this->diffuseTexture->getTexture()); 236 152 else 237 153 glBindTexture(GL_TEXTURE_2D, 0); … … 394 310 void Material::addTexturePath(char* pathName) 395 311 { 396 this->pathList->addPath (pathName); 397 } 398 399 /** 400 \brief Searches for a Texture inside one of the defined Paths 401 \param texName The name of the texture o search for. 402 \returns pathName+texName if texName was found in the pathList. NULL if the Texture is not found. 403 */ 404 char* Material::searchTextureInPaths(char* texName) const 405 { 406 char* tmpName = NULL; 407 PathList* pList = pathList; 408 while (pList) 409 { 410 if (pList->pathName) 411 { 412 tmpName = new char [strlen(pList->pathName)+strlen(texName)+1]; 413 strcpy(tmpName, pList->pathName); 414 } 415 else 416 { 417 tmpName = new char [strlen(texName)+1]; 418 tmpName[0]='\0'; 419 } 420 strcat(tmpName, texName); 421 if (access (tmpName, F_OK) == 0) 422 return tmpName; 423 424 if (tmpName) 425 delete []tmpName; 426 tmpName = NULL; 427 pList = pList->next; 428 } 429 return NULL; 430 } 431 312 PathList::getInstance()->addPath(pathName); 313 } 432 314 433 315 // MAPPING // … … 440 322 { 441 323 PRINTF(3)("setting Diffuse Map %s\n", dMap); 442 443 // diffuseTextureSet = loadBMP(dMap, &diffuseTexture); 444 this->diffuseTextureSet = this->loadImage(dMap, &this->diffuseTexture); 324 diffuseTexture = new Texture(); 325 this->diffuseTextureSet = diffuseTexture->loadImage(dMap); 445 326 446 327 } … … 477 358 478 359 } 479 480 /**481 \brief Loads a Texture to the openGL-environment.482 \param pImage The Image to load to openGL483 \param texture The Texture to apply it to.484 */485 bool Material::loadTexToGL (Image* pImage, GLuint* texture)486 {487 PRINTF(2)("Loading texture to OpenGL-Environment.\n");488 glGenTextures(1, texture);489 glBindTexture(GL_TEXTURE_2D, *texture);490 /* not Working, and not needed.491 glTexImage2D( GL_TEXTURE_2D, 0, 3, width,492 height, 0, GL_BGR,493 GL_UNSIGNED_BYTE, map->pixels );494 */495 gluBuild2DMipmaps(GL_TEXTURE_2D, 3, pImage->width, pImage->height, GL_RGB, GL_UNSIGNED_BYTE, pImage->data);496 497 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);498 glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR_MIPMAP_LINEAR);499 }500 501 502 #ifdef HAVE_SDL_SDL_IMAGE_H503 bool Material::loadImage(char* imageName, GLuint* texture)504 {505 char* imgNameWithPath = searchTextureInPaths(imageName);506 if (imgNameWithPath)507 {508 SDL_Surface* map;509 Image* pImage = new Image;510 map=IMG_Load(imgNameWithPath);511 if(!map)512 {513 PRINTF(1)("IMG_Load: %s\n", IMG_GetError());514 return false;515 }516 pImage->height = map->h;517 pImage->width = map->w;518 pImage->data = (GLubyte*)map->pixels;519 if( !IMG_isPNG(SDL_RWFromFile(imgNameWithPath, "rb")) && !IMG_isJPG(SDL_RWFromFile(imgNameWithPath, "rb")))520 for (int i=0;i<map->h * map->w *3;i+=3)521 {522 GLuint temp = pImage->data[i];523 pImage->data[i] = pImage->data[i+2];524 pImage->data[i+2] = temp;525 }526 this->loadTexToGL (pImage, texture);527 }528 else529 {530 PRINTF(1)("Image not Found: %s\n", imgNameWithPath);531 return false;532 }533 }534 535 536 #else /* HAVE_SDL_SDL_IMAGE_H */537 /**538 \brief Makes the Programm ready to Read-in a texture-File539 1. Checks what type of Image should be imported540 2. ToDO: Checks where to find the Image541 */542 bool Material::loadImage(char* imageName, GLuint* texture)543 {544 char* imgNameWithPath = searchTextureInPaths(imageName);545 if (imgNameWithPath)546 {547 if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".bmp", 4))548 {549 PRINTF(3)("Requested bmp-image. Trying to Import.\n");550 return this->loadBMP(imgNameWithPath, texture);551 }552 553 else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".jpg", 4) || !strncmp(imgNameWithPath+strlen(imgNameWithPath)-5, ".jpg", 5))554 {555 PRINTF(3)("Requested jpeg-image. Trying to Import\n");556 return this->loadJPG(imgNameWithPath, texture);557 }558 else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".tga", 4))559 {560 PRINTF(3)("Requested tga-image. Trying to Import\n");561 return this->loadTGA(imgNameWithPath, texture);562 }563 else if (!strncmp(imgNameWithPath+strlen(imgNameWithPath)-4, ".png", 4))564 {565 PRINTF(3)("Requested png-image. Trying to Import\n");566 return this->loadPNG(imgNameWithPath, texture);567 }568 else569 {570 PRINTF(1)("Requested Image was not recognized in its type. (Maybe a type-Cast-error.)\n FileName: %s", imgNameWithPath);571 return false;572 }573 }574 else575 {576 PRINTF(1)("Image not Found: %s\n", imgNameWithPath);577 return false;578 }579 }580 581 /**582 \brief reads in a Windows BMP-file, and imports it to openGL.583 \param bmpName The name of the Image to load.584 \param texture A pointer to the Texture which should be read to.585 */586 bool Material::loadBMP (char* bmpName, GLuint* texture)587 {588 Image* pImage = new Image;589 FILE *file;590 unsigned long size; // size of the image in bytes.591 unsigned long i; // standard counter.592 unsigned short int planes; // number of planes in image (must be 1)593 unsigned short int bpp; // number of bits per pixel (must be 24)594 GLuint temp; // temporary color storage for bgr-rgb conversion.595 596 // make sure the file is there.597 if ((file = fopen(bmpName, "rb"))==NULL)598 {599 PRINTF(1)("File Not Found : %s\n",bmpName);600 return false;601 }602 // seek through the bmp header, up to the width/height:603 fseek(file, 18, SEEK_CUR);604 605 // read the width606 if ((i = fread(&pImage->width, 4, 1, file)) != 1)607 {608 PRINTF(1)("Error reading width from %s.\n", bmpName);609 return false;610 }611 // read the height612 if ((i = fread(&pImage->height, 4, 1, file)) != 1)613 {614 PRINTF(1)("Error reading height from %s.\n", bmpName);615 return false;616 }617 618 // calculate the size (assuming 24 bits or 3 bytes per pixel).619 size = pImage->width * pImage->height * 3;620 621 // read the planes622 if ((fread(&planes, 2, 1, file)) != 1)623 {624 PRINTF(1)("Error reading planes from %s.\n", bmpName);625 return false;626 }627 if (planes != 1)628 {629 PRINTF(1)("Planes from %s is not 1: %u\n", bmpName, planes);630 return false;631 }632 633 // read the bpp634 if ((i = fread(&bpp, 2, 1, file)) != 1)635 {636 PRINTF(1)("Error reading bpp from %s.\n", bmpName);637 return false;638 }639 if (bpp != 24)640 {641 PRINTF(1)("Bpp from %s is not 24: %u\n", bmpName, bpp);642 return false;643 }644 645 // seek past the rest of the bitmap header.646 fseek(file, 24, SEEK_CUR);647 648 // read the data.649 pImage->data = (GLubyte *) malloc(size);650 if (pImage->data == NULL)651 {652 PRINTF(1)("Error allocating memory for color-corrected image data");653 return false;654 }655 656 if ((i = fread(pImage->data, size, 1, file)) != 1)657 {658 PRINTF(1)("Error reading image data from %s.\n", bmpName);659 return false;660 }661 fclose(file);662 663 // reverse all of the colors. (bgr -> rgb)664 for (i=0;i<size;i+=3)665 {666 temp = pImage->data[i];667 pImage->data[i] = pImage->data[i+2];668 pImage->data[i+2] = temp;669 }670 this->loadTexToGL (pImage, texture);671 672 673 if (pImage)674 {675 if (pImage->data)676 {677 free(pImage->data);678 }679 680 free(pImage);681 }682 return true;683 684 }685 686 /**687 \brief reads in a jpg-file688 \param jpgName the Name of the Image to load689 \param texture a reference to the Texture to write the image to690 */691 bool Material::loadJPG (char* jpgName, GLuint* texture)692 {693 #ifdef HAVE_JPEGLIB_H694 struct jpeg_decompress_struct cinfo;695 Image *pImage = NULL;696 FILE *pFile;697 698 // Open a file pointer to the jpeg file and check if it was found and opened699 if((pFile = fopen(jpgName, "rb")) == NULL)700 {701 // Display an error message saying the file was not found, then return NULL702 PRINTF(1)("Unable to load JPG File %s.\n", jpgName);703 return false;704 }705 706 // Create an error handler707 jpeg_error_mgr jerr;708 709 // Have our compression info object point to the error handler address710 cinfo.err = jpeg_std_error(&jerr);711 712 // Initialize the decompression object713 jpeg_create_decompress(&cinfo);714 715 // Specify the data source (Our file pointer)716 jpeg_stdio_src(&cinfo, pFile);717 718 // Allocate the structure that will hold our eventual jpeg data (must free it!)719 pImage = (Image*)malloc(sizeof(Image));720 721 // DECOFING722 // Read in the header of the jpeg file723 jpeg_read_header(&cinfo, TRUE);724 725 // Start to decompress the jpeg file with our compression info726 jpeg_start_decompress(&cinfo);727 728 // Get the image dimensions and row span to read in the pixel data729 pImage->rowSpan = cinfo.image_width * cinfo.num_components;730 pImage->width = cinfo.image_width;731 pImage->height = cinfo.image_height;732 733 // Allocate memory for the pixel buffer734 pImage->data = new unsigned char[pImage->rowSpan * pImage->height];735 736 // Here we use the library's state variable cinfo.output_scanline as the737 // loop counter, so that we don't have to keep track ourselves.738 739 // Create an array of row pointers740 unsigned char** rowPtr = new unsigned char*[pImage->height];741 for (int i = 0; i < pImage->height; i++)742 rowPtr[i] = &(pImage->data[i*pImage->rowSpan]);743 744 // Now comes the juice of our work, here we extract all the pixel data745 int rowsRead = 0;746 while (cinfo.output_scanline < cinfo.output_height)747 {748 // Read in the current row of pixels and increase the rowsRead count749 rowsRead += jpeg_read_scanlines(&cinfo, &rowPtr[rowsRead], cinfo.output_height - rowsRead);750 }751 752 // Delete the temporary row pointers753 delete [] rowPtr;754 755 // Finish decompressing the data756 jpeg_finish_decompress(&cinfo);// decodeJPG(&cinfo, pImage);757 758 // This releases all the stored memory for reading and decoding the jpeg759 jpeg_destroy_decompress(&cinfo);760 761 // Close the file pointer that opened the file762 fclose(pFile);763 764 765 if(pImage == NULL)766 exit(0);767 768 this->loadTexToGL (pImage, texture);769 if (pImage)770 {771 if (pImage->data)772 {773 free(pImage->data);774 }775 776 free(pImage);777 }778 return true;779 #else /* HAVE_JPEGLIB_H */780 PRINTF(1)("sorry, but you did not compile with jpeg-support.\nEither install SDL_image or jpeglib, and recompile to see the image\n");781 return false;782 #endif /* HAVE_JPEGLIB_H */783 784 }785 786 /**787 \brief reads in a tga-file788 \param tgaName the Name of the Image to load789 \param texture a reference to the Texture to write the image to790 */791 bool Material::loadTGA(const char * tgaName, GLuint* texture)792 {793 typedef struct794 {795 GLubyte Header[12];796 } TGAHeader;797 TGAHeader tgaHeader;798 799 GLubyte uTGAcompare[12] = {0,0,2, 0,0,0,0,0,0,0,0,0}; // Uncompressed TGA Header800 GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0}; // Compressed TGA Header801 FILE * fTGA;802 fTGA = fopen(tgaName, "rb");803 804 if(fTGA == NULL)805 {806 PRINTF(1)("Error could not open texture file: %s\n", tgaName);807 return false;808 }809 810 if(fread(&tgaHeader, sizeof(TGAHeader), 1, fTGA) == 0)811 {812 PRINTF(1)("Error could not read file header of %s\n", tgaName);813 if(fTGA != NULL)814 {815 fclose(fTGA);816 }817 return false;818 }819 820 if(memcmp(uTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)821 {822 loadUncompressedTGA(tgaName, fTGA, texture);823 if (fTGA)824 fclose (fTGA);825 }826 else if(memcmp(cTGAcompare, &tgaHeader, sizeof(TGAHeader)) == 0)827 {828 loadCompressedTGA(tgaName, fTGA, texture);829 if (fTGA)830 fclose (fTGA);831 }832 else833 {834 PRINTF(1)("Error TGA file be type 2 or type 10\n");835 if (fTGA)836 fclose(fTGA);837 return false;838 }839 return true;840 }841 842 /**843 \brief reads in an uncompressed tga-file844 \param filename the Name of the Image to load845 \param fTGA a Pointer to a File, that should be read846 \param texture a reference to the Texture to write the image to847 */848 bool Material::loadUncompressedTGA(const char * filename, FILE * fTGA, GLuint* texture)849 {850 GLubyte header[6]; // First 6 Useful Bytes From The Header851 GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File852 GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram853 GLuint temp; // Temporary Variable854 GLuint type;855 GLuint Height; // Height of Image856 GLuint Width; // Width of Image857 GLuint Bpp; // Bits Per Pixel858 859 Image* pImage = new Image;860 GLuint cswap;861 if(fread(header, sizeof(header), 1, fTGA) == 0)862 {863 PRINTF(1)("Error could not read info header\n");864 return false;865 }866 867 Width = pImage->width = header[1] * 256 + header[0];868 Height = pImage->height = header[3] * 256 + header[2];869 Bpp = pImage->bpp = header[4];870 // Make sure all information is valid871 if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))872 {873 PRINTF(1)("Error invalid texture information\n");874 return false;875 }876 877 if(pImage->bpp == 24)878 {879 pImage->type = GL_RGB;880 }881 else882 {883 pImage->type = GL_RGBA;884 }885 886 bytesPerPixel = (Bpp / 8);887 imageSize = (bytesPerPixel * Width * Height);888 pImage->data = (GLubyte*) malloc(imageSize);889 890 if(pImage->data == NULL)891 {892 PRINTF(1)("Error could not allocate memory for image\n");893 return false;894 }895 896 if(fread(pImage->data, 1, imageSize, fTGA) != imageSize)897 {898 PRINTF(1)("Error could not read image data\n");899 if(pImage->data != NULL)900 {901 free(pImage->data);902 }903 return false;904 }905 906 for(cswap = 0; cswap < (int)imageSize; cswap += bytesPerPixel)907 {908 pImage->data[cswap] ^= pImage->data[cswap+2] ^=909 pImage->data[cswap] ^= pImage->data[cswap+2];910 }911 912 this->loadTexToGL (pImage, texture);913 914 return true;915 }916 917 /**918 \brief reads in a compressed tga-file919 \param filename the Name of the Image to load920 \param fTGA a Pointer to a File, that should be read921 \param texture a reference to the Texture to write the image to922 */923 bool Material::loadCompressedTGA(const char * filename, FILE * fTGA, GLuint* texture)924 {925 GLubyte header[6]; // First 6 Useful Bytes From The Header926 GLuint bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File927 GLuint imageSize; // Used To Store The Image Size When Setting Aside Ram928 GLuint temp; // Temporary Variable929 GLuint type;930 GLuint Height; // Height of Image931 GLuint Width; // Width of Image932 GLuint Bpp; // Bits Per Pixel933 934 Image* pImage = new Image;935 936 937 if(fread(header, sizeof(header), 1, fTGA) == 0)938 {939 PRINTF(1)("Error could not read info header\n");940 return false;941 }942 943 Width = pImage->width = header[1] * 256 + header[0];944 Height = pImage->height = header[3] * 256 + header[2];945 Bpp = pImage->bpp = header[4];946 947 GLuint pixelcount = Height * Width;948 GLuint currentpixel = 0;949 GLuint currentbyte = 0;950 GLubyte * colorbuffer = (GLubyte *)malloc(bytesPerPixel);951 952 //Make sure all pImage info is ok953 if((pImage->width <= 0) || (pImage->height <= 0) || ((pImage->bpp != 24) && (pImage->bpp !=32)))954 {955 PRINTF(1)("Error Invalid pImage information\n");956 return false;957 }958 959 bytesPerPixel = (Bpp / 8);960 imageSize = (bytesPerPixel * Width * Height);961 pImage->data = (GLubyte*) malloc(imageSize);962 963 if(pImage->data == NULL)964 {965 PRINTF(1)("Error could not allocate memory for image\n");966 return false;967 }968 969 do970 {971 GLubyte chunkheader = 0;972 973 if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0)974 {975 PRINTF(1)("Error could not read RLE header\n");976 if(pImage->data != NULL)977 {978 free(pImage->data);979 }980 return false;981 }982 // If the ehader is < 128, it means the that is the number of RAW color packets minus 1983 if(chunkheader < 128)984 {985 short counter;986 chunkheader++;987 // Read RAW color values988 for(counter = 0; counter < chunkheader; counter++)989 {990 // Try to read 1 pixel991 if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel)992 {993 PRINTF(1)("Error could not read image data\n");994 if(colorbuffer != NULL)995 {996 free(colorbuffer);997 }998 999 if(pImage->data != NULL)1000 {1001 free(pImage->data);1002 }1003 1004 return false;1005 }1006 // write to memory1007 // Flip R and B vcolor values around in the process1008 pImage->data[currentbyte ] = colorbuffer[2];1009 pImage->data[currentbyte + 1] = colorbuffer[1];1010 pImage->data[currentbyte + 2] = colorbuffer[0];1011 1012 if(bytesPerPixel == 4) // if its a 32 bpp image1013 {1014 pImage->data[currentbyte + 3] = colorbuffer[3];// copy the 4th byte1015 }1016 1017 currentbyte += bytesPerPixel;1018 currentpixel++;1019 1020 // Make sure we haven't read too many pixels1021 if(currentpixel > pixelcount)1022 {1023 PRINTF(1)("Error too many pixels read\n");1024 if(colorbuffer != NULL)1025 {1026 free(colorbuffer);1027 }1028 1029 if(pImage->data != NULL)1030 {1031 free(pImage->data);1032 }1033 1034 return false;1035 }1036 }1037 }1038 // chunkheader > 128 RLE data, next color reapeated chunkheader - 127 times1039 else1040 {1041 short counter;1042 chunkheader -= 127; // Subteact 127 to get rid of the ID bit1043 if(fread(colorbuffer, 1, bytesPerPixel, fTGA) != bytesPerPixel) // Attempt to read following color values1044 {1045 PRINTF(1)("Error could not read from file");1046 if(colorbuffer != NULL)1047 {1048 free(colorbuffer);1049 }1050 1051 if(pImage->data != NULL)1052 {1053 free(pImage->data);1054 }1055 1056 return false;1057 }1058 1059 for(counter = 0; counter < chunkheader; counter++) //copy the color into the image data as many times as dictated1060 {1061 // switch R and B bytes areound while copying1062 pImage->data[currentbyte ] = colorbuffer[2];1063 pImage->data[currentbyte + 1] = colorbuffer[1];1064 pImage->data[currentbyte + 2] = colorbuffer[0];1065 1066 if(bytesPerPixel == 4)1067 {1068 pImage->data[currentbyte + 3] = colorbuffer[3];1069 }1070 1071 currentbyte += bytesPerPixel;1072 currentpixel++;1073 1074 if(currentpixel > pixelcount)1075 {1076 PRINTF(1)("Error too many pixels read\n");1077 if(colorbuffer != NULL)1078 {1079 free(colorbuffer);1080 }1081 1082 if(pImage->data != NULL)1083 {1084 free(pImage->data);1085 }1086 1087 return false;1088 }1089 }1090 }1091 }1092 1093 while(currentpixel < pixelcount); // Loop while there are still pixels left1094 1095 this->loadTexToGL (pImage, texture);1096 1097 return true;1098 }1099 1100 1101 /*1102 static int ST_is_power_of_two(unsigned int number)1103 {1104 return (number & (number - 1)) == 0;1105 }1106 */1107 1108 /**1109 \brief reads in a png-file1110 \param pngName the Name of the Image to load1111 \param texture a reference to the Texture to write the image to1112 */1113 bool Material::loadPNG(const char* pngName, GLuint* texture)1114 {1115 #ifdef HAVE_PNG_H1116 Image* pImage = new Image;1117 1118 FILE *PNG_file = fopen(pngName, "rb");1119 if (PNG_file == NULL)1120 {1121 return 0;1122 }1123 1124 GLubyte PNG_header[8];1125 1126 fread(PNG_header, 1, 8, PNG_file);1127 if (png_sig_cmp(PNG_header, 0, 8) != 0)1128 {1129 PRINTF(2)("Not Recognized as a pngFile\n");1130 fclose (PNG_file);1131 return 0;1132 }1133 1134 png_structp PNG_reader = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);1135 if (PNG_reader == NULL)1136 {1137 fclose(PNG_file);1138 return 0;1139 }1140 1141 png_infop PNG_info = png_create_info_struct(PNG_reader);1142 if (PNG_info == NULL)1143 {1144 png_destroy_read_struct(&PNG_reader, NULL, NULL);1145 fclose(PNG_file);1146 return 0;1147 }1148 1149 png_infop PNG_end_info = png_create_info_struct(PNG_reader);1150 if (PNG_end_info == NULL)1151 {1152 png_destroy_read_struct(&PNG_reader, &PNG_info, NULL);1153 fclose(PNG_file);1154 return 0;1155 }1156 1157 if (setjmp(png_jmpbuf(PNG_reader)))1158 {1159 png_destroy_read_struct(&PNG_reader, &PNG_info, &PNG_end_info);1160 fclose(PNG_file);1161 return (0);1162 }1163 1164 png_init_io(PNG_reader, PNG_file);1165 png_set_sig_bytes(PNG_reader, 8);1166 1167 png_read_info(PNG_reader, PNG_info);1168 1169 pImage->width = png_get_image_width(PNG_reader, PNG_info);1170 pImage->height = png_get_image_height(PNG_reader, PNG_info);1171 1172 png_uint_32 bit_depth, color_type;1173 bit_depth = png_get_bit_depth(PNG_reader, PNG_info);1174 color_type = png_get_color_type(PNG_reader, PNG_info);1175 1176 if (color_type == PNG_COLOR_TYPE_PALETTE)1177 {1178 png_set_palette_to_rgb(PNG_reader);1179 }1180 1181 if (color_type == PNG_COLOR_TYPE_GRAY && bit_depth < 8)1182 {1183 png_set_gray_1_2_4_to_8(PNG_reader);1184 }1185 1186 if (color_type == PNG_COLOR_TYPE_GRAY || color_type == PNG_COLOR_TYPE_GRAY_ALPHA)1187 {1188 png_set_gray_to_rgb(PNG_reader);1189 }1190 1191 if (png_get_valid(PNG_reader, PNG_info, PNG_INFO_tRNS))1192 {1193 png_set_tRNS_to_alpha(PNG_reader);1194 }1195 else1196 {1197 png_set_filler(PNG_reader, 0xff, PNG_FILLER_AFTER);1198 }1199 1200 if (bit_depth == 16)1201 {1202 png_set_strip_16(PNG_reader);1203 }1204 1205 png_read_update_info(PNG_reader, PNG_info);1206 1207 pImage->data = (png_byte*)malloc(4 * pImage->width * pImage->height);1208 png_byte** PNG_rows = (png_byte**)malloc(pImage->height * sizeof(png_byte*));1209 1210 unsigned int row;1211 for (row = 0; row < pImage->height; ++row)1212 {1213 PNG_rows[pImage->height - 1 - row] = pImage->data + (row * 4 * pImage->width);1214 }1215 1216 png_read_image(PNG_reader, PNG_rows);1217 1218 free(PNG_rows);1219 1220 png_destroy_read_struct(&PNG_reader, &PNG_info, &PNG_end_info);1221 fclose(PNG_file);1222 1223 /* if (!ST_is_power_of_two(pImage->width) || !ST_is_power_of_two(pImage->height))1224 {1225 free(pImage->data);1226 return 0;1227 }1228 */1229 this->loadTexToGL (pImage, texture);1230 1231 free(pImage->data);1232 1233 return true;1234 #else /* HAVE_PNG_H */1235 PRINTF(1)("sorry, but you did not compile with png-support.\nEither install SDL_image or libpng, and recompile to see the image\n");1236 return false;1237 #endif /* HAVE_PNG_H */1238 1239 }1240 1241 #endif /* HAVE_SDL_SDL_IMAGE_H */ -
orxonox/branches/nico/src/importer/material.h
r3238 r3399 14 14 15 15 #include "../stdincl.h" 16 #include "texture.h" 16 17 17 18 #if HAVE_CONFIG_H … … 19 20 #endif /* HAVE_CONFIG_H */ 20 21 21 #ifdef HAVE_SDL_SDL_IMAGE_H22 #include <SDL/SDL_image.h>23 #else24 // IMAGE LIBS //25 #ifdef HAVE_JPEGLIB_H26 extern "C"{ // This has to be done, because not a c++ lib27 #include <jpeglib.h>28 }29 #endif /* HAVE_JPEGLIB_H */30 #ifdef HAVE_PNG_H31 #include <png.h>32 #endif /* HAVE_PNG_H */33 #endif /* HAVE_SDL_SDL_IMAGE_H */34 22 35 //! Class to handle lists of paths.36 /**37 \todo Ability to return Paths by itself.38 39 It is simple to use, and good, for all PathList you want.40 just create a new Pathlist, and add Paths.41 */42 class PathList43 {44 public:45 PathList();46 PathList(char* pName);47 48 ~PathList();49 void addPath (char* pName);50 char* pathName; //!< The Name of the current Path.51 PathList* next; //!< Pointer to the next Pathlist.52 };53 23 54 24 … … 92 62 93 63 private: 94 //! Struct to handle Infos about an Image95 struct Image96 {97 int rowSpan; //!< The count of the rows this Image has.98 GLuint width; //!< The width of the Image.99 GLuint height; //!< The height of the Image.100 GLuint bpp; //!< BitsPerPixel101 GLuint type; //!< Type of the Image.102 GLubyte *data; //!< The Image Data comes here! DANGER: uncompressed data.103 };104 105 106 64 char* name; //!< The Name of the Material. 107 65 int illumModel; //!< The IlluminationModel is either flat or smooth. … … 112 70 float transparency;//!< The transperency of the Material. 113 71 114 static PathList* pathList; //!< A pointer to the first element of Pathlist. This is static, because pathlists are global \todo copy this to the Globals.h or DataTank for deletion at the end. 115 116 GLuint diffuseTexture; //!< The diffuse texture of the Material. 117 GLuint ambientTexture; //!< The ambient texture of the Material. 118 GLuint specularTexture;//!< The specular texture of the Material. 72 Texture* diffuseTexture; //!< The diffuse texture of the Material. 73 Texture* ambientTexture; //!< The ambient texture of the Material. 74 Texture* specularTexture;//!< The specular texture of the Material. 119 75 120 76 bool diffuseTextureSet; //!< Chekcs if the diffuse texture is Set. … … 124 80 Material* nextMat; //!< pointer to the Next Material of the List. NULL if no next exists. 125 81 126 // TEXTURING127 bool loadTexToGL (Image* pImage, GLuint* texture);128 82 129 bool loadImage(char* imageName, GLuint* texture);130 #ifndef HAVE_SDL_SDL_IMAGE_H131 132 bool loadBMP (char* bmpName, GLuint* texture);133 134 bool loadJPG (char* jpgName, GLuint* texture);135 136 /// TGA ///137 138 bool loadTGA(const char * tgaName, GLuint* texture);139 bool loadUncompressedTGA(const char * filename, FILE * fTGA, GLuint* texture);140 bool loadCompressedTGA(const char * filename, FILE * fTGA, GLuint* texture);141 142 bool loadPNG(const char* pngName, GLuint* texture);143 #endif144 83 }; 145 84 #endif
Note: See TracChangeset
for help on using the changeset viewer.