Hello everyone! I'm trying to create a 2D texture renderer for my engine and cannot get anything working or to show up. Here's how it's currently setup. I have a class called NXSpriteBatch. When NXSpriteBatch::Initialize() is called, it sets up the vertex buffer. After the vertex buffer is setup, I render textures using the method below:
bool NXSpriteBatch::Render(ID3D11ShaderResourceView* view, XMFLOAT4 color, XMFLOAT2 position, XMFLOAT2 scale)
In this bool, I start by updating the vertices based on the texture size:
bool NXSpriteBatch::UpdateVertices(XMFLOAT2 size)
{
float sizeX = (float) size.x / 2;
float sizeY = (float) size.y / 2;
Vertices[0].Position = XMFLOAT3(sizeX, sizeY, 1.0f);
Vertices[1].Position = XMFLOAT3(sizeX, -sizeY, 1.0f);
Vertices[2].Position = XMFLOAT3(-sizeX, -sizeY, 1.0f);
Vertices[3].Position = XMFLOAT3(-sizeX, -sizeY, 1.0f);
Vertices[4].Position = XMFLOAT3(-sizeX, sizeY, 1.0f);
Vertices[5].Position = XMFLOAT3(sizeX, sizeY, 1.0f);
Vertices[0].TextureCoordinate = XMFLOAT2(1.0f, 0.0f);
Vertices[1].TextureCoordinate = XMFLOAT2(1.0f, 1.0f);
Vertices[2].TextureCoordinate = XMFLOAT2(0.0f, 1.0f);
Vertices[3].TextureCoordinate = XMFLOAT2(0.0f, 1.0f);
Vertices[4].TextureCoordinate = XMFLOAT2(0.0f, 0.0f);
Vertices[5].TextureCoordinate = XMFLOAT2(1.0f, 0.0f);
return true;
}
Next, I update the vertex buffer:
bool NXSpriteBatch::UpdateVertexBuffer()
{
if (OriginalVertices[0] == Vertices[0] || OriginalVertices[1] == Vertices[1] || OriginalVertices[2] == Vertices[2]
|| OriginalVertices[3] == Vertices[3] || OriginalVertices[4] == Vertices[4] || OriginalVertices[5] == Vertices[5])
return true;
VertexBuffer.Update(&Vertices);
OriginalVertices = Vertices;
return true;
}
You can check out my NXVertexBuffer code using the links below. After updating my vertex buffer (hopefully I did it right),I finish off the method by rendering. Here's the full Render method:
bool NXSpriteBatch::Render(ID3D11ShaderResourceView* view, XMFLOAT4 color, XMFLOAT2 position, XMFLOAT2 scale)
{
//Update Vertices
if (!UpdateVertices(position, NXGetSRVSize(view)))
return false;
//Update Buffer
if (!UpdateVertexBuffer())
return false;
XMMATRIX viewMatrix = XMMatrixIdentity();
XMMATRIX projMatrix; NXGPU::Context->GetOrthoMatrix(projMatrix);
XMMATRIX vp = XMMatrixMultiply(viewMatrix, projMatrix);
XMMATRIX mvp = XMMatrixMultiply(CalculateWorldMatrix(position, scale, 0), vp);
mvp = XMMatrixTranspose(mvp);
Shaders::NXTextureShader* shader = GetTextureShader();
shader->SetVSConstants(mvp);
shader->SetPSConstants(XMLoadFloat4(&color), view);
shader->Apply();
NXGPU::DisableZBuffer();
NXGPU::Draw(D3D_PRIMITIVE_TOPOLOGY_TRIANGLELIST, 6, 0);
return true;
}
I have my ortho matrix setup like this:
XMStoreFloat4x4(&m_orthoMatrix, XMMatrixOrthographicOffCenterLH(0.0f, (float) width, 0.0f, (float) height, snear, sdepth));
Here's all of the code:
I know the texture is being loaded correctly because I used it for 3D objects and I also know the texture shader works correctly as well. I find it funny that I could get deferred renderer working great, but can't get a simple texture to render. LOL. Anyways, any help? Thanks!