使用Directx10进行渲染

时间:2018-01-22 13:35:04

标签: c++ 3d directx directx-11 directx-10

在运行期间,我期望在初始化期间在程序中成功创建和绘制的所有对象都会在每次调用GraphicsClass :: Render()函数时更改其位置。但没有任何事情发生:所有数字(我的情况都是如此)都停滞不前,没有任何动静。

我知道Directx10中的常用方法是使用世界,视图和投影矩阵。但是在这里我直接更改缓冲区,然后将其发送到ID3D10Device对象进行绘制,因为我需要绘制许多具有不同轨迹的对象,并且每个对象都由一个点表示。

我怀疑错误可能出在ColorShaderClass :: SetShaderParameters方法中。所以,我只附加了与程序中的渲染直接相关的代码。

graphicsclass.h

class GraphicsClass
{
public:
    GraphicsClass();
    GraphicsClass(const GraphicsClass&);
    ~GraphicsClass();

    bool Initialize(int, int, HWND);
    void Shutdown();
    bool Frame(double time);

private:
    bool Render(double time);

private:
    D3DClass * m_Direct3D;
    CameraClass* m_Camera;
    ModelClass* m_Model;
    ColorShaderClass* m_ColorShader;

    //amount of bodies
    int m_bodies;
};

graphicsclass.cpp

bool GraphicsClass::Render()
{
    // Clear the buffers to begin the scene.
    m_Direct3D->BeginScene(0.5f, 0.5f, 0.5f, 1.0f);

    bool result;

    // Clear the buffers to begin the scene.
    m_Direct3D->BeginScene(0.0f, 0.0f, 0.0f, 1.0f);

    // Generate the view matrix based on the camera's position.
    m_Camera->Render();

    // Put the model vertex and index buffers on the graphics pipeline to prepare them for drawing.
    m_Model->Render(m_Direct3D->GetDevice());

    // Render the model using the color shader.
    result = m_ColorShader->Render(m_Direct3D->GetDevice(), m_Model->GetIndexCount(),m_Model->GetPositions());
    if (!result)
    {
        return false;
    }

    // Present the rendered scene to the screen.
    m_Direct3D->EndScene();
    return true;
}

modelclsss.h

class ModelClass
{
private:
    struct ModelVertex {
        XMFLOAT4 Pos;
    };

public:
    ModelClass(int); //pass number of bodies
    ModelClass(const ModelClass&);
    ~ModelClass();

    bool Initialize(ID3D10Device*);
    void Shutdown();
    void Render(ID3D10Device*);

    int GetIndexCount();
    float * GetPositions();

private:
    bool InitializeBuffers(ID3D10Device*);
    void ShutdownBuffers();
    void RenderBuffers(ID3D10Device*);

private:
    ID3D10Buffer *m_vertexBuffer, *m_indexBuffer;
    int m_vertexCount, m_indexCount;

    float  * m_positions;
    float  * m_velocities;
    NBodySystemGPU * m_bodySystem;
    int m_bodies;
};

modelclass.cpp

void ModelClass::Render(ID3D10Device* device)
{
    //update bodies' positions
    bool result;
    //m_bodySystem->update(0.00001f, m_positions, m_velocities, result);
    for (int i = 0; i < m_bodies; ++i) {
        m_positions[i] += 0.5;
    }
    InitializeBuffers(device);
    // Put the vertex and index buffers on the graphics pipeline to prepare them for drawing.
    RenderBuffers(device);
    return;
}

void ModelClass::RenderBuffers(ID3D10Device* device)
{
    unsigned int stride;
    unsigned int offset;

    // Set vertex buffer stride and offset.
    stride = sizeof(ModelVertex);
    offset = 0;

    // Set the vertex buffer to active in the input assembler so it can be rendered.
    device->IASetVertexBuffers(0, 1, &m_vertexBuffer, &stride, &offset);

    // Set the index buffer to active in the input assembler so it can be rendered.
    device->IASetIndexBuffer(m_indexBuffer, DXGI_FORMAT_R32_UINT, 0);

    // Set the type of primitive that should be rendered from this vertex buffer, in this case triangles.
    device->IASetPrimitiveTopology(D3D10_PRIMITIVE_TOPOLOGY_POINTLIST);

    return;
}

bool ModelClass::InitializeBuffers(ID3D10Device* device)
{
    unsigned long* indices;
    D3D10_BUFFER_DESC vertexBufferDesc, indexBufferDesc;
    D3D10_SUBRESOURCE_DATA vertexData, indexData;
    HRESULT result;

    // Create the index array.
    indices = new unsigned long[m_bodies];
    if(!indices)
    {
        return false;
    }

    // Load the vertex array with data.
    for (int i = 0; i < m_bodies; ++i) {
        indices[i] = i;
    }

    // Set up the description of the vertex buffer.
    ZeroMemory(&vertexBufferDesc, sizeof(vertexBufferDesc));
    vertexBufferDesc.Usage = D3D10_USAGE_DYNAMIC;
    vertexBufferDesc.ByteWidth = sizeof(ModelVertex) * m_bodies;
    vertexBufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
    vertexBufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
    vertexBufferDesc.MiscFlags = 0;

    // Give the subresource structure a pointer to the vertex data.
    ZeroMemory(&vertexData, sizeof(vertexData));
    vertexData.pSysMem = m_positions;
    vertexData.SysMemPitch = 0;
    vertexData.SysMemSlicePitch = 0;

    // Now finally create the vertex buffer.
    result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);
    if(FAILED(result))
    {
        return false;
    }

    // Set up the description of the index buffer.
    ZeroMemory(&indexBufferDesc, sizeof(indexBufferDesc));
    indexBufferDesc.Usage = D3D10_USAGE_DYNAMIC;
    indexBufferDesc.ByteWidth = sizeof(unsigned long) * m_bodies;
    indexBufferDesc.BindFlags = D3D10_BIND_INDEX_BUFFER;
    indexBufferDesc.CPUAccessFlags = D3D10_CPU_ACCESS_WRITE;
    indexBufferDesc.MiscFlags = 0;

    // Give the subresource structure a pointer to the index data.
    indexData.pSysMem = indices;

    // Create the index buffer.
    result = device->CreateBuffer(&indexBufferDesc, &indexData, &m_indexBuffer);
    if(FAILED(result))
    {
        return false;
    }

    // Release the arrays now that the vertex and index buffers have been created and loaded.

    delete [] indices;
    indices = 0;

    return true;
}

colorshadersclss.h

class ColorShaderClass
{
private:
    struct ColorVertex {
        XMFLOAT4 Pos;
    };

public:
    ColorShaderClass(int); //pass number of bodies
    ColorShaderClass(const ColorShaderClass&);
    ~ColorShaderClass();

    bool Initialize(ID3D10Device*, HWND);
    void Shutdown();
    bool Render(ID3D10Device*, int,float *);

private:
    bool InitializeShader(ID3D10Device*, HWND, WCHAR*, WCHAR*);
    void ShutdownShader();
    void OutputShaderErrorMessage(ID3D10Blob*, HWND, WCHAR*);

    bool SetShaderParameters(ID3D10Device*,float *);
    void RenderShader(ID3D10Device*, int);

private:
    ID3D10VertexShader* m_vertexShader;
    ID3D10PixelShader* m_pixelShader;
    ID3D10InputLayout* m_layout;
    ID3D10Buffer* m_matrixBuffer;

    //Body system 
    int m_bodies;
};

colorshaders.cpp

bool ColorShaderClass::Render(ID3D10Device* device, int indexCount, float * positions)
{
    bool result;

    // Set the shader parameters that it will use for rendering.
    result = SetShaderParameters(device,positions);
    if (!result)
    {
        return false;
    }

    // Now render the prepared buffers with the shader.
    RenderShader(device, indexCount);

    return true;
}

bool ColorShaderClass::SetShaderParameters(ID3D10Device* device, float * positions)
{
    HRESULT result;
    void* mappedResource;

    unsigned int bufferNumber;

    // Lock the constant buffer so it can be written to.
    result = m_matrixBuffer->Map(D3D10_MAP_WRITE_DISCARD, 0, &mappedResource);
    if(FAILED(result))
    {
        return false;
    }

    // Get a pointer to the data in the constant buffer.
    memcpy(mappedResource, positions, 4 * m_bodies * sizeof(float));

    // Unlock the constant buffer.
    m_matrixBuffer->Unmap();

    // Set the position of the constant buffer in the vertex shader.
    bufferNumber = 0;

    // Finally set the constant buffer in the vertex shader with the updated values.
    device->VSSetConstantBuffers(bufferNumber, 1, &m_matrixBuffer);

    return true;
}

void ColorShaderClass::RenderShader(ID3D10Device* device, int indexCount)
{
    // Set the vertex input layout.
    device->IASetInputLayout(m_layout);

    // Set the vertex and pixel shaders that will be used to render the objects.
    device->VSSetShader(m_vertexShader);
    device->PSSetShader(m_pixelShader);

    // Render the objects.
    device->DrawIndexed(indexCount, 0, 0);

    return;
}

color.ps

// Pixel Shader
float4 PS( float4 Pos : SV_POSITION ) : SV_Target
{
    return float4( 1.0f, 1.0f, 1.0f, 1.0f );    // White, with Alpha = 1
}

color.vs

// GLOBALS 
cbuffer MatrixBuffer { 
    float4 updatedPosition;
};

float4 VS( float4 Pos : POSITION ) : SV_POSITION
{
    return Pos;
}

1 个答案:

答案 0 :(得分:1)

当您在float * m_positions;

中更新值存储时

您正在更改CPU端的值。它不是存储在gpu上的实际值,也就是您在创建m_vertexBuffer时指定的值

为此,您需要再次调用渲染函数Initialize(ID3D10Device*, HWND);

应该有一行代码如下:

vertexData.pSysMem = vertices;
vertexData.SysMemPitch = 0;
vertexData.SysMemSlicePitch = 0;

// Now create the vertex buffer.
result = device->CreateBuffer(&vertexBufferDesc, &vertexData, &m_vertexBuffer);

这里顶点是你的m_positions +你怀疑的颜色。

不建议这样做,但应该做好。

如果您想进一步查看,则需要使用D3D11_USAGE_DYNAMIC标志创建此缓冲区并使用

更新值
D3D11_MAPPED_SUBRESOURCE resource;
d3dDeviceContext->Map( vertexBuffer, 0, D3D11_MAP_WRITE_DISCARD, 0, &resource );
memcpy( resource.pData, sourceData, vertexDataSize );
d3dDeviceContext->Unmap( vertexBuffer, 0 );

看看: Direct3D c++ api how to update vertex buffer?

我希望你能找到与你相关的东西。 Razterizer太棒了