我正在开发2D游戏,似乎无法在我的四边形中添加纹理。我已经精确地遵循了一些教程,它看起来很简单,但我得到的只是它的默认颜色(白色)的四边形。纹理大小为64x64,是2的倍数,所以这应该不是问题。这是我的代码:
#include "includes.h"
#define CUSTOMFVF (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_TEX1)
LPDIRECT3D9 d3d;
LPDIRECT3DDEVICE9 d3ddev;
LPDIRECT3DVERTEXBUFFER9 v_buffer;
LPDIRECT3DTEXTURE9 Tex = NULL;
struct SQR
{
float TLX = tile * 2;
float TLY = tile * 4;
float width = tile * 8;
float height = tile * 6;
} sqr;
struct D3DVERTEX
{
float fX,
fY,
fZ,
fRHW;
D3DCOLOR colour;
float fU,
fV;
} vertex[4];
void GenerateQuad()
{
vertex[0] = { sqr.TLX - 0.5f, sqr.TLY - 0.5f, 1.0f, 1.0f, 0xffffffff, 0.0f, 0.0f };
vertex[1] = { sqr.TLX + sqr.width - 0.5f, sqr.TLY - 0.5f, 1.0f, 1.0f, 0xffffffff, 1.0f, 0.0f };
vertex[3] = { sqr.TLX - 0.5f, sqr.TLY + sqr.height - 0.5f, 1.0f, 1.0f, 0xffffffff, 0.0f, 1.0f };
vertex[2] = { sqr.TLX + sqr.width - 0.5f, sqr.TLY + sqr.height - 0.5f, 1.0f, 1.0f, 0xffffffff, 1.0f, 1.0f };
}
IDirect3DTexture9 *LoadTexture(LPCWSTR fileName)
{
IDirect3DTexture9 *d3dTexture;
D3DXIMAGE_INFO SrcInfo;
D3DCOLOR colorkey = 0xffffffff;
if (FAILED(D3DXCreateTextureFromFileEx(d3ddev, fileName, 0, 0, 1, 0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
colorkey, &SrcInfo, NULL, &d3dTexture)))
{
return NULL;
}
return d3dTexture;
}
bool Init3D()
{
d3d = Direct3DCreate9(D3D_SDK_VERSION);
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory(&d3dpp, sizeof(d3dpp));
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
d3dpp.BackBufferFormat = D3DFMT_X8R8G8B8;
d3dpp.BackBufferWidth = 960;
d3dpp.BackBufferHeight = 600;
d3d->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL,
hwnd,
D3DCREATE_MIXED_VERTEXPROCESSING,
&d3dpp,
&d3ddev);
GenerateQuad();
v_buffer = NULL;
d3ddev->CreateVertexBuffer(4 * sizeof(D3DVERTEX),
0,
CUSTOMFVF,
D3DPOOL_MANAGED,
&v_buffer,
NULL);
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(D3DVERTEX));
d3ddev->SetRenderState(D3DRS_LIGHTING, FALSE);
d3ddev->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3ddev->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3ddev->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3ddev->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
Tex=LoadTexture(L"Tex.bmp");
v_buffer->Lock(0, 0, (void**)&pVoid, 0);
memcpy(pVoid, vertex, sizeof(vertex));
v_buffer->Unlock();
d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
d3ddev->SetTexture(0, Tex);
return TRUE;
}
DWORD renderf()
{
d3ddev->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_ARGB(0xff, 0, 0, 0), 1.0f, 0);
sqr.TLX += 1.0f;
GenerateQuad();
v_buffer->Lock(0, sizeof(vertex), (void**)&pVoid, D3DLOCK_NO_DIRTY_UPDATE);
memcpy(pVoid, vertex, sizeof(vertex));
v_buffer->Unlock();
d3ddev->SetTexture(0, Tex);
d3ddev->BeginScene();
d3ddev->SetFVF(CUSTOMFVF);
d3ddev->SetStreamSource(0, v_buffer, 0, sizeof(D3DVERTEX));
d3ddev->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2);
d3ddev->EndScene();
d3ddev->Present(NULL, NULL, NULL, NULL);
return 0x0000;
}
答案 0 :(得分:0)
加载纹理后,检查纹理是否不等于NULL。你还应该检查你的d3ddevice对象和其他分配的资源是不是NULL,因为如果它没有分配而你没有检查,你就不会知道它,直到你想知道为什么没有渲染的东西。