#include "system.h" // Texture constructor // Creates a new OpenGL texture. Two types are available: // DEFAULT: 32-bit integer ARGB texture; // OPENCL: 128-bit floating point ARGB texture to be used with OpenCL. // ---------------------------------------------------------------------------- GLTexture::GLTexture( unsigned int w, unsigned int h, unsigned int type ) { width = w; height = h; glGenTextures( 1, &id ); glBindTexture( GL_TEXTURE_2D, id ); if (type == DEFAULT) { // regular texture glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_BGR, GL_UNSIGNED_BYTE, 0 ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); } else /* type == FLOAT */ { // texture to be used with OpenCL code float* data = new float[width * height * 4]; memset( data, 0, width * height * 16 ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA32F, width, height, 0, GL_RGBA, GL_FLOAT, data ); } CheckGL(); } // Texture constructor // Loads an image using FreeImage and creates an OpenGL texture for it. // ---------------------------------------------------------------------------- GLTexture::GLTexture( char* fileName ) { GLuint textureType = GL_TEXTURE_2D; glGenTextures( 1, &id ); glBindTexture( textureType, id ); FREE_IMAGE_FORMAT fif = FIF_UNKNOWN; fif = FreeImage_GetFileType( fileName, 0 ); if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename( fileName ); FIBITMAP* tmp = FreeImage_Load( fif, fileName ); if (!tmp) ERRORMESSAGE( fileName, "File not found" ); FIBITMAP* dib = FreeImage_ConvertTo24Bits( tmp ); FreeImage_Unload( tmp ); width = FreeImage_GetWidth( dib ); height = FreeImage_GetHeight( dib ); unsigned int* data = new unsigned int[width * height]; unsigned char* line = new unsigned char[width * 3]; for (unsigned int y = 0; y < height; y++) { memcpy( line, FreeImage_GetScanLine( dib, height - 1 - y ), width * 3 ); for (unsigned int x = 0; x < width; x++) data[y * width + x] = (line[x * 3 + 0] << 16) + (line[x * 3 + 1] << 8) + line[x * 3 + 2]; } FreeImage_Unload( dib ); glTexParameteri( textureType, GL_TEXTURE_MAG_FILTER, GL_NEAREST ); glTexParameteri( textureType, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexImage2D( textureType, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data ); CheckGL(); } // Bind method // Binds the texture for rendering. // ---------------------------------------------------------------------------- void GLTexture::Bind() { glBindTexture( GL_TEXTURE_2D, id ); } // CopyFrom method // Fill texture with surface contents // ---------------------------------------------------------------------------- void GLTexture::CopyFrom( Bitmap* src ) { glBindTexture( GL_TEXTURE_2D, id ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, src->pixels ); } // EOF