RAW Texturing Class

I wrote up this raw image file texturing class but it isn’t working. Whatever I texture with it just comes out completely white. Any ideas on how I need to change it? (by the way my OBJ loader is working for the most part but it only draws every other triangle… wierd…)

void Texture::bindTexture() {
glBindTexture(GL_TEXTURE_2D, *data);
}

int Texture::loadRAW(const char* filename, int x, int y) { // Function for loading raw files
howmuch = 0;
if(FILE *inputfile = fopen(filename,“rb”)) {
// Our first pass will be to decide how much memory to allocate for the image
while((c = fgetc(inputfile))!=EOF) {
howmuch++;
}
data = new unsigned int [howmuch];
fseek(inputfile,0,SEEK_SET); // Reset to the beginning of the file
fread(data,1,howmuch,inputfile); // Read the file into data
xd = x; // Set the dimensions into memory
yd = y;

}
else {
cout << "COULD NOT OPEN FILE: " << filename << endl;
return -1;
}
return 0;
}

int Texture::makeTexture() {
glEnable( GL_TEXTURE_2D );
glGenTextures(1, data);
glBindTexture(GL_TEXTURE_2D, *data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
return 0;
}

glBindTexture(GL_TEXTURE_2D, *data);

Dude, look up some basic tutorial on textures, NeHe for example.

glBindTexture takes a texture ID (instead of the pointer), and you upload the texture data with glTexImage2D.

ohhhhhhhhhhhh yahhhhhhhhhhhhhhhh I forgot about that… let me try plugging that in

Ok so I went back and read nehe’s tutorial and I fixed it. Wahoo I’m happy that was pretty simple.

Doh! I just tried changing the texture image and it died. I knew it was too easy. Well here’s the new code I have:

void Texture::bindTexture() {
glBindTexture(GL_TEXTURE_2D, texturev);
}

int Texture::loadRAW(const char* filename, int x, int y) { // Function for loading raw files
howmuch = 0;
if(FILE *inputfile = fopen(filename,“rb”)) {
// Our first pass will be to decide how much memory to allocate for the image
while((c = fgetc(inputfile))!=EOF) {
howmuch++;
}
data = new unsigned int [howmuch];
fseek(inputfile,0,SEEK_SET); // Reset to the beginning of the file
fread(data,1,howmuch,inputfile); // Read the file into data
xd = x; // Set the dimensions into memory
yd = y;
}
else {
cout << "COULD NOT OPEN FILE: " << filename << endl;
return -1;
}
return 0;
}

int Texture::makeTexture() {
glEnable( GL_TEXTURE_2D );
glGenTextures(1, &texturev);
glBindTexture(GL_TEXTURE_2D, texturev);
glTexImage2D(GL_TEXTURE_2D, 0, 3, xd, yd, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
return 0;
}

Here’s the header file:

#ifndef LOADTEXTURE_H
#define LOADTEXTURE_H

class Texture {
public:
void bindTexture();
int loadRAW(const char* filename, int x, int y); // The filename, the x dimension, and the y dimension
int makeTexture();
private:
unsigned int * data; // the data for the image
int xd; // the x dimension
int yd; // the y dimension
unsigned int c; // simple character buffer
int howmuch; // how much memory to allocate for data
unsigned int texturev; // The storage for the texture
};

#endif

Anybody see where I’m making my mistake? Thanks a lot you guys rock.

Some things I would like to say about the class. None of them should cause a crach though, so you may still have the problem.
[ul][]In loadRAW, you’re counting the number of bytes in the file, but you’re allocating that number of integers. Will use 300% more memory than needed. Make data a GLubyte * instead, and allocate only as much memory as you need.[]Use fseek(inputfile, 0, SEEK_END) and ftell(inputfile) to get the size instead. No need to read through the entire file to get file size…[]… but since you already pass the size of the image as parameters to loadRAW, you can skip this process entirely.[]Once the texture is uploaded, you can delete the pointer to free the memory.[*]You don’t have to enable texturing before uploading the texture (in makeTexture). I would have the glEnable(GL_TEXTURE_2D) in bindTexture() instead. If yo do so, you should also make an unbindTexture() which disables texturing again.[/ul]

Alright thanks bob. I will go ahead and make those changes. I have a couple of questions though. My compiler complains when I make things like GLuint, so instead of using GLuint I just used an unsigned int. I will try to change it to what you’ve suggested. Thanks for your help I really appreciate it

it complains about GLubyte - it says line 10: syntax error: missing ‘;’ before ‘*’ on the header file when I changed it from unsigned int * data; to GLubyte *data;

Ok I made all your changes except those involving memory. Is it because I’m not using GLubyte’s that my texture isn’t drawing? How do I fix the problem with GLubytes? Thanks

Sounds like you haven’t included gl.h at the time you use GLubyte.

Ok I changed c and data to being GLubytes but now my program crashes

Use a debugger and see where it craches.

It was crashing with fgetc(). I realized that I hadn’t done the fseek/ftell thing so I implemented that now it doesn’t crash. However, there still is no texture mapped . Whats strange though is that it does change the color of the model I’m mapping it to to purple. How bizare.

here’s the new code:

void Texture::bindTexture() {
glEnable( GL_TEXTURE_2D );
glBindTexture(GL_TEXTURE_2D, texturev);
}

void Texture::freeTexture() {
delete data;
}

int Texture::loadRAW(const char* filename, int x, int y) { // Function for loading raw files
howmuch = 0;
if(FILE *inputfile = fopen(filename,“rb”)) {
// Our first pass will be to decide how much memory to allocate for the image
fseek(inputfile, 0, SEEK_END); // Go to the end of the file
howmuch = ftell(inputfile);
data = new GLubyte [howmuch];
fseek(inputfile,0,SEEK_SET); // Reset to the beginning of the file
fread(data,1,howmuch,inputfile); // Read the file into data
xd = x; // Set the dimensions into memory
yd = y;
}
else {
cout << "COULD NOT OPEN FILE: " << filename << endl;
return -1;
}
return 0;
}

int Texture::makeTexture() {
glGenTextures(1, &texturev);
glBindTexture(GL_TEXTURE_2D, texturev);
glTexImage2D(GL_TEXTURE_2D, 0, 3, xd, yd, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
return 0;
}

Well dude, I see that in your code you are not closing the file after you are reading the data… You must close the file. e.g. use fclose(FILE *file);

Other than that, I do not see anything else…