游戏开发论坛

 找回密码
 立即注册
搜索
查看: 4713|回复: 11

opengl中如何抠像?

[复制链接]

2

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
发表于 2006-11-7 09:14:00 | 显示全部楼层 |阅读模式
我的问题是:我想在opengl中显示一个人物,但图片是有背景色的,我怎么才能把背景色去掉呢?

2万

主题

2万

帖子

6万

积分

论坛元老

Rank: 8Rank: 8

积分
66489
QQ
发表于 2006-11-7 09:32:00 | 显示全部楼层

Re:opengl中如何抠像?

使用带alpha通道的图片,可以在各种绘图软件中建立成透明底色的图片,然后存成png之类可以保存alpha通道的格式。

180

主题

3511

帖子

3520

积分

论坛元老

Rank: 8Rank: 8

积分
3520
发表于 2006-11-7 14:48:00 | 显示全部楼层

Re:opengl中如何抠像?

"但图片是有背景色的"?
图片怎么可能有背景色? 你是说透明吗? 还是多边形的颜色?

180

主题

3511

帖子

3520

积分

论坛元老

Rank: 8Rank: 8

积分
3520
发表于 2006-11-7 14:49:00 | 显示全部楼层

Re:opengl中如何抠像?

爽快点啦,发个图上来!

2

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
 楼主| 发表于 2006-11-7 20:54:00 | 显示全部楼层

Re: opengl中如何抠像?

谢谢关注
就是有白色背景的人物图(*.bmp文件),怎么把白色去除?

21

主题

230

帖子

230

积分

中级会员

Rank: 3Rank: 3

积分
230
发表于 2006-11-7 21:09:00 | 显示全部楼层

Re:opengl中如何抠像?

不用bmp.用带alpha通道的图片

1

主题

28

帖子

28

积分

注册会员

Rank: 2

积分
28
QQ
发表于 2006-11-7 21:22:00 | 显示全部楼层

Re:opengl中如何抠像?

这个简单,我们可以给BMP图象数据添加一个Alpha通道。下面给你两个函数,你自己揣摩一下。基本思路是把需要去掉的部分的颜色的alpha分量设为0,其他颜色(就是需要保留的)设为1,然后贴图的时候打开混合功能即可,设置一下混合函数的源因子。事例代码是假设背景为黑色,你只需把判断黑色的地方改成判断白色即可。

1

主题

28

帖子

28

积分

注册会员

Rank: 2

积分
28
QQ
发表于 2006-11-7 21:25:00 | 显示全部楼层

Re:opengl中如何抠像?

这个函数是读24位BMP数据的,返回值即为图象数据。如果是其他的BMP,你把这个函数替换掉就可以了。
UCHAR *LoadBmpFile(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
  FILE *filePtr;                        // the file pointer
  BITMAPFILEHEADER  bitmapFileHeader;   // bitmap file header
  unsigned char    *bitmapImage;        // bitmap image data
  unsigned int      imageIdx = 0;       // image index counter
  unsigned char     tempRGB;            // swap variable

  // open filename in "read binary" mode
  filePtr = fopen(filename, "rb");
  if (filePtr == NULL)
  {
        AfxMessageBox("文件不存在");
        return NULL;
  }
  // read the bitmap file header
  fread(&bitmapFileHeader, sizeof(BITMAPFILEHEADER), 1, filePtr);

  // verify that this is a bitmap by checking for the universal bitmap id
  if (bitmapFileHeader.bfType != BITMAP_ID)
  {
    fclose(filePtr);
    return NULL;
  }

  // read the bitmap information header
  fread(bitmapInfoHeader, sizeof(BITMAPINFOHEADER), 1, filePtr);

  // move file pointer to beginning of bitmap data
  fseek(filePtr, bitmapFileHeader.bfOffBits, SEEK_SET);

  // allocate enough memory for the bitmap image data
  bitmapImage = (unsigned char*)malloc(bitmapInfoHeader->biSizeImage);

  // verify memory allocation
  if (!bitmapImage)
  {
    free(bitmapImage);
    fclose(filePtr);
    return NULL;
  }

  // read in the bitmap image data
  fread(bitmapImage, 1, bitmapInfoHeader->biSizeImage, filePtr);

  // make sure bitmap image data was read
  if (bitmapImage == NULL)
  {
    fclose(filePtr);
    return NULL;
  }

  // swap the R and B values to get RGB since the bitmap color format is in BGR
  for (imageIdx = 0; imageIdx < bitmapInfoHeader->biSizeImage; imageIdx+=3)
  {
    tempRGB = bitmapImage[imageIdx];
    bitmapImage[imageIdx] = bitmapImage[imageIdx + 2];
    bitmapImage[imageIdx + 2] = tempRGB;
  }

  // close the file and return the bitmap image data
  fclose(filePtr);
  return bitmapImage;
} // end LoadBitmapFile()

1

主题

28

帖子

28

积分

注册会员

Rank: 2

积分
28
QQ
发表于 2006-11-7 21:26:00 | 显示全部楼层

Re:opengl中如何抠像?

这个函数是给获得的图象数据添加alpha 通道
/*****************************************************************************
LoadBitmapFileWithAlpha
Loads a bitmap file normally, and then adds an alpha component to use for
blending
*****************************************************************************/
UCHAR *LoadBmpFileWithAlpha(char *filename, BITMAPINFOHEADER *bitmapInfoHeader)
{
  unsigned char *bitmapImage = LoadBmpFile(filename, bitmapInfoHeader);
  unsigned char *bitmapWithAlpha = (unsigned char *)malloc(bitmapInfoHeader->biSizeImage * 4 / 3);

  if (bitmapImage == NULL || bitmapWithAlpha == NULL)
    return NULL;

  // loop through the bitmap data
  for (unsigned int src = 0, dst = 0; src < bitmapInfoHeader->biSizeImage; src +=3, dst +=4)
  {
    // if the pixel is black, set the alpha to 0. Otherwise, set it to 255.
    if (bitmapImage[src] <= 0.05 && bitmapImage[src+1] <= 0.05 && bitmapImage[src+2] <= 0.05)
      bitmapWithAlpha[dst+3] = 0;
    else
      bitmapWithAlpha[dst+3] = 0xFF;

    // copy pixel data over
    bitmapWithAlpha[dst] =   bitmapImage[src];
    bitmapWithAlpha[dst+1] = bitmapImage[src+1];
    bitmapWithAlpha[dst+2] = bitmapImage[src+2];
  }

  free(bitmapImage);

  return bitmapWithAlpha;
} // end LoadBitmapFileWithAlpha()

2

主题

6

帖子

6

积分

新手上路

Rank: 1

积分
6
 楼主| 发表于 2006-11-8 15:12:00 | 显示全部楼层

Re: opengl中如何抠像?

谢谢
不过这样把人物中与背景色相同的颜色也去掉了,是吧?
我看Nehe的教程是用两张图片与背景混合,但要求背景必须是黑色的,若背景是白色或其他色的好像不行。
sf_2006118151136.jpg
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2026-1-25 20:47

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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