Sign in to follow this  
AdamGL

OpenGL TGA fiiles

Recommended Posts

Sorry, I just can't seem to get it. I have Beginning OpenGL Game programming the book, but I can't seem to grasp the TGA loading part. Can anyone post a simple bit of code that loads a tga, with no linked lists. I would appreciate it. And if you could explain each part and what it does. I'm a newbie so please don't slap me for asking this. Adam L

Share this post


Link to post
Share on other sites
Sure jake_Ghost, if you wouldn't mind also including descriptions of what certain things do in it that would be great!

Share this post


Link to post
Share on other sites
Alight if you guys could help me debug that would be great. What happens is I get a bunch of verical bars. Multicolored bars. Hippi bars. Anyways, I'll post the code. Its long so bear with me please.

tga.h

#include <windows.h> // Standard Windows header
#include <stdio.h> // Standard Header For File I/O
#include <gl\gl.h> // Standard Header For OpenGL
#include <gl\glu.h>

typedef struct
{
GLubyte* imageData; // Hold All The Color Values For The Image.
GLuint bpp; // Hold The Number Of Bits Per Pixel.
GLuint width; // The Width Of The Entire Image.
GLuint height; // The Height Of The Entire Image.
GLuint texID; // Texture ID For Use With glBindTexture.
GLuint type; // Data Stored In * ImageData (GL_RGB Or GL_RGBA)
} Texture;

typedef struct
{
GLubyte Header[12]; // File Header To Determine File Type
} TGAHeader;

typedef struct
{
GLubyte header[6]; // Holds The First 6 Useful Bytes Of The File
GLuint bytesPerPixel; // Number Of BYTES Per Pixel (3 Or 4)
GLuint imageSize; // Amount Of Memory Needed To Hold The Image
GLuint type; // The Type Of Image, GL_RGB Or GL_RGBA
GLuint Height; // Height Of Image
GLuint Width; // Width Of Image
GLuint Bpp; // Number Of BITS Per Pixel (24 Or 32)
} TGA;

TGAHeader tgaheader; // Used To Store Our File Header
TGA tga; // Used To Store File Information

// Uncompressed TGA Header
GLubyte uTGAcompare[12] = {0,0, 2,0,0,0,0,0,0,0,0,0};
// Compressed TGA Header
GLubyte cTGAcompare[12] = {0,0,10,0,0,0,0,0,0,0,0,0};

// Load An Uncompressed File
bool LoadUncompressedTGA(Texture *, char *, FILE *);
// Load A Compressed File
bool LoadCompressedTGA(Texture *, char *, FILE *);

//load a tga file
bool LoadTGA(Texture * texture, char * filename)
{
FILE * fTGA; // Declare File Pointer
fTGA = fopen(filename, "rb"); // Open File For Reading

if(fTGA == NULL) // If Here Was An Error
{

return false; // Return False
}

if(fread(&tgaheader, sizeof(TGAHeader), 1, fTGA) == 0)
{

return false; // Return False If It Fails
}

// If The File Header Matches The Uncompressed Header
if(memcmp(uTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)
{
// Load An Uncompressed TGA
LoadUncompressedTGA(texture, filename, fTGA);
}
// If The File Header Matches The Compressed Header
else if(memcmp(cTGAcompare, &tgaheader, sizeof(tgaheader)) == 0)
{
// Load A Compressed TGA
LoadCompressedTGA(texture, filename, fTGA);
}
else // If It Doesn't Match Either One
{
fclose(fTGA);
return false; // Return False
}
}

// Load An Uncompressed TGA!
bool LoadUncompressedTGA(Texture * texture, char * filename, FILE * fTGA)
{
// Attempt To Read Next 6 Bytes
if(fread(tga.header, sizeof(tga.header), 1, fTGA) == 0)
{

return false; // Return False
}

texture->width = tga.header[1] * 256 + tga.header[0]; // Calculate Height
texture->height = tga.header[3] * 256 + tga.header[2]; // Calculate The Width
texture->bpp = tga.header[4]; // Calculate Bits Per Pixel
tga.Width = texture->width; // Copy Width Into Local Structure
tga.Height = texture->height; // Copy Height Into Local Structure
tga.Bpp = texture->bpp; // Copy Bpp Into Local Structure

// Make Sure All Information Is Valid
if((texture->width <= 0) || (texture->height <= 0) || ((texture->bpp != 24) && (texture->bpp !=32)))
{

return false; // Return False
}

if(texture->bpp == 24) // Is It A 24bpp Image?
texture->type = GL_RGB; // If So, Set Type To GL_RGB
else // If It's Not 24, It Must Be 32
texture->type = GL_RGBA; // So Set The Type To GL_RGBA

tga.bytesPerPixel = (tga.Bpp / 8); // Calculate The BYTES Per Pixel
// Calculate Memory Needed To Store Image
tga.imageSize = (tga.bytesPerPixel * tga.Width * tga.Height);

// Allocate Memory
texture->imageData = (GLubyte *)malloc(tga.imageSize);
if(texture->imageData == NULL) // Make Sure It Was Allocated Ok
{

return false; // If Not, Return False
}

// Attempt To Read All The Image Data
if(fread(texture->imageData, 1, tga.imageSize, fTGA) != tga.imageSize)
{

return false; // If We Cant, Return False
}

// Start The Loop
for(GLuint cswap = 0; cswap < (int)tga.imageSize; cswap += tga.bytesPerPixel)
{
// 1st Byte XOR 3rd Byte XOR 1st Byte XOR 3rd Byte
texture->imageData[cswap] ^= texture->imageData[cswap+2] ^=
texture->imageData[cswap] ^= texture->imageData[cswap+2];
}

fclose(fTGA); // Close The File
return true; // Return Success
}

bool LoadCompressedTGA(Texture * texture, char * filename, FILE * fTGA)
{
if(fread(tga.header, sizeof(tga.header), 1, fTGA) == 0) // Attempt to read header
{
MessageBox(NULL, "Could not read info header", "ERROR", MB_OK); // Display Error
if(fTGA != NULL) // If file is open
{
fclose(fTGA); // Close it
}
return false; // Return failed
}

texture->width = tga.header[1] * 256 + tga.header[0]; // Determine The TGA Width (highbyte*256+lowbyte)
texture->height = tga.header[3] * 256 + tga.header[2]; // Determine The TGA Height (highbyte*256+lowbyte)
texture->bpp = tga.header[4]; // Determine Bits Per Pixel
tga.Width = texture->width; // Copy width to local structure
tga.Height = texture->height; // Copy width to local structure
tga.Bpp = texture->bpp; // Copy width to local structure

if((texture->width <= 0) || (texture->height <= 0) || ((texture->bpp != 24) && (texture->bpp !=32))) //Make sure all texture info is ok
{
MessageBox(NULL, "Invalid texture information", "ERROR", MB_OK); // If it isnt...Display error
if(fTGA != NULL) // Check if file is open
{
fclose(fTGA); // Ifit is, close it
}
return false; // Return failed
}

tga.bytesPerPixel = (tga.Bpp / 8); // Compute BYTES per pixel
tga.imageSize = (tga.bytesPerPixel * tga.Width * tga.Height); // Compute amout of memory needed to store image
texture->imageData = (GLubyte *)malloc(tga.imageSize); // Allocate that much memory

if(texture->imageData == NULL) // If it wasnt allocated correctly..
{
MessageBox(NULL, "Could not allocate memory for image", "ERROR", MB_OK); // Display Error
fclose(fTGA); // Close file
return false; // Return failed
}

GLuint pixelcount = tga.Height * tga.Width; // Nuber of pixels in the image
GLuint currentpixel = 0; // Current pixel being read
GLuint currentbyte = 0; // Current byte
GLubyte * colorbuffer = (GLubyte *)malloc(tga.bytesPerPixel); // Storage for 1 pixel

do
{
GLubyte chunkheader = 0; // Storage for "chunk" header

if(fread(&chunkheader, sizeof(GLubyte), 1, fTGA) == 0) // Read in the 1 byte header
{
MessageBox(NULL, "Could not read RLE header", "ERROR", MB_OK); // Display Error
if(fTGA != NULL) // If file is open
{
fclose(fTGA); // Close file
}
if(texture->imageData != NULL) // If there is stored image data
{
free(texture->imageData); // Delete image data
}
return false; // Return failed
}

if(chunkheader < 128) // If the ehader is < 128, it means the that is the number of RAW color packets minus 1
{ // that follow the header
chunkheader++; // add 1 to get number of following color values
for(short counter = 0; counter < chunkheader; counter++) // Read RAW color values
{
if(fread(colorbuffer, 1, tga.bytesPerPixel, fTGA) != tga.bytesPerPixel) // Try to read 1 pixel
{
MessageBox(NULL, "Could not read image data", "ERROR", MB_OK); // IF we cant, display an error

if(fTGA != NULL) // See if file is open
{
fclose(fTGA); // If so, close file
}

if(colorbuffer != NULL) // See if colorbuffer has data in it
{
free(colorbuffer); // If so, delete it
}

if(texture->imageData != NULL) // See if there is stored Image data
{
free(texture->imageData); // If so, delete it too
}

return false; // Return failed
}
// write to memory
texture->imageData[currentbyte ] = colorbuffer[2]; // Flip R and B vcolor values around in the process
texture->imageData[currentbyte + 1 ] = colorbuffer[1];
texture->imageData[currentbyte + 2 ] = colorbuffer[0];

if(tga.bytesPerPixel == 4) // if its a 32 bpp image
{
texture->imageData[currentbyte + 3] = colorbuffer[3]; // copy the 4th byte
}

currentbyte += tga.bytesPerPixel; // Increase thecurrent byte by the number of bytes per pixel
currentpixel++; // Increase current pixel by 1

if(currentpixel > pixelcount) // Make sure we havent read too many pixels
{
MessageBox(NULL, "Too many pixels read", "ERROR", NULL); // if there is too many... Display an error!

if(fTGA != NULL) // If there is a file open
{
fclose(fTGA); // Close file
}

if(colorbuffer != NULL) // If there is data in colorbuffer
{
free(colorbuffer); // Delete it
}

if(texture->imageData != NULL) // If there is Image data
{
free(texture->imageData); // delete it
}

return false; // Return failed
}
}
}
else // chunkheader > 128 RLE data, next color reapeated chunkheader - 127 times
{
chunkheader -= 127; // Subteact 127 to get rid of the ID bit
if(fread(colorbuffer, 1, tga.bytesPerPixel, fTGA) != tga.bytesPerPixel) // Attempt to read following color values
{
MessageBox(NULL, "Could not read from file", "ERROR", MB_OK); // If attempt fails.. Display error (again)

if(fTGA != NULL) // If thereis a file open
{
fclose(fTGA); // Close it
}

if(colorbuffer != NULL) // If there is data in the colorbuffer
{
free(colorbuffer); // delete it
}

if(texture->imageData != NULL) // If thereis image data
{
free(texture->imageData); // delete it
}

return false; // return failed
}

for(short counter = 0; counter < chunkheader; counter++) // copy the color into the image data as many times as dictated
{ // by the header
texture->imageData[currentbyte ] = colorbuffer[2]; // switch R and B bytes areound while copying
texture->imageData[currentbyte + 1 ] = colorbuffer[1];
texture->imageData[currentbyte + 2 ] = colorbuffer[0];

if(tga.bytesPerPixel == 4) // If TGA images is 32 bpp
{
texture->imageData[currentbyte + 3] = colorbuffer[3]; // Copy 4th byte
}

currentbyte += tga.bytesPerPixel; // Increase current byte by the number of bytes per pixel
currentpixel++; // Increase pixel count by 1

if(currentpixel > pixelcount) // Make sure we havent written too many pixels
{
MessageBox(NULL, "Too many pixels read", "ERROR", NULL); // if there is too many... Display an error!

if(fTGA != NULL) // If there is a file open
{
fclose(fTGA); // Close file
}

if(colorbuffer != NULL) // If there is data in colorbuffer
{
free(colorbuffer); // Delete it
}

if(texture->imageData != NULL) // If there is Image data
{
free(texture->imageData); // delete it
}

return false; // Return failed
}
}
}
}

while(currentpixel < pixelcount); // Loop while there are still pixels left
fclose(fTGA); // Close the file
return true; // return success
}



Stuff to initialize once

Texture texture;
glEnable(GL_TEXTURE_2D); // Enable Texture Mapping ( NEW )
glShadeModel(GL_SMOOTH); // Enable Smooth Shading
glClearColor(0.0f, 0.0f, 0.0f, 0.5f); // Black Background
glClearDepth(1.0f); // Depth Buffer Setup
glEnable(GL_DEPTH_TEST); // Enables Depth Testing
glDepthFunc(GL_LEQUAL); // The Type Of Depth Testing To Do
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations
LoadTGA(&texture, "lightsaber.tga");

glGenTextures(1, &texture.texID); // Create The Texture
// Typical Texture Generation Using Data From The Bitmap
glBindTexture(GL_TEXTURE_2D, texture.texID);
glTexImage2D(GL_TEXTURE_2D, 0, 3, texture.width, texture.height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture.imageData);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);



stuff in loop

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
glLoadIdentity();
glBindTexture(GL_TEXTURE_2D, texture.texID);
glBegin(GL_QUADS);
glTexCoord2f(0.0f, 0.0f); glVertex3f(-1.0f, -1.0f, 1.0f); // Bottom Left Of The Texture and Quad
glTexCoord2f(1.0f, 0.0f); glVertex3f( 1.0f, -1.0f, 1.0f); // Bottom Right Of The Texture and Quad
glTexCoord2f(1.0f, 1.0f); glVertex3f( 1.0f, 1.0f, 1.0f); // Top Right Of The Texture and Quad
glTexCoord2f(0.0f, 1.0f); glVertex3f(-1.0f, 1.0f, 1.0f); // Top Left Of The Texture and Quad
glEnd();
SwapBuffers(hDC);



I'm not in a rush so take as long as you need to reply, if you reply at all. Sorry about all this.

Share this post


Link to post
Share on other sites
Quote:
Original post by AdamGL
What happens is I get a bunch of verical bars. Multicolored bars. Hippi bars.

Does the problem happen with all .tga textures you try, or just with some of them? More specifically... do you get the same effect with both uncompressed and compressed textures, or just with one of these types..?

Share this post


Link to post
Share on other sites
thanks man, that was a big waste of space up there. It shows up, the same type of tga uncompressed and 32 bit. How do I make a proper tga file. What aree my limitations?

Share this post


Link to post
Share on other sites
Wait, I found out why. My image was a 32 bit image. The others were 24 bit. Why can't I load the 32 bit one?

Share this post


Link to post
Share on other sites
Quote:
Original post by AdamGL
Wait, I found out why. My image was a 32 bit image. The others were 24 bit. Why can't I load the 32 bit one?

Because when you generate the texture, you specifically tell openGL it has only RGB channels, not RGBA ^^

// Typical Texture Generation Using Data From The Bitmap
glTexImage2D(GL_TEXTURE_2D, 0, 3, texture.width, texture.height, 0, GL_RGB, GL_UNSIGNED_BYTE, texture.imageData);

It has hard-coded 3 channels of colour data and RGB format. Yo'll have to modify that call to something like:


glTexImage2D(GL_TEXTURE_2D, 0, texture.type, texture.width, texture.height, 0, texture.type, GL_UNSIGNED_BYTE, texture.imageData);

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this  

  • Forum Statistics

    • Total Topics
      627718
    • Total Posts
      2978793
  • Similar Content

    • By DelicateTreeFrog
      Hello! As an exercise for delving into modern OpenGL, I'm creating a simple .obj renderer. I want to support things like varying degrees of specularity, geometry opacity, things like that, on a per-material basis. Different materials can also have different textures. Basic .obj necessities. I've done this in old school OpenGL, but modern OpenGL has its own thing going on, and I'd like to conform as closely to the standards as possible so as to keep the program running correctly, and I'm hoping to avoid picking up bad habits this early on.
      Reading around on the OpenGL Wiki, one tip in particular really stands out to me on this page:
      For something like a renderer for .obj files, this sort of thing seems almost ideal, but according to the wiki, it's a bad idea. Interesting to note!
      So, here's what the plan is so far as far as loading goes:
      Set up a type for materials so that materials can be created and destroyed. They will contain things like diffuse color, diffuse texture, geometry opacity, and so on, for each material in the .mtl file. Since .obj files are conveniently split up by material, I can load different groups of vertices/normals/UVs and triangles into different blocks of data for different models. When it comes to the rendering, I get a bit lost. I can either:
      Between drawing triangle groups, call glUseProgram to use a different shader for that particular geometry (so a unique shader just for the material that is shared by this triangle group). or
      Between drawing triangle groups, call glUniform a few times to adjust different parameters within the "master shader", such as specularity, diffuse color, and geometry opacity. In both cases, I still have to call glBindTexture between drawing triangle groups in order to bind the diffuse texture used by the material, so there doesn't seem to be a way around having the CPU do *something* during the rendering process instead of letting the GPU do everything all at once.
      The second option here seems less cluttered, however. There are less shaders to keep up with while one "master shader" handles it all. I don't have to duplicate any code or compile multiple shaders. Arguably, I could always have the shader program for each material be embedded in the material itself, and be auto-generated upon loading the material from the .mtl file. But this still leads to constantly calling glUseProgram, much more than is probably necessary in order to properly render the .obj. There seem to be a number of differing opinions on if it's okay to use hundreds of shaders or if it's best to just use tens of shaders.
      So, ultimately, what is the "right" way to do this? Does using a "master shader" (or a few variants of one) bog down the system compared to using hundreds of shader programs each dedicated to their own corresponding materials? Keeping in mind that the "master shaders" would have to track these additional uniforms and potentially have numerous branches of ifs, it may be possible that the ifs will lead to additional and unnecessary processing. But would that more expensive than constantly calling glUseProgram to switch shaders, or storing the shaders to begin with?
      With all these angles to consider, it's difficult to come to a conclusion. Both possible methods work, and both seem rather convenient for their own reasons, but which is the most performant? Please help this beginner/dummy understand. Thank you!
    • By JJCDeveloper
      I want to make professional java 3d game with server program and database,packet handling for multiplayer and client-server communicating,maps rendering,models,and stuffs Which aspect of java can I learn and where can I learn java Lwjgl OpenGL rendering Like minecraft and world of tanks
    • By AyeRonTarpas
      A friend of mine and I are making a 2D game engine as a learning experience and to hopefully build upon the experience in the long run.

      -What I'm using:
          C++;. Since im learning this language while in college and its one of the popular language to make games with why not.     Visual Studios; Im using a windows so yea.     SDL or GLFW; was thinking about SDL since i do some research on it where it is catching my interest but i hear SDL is a huge package compared to GLFW, so i may do GLFW to start with as learning since i may get overwhelmed with SDL.  
      -Questions
      Knowing what we want in the engine what should our main focus be in terms of learning. File managements, with headers, functions ect. How can i properly manage files with out confusing myself and my friend when sharing code. Alternative to Visual studios: My friend has a mac and cant properly use Vis studios, is there another alternative to it?  
    • By ferreiradaselva
      Both functions are available since 3.0, and I'm currently using `glMapBuffer()`, which works fine.
      But, I was wondering if anyone has experienced advantage in using `glMapBufferRange()`, which allows to specify the range of the mapped buffer. Could this be only a safety measure or does it improve performance?
      Note: I'm not asking about glBufferSubData()/glBufferData. Those two are irrelevant in this case.
    • By xhcao
      Before using void glBindImageTexture(    GLuint unit, GLuint texture, GLint level, GLboolean layered, GLint layer, GLenum access, GLenum format), does need to make sure that texture is completeness. 
  • Popular Now