Jump to content
  • Advertisement
Sign in to follow this  
alireza.pir

OpenGL Comparing two textures in openGL

This topic is 907 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm new to OpenGL, in my android game I'm looking for a way to compare two textures to tell me the similarity of two in percent value or in any other way! I know how to to this with two bitmap images but I really need to use a method to compare two textures.

Question is: Is there any way to compare two textures as we compare two images? Like comparing two images pixel by pixel? 

any resources? 

Share this post


Link to post
Share on other sites
Advertisement

Do you need the result on the CPU or is it OK if it stays on the GPU?  If the former, it can be done, but you're not going to get acceptable performance from it, so you may need to totally redesign your algorithm.  It might help us to help you a bit more if you talked a little about why you're doing this, what you want to do with the result, and so on.

Share this post


Link to post
Share on other sites

which version of ogles are you using if 1.0 forget about performance,

for 2.0 and above theres something in glsl like texture2d(for sampler2d)

 

values are between 0..1 for all channels.

 

either way you always load textures from a file so you could do that on cpu when loading textures to opengl.

 

but like mhagain said you must tell us more like which ver of ogles, and what are you trying to do.

Share this post


Link to post
Share on other sites

Do you need the result on the CPU or is it OK if it stays on the GPU?  If the former, it can be done, but you're not going to get acceptable performance from it, so you may need to totally redesign your algorithm.  It might help us to help you a bit more if you talked a little about why you're doing this, what you want to do with the result, and so on.

 

 

which version of ogles are you using if 1.0 forget about performance,

for 2.0 and above theres something in glsl like texture2d(for sampler2d)

 

values are between 0..1 for all channels.

 

either way you always load textures from a file so you could do that on cpu when loading textures to opengl.

 

but like mhagain said you must tell us more like which ver of ogles, and what are you trying to do.

 

 

i am making an origami like game and user folds the paper in 2D Space, i want to check if the final texture is what i wanted the user to make or not, i am using opengl 1! 

does i need to upgrade to 2.0?! and if so, how to achieve this by that?

Share this post


Link to post
Share on other sites

well doing that in es 1.0 is only acceptable if you do the check only once, if you manage to do it after every change of the origami then you are 'screwed'

it really depends on the size of the texture itself (screen size aswell) let me be clear first of all you load image to unisgned char * array then you upload that to texture, 

then depending on the screenspace of origami, (the window where you draw it - this is your compare space you will need to make another unsigned char * base_origami = new unsigned char[that_screen_width * that_screen_height * 4] (i recall that es 1.0 uses 32 bit colorspace [rgba])

then you draw that texture you want to compare (as the base) to that screen and call glReadPixels() where you upload screen to base_origami

then to actually compare textures you will have to draw the actual folded origami, and then again call glReadPixels but to another unsigned char array

HOWEVER this won't guarantee you 100% image recognition, there could be a thing with texture borders so colors wont match additionally you will have to add specific drawing routine to draw only the folded origami without any additional draw calls. (like colored lines etc), also backgrounds of both base origami and folded origami must be the same.

then you go in a loop on cpu side and compare these two arrays.

 

so when you have like 95-100% similarity then you are done they should be the same.

This is really not the thing you would like to go for, so maybe you could post here images of a base image of origami, and the folded one  (not completed but folded somehow)

so then we could think of something else.

 

The same problem is for es 2.0, you will have to use glreadpixels aswell but you could compare each fragment in shader (knowing the floating point inaccuracy) if they do not match you flag output color alpha as lets say 0 and if they do match you flag output red color (you dont change the alpha), then anyway you have to call glreadpixels to find every 0 in alpha value, this would suck either but should be faster a bit. too bad we cant change fragment coord in fragment shader and vertex shader wont interpolate through all pixels in texture so we could write only to another texture 1 pixel and glreadpixels one pixel so we acutally could read it back even in realtime. anyway for es 3.0, you could compare everything in shader and use transformfeedback to fill an array on cpu (or something) so you could actually have rapid check.

 

so again we need more details, because your way won't work as you expect.

Edited by WiredCat

Share this post


Link to post
Share on other sites
Just to add a possible alternative view, might it be easier to remember the list of transformations needed to fold the paper to get the final result and a list of actions taken by the player and simply compare the list and take into account error.

Share this post


Link to post
Share on other sites

well doing that in es 1.0 is only acceptable if you do the check only once, if you manage to do it after every change of the origami then you are 'screwed'

it really depends on the size of the texture itself (screen size aswell) let me be clear first of all you load image to unisgned char * array then you upload that to texture, 

then depending on the screenspace of origami, (the window where you draw it - this is your compare space you will need to make another unsigned char * base_origami = new unsigned char[that_screen_width * that_screen_height * 4] (i recall that es 1.0 uses 32 bit colorspace [rgba])

then you draw that texture you want to compare (as the base) to that screen and call glReadPixels() where you upload screen to base_origami

then to actually compare textures you will have to draw the actual folded origami, and then again call glReadPixels but to another unsigned char array

HOWEVER this won't guarantee you 100% image recognition, there could be a thing with texture borders so colors wont match additionally you will have to add specific drawing routine to draw only the folded origami without any additional draw calls. (like colored lines etc), also backgrounds of both base origami and folded origami must be the same.

then you go in a loop on cpu side and compare these two arrays.

 

so when you have like 95-100% similarity then you are done they should be the same.

This is really not the thing you would like to go for, so maybe you could post here images of a base image of origami, and the folded one  (not completed but folded somehow)

so then we could think of something else.

 

The same problem is for es 2.0, you will have to use glreadpixels aswell but you could compare each fragment in shader (knowing the floating point inaccuracy) if they do not match you flag output color alpha as lets say 0 and if they do match you flag output red color (you dont change the alpha), then anyway you have to call glreadpixels to find every 0 in alpha value, this would suck either but should be faster a bit. too bad we cant change fragment coord in fragment shader and vertex shader wont interpolate through all pixels in texture so we could write only to another texture 1 pixel and glreadpixels one pixel so we acutally could read it back even in realtime. anyway for es 3.0, you could compare everything in shader and use transformfeedback to fill an array on cpu (or something) so you could actually have rapid check.

 

so again we need more details, because your way won't work as you expect.

 

here is some pictures of it (the dashed line is not drawn in opengl and is just a test image on top of opengl render space)

 

1_a2377.jpg

 

2_dc380.jpg

 

3_c29f2.jpg

Share this post


Link to post
Share on other sites

Just to add a possible alternative view, might it be easier to remember the list of transformations needed to fold the paper to get the final result and a list of actions taken by the player and simply compare the list and take into account error.

thats not possible because the user may use different ways to achieve the final shape... 

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!