I am trying to add texture to a glutSolidDodecahedron. Currently experiencing and access violation in the glTexImage2D. I think I am either not reading the bitmaps in correctly or perhaps they have to be translated somehow instead of sent directly to the image pointers…
System information:
Windows 2000 Prof
Visual C++ 6.0
This is an addition to existing code that works correctly. In other words I know all the OpenGL is properly configured with the C++ IDE.
Here is the relavent code
for (int i = 0;i<13;i++) {
Img[i] = auxDIBImageLoad(Names[i]);
glGenTextures(13, &ImgText[2]);
glBindTexture(GL_TEXTURE_2D, ImgText[i]);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
//ERROR OCCURS HERE —>
glTexImage2D(GL_TEXTURE_2D, 0, 3, Img[i]->sizeX, Img[i]->sizeY, 0, GL_RGB, GL_UNSIGNED_BYTE, Img[i]->data);
}
This line
glGenTextures(13, &ImgText[2]);
You should delete it and write this out of the loop:
glGenTextures(13, &ImgText[0]);
or write this inside the loop:
glGenTextures(1, &ImgText[i]);
that’s because, in your code, every time the loop is executed, you try to allocate the space for 13 “textures” in ImgText starting from the 3rd position of your array (that has only 12 positions)…
Good luck!
Well I moved some stuff around. I found out it didn’t like the [0] element either so I made the arrays size 14 and use 1-13.
I also noticed the problem with glGenTextures(13, &ImgText[12]); and corrected it.
the new code looks more like this:
void ClearTexture() //Added this but it doesn’t help
{ //Memory is food for froo and he eats like a fat guy at a buffet!!
if (Img) // If Texture Exists
{
if (Img->data) // If Texture Image Exists
{
free(Img->data); // Free The Texture Image Memory
}
This works but it turns out to be a huge memory hog. When I change the camera position or just about anytime the display is re-rendered it starts sucking up memory.
Will probably turn in what I have for the class, it does meet the specs, But I would like to know what I could do to plug these memory leaks.
Mike,
in order to use the glaux.h include you have to add the glaux.lib to the project settings in Visual C++.
Project->settings->link->object/library modules—add glaux.lib
if you don’t it will give you a link error when you try to compile
Is this stuff inside your drawing routine? If it’s so, it leaks a lot of memory just because you’re loading the texture each time you draw the scene!
Try to do all the getImage() you need to load the textures during initialization, then in your drawing routine, before calling glutSolidDodecahedron(), simply bind the texture by its number…
I suggest you to make these changes to your code:
//first of all you need an ImgText array declared this way:
GLuint ImgText[14]; //since you want to use 1-13 positions
//call getImage once per texture in your init function
//then in your drawing routine
glTranslatef(0,0,-2.75);
glShadeModel(GL_SMOOTH);
glBindTexture(GL_TEXTURE_2D,ImgText[3]; //instead of getImage(3)
glutSolidDodecahedron();
Doing so you load your textures once when your program starts, then you simply bind them when you have to use them.