游戏开发论坛

 找回密码
 立即注册
搜索
查看: 3037|回复: 3

opengl 中怎么实现指定颜色像素为透明?

[复制链接]

2

主题

2

帖子

8

积分

新手上路

Rank: 1

积分
8
发表于 2011-6-28 17:14:00 | 显示全部楼层 |阅读模式
如图,怎么实现类似于directX 里面的colorkey?例如图片中黑色像素在渲染的时候为透明色。 不怎么熟悉opengl ,请教!!由于很多素材做的时候都为黑色背景。如果处理太麻烦了。。

用混色也不好控制啊。有简单的方法吗??

60

主题

1319

帖子

1319

积分

金牌会员

Rank: 6Rank: 6

积分
1319
发表于 2011-7-5 23:05:00 | 显示全部楼层

Re:opengl 中怎么实现指定颜色像素为透明?

你这个图黑白交界的地方是要半透明吗?如果只是纯黑做为colorkey倒是不太难,加载的时候把黑像素的alpha设成0就行了。

60

主题

1319

帖子

1319

积分

金牌会员

Rank: 6Rank: 6

积分
1319
发表于 2011-7-5 23:07:00 | 显示全部楼层

Re:opengl 中怎么实现指定颜色像素为透明?

回复不能传附件啊!



  1. void loadTexture( void )   
  2. {
  3.     AUX_RGBImageRec *pImage_RGB = auxDIBImageLoad( ".\\door.bmp" );
  4.     unsigned char *pImage_RGBA = NULL;

  5.     if( pImage_RGB != NULL )
  6.     {
  7.         int imageSize_RGB  = pImage_RGB->sizeX * pImage_RGB->sizeY * 3;
  8.         int imageSize_RGBA = pImage_RGB->sizeX * pImage_RGB->sizeY * 4;

  9.         // allocate buffer for a RGBA image
  10.         pImage_RGBA = new unsigned char[imageSize_RGBA];

  11.         //
  12.         // Loop through the original RGB image buffer and copy it over to the
  13.         // new RGBA image buffer setting each pixel that matches the key color
  14.         // transparent.
  15.         //

  16.         int i, j;

  17.         for( i = 0, j = 0; i < imageSize_RGB; i += 3, j += 4 )
  18.         {
  19.             // Does the current pixel match the selected color key?
  20.             if( pImage_RGB->data[i]   == g_keyColor[0] &&
  21.                 pImage_RGB->data[i+1] == g_keyColor[1] &&
  22.                 pImage_RGB->data[i+2] == g_keyColor[2] )
  23.             {
  24.                 pImage_RGBA[j+3] = 0;   // If so, set alpha to fully transparent.
  25.             }
  26.             else
  27.             {
  28.                 pImage_RGBA[j+3] = 255; // If not, set alpha to fully opaque.
  29.             }

  30.             pImage_RGBA[j]   = pImage_RGB->data[i];
  31.             pImage_RGBA[j+1] = pImage_RGB->data[i+1];
  32.             pImage_RGBA[j+2] = pImage_RGB->data[i+2];
  33.         }

  34.         glGenTextures( 1, &g_textureID );
  35.         glBindTexture( GL_TEXTURE_2D, g_textureID );
  36.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER, GL_NEAREST);
  37.         glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER, GL_NEAREST);

  38.         // Don't forget to use GL_RGBA for our new image data... we support Alpha transparency now!
  39.         glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, pImage_RGB->sizeX, pImage_RGB->sizeY, 0,
  40.                       GL_RGBA, GL_UNSIGNED_BYTE, pImage_RGBA );
  41.     }

  42.     if( pImage_RGB )
  43.     {
  44.         if( pImage_RGB->data )
  45.             free( pImage_RGB->data );

  46.         free( pImage_RGB );
  47.     }

  48.     if( pImage_RGBA )
  49.         delete [] pImage_RGBA;
  50. }

复制代码

60

主题

1319

帖子

1319

积分

金牌会员

Rank: 6Rank: 6

积分
1319
发表于 2011-7-5 23:42:00 | 显示全部楼层

Re:opengl 中怎么实现指定颜色像素为透明?

http://www.codesampler.com/source/ogl_color_key.zip
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

GMT+8, 2025-6-8 10:35

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

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