Sign in to follow this  
RichRoberts

texture function problems

Recommended Posts

RichRoberts    100
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

[code]#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"};[/code]

Share this post


Link to post
Share on other sites
Hodgman    51324
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 ([i]when you rename the duplicate variable[/i]), 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:[code]char* texName[texNum] = {"floor.bmp", "door.bmp"};[/code]

Share this post


Link to post
Share on other sites
RichRoberts    100
thanks for that bit but its still crashing and im wondering if it has to do with this part of the code

[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);
}
}[/code]

Share this post


Link to post
Share on other sites
L. Spiro    25638
Firstly you don’t need two for loops.

[color="#1C2837"][size="2"] [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);
}[/code] [/size][/color]

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


Secondly, you never called [url="http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml"]glTexImage2D[/url], and it is crashing when you build mipmaps on a blank image.


L. Spiro

Share this post


Link to post
Share on other sites
Brother Bob    10344
[quote name='YogurtEmperor' timestamp='1313747774' post='4851126']
Secondly, you never called [url="http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml"]glTexImage2D[/url], and it is crashing when you build mipmaps on a blank image.
[/quote]
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.

Share this post


Link to post
Share on other sites
AndyEsser    394
Hidden
[quote name='rich606' timestamp='1313727613' post='4851048']
[code]
char* texName[texNum] = {"floor.bmp"};
char* texName[texNum] = {"door.bmp"};
[/code]
[/quote]

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

[code]
char* texName[0] = {"floor.bmp"};
char* texName[1] = {"door.bmp"};
[/code]

Share this post


Link to post
L. Spiro    25638
[quote name='Brother Bob' timestamp='1313748402' post='4851131']
[quote name='YogurtEmperor' timestamp='1313747774' post='4851126']
Secondly, you never called [url="http://www.opengl.org/sdk/docs/man/xhtml/glTexImage2D.xml"]glTexImage2D[/url], and it is crashing when you build mipmaps on a blank image.
[/quote]
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

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this