PDA

View Full Version : Texturing in C



Mephisto
03-01-2000, 12:02 AM
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

nickels
03-01-2000, 02:25 PM
I have sent you some files that may be of help.

Talon
03-08-2000, 08:24 PM
Could I get that code too? :-)

daflip@acxiom.com

Le Bayonnais
03-09-2000, 11:58 PM
And me too please
le_bayonnais@hotmail.com

nickels
03-10-2000, 06:44 PM
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!

martho
03-16-2000, 04:02 PM
.. I think, I need an example how to use windows-bmp as textures, too ;-) Could you mail me some example ?

Thanx in advance.

Martin

tmwp197
03-20-2000, 05:14 AM
Sorry to keep perstering you. Could I have a copy of this code as well cheers!

Tom.
tmwp197@soton.ac.uk
http://www.opengl.org/discussion_boards/ubb/cool.gif

fenris
03-20-2000, 08:17 AM
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. http://www.opengl.org/discussion_boards/ubb/cool.gif

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

puerrom
09-17-2005, 02:35 PM
Originally posted by fenris:
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. http://www.opengl.org/discussion_boards/ubb/cool.gif

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

john
09-18-2005, 03:09 PM
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!?

Vexator
09-18-2005, 11:18 PM
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(&amp;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( &amp;Width, sizeof(unsigned short), 1, File );
fread( &amp;Height, sizeof(unsigned short), 1, File );
fread( &amp;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 = &amp;(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.

jasso
09-22-2005, 08:00 PM
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/