Directx 使用Direct3D绘制最基本的变换顶点

Directx 使用Direct3D绘制最基本的变换顶点,directx,drawing,direct3d,primitive,vertices,Directx,Drawing,Direct3d,Primitive,Vertices,我是DirectX的新手,我想用最基本的Direct3D配置绘制一些未转换的原语(用于学习目的)。我已经绘制了一些带有变换顶点的基本体,即带有D3DFVF_XYZRHW标志集的顶点 现在,我试图用未转换的顶点获得相同的输出,但在屏幕上没有任何视觉效果。我更改了FVF并调整了顶点,但还没有设置任何变换矩阵(世界、视图、投影)。有必要设置这些矩阵中的任何一个吗?我假设一切都会像在没有设置矩阵的情况下变换顶点一样工作,但显然不是这样 默认情况下,哪个区域(在世界坐标中)可见?我必须做什么才能使它工作

我是DirectX的新手,我想用最基本的Direct3D配置绘制一些未转换的原语(用于学习目的)。我已经绘制了一些带有变换顶点的基本体,即带有D3DFVF_XYZRHW标志集的顶点

现在,我试图用未转换的顶点获得相同的输出,但在屏幕上没有任何视觉效果。我更改了FVF并调整了顶点,但还没有设置任何变换矩阵(世界、视图、投影)。有必要设置这些矩阵中的任何一个吗?我假设一切都会像在没有设置矩阵的情况下变换顶点一样工作,但显然不是这样

默认情况下,哪个区域(在世界坐标中)可见?我必须做什么才能使它工作


我基本上就是这样做的:

struct Vertex
{
    float x, y, z;
    D3DCOLOR color;

    static const DWORD format = D3DFVF_XYZ | D3DFVF_DIFFUSE;
};


const Vertex vertices[] =  {
                           {0.0f, 0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {-0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)}
                           };


pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, Vertex::format, D3DPOOL_DEFAULT, &pVB, NULL);

VOID* vertexData = 0;
pVB->Lock(0, sizeof(vertices), &vertexData, 0);
memcpy(vertexData, vertices, sizeof(vertices));
pVB->Unlock();



D3DMATRIX matrixIdentitiy;
ZeroMemory(&matrixIdentitiy, sizeof(matrixIdentitiy));
matrixIdentitiy._11 = 1.0f;
matrixIdentitiy._22 = 1.0f;
matrixIdentitiy._33 = 1.0f;
matrixIdentitiy._44 = 1.0f;

pd3dDevice->SetTransform(D3DTS_WORLD, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_VIEW, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &matrixIdentitiy);



pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 0.0f, 0);
pd3dDevice->BeginScene();

pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pd3dDevice->SetRenderState(D3DRS_CLIPPING, FALSE);
pd3dDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_ALWAYS );

pd3dDevice->SetStreamSource(0, pVB, 0, sizeof(Vertex));
pd3dDevice->SetFVF(Vertex::format);
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

pd3dDevice->EndScene();
pd3dDevice->Present(NULL, NULL, NULL, NULL);
struct Vertex
{
    float x, y, z;
    D3DCOLOR color;

    static const DWORD format = D3DFVF_XYZ | D3DFVF_DIFFUSE;
};


const Vertex vertices[] =  {
                           {0.0f, 0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {-0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)}
                           };


pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, Vertex::format, D3DPOOL_DEFAULT, &pVB, NULL);

VOID* vertexData = 0;
pVB->Lock(0, sizeof(vertices), &vertexData, 0);
memcpy(vertexData, vertices, sizeof(vertices));
pVB->Unlock();



D3DMATRIX matrixIdentitiy;
ZeroMemory(&matrixIdentitiy, sizeof(matrixIdentitiy));
matrixIdentitiy._11 = 1.0f;
matrixIdentitiy._22 = 1.0f;
matrixIdentitiy._33 = 1.0f;
matrixIdentitiy._44 = 1.0f;

pd3dDevice->SetTransform(D3DTS_WORLD, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_VIEW, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &matrixIdentitiy);



pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 0.0f, 0);
pd3dDevice->BeginScene();

pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pd3dDevice->SetRenderState(D3DRS_CLIPPING, FALSE);

pd3dDevice->SetStreamSource(0, pVB, 0, sizeof(Vertex));
pd3dDevice->SetFVF(Vertex::format);
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

pd3dDevice->EndScene();
pd3dDevice->Present(NULL, NULL, NULL, NULL);
提前谢谢


编辑:现在我明白了,灯光被启用了,愚蠢的初学者的错误。无论如何,谢谢你的帮助

如果您将世界、视图和投影矩阵设置为identity,那么您将得到一个简单的过程

x的范围为-1到1
y的范围为-1到1
z的范围为0到1(此位可能会导致问题)。

若你们定义了标准的未转换顶点,并将它们传递到该范围内,它们将显示在屏幕上

如果这无助于发布一些代码,我会看看我能提供什么建议

编辑:您是否启用了Z缓冲?因为您没有清除Z缓冲区,这可能会导致奇怪的问题。一切都将在这背后呈现,因此永远不会呈现

设置

为了验证这一假设

还非常值得学习如何使用PIX,然后查看三角形在场景中的变化

Edit2:

您的问题可能来自照明。尝试关闭照明

还可以尝试将清除更改为:

pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(255, 255, 255), 0.0f, 0);

并检查多边形是否呈现黑色。

这基本上就是我所做的:

struct Vertex
{
    float x, y, z;
    D3DCOLOR color;

    static const DWORD format = D3DFVF_XYZ | D3DFVF_DIFFUSE;
};


const Vertex vertices[] =  {
                           {0.0f, 0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {-0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)}
                           };


pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, Vertex::format, D3DPOOL_DEFAULT, &pVB, NULL);

VOID* vertexData = 0;
pVB->Lock(0, sizeof(vertices), &vertexData, 0);
memcpy(vertexData, vertices, sizeof(vertices));
pVB->Unlock();



D3DMATRIX matrixIdentitiy;
ZeroMemory(&matrixIdentitiy, sizeof(matrixIdentitiy));
matrixIdentitiy._11 = 1.0f;
matrixIdentitiy._22 = 1.0f;
matrixIdentitiy._33 = 1.0f;
matrixIdentitiy._44 = 1.0f;

pd3dDevice->SetTransform(D3DTS_WORLD, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_VIEW, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &matrixIdentitiy);



pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 0.0f, 0);
pd3dDevice->BeginScene();

pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pd3dDevice->SetRenderState(D3DRS_CLIPPING, FALSE);
pd3dDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_ALWAYS );

pd3dDevice->SetStreamSource(0, pVB, 0, sizeof(Vertex));
pd3dDevice->SetFVF(Vertex::format);
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

pd3dDevice->EndScene();
pd3dDevice->Present(NULL, NULL, NULL, NULL);
struct Vertex
{
    float x, y, z;
    D3DCOLOR color;

    static const DWORD format = D3DFVF_XYZ | D3DFVF_DIFFUSE;
};


const Vertex vertices[] =  {
                           {0.0f, 0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {-0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)}
                           };


pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, Vertex::format, D3DPOOL_DEFAULT, &pVB, NULL);

VOID* vertexData = 0;
pVB->Lock(0, sizeof(vertices), &vertexData, 0);
memcpy(vertexData, vertices, sizeof(vertices));
pVB->Unlock();



D3DMATRIX matrixIdentitiy;
ZeroMemory(&matrixIdentitiy, sizeof(matrixIdentitiy));
matrixIdentitiy._11 = 1.0f;
matrixIdentitiy._22 = 1.0f;
matrixIdentitiy._33 = 1.0f;
matrixIdentitiy._44 = 1.0f;

pd3dDevice->SetTransform(D3DTS_WORLD, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_VIEW, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &matrixIdentitiy);



pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 0.0f, 0);
pd3dDevice->BeginScene();

pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pd3dDevice->SetRenderState(D3DRS_CLIPPING, FALSE);

pd3dDevice->SetStreamSource(0, pVB, 0, sizeof(Vertex));
pd3dDevice->SetFVF(Vertex::format);
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

pd3dDevice->EndScene();
pd3dDevice->Present(NULL, NULL, NULL, NULL);

这不是答案,这是问题的一部分!文本应该被编辑到问题中,答案被删除。同意,但没有删除链接。很好,我禁用了z缓冲区测试,但问题仍然存在。
struct Vertex
{
    float x, y, z;
    D3DCOLOR color;

    static const DWORD format = D3DFVF_XYZ | D3DFVF_DIFFUSE;
};


const Vertex vertices[] =  {
                           {0.0f, 0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)},
                           {-0.8f, -0.8f, 0.5f, D3DCOLOR_XRGB(255, 255, 255)}
                           };


pd3dDevice->CreateVertexBuffer(sizeof(vertices), 0, Vertex::format, D3DPOOL_DEFAULT, &pVB, NULL);

VOID* vertexData = 0;
pVB->Lock(0, sizeof(vertices), &vertexData, 0);
memcpy(vertexData, vertices, sizeof(vertices));
pVB->Unlock();



D3DMATRIX matrixIdentitiy;
ZeroMemory(&matrixIdentitiy, sizeof(matrixIdentitiy));
matrixIdentitiy._11 = 1.0f;
matrixIdentitiy._22 = 1.0f;
matrixIdentitiy._33 = 1.0f;
matrixIdentitiy._44 = 1.0f;

pd3dDevice->SetTransform(D3DTS_WORLD, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_VIEW, &matrixIdentitiy);
pd3dDevice->SetTransform(D3DTS_PROJECTION, &matrixIdentitiy);



pd3dDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0, 0, 0), 0.0f, 0);
pd3dDevice->BeginScene();

pd3dDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
pd3dDevice->SetRenderState(D3DRS_CLIPPING, FALSE);

pd3dDevice->SetStreamSource(0, pVB, 0, sizeof(Vertex));
pd3dDevice->SetFVF(Vertex::format);
pd3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

pd3dDevice->EndScene();
pd3dDevice->Present(NULL, NULL, NULL, NULL);