简体   繁体   中英

DirectX 11 Sprite 2D Camera

Long time reader. First time poster. I got a problem that has been destroying brain cells for some hours now and getting no where fast. So I thought I'd shove it on here to get a fresh pair of eyes on it...

Basically I'm trying to create a 2D game using C++ and DirectX 11. I've used C# and XNA before and I've made a few 3D things in DirectX but nothing this big.

I'm trying to draw a sprite on the center of my screen using a basic camera and basic sprite batch for now but not a thing is showing up and I can't for the life of me figure out why. I'll post some code to give an idea.

This is where I'm setting all my matrices in the camera class and they stay that way (for now, will change later to move etc):

XMStoreFloat4x4(&m_world, XMMatrixIdentity());
XMStoreFloat4x4(&m_proj, XMMatrixOrthographicOffCenterLH(0.0f, width, height, 0.0f, 0.0f, 1.0f));

m_position = XMFLOAT3(width / 2, height / 2, 0);
m_look = XMFLOAT3(0, 0, 1);
m_up = XMFLOAT3(0, 1, 0);

XMVECTOR p = XMLoadFloat3(&m_position);
XMVECTOR l = XMLoadFloat3(&m_look);
XMVECTOR u = XMLoadFloat3(&m_up);

XMStoreFloat4x4(&m_view, XMMatrixLookToLH(p, l, u));

This then gets passed into the sprite batch begin method which passes the info to the shader:

D3D11_MAPPED_SUBRESOURCE data;
ZeroMemory(&data, sizeof(data));
m_deviceContext->Map(m_cBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &data);
ConstantBuffer* cb = (ConstantBuffer*)data.pData;

XMMATRIX w = XMLoadFloat4x4(&world);
XMMATRIX v = XMLoadFloat4x4(&view);
XMMATRIX p = XMLoadFloat4x4(&proj);

w = XMMatrixTranspose(w);
v = XMMatrixTranspose(v);
p = XMMatrixTranspose(p);

XMStoreFloat4x4(&world, w);
XMStoreFloat4x4(&view, v);
XMStoreFloat4x4(&proj, p);

cb->World = world;
cb->View = view;
cb->Proj = proj;

m_deviceContext->Unmap(m_cBuffer, 0);

And finally the shader itself:

cbuffer ConstantBuffer : register(b0)
{
    float4x4 World;
    float4x4 View;
    float4x4 Proj;
};

SamplerState sam : register(s0);
Texture2D tex : register(t0);

struct VertexIn
{
    float3 Position : POSITION;
    float2 Texture : TEXCOORD;
    float4 Color : COLOR;
};

struct PixelIn
{
    float4 Position : SV_POSITION;
    float2 Texture : TEXCOORD;
    float4 Color : COLOR;
};

PixelIn VS(VertexIn vin)
{
    float4 position = float4(vin.Position, 1.0f);
    position = mul(position, World);
    position = mul(position, View);
    position = mul(position, Proj);

    PixelIn vout;
    vout.Position = position;
    vout.Texture = vin.Texture;
    vout.Color = vin.Color;

    return vout;
}

float4 PS(PixelIn pin) : SV_Target
{
    float4 textureColor = tex.Sample(sam, pin.Texture);

    return pin.Color * textureColor;   
}

As you can see, pretty simple stuff. But nothing gets drawn to the screen at all. I've tried not bothering with the matrices at all, leaving out the world matrix, using a perspective projection matrix, using look at rather than look to matrix, using (0, 0, 0) as center via XMMatrixOrthographicLH and converting sprite positions to screen space.

I have even greatly simplified the sprite batch to restricting it to only draw one sprite at a time!

I'm using an immutable index buffer (0, 1, 2, 0, 2, 3) and a dynamic vertex buffer which gets updated as follows:

D3D11_MAPPED_SUBRESOURCE data;
ZeroMemory(&data, sizeof(data));
m_deviceContext->Map(m_vBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &data);
Vertex* vertex = (Vertex*)data.pData;

for(UINT i = 0; i < sprite->Vertices.size(); ++i)
{
    vertex[i] = sprite->Vertices[i];
}

m_deviceContext->Unmap(m_vBuffer, 0);

m_deviceContext->DrawIndexed(6, 0, 0);

I used similar methods for rendering 3D models. In fact that was harder because I had a dynamic index buffer and a lot more constant buffer data and the shader was a lot more complicated.

I thought it might be a problem with data getting lost somewhere but it all seems to get passed round and through properly right up to the DrawIndexed method call. I've double checked buffer creation and states and currently have it on CULL_NONE just to make sure it's just not being culled.

I'll post buffer creation and sprite creation for clarity:

HRESULT result = S_OK;

device->GetImmediateContext(&m_deviceContext);

D3D11_BUFFER_DESC cbd;
ZeroMemory(&cbd, sizeof(cbd));
cbd.ByteWidth = sizeof(ConstantBuffer);
cbd.Usage = D3D11_USAGE_DYNAMIC;
cbd.BindFlags = D3D11_BIND_CONSTANT_BUFFER;
cbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
cbd.MiscFlags = 0;
cbd.StructureByteStride = 0;

result = device->CreateBuffer(&cbd, 0, &m_cBuffer);

if (FAILED(result))
{
    return 0;
}

vector<short> indices;
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
indices.push_back(0);
indices.push_back(2);
indices.push_back(3);

D3D11_SUBRESOURCE_DATA indexData;
ZeroMemory(&indexData, sizeof(indexData));
indexData.pSysMem = &indices;

D3D11_BUFFER_DESC ibd;
ZeroMemory(&ibd, sizeof(ibd));
ibd.ByteWidth = 6 * sizeof(short);
ibd.Usage = D3D11_USAGE_IMMUTABLE;
ibd.BindFlags = D3D11_BIND_INDEX_BUFFER;
ibd.CPUAccessFlags = 0;
ibd.MiscFlags = 0;
ibd.StructureByteStride = 0;

result = device->CreateBuffer(&ibd, &indexData, &m_iBuffer);

if (FAILED(result))
{
    return 0;
}

D3D11_BUFFER_DESC vbd;
ZeroMemory(&vbd, sizeof(vbd));
vbd.ByteWidth = 4 * sizeof(Vertex);
vbd.Usage = D3D11_USAGE_DYNAMIC;
vbd.BindFlags = D3D11_BIND_VERTEX_BUFFER;
vbd.CPUAccessFlags = D3D11_CPU_ACCESS_WRITE;
vbd.MiscFlags = 0;
vbd.StructureByteStride = 0;

result = device->CreateBuffer(&vbd, 0, &m_vBuffer);

if (FAILED(result))
{
    return 0;
}

return 1;

Sprite:

ID3D11Resource* resource;
ZeroMemory(&resource, sizeof(resource));
texture->GetResource(&resource);

ID3D11Texture2D* tex = (ID3D11Texture2D*)resource;

D3D11_TEXTURE2D_DESC t;
ZeroMemory(&t, sizeof(t));
tex->GetDesc(&t);

Vertex v[4];
ZeroMemory(&v, sizeof(v));

v[0].Position = XMFLOAT3((float)destinationRectangle.value.left, (float)destinationRectangle.value.top, z);
v[1].Position = XMFLOAT3((float)destinationRectangle.value.right, (float)destinationRectangle.value.top, z);
v[2].Position = XMFLOAT3((float)destinationRectangle.value.right, (float)destinationRectangle.value.bottom, z);
v[3].Position = XMFLOAT3((float)destinationRectangle.value.left, (float)destinationRectangle.value.bottom, z);

v[0].Texture = XMFLOAT2((float)(sourceRectangle.value.left / t.Width), (float)(sourceRectangle.value.top / t.Height));
v[1].Texture = XMFLOAT2((float)(sourceRectangle.value.right / t.Width), (float)(sourceRectangle.value.top / t.Height));
v[2].Texture = XMFLOAT2((float)(sourceRectangle.value.right / t.Width), (float)(sourceRectangle.value.bottom / t.Height));
v[3].Texture = XMFLOAT2((float)(sourceRectangle.value.left / t.Width), (float)(sourceRectangle.value.bottom / t.Height));

v[0].Color = color;
v[1].Color = color;
v[2].Color = color;
v[3].Color = color;

Sprite* sprite = new Sprite();
sprite->Vertices.push_back(v[0]);
sprite->Vertices.push_back(v[1]);
sprite->Vertices.push_back(v[2]);
sprite->Vertices.push_back(v[3]);
sprite->Texture = texture;

m_sprites.push_back(sprite);

Am I going mad? Am I being stupid? Or is there just simply an easier way to do this? Thanks. Any help is appreciated!

Also just like to point out that the sprite I'm drawing has a rectangle (left = 300, right = 500, top = 350, bottom = 450) on a 800x600 screen with an all white color. The texture is being loaded correctly and I've even tried just outputting color in the shader just to make sure with no luck.

Oh and the Z value of the sprite I'm trying to draw is set to 0.5f although I have tried setting this to different values but this is the one that I was expecting to work with it.

I found the answer thanks to MJP over at another website. The problem was this line:

indexData.pSysMem = &indices;

As it should have been this:

indexData.pSysMem = &indices[0];

Apparently, I was sending a pointer to the entire vector rather than the first instance so the index buffer had no idea what I was on about!

Like I said, I've only just started DirectX programming from XNA programming. ;)

I hope this helps someone else in a similar position.

Did you configure the DepthStencil for using 2D? Also try a negative Z to camera position, you put it on 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM