Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

rick_appleton

Correct image swapping onMouseOver with alpha

This topic is 5325 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 working on my UI system, and I can currently show alphablended quads, and correctly flip this texture on mouse over. However, this flipping takes place when the mouse comes over the quad, not the visible texture. So what I really need to do is (I think) first detect if the cursor is within the quad, then calculate the position on the texture, then get the alpha value for that point. If alpha>certainValue do the swap of the image. Normally I dump my image data once it''s been passed to openGL, so I can''t easily get the alpha value. I could make a kind of hotspot on the quad, but this requires extra work (not that I''m opposed to that mind you), but it would be much more convinient if I could just use tha alpha. What other possibilities could be used to do this?

Share this post


Link to post
Share on other sites
Advertisement
You can get the X pixel out of the GL context IIRC, but I also remember its quite slow, why not make your quads smaller, or the texture fit to them exactly or are we talking a curvy interface here?

Share this post


Link to post
Share on other sites
Hmm bit of a tricky one :| If it was fixed size then I would day just create a hotspot bitmap and use a function (seperate from GL) to test that, but it depends on how the texture has been displayed and scaled on your context. Im sure it can be done.. Some how.

Share this post


Link to post
Share on other sites
This is exactly my problem too, so I can''t give any solutions, but this is what I''ve read so far - I''ve just started with openGL so this might be very basic or downright wrong, but for what it''s worth:

You could assign a unique color to every object near the mouse click, render that small area to the backbuffer only and use glReadPixels to get the pixel under the mouse. I guess it would slow things down a bit, but not as much as keeping a separate bitmap, would it?

You''d have to disable everything that modifies the color, like lighting and dithering and use an alpha test to "quantize" the alpha (e.g. everything below .5 is considered transparent) in order to get flat colors.

The thing I can''t figure out is how to render a quad with the alpha from the texture and a solid color instead of the image data without generating a new texture, but my guess (and hope...) is that it should be doable.

Anyone?

If you find a good solution to this on your own, please post it.


Share this post


Link to post
Share on other sites
Well, so far as I know, reading from the videocard is very slow. So my best solution is storing a imagemap with 1byte (or even 1 bit) per pixel. The when determining if the mouse is ''over'' just check that ''texture'' map.

If you use 1 bit per pixel (so 8 pixels per byte) then an image of 128x64 (which seems to me to be a nice size for a button image) could be stored as a 1 kByte array. So the overhead isn''t too large.

Share this post


Link to post
Share on other sites
Oh, ok that would probably work fine, as long as you have no scaling or perspective - which you don''t I suppose since it''s an interface. If nothing moves or changes much except for texture flipping, wouldn''t it be easier to build one array in memory with index colors i.e. if array[10,10] = 20 then it''s button 5. Then you can skip picking all together?

My problem is that stuff is moving and scaling on screen (it''s a very light tile/sprite 2D engine) and I still need to pick based on the alpha. Reading from the screen buffer can''t be THAT slow, can it? I just have to render a very minimal sceen area and need only read one pixel.

Anyway, I think I''ve figured out a (clumsy) way of doing it so I''ll try - if you''re interested I''ll let you know how it worked out.

Share this post


Link to post
Share on other sites
hmm, I''m not sure what you mean with that array. Could you explain that with a bit more detail?

I''m not sure about the reading speed of a single pixel. You could try it. And yes please do let me know how it works.

It''s true that my idea works better for static object (UI elements) and less well for moving objects I think.

Share this post


Link to post
Share on other sites
Personally, I would just keep a copy of the texture in RAM. If you can''t for some reason, your can probably use low-threshold alphatest and stencil buffer (set the stencil bits to an id number that''s unique to each window). Reading just one pixel shouldn''t be that slow. This can of course conflict with any other use of stencil in your program, ie. you might have to clear it multiple times per frame.

Share this post


Link to post
Share on other sites

  • 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!