unsigned int CBitmap::CreateTexture(bool mipmaps) { if(type == BitmapTypeDDS) { return CreateDDSTexture(); } if(mem==NULL) return 0; unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR); if(mipmaps) { // create mipmapped texture glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8 ,xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, mem); } else { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8 ,xsize, ysize, 0,GL_RGBA, GL_UNSIGNED_BYTE, mem); gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8 ,xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, mem); } return texture; }
//CBool CTexture::LoadTargaTexture( CImage * texObj, CChar* name, CChar* sceneFileName ) //{ // //attache the sceneFileName path( without the dea file ) to the texture name // CChar pathName[MAX_NAME_SIZE]; // // if( sceneFileName ) //To deal with Collada files. // { // CChar * texName = GetAfterPath( name ); //don't know if it's required? Maybe the name in collada file has no path // //strcpy( pathName , sceneFileName ); // //CChar *removeExtra = GetAfterPath( pathName ); // //removeExtra[0] = 0; // //strcat( pathName, texName ); // CChar g_currentVSceneNameWithoutDot[MAX_NAME_SIZE]; // Cpy( g_currentVSceneNameWithoutDot, g_currentVSceneName ); // GetWithoutDot( g_currentVSceneNameWithoutDot ); // // sprintf( pathName, "%s%s%s%s", "assets/vscenes/", g_currentVSceneNameWithoutDot, "/Textures/", texName ); // } // else //To load independent targa files(not specified in a collada file ) // strcpy( pathName, name ); // // ILuint imageId; // ilGenImages(1, &imageId); // ilBindImage(imageId); // // //PrintInfo( _T( "Reading Image : " ) ); // //PrintInfo( _T( "'" ) + CString( pathName ) + _T("'\n"), COLOR_RED_BLUE ); // // // Read in the image file into DevIL. // if (!ilLoadImage(pathName)) { // // ERROR // ilDeleteImages(1, &imageId); // MessageBox( NULL, _T("CTexture::LoadTargaTexture > Couldn't load the targa file\n"), _T( "VandaEngine Error"), MB_OK ); // return false; // } // else { // texObj->SetWidth( ilGetInteger(IL_IMAGE_WIDTH) ); // texObj->SetHeight( ilGetInteger(IL_IMAGE_HEIGHT) ); // // CUChar* imageData; // CInt imageSize; // // imageSize = ilGetInteger(IL_IMAGE_WIDTH) * ilGetInteger(IL_IMAGE_HEIGHT) * ilGetInteger(IL_IMAGE_CHANNELS); // imageData = (CUChar*)malloc( imageSize ); // // if( ilGetInteger(IL_IMAGE_CHANNELS) == 3 ) // texObj->SetFormat( TGA_TRUECOLOR_24 ); // else if ( ilGetInteger(IL_IMAGE_CHANNELS) == 4 ) // texObj->SetFormat( TGA_TRUECOLOR_32 ); // // memcpy( imageData , ilGetData() , imageSize ); // texObj->SetImageData( imageData ); // ilDeleteImages(1, &imageId); // } // CreateTargaTexture( texObj ); // // return CTrue; //} // CBool CTexture::LoadDDSTexture( CImage * texObj, CChar* name, CChar* sceneFileName ) { //attache the sceneFileName path( without the dea file ) to the texture name CChar pathName[MAX_NAME_SIZE]; if( sceneFileName ) //To deal with Collada files. { CChar * texName = GetAfterPath( name ); //replace %20 with space using std::string std::string s(texName); size_t i = 0; for (;;) { i = s.find("%20", i); if (i == string::npos) { break; } s.replace(i, 3, " "); } strcpy(texName, s.c_str()); sprintf( pathName, "%s%s", g_pathProperties.m_meshDiffuseTexturePath, texName ); } else //To load independent dds files(not specified in a collada file ) strcpy( pathName, name ); ifstream file(pathName, ios::binary); if (! file) { CChar temp[MAX_NAME_SIZE]; sprintf( temp, "%s%s%s", "CTexture::LoadDDSTexture > Couldn't open the DDS file '", pathName, "'" ); MessageBoxA( NULL, temp, "VandaEngine Error", MB_OK ); return false; } CDDS *m_ddsImage = CNew( CDDS ); if (! m_ddsImage->LoadFile(file)) { CChar temp[MAX_NAME_SIZE]; sprintf( temp, "%s%s%s", "CTexture::LoadDDSTexture > Couldn't load the DDS file '", pathName, "'" ); MessageBoxA( NULL, temp, "VandaEngine Error", MB_OK ); return false; } texObj->SetWidth( (CInt32)m_ddsImage->GetWidth() ); texObj->SetHeight( (CInt32)m_ddsImage->GetHeight() ); if( m_ddsImage->m_alphaChannel) texObj->SetFormat(TGA_TRUECOLOR_32); else texObj->SetFormat(TGA_TRUECOLOR_24); CreateDDSTexture( texObj, m_ddsImage ); return CTrue; }
unsigned int CBitmap::CreateTexture(bool mipmaps) { if(type == BitmapTypeDDS) { return CreateDDSTexture(); } if(mem==NULL) return 0; // jcnossen: ATI drivers appear to support OpenGL 2.0, but switch to software rendering when non-power-of-two textures are used. if ((xsize != next_power_of_2(xsize) || ysize != next_power_of_2(ysize))) //&& strcmp(reinterpret_cast<const char*>(glGetString(GL_VERSION)), "2.0") < 0 ) { CBitmap bm = CreateRescaled(next_power_of_2(xsize), next_power_of_2(ysize)); return bm.CreateTexture(mipmaps); } unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); 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); if(mipmaps) { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); // create mipmapped texture if (strcmp(reinterpret_cast<const char*>(glGetString(GL_VERSION)), "1.4") >= 0) { // This required GL-1.4 // instead of using glu, we rely on glTexImage2D to create the Mipmaps. glTexParameteri(GL_TEXTURE_2D,GL_GENERATE_MIPMAP,true); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8 ,xsize, ysize, 0,GL_RGBA, GL_UNSIGNED_BYTE, mem); } else gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8 ,xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, mem); } else { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8 ,xsize, ysize, 0,GL_RGBA, GL_UNSIGNED_BYTE, mem); //gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8 ,xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, mem); glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8 ,xsize, ysize, 0,GL_RGBA, GL_UNSIGNED_BYTE, mem); } return texture; }
const unsigned int CBitmap::CreateTexture(bool mipmaps) const { ScopedTimer timer("Textures::CBitmap::CreateTexture"); if (type == BitmapTypeDDS) { return CreateDDSTexture(); } if (mem == NULL) { return 0; } // jcnossen: Some drivers return "2.0" as a version string, // but switch to software rendering for non-power-of-two textures. // GL_ARB_texture_non_power_of_two indicates that the hardware will actually support it. if (!globalRendering->supportNPOTs && (xsize != next_power_of_2(xsize) || ysize != next_power_of_2(ysize))) { CBitmap bm = CreateRescaled(next_power_of_2(xsize), next_power_of_2(ysize)); return bm.CreateTexture(mipmaps); } unsigned int texture; glGenTextures(1, &texture); glBindTexture(GL_TEXTURE_2D, texture); 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); //FIXME add glPixelStorei(GL_UNPACK_ALIGNMENT, 1); for NPOTs if (mipmaps) { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR); glBuildMipmaps(GL_TEXTURE_2D, GL_RGBA8, xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, mem); } else { glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR); //glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA8 ,xsize, ysize, 0,GL_RGBA, GL_UNSIGNED_BYTE, mem); //gluBuild2DMipmaps(GL_TEXTURE_2D,GL_RGBA8 ,xsize, ysize, GL_RGBA, GL_UNSIGNED_BYTE, mem); glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, xsize, ysize, 0, GL_RGBA, GL_UNSIGNED_BYTE, mem); } return texture; }
//CBool CTexture::LoadTargaTexture( CImage * texObj, CChar* name, CChar* sceneFileName ) //{ // //attache the sceneFileName path( without the dea file ) to the texture name // CChar pathName[MAX_NAME_SIZE]; // // if( sceneFileName ) //To deal with COLLADA files. // { // CChar * texName = GetAfterPath( name ); //don't know if it's required? Maybe the name in collada file has no path // //strcpy( pathName , sceneFileName ); // //CChar *removeExtra = GetAfterPath( pathName ); // //removeExtra[0] = 0; // //strcat( pathName, texName ); // //save functions. it should be copies in WIN32 Project as well // CChar g_currentVSceneNameWithoutDot[MAX_NAME_SIZE]; // Cpy( g_currentVSceneNameWithoutDot, g_currentVSceneName ); // GetWithoutDot( g_currentVSceneNameWithoutDot ); // // sprintf( pathName, "%s%s%s%s", "assets/vscenes/", g_currentVSceneNameWithoutDot, "/Textures/", texName ); // // } // else //To load independent targa files(not specified in a collada file ) // strcpy( pathName, name ); // // ILuint imageId; // ilGenImages(1, &imageId); // ilBindImage(imageId); // // PrintInfo( _T( "\nReading Image : " ) ); // PrintInfo( _T( "'" ) + CString( pathName ) + _T("'"), COLOR_RED_GREEN ); // // // Read in the image file into DevIL. // if (!ilLoadImage(pathName)) { // // ERROR // ilDeleteImages(1, &imageId); // CChar temp[MAX_NAME_SIZE]; // sprintf( temp, "\nError! CTexture::LoadTargaTexture > Couldn't load the targa file: '%s'", pathName ); // PrintInfo( temp, COLOR_RED ); // numErrors += 1; // // return false; // } // else { // texObj->SetWidth( ilGetInteger(IL_IMAGE_WIDTH) ); // texObj->SetHeight( ilGetInteger(IL_IMAGE_HEIGHT) ); // // CUChar* imageData; // CInt imageSize; // // imageSize = ilGetInteger(IL_IMAGE_WIDTH) * ilGetInteger(IL_IMAGE_HEIGHT) * ilGetInteger(IL_IMAGE_CHANNELS); // imageData = (CUChar*)malloc( imageSize ); // // if( ilGetInteger(IL_IMAGE_CHANNELS) == 3 ) // texObj->SetFormat( TGA_TRUECOLOR_24 ); // else if ( ilGetInteger(IL_IMAGE_CHANNELS) == 4 ) // texObj->SetFormat( TGA_TRUECOLOR_32 ); // // memcpy( imageData , ilGetData() , imageSize ); // texObj->SetImageData( imageData ); // ilDeleteImages(1, &imageId); // } // PrintInfo( "\nCreating Texture '" ); // PrintInfo(pathName, COLOR_RED_GREEN); // PrintInfo( "' "); // // CreateTargaTexture( texObj ); // // return CTrue; //} // CBool CTexture::LoadDDSTexture( CImage * texObj, CChar* name, CChar* sceneFileName, CBool reportError ) { //attache the sceneFileName path( without the dea file ) to the texture name CChar pathName[MAX_NAME_SIZE]; if( sceneFileName ) //To deal with COLLADA files. { CChar * texName = NULL; if( g_useOriginalPathOfDAETextures || g_updateTextureViaEditor) { texName = CNewData(CChar,MAX_NAME_SIZE); Cpy( texName, name ); } else { texName = GetAfterPath( name ); } GetWithoutDot( texName); Append( texName, ".dds" ); //replace %20 with space using std::string std::string s(texName); size_t i = 0; for (;;) { i = s.find("%20", i); if (i == string::npos) { break; } s.replace(i, 3, " "); } if( g_useOriginalPathOfDAETextures || g_updateTextureViaEditor) { s.begin(); size_t i = 0; for (;;) { i = s.find("file:/", i); if (i == string::npos) { break; } s.replace(i, 6, ""); } } strcpy(texName, s.c_str()); if( g_useOriginalPathOfDAETextures || g_updateTextureViaEditor) { strcpy( pathName, texName ); CDelete(texName); } else { CChar g_currentVSceneNameWithoutDot[MAX_NAME_SIZE]; Cpy( g_currentVSceneNameWithoutDot, g_currentVSceneName ); GetWithoutDot( g_currentVSceneNameWithoutDot ); sprintf( pathName, "%s%s%s%s", g_VScenePath, g_currentVSceneNameWithoutDot, "/Textures/", texName ); } } else //To load independent dds files(not specified in a collada file ) strcpy( pathName, name ); ifstream file(pathName, ios::binary); if (! file ) { if( reportError ) { CChar temp[MAX_NAME_SIZE]; sprintf( temp, "\nError! CTexture::LoadDDSTexture > Couldn't load the dds file: '%s'", pathName ); PrintInfo( temp, COLOR_RED ); numErrors += 1; } return false; } CDDS *m_ddsImage = CNew( CDDS ); if (! m_ddsImage->LoadFile(file) ) { if( reportError ) { CChar temp[MAX_NAME_SIZE]; sprintf( temp, "\nError! CTexture::LoadDDSTexture > Couldn't load the dds file: '%s'", pathName ); PrintInfo( temp, COLOR_RED ); numErrors += 1; } return false; } texObj->SetWidth( (CInt32)m_ddsImage->GetWidth() ); texObj->SetHeight( (CInt32)m_ddsImage->GetHeight() ); if( m_ddsImage->m_alphaChannel) texObj->SetFormat(TGA_TRUECOLOR_32); else texObj->SetFormat(TGA_TRUECOLOR_24); PrintInfo( "\nCreating Texture ' " ); PrintInfo(pathName, COLOR_RED_GREEN); PrintInfo( " '"); CreateDDSTexture( texObj, m_ddsImage ); return CTrue; }