Texturing in C

Could someone tell me a link to a page where i can find the sourcecode for a program which
loads an windows bmp file and use it as texture. The sourcecode should be written in C. Don´t send a link to a C++ program, please. I need it in C. If you have such a file at home please mail it to me.
Breier@odn.de
It would be the best if i can have a source code with explanations or a tutorial.
THX
Breier@odn.de

I have sent you some files that may be of help.

Could I get that code too? :slight_smile:

daflip@acxiom.com

And me too please
le_bayonnais@hotmail.com

I have tossed the code you ways. Some of the code is a bit ‘hackish’, but hopefully it helps. Windows bitmaps are not to bad to read. Basically you have your headers, the your color palette (unless it is a 24-bit), and then your data. If it’s an 8 bit bitmap, you have 256 colormap entries and each byte in the image data is a reference into this table. If it’s 4-bit, each byte in the data contains two pixels referencing into a 16 entry colormap (you have to mask out each pixel).
Two cautions:
Bitmaps are stored upside-down.
Each row of bitmap data is 4-byte aligned to the beginning of the data section.
My code only handles 8-bit bitmaps, but expand away!

… I think, I need an example how to use windows-bmp as textures, too :wink: Could you mail me some example ?

Thanx in advance.

Martin

Sorry to keep perstering you. Could I have a copy of this code as well cheers!

Tom.
tmwp197@soton.ac.uk

In my opinion, TGA images are much easier to work with. Windows DIB (BMP) are too proprietary for me, as I like to multi-platform my apps. The RLE encoding of a Windows bitmap is utterly ubsurd in my opinion, as it makes no sense to me what-so-ever. I do have a TGA image reading/writing library I wrote for my apps and the code is GPL’d. It’s platform independant and written in C, but works with C++ as well. It supports 8, 16, 24 and 32-bit RGB(A) and RLE compressed TGA images. Drop me a mail if you’d like a copy.

[This message has been edited by fenris (edited 03-20-2000).]

Originally posted by fenris:
[b]In my opinion, TGA images are much easier to work with. Windows DIB (BMP) are too proprietary for me, as I like to multi-platform my apps. The RLE encoding of a Windows bitmap is utterly ubsurd in my opinion, as it makes no sense to me what-so-ever. I do have a TGA image reading/writing library I wrote for my apps and the code is GPL’d. It’s platform independant and written in C, but works with C++ as well. It supports 8, 16, 24 and 32-bit RGB(A) and RLE compressed TGA images. Drop me a mail if you’d like a copy.

[This message has been edited by fenris (edited 03-20-2000).][/b]
could you send me this TGA reading Library? or tell me where to download? THX puerrom at gmail dot com

http://www.wotsit.org/

seriously. Anyways, what’s the story with the “it has to be C and not C++”? C++ is not THAT obsiscated that translating it is prohibitively difficult!?

instead of sending a dozen emails why didn’t you just post your code? this is my texture class which can load bmp (24bit only… is there really anyone who still uses paltetted bmp?) and tga (no alpha channel yet).

class vxTexture
{
public:

// Constructor/Destructor

vxTexture( char *filename, int format );
~vxTexture() {}

// Variables

char *Filename;
int Format;
unsigned int Id;

unsigned short Width;
unsigned short Height;
unsigned int Channels;
unsigned char Colordepth;

unsigned char *Data;
};

vxTexture::vxTexture( char *filename, int format )
{
// Set values
Filename = filename;
Format = format;
glGenTextures( 1, &Id );

// Bind Image to texture object
glBindTexture( GL_TEXTURE_2D, Id );
	
// Open the file
FILE *File = fopen( Filename, "rb" );

if( Format == vxBMP )
{
// Skip the header
fseek( File, 3*(sizeof(unsigned short)+sizeof(unsigned int)), 0 );

// Read width and length
fread(&Width, sizeof(long), 1, File);
fread(&Height, sizeof(long), 1, File);

// Skip useless info
fseek( File, 2*(sizeof(unsigned short)+sizeof(long))+sizeof(unsigned long)+3*sizeof(unsigned int), 1 );

// Get channel count
Channels = 3;

// Reserve memory for data
Data = new unsigned char[Width*Height*3];

// Read color values
for( unsigned int x = 0; x < Width; x++ )
for( nsigned int y = 0; y < Height; y++ )
for( unsigned int pixel = 0; pixel < 3; pixel++ )
					fread(&Data[(x*Height+y)*3+pixel], sizeof(char), 1, File);
}
else if( Format == vxTGA )
{
// Skip useless info
fseek( File, 12*(sizeof(unsigned char)), 1 ); 

// Read width, length and depth
fread( &Width, sizeof(unsigned short), 1, File );
fread( &Height, sizeof(unsigned short), 1, File );
fread( &Colordepth, sizeof(unsigned char), 1, File );

// Skip useless info
fseek( File, sizeof(unsigned char), 1 ); 

// Get channel count
Channels = Colordepth/8;

// Reserve memory for data
Data = new unsigned char[Channels*Width*Height];

// Read color values
for(int y = 0; y < Height; y++)
{
unsigned char *Line = &(Data[Channels*Width*y]);
fread( Line, Channels*Width, 1, File );
}
}

// Close file
fclose( File );

// Set parameters and build mipmaps
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST );
gluBuild2DMipmaps( GL_TEXTURE_2D, Channels, Width, Height, Channels == 4 ? GL_BGRA : GL_BGR, GL_UNSIGNED_BYTE, Data );

// Free data
delete[] Data;
}

the one who wanted it i c should rather learn who oop works.

The SDL_image library supports many image formats, including bmp and tga. It’s fairly straightforward to load an image of just about any format and pass it to OpenGL for use as a texture.

http://libsdl.org/
http://www.libsdl.org/projects/SDL_image/