Problems with transparency when creating a texture from a mixture of textures with SDL_2? - transparency

I am trying to create a texture from several textures, but it seems that I have a problem setting the transparency:
SDL_Texture *backgr = SDL_CreateTexture(render,
SDL_PIXELFORMAT_RGBA8888,
SDL_TEXTUREACCESS_TARGET,
width,
height);`<br/>
//After this line, the texture is rendered black
SDL_SetAlphaMod(backgr, 0);
Any ideas?

You need to set blend mode as well to enable Alpha-blending.
SDL_SetTextureBlendMode( backgr , SDL_BLENDMODE_BLEND );

According to the migration guide for SDL2 you shouldn't use SDL_SetAlphaMod with SDL2 but rather SDL_SetTextureAlphaMod (or SDL_SetSurfaceAlphaModfor surfaces). If you need an example on how to use it check out the tutorial by Lazy Foo.

Related

Changing background with OpenGl and GLPaint example

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.

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.

Getting the color of the back buffer in GLSL

I am trying to extract the color behind my shader fragment. I have searched around and found various examples of people doing this as such:
vec2 position = ( gl_FragCoord.xy / u_resolution.xy );
vec4 color = texture2D(u_backbuffer, v_texCoord);
This makes sense. However nobody has shown an example where you pass in the back buffer uniform.
I tried to do it like this:
int backbuffer = glGetUniformLocation(self.shaderProgram->program_, "u_backbuffer");
GLint textureID;
glGetIntegerv(GL_FRAMEBUFFER_BINDING, &textureID);//tried both of these one at a time
glGetIntegerv(GL_RENDERBUFFER_BINDING, &textureID);//tried both of these one at a time
glUniform1i(backbuffer, textureID);
But i just get black. This is in cocos2d iOS FYI
Any suggestions?
You can do this, but only on iOS 6.0. Apple added an extension called GL_EXT_shader_framebuffer_fetch which lets you read the contents of the current framebuffer at the fragment you're rendering. This extension introduces a new variable, called gl_lastFragData, which you can read in your fragment shader.
This question by RayDeeA shows en example of this in action, although you'll need to change the name of the extension as combinatorial points out in their answer.
This should be supported on all devices running iOS 6.0 and is a great way to implement custom blend modes. I've heard that it's a very low cost operation, but haven't done much profiling myself yet.
That is not allowed. You cannot simultaneously sample from an image that you're currently writing to as part of an FBO.

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);

XNA How To Make Transparent Texture

I read and try that pure Magenta Color 255, 0, 255 in an image is automatically transparent, but unfortunately failed, is there anything should I setup first?
Also I read and try include alpha in a PNG Image, the strange is when I used it on a project it's work, but not on the other, what is likely wrong with the code?
Please kindly reply, thank you
For Magenta to be transparent, look at the properties of your image file. Right click in solution explorer, properties.
There is an option there to specify the transparent color to be used (magenta by default).
Then, there is the rendering to consider. How are you drawing your texture? With SpriteBatch, it is automatic, but you may have sorting issues. Check the order in which you draw your textures and the SpriteSortMode.

Resources