PDA

View Full Version : RAW Texturing Class



31337
12-02-2002, 06:24 AM
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_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
return 0;
}

Bob
12-02-2002, 06:36 AM
glBindTexture(GL_TEXTURE_2D, *data);

Dude, look up some basic tutorial on textures, NeHe (http://nehe.gamedev.net) for example. http://www.opengl.org/discussion_boards/ubb/biggrin.gif

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

31337
12-02-2002, 08:16 AM
ohhhhhhhhhhhh yahhhhhhhhhhhhhhhh I forgot about that... let me try plugging that in

31337
12-02-2002, 08:25 AM
Ok so I went back and read nehe's tutorial and I fixed it. Wahoo I'm happy that was pretty simple.

31337
12-02-2002, 08:47 AM
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, &amp;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_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,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.

Bob
12-02-2002, 09:40 AM
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.
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.

31337
12-02-2002, 01:26 PM
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

31337
12-02-2002, 01:30 PM
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;

31337
12-02-2002, 01:39 PM
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

Bob
12-02-2002, 02:28 PM
Sounds like you haven't included gl.h at the time you use GLubyte.

31337
12-02-2002, 03:07 PM
Ok I changed c and data to being GLubytes but now my program crashes http://www.opengl.org/discussion_boards/ubb/frown.gif

Bob
12-02-2002, 03:25 PM
Use a debugger and see where it craches.

31337
12-02-2002, 03:46 PM
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 http://www.opengl.org/discussion_boards/ubb/frown.gif. Whats strange though is that it does change the color of the model I'm mapping it to to purple. How bizare.

31337
12-02-2002, 03:48 PM
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, &amp;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_FILTE R,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTE R,GL_LINEAR);
return 0;
}

Miguel_dup1
12-02-2002, 09:41 PM
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...