我怎样才能裁剪图像的面具,并与iPhone上的另一个图像(背景)结合? (首选OpenGL ES 1.1)

我需要结合三个图像的方式,我代表附件:

在这里输入图像说明

1)一个图像是背景。 从意义上说,它是“稳定的”,它没有alpha通道。

2)另一个是精灵。 雪碧依靠背景。 雪碧可能有自己的阿尔法通道,背景必须在精灵透明的地方可见。

3)有许多掩码:我将每个帧的新掩码应用于Sprite。 蒙版不是矩形的。

换句话说,可见像素=背景的像素,如果裁剪遮罩相应的颜色是白色的或者子画面是透明的; 否则为精灵的像素(例如,相应的掩模的像素是黑色的)。

我正在与cocos2d-iphone合作。 我可以与cocos2d-iphone或OpenGL ES 1.1做这样的组合吗? 如果任何答案是肯定的,工作代码将不胜感激。 如果两个答案都是NO,iOS上还有另外一种技术可以实现我想要的function(可能是Quartz2d或OpenGL ES 2.0)?

遮罩格式对于Sprite不是强制性的黑色,对于Background而言是白色的。 如果需要,我可以制作所需格式的Mask,如Background的透明度和Sprite的白色。

PS还有另外一个没有答案的同类问题: 可以改变iPhone上某些像素的alpha值吗?

这是我对OpenGL的回答。 Quartz的程序会非常不同。 实际的代码是非常简单的,但要正确的是棘手的部分。 我使用的是1024X1024的GL上下文,原点在左下angular。 我没有发布我的代码,因为它使用了OpenGL | ES中不可用的即时模式。 如果你想要我的绘图代码,让我知道,我会更新我的答案。

  1. 绘制与禁用混合的面具。
  2. 启用混合,设置GLBlendFunc(GL_DST_COLOR,GL_ZERO)并绘制stream血纹理。 我的面具是白色,它应该stream血。 在你的问题是黑色的。
  3. 现在绘制背景,将混合函数设置为glBlendFunc(GL_ONE_MINUS_DST_COLOR,GL_DST_COLOR)并绘制背景纹理。

编辑这里是我上面描述的代码。 请注意,这不适用于iOS,因为没有即时模式,但是您应该能够在Macintosh项目中正常工作。 一旦这个工作,你可以将其转换为与Macintosh项目兼容的iOS,然后将该代码移动到您的iOS项目。

renderMask()调用是最有趣的部分。 renderTextures()在首行中绘制样本纹理。

static GLuint color_texture; static GLuint mask_texture; static GLuint background_texture; static float window_size[2]; void renderMask() { float texture_x=0, texture_y=0; float x=0, y=0; { glBindTexture(GL_TEXTURE_2D, mask_texture); glDisable(GL_BLEND); glBegin(GL_QUADS); glTexCoord2f(texture_x,texture_y); glVertex2f(x,y); glTexCoord2f(texture_x+1.0,texture_y); glVertex2f(x+512.0,y); glTexCoord2f(texture_x+1.0,texture_y+1.0); glVertex2f(x+512.0,y+512.0); glTexCoord2f(texture_x,texture_y+1.0); glVertex2f(x,y+512.0); glEnd(); } { glBindTexture(GL_TEXTURE_2D, color_texture); glEnable(GL_BLEND); glBlendFunc(GL_DST_COLOR, GL_ZERO); glBegin(GL_QUADS); glTexCoord2f(texture_x,texture_y); glVertex2f(x,y); glTexCoord2f(texture_x+1.0,texture_y); glVertex2f(x+512.0,y); glTexCoord2f(texture_x+1.0,texture_y+1.0); glVertex2f(x+512.0,y+512.0); glTexCoord2f(texture_x,texture_y+1.0); glVertex2f(x,y+512.0); glEnd(); } { glBindTexture(GL_TEXTURE_2D, background_texture); glEnable(GL_BLEND); glBlendFunc(GL_ONE_MINUS_DST_COLOR, GL_DST_COLOR); glBegin(GL_QUADS); glTexCoord2f(texture_x,texture_y); glVertex2f(x,y); glTexCoord2f(texture_x+1.0,texture_y); glVertex2f(x+512.0,y); glTexCoord2f(texture_x+1.0,texture_y+1.0); glVertex2f(x+512.0,y+512.0); glTexCoord2f(texture_x,texture_y+1.0); glVertex2f(x,y+512.0); glEnd(); } } // Draw small versions of the textures. void renderTextures() { float texture_x=0, texture_y=0; float x=0, y=532.0; float size = 128; { glBindTexture(GL_TEXTURE_2D, mask_texture); glDisable(GL_BLEND); glBegin(GL_QUADS); glTexCoord2f(texture_x,texture_y); glVertex2f(x,y); glTexCoord2f(texture_x+1.0,texture_y); glVertex2f(x+size,y); glTexCoord2f(texture_x+1.0,texture_y+1.0); glVertex2f(x+size,y+size); glTexCoord2f(texture_x,texture_y+1.0); glVertex2f(x,y+size); glEnd(); } { glBindTexture(GL_TEXTURE_2D, color_texture); x = size + 16; glBegin(GL_QUADS); glTexCoord2f(texture_x,texture_y); glVertex2f(x,y); glTexCoord2f(texture_x+1.0,texture_y); glVertex2f(x+size,y); glTexCoord2f(texture_x+1.0,texture_y+1.0); glVertex2f(x+size,y+size); glTexCoord2f(texture_x,texture_y+1.0); glVertex2f(x,y+size); glEnd(); } { glBindTexture(GL_TEXTURE_2D, background_texture); x = size*2 + 16*2; glBegin(GL_QUADS); glTexCoord2f(texture_x,texture_y); glVertex2f(x,y); glTexCoord2f(texture_x+1.0,texture_y); glVertex2f(x+size,y); glTexCoord2f(texture_x+1.0,texture_y+1.0); glVertex2f(x+size,y+size); glTexCoord2f(texture_x,texture_y+1.0); glVertex2f(x,y+size); glEnd(); } } void init() { GLdouble bounds[4]; glGetDoublev(GL_VIEWPORT, bounds); window_size[0] = bounds[2]; window_size[1] = bounds[3]; glClearColor(0.0, 0.0, 0.0, 1.0); glShadeModel(GL_SMOOTH); // Load our textures... color_texture = [[NSImage imageNamed:@"colors"] texture]; mask_texture = [[NSImage imageNamed:@"mask"] texture]; background_texture = [[NSImage imageNamed:@"background"] texture]; // Enable alpha blending. We'll learn more about this later glEnable(GL_BLEND); glEnable(GL_TEXTURE_2D); } void draw() { glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glColor3f(1.0, 1.0, 1.0); renderMask(); renderTextures(); } void reshape(int width, int height) { glViewport(0, 0, width, height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, width, 0.0, height); glMatrixMode(GL_MODELVIEW); window_size[0] = width; window_size[1] = height; } 

这显示了我的三个纹理正常绘制(裁剪,stream血和背景),然后在下面合并。

图片