bool initGL(void) { GLenum glewError = glewInit(); if (glewError != GLEW_OK) { fprintf(stderr, "Error initializing GLEW!\n%s\n", glewGetErrorString(glewError)); return false; } if (!GLEW_VERSION_3_1) { fprintf(stderr, "OpenGL 3.1 not supported!\n"); return false; } glViewport(0.0f, 0.0f, SCREEN_WIDTH, SCREEN_HEIGHT); glClearColor(0.0f, 0.0f, 0.0f, 1.0f); glEnable(GL_TEXTURE_2D); glEnable(GL_BLEND); glDisable(GL_DEPTH_TEST); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); GLenum error = glGetError(); if (error != GL_NO_ERROR) { fprintf(stderr, "Error initializing OpenGL!\n%s\n", gluErrorString(error)); return false; } ilInit(); iluInit(); ilClearColor(255, 255, 255, 000); ILenum ilError = ilGetError(); if (ilError != IL_NO_ERROR) { fprintf(stderr, "Unable to initialize DevIL!\n%s\n", iluErrorString(ilError)); return false; } if (!LFont::initFreetype()) { fprintf(stderr, "Unable to initialize Truetype!\n"); } return true; }
//Load---------------------------------------------------------------------- bool TextureItem::Load( GLuint minFilter, GLuint maxFilter, bool forceMipmap, bool resizeIfNeeded ) { /** NOTE: This is a test of using an archive file. This will need to be modified to allow direct file access, or archived file access. */ ArchiveFile* file = ArchiveManager::GetSingleton().CreateArchiveFile( mImageFileName ); if ( file ) { UInt32 fileSize = file->Length(); unsigned char* buf = new unsigned char [ fileSize ]; if ( !buf ) { delete file; return false; } file->Read( buf, fileSize ); // Load the texture: //**** ilInit(); iluInit(); // Make sure the DevIL version is valid: if ( ilGetInteger( IL_VERSION_NUM ) < IL_VERSION || iluGetInteger( ILU_VERSION_NUM ) < ILU_VERSION ) { // Invalid version... delete file; delete buf; return false; } // Get the decompressed data ILuint imageID; ilGenImages( 1, &imageID ); ilBindImage( imageID ); //if ( ilLoadImage( const_cast< char* >( mImageFileName.c_str() ) ) ) if ( ilLoadL( IL_TYPE_UNKNOWN, buf, fileSize ) ) { // Convert the image to unsigned bytes: ilConvertImage( IL_RGBA, IL_UNSIGNED_BYTE ); // Generate the GL texture glGenTextures( 1, &mTextureID ); glBindTexture( GL_TEXTURE_2D, mTextureID ); mWidth = ilGetInteger( IL_IMAGE_WIDTH ); mHeight = ilGetInteger( IL_IMAGE_HEIGHT ); mOriginalWidth = mWidth; mOriginalHeight = mHeight; // OpenGL will work better with textures that have dimensions // that are a power of 2. If doing a scrolling tile map, then // this is pretty much a necessity. However, there are times // when using a mipmap instead is perfectly fine (ie, when NOT // doing tiles, or in cases where we might be running out of // video memory... if ( resizeIfNeeded && !forceMipmap ) { UInt32 newWidth = mWidth, newHeight = mHeight; if ( !Math::IsPowerOf2( mWidth ) ) { // Find the next power of 2: newWidth = Math::FindNextPowerOf2( mWidth ); } if ( !Math::IsPowerOf2( mHeight ) ) { // Find the next power of 2: newHeight = Math::FindNextPowerOf2( mHeight ); } if ( newWidth != mWidth || newHeight != mHeight ) { // Resize the canvas: ilClearColor( 0, 0, 0, 0 ); iluImageParameter( ILU_PLACEMENT, ILU_UPPER_LEFT ); iluEnlargeCanvas( newWidth, newHeight, ilGetInteger( IL_IMAGE_DEPTH ) ); mWidth = ilGetInteger( IL_IMAGE_WIDTH ); mHeight = ilGetInteger( IL_IMAGE_HEIGHT ); } } // If forcing mipmap generation, or if the size is not a power of 2, // generate as mipmaps if ( forceMipmap || !Math::IsPowerOf2( mWidth ) || !Math::IsPowerOf2( mHeight ) ) { gluBuild2DMipmaps( GL_TEXTURE_2D, ilGetInteger( IL_IMAGE_BPP ), mWidth, mHeight, ilGetInteger( IL_IMAGE_FORMAT ), GL_UNSIGNED_BYTE, ilGetData() ); } else { glTexImage2D( GL_TEXTURE_2D, 0, ilGetInteger( IL_IMAGE_BPP ), mWidth, mHeight, 0, ilGetInteger( IL_IMAGE_FORMAT ), GL_UNSIGNED_BYTE, ilGetData() ); } // Set the minification and magnification filters glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, maxFilter ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP ); mIsLoaded = true; } else { ILenum error; error = ilGetError(); //std::string errString = iluErrorString( error ); } ilDeleteImages( 1, &imageID ); //*** // Free memory: delete buf; delete file; } else return false; return true; }