简体   繁体   English

基于GLPaint的OpenGL ES混合问题

[英]GLPaint based OpenGL ES Blending Issue

I'm working on an app based on Apple's GLPaint sample code. 我正在开发基于Apple的GLPaint示例代码的应用程序。 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). 通过使用glReadPixels分别渲染每种颜色并将其与先前渲染的图像合并(即使用苹果的默认混合)来获得“期望结果”。

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? 但是,将每个帧与之前的帧混合在一起动作太费时间,如何让OpenGL正确混合颜色? 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. 仅使用GLKit是不可能的。

Luckily you can mix GLKit and OpenGL ES. 幸运的是,你可以混合使用GLKit和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). 除了使用GLKView设置渲染表面之外,不要再使用GLKit(这在没有GLKit的情况下很乏味)。
  2. Use an OpenGl program with custom shaders to draw to a texture that is backing an FBO. 使用带有自定义着色器的OpenGl程序绘制到支持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. 一个很好的起点是加载Xcode附带的OpenGl模板。 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. 它绘制了2个立方体,一个使用GLKit,一个没有 - 使用自定义着色器。

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 . 最后,如果您真的非常认真地使用OpenGL ES来充分发挥它的潜力,那么您真的应该花时间阅读OpenGL ES 2.0编程指南 Even though it is 6 years old, it is still relevant and the only book I've found that explains all the concepts correctly. 即使它已经有6年了,它仍然是相关的,我发现的唯一一本正确解释所有概念的书。

Your "Current Result" is additive color, which is how OpenGL is supposed to work. 您的“当前结果”是加色,这是OpenGL的工作方式。 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. 您无法使用OpenGL ES 1.1控制它,但您可以为OpenGL ES 2.0编写一个自定义片段着色器,它将使颜色变浅。 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. 如果要混合来自iOS的纹理图像,则需要知道图像数据是否已经被alpha预乘,以便进行混合。 OpenGL ES expects the non-premultiplied format. OpenGL ES需要非预乘格式。

You need to write that code in the function which is called on color change. 您需要在颜色更改调用的函数中编写该代码。 and each time you need to set BlendFunc. 每次你需要设置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 要使用BlendFunc执行更多操作,请使用此链接

Please specify if it works or not. 请说明它是否有效。 It work for me. 它对我有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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