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

发布于 2024-08-03 09:29:23 字数 1970 浏览 7 评论 0原文

我对 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);

提前致谢!

编辑:现在我明白了,照明已启用,愚蠢的初学者错误。不管怎样,谢谢你的帮助!

I'm absolutely new to DirectX and I'd like to draw some untransformed primitives with the most basic Direct3D configuration (for learning purposes). I already drew some primitives with transformed vertices, that is vertices with the D3DFVF_XYZRHW flag set.

Now I'm trying to get the same output with untransformed vertices, but I don't get any visuals on the screen. I changed my FVF and adjusted the vertices but didn't set any transformation matrix (world, view, projection) yet. Is it necessary to set any of those matrices? I'd assume that everything would work just like with transformed vertices when no matrices are set, but obviously that's not the case.

What area (in world-coordinates) is visible by default? What do I have to do in order to make it work?

This is basically what I do:

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);

Thanks in advance!

EDIT: Now i got it, lighting was enabled, stupid beginner's mistake. Thanks for your help anyway!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

宛菡 2024-08-10 09:29:23

好吧,如果你将你的世界、视图和投影矩阵设置为恒等,那么你将得到一个简单的传递。

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

如果您随后定义标准未变换的顶点并将它们传递到该范围内,它们将显示在屏幕上。

如果这没有帮助发布一些代码,我会看看我能提出什么建议。

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

设置

pd3dDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_ALWAYS );

pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE );

检验这个假设。

学习如何使用 PIX,然后看看三角形穿过场景时会发生什么情况也是非常值得的。

编辑2:

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

,即

pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

也尝试将您的“清除”更改为:

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

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

Well if you set your world, view and projection matrices to identity then what you will get is a simple pass through.

x will range from -1 to 1
y will range from -1 to 1
z will range from 0 to 1 (This bit can cause problems).

If you then define standard untransformed verts and pass them on inside that range they will display on screen.

If that doesn't help post up some code and I'll see what i can suggest.

Edit: Have you got Z-Buffering enabled? Because you don't clear your Z-buffer which could cause odd issues. Everything will render behind this and hence will never render.

Set

pd3dDevice->SetRenderState( D3DRS_ZFUNC, D3DCMP_ALWAYS );

or

pd3dDevice->SetRenderState( D3DRS_ZENABLE, D3DZB_FALSE );

to test this hypothesis.

It is also very much worth learning how to use PIX and then seeing what happens to your triangle as it goes through the scene.

Edit2:

Its possible that your problem is coming from lighting. Try turning lighting off

ie

pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );

Also try changing your Clear to this:

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

And check to see if your polys are rendering black.

避讳 2024-08-10 09:29:23

这基本上就是我所做的:

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);

This is basically what I do:

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);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文