• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
trick

Advice for 2d in a 3d world

6 posts in this topic

After being out of programming since Directx 8, I decided recently to get back into it. So, I jumped right in with Directx 11 and started working on a library of objects to ease the process once I start to actually make something. When it came to doing 2D stuff (for user interfaces, etc) I found two primary options on the various sites I went to.

The first, is to interop Direct2D with Direct3D. This seemed like a lot of work to do, and I didn't need all the functionality of Direct2D. The second, was to use Direct3D but use an ortho matrix in place of the projection matrix. I'm sure it's something on my part, but I could never get this working correctly, plus the examples I've seen would either use a non-constant buffer for the vertex data, or have to recreate it each time the sprite was to move.

So, I came across another "solution". My question is, it seems far to simple to not have problems, otherwise why wouldn't it have been done that way in place of the ortho matrix. Does anyone see any potential problems with doing 2D sprites in this way?

When I create the quad to display the 2D image, I give it pixel size of the width and height to create it. I also store a Position value, in pixels, of where the quad should be displayed. I built a seperate vertex and pixel shaders to be used with the 2D images, where the screen size and quad position are loaded into a constant buffer. Vertex Shader below, where the vertex position is added to the position (pX and pY) in the buffer, multiplied by 2 and divided by screen size (to get the vertex position in the range of 0.0 to 2.0), then reduced by 1 to get range of -1.0 to 1.0

Vertex Shader:
[CODE]
//Sprites.vs
//--------------------------------------------------------------------------------------
// Buffers
//--------------------------------------------------------------------------------------
cbuffer ModelVCBSTRUCT : register( b0 )
{
float sWidth;
float sHeight;
float pX;
float pY;
}
//--------------------------------------------------------------------------------------
// Typedefs
//--------------------------------------------------------------------------------------
struct VertexInput{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
};
struct PixelInput{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PixelInput VS( VertexInput input )
{
PixelInput output;
output.Pos = input.Pos;
output.Pos[0] = (((output.Pos[0] + pX) * 2) / sWidth) - 1;
output.Pos[1] = (((output.Pos[1] + pY) * 2) / sHeight) - 1;
output.Tex = input.Tex;

return output;
}
[/CODE]

Again, I just want to know if this will have some problem with it, as the solution seems far too simple compared to the other options I've seen so far. Thanks in advance for feedback! [img]http://public.gamedev.net//public/style_emoticons/default/biggrin.png[/img]
0

Share this post


Link to post
Share on other sites
With this method, as it currently stands, for every sprite you want to render you have to update the cbuffer, this means you can draw ONE sprite per draw call before having to do what is quite an expensive operation on the CPU side and will likely stall the GPU too.

You would be better served by using an Ortho matrix, using instancing to draw one quad multiple times and passing the x,y location via a vertex stream. While it might seem slightly counter intuative to update a buffer just because something has moved it will still be faster once you start drawing more than one object on the screen as you'll remove a lot of CPU cost by being able to draw multiple sprites in one batch.
0

Share this post


Link to post
Share on other sites
[quote name='phantom' timestamp='1327272430' post='4905240']
You would be better served by using an Ortho matrix, using instancing to draw one quad multiple times and passing the x,y location via a vertex stream. While it might seem slightly counter intuative to update a buffer just because something has moved it will still be faster once you start drawing more than one object on the screen as you'll remove a lot of CPU cost by being able to draw multiple sprites in one batch.
[/quote]

The SpriteRenderer class in my sample framework does exactly this, if you need some code to look at.
0

Share this post


Link to post
Share on other sites
Thanks for the responses, though it seems there's something about it I'm having trouble grasping. For 3D models, you'd need to pass the model matrix in each object, each frame whereas the other matrices wouldn't need changed that often. How is it such a larger hit when doing this with two floats for position to do the sprite, vs the 16floats required for a matrix to render a 3d model?

Also, if you pass the ortho matrix once, and then render all of your 2d sprites sending the position in the vertex data, doesn't that limit you to one size for all sprites (unless you also send a world matrix, or some other data in the constant buffer for it to modify each one)?
0

Share this post


Link to post
Share on other sites
It's not about passing data, it's about batching to reduce draw calls. Draw calls and state modification can consume a lot of CPU time if you use too many of them.
0

Share this post


Link to post
Share on other sites
Thanks for the input guys! Looks like I need to do some more research on instancing, using ortho matrix, etc.! [img]http://public.gamedev.net//public/style_emoticons/default/blink.png[/img]
0

Share this post


Link to post
Share on other sites
One last question on the subject. If the object here is to use instancing, to reduce the number of draw calls being made, I see two things I want to know if I'm correct on.

1) The textures need to be in one sprite-sheet for all sprites, so that the texture buffer can be updated once prior to calling draw function.

2) Couldn't the same idea be applied without the ortho matrix, like the vertex shader below?

[CODE]
//Sprites.vs
//--------------------------------------------------------------------------------------
// Buffers
//--------------------------------------------------------------------------------------
cbuffer OnScreenUpdate : register( b0 )
{
float sWidth;
float sHeight;
}
//--------------------------------------------------------------------------------------
// Typedefs
//--------------------------------------------------------------------------------------
struct VertexInput{
float4 Pos : POSITION;
float2 Tex : TEXCOORD0;
float2 sPos : TEXCOORD1;
};
//sPos = screen position

struct PixelInput{
float4 Pos : SV_POSITION;
float2 Tex : TEXCOORD0;
};

//--------------------------------------------------------------------------------------
// Vertex Shader
//--------------------------------------------------------------------------------------
PixelInput VS( VertexInput input )
{
PixelInput output;
output.Pos = input.Pos;
output.Pos[0] = (((output.Pos[0] + sPos[0]) * 2) / sWidth) - 1;
output.Pos[1] = (((output.Pos[1] + sPos[1]) * 2) / sHeight) - 1;
output.Tex = input.Tex;

return output;
}
[/CODE]
0

Share this post


Link to post
Share on other sites

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  
Followers 0