我想要做的是在鲜红色(1,0,0,1)之上添加带有alpha(0.1,0,0,0.2)的深红色。对于第一层它工作正常,结果是 (0.9 ,0 ,0, 1); 但是,当红色值达到 0.5 时,它不能低于该值。
第一层用以下等式演示,并且工作正常:
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
ColorBuffer color = (1,0,0,1) Bright Red
SourceColor color = (0.1,0,0,0.2) Dark Red
GL_ONE = 1 , GL_ONE_MINUS_SRC_ALPHA = 1 - 0.2 = 0.8
Cf = (ColorSource * One) + (ColorBuffer * One Minus Src Alpha);
Cf = ((0.1,0,0)*1 ) + ((1,0,0) * 0.8);
Cf = 0.1,0,0 + 0.8,0,0;
Cf = 0.9,0,0 // Here is the result
现在在许多层之后再往下走,它将到达目标颜色更暗的点:0.5,现在颜色永远不会变暗,如下所示,它以 0.5,0,0 开头,但结果为 0.5,0, 0:
Cf = ((0.1,0,0)*1 ) + ((0.5,0,0) * 0.8);
Cf = 0.1,0,0 + 0.4,0,0;
Cf = 0.5,0,0
这是结果,这意味着颜色缓冲区没有改变,我覆盖的颜色不再有任何影响。
如何让我的深红色分层,直到它取代鲜红色?
演示问题的简单处理草图 - 你会注意到这里我正在尝试 GL_SRC_ALPHA 并且它仍然存在这个问题:
http://studionu.net/files/OPENGL_test.zip
下图描述了左边的问题,右边是想要的效果。
编辑这里是代码
所以这是我的代码:
我设置了一个纹理缓冲区对象并附加到一个 FBO:
'gl.glGenTextures(1, drawTex, 0);
gl.glBindTexture(GL.GL_TEXTURE_2D, drawTex[0]);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_S, GL.GL_CLAMP_TO_EDGE);
gl.glTexParameteri(GL.GL_TEXTURE_2D, GL.GL_TEXTURE_WRAP_T, GL.GL_CLAMP_TO_EDGE);
gl.glTexImage2D(GL.GL_TEXTURE_2D, 0, GL.GL_RGBA8, 1000, 500, 0, GL.GL_BGRA, GL.GL_UNSIGNED_BYTE, null); '
' // Creating FBO.
gl.glGenFramebuffersEXT(1, drawFBO, 0);
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, drawFBO[0]);
gl.glFramebufferTexture2DEXT(GL.GL_FRAMEBUFFER_EXT, GL.GL_COLOR_ATTACHMENT0_EXT, GL.GL_TEXTURE_2D, drawTex[0], 0);
int stat = gl.glCheckFramebufferStatusEXT(GL.GL_FRAMEBUFFER_EXT);
if (stat != GL.GL_FRAMEBUFFER_COMPLETE_EXT) System.out.println("FBO error");
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, 0);'
然后我清除帧缓冲区:
' gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, drawFBO[0]);
// Drawing to the first color attachement of drawFBO (this is where drawTex is attached to).
gl.glDrawBuffer(GL.GL_COLOR_ATTACHMENT0_EXT);
gl.glViewport(0, 0, 1000, 500);
gl.glColor4f(0.0, 0.0, 0.0, 0.0);
gl.glClear(gl.GL_COLOR_BUFFER_BIT);
// Unbinding drawFBO. Now drawing to screen again.
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, 0); '
在这里,我绑定帧缓冲区并绘制到其中,并在此处设置颜色膨胀:
' gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, drawFBO[0]);
// Drawing to the first color attachement of drawFBO (this is where drawTex is attached to).
gl.glDrawBuffer(GL.GL_COLOR_ATTACHMENT0_EXT);
// Setting orthographic projection.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0.0, 1000, 0.0, 500, -100.0, +100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glViewport(0, 0, 1000, 500);
gl.glDisable(GL.GL_TEXTURE_2D);
gl.glEnableClientState(GL.GL_VERTEX_ARRAY);
gl.glVertexPointer(2, GL.GL_FLOAT, 0, paintBuffer);
// gl.glHint (gl.GL_POINT_SMOOTH_HINT, gl.GL_NICEST);
// gl.glEnable(gl.GL_POINT_SMOOTH);
gl.glEnable(gl.GL_VERTEX_PROGRAM_POINT_SIZE);
gl.glEnable(gl.GL_BLEND);
gl.glEnable(gl.GL_DEPTH_TEST);
gl.glDepthFunc(gl.GL_NICEST);
gl.glDisable(gl.GL_ALPHA_TEST);
gl.glAlphaFunc(gl.GL_LESS, 0.01);
gl.glPointSize(35.0);
float kBrushOpacity = 0.05/3.0;
println(kBrushOpacity);
float colorchange = 1.0;
if (timer>200) {
colorchange = 0.5; //////COLOR GETS DARKER
}
if (timer>400) {
//colorchange = 0.2; //////COLOR GETS DARKER AGAIN
}
timer++;
gl.glDisableClientState( gl.GL_COLOR_ARRAY );
gl.glColor4f(colorchange, 0, 0.0, kBrushOpacity);
gl.glBlendFuncSeparate(gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_SRC_ALPHA,gl.GL_SRC_ALPHA, gl.GL_ONE_MINUS_DST_ALPHA);////////THIS IS THE OPENGL BLEND EQUATION FOR PREMULTIPLIED COLORS
gl.glDrawArrays(GL.GL_POINTS, 0, count); //Count tells us how many point exist to be drawn.
gl.glDisable(gl.GL_BLEND); //Dont use blend when drawing the texture to screen. Just draw it.
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();
// Unbinding drawFBO. Now drawing to screen again.
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, 0); '
在这里,我在屏幕上绘制纹理:
' gl.glDrawArrays(GL.GL_POINTS, 0, count); //Count tells us how many point exist to be drawn.
gl.glDisable(gl.GL_BLEND); //Dont use blend when drawing the texture to screen. Just draw it.
gl.glDisableClientState(GL.GL_VERTEX_ARRAY);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix();
// Unbinding drawFBO. Now drawing to screen again.
gl.glBindFramebufferEXT(GL.GL_FRAMEBUFFER_EXT, 0);
// Setting orthographic projection.
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glOrtho(0.0, width, 0.0, height, -100.0, +100.0);
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPushMatrix();
gl.glLoadIdentity();
gl.glViewport(0, 0, width, height);
// Drawing texture to screen.
gl.glEnable(GL.GL_TEXTURE_2D);
gl.glActiveTexture(GL.GL_TEXTURE0);
gl.glBindTexture(GL.GL_TEXTURE_2D, drawTex[0]);
gl.glTexEnvf(GL.GL_TEXTURE_ENV, GL.GL_TEXTURE_ENV_MODE, GL.GL_REPLACE);
gl.glBegin(GL.GL_QUADS);
gl.glTexCoord2f(0.0, 1.0);
gl.glVertex2f(0.0, 0.0);
gl.glTexCoord2f(1.0, 1.0);
gl.glVertex2f(width, 0.0);
gl.glTexCoord2f(1.0, 0.0);
gl.glVertex2f(width, height);
gl.glTexCoord2f(0.0, 0.0);
gl.glVertex2f(0.0, height);
gl.glEnd();
gl.glBindTexture(GL.GL_TEXTURE_2D, 0);
gl.glMatrixMode(GL.GL_PROJECTION);
gl.glPopMatrix();
gl.glMatrixMode(GL.GL_MODELVIEW);
gl.glPopMatrix(); '