texture function problems

Started by
5 comments, last by L. Spiro 12 years, 8 months ago
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"};
Advertisement
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"};
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);
}
}
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


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.


char* texName[texNum] = {"floor.bmp"};
char* texName[texNum] = {"door.bmp"};



This is wrong - here you are declaring 2 char* arrays with the same name. You probably want to do this:


char* texName[0] = {"floor.bmp"};
char* texName[1] = {"door.bmp"};
If it crashes
http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads
Sig: http://glhlib.sourceforge.net
an open source GLU replacement library. Much more modern than GLU.
float matrix[16], inverse_matrix[16];
glhLoadIdentityf2(matrix);
glhTranslatef2(matrix, 0.0, 0.0, 5.0);
glhRotateAboutXf2(matrix, angleInRadians);
glhScalef2(matrix, 1.0, 1.0, -1.0);
glhQuickInvertMatrixf2(matrix, inverse_matrix);
glUniformMatrix4fv(uniformLocation1, 1, FALSE, matrix);
glUniformMatrix4fv(uniformLocation2, 1, FALSE, inverse_matrix);

[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

This topic is closed to new replies.

Advertisement