I'm trying to use textures on a cube for a project, and they aren't working at all. I can read them in successfully, but they always display weird.
The cube currently looks something like this (the main face in the image being a 'textured' one, it's supposed to be a picture of a Pokemon so it is clearly wrong):
What.png picture by snipy3 - Photobucket
Here is my (relevant) code:
Code:
GLuint make_texture(const char * filename) {
GLuint text = 50;
int width, height;
GLubyte *data;
FILE * file;
//Open texture data
file = fopen(filename, "rb");
if(file == NULL) {printf("File fail %s: %s\n", filename, strerror(errno)); return 0;}
//Allocate buffer
data = read_image(filename);
width = tex_w;
height = tex_h;
glEnable(GL_TEXTURE_2D);
//Allocate a texture name
glGenTextures(1, &text);
//Select current texture
glBindTexture(GL_TEXTURE_2D, text);
//Select modulate to mix texture with color for shading
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_MODULATE);
//When texture area is small, bilinear filter the closest mipmap
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
//When large, bilinear filter the FIRST mipmap
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
//If wrap is true, texture wraps over the edges, otherwise it ends at the edges
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
//glBuild2DMipmaps(GL_TEXTURE_2D, GL_RGB, width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data);
//free buffer
free(data);
return text;
}
Code:
void draw_polys() {
GLuint tex;
int i;
VECTOR *one, *two, *three;
for(i = 0; i < data.tCount; i++) {
one = &data.vArray[data.tArray[i].a - 1].pos;
two = &data.vArray[data.tArray[i].b - 1].pos;
three = &data.vArray[data.tArray[i].c - 1].pos;
tex = data.tArray[i].texture;
if(tex == 0) { //No texture, then draw it normally
glBegin(GL_TRIANGLES);
glNormal3d(data.tArray[i].n.x, data.tArray[i].n.y, data.tArray[i].n.z);
glVertex3d(one->x, one->y, one->z);
glVertex3d(two->x, two->y, two->z);
glVertex3d(three->x, three->y, three->z);
}
else { //Texture exists, then texture it
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, tex);
glBegin(GL_TRIANGLES);
glNormal3d(data.tArray[i].n.x, data.tArray[i].n.y, data.tArray[i].n.z);
//printf("a's s value is %lf, and t value is %lf\n", data.vArray[data.tArray[i].a - 1].s, data.vArray[data.tArray[i].a - 1].t);
glTexCoord2d(data.vArray[data.tArray[i].a - 1].s, data.vArray[data.tArray[i].a - 1].t);
glVertex3d(one->x, one->y, one->z);
//printf("b's s value is %lf, and t value is %lf\n", data.vArray[data.tArray[i].b - 1].s, data.vArray[data.tArray[i].b - 1].t);
glTexCoord2d(data.vArray[data.tArray[i].b - 1].s, data.vArray[data.tArray[i].b - 1].t);
glVertex3d(two->x, two->y, two->z);
//printf("c's s value is %lf, and t value is %lf\n", data.vArray[data.tArray[i].c - 1].s, data.vArray[data.tArray[i].c - 1].t);
glTexCoord2d(data.vArray[data.tArray[i].c - 1].s, data.vArray[data.tArray[i].c - 1].t);
glVertex3d(three->x, three->y, three->z);
}
glEnd();
glDisable(GL_TEXTURE_2D);
}
}
I'm probably misusing a GL texture function somehow... any help would be greatly appreciated!