gpt4 book ai didi

c - OpenGL 纹理显示为实体对象?

转载 作者:太空宇宙 更新时间:2023-11-03 23:49:50 25 4
gpt4 key购买 nike

据我所知,我的纹理加载正确,因为我正在制作的 table 应用了不同深浅的棕色,该 table 使用了 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);
}

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

最佳答案

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

否则多个顶点将具有相同的纹理坐标,导致从您的纹理中采样单一颜色。

关于c - OpenGL 纹理显示为实体对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22922974/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com