0

im trying to apply 2 textures to my level the code compiles but the levels come out completly white,

struct Image { unsigned long size_x; unsigned long size_y; char *data; } typedef struct Image Image; const int textureCount = 2; Image myTextureData[textureCount]; GLuint theTexture[textureCount]; char* textureFilenames0[textureCount] = {"road.bmp"}; char* textureFilenames1[textureCount] = {"building.bmp"}; 

texture loader class

void textureLoader() { /*glPixelStorei(GL_UNPACK_ALIGNMENT, 1);*/ glGenTextures(2, theTexture); glBindTexture(GL_TEXTURE_2D, theTexture[0]); for(int k=0; k < textureCount; k++) { if(!imageLoader(textureFilenames0[k], &myTextureData[k])) exit(1); 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, myTextureData[k].size_x, myTextureData[k].size_y, GL_RGB, GL_UNSIGNED_BYTE, myTextureData[k].data); glBindTexture(GL_TEXTURE_2D, theTexture[1]); for(int k=0; k < textureCount; k++) { if(!imageLoader(textureFilenames1[k], &myTextureData[k])) exit(1); 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, myTextureData[k].size_x, myTextureData[k].size_y, GL_RGB, GL_UNSIGNED_BYTE, myTextureData[k].data); } } 

1 Answer 1

1

Have you called

glEnable(GL_TEXTURE_2D); 

and glTexCoord2 or glTexCoordPointer?

Also, try setting the texture parameters before uploading the data.

Finally, when using loops, you need all the filenames in one array, like

char* textureFilenames[textureCount] = {"road.bmp", "building.bmp"}; 

Or you can get rid of the loop and write

char* textureFilenames0 = "road.bmp"; char* textureFilenames1 = "building.bmp"; 

But there are not textureCount files with the road bitmap inside.

Sign up to request clarification or add additional context in comments.

7 Comments

appreiate that mate, but how does the program know which file i want to call and where would i call glEnable(GL_TEXTURE_2D);
@rich: The code you have now is wrong. You need to bind texture#k and then load file#k. Right now you are loading file0#k into texture#0, your loop looks for two filenames, there's only one, so you end up using a NULL pointer and loading a non-existant file on top of your good texture. I don't know what your texture loader does when you pass it a NULL filename. glEnable(GL_TEXTURE_2D) needs to be done before you draw textured geometry.
You use glBindTexture(GL_TEXTURE_2D, a_texture_ID) to tell OpenGL, which texture to use. Unfortunately in your code above the texture ID, which is allocated using glGenTextures seems to be placed in a single index global variable. Technically textureLoader should return the value of theTexture, so that you can later, when rendering bind it.
@datenwolf: It's perfectly reasonable (and more flexible) for a texture loader to be designed to load into the currently bound texture. That's not the problem, since two textures get allocated and two textures get bound. The problem is that the texture loader gets called N*N times.
@Ben Voigt: I was referring to @rich's question how to tell the program/OpenGL which texture to use. In it's current implementation textureLoader seems to worth through some global variable.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.