Hi all,
After following the steps are described in the article "2D rendering in DirectX8" (http://www.gamedev.net/reference/articles/article1434.asp), I run into a couple of problems. Basically, I am trying to setup a 2d quad and texture it. The problems are:
1. The quad turns up as a black rectangle, ignoring the Diffuse colour which I have set.
I have make sure that I have disable lightning and the z-buffer, and set a valid colour for the diffuse property of each vertices but I all get is a mocking black rectangle...
Here are some code snippets:
CUSTOM VERTEX DEFINTION
// Custom vertex definition
struct FVF_2D
{
float x, y, z;
DWORD colour;
float u, v;
};
// Custom vertex marco
#define FVF_2DFORMAT ( D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1)
DIRECT 3D Device setup
// { Create the structure of parameters for the Device.}
D3DPRESENT_PARAMETERS d3dpp;
// {Fill the structure with zeroes }
ZeroMemory ( &d3dpp, sizeof(d3dpp) );
// {Fill the display parameters with properties of the desktop }
// NOTE: A new function call GetDisplayParameters, can be used to get custom
// properties stored in a file and gathered through inputs
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferWidth = DesktopDisplayMode.Width;
d3dpp.BackBufferHeight = DesktopDisplayMode.Height;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.Windowed = FALSE;
d3dpp.BackBufferCount=1;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.EnableAutoDepthStencil = TRUE;
d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
d3dpp.FullScreen_PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
d3dpp.MultiSampleType=D3DMULTISAMPLE_NONE;
// { Store the width and height }
m_width = d3dpp.BackBufferWidth;
m_height = d3dpp.BackBufferHeight;
// {Create the Direct3D device }
theResult = pD3D->CreateDevice ( D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hWnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp,
&pDevice);
Rendering States
void cGraphics::Toggle2D()
{
// { Create an identity matrix }
D3DXMATRIX Identity;
D3DXMatrixIdentity(&Identity);
// { Create an orthographic matrix }
D3DXMATRIX Ortho2D;
D3DXMatrixOrthoLH(&Ortho2D, (float)m_width, (float)m_height, 0.0f, 1.0f);
// { Set an orthographic projetion }
pDevice->SetTransform(D3DTS_PROJECTION, &Ortho2D);
// { Use default camera position }
pDevice->SetTransform(D3DTS_WORLD, &Identity);
// { Use default world position }
pDevice->SetTransform(D3DTS_VIEW, &Identity);
// { Switch of the lightning and depth buffer }
pDevice->SetRenderState(D3DRS_ZENABLE, D3DZB_FALSE);
pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
// { All the texture settings }
pDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
pDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_INVSRCALPHA);
pDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
pDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
}
Vertices setup
HRESULT cImage::SetImage(char *imageFilename)
{
HRESULT theResult;
// { Create an image info structure to hold the image data }
D3DXIMAGE_INFO ImageInfo;
// { Create the texture from the image file }
pTexture=pRenderer->LoadTexture(imageFilename, &ImageInfo);
// { Make sure a valid texture is created }
assert(pTexture);
// { Extract the width and height of the image }
m_Height = ImageInfo.Height;
m_Width = ImageInfo.Width;
// { Create the vertex buffer }
theResult=pRenderer->pDevice->CreateVertexBuffer( 4 * sizeof(FVF_2D),
0,
FVF_2DFORMAT,
D3DPOOL_DEFAULT,
&pVertexBuffer);
// { Lock the vertex buffer }
FVF_2D *pVertices = NULL;
theResult=pVertexBuffer->Lock(0, 4 * sizeof (FVF_2D), (BYTE**)&pVertices, 0);
// { Verify the vertex buffer }
if FAILED(theResult)
{
assert(false);
return E_FAIL;
}
// { Fill the vertex buffer }
pVertices[0].x = pVertices[3].x = -m_Width / 2.0f;
pVertices[1].x = pVertices[2].x = m_Width / 2.0f;
pVertices[0].y = pVertices[1].y = m_Height / 2.0f;
pVertices[2].y = pVertices[3].y = -m_Height / 2.0f;
pVertices[0].z = pVertices[1].z = pVertices[2].z = pVertices[3].z = 1.0f;
pVertices[1].u = pVertices[2].u = 1.0f;
pVertices[0].u = pVertices[3].u = 0.0f;
pVertices[0].v = pVertices[1].v = 0.0f;
pVertices[2].v = pVertices[3].v = 1.0f;
pVertices[0].colour = pVertices[1].colour = pVertices[2].colour = pVertices[3].colour = D3DCOLOR_XRGB(255,255,255);
// { Unlock the vertex buffer }
pVertexBuffer->Unlock();
return S_OK;
}
Actual rendering
// BeginScene() and Toggle2D has been called.
HRESULT cImage::Draw(int x, int y)
{
HRESULT theResult;
// { Set the current vertex shader for 2d rendering }
pRenderer->pDevice->SetVertexShader(FVF_2DFORMAT);
// { Set image's vertex buffer as the stream to render }
pRenderer->pDevice->SetStreamSource(0, pVertexBuffer, sizeof(FVF_2D));
// { Set the texture to draw }
if (pTexture != NULL)
theResult=pRenderer->pDevice->SetTexture(0, pTexture);
if FAILED(theResult)
{
//MSGBOX("Error in setting the the texture in cImage::Draw()", "Error setting texture")
assert(theResult);
}
// { Draw the quad }
pRenderer->pDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
return S_OK;
}
2. No texture shows up.
I ensured that my texture pointer is valid, that I have called SetTexture, that lightning is disabled, that the texture co-ordinates are right, that BeginScene is called, that the vertex shader format is set correctedly and even tried a couple of different textures of different format and size...Still no texture...
This is really, really puzzling to me. I spent about three hours trying to get it right. If anyone is interested, the source code is
here
Many, many thanks in advance!
[edited by - LonelyTower on October 1, 2002 3:43:55 AM]
"Magic makes the world go round." - Erasmus, Quest For Glory I