Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


DirectX 11 2D Camera Problem?


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
7 replies to this topic

#1 Bash Mills   Members   -  Reputation: 108

Like
0Likes
Like

Posted 05 January 2013 - 08:57 PM

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.


Sponsor:

#2 Nusakan   Members   -  Reputation: 444

Like
0Likes
Like

Posted 06 January 2013 - 02:19 AM

Could this be the problem?

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);
I am not sure why you would transpose your world and projection matrix.

To map your model from object space to screen space;
- you first multiply it with a world(aka model matrix) to transform your model in to world space.no need to transpose it as far as I'm aware of it.
- After transforming the model into world space, you multiply with view matrix in order to transform into eye space. View matrix is the inverse matrix of the camera's rotation and position. Thus it makes sense to transpose the view matrix (only if your matrix is orthogonal) however, you also need to invert the position component.
- After transforming your model in to eye space, you multiply with the projection matrix in order to map into screen. again i don't see why you would need a transpose of the projection matrix here.

Edited by Nusakan, 06 January 2013 - 02:38 AM.


#3 Bash Mills   Members   -  Reputation: 108

Like
0Likes
Like

Posted 06 January 2013 - 04:01 AM

Hi. Thanks for your reply. I was made aware that DirectX 11 uses row major matrices but HLSL uses column major matrices so this transpose was necessary for this version of DirectX unlike previous versions. I have the same code in my 3D application that works fine. I did also try commenting them three lines out just in case but still no luck whatsoever. I am using a XMMatrixLookTo to calculate my view matrix from position, look and up vectors. But I've also tried setting each component manually using an additional right vector. No luck there either.

 

Thanks for your help though :)



#4 Bash Mills   Members   -  Reputation: 108

Like
0Likes
Like

Posted 06 January 2013 - 04:04 AM

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.



#5 Bash Mills   Members   -  Reputation: 108

Like
0Likes
Like

Posted 06 January 2013 - 04:30 AM

I've also just tried using a LookAt view matrix using a look vector that should be pointing directly at the central sprite position (400, 300, 0.5f) and it still doesn't show anything!

 

I think something is aiming to misbehave!



#6 Bash Mills   Members   -  Reputation: 108

Like
0Likes
Like

Posted 06 January 2013 - 07:17 AM

Well I just converted my index buffer to a dynamic rather than immutable and updated every frame using the same indices (0, 1, 2, 0, 2, 3) along with the vertex buffer and now it works? Must be something wrong with my creation of the immutable index buffer then?



#7 MJP   Moderators   -  Reputation: 11585

Like
0Likes
Like

Posted 06 January 2013 - 02:50 PM

You do indeed have a problem with initializing your index buffer. You have this, which is wrong:

 

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;


The problem is that you're passing the address of the std::vector itself, which isn't what you want. You want to pass an address to the array of data being managed by the std::vector. To get this address, std::vector has the "data" method which returns a pointer to its internal array:

 

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.data();


The "data" member is actually a recent addition to the C++ spec, so if you're using an older compiler (VS 2008 or older) you'll need to do it the old-fashioned way:

 

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[0];


That code takes the address of the first element of the array, which means you have a pointer to the entire array since the array elements are contiguous in memory.



#8 Bash Mills   Members   -  Reputation: 108

Like
0Likes
Like

Posted 06 January 2013 - 03:48 PM

Ahha! Thank you. Works a treat. :)






Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS