Strange blending effect in OpenGL ES 2.0 using GLKit - ios

This happens on GLKit with MonoTouch 5.3, but I think the problem may be of general OpenGL ES 2.0 nature.
I have three faces, one of them red, one green and one blue, all with an alpha value of 1.0, so they should be opaque. When they are rendered on the black background, everything is okay. If the green face is in front of the others, everything is okay as well. But if
the red face is in front of the green
or the blue face is in front of one of the others
the foreground color is not rendered, but the background face is fully visible. This seems to be some kind of blending effect, but I don't see anything special in my code, and I have tried out several things like glBlendFunc, but it didn't change anything.
I could post my code, but since the code is quite simple, I thought perhaps someone knows the answer immediately.
Update: As this seems to be a Depth-Sorting issue, here are some important parts of the code:
var aContext = new EAGLContext(EAGLRenderingAPI.OpenGLES2);
var view = this.View as GLKView;
view.Delegate = new GenericGLKViewDelegate(this);
view.Context = aContext;
view.DrawableColorFormat = GLKViewDrawableColorFormat.RGBA8888;
view.DrawableDepthFormat = GLKViewDrawableDepthFormat.Format16;
view.DrawableMultisample = GLKViewDrawableMultisample.Sample4x;
here is the DrawInRect method:
_baseEffect.PrepareToDraw();
GL.Clear(ClearBufferMask.ColorBufferBit | ClearBufferMask.DepthBufferBit);
GL.EnableVertexAttribArray((int)GLKVertexAttrib.Position);
GL.EnableVertexAttribArray((int)GLKVertexAttrib.Color);
GL.VertexAttribPointer((int)GLKVertexAttrib.Position, 3, VertexAttribPointerType.Float, false, 0, _squareVertices);
GL.VertexAttribPointer((int)GLKVertexAttrib.Color, 4, VertexAttribPointerType.UnsignedByte, true, 0, _squareColors);
GL.DrawArrays(BeginMode.TriangleStrip, 0, 9);
GL.DisableVertexAttribArray((int)GLKVertexAttrib.Position);
GL.DisableVertexAttribArray((int)GLKVertexAttrib.Color);
I tried every possible combination of ColorFormat, DepthFormat and Multisample, but it's still the same.
Solution:
I was missing some calls to enable the depth buffer, these calls were missing in the ViewDidLoad method:
GL.ClearDepth(30);
GL.Enable(EnableCap.DepthTest);
GL.DepthFunc(DepthFunction.Lequal);

This does not sound like a blending issue- rather a depth-sorting issue.
Is the depth buffer enabled? Are you clearing it with each frame?

Related

Blending in Metal: alpha set to 0 is still opaque

I'm having trouble setting up blending in Metal. Even when starting with the Hello Triangle example provided by Apple, using the following code
pipelineStateDescriptor.colorAttachments[0].blendingEnabled = YES;
pipelineStateDescriptor.colorAttachments[0].sourceAlphaBlendFactor = MTLBlendFactorZero;
pipelineStateDescriptor.colorAttachments[0].destinationAlphaBlendFactor = MTLBlendFactorZero;
and the fragment function
fragment float4 fragmentShader(RasterizerData in [[stage_in]]) {
return float4(in.color.rgb, 0);
}
the triangle still draws completely opaque. What I want to achieve in the end is blending between two shapes by using different blending factors, but I thought I would start with a simple example to understand what is going on. What am I missing?
sourceAlphaBlendFactor and destinationAlphaBlendFactor are to do with constructing a blend for the alpha channel. i.e. they control the alpha that will be written into your destination buffer, which will not really be visible to you. You are probably more interested in the RGB that is written into the frame buffer.
Try setting values for sourceRGBBlendFactor and destinationRGBBlendFactor instead. For traditional alpha blending set sourceRGBBlendFactor to MTLBlendFactorSourceAlpha and set destinationRGBBlendFactor to MTLBlendFactorOneMinusSourceAlpha

Sketch App Paint Blending OpenGLES

Can anyone suggest why my low opacity painting does this weird blending, while the SketchBookX app does it perfect?
In both images attached the vertical strokes on the left are done at full opacity, the strokes on the right are done at low opacity. The top image is mine and as you can see the strokes on the right at low opacity turn a orange-red color and don't blend/mesh with the full opacity strokes. But the SketchBookX app blends perfectly and maintains the same color.
I'm using glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA) and have tried many variations with no luck, so I'm starting to think there are other things that are giving me this problem.
Do I need to handle this problem in the fragment shader? I currently have this,gl_FragColor = color * rotatedTexture; I'm using PNGs for brush textures.
UPDATE: Im getting the same results without using a texture. gl_FragColor = color;
I want it to be like mixing ink, not like mixing light :)

Alpha blending in Direct3D 9. Some of the primitives aren't rendering behind the texture

I enabled alpha blending in my game, but some of the primitives aren't rendering behind the transparent texture.
Here are my render states:
d3ddevice->SetRenderState(D3DRS_LIGHTING, true);
d3ddevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CW);
d3ddevice->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
d3ddevice->SetRenderState(D3DRS_AMBIENT, D3DCOLOR_XRGB(15, 15, 15));
d3ddevice->SetRenderState(D3DRS_NORMALIZENORMALS, TRUE);
d3ddevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3ddevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3ddevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3ddevice->SetRenderState(D3DRS_BLENDOP, D3DBLENDOP_ADD);
d3ddevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
d3ddevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
d3ddevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
d3ddevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
d3ddevice->SetTextureStageState( 0, D3DTSS_TEXTURETRANSFORMFLAGS, D3DTTFF_DISABLE );
d3ddevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_NONE);
d3ddevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_NONE);
d3ddevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_NONE);
What happens here is that the transparent objects still get written to the depth buffer and therefore block objects which lie behind them from being rendered. There are (at least) two ways to solve this.
Sort all transparent objects in your scene such that they are rendered from back to front. You need to do this in your code, D3D won't handle this for you. Here is an MSDN article on this.
It looks that all objects that you are rendering are either fully transparent or fully opaque at any given point. In this case, there is a simpler way to deal with transparency: you can use alpha testing to discard all fragments which have 0 alpha (or alpha below a certain threshold, say 128). Here is an article on how to do this. If you are using shaders, you can use the discard command to discard all transparent fragments.

Making opaque layer in OpenGL ES 2.0 on iOS.

I'm working on a simple 3D application with OpenGL ES 2 on iOS.
I just followed steps in "OpenGL ES Programming Guide for iOS" in Apple Developer Site.
I wanted make the OpenGL View entirely opaque for better performance as suggested in the document. So, I did as below.
CAEAGLLayer *eaglLayer = (CAEAGLLayer *)self.layer;
eaglLayer.opaque = TRUE;
And, I ran the application with the Core Animation instrument. Then, I turned on 'Color Blended Layers' in Debug Options in Core Animation instrument.
Then, the entire screen became reddish which means the entire view was being blended. I tested another example OpenGL apps from Apple, but they were all greenish with the instrument.
The document dosen't say anything about this except making the layer opaque just like I did.
What else could there be possible cause for this problem?
Be sure to set the alpha component to 1.0 when you call glClearColor(0.0, 0.0, 0.0, 1.0).

Cocos2D 2.0 - Changing color of a CCParticle

So after messing around with my game, there's a thing that I wanted to share with you that I discovered about CCParticle.
How do I change the color of the particles in a CCParticleSystem?
When I was on cocos 1.x, I could change it with startColor, endColor, but in 2.0, whenever I do it, it stays black. Why?
A:In Cocos2D 2.0, it stays the same, but unlike in 1.x, you must first make sure that the particle texture is white, because otherwise it will not blend with the color you want! Make sure too that the texture is correctly added, because if not, it will show big black pixels and that's probably not the result you want!

Resources