繁体   English   中英

OpenGL纹理显示为实体对象?

[英]OpenGL texture showing as a solid object?

因此,据我所知,我正确地加载了纹理,因为我正在制作的桌子上使用了两种不同的纹理,分别应用了不同的棕色阴影。 但是,纹理显示为纯色,没有任何细节。 我不知道是什么原因造成的。

这是我用于加载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);
}

这是完整代码的pastebin :: http://pastebin.com/0Ns5JdDX

每个glVertex3f()之前都需要一个glTexCoord2f() glVertex3f()

否则,多个顶点将具有相同的texcoord,从而从纹理中采样一种颜色。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM