Separate mask and texture: Blit AND / OR effect?

BlitzMax Forums/BlitzMax Programming/Separate mask and texture: Blit AND / OR effect?

Mr. Write Errors Man(Posted 2007) [#1]
I am sleepy and I must be missing something, but this problem puzzles me.

I have three images that are masks. First is a circle, second a triangle and third a star.

I also have five other images that are different textures. Wood, sand, silver, gold and snow.

I want to be able to draw the texture images by using the mask images for mask definition. For example draw a silver star or snow triangle.

In the old days this was easy to achieve with classical blitting operators AND and OR. You first blitted the mask with AND operation, which resulted in a black area the shape of the mask drawn on the background. Then you blitted the texture image with OR operation, which resulted in the texture image being draw on the background just on the black area created by the AND operation.

How do I achieve the same result with standard BlitzMax functionality, without doing it by creating pixmaps of all the possible combinations "by hand"?

I want to avoid using pixmaps because of memory and speed issues: in reality I have a great number of different masks and textures.

-AF


ImaginaryHuman(Posted 2007) [#2]
You can't do it with standard BlitzMax, without resorting to making direct calls to DirectX or OpenGL, because there is no `and` and `or` graphics operations in Max2D.

In modern graphics api's these days you have to think in terms of using an alpha channel as a mask, using 8-bit data to decide at greater than what value the channel represents draw and below what value it represents no draw, as in SetBlend MaskBlend. But the alpha channel has to be `attached to` (ie embedded within) the RGB color data all as one stream of data (ie an RGBA-like format). Since you want the mask to be selectable separate from the color data, it would entail drawing the mask to the backbuffer to the destination alpha channel only (meaning 32-bit color modes only), draw the RGB color image to the RGB channels only (alpha off) and then grabbing the backbuffer back into a texture. Alternatively you'd need to play with something like glEnable(gl_Color_Logic_Op) in OpenGL to draw using AND and OR modes. Again this is not standard Max2D stuff. Things have moved on since the `old days` and the api does not do things in the same mindset. Since alpha channels are much more modern and advanced the idea of doing cookie-cutter and's and or's is `not how it's meant to work`.

Another method is to think of stencils, ala the old BlitzBasic on the Amiga, whereby you could use OpenGL's stencil buffer to mask off an area of the screen using your `alpha` or `luminosity` image, and then set things up to draw only to those pixels when you draw your RGB image over the top. Again that requires extra custom GL code since Max2D doesn't include any kind of stencilling.

There are ways to do what you want but you have to get into writing your own OpenGL or DirectX code.


Mr. Write Errors Man(Posted 2007) [#3]
Thanks for your response Daniel, even if it confirmed my fears that I wasn't just sleepy. I don't like the idea of writing my own OpenGL and DirectX code to get AND and OR fuctionality, so I'll do some tests with pixmaps. Hopefully speed and memory issues are manageable.

-AF