游戏开发论坛

 找回密码
 立即注册
搜索
12
返回列表 发新帖
楼主: gaoxudong

关于.TGA文件.

[复制链接]

42

主题

115

帖子

141

积分

注册会员

Rank: 2

积分
141
发表于 2008-12-18 17:36:00 | 显示全部楼层

Re:关于.TGA文件.

我觉得alpha通道没什么用,纹理混合无非是颜色加减,用alpha最多用来镂空,但是可以直接将读入的rgb位图加一个alpha通道就可以了,看看下面的代码,formatbmp函数的用处在于防止线性过滤或者其他过滤之后产生一圈关键色的边,不用也无关紧要,但是如果要写个类似sfc的rpg的游戏,我觉得有用,这里也不考虑数据处理的速度,因为把这些处理之后的数据保存到文件里也就不再用bmp位图了





  1. void formatbmp(int now, int w, int h, byte *s,byte *d)
  2. {
  3.         int tr=0,tg=0,tb=0,cc=0;
  4.         int lp;
  5.         if(now > w)
  6.                 if(now < w*h-w)
  7.                         if((now%w) !=0 )
  8.                                 if( (now+w-1)%w !=0 )
  9.                                         goto start ;
  10.         d[now*4+2]=s[now*3+2];
  11.         d[now*4+1]=s[now*3+1];
  12.         d[now*4]=s[now*3];
  13.         d[now*4+3]=0;
  14.         return;
  15. start:
  16.         for(char a=0;a<4;a++)
  17.         {
  18.                 switch(a)
  19.                 {
  20.                 case 0:lp=(now-w)*3;break;
  21.                 case 1:lp=(now+w)*3;break;
  22.                 case 2:lp=now*3-3;        break;
  23.                 case 3:lp=now*3+3;
  24.                 }
  25.                 if( s[lp]==0 && s[lp+1]==0 && s[lp+2]==0 )
  26.                         continue;
  27.                 tr += s[lp];
  28.                 tg += s[lp+1];
  29.                 tb += s[lp+2];
  30.                 cc++;
  31.         }
  32.         d[now*4]=(cc!=0)?tr/cc:0;
  33.         d[now*4+1]=(cc!=0)?tg/cc:0;
  34.         d[now*4+2]=(cc!=0)?tb/cc:0;
  35.         d[now*4+3]=0;
  36. }

  37. GLuint CreateTexture(char *picname)
  38. {
  39.         GLuint                texture;
  40.         HBITMAP                hbmp;
  41.         BITMAP                bmp;
  42.         byte *image,*imagetemp;
  43.         hbmp=(HBITMAP)LoadImage(GetModuleHandle(0), picname, IMAGE_BITMAP, 0, 0, LR_CREATEDIBSECTION| LR_LOADFROMFILE);
  44.         GetObject(hbmp,sizeof bmp,&bmp);
  45.         imagetemp=new byte[bmp.bmWidth * bmp.bmHeight * 4];

  46.         image=(byte*)bmp.bmBits;
  47.         byte temp;
  48.         memset(imagetemp,255,sizeof &imagetemp);//这貌似是无关紧要的语句,
  49.                                                    //  我都忘了当时是为了做什么
  50.                                                     // 才写这句话的
  51.         for(int xx=0; xx < bmp.bmWidth *bmp.bmHeight; xx++)
  52.         {
  53.                 if(image[xx *3]==0 && image[xx *3+1]==0 && image[xx *3+2]==0 )
  54.                 {
  55.                         formatbmp(xx, bmp.bmWidth, bmp.bmHeight, image, imagetemp);
  56.                         temp                                =imagetemp[xx * 4+2];
  57.                         imagetemp[xx * 4+2] =imagetemp[xx * 4];
  58.                         imagetemp[xx * 4]        =temp;
  59.                 }
  60.                         else
  61.                 {
  62.                         imagetemp[xx * 4+2]                =        image[xx *3];
  63.                         imagetemp[xx * 4+1]                =        image[xx *3+1];
  64.                         imagetemp[xx * 4]                =        image[xx *3+2];
  65.                         imagetemp[xx * 4+3]                =        255;
  66.                 }
  67.         }
  68.         glGenTextures(1,&texture);
  69.         glBindTexture(GL_TEXTURE_2D,texture);
  70.         glTexImage2D(GL_TEXTURE_2D, 0, 4, bmp.bmWidth, bmp.bmHeight, 0, GL_RGBA, GL_UNSIGNED_BYTE, imagetemp);

  71.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
  72.         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);


  73.         DeleteObject(hbmp);
  74.         delete[]        imagetemp;
  75.         imagetemp=0;

  76.         return texture;
  77. }
复制代码

42

主题

115

帖子

141

积分

注册会员

Rank: 2

积分
141
发表于 2008-12-18 17:39:00 | 显示全部楼层

Re:关于.TGA文件.

不然的话还不如用directx的d3dxsprite,这东西还方便得多,而且用d3dxcreatetexturefromfileex还能读其他格式的图片,不过我认为没必要,因为这东西混合的时候只能以S*alpha+D*(1-alpha)的方式进行混合,不能直接S+D或S-D,也就是说只能镂空和透明,如果要以其他的方式混合的话就不能用d3dxsprite了(我用的是directx9,不知道后面版本改没有),没意思

1

主题

50

帖子

52

积分

注册会员

Rank: 2

积分
52
发表于 2008-12-21 14:19:00 | 显示全部楼层

Re: Re:关于.TGA文件.

lizelglg: Re:关于.TGA文件.

我觉得alpha通道没什么用,纹理混合无非是颜色加减,用alpha最多用来镂空,但是可以直接将读入的rgb位图加一...

代码不是光你一个人看的,代码这么乱,真不知道你以后怎么维护?
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

作品发布|文章投稿|广告合作|关于本站|游戏开发论坛 ( 闽ICP备17032699号-3 )

GMT+8, 2026-1-20 15:34

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表