• 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
Inuyashakagome16

Drawing a square in DX11

9 posts in this topic

I'm having a bit of trouble at the moment. 

I'm reading Frank Luna's "Introduction to 3D Game Programming with DirectX 11" (great book so far by the way) and going through tutorials on 

http://www.braynzarsoft.net/index.php?p=DX11Lessons < Also pretty awesome. Well one of the exercises at the end of the 4th tutorial off of the braynzarsoft site, is to " Try to draw a square" which they give you a hint, it's "Vertex Buffer." However, I cannot seem to figure out how to change the code / add to it to make this happen. I've heard a few times that you need to use two triangles to make a square in DX (and maybe a few others libraries) but I'm still not sure about that either. I've read this the tutorials and through some of the book I mentioned and I just can't find it. I've search on here, and through google for a while but with no avail. 

 

The full source is here:

 

http://pastebin.com/XaJvnB1B <

 

Because the hint is "Vertex Buffer" i thought maybe it would be in this section of code:

 

//set the vertex and pixel shaders
        Vertex v[] =
        {
                //X / Y/ Z
                Vertex(0.0f, 0.5f, 0.5f),
                Vertex(0.5f, -0.5f, 0.5f),
                Vertex(-0.5f, -0.5f, 0.5f),
                //Vertex(0.2f, 0.5f, -0.5f),
        };
 
        D3D11_BUFFER_DESC vertexBufferDesc;
        ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
 
        vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
        vertexBufferDesc.ByteWidth = sizeof(Vertex) * _countof(v); //was * 3
        vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
        vertexBufferDesc.CPUAccessFlags = 0;
        vertexBufferDesc.MiscFlags = 0;
 
        D3D11_SUBRESOURCE_DATA vertexBufferData;
 
        ZeroMemory(&vertexBufferData, sizeof(vertexBufferData));
        vertexBufferData.pSysMem = v;
        hr = d3d11Device->CreateBuffer(&vertexBufferDesc, &vertexBufferData, &triangleVertBuffer);

 

But i'm still not sure how to advance in all honestly. I know that the "Vertex v[]" and the coordinates given are to point to where which line should go (?) but after playing with it and attempting to add an extra vertex, it didn't seem to update after i compiled and ran it. 

 

(For the record, this is DX11, and i'm using Visual Studio 2010 Ultimate.)

 

0

Share this post


Link to post
Share on other sites

You're on the right track that you need to provide four vertices.  Actually, you can approach this one of (at least) two ways:

 

1) With 4 vertices.  This will require an index buffer, because each triangle needs 3 points, but with indexing, you can re-use the two shared vertices.

2) With 6 vertices.  This just needs you to indicate the 3 verts for each triangle that makes up the square.

 

Another hint: the vertices as declared in your code above create an equilateral triangle in the center of the view.  If you want to make a square, you'll need to create two right-triangles.  This means you'll need to change the X and Y values of all the verts.  And make sure to declare verts in correct winding order. :)

1

Share this post


Link to post
Share on other sites

So if i was going to go with the 6 vertices, I would go with:

 

        Vertex v[] =
        {
                //X / Y/ Z
                Vertex(0.0f, 0.5f, 0.5f),
                Vertex(0.5f, -0.5f, 0.5f),
                Vertex(-0.5f, -0.5f, 0.5f),
                
                Vertex(X.Xf, X.Xf, -X.Xf),
                Vertex(X.Xf, -X.Xf, X.Xf),
                Vertex(-X.Xf, -X.Xf, X.Xf),
                
        };

 

Correct? However, when I put that in and compile, nothing changes on the screen. I have changed the values in my own code to attempt to move around the triangles, placing them in a winding order, but it still doesn't show. Is there a value I should be changing else where? 

0

Share this post


Link to post
Share on other sites

You also need to change the line that reads "d3d11DevCon->Draw(3,0);" so that it knows you want it to draw 6 vertices instead of 3.

2

Share this post


Link to post
Share on other sites

Winding order just refers to the order in which vertices are "read" from their buffer.  Since you're using non-indexed primitives, the order you declare them is the order they're read.  So if your renderer describes "front-facing" polygons as wound clock-wise, you'd make sure they were listed like your example (so if you were to trace between verts as listed, you'd be going in a clockwise  rotation).

 

For a standard square, with this established (clockwise) winding order, and given four corner locations, you'd likely draw

upper right, lower right, lower left, (triangle one)

followed by

lower left, upper left, upper right. (triangle two)

 

When you say "it still doesn't show" do you mean the second triangle, or anything at all?

Edited by BCullis
1

Share this post


Link to post
Share on other sites
You also need to change the line that reads "d3d11DevCon->Draw(3,0);" so that it knows you want it to draw 6 vertices instead of 3.

^ You were totally right. I didn't know i needed to do that. >_<

 

Winding order just refers to the order in which vertices are "read" from their buffer.  Since you're using non-indexed primitives, the order you declare them is the order they're read.  So if your renderer describes "front-facing" polygons as wound clock-wise, you'd make sure they were listed like your example (so if you were to trace between verts as listed, you'd be going in a clockwise  rotation).

 

For a standard square, with this established (clockwise) winding order, and given four corner locations, you'd likely draw

upper right, lower right, lower left, (triangle one)

followed by

lower left, upper left, upper right. (triangle two)

 

When you say "it still doesn't show" do you mean the second triangle, or anything at all?

 

It didn't show because I didn't indicate to d3d11DevCon->Draw that I was drawing 6 instead of 3. Which, many thanks to adt7 for that info. 

Now I was able to draw a square/rectangle. 

 

	Vertex v[] =
	{
		//X / Y/ Z
		Vertex(0.5f, 0.5f, 0.5f),
		Vertex(0.5f, -0.5f, 0.5f),
		Vertex(-0.5f, -0.5f, 0.5f),
		Vertex(-0.5f, 0.5f, 0.0f),
		Vertex(0.5f, 0.5f, 0.0f),
		Vertex(0.5f, 0.5f, 0.5f),
	};

 

I believe that's setup correctly. It took a minute after reading what you wrote to understand how each of those numbers were corresponding to a point in the window.

But, did I do it correctly? I feel like the 3rd field there (Z) isn't really setup correctly but it shouldn't matter considering it's not really 3D. (?) 

 

I also changed the topology from TRIANGLESTRIP to LINESTRIP to see it all together before switching back to TRIANGLESTRIP. Which now that makes sense as to what it does. :P

d3d11DevCon->IASetPrimitiveTopology(D3D11_PRIMITIVE_TOPOLOGY_TRIANGLESTRIP);
0

Share this post


Link to post
Share on other sites

That Z value will matter if it clips into the nearplane of your view frustum.  But I don't know how you're setting up any kind of view/projection matrices.  If you even are.
What I can tell you is with those 6 verts, if you're "looking" down the Z axis, it won't draw you a square.  Unless you left out the negative signs on the last vertex (i.e. if its values were (-.5, -.5, .5) you would get a square).

 

1

Share this post


Link to post
Share on other sites

Okay. The only thing I'm aware of that I'm doing view wise is:

        //create the viewport
        D3D11_VIEWPORT viewport;
        ZeroMemory(&viewport, sizeof(D3D11_VIEWPORT));
 
        viewport.TopLeftX = 0;
        viewport.TopLeftY = 0;
        viewport.Width = Width;
        viewport.Height = Height;
 
        //set the viewport
        d3d11DevCon->RSSetViewports(1, &viewport);
        return true;

 

If that's what you mean.

I haven't gotten into displaying a square / shape as a 3D object just yet. I know its coming though very soon though. 

 

On the same (kind of) topic, if you were (or anyone for that matter) to draw shapes in DX, is this how it would be done? Or would an external program be used and then the object just imported?

0

Share this post


Link to post
Share on other sites

The nice thing about learning how to work with vertices is that it scales.  Once you can draw one triangle, you can draw 10,000 triangles.  So once you're serious about getting 3d shapes into your game, you usually go the route of 3d models exported from modeling tools -> importing into memory (and accessing their vertex information) -> drawing vertex buffers made from said information.

 

If you're talking about 2D shapes, this is still the way to draw them in DX11 (if they're vertex-based).  I'm actually writing a tool for my own game that will let me edit layers of 2d vert-based shapes for GUIs: I'd much rather drag, drop, and tweak vertices than hard-code values into each one.

1

Share this post


Link to post
Share on other sites

That's awesome! I kind of figured as much honestly. :P It's nice to know anyway about how its done. :) Thank you sir! I know I'll probably be back to ask more in a few weeks

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