Jump to content
  • Advertisement
Sign in to follow this  
phil67rpg

OpenGL texturing bricks

This topic is 2630 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am working on a breakout game using opengl and c.I am trying to load a tga file over the bricks in my game.I have done alot of reseach on this topic but I am still a little confused as how to begin.

Share this post


Link to post
Share on other sites
Advertisement

I am working on a breakout game using opengl and c.I am trying to load a tga file over the bricks in my game.I have done alot of reseach on this topic but I am still a little confused as how to begin.


You could use the code below as a tga loader. I used it when I was learning to texture a cube in modern OpenGL so I know it works. I found it over at dreaming code, but don't recall where.




/**
* An uncompressed TGA image loader.
*
* @author Tom Arnold
*/

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

// Some macros to shorten things up.
#define uchar unsigned char
#define sint short int

// Eliminate extraneous calls to sizeof().
const size_t size_uchar = sizeof(uchar);
const size_t size_sint = sizeof(sint);

/**
* This structure holds info about the TGA image before we load it into OpenGL.
*
* @author Tom Arnold
*/
typedef struct
{
uchar depth;
sint w, h;
uchar* data;
} tga_data_t;

/**
* Load a TGA image into an info structure.
*
* @param fn The TGA image to load.
* @return An info structure.
* @author Tom Arnold
*/
tga_data_t* tga_data_load(char* fn)
{
tga_data_t* tga = NULL;
FILE* fh = NULL;
int md, t;

/* Allocate memory for the info structure. */
tga = malloc(sizeof(tga_data_t));

/* Open the file in binary mode. */
fh = fopen(fn, "rb");

/* Problem opening file? */
if (fh == NULL)
{
fprintf(stderr, "Error: problem opening TGA file (%s).\n", fn);
}

else
{
tga = malloc(sizeof(tga_data_t));

// Load information about the tga, aka the header.
{
// Seek to the width.
fseek(fh, 12, SEEK_SET);
fread(&tga->w, size_sint, 1, fh);

// Seek to the height.
fseek(fh, 14, SEEK_SET);
fread(&tga->h, size_sint, 1, fh);

// Seek to the depth.
fseek(fh, 16, SEEK_SET);
fread(&tga->depth, size_sint, 1, fh);
}

// Load the actual image data.
{
// Mode = components per pixel.
md = tga->depth / 8;

// Total bytes = h * w * md.
t = tga->h * tga->w * md;

printf("Reading %d bytes.\n", t);

// Allocate memory for the image data.
tga->data = malloc(size_uchar * t);

// Seek to the image data.
fseek(fh, 18, SEEK_SET);
fread(tga->data, size_uchar, t, fh);

// We're done reading.
fclose(fh);

// Mode 3 = RGB, Mode 4 = RGBA
// TGA stores RGB(A) as BGR(A) so
// we need to swap red and blue.
if (md >= 3)
{
uchar aux;

for (int i = 0; i < t; i+= md)
{
aux = tga->data;
tga->data = tga->data[i + 2];
tga->data[i + 2] = aux;
}
}
}

printf("Loaded texture -> (%s)\nWidth: %d\nHeight: %d\nDepth: %d\n", fn, tga->w, tga->h, tga->depth);
}

return tga;
}

int main(int argc, char** argv)
{
tga_data_t* tex = NULL;

if (argc != 2)
{
printf("Usage: ./tga image.tga\n");
exit(1);
}

// Load an uncompressed TGA file.
tex = tga_data_load(argv[1]);

return EXIT_SUCCESS;
}

Share this post


Link to post
Share on other sites
DevIL is a good choice for reading all sorts of image files
http://www.opengl.org/wiki/Image_Libraries

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!