NEHE TGA C texture loading ported to c#

Started by
-1 comments, last by Gl_Terminator 12 years, 7 months ago
I ought a lot to NEHE it was my way to start in this world and so I am going to share my C# tga loading code, compressed tga loading its not implemented
this code works

struct TextureImage
{
public byte[] imageData; // Image Data (Up To 32 Bits)
public int bpp; // Image Color Depth In Bits Per Pixel.
public int width; // Image Width
public int height; // Image Height
public int texID; // Texture ID Used To Select A Texture
}


public static bool Compare(byte[] array1, byte[] array2) // this is called Helper.compare
{
int length1 = array1.Length;
int length2 = array2.Length;
if (length1 != length2)
{
return false;
}
// if continue the 2 lenghts are the same
for (int i = 0; i < length1; i++)
{
if (array1 != array2)
{
return false;
}
}
return true;
}

static int LoadTGA(string filename)// this function return the integer texture id
{
byte[] uTGACompare = { 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Uncompressed TGA Header
byte[] cTGACompare = { 0, 0, 10, 0, 0, 0, 0, 0, 0, 0, 0, 0 }; // Compressed TGA Header
byte[] TGACompare = new byte[12]; // Used To Compare TGA Header

FileStream file = new FileStream(filename, FileMode.Open, FileAccess.Read); // Open The TGA File
file.Read(TGACompare, 0, 12);
if (Helper.Compare(TGACompare, uTGACompare))
{
return LoadUncompressedTGA(file, filename);
}
else
{
if (Helper.Compare(TGACompare, uTGACompare))
{
return LoadCompressedTGA(file);
}
}
file.Close();
return 0;
}

/// <summary>
/// This function load an uncompressed TGA
/// </summary>
static int LoadUncompressedTGA(FileStream file, string name)
{
byte[] header = new byte[6]; // First 6 Useful Bytes From The Header
int bytesPerPixel; // Holds Number Of Bytes Per Pixel Used In The TGA File
int imageSize; // Used To Store The Image Size When Setting Aside Ram
int temp; // Temporary Variable
int type = Gl.GL_RGBA; // Set The Default GL Mode To RBGA (32 BPP)

if (file == null || file.Read(header, 0, 6) != 6)
{
if (file == null)
return -1;
else
{
file.Close();
return -1;
}
}
texture.width = header[1] * 256 + header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
texture.height = header[3] * 256 + header[2]; // Determine The TGA Height (highbyte*256+lowbyte)

if (texture.width <= 0 || texture.height <= 0 || (header[4] != 24 && header[4] != 32)) // Is The TGA 24 or 32 Bit?
{
file.Close();
return -1;
}
texture.bpp = header[4]; // Grab The TGA's Bits Per Pixel (24 or 32)
bytesPerPixel = texture.bpp / 8; // Divide By 8 To Get The Bytes Per Pixel
imageSize = texture.width * texture.height * bytesPerPixel; // Calculate The Memory Required For The TGA Data
texture.imageData = new byte[imageSize]; // Reserve Memory To Hold The TGA Data
if (imageSize == 0 || file.Read(texture.imageData, 0, imageSize) != imageSize)
{
if (texture.imageData != null)
texture.imageData = null;

file.Close();
return -1;
}
for (int i = 0; i < imageSize; i += bytesPerPixel) // Loop Through The Image Data
{ // Swaps The 1st And 3rd Bytes ('R'ed and 'B'lue)
temp = texture.imageData; // Temporarily Store The Value At Image Data 'i'
texture.imageData = texture.imageData[i + 2]; // Set The 1st Byte To The Value Of The 3rd Byte
texture.imageData[i + 2] = (byte)temp; // Set The 3rd Byte To The Value In 'temp' (1st Byte Value)
}

file.Close();
// Build A Texture From The Data
int[] textureArray = new int[1];
textureArray[0] = texture.texID;
Gl.glGenTextures(1, textureArray); // Generate OpenGL texture IDs

Gl.glBindTexture(Gl.GL_TEXTURE_2D, textureArray[0]); // Bind Our Texture
if (texture.bpp == 24) // Was The TGA 24 Bits
{
type = Gl.GL_RGB; // If So Set The 'type' To GL_RGB
}

//if the texture is intended to be rendered anisotropically
if (ContentManager.IsAnisotropic(name) && maximumAnisotropy != 0)
{
Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAX_ANISOTROPY_EXT, maximumAnisotropy); // anisotropic Filtered
}

Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MIN_FILTER, Gl.GL_LINEAR); // Linear Filtered
Gl.glTexParameterf(Gl.GL_TEXTURE_2D, Gl.GL_TEXTURE_MAG_FILTER, Gl.GL_LINEAR); // Linear Filtered
Glu.gluBuild2DMipmaps(Gl.GL_TEXTURE_2D, 4, texture.width, texture.height, Gl.GL_RGBA, Gl.GL_UNSIGNED_BYTE, texture.imageData);


texture.texID = textureArray[0];
return texture.texID;
}

This topic is closed to new replies.

Advertisement