据我所知,我的纹理正确加载,因为不同的棕色色调应用于我正在制作的桌子,使用2种不同的纹理。然而,纹理显示为纯色,没有细节。 我不知道造成这种情况的原因。
这是我加载bmp纹理的代码:
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");
}
然后我在drawTable方法中使用它:
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);
}
这是完整代码:: http://pastebin.com/0Ns5JdDX
的pastebin答案 0 :(得分:1)
每个glTexCoord2f()
之前需要glVertex3f()
。
否则多个顶点将具有相同的texcoord,从而从纹理中采样单个颜色。