We are about to switch to a new forum software. Until then we have removed the registration on this forum.
Inside a fragment shader I have a loop like this:
for (float x = 0; x < vertTexCoord.x; x += xInc) {
vec2 pos = vec2(x, vertTexCoord.y);
fcount += texture(texture, pos).r; // red for now
}
In this case we use red, but I wan't to be able to switch to green, blue and alpha as well. What is an easy way to switch to whatever channel I want from inside processing? (And how will that solution impact performance)
Answers
Create a shader for each channel or add a number option, maybe? Shader for each channel might be the least prone to errors (I've had shaders ignoring variables that were sent from Processing).
You can access vector fields just like an array. Just add some uniform that holds the channel index, let's say
channel
, and use it in your loop like this:And here a complete example (that probably won't work on your Mac):
Press 1, 2, or 3 to change the selected channel.
And no, this should have no noticeable impact on the performance.
Really cool how flexible the vectors are! Thanks again. I learned so much from you today :)
Haha :D Don't mention it! I'm looking forward to see some "funky" shader based sketches from you... maybe a Depth of Field. ;)
I was working on a Integral image shader yesterday. I made that before using the cpu. With a Integral Image you count up the total seen from the left top corner. So If I have a image and those are the values for red for example:
Then the integral Image looks like:
Now to get the average color of an area you can use:
So this is really fast (once the Integral image is computed).
But in the evening I figured it's better to use cuda for that :) The problem with a shader is that for the bottom right pixel for example it has to loop over every pixel in the image to compute the total count for that one. This slows down the shader a lot.
In case your interested, this is how far I came. (I didn't do the channel thing yet since I was to tired)
I made this before using a integral image:
10000 rectangles are drawn each frame with the average of the color of the image for that rectangle.
O yeah, I only had the horizontal pass ready in the shader!