简体   繁体   中英

GLPaint based OpenGL ES Blending Issue

I'm working on an app based on Apple's GLPaint sample code. I've changed the clear color to transparent black and have added an opacity slider, however when I mix colors together with a low opacity setting they don't mix the way I'm expecting. They seem to mix the way light mixes, not the way paint mixes. Here is an example of what I mean:

在此输入图像描述

The "Desired Result" was obtained by using glReadPixels to render each color separately and merge it with the previous rendered image (ie using apple's default blending).

However, mixing each frame with the previous is too time consuming to be done on the fly, how can I get OpenGL to blend the colors properly? I've been researching online for quite a while and have yet to find a solution that works for me, please let me know if you need any other info to help!

From the looks of it, with your current setup, there is no easy solution. For what you are trying to do, you need custom shaders. Which is not possible using just GLKit.

Luckily you can mix GLKit and OpenGL ES.

My recommendation would be to:

  1. Stop using GLKit for everything except setting up your rendering surface with GLKView (which is tedious without GLKit).
  2. Use an OpenGl program with custom shaders to draw to a texture that is backing an FBO.
  3. Use a second program with custom shaders that does post processing (after drawing above texture to a quad which is then rendered to the screen).

A good starting point would be to load up the OpenGl template that comes with Xcode. And start modifying it. Be warned: If you don't understand shaders, the code here will make little sense. It draws 2 cubes, one using GLKit, and one without - using custom shaders.

References to start learning:

Finally, if you are really serious about using OpenGL ES to it's full potential, you really should invest the time to read through OpenGL ES 2.0 programming guide . Even though it is 6 years old, it is still relevant and the only book I've found that explains all the concepts correctly.

Your "Current Result" is additive color, which is how OpenGL is supposed to work. To work like mixing paint would be substractive color. You don't have control over this with OpenGL ES 1.1, but you could write a custom fragment shader for OpenGL ES 2.0 that would do substractive color. If you are blending textures images from iOS, you need to know if the image data has been premultiplied by alpha or not, in order to do blending. OpenGL ES expects the non-premultiplied format.

You need to write that code in the function which is called on color change. and each time you need to set BlendFunc.

CGFloat red , green, blue;
// set red, green ,blue with desire color combination
glBlendFunc(GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glColor4f(red* kBrushOpacity,
          green * kBrushOpacity,
          blue* kBrushOpacity,
                      kBrushOpacity);

To do more things by using BlendFunc use this link

Please specify if it works or not. It work for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM