简体   繁体   English

DirectX9 C ++实时为顶点数据重新着色

[英]DirectX9 C++ Recoloring Vertex data in real time

I'm very new to DirectX and I'm starting to get a grasp on how the API functions. 我是DirectX的新手,并且开始了解API的功能。

I've managed to get triangles showing and rendering properly using these functions: 我设法使用以下函数正确显示和渲染了三角形:

Initializing the vertices: 初始化顶点:

void Menu::InitializeMenu(float x, float y, float width, float height, D3DCOLOR color,    IDirect3DDevice9* d3dDevice)
{
CUSTOMVERTEX vertices[] =
{
    { x, y, 0.5f, 1.0f, color },
    { x + width, y, 0.5f, 1.0f, color },
    { x + width, y + height, 0.5f, 1.0f, color },

    { x, y, 0.5f, 1.0f, color },
    { x , y + height, 0.5f, 1.0f, color },
    { x + width, y + height, 0.5f, 1.0f, color },
};

if (FAILED(d3dDevice->CreateVertexBuffer(6 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_vertexBuffer, NULL)))
    return;

void *locked_buffer;
if (FAILED(m_vertexBuffer->Lock(0, sizeof(vertices), (void **)&locked_buffer, 0)))
    return;



memcpy(locked_buffer, vertices, sizeof(vertices));
m_vertexBuffer->Unlock();
}

Everything here is defined within the Menu class. 这里的所有内容都在Menu类中定义。

Drawing: 画画:

void Menu::RenderMenu(IDirect3DDevice9 *d3dDevice)
{
d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3dDevice->SetRenderState(D3DRS_SRCBLENDALPHA, D3DRS_DESTBLENDALPHA);
d3dDevice->SetStreamSource(0, m_vertexBuffer, 0, sizeof(CUSTOMVERTEX));
d3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
d3dDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);
}

Everything works perfect, I get my two triangles rendered, which in turn produce a semi-transparent quad. 一切正常,我渲染了两个三角形,这又生成了一个半透明的四边形。

Now the Issue: I want to be able to change the colors of the vertices in my triangles after the program has started rendering (everything has been initialized already and rendered at least once). 现在的问题是:我希望能够在程序开始渲染后更改三角形中顶点的颜色(所有内容都已初始化,并且至少渲染过一次)。

Things I've thought about: -I've thought about calling the InitializeMenu function with different parameters to reinitialize the vertices with different color, reason I haven't done it is because it seems very inefficient and not practical. 我考虑过的事情:-我考虑过使用不同的参数调用InitializeMenu函数以用不同的颜色重新初始化顶点,原因是我没有这样做是因为它看起来效率很低而且不切实际。

-Materials: I have not implemented materials, this is because I don't know how (yet) and because I'm hoping to find a simpler alternative. -材料:我还没有实现材料,这是因为我还不知道如何做,并且因为我希望找到一个更简单的选择。 All I need is the vertex colors. 我需要的只是顶点颜色。 If materials are the only way to accomplish this, I will implement. 如果材料是完成此任务的唯一方法,我将执行。

-Shaders: I understand you can color vertices with shaders, but I have very little shader experience, and as stated before I'd rather find a simpler alternative. -着色器:我知道您可以使用着色器为顶点着色,但是我几乎没有着色器经验,并且正如我之前所言,我宁愿找到一个更简单的替代方法。 Yes, I know shaders are simple, I've gotten to the point where I can change the color of vertices in a shader in real time. 是的,我知道着色器很简单,我已经可以实时更改着色器中顶点的颜色。 It was in GLSL but I'm sure it doesn't differ too much. 它使用的是GLSL,但我敢肯定它的差别不会太大。 Issue comes when I want to add multiple quads (collection of 2 triangles for ea quad). 当我要添加多个四边形(每个四边形的2个三角形的集合)时,问题就来了。 I only know how to change the color of all vertices coming into the vertex shader. 我只知道如何更改进入顶点着色器的所有顶点的颜色。 As before though, if shaders is the only way to accomplish, I'll implement. 和以前一样,如果着色器是唯一的实现方法,我将实现。 Please just point me in the right direction. 请给我指出正确的方向。 I have VERY little understanding on how shaders work on the low level (I understand the concept and flow, just don't know how to use to my advantage to use effectively). 我对着色器在低层的工作方式了解得很少(我了解着色器的概念和流程,只是不知道如何利用着色器才能有效地使用它)。

-Research: I've looked everywhere, maybe I'm not asking my question properly, but I cannot find an answer anywhere. 研究:我到处都看过了,也许我没问正确的问题,但是我找不到任何答案。

This is actually my first time posting a question, usually someone has already asked my questions. 这实际上是我第一次发布问题,通常有人已经问过我问题。 I've tried to explain my problem as best as I could, but if it's still unclear feel free to ask for more code or information. 我试图尽我最大的努力来解释我的问题,但是如果仍然不清楚,请随时请求更多代码或信息。

PS: I'm using windows 8 desktop, not sure if that really matters. PS:我正在使用Windows 8桌面,不确定是否真的很重要。

To update the vertices, you will need to do something similar to InitializeMenu, but without calling CreateVertexBuffer again. 要更新顶点,您将需要执行与InitializeMenu类似的操作,但无需再次调用CreateVertexBuffer。 You will also need make a slight modification to how you create the vertex buffer. 您还需要对创建顶点缓冲区的方式进行一些修改。

There are two types of vertex buffers: static and dynamic. 顶点缓冲区有两种类型:静态和动态。 A static vertex buffer does not allow access by the CPU to make changes whereas a dynamic vertex buffer does. 静态顶点缓冲区不允许CPU进行更改,而动态顶点缓冲区则允许。

To create a dynamic vertex buffer, add D3DUSAGE_DYNAMIC to CreateVertexBuffer: 要创建动态顶点缓冲区,请将D3DU​​SAGE_DYNAMIC添加到CreateVertexBuffer:

d3dDevice->CreateVertexBuffer(6 * sizeof(CUSTOMVERTEX), D3DUSAGE_DYNAMIC, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_vertexBuffer, NULL)

You can then make a new function like this to change color: 然后,您可以创建一个新的函数来更改颜色:

void Menu::ChangeColor(D3DColor color) {
    CUSTOMVERTEX *locked_buffer;
    if (FAILED(m_vertexBuffer->Lock(0, 0, (void **)&locked_buffer, 0))) {
        return;
    }

    for (int i=0; i<6; i++) {
        // use whatever you called color in your CUSTOMVERTEX struct
        locked_buffer[i].color = color;
    }
    m_vertexBuffer->Unlock();
}

This code essentially gets the vertex data from the GPU and allows you to update it on the CPU. 该代码本质上是从GPU获取顶点数据,并允许您在CPU上对其进行更新。 You don't need to recreate the vertex buffer, you just update the values you want. 您不需要重新创建顶点缓冲区,只需更新所需的值即可。

You can define a random function and then add color elements ie: Green, Blue, Yellow, Red, Purple etc into an array. 您可以定义一个随机函数,然后将绿色,蓝色,黄色,红色,紫色等颜色元素添加到数组中。 Then you call the random function to randomly select the colors inside an array. 然后,您可以调用random函数来随机选择数组中的颜色。

Like this: 像这样:

int arr[15] = {10, 210, 140, 180, 250, 189, 183, 107, 183, 107, 60, 2, 55, 85, 48}; int arr [15] = {10,210,140,180,250,189,183,107,183,107,60,2,55,85,48};

D3DCOLOR color1; D3DCOLOR color2; D3DCOLOR color3; D3DCOLOR color4;

srand(time(NULL));
CUSTOMVERTEX vertices[] =
{{ x, y, 0.5f, 1.0f, color = D3DCOLOR_XRGB(arr[rand() % 14 + 0], arr[rand() % 14 + 0], arr[rand() % 14 + 0])},
{ x + width, y, 0.5f, 1.0f, color = D3DCOLOR_XRGB(arr[rand() % 14 + 0], arr[rand() % 14 + 0], arr[rand() % 14 + 0])},
{ x + width, y + height, 0.5f, 1.0f, color = D3DCOLOR_XRGB(arr[rand() % 14 + 0], arr[rand() % 14 + 0], arr[rand() % 14 + 0])},

{ x, y, 0.5f, 1.0f, color = D3DCOLOR_XRGB(arr[rand() % 14 + 0], arr[rand() % 14 + 0], arr[rand() % 14 + 0])},
{ x , y + height, 0.5f, 1.0f, color = D3DCOLOR_XRGB(arr[rand() % 14 + 0], arr[rand() % 14 + 0], arr[rand() % 14 + 0])},
{ x + width, y + height, 0.5f, 1.0f, color = D3DCOLOR_XRGB(arr[rand() % 14 + 0], arr[rand() % 14 + 0], arr[rand() % 14 + 0])},
};

One thing, you might have to use different D3DCOLOR color variables ie; 一件事,您可能必须使用不同的D3DCOLOR颜色变量,即; color1, color2, color3 etc to avoid dereferencing and value overwritting. color1,color2,color3等,以避免取消引用和覆盖值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM