Archived

This topic is now archived and is closed to further replies.

Probably Newbie Texture Question

This topic is 5651 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 just got started programming OpenGL a couple days ago and have been messing around with texture mapping lately. However, every time i try to load a 512x512 and about 50% of the 256x256 pixel bitmaps that I try to load into the program cause it to crash. Everything compiles and builds with no errors, but I crash when I try to run the program. It''s just a textured half cubey looking thing. Here''s the relevant code if it helps, though I''m suspecting some kind of memory limit that i''m not aware of. Thanks in advance! // load the land texture data landTexture = LoadBitmapFile("texture.bmp", &landInfo); if (!landTexture) return false; // generate the land texture as a mipmap glGenTextures(1, &land); glBindTexture(GL_TEXTURE_2D, land); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST); gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, landInfo.biWidth, landInfo.biHeight, GL_RGB, GL_UNSIGNED_BYTE, landTexture);

Share this post


Link to post
Share on other sites
shouldnt the 2nd parameter be 3 (3 components) not GL_RGB?

the GF3 should be able to handle textures up to 2048x2048 afaik


just looked:
#define GL_RGB 0x1907
(from the windows gl.h)



mfg Phreak
--
"Input... need input!" - Johnny Five, Short Circuit.

Share this post


Link to post
Share on other sites
you are using mipmaps, then you should specify a mipmap min filter. replace the GL_NEAREST in
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);

with one of the following

GL_NEAREST_MIPMAP_NEAREST
GL_LINEAR_MIPMAP_NEAREST
GL_NEAREST_MIPMAP_LINEAR
GL_LINEAR_MIPMAP_LINEAR

and the GL_RGB as 2nd component are fine. in some older helpfiles the 2nd component are described as ''components'' counter, ie houb many components there are, and in the newer (i think its related to the 1.1 spec) its described as ''internal format'' where GL_RGB is the driver default setting, and GL_RGB4 = 16 bit, and GL_RGB8 = 24bit internal format. The driver dont HAVE to listen to this, it''s a hint, but as far a i know the nvidia cards do listen to it. (using GL_RGB4 to save gfxcard-mem when the image can cope with it, ie. not much gradients in it)

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
how do you have [ land ] defined

i think it needs to be GLuint land[1];

Share this post


Link to post
Share on other sites
the 3 suggestions aint correct

the problem prolly lies in this function here

landTexture = LoadBitmapFile("texture.bmp", &landInfo);
youre most likely not allocating enuf memory or returning a temporary

http://uk.geocities.com/sloppyturds/gotterdammerung.html

Share this post


Link to post
Share on other sites
quote:
Original post by LousyPhreak
shouldnt the 2nd parameter be 3 (3 components) not GL_RGB?



OpenGL 1.0 only accepts "3" or "4" as a 2nd parameter. With opengl 1.1 you can use either "3" or "4" or 32(If I remember correctly) constants (GL_RGB, GL_RGBA, ...) Check the docs for more details.

-Lev

Share this post


Link to post
Share on other sites
Well, the code says... if not loaded properly then quit program (or crash or whatever)

Maybe you made the old VC++ (are you using that?) mistake that your program is compiled in a /debug/ directory. This means that your textures need to be located there as well.

Don''t know if this is your problem or not but many beginners make this mistake (I did once as well.... long time ago though )

Sander Maréchal
[Lone Wolves Production][Articles][E-mail]

Share this post


Link to post
Share on other sites
Well I appreciate all the help, and as it stands now, changing that second parameter to GL_RBG8 allowed SOME of the 512 textures to load, but not all of them. go figure. I also changed land from just unsigned int to GLuint which didn''t seem to change anything. I also changed GL_NEAREST to GL_NEAREST_MIPMAP_LINEAR as suggested, no change. as for the compile problem from the debug folder thing, if that was the problem it "should" affect the program for every single texture that I try, or at least that''s how I understand it, and finally, my loadbitmapFile function is pulled straight from the OpenGL game book and it should allocate as much memory as is needed by the texture. here''s a copy. but anyway, thanks for all the support, I got more replies faster than anywhere else I''ve ever posted.

// LoadBitmapFile
// desc: Returns a pointer to the bitmap image of the bitmap specified
// by filename. Also returns the bitmap header information.
// No support for 8-bit bitmaps.
unsigned char *LoadBitmapFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
FILE *filePtr; // the file pointer
BITMAPFILEHEADER bitmapFileHeader; // bitmap file header
unsigned char *bitmapImage; // bitmap image data
int imageIdx = 0; // image index counter
unsigned char tempRGB; // swap variable

// open filename in "read binary" mode
filePtr = fopen(filename, "rb");
if (filePtr == NULL)
return NULL;

// read the bitmap file header
fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);

// verify that this is a bitmap by checking for the universal bitmap id
if (bitmapFileHeader.bfType != BITMAP_ID)
{
fclose(filePtr);
return NULL;
}

// read the bitmap information header
fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

// move file pointer to beginning of bitmap data
fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

// allocate enough memory for the bitmap image data
bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

// verify memory allocation
if (!bitmapImage)
{
free(bitmapImage);
fclose(filePtr);
return NULL;
}

// read in the bitmap image data
fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);

// make sure bitmap image data was read
if (bitmapImage == NULL)
{
fclose(filePtr);
return NULL;
}

// swap the R and B values to get RGB since the bitmap color format is in BGR
for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
{
tempRGB = bitmapImage[imageIdx];
bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
bitmapImage[imageIdx + 2] = tempRGB;
}

// close the file and return the bitmap image data
fclose(filePtr);
return bitmapImage;
}

Share this post


Link to post
Share on other sites
changing the second parameter to GL_RGB8 is wrong!

change it to 3 because this is the number of components you have per pixel (r,g,b) or 4 if you also have an alpha channel!!!



mfg Phreak
--
"Input... need input!" - Johnny Five, Short Circuit.

Share this post


Link to post
Share on other sites
I think you are having the same problem I have (also using OGL Game Programming bitmap code). It''s because the textures that are to be loaded using that function have to be 24 bit or else it will crash. So just open them in Paint and save them as 24 bit images. That is at least how I solved it but there are surely plenty of better solutions.

Share this post


Link to post
Share on other sites