GLuint TextureLoader::loadTexture(const std::string & filename) { unsigned char* data = NULL; if(filename.length() < 5) { return 0; } std::string extension = filename.substr(filename.length() - 3, 3); bool isPNG = false; bool isBMP = false; if(extension.compare("png") == 0) { isPNG = true; } else if(extension.compare("bmp") == 0) { isBMP = true; } else { std::cout << "Texture loading error: Bad file extension!\n"; return 0; } std::ifstream textureFile(filename.c_str(), std::ios::in | std::ios::binary); if(!textureFile.good()) { std::cout << "Texture loading error: File not good!\n"; return 0; } unsigned int width = 0; unsigned int height = 0; if(isPNG && !isBMP) { loadPNGTexture(textureFile, width, height, data); } else if(isBMP && !isPNG) { loadBMPTexture(textureFile, width, height, data); } else { return 0; } textureFile.close(); if(data == NULL) { return 0; } GLuint texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); GLfloat maxAnisotropy = 1.0; glGetFloatv(GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT, &maxAnisotropy); glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAX_ANISOTROPY_EXT, maxAnisotropy); // Not needed after adding alpha byte - natural alignment to 4 bytes // glPixelStorei(GL_UNPACK_ALIGNMENT, 1); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data); glGenerateMipmap(GL_TEXTURE_2D); delete data; return texture; }
int* loadTextures(B3DFile* b3d) { int* output; Blitz3DTEXSChunk* texsChunk; unsigned int textureCount; unsigned int iter; texsChunk = getTEXSChunkFromBB3DChunk(getBB3DChunkFromFile(b3d)); textureCount = getTextureArrayCountFromTEXSChunk(texsChunk); output = (int*)calloc(textureCount, sizeof(int)); glEnable(GL_TEXTURE_2D); for (iter = 0; iter < textureCount; iter++) { char* directoryPath; char* fileName; char* filePath; directoryPath = getDirectoryFromFile(b3d); fileName = getFileFromTexture(getTextureArrayEntryFromTEXSChunk(texsChunk, iter)); filePath = (char*)calloc(strlen(directoryPath) + strlen(fileName) + 1, sizeof(char)); sprintf(filePath, "%s%s", directoryPath, fileName); /*printf("full path: %s\n", filePath);*/ if (filePath[strlen(filePath) - 1] == 'g') { /*printf(" is a PNG image\n");*/ output[iter] = loadPNGTexture(filePath); /*printf(" texture id: %d\n", output[iter]);*/ } if (filePath[strlen(filePath) - 1] == 'p') { /*printf(" is a BMP image\n");*/ output[iter] = loadBMPTexture(filePath); /*printf(" texture id: %d\n", output[iter]);*/ } free(filePath); } return output; }
/** Initializes the visualization, creates the window and draws the objects. @param void @return void */ void Cyb3DWorld::init() { CybParameters *cybCore = CybParameters::getInstance(); int p[1] = { 1 }; char **config = new char*[2]; config[0] = windowName; config[1] = NULL; cybCore->viewType = viewNumber; if(viewNumber != 5) { glutInit(p, config); if(stereo) glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH | GLUT_STEREO ); else glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); glutInitWindowSize(cybCore->Resx, cybCore->Resy); glutInitWindowPosition(50, 50); double aspect = (double)cybCore->Resx / (double)cybCore->Resy frustum = new CybPerpectiveFrustum(PI / 3.0, aspect, 1, 100); glutCreateWindow(windowName); /*If texturing is active*/ if(cybCore->nTextures) { for(int j=0; j < cybCore->nTextures; j++) loadBMPTexture(cybCore->texName[j],j); } glutDisplayFunc(mainDisplay); glutReshapeFunc(mainReshape); glutIdleFunc(mainIdle); if(cybCore->isKeyboardEnable()) { glutKeyboardFunc(mainKeyboard); glutSpecialFunc(mainSpecialKey); } glutMouseFunc(mainMouseFunc); glutMotionFunc(mainMouseMotionFunc); glutPassiveMotionFunc(mainMousePassiveMotionFunc); loadObjects(); glutMainLoop(); } else { glutInit(p, config); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH); if(!defResolution) { std::cout << "Warning: Resolution has not been defined!. Using default 800x600 Resolution." << std::endl; } glutInitWindowSize(horResolution, verResolution); double aspect = (double)horResolution / (double)verResolution; frustum = new CybPerpectiveFrustum(PI / 3.0, aspect, 1, 100); /*If texturing is active*/ if(cybCore->nTextures) { for(int j=0; j < cybCore->nTextures; j++) loadBMPTexture(cybCore->texName[j],j); } //Left Window if(swap) { glutInitWindowPosition(horResolution, 0); } else { glutInitWindowPosition(0, 0); } leftWindow = glutCreateWindow(windowName); glutSetWindow(leftWindow); glutDisplayFunc(mainDisplay); glutReshapeFunc(mainReshape); glutIdleFunc(mainIdle); if(cybCore->isKeyboardEnable()) { glutKeyboardFunc(mainKeyboard); glutSpecialFunc(mainSpecialKey); } glutMouseFunc(mainMouseFunc); glutMotionFunc(mainMouseMotionFunc); glutPassiveMotionFunc(mainMousePassiveMotionFunc); //Right Window if(swap) { glutInitWindowPosition(0, 0); } else { glutInitWindowPosition(horResolution, 0); } rightWindow = glutCreateWindow(windowName); glutSetWindow(rightWindow); glutDisplayFunc(mainDisplay); glutReshapeFunc(mainReshape); glutIdleFunc(mainIdle); if(cybCore->isKeyboardEnable()) { glutKeyboardFunc(mainKeyboard); glutSpecialFunc(mainSpecialKey); } glutMouseFunc(mainMouseFunc); glutMotionFunc(mainMouseMotionFunc); glutPassiveMotionFunc(mainMousePassiveMotionFunc); loadObjects2(); glutMainLoop(); } }