所以我有点做了一个小的bmp导入器,以便能够在opengl中使用纹理,但在我加载图片和东西而不是纹理后,它只是将颜色变为棕色... sqare 我也尝试了超过1个纹理,我得到了相同的resault。 这是一些代码:
int LoadBitmap ( char *filename) {
BITMAPFILEHEADER fileheader;
BITMAPINFOHEADER fileinfo;
RGBTRIPLE rgb;
unsigned char *texture;
FILE *f=fopen(filename,"r");
num_texture++;
fread(&fileheader,sizeof(fileheader),1,f);
fread(&fileinfo,sizeof(fileinfo),1,f);
cout<<fileinfo.biWidth<<" "<<fileinfo.biHeight<<endl;
texture=new unsigned char[fileinfo.biWidth*fileinfo.biHeight*4];
int j=0;
for(int i=0;i<fileinfo.biWidth*fileinfo.biHeight;i++)
{
fread(&rgb,sizeof(rgb),1,f);
texture[j]=rgb.rgbtRed;
texture[j+1]=rgb.rgbtGreen;
texture[j+2]=rgb.rgbtBlue;
texture[j+3]=255;
j+=4;
}
fclose(f);
glBindTexture(GL_TEXTURE_2D,num_texture);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
glTexImage2D(GL_TEXTURE_2D,0,4,fileinfo.biWidth,fileinfo.biHeight,0,GL_RGBA,GL_UNSIGNED_BYTE,texture);
gluBuild2DMipmaps(GL_TEXTURE_2D,4,fileinfo.biWidth,fileinfo.biHeight,GL_RGBA,GL_UNSIGNED_BYTE,texture);
delete[] texture;
return num_texture;
return 0;
}
这是绘图部分:
glEnable(GL_TEXTURE_2D);
int a=LoadBitmap("texture1.bmp");
glBindTexture(GL_TEXTURE_2D,a);
glBegin(GL_QUADS);
glTexCoord2f(0,0);
glVertex3f(-10,-10,0 );
glTexCoord2f(0,1);
glVertex3f(-10,10,0);
glTexCoord2f(1,1);
glVertex3f(10,10,0 );
glTexCoord2f(1,0);
glVertex3f(10,-10,0 );
glEnd();
答案 0 :(得分:2)
FILE *f=fopen(filename,"r");
您可能想尝试以二进制模式打开文件,它可能只是检测文件的结尾,或者以其他方式破坏输入。
FILE *f=fopen(filename,"rb");
您还对文件的实际格式做了一些假设,但假设您自己生成了BMP,我想这可能没问题。