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; }
void Style::write ( QDomElement& xml_rep ) const { QDomDocument document = xml_rep.ownerDocument(); QDomElement style_element = document.createElement( lC::STR::STYLE ); style_element.setAttribute( lC::STR::STYLE, lC::Render::styleText( style())); style_element.setAttribute( lC::STR::EDGE, lC::Edge::styleText( edge() ) ); style_element.setAttribute( lC::STR::WIREFRAME, wireframeColor().name() ); style_element.setAttribute( lC::STR::SOLID, solidColor().name() ); style_element.setAttribute( lC::STR::TEXTURE, textureFile() ); xml_rep.appendChild( style_element ); }
bool CCTextureFontPageFile::load() { CCText textureFile( name ); textureFile += ".png"; CCResourceType resourceType = CCFileManager::FindFile( textureFile.buffer ); if( resourceType != Resource_Unknown ) { texturePageIndex = gEngine->textureManager->assignTextureIndex( textureFile.buffer, resourceType, true, true, true ); CCText csvFile = name; csvFile += ".csv"; // Load the descriptor file CCText textData; CCFileManager::GetFile( csvFile.buffer, textData, resourceType ); CCList<char> lettersSplit; textData.split( lettersSplit, "\n" ); CCText rawLetterData; CCList<char> letterDataSplit; for( int i=0; i<lettersSplit.length; ++i ) { const char *raw = lettersSplit.list[i]; rawLetterData.set( raw ); letterDataSplit.clear(); rawLetterData.split( letterDataSplit, "," ); ASSERT( letterDataSplit.length == 4 ); Letter &letter = letters[i]; const char *x1 = letterDataSplit.list[0]; const char *y1 = letterDataSplit.list[1]; const char *x2 = letterDataSplit.list[2]; const char *y2 = letterDataSplit.list[3]; letter.start.x = (float)atof( x1 ); letter.start.y = (float)atof( y1 ); letter.end.x = (float)atof( x2 ); letter.end.y = (float)atof( y2 ); // 16.0f because there's 16 tiles per font page letter.size.width = ( letter.end.x - letter.start.x ) * 16.0f; letter.size.height = ( letter.end.y - letter.start.y ) * 16.0f; } return true; } return false; }
textureTga::textureTga(const char *filename, const int textureId){ byte *imageData; int numPixels; int bytesInPixel; int imageDataSize; headerTga fileHeader; byte temp; // Open file std::ifstream textureFile(filename, std::ios::in | std::ios::binary | std::ios::ate); if(!textureFile.is_open()){ std::cout<<"[TGA] ERROR: Could not open '"<<filename<<"'"<<std::endl; return; } if(int(textureFile.tellg()) == 0){ std::cout<<"[TGA] ERROR: Texture '"<<filename<<"' is empty"<<std::endl; textureFile.close(); return; } // Read TGA header textureFile.seekg(0, std::ios::beg); textureFile.read((char*)&fileHeader, sizeof(fileHeader)); m_width = fileHeader.width[0] + (fileHeader.width[1] << 8); m_height = fileHeader.height[0] + (fileHeader.height[1] << 8); m_bpp = fileHeader.bpp; // We only support uncompressed 24 or 32 bits per pixel TGAs if(fileHeader.colorMapType == 1 || int(fileHeader.imageType) != 2){ std::cout<<"[TGA] ERROR: Invalid texture format, '"<<filename<<"' must be uncompressed 24/32bpp TGA"<<std::endl; textureFile.close(); return; } if(m_bpp != 32 && m_bpp != 24){ std::cout<<"[TGA] ERROR: Invalid texture color depth, '"<<filename<<"' must be uncompressed 24/32bpp TGA"<<std::endl; textureFile.close(); return; } // OpenGL textures must be power-of-two if((m_width&(m_width-1)) || (m_height&(m_height-1))){ std::cout<<"[TGA] ERROR: Texture '"<<filename<<"' must have power-of-two width & height"<<std::endl; textureFile.close(); return; } // Determine format switch(m_bpp){ case 24:m_format = GL_RGB; bytesInPixel = 3; break; case 32:m_format = GL_RGBA; bytesInPixel = 4; break; default: std::cout<<"[TGA] ERROR: Invalid texture color depth, '"<<filename<<"' must be uncompressed 24/32bpp TGA"<<std::endl; textureFile.close(); return; break; } // Allocate memory for image numPixels = m_width * m_height; imageDataSize = numPixels * bytesInPixel; imageData = new byte[imageDataSize]; if(!imageData){ std::cout<<"[TGA] ERROR: Out of memory"<<std::endl; textureFile.close(); return; } // Read image data textureFile.seekg(18, std::ios::beg); textureFile.read((char*)imageData, imageDataSize); textureFile.close(); // TGAs are BGRA, convert to RGBA for(int pixel=0; pixel < numPixels*bytesInPixel; pixel+=bytesInPixel){ temp = imageData[pixel+2]; imageData[pixel+2] = imageData[pixel]; imageData[pixel] = temp; } // Bind texture ID to load glBindTexture(GL_TEXTURE_2D, textureId); // Set texture parameters glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE); // Upload texture to card with bound texture ID glTexImage2D(GL_TEXTURE_2D, 0, m_format, m_width, m_height, 0, m_format, GL_UNSIGNED_BYTE, imageData); // Texture's uploaded, don't need data any more delete imageData; std::cout<<"[TGA] Texture '"<<filename<<"' loaded"<<std::endl; }
// Load Material void GLC_3dsToWorld::loadMaterial(Lib3dsMaterial* p3dsMaterial) { GLC_Material* pMaterial= new GLC_Material; // Set the material name const QString materialName(p3dsMaterial->name); pMaterial->setName(materialName); // Check if there is a texture if (p3dsMaterial->texture1_map.name[0]) { const QString textureName(p3dsMaterial->texture1_map.name); // Retrieve the .3ds file path QFileInfo fileInfo(m_FileName); QString textureFileName(fileInfo.absolutePath() + QDir::separator()); textureFileName.append(textureName); // TGA file type are not supported if (!textureName.right(3).contains("TGA", Qt::CaseInsensitive)) { QFile textureFile(textureFileName); if (textureFile.open(QIODevice::ReadOnly)) { // Create the texture and assign it to the material GLC_Texture *pTexture = new GLC_Texture(textureFile); pMaterial->setTexture(pTexture); m_ListOfAttachedFileName << textureFileName; textureFile.close(); } else { QStringList stringList(m_FileName); stringList.append("Open File : " + textureFileName + " failed"); GLC_ErrorLog::addError(stringList); } } else { QStringList stringList(m_FileName); stringList.append("Image : " + textureFileName + " not suported"); GLC_ErrorLog::addError(stringList); } } // Ambient Color QColor ambient; ambient.setRgbF(p3dsMaterial->ambient[0], p3dsMaterial->ambient[1], p3dsMaterial->ambient[2]); ambient.setAlphaF(p3dsMaterial->ambient[3]); pMaterial->setAmbientColor(ambient); // Diffuse Color QColor diffuse; diffuse.setRgbF(p3dsMaterial->diffuse[0], p3dsMaterial->diffuse[1], p3dsMaterial->diffuse[2]); diffuse.setAlphaF(p3dsMaterial->diffuse[3]); pMaterial->setDiffuseColor(diffuse); // Specular Color QColor specular; specular.setRgbF(p3dsMaterial->specular[0], p3dsMaterial->specular[1], p3dsMaterial->specular[2]); specular.setAlphaF(p3dsMaterial->specular[3]); pMaterial->setSpecularColor(specular); // Shininess if (0 != p3dsMaterial->shininess) { float matShininess= p3dsMaterial->shininess * 128.0f; if (matShininess > 128.0f) matShininess= 128.0f; if (matShininess < 5.0f) matShininess= 20.0f; pMaterial->setShininess(matShininess); } // Transparency pMaterial->setOpacity(1.0 - p3dsMaterial->transparency); // Add the material to the hash table m_Materials.insert(materialName, pMaterial); }