Jump to content
  • Advertisement
Sign in to follow this  
RichRoberts

texture function problems

This topic is 2619 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

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"};

Share this post


Link to post
Share on other sites
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"};

Share this post


Link to post
Share on other sites
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);
}
}

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites

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.

Share this post


Link to post
Share on other sites
Hidden


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"};

Share this post


Link to post
If it crashes
http://www.opengl.org/wiki/Common_Mistakes#Texture_upload_and_pixel_reads

Share this post


Link to post
Share on other sites

[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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!