Changing background with OpenGl and GLPaint example - ios

I am using Apple's provided example of OpenGl GLPaint, I did brush width, colors select, without big problems, but can't find any way how I could change background color to image. Any suggestions?

Try to define a clear color using glClearColor in your initGL method.

Related

Darken an opaque UIView without blending

My App's background is an opaque UIImageView. Under some circumstances I would like to darken this down in an animated way from full brightness to about 50%. Currently I lower the alpha property of the view and this works well. Because nothing is behind the view, the background image just becomes dark.
However, I've been profiling using the Core Animation Instrument and when I do this, I see that the whole background shows as being blended. I'd like to avoid this if possible.
It seems to me that this would be achievable during compositing. If a view is opaque, it is possible to mix is with black without anything behind showing through. It's not necessary to blend it, just adjust the pixel values.
I wondered if this was something that UIKit's GPU compositing supports. While blending isn't great, it's probably a lot better than updating the image on the CPU, so I think a CPU approach is probably not a good substitute.
Another question asks about this, and a few ideas are suggested including setting the Alpha. No one has brought up a mechanism for avoiding blending though.
An important question here is whether you want the change to using a darkened background to be animated.
Not animated
Prepare two different background images and simply swap between them. The UIImage+imageEffects library could help with generating the darkened image, or give you some leads.
Animated.
Take a look at GPUImage - "An open source iOS framework for GPU-based image and video processing". Based on this you could render the background in to the scene in a darkened way.

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 (i.e. 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:
Stop using GLKit for everything except setting up your rendering
surface with GLKView (which is tedious without GLKit).
Use an OpenGl program with custom shaders to draw to a texture that is backing an FBO.
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:
Intro to shaders
Rendering to a Texture
Shader Toy - This should help you experiment with your post processing frag shader.
GLEssentials example - This shows how to render to texture using OpenGL ( a bit outdated.)
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.

In iOS openGL, how can one create a blend mode similar to kCGBlendModeDarken?

On a white background, drawing lines using kCGBlendModeDarken darkens the areas where different colors meet, like this:
I am trying to recreate this using openGL in iOS, but I don't know how to set the glBlendFunc properties to achieve this. The openGL documentation is hard to grasp for a beginner.
What would be the proper way to achieve this in openGL ES 1_X on iOS?
Assuming that kCGBlendModeDarken is just a regular darkening blend mode, the same effect can be achieved in OpenGL using these two commands:
glBlendFunc(GL_ONE, GL_ONE);
glBlendEquation(GL_MIN);
Depending on your version of OpenGL, GL_MIN might be GL_FUNC_MIN.
I am using glBlendEquationOES(GL_MIN_EXT);.
glBlendFuncSeparate(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA, GL_ONE, GL_ONE_MINUS_SRC_ALPHA);
glBlendEquationSeparate(GL_FUNC_ADD, GL_FUNC_ADD);

Blackberry glossy tabbar

I have a tab bar in my application, however I'm trying to attain a glossy effect like the one featured in the Rhapsody Blackberry app or the Flixster Blackberry app. I've thought about using a PNG, but I'm sure theres a better way to do it programmatically like using a gradient. Any help would be much appreciated!
Flixster app
http://news.cnet.com/i/bto/20091106/Flixter_Curve.png
Rhapsody app (I would prefer to have it look like this)
http://nexus404.com/Blog/wp-content/uploads2/2010/12/blackberry-for-rhapsody-300.jpg
Thanks!
it is indeed possible.
Create a 1 pixelx40pixel (height as much as you want) png image of a gradient which is glossy so we get the glossy effect.
in paint() call graphics.tileRop() passing in Graphics.ROP_SRC_COPY as the Constant for the raster operation to execute.
This will draw the gradient to the extent of the region you set to.
The best way is use PNG for that. If you try to paint gradient programmatically be ready to see non-smooth gradient with visible color lines.

iPad programming, any way to invert color programmatically?

You know how the iPad can invert colors? For the app I'm working on we wanted to have a button that would invert the colors for one of the views the same way. Does the sdk have any kind of support for something like that already?
I don't think that there is an easy way at the moment. CALayer has a compositingFilter property which is described as
A CoreImage filter used to composite
the receiver’s contents with the
background. Animatable.
It's available since iOS 2.0 but it also says:
While the CALayer class exposes this
property, Core Image is not available
in iOS. Currently the filters
available for this property are
undefined.
Core Image would have been ideal for that purpose, it does provide a filter (CIColorInvert) to invert colors.
Another apporach would be: Change the color properties of the UI controls you are using, though I doubt that everything will look they way you expect, as there may be gradients and such. You have to implement your own small algorithm to invert a given color.
I see only the hard way: You have to draw your controls manually with drawRect and invert the colors with your own algorithm there.

Resources