Reputation: 83
I'm writing a program which draws some plygons which should imitate input (raster)image. After drawing shapes I need to compare my rendered image with input one and then decide if I achive my goal or not. Currently i'm usuing CIMG library to draw. Whole program runs on CPU. I tried to use SFML which uses OpenGL for drawing - rendering was extreme fast, but copying image from GPU to CPU (to make coamparison with input image) take very long. I want to:
Unfortunatelly I don't know if it (comparing two images) is possible to achive with OpenGL. If it is possible I will learn OpenGL or any other library which allows to do my task very fast.
Upvotes: 2
Views: 2476
Reputation: 171167
Here's an outline of a possible approach. They're really just tips to get you started.
Render your image to a texture (I'll call it Render
) and copy the original into another texture (I'll call it Orig
). Disable any texture filtering.
Create one fragment shader that will compute the difference. In pseudo-code:
vec3 render = texture2D(Render, fragmentPosition.xy);
vec3 orig = texture2D(Orig, fragmentPosition.xy);
gl_FragColor.r = (render - orig).dot(render - orig);
Run this fragment shader on a quad the size of your image, sending the output into another texture (I'll call it Difference
). You might need to enable rectangular textures.
Create another fragment shader to compute partial sums:
float sum = 0;
for (row = 0; row < imageHeight; ++row) {
sum += texture2D(Difference, vec2(fragmentPosition.x, row));
}
gl_FragColor.r = sum;
Run this fragment shader on a quad sized image_width x 1
, sending the output into another texture (PartialSum
).
Either read back PartialSum
to the CPU (it's much smaller) and sum it there, or create a single-fragment quad and run a modified summation shader to arrive at one number, than read that back.
Upvotes: 6