Jump to content
  • Advertisement
Sign in to follow this  
harmless

Clipping a Textured Quad in Direct3D9?

This topic is 4073 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

Dear Sirs, I am currently working on my 2D sprite class to handle the graphics portion of my GUI engine, and my current setup is as follows: 1. Using Direct3DGraphics, fixed pipeline. 2. Uses vertex buffers and textures to form textured quad to hold 2d image. 3. uses orthogonal camera settings to simulate 2D. My problem now is this: Say a 10x50 pixel sized button is a child control of a dialog box and the dialog box has been resized to the point that only 50% of the button is visible. Therefore i need to clip the image that represents the button. How do i actually do this? Before, i was using iDirect3DSurface9 interface to handle 2D images, and just like back in the days of DirectDraw, clipping was pretty much straight forward. This time i am trying to use textures and direct3d vertices as i believe it's the next step forward. Any thoughts and replies are greatly appreciated. cheers!

Share this post


Link to post
Share on other sites
Advertisement
First thought is to try to create a UI framework that doesn't clip, scaling or moving instead, so that it continues to work when the window size changes.

Second thought is that if you do decide to go with a fixed layout, you can use pre-transformed vertices with pixel locations instead of an orthogonal transformation.

Thirdly, I'm trying to understand the problem. If I understand correctly before you were rendering to an off-screen surface, then copying just the right portion? And now you're rendering directly, and everything scales?

In this case, just scale it back. If you use pre-transformed vertices things will automatically work correctly.

Share this post


Link to post
Share on other sites
Quote:
Original post by ET3D
First thought is to try to create a UI framework that doesn't clip, scaling or moving instead, so that it continues to work when the window size changes.

Second thought is that if you do decide to go with a fixed layout, you can use pre-transformed vertices with pixel locations instead of an orthogonal transformation.

Thirdly, I'm trying to understand the problem. If I understand correctly before you were rendering to an off-screen surface, then copying just the right portion? And now you're rendering directly, and everything scales?

In this case, just scale it back. If you use pre-transformed vertices things will automatically work correctly.


thanks for your reply!


Basically, my problem is how to clip a quad. Say i have a texture of 32x32 and i only wanted to render a portion of it, say the texture is divided into 4 quadrants and i only want to render the first quadrant. I know i can just set the U and V values when initializing my vertex buffers.

but how do i do it in real-time without having to lock the vertex buffer? Please note that i am on fixed pipeline mode.

Share this post


Link to post
Share on other sites
Use SetViewport. I use this in my GUI to do exactly what you want.

Only downside: You have to check if the given range is outside the screen. In that case you have to modify the viewport and if you're outside the top or left border apply an offset to the GUI drawing.

Don't forget to reset the viewport to fullscreen after you're done with the GUI. The viewport affects everything, even the 3d rendering.

Share this post


Link to post
Share on other sites
Quote:
Original post by Endurion
Use SetViewport. I use this in my GUI to do exactly what you want.

Only downside: You have to check if the given range is outside the screen. In that case you have to modify the viewport and if you're outside the top or left border apply an offset to the GUI drawing.

Don't forget to reset the viewport to fullscreen after you're done with the GUI. The viewport affects everything, even the 3d rendering.


Thanks a lot. Setting viewport seems like a very good idea. However i have some concerns. Will it cost me performance hit if i do frequent calls to setting viewport every frame?

Share this post


Link to post
Share on other sites
Quote:
Original post by harmless
Quote:
Original post by Endurion
Use SetViewport. I use this in my GUI to do exactly what you want.

Only downside: You have to check if the given range is outside the screen. In that case you have to modify the viewport and if you're outside the top or left border apply an offset to the GUI drawing.

Don't forget to reset the viewport to fullscreen after you're done with the GUI. The viewport affects everything, even the 3d rendering.


Thanks a lot. Setting viewport seems like a very good idea. However i have some concerns. Will it cost me performance hit if i do frequent calls to setting viewport every frame?


Well i did try setting viewport for the first time. However, i noticed that changing the viewport seems like scaling the screen. This results in my image being stretched as well. It's not what i want to do.

Say i have a texture of 128x128 pixel size loaded into a quad and i only wanted to render a portion of it, say 64x64. to do this, i need to set my vertex like this:

// top-left vertex
v[0].x = 0.0f;
v[0].y = 0.0f;
v[0].u = 0.0f;
v[0].v = 0.0f;

// top-right vertex
v[1].x = 128.0f;
v[1].y = 0.0f;
v[1].u = 0.5f;
v[1].v = 0.0f;

// bottom-left vertex
v[2].x = 0.0f;
v[2].y = 128.0f;
v[2].u = 0.0f;
v[2].v = 0.5f;

// bottom-right vertex
v[3].x = 128.0f;
v[3].y = 128.0f;
v[3].u = 0.5f;
v[3].v = 0.5f;

This works well if i only need to render a fixed portion of the texture all the time. but what if the value of u and v should change in real-time? do only way i can do this so far is try to lock the vertex buffer and change the value of u and v during each frame, and i am afraid it will be performance costly so i am finding a better way to do it.



Share this post


Link to post
Share on other sites
Quote:
Original post by harmless
and i am afraid it will be performance costly so i am finding a better way to do it.
I don't see what choice you have here, really; you're talking about dynamic data so you're going to have to update the vertex information (texture coordinates, specifically) at some point or other. Use a dynamic vertex buffer with NOOVERWRITE/WRITEONLY etc. and you can get very good performance, if not great performance. The only other thing I can think of would be to use a texture transformation matrix; you might be able to come up with a way to clip using one. But, I'm averse to such solutions because they require you to break the batch (ie set matrix, render quad, set matrix, render.... which is suboptimal in a lot of cases.)

Share this post


Link to post
Share on other sites
Quote:
Original post by MasterWorks
Quote:
Original post by harmless
and i am afraid it will be performance costly so i am finding a better way to do it.
I don't see what choice you have here, really; you're talking about dynamic data so you're going to have to update the vertex information (texture coordinates, specifically) at some point or other. Use a dynamic vertex buffer with NOOVERWRITE/WRITEONLY etc. and you can get very good performance, if not great performance. The only other thing I can think of would be to use a texture transformation matrix; you might be able to come up with a way to clip using one. But, I'm averse to such solutions because they require you to break the batch (ie set matrix, render quad, set matrix, render.... which is suboptimal in a lot of cases.)


Thanks a lot. I guess i have to choose this brute force method of locking vertices then if i stick to fixed pipeline mode.

After some research, this problem that i have could easily be solved if i was using vertex shaders as it gives me much more flexibility manipulating the vertex buffers in real-time. even more so i can change vertex diffuse on the fly among other things.

However, as someone who just got back from a long hiatus in game development, im kinda left behind with the latest APIs and such and i want to be comfortable with fixed pipline D3D before i decide to jump in to shaders.

I'll just have to keep note of myself that my way of rendering me 2D sprite is not the best way out there. But as long as it's working during development stage of my project, i'll stay with it and will just revisit the issue once i'm in optimization stage.

thanks again for the sweet advice.

Share this post


Link to post
Share on other sites
I think i found a good solution to this problem of mine. And that's by using stencil buffer.

So i setup a vertex buffer and transform it to represent the rectangular area where i can render. It acts as a "stencil", as the name implies hehe.

Then after i render it, i set my render states back to normal then render my actual 2d images. Anything outside the stencil is clipped.

The only thing that i need is turn on stencil buffer. But i guess it's fine because i believe it's now a typical feature in every video cards out there.

Performance hit though, is something i haven't checked yet when using stencil buffers.

Share this post


Link to post
Share on other sites
Quote:
Original post by harmless
Performance hit though, is something i haven't checked yet when using stencil buffers.
The performance hit isn't from using the stencil buffer really, it's from rendering the geometry twice.

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!