Skip to main content
gamedev.net
🔒 Locked

texture function problems

Started by RichRoberts Aug 19, 2011 at 4:20 AM 5 replies 1.5k views
Original Post
RichRoberts
RichRoberts
im trying to add another texture into my scene everything ok apart from the start when i go to compile i get error C2374: 'texName' : redefinition; multiple initialization, but when i change it to texName 2 it crashes on me, below is a sample of the code im having an issue with

#define FLOOR 0
#define DOOR 1

struct Image
{
unsigned long size_x;
unsigned long size_y;
char *data;
};

typedef struct Image Image;

const int texNum = 2;

Image texData[texNum];
GLuint theTexture[texNum];


char* texName[texNum] = {"floor.bmp"};
char* texName[texNum] = {"door.bmp"};
Hodgman
Hodgman
This isn't to do with open GL or textures -- it's a question about C array syntax.

You're getting that compile error because you've made two variables of the same name.

You're getting the crash (when you rename the duplicate variable), because your original variable is an array of two '[font="Courier New"]char*[/font]'s, but you've only put one item into the array -- at a later point in time when you try to access the 2nd item, you'll get garbage, and when you use that garbage as a pointer, it will likely crash.

I'm guessing you want:char* texName[texNum] = {"floor.bmp", "door.bmp"};
RichRoberts
RichRoberts
thanks for that bit but its still crashing and im wondering if it has to do with this part of the code

for(int k=0; k < texNum; k++)
{
if(!GLloader(texName[k], &texData[k]))
exit(1);

glGenTextures(2, &theTexture[k]);
glBindTexture(GL_TEXTURE_2D, theTexture[k]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texData[k].size_x, texData[k].size_y, GL_RGB, GL_UNSIGNED_BYTE, texData[0].data);
}

for(int d=0; d < texNum; d++)
{
if(!GLloader(texName[d], &texData[d]))
exit(1);

glGenTextures(2, &theTexture[d]);
glBindTexture(GL_TEXTURE_2D, theTexture[d]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texData[d].size_x, texData[d].size_y, GL_RGB, GL_UNSIGNED_BYTE, texData[d].data);
}
}
L. Spiro
L. Spiro
Firstly you don’t need two for loops.

[color="#1C2837"] for(int k=0; k < texNum; k++)
{
if(!GLloader(texName[k], &texData[k]))
exit(1);

glGenTextures(2, &theTexture[k]);
glBindTexture(GL_TEXTURE_2D, theTexture[k]);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_NEAREST);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3, texData[k].size_x, texData[k].size_y, GL_RGB, GL_UNSIGNED_BYTE, texData[0].data);
}


Second for loop serves no purpose except to create a resource leak.


Secondly, you never called glTexImage2D, and it is crashing when you build mipmaps on a blank image.


L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid
Brother Bob
Brother Bob

Secondly, you never called glTexImage2D, and it is crashing when you build mipmaps on a blank image.

What do you mean? He's using gluBuild2DMipmaps which loads the texture with all mipmap levels to OpenGL, so there is no need to involve glTexImage here. If gluBuild2DMipmaps crashes, then it's because the parameters are wrong, and then they would have been wrong with glTexImage as well.
V-man
V-man
If it crashes
http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads
L. Spiro
L. Spiro

[quote name='YogurtEmperor' timestamp='1313747774' post='4851126']
Secondly, you never called glTexImage2D, and it is crashing when you build mipmaps on a blank image.

What do you mean? He's using gluBuild2DMipmaps which loads the texture with all mipmap levels to OpenGL, so there is no need to involve glTexImage here. If gluBuild2DMipmaps crashes, then it's because the parameters are wrong, and then they would have been wrong with glTexImage as well.
[/quote]

Oh that is right. Sorry for my misdirection; I never use glut/glue/glu/etc.


But I still wager that it is the cause of the crash.
After reading the original poster’s other questions and this one, I have no reason to suspect that he ever filled the data member of his Image structure named texData. It is most likely a dangling pointer.


L. Spiro
I restore Nintendo 64 video-game OST’s into HD! https://www.youtube.com/channel/UCCtX_wedtZ5BoyQBXEhnVZw/playlists?view=1&sort=lad&flow=grid

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.