I am following along on the book called Introduction To 3D Game Programming With Directx 9.0
and I've copied and pasted the following example out of the book:
struct Vertex
{
float _x, _y, _z;
float _nx, _ny, _nz;
float _u, _v; // texture coordinates
static const DWORD FVF;
};
const DWORD Vertex::FVF = D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1;
void D3DGraphics::drawBMP()
{
pDevice->CreateVertexBuffer(6 * sizeof(Vertex), D3DUSAGE_WRITEONLY, Vertex::FVF, D3DPOOL_MANAGED, &quadVertexBuffer, 0);
Vertex* v;
quadVertexBuffer->Lock(0, 0, (void**)&v, 0);
// quad built from two triangles, note texture coordinates:
v[0] = { -1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f }; // was Vertex()
v[1] = { -1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f };
v[2] = { 1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f };
v[3] = { -1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f };
v[4] = { 1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f };
v[5] = { 1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f };
quadVertexBuffer->Unlock();
// Load texture data.
if (!texture)
{
D3DXCreateTextureFromFile(pDevice, L"C:\\Users\\Owner\\Desktop\\Result.bmp", &texture);
}
// Enable the texture.
pDevice->SetTexture(0, texture);
// Set texture filters.
pDevice->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);
pDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
pDevice->SetSamplerState(0, D3DSAMP_MIPFILTER, D3DTEXF_POINT);
// set projection matrix
D3DXMATRIX proj;
D3DXMatrixPerspectiveFovLH(&proj, D3DX_PI * 0.5f, (float)1280 / (float)720, 1.0f, 1000.0f);
pDevice->SetTransform(D3DTS_PROJECTION, &proj);
// don't use lighting for this sample
pDevice->SetRenderState(D3DRS_LIGHTING, false);
// render code
pDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, 0xffffffff, 1.0f, 0);
pDevice->BeginScene();
pDevice->SetStreamSource(0, quadVertexBuffer, 0, sizeof(Vertex));
pDevice->SetFVF(Vertex::FVF);
// Draw primitives using presently enabled texture.
pDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
pDevice->EndScene();
pDevice->Present(0, 0, 0, 0);
}
I only changed one thing. The structs in the book are initialized as such:
v[0] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[1] = Vertex(-1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 0.0f);
v[2] = Vertex( 1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[3] = Vertex(-1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 0.0f, 1.0f);
v[4] = Vertex( 1.0f, 1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 0.0f);
v[5] = Vertex( 1.0f, -1.0f, 1.25f, 0.0f, 0.0f, -1.0f, 1.0f, 1.0f);
but that seems to give a compiler error so I changed it in my code. Anyway, Result.bmp's dimensions are 1280x720. The window I am rendering to is also 1280x720. Here is Result.bmp. Here is the output I am getting. Normally I wouldn't ask such a simple question but I am trying to learn and this is an example straight out of the textbook (that doesn't work). Considering this is a copy & paste from the book, I don't think I have made any mistake (yet) and I was wondering if this is an error in the book. Playing around with the second argument to D3DXMatrixPerspectiveFovLH
seemed to have made things better if I change 0.5f to 0.4f but it's still distorted.
Since you are using
D3DXMatrixPerspectiveFovLH
, you are rendering a square quad with corners (-1,-1) -> (1,1). Since the BMP has a 16:9 aspect ratio, the result is a distorted texture image.If you want the image to be full-screen, you need to use a non-perspective projection such as
D3DXMatrixOrthoLH
:In that case, the same quad will have the 16:9 aspect ratio needed to make the input image to remove the distortion.
BTW: I'd recommend moving to DirectX 11 and making use of DirectX Tool Kit's
SpriteBatch
instead of the decade old Direct3D 9 API...