texturing bricks

Started by
2 comments, last by V-man 12 years, 6 months ago
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.
Advertisement
Developers image library.

NBA2K, Madden, Maneater, Killing Floor, Sims http://www.pawlowskipinball.com/pinballeternal


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;
}
Good judgment comes from experience; experience comes from bad judgment.
DevIL is a good choice for reading all sorts of image files
http://www.opengl.org/wiki/Image_Libraries
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

This topic is closed to new replies.

Advertisement