// teximage.cpp -- Image data for OpenGL textures // // Written by Frederic Bouvier, started October 2001. // // Copyright (C) 2001 Frederic Bouvier - fredb@users.sourceforge.net // // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License as // published by the Free Software Foundation; either version 2 of the // License, or (at your option) any later version. // // This program is distributed in the hope that it will be useful, but // WITHOUT ANY WARRANTY; without even the implied warranty of // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU // General Public License for more details. // // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. // // $Id: teximage.cpp,v 1.3 2004/02/14 14:17:40 fredb2 Exp $ #include // teximage #include "teximage.hpp" FGSD_TexImage::FGSD_TexImage( FGSD_Image *__image, size_t __comp, size_t __size, size_t __x, size_t __y ) { allocImage( __size, __size ); size_t w = __image->width(); size_t h = __image->height(); size_t x1 = __x * __size; size_t x2 = x1 + __size; if ( w < x2 ) x2 = w; size_t y1 = __y * __size; size_t y2 = y1 + __size; if ( h < y2 ) y2 = h; size_t d = __image->depth(); unsigned char *buf = __image->buffer(); size_t i, j; for ( i = y1; i < y2; i++ ) { for ( j = x1; j < x2; j++ ) { _buffer[ ( ( i - y1 ) * __size + ( j - x1 ) ) * _depth ] = buf[ ( i * w + j ) * d + 0 ]; _buffer[ ( ( i - y1 ) * __size + ( j - x1 ) ) * _depth + 1 ] = buf[ ( i * w + j ) * d + 1 ]; _buffer[ ( ( i - y1 ) * __size + ( j - x1 ) ) * _depth + 2 ] = buf[ ( i * w + j ) * d + 2 ]; } for ( j = x2 - x1; j < __size; j++ ) { _buffer[ ( ( i - y1 ) * __size + j ) * _depth ] = 0; _buffer[ ( ( i - y1 ) * __size + j ) * _depth + 1 ] = 0; _buffer[ ( ( i - y1 ) * __size + j ) * _depth + 2 ] = 0; } } for ( i = y2 - y1; i < __size; i++ ) for ( j = 0; j < __size; j++ ) { _buffer[ ( i * __size + j ) * _depth ] = 0; _buffer[ ( i * __size + j ) * _depth + 1 ] = 0; _buffer[ ( i * __size + j ) * _depth + 2 ] = 0; } glPixelStorei( GL_UNPACK_ALIGNMENT, 1 ); glGenTextures( 1, &_texName ); glBindTexture( GL_TEXTURE_2D, _texName ); GLenum error = glGetError(); 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_NEAREST ); glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST ); glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, __size, __size, 0, GL_RGB, GL_UNSIGNED_BYTE, _buffer ); delete[] _buffer; _buffer = 0; } FGSD_TexImage::~FGSD_TexImage() { glDeleteTextures( 1, &_texName ); }