• Advertisement
Sign in to follow this  

Sprite Masking

This topic is 4361 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 a beginner to DirectX and Windows, but not a beginner to programming. I have a long history in Macromedia (now Adobe) Flash, and am currently attending the DigiPen Institute of Technology (going into my sophomore year next year). I know my way around C/C++. That said, I was hoping to do a Mode 7 racing game (ala Super Mario Kart or F-Zero) for my project next year. I've already done something similar in Flash, so I know how it works. However, the one dilema I've encountered has to do with masking... In Flash, I'd create multiple rectangles and use those to mask respective copies of the level pic, then do the scaling and rotation, which of course creates the illusion of perspective. Flash has a handy little function called setMask() that makes it quite simple. The problem is that I have no idea how to do masking like this with DirectX. I've looked all over MSDN and Google to no avail. I'm crossing my fingers I overlooked something simple. Any ideas? Thanks, Marshall

Share this post


Link to post
Share on other sites
Advertisement
You could probably do it with something like alpha blending. I presume that the mask you are talking about has some opaque color values and the a alpha mask. This can be done in Direct3D, although I don't recommend it as Direct3D provides you with a foundation to easily get perspective.

Open the documentation and go to
DirectX Graphics --> Direct3D9 --> Programming Guide and start reading from there.

I hope this helps.
Take care.

Share this post


Link to post
Share on other sites
The project for next year must be 2D, I believe. I'm not even sure if I can do Mode 7 yet, I'm still waiting on a response from the instructor.

The mask I'm talking about is just a simple rectangle. The only opacity is on or off. Here's a picture of what I'm talking about...

http://i3.tinypic.com/xdfrdj.jpg

Each red rectangle is a mask that is only displaying a specific part of the image. Is there no easy way to do this with DirectX?

Share this post


Link to post
Share on other sites
Alpha blending or alpha testing. With alpha testing when you load in a texture in d3d (using the D3DXLoadTextureFromFileEx function), you can spcify a color key. The texture loading function will load your texture and manipulate the color values of your texture. If you set a green (0xff00ff00) key for example, it will check each pixel in your texture and make all the pixel that match your key transparent black (i.e. set them to 0x00000000).

Then when you are rendering the texture, you tell d3d to ignore pixels that have an alpha value below some threshold, and those pixels will not be shown. Check out SetRenderState and D3DRS_ALPHATESTENABLE

Share this post


Link to post
Share on other sites
I'm not trying to mask specific colors on the map though. Any pixel on the map could potentially be shown if you drive over it.

Here's another example:
http://i2.tinypic.com/xdgjuf.jpg

This rectangle is a lot bigger than the actual thing, but it's the same idea. The map is rotated around depending on the turning you make while driving. The map's x/y position shifts as you drive along. What you actually see is only on that rectangle, which makes up a fraction of what's actually drawn. There's multiple copies of the same thing. When drawing the screen, as the y pos on the screen goes down, the mask gets larger (vertically), and the map scales larger. Line all these up and it creates the illusion of depth. (See the pic in my last post for the effect.)

All I need it to do is show what's in the red rectangle, and hide everything outside of it.

Share this post


Link to post
Share on other sites
Guys are you sure he's not just asking about texture coordinates?? Sounds to me like that's the best way to pick out a rectangle from a larger image to be (drawn/distorted/skewed/etc) to make the fake 3D effect. You'll be using Direct3D polygons but in a 2D way, which is very common. Just my $.02

Share this post


Link to post
Share on other sites
You can display a specific part of an image by loading the image as a texture, then give each vertex texture coordinates.

But you probably want to approach this in a fundamentally different way if you are using Direct3D. Draw the ground as a big 3D quad using the ground racetrack image as the texture.

Share this post


Link to post
Share on other sites
From your second example (that you posted while I was writing the first reply!) I definitely think you're looking for info on texture coordinates. The hardest part is going to be calculating the (tu,tv) pairs for each of the four corners of that red box (which I'm sure you've already thought about). Once you do that you can render that part of the texture into your perspective polys, and Direct3D will handle any stretching/skewing/etc as necessary.

Just for starters, the texture coordinates (tu/tv) are inserted into every vertex that you render. Typically one quad requires 6 vertices (2 triangles), but with index buffers you can cut it to four vertices and reuse the 2 overlapping ones.

Just out of curiousity, why must the perspective view be subdivided into so many little rectangles (as you show in the first picture)? Couldn't you do it all in one go?

Share this post


Link to post
Share on other sites
Ok, I'll look into that then.

It's not possible to do it in one go, because behind each mask the image is a different size. The lower on the screen the mask is, the larger the image it's masking is. If you did it all on one go, you'd be looking at a flat map.

Here's a link to the Flash version I have:
http://flash.isnan.net/index.php?height=300&width=600&bg=FFFFFF&url=kart.swf&name=Mario%20Kart%20Engine

Look close and you can see where the masks are.

Flash is abysmal at handling 3D stuff (and is just flatout slow in general), so this masking technique actually works quite well. If it turns out I can do 3D, I'll just do a 3D quad, as that sounds easier.

Please forgive my general ignorance on the subject. The most advanced stuff I've done with DirectX so far has been Breakout.

Share this post


Link to post
Share on other sites
Sorry for the double post, but I'm a bit lost on this.

In Flash, the mask was incredibly easy to make. All I had to do was use the drawing API and tell it the physical coordinates on the screen to make a rectangle. Then just use that rectangle for a mask. No fancy math needed.

Obviously I can't do this with DirectX, and I know MasterWorks mentioned finding the (tu,tv) pairs for the 4 corners. Where do I tell DirectX to use these pairs? I know in the ID3DXSprite::Draw() function, you can provide a RECT that it will grab. Problem is, I'm doing rotation, and the function doesn't seem to account for that. Is there some other way I can grab a specific area from the texture using 4 coordinates?

Share this post


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

  • Advertisement