|
|
发表于 2008-12-18 17:36:00
|
显示全部楼层
Re:关于.TGA文件.
我觉得alpha通道没什么用,纹理混合无非是颜色加减,用alpha最多用来镂空,但是可以直接将读入的rgb位图加一个alpha通道就可以了,看看下面的代码,formatbmp函数的用处在于防止线性过滤或者其他过滤之后产生一圈关键色的边,不用也无关紧要,但是如果要写个类似sfc的rpg的游戏,我觉得有用,这里也不考虑数据处理的速度,因为把这些处理之后的数据保存到文件里也就不再用bmp位图了
- void formatbmp(int now, int w, int h, byte *s,byte *d)
- {
- int tr=0,tg=0,tb=0,cc=0;
- int lp;
- if(now > w)
- if(now < w*h-w)
- if((now%w) !=0 )
- if( (now+w-1)%w !=0 )
- goto start ;
- d[now*4+2]=s[now*3+2];
- d[now*4+1]=s[now*3+1];
- d[now*4]=s[now*3];
- d[now*4+3]=0;
- return;
- start:
- for(char a=0;a<4;a++)
- {
- switch(a)
- {
- case 0:lp=(now-w)*3;break;
- case 1:lp=(now+w)*3;break;
- case 2:lp=now*3-3; break;
- case 3:lp=now*3+3;
- }
- if( s[lp]==0 && s[lp+1]==0 && s[lp+2]==0 )
- continue;
- tr += s[lp];
- tg += s[lp+1];
- tb += s[lp+2];
- cc++;
- }
- d[now*4]=(cc!=0)?tr/cc:0;
- d[now*4+1]=(cc!=0)?tg/cc:0;
- d[now*4+2]=(cc!=0)?tb/cc:0;
- d[now*4+3]=0;
- }
- GLuint CreateTexture(char *picname)
- {
- GLuint texture;
- HBITMAP hbmp;
- BITMAP bmp;
- byte *image,*imagetemp;
- hbmp=(HBITMAP)LoadImage(GetModuleHandle(0), picname, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION| LR_LOADFROMFILE);
- GetObject(hbmp,sizeof bmp,&bmp);
- imagetemp=new byte[bmp.bmWidth * bmp.bmHeight * 4];
- image=(byte*)bmp.bmBits;
- byte temp;
- memset(imagetemp,255,sizeof &imagetemp);//这貌似是无关紧要的语句,
- // 我都忘了当时是为了做什么
- // 才写这句话的
- for(int xx=0; xx < bmp.bmWidth *bmp.bmHeight; xx++)
- {
- if(image[xx *3]==0 && image[xx *3+1]==0 && image[xx *3+2]==0 )
- {
- formatbmp(xx, bmp.bmWidth, bmp.bmHeight, image, imagetemp);
- temp =imagetemp[xx * 4+2];
- imagetemp[xx * 4+2] =imagetemp[xx * 4];
- imagetemp[xx * 4] =temp;
- }
- else
- {
- imagetemp[xx * 4+2] = image[xx *3];
- imagetemp[xx * 4+1] = image[xx *3+1];
- imagetemp[xx * 4] = image[xx *3+2];
- imagetemp[xx * 4+3] = 255;
- }
- }
- glGenTextures(1,&texture);
- glBindTexture(GL_TEXTURE_2D,texture);
- glTexImage2D(GL_TEXTURE_2D, 0, 4, bmp.bmWidth, bmp.bmHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imagetemp);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
- glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
- DeleteObject(hbmp);
- delete[] imagetemp;
- imagetemp=0;
- return texture;
- }
复制代码
|
|