Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Multiple layers and colorizing thereof  (Read 1567 times)

0 Members and 1 Guest are viewing this topic.

Garbeld

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Multiple layers and colorizing thereof
« on: December 14, 2012, 09:54:32 pm »
I have a variable length array of largeish (probably 640x480, enough to fill the app's window) images, from which I will take 5 (henceforth "layers") and draw them in order.
Each image is greyscale, and layer 2 will be drawn without change; layers 0,1,3,4 will be colorized and transparent (pixels will be rendered as red in layers 0,1, and blue in layers 3,4, in proportion to how dark they are).

I'm going to be drawing a lot of images (again, all greyscale) to layer 2, with frequent scale and rotation shifts; thus, I assume that it would be fastest and easiest (fewest lines of code, and not having to learn unfamiliar functions and/or math) for these layers to be rendertextures and drawing on layer 2 to be done with sprites. Assuming colorizing the layers is simple enough, then everything's basically solved and done. However, I'm not sure what the best way to go about that would be.

I'm guessing that the "correct" answer would involve shaders, but I'm not terribly familiar with them (or graphics cards, openGL, anything like that). A cursory search for shader tutorials didn't help me much.
« Last Edit: December 14, 2012, 10:00:50 pm by Garbeld »

Haikarainen

  • Guest
Re: Multiple layers and colorizing thereof
« Reply #1 on: December 17, 2012, 07:37:40 pm »
If  I've understood your post correctly, you have several options;

When rendering the rendertextures using a sprite, you have an option in sf::Sprite thats called "setColor". Take a look at this; http://www.sfml-dev.org/documentation/2.0/classsf_1_1Sprite.php#a14def44da6437bfea20c4df5e71aba4c

The other options is shaders, if you feel like you want to learn glsl. You'd want a glsl shader. Take a look at "uniform sampler2D" and "gl_FragColor". You'd want to pass each layer texture to a shader through the sampler, then set the pixels/fragments accordingly with gl_FragColor

Garbeld

  • Newbie
  • *
  • Posts: 2
    • View Profile
    • Email
Re: Multiple layers and colorizing thereof
« Reply #2 on: December 18, 2012, 06:23:33 am »
When rendering the rendertextures using a sprite, you have an option in sf::Sprite thats called "setColor". Take a look at this; http://www.sfml-dev.org/documentation/2.0/classsf_1_1Sprite.php#a14def44da6437bfea20c4df5e71aba4c
On re-reading my post, I realize that my sentence was rather ambiguously phrased (I'm terrible at succinct explanations, one might tell): as a source pixel's color approaches black, the drawn pixel's color should approach red (or blue). So far as I'm aware, sf::Sprite::setColor will only allow me to do the opposite (as a source pixel's color approaches white ...).

I've since noticed that I can pass a blend mode when drawing sprites, and this seems to mostly work; simply draw an image consisting of a block of flat color atop the desired layers with sf::BlendAdd. Clear my window with white, draw my lower layers in, color them red, draw my non-coloured layer.
The only problem is that this doesn't seem to preserve transparency - I draw to an sf::RenderTexture and it has the correct colors, but is fully opaque. And I can't sf::BlendAdd a block of blue directly to the window after drawing my red layers, or else they turn magenta.
« Last Edit: December 18, 2012, 06:27:16 am by Garbeld »

 

anything