Reputation: 5189
So I have my textures loading in correctly as far as I know, as different shades of brown are being applied to a table I am making, that uses 2 different textures. However the texture is showing as a solid colour with no detail in it. I have no idea what is causing this.
This is my code for loading bmp textures:
GLuint LoadTexture(const char * filename){
GLuint texture;
int width, height;
unsigned char * data;
FILE * file;
file = fopen(filename, "rb");
if(file == NULL)
return 0;
width = 512;
height = 1024;
data = (unsigned char *) malloc (width * height * 3);
fread(data, width * height * 3, 1, file);
fclose(file);
for(int i = 0; i < width * height; ++i){
int index = i * 3;
unsigned char B,R;
B = data[index];
R = data[index + 2];
data[index] = R;
data[index + 2] = B;
}
glGenTextures(1, &texture);
glBindTexture(GL_TEXTURE_2D, texture);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
gluBuild2DMipmaps(GL_TEXTURE_2D, 3,width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
return texture;
}
void loadTextures(){
tableTopTexture = LoadTexture("table_top.bmp");
tableLegTexture = LoadTexture("table_leg.bmp");
}
And then I use it like so in a drawTable method:
void drawTable(){
//texture stuff
glEnable(GL_TEXTURE_2D);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_DECAL);
//draw legs
glBindTexture(GL_TEXTURE_2D, tableLegTexture);
glTexCoord2f(0.0,0.0);
drawCube(0.050,0.5,0.060,0,0,0);
glTexCoord2f(0.0,1.0);
drawCube(0.050,0.5,0.060,3,0,0);
glTexCoord2f(1.0,1.0);
drawCube(0.050,0.5,0.060,3,0,-1);
glTexCoord2f(1.0,0.0);
drawCube(0.050,0.5,0.060,0,0,-1);
//draw table top
glBindTexture(GL_TEXTURE_2D, tableTopTexture);
glTexCoord2f(0.0,0.0);
glTexCoord2f(1.0,0.0);
glTexCoord2f(1.0,1.0);
glTexCoord2f(0.0,1.0);
drawCube(2,0.050,1,1.5,0.5,-0.5);
glDisable(GL_TEXTURE_2D);
}
Here is a pastebin of the complete code:: http://pastebin.com/0Ns5JdDX
Upvotes: 2
Views: 433
Reputation: 52123
You need a glTexCoord2f()
before each glVertex3f()
.
Otherwise multiple vertices will have the same texcoord, resulting in sampling a single color from your texture.
Upvotes: 1