Drawing Shape on top of Sprites

Started by
30 comments, last by nlraley 13 years, 5 months ago
I am currently using sprites to fill my background. If I wanted to draw a rectangle on my map, what would I need to do?

Can I get by with creating a Vertex Buffer, locking that, and generating a quad after I have rendered the background?

[Edited by - nlraley on October 26, 2010 4:08:59 PM]
Advertisement
Would this just be a manner of locking the memory buffer and drawing the shape as I would normally create a shape with vertices?
After calling ID3DXSprite::Begin(), you could disable Z-writes (SetRenderState(D3DRS_ZWRITEENABLE, FALSE);). That way the sprite won't write anything to the Z buffer (Although I'm not sure if it currently does anyway), and you can render whatever you like on top.
And just create it by creating a 4 point vector and calling draw primitive?
Quote:Original post by nlraley
And just create it by creating a 4 point vector and calling draw primitive?
However you like. If you just want to draw a single colour, then yes, I'd fill a vertex buffer with the 4 points and draw them as a quad. If you want a texture applied, you could also use ID3DXSprite again.
What is going to be the best way to do this?

After I call my draw sprite should I create me vertices that I need for all my rectangles, then create a new vertex buffer, lock it, then pass it the size of my vertices?

What I was wanting to do is go through a list of my objects, which I know their x,y positions and everything ( some of which aren't going to be in view ) and then draw a rectangle for each of these, but not sure exactly how to go about doing that on top of a sprite.

Here is what I am trying atm, here is my Render:
//-------------------if ( SUCCEEDED( hr = d3d9Device->BeginScene() ) ){    //-------------------    //  Scene Rendering takes place Here    //-------------------    // Draw the triangles in the vertex buffer. This is broken into a few    // steps. We are passing the vertices down a "stream", so first we need    // to specify the source of that stream, which is our vertex buffer. Then    // we need to let D3D know what vertex shader to use. Full, custom vertex    // shaders are an advanced topic, but in most cases the vertex shader is    // just the FVF, so that D3D knows what type of vertices we are dealing    // with. Finally, we call DrawPrimitive() which does the actual rendering    // of our geometry (in this case, just one triangle).    //-------------------  Begin Sprite Drawing    d3d9Sprite->Begin(D3DXSPRITE_ALPHABLEND );    //-------------------  Draw the Map Tiles    DXTexture dxt = DXTexture(this);    dxt.DrawSprites();    //-------------------  Draw the Navigation Control    hr = d3d9Sprite->Draw(        navTexture,        NULL,        &D3DXVECTOR3(0.0f, 0.0f, 0.0f),        &D3DXVECTOR3(0.0f, 0.0f, 0.0f),        D3DCOLOR_ARGB(255, 255, 255, 255)    );    if (FAILED(hr))        return;    //-------------------    DrawIntersections();    //-------------------    DrawVehicles();    //-------------------  End Sprite Drawing    d3d9Sprite->End();    //-------------------  Draw Test Zone    DrawZones();    //-------------------  End the Scene    d3d9Device->EndScene();}//-------------------  Present the Backbuffer contents to the displayd3d9Device->Present( NULL, NULL, NULL, NULL);//-------------------


As you see, after I draw my sprites I call DrawZones, which is supposed to draw my rectangles. Here is that function:
VOID* pVertices;Vertex vertices[] ={    {0.0f, 0.0f, 2.0f, 1.0f, 0, 0, 0xff0000ff},    {256.0f, 0.0f, 2.0f, 1.0f, 1, 0, 0xff0000ff},    {0.0f, 256.0f, 2.0f, 1.0f, 0, 1, 0xff0000ff},    {256.0f, 256.0f, 2.0f, 1.0f, 1, 1, 0xff0000ff}};//-------------------if( FAILED( d3d9VertexBuffer->Lock( 0, sizeof( vertices[4] ), ( void** )&pVertices, 0 ) ) )    return E_FAIL;memcpy( pVertices, vertices, sizeof( vertices[4] ) );d3d9VertexBuffer->Unlock();//-------------------if (FAILED( hr = d3d9Device->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2 ) ) ){    const char* szError = DXGetErrorDescription9(hr);    MessageBox (MainForm->Handle, szError, "Error", MB_OK );    return E_FAIL;}


This does not fail; however, there is no rectangle being drawn. Any ideas?

[Edited by - nlraley on October 26, 2010 3:50:46 PM]
Well, I'm not really sure why the quad isn't being rendered.

I created my vertex with 4 points. Locked the VertexBuffer. Filled the VertexBuffer with the points. Unlocked the VertexBuffer. Then called DrawPrimitive telling it to draw the 2 primitives using TRIANGLESTRIP. However my sprites show up yet my primitive does not.

I'm sure I'm probably missing something minor.
I think you've got your vertices in the wrong order. For a triangle strip to be drawn as a quad, the vertex order is lower-left, upper-left, lower-right, upper right. Try:

{0.0f, 256.0f, 2.0f, 1.0f, 0, 0, 0xff0000ff},
{0.0f, 0.0f, 2.0f, 1.0f, 1, 0, 0xff0000ff},
{256.0f, 256.0f, 2.0f, 1.0f, 0, 1, 0xff0000ff},
{256.0f, 0.0f, 2.0f, 1.0f, 1, 1, 0xff0000ff}

Jon Hellebuyck - Mode13.com
I'll give it a try but in the examples in the SDK adding a 4th point like this:
CUSTOMVERTEX vertices[] =    {        {   0.0f,   0.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color        { 250.0f,   0.0f, 0.5f, 1.0f, 0xff00ff00, },        {   0.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },		{ 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },    };


And declaring it a strip draws the rectangle perfectly fine.

I didn't think the order mattered as long as the ends "connected" properly? For example, a triangle strip uses 1 point to define each triangle. The last 2 points from the previous triangle are used in creating the next triangle and basically connect to the new point.

Using my example would work as I have:
A B




C

Creating triangle ABC, when I add a 4th point as such:
A B




C D

The new triangle uses points B and C, previously used in Triangle ABC to create triangle BCD. Your example works the same way but in a different order.

[Edited by - nlraley on October 26, 2010 3:24:03 PM]
Although, oddly enough, that solution worked. That's odd. Was the source I read wrong?

This topic is closed to new replies.

Advertisement