# Drawing a square in DX11

This topic is 1845 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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:

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.)

##### 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. :)

##### 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?

##### 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.

##### 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

##### 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);

##### 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).

##### 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
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?

##### 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.

##### 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