Sign in to follow this  

how to create gui buttons with DirectX 11?

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

A button is usually nothing more than a virtual rectangle that exists within the client area of a window. As the user moves the mouse just test whether the mouse is inside of each rectangle (perhaps using PtInRect). Now this doesn't help the user much, so you would also have to render some quads on the screen in the same positions using some kind of button texture - perhaps a 'normal' one and a 'mouse over' one. Something like

 

[sharedmedia=core:attachments:24797]

 

Share this post


Link to post
Share on other sites

 

A button is usually nothing more than a virtual rectangle that exists within the client area of a window. As the user moves the mouse just test whether the mouse is inside of each rectangle (perhaps using PtInRect). Now this doesn't help the user much, so you would also have to render some quads on the screen in the same positions using some kind of button texture - perhaps a 'normal' one and a 'mouse over' one. Something like

 

Yeh i understand you render 2 triangles to make a quad, but can you just specify screencoords for the vertexbuffer and it will understand the vertices are pretransform through the pipeline?

Share this post


Link to post
Share on other sites

If you have SpriteBatch available, you can use that ...

            if (srvNormal == null) // then render a simple text button
            {
                // set viewport - sprite batch uses full viewport
                Viewport windowViewport = new Viewport(0, 0, UI.Window.ClientSize.Width, UI.Window.ClientSize.Height);
                UI.Window.Device3D.ImmediateContext.Rasterizer.SetViewport(windowViewport);

                // render background
                if (State == States.Down)
                {
                    UserInterface.SpriteBatch.Begin();
                    SpriteBatchDraw.DrawSolidRectangleAA(new RectangleF(absRegion.X + 2, absRegion.Y + 2, absRegion.Width - 4, absRegion.Height - 4), Color.White);
                    SpriteBatchDraw.DrawSolidRectangleAA(new RectangleF(absRegion.X + 5, absRegion.Y + 5, absRegion.Width - 10, absRegion.Height - 10), Color.Black);
                    UserInterface.SpriteBatch.End();

                     // cost [3949-5037 ticks]
                    RenderTarget renderTarget2D = UI.Window.RenderTarget2d;
                    renderTarget2D.BeginDraw();
                    renderTarget2D.Transform = Matrix.Scaling((absRegion.Width - 4) / (float)absRegion.Width) * Matrix.Translation(2, 2, 0);
                    renderTarget2D.DrawText(Text, TextFormat, new RectangleF(absRegion.X, absRegion.Y, absRegion.Width, absRegion.Height), Brush);
                    renderTarget2D.EndDraw();
                }

If you don't have a SpriteBatch facility, then you're right, you can use screen co-ordinates.

Edited by Gavin Williams

Share this post


Link to post
Share on other sites
No more pretransformed vertices with D3D11 indeed. Even if you pass through vertices (in NDC coordinates) you still need a vertex shader, at which point you can grant it a transformation for convenience. For D3D 9 / Windows style pixel coordinates you use something like this:
[tt]
XMMatrixOrthographicOffCenterLH(0, windowClientWidth, windowClientHeight, 0, 0, 1);[/tt]

Or use the DirectX Tool Kit's sprite batch, as Gavin suggested.

Share this post


Link to post
Share on other sites


Yeh i understand you render 2 triangles to make a quad, but can you just specify screencoords for the vertexbuffer and it will understand the vertices are pretransform through the pipeline?

You can do that, but like unbird says, you need a vertex shader no matter what.  You can make your vertex shader just pass through your pre-transformed vertices without modification, so you can sort of emulate how it worked in D3D9.  As far as creating vertices with 4 coordinates, that isn't an issue at all - you have pretty much free will to create vertex layouts of the size and type that you want.

Share this post


Link to post
Share on other sites

This topic is 1105 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this