текстура DirectX отображается неправильно - PullRequest
3 голосов
/ 30 мая 2010

Мне наконец-то удалось получить текстуру на кубе, но она появляется неправильно. как показано на рисунке ниже.

alt text

В любом случае, я не уверен, что это может быть. Мое первое предположение - это может быть мое ультрафиолетовое отображение или мое расположение вершин отключено. Если кто-то может проверить и убедиться, что это хорошо. Первый элемент - это положение вершины, второй - это цвет, а третий - это ультрафиолетовая текстура.

struct VertexPos
{
    D3DXVECTOR3 pos;
    D3DXVECTOR4 color;
    D3DXVECTOR3 normal;
    D3DXVECTOR2 texCoord;
};

 // Create vertex buffer
     VertexPos vertices[] =
    {
        // BACK SIDES
        { D3DXVECTOR3(-5.0f,  5.0f, 5.0f), D3DXVECTOR4(1.0f,0.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,0.0)},
        { D3DXVECTOR3(-5.0f, -5.0f, 5.0f), D3DXVECTOR4(1.0f,0.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,1.0)},
        { D3DXVECTOR3(5.0f,  5.0f, 5.0f), D3DXVECTOR4(1.0f,0.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f)  ,D3DXVECTOR2(0.0,1.0)},

        { D3DXVECTOR3(5.0f,  5.0f, 5.0f), D3DXVECTOR4(1.0f,0.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},
        { D3DXVECTOR3(-5.0f, -5.0f, 5.0f), D3DXVECTOR4(1.0f,0.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,1.0)},
        { D3DXVECTOR3(5.0f,  -5.0f, 5.0f), D3DXVECTOR4(1.0f,0.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,1.0)},

        // 2 FRONT SIDE
        { D3DXVECTOR3(-5.0f,  5.0f, -5.0f), D3DXVECTOR4(0.0f,1.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,0.0)},
        { D3DXVECTOR3(5.0f,  5.0f, -5.0f), D3DXVECTOR4(0.0f,1.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f)  ,D3DXVECTOR2(1.0,0.0)},
        { D3DXVECTOR3(-5.0f, -5.0f, -5.0f), D3DXVECTOR4(0.0f,1.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},

        { D3DXVECTOR3(-5.0f, -5.0f, -5.0f), D3DXVECTOR4(0.0f,1.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},
        { D3DXVECTOR3(5.0f,  5.0f, -5.0f), D3DXVECTOR4(0.0f,1.0f,0.0f,0.0f) ,D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,0.0)},
        { D3DXVECTOR3(5.0f,  -5.0f, -5.0f), D3DXVECTOR4(0.0f,1.0f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,1.0)},

        // 3
        { D3DXVECTOR3(-5.0f, 5.0f,  5.0f), D3DXVECTOR4(0.0f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,0.0)},
        { D3DXVECTOR3(5.0f, 5.0f,  5.0f),  D3DXVECTOR4(0.0f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,0.0)},
        { D3DXVECTOR3(-5.0f, 5.0f, -5.0f), D3DXVECTOR4(0.0f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},

        { D3DXVECTOR3(-5.0f, 5.0f, -5.0f), D3DXVECTOR4(0.0f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},
        { D3DXVECTOR3(5.0f, 5.0f, 5.0f), D3DXVECTOR4(0.0f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,0.0)},
        { D3DXVECTOR3(5.0f, 5.0f, -5.0f),  D3DXVECTOR4(0.0f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},

        // 4
        { D3DXVECTOR3(-5.0f, -5.0f,  5.0f), D3DXVECTOR4(1.0f,0.5f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,0.0)},
        { D3DXVECTOR3(-5.0f, -5.0f, -5.0f), D3DXVECTOR4(1.0f,0.5f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,0.0)},
        { D3DXVECTOR3(5.0f, -5.0f,  5.0f),  D3DXVECTOR4(1.0f,0.5f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},

        { D3DXVECTOR3(5.0f, -5.0f,  5.0f),  D3DXVECTOR4(1.0f,0.5f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},
        { D3DXVECTOR3(-5.0f, -5.0f, -5.0f), D3DXVECTOR4(1.0f,0.5f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,0.0)},
        { D3DXVECTOR3(5.0f, -5.0f, -5.0f),  D3DXVECTOR4(1.0f,0.5f,0.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},

        // 5
        { D3DXVECTOR3(5.0f,  5.0f, -5.0f), D3DXVECTOR4(0.0f,1.0f,0.5f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,0.0)},
        { D3DXVECTOR3(5.0f,  5.0f,  5.0f), D3DXVECTOR4(0.0f,1.0f,0.5f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,0.0)},
        { D3DXVECTOR3(5.0f, -5.0f, -5.0f), D3DXVECTOR4(0.0f,1.0f,0.5f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},

        { D3DXVECTOR3(5.0f, -5.0f, -5.0f), D3DXVECTOR4(0.0f,1.0f,0.5f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},
        { D3DXVECTOR3(5.0f,  5.0f,  5.0f), D3DXVECTOR4(0.0f,1.0f,0.5f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,0.0)},
        { D3DXVECTOR3(5.0f, -5.0f,  5.0f), D3DXVECTOR4(0.0f,1.0f,0.5f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},

        // 6
        {D3DXVECTOR3(-5.0f,  5.0f, -5.0f), D3DXVECTOR4(0.5f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,0.0)},
        {D3DXVECTOR3(-5.0f, -5.0f, -5.0f), D3DXVECTOR4(0.5f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,0.0)},
        {D3DXVECTOR3(-5.0f,  5.0f,  5.0f), D3DXVECTOR4(0.5f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},

        {D3DXVECTOR3(-5.0f,  5.0f,  5.0f), D3DXVECTOR4(0.5f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},
        {D3DXVECTOR3(-5.0f, -5.0f, -5.0f), D3DXVECTOR4(0.5f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(1.0,0.0)},
        {D3DXVECTOR3(-5.0f, -5.0f,  5.0f), D3DXVECTOR4(0.5f,0.0f,1.0f,0.0f),D3DXVECTOR3(0.0f,0.0f,0.0f) ,D3DXVECTOR2(0.0,1.0)},
    };

Моим вторым предположением может быть ошибка, которую я получаю при запуске программы. Но я не знаю, с чего начать. Ниже приведено описание ошибки.

D3D10: WARNING: ID3D10Device::Draw: Vertex Buffer at the input vertex slot 0 is not big enough for what the Draw*() call expects to traverse. This is OK, as reading off the end of the Buffer is defined to return 0. However the developer probably did not intend to make use of this behavior.  [ EXECUTION WARNING #356: DEVICE_DRAW_VERTEX_BUFFER_TOO_SMALL ]

Не уверен, что это может быть. но вот мое описание макета вершины

//Create Layout
D3D10_INPUT_ELEMENT_DESC layout[] = {
    {"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT, 0 , 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0 , 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"NORMAL",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0 , 28, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"TEXCOORD",0, DXGI_FORMAT_R32G32_FLOAT, 0 , 44, D3D10_INPUT_PER_VERTEX_DATA, 0}
};

UINT numElements = (sizeof(layout)/sizeof(layout[0]));
modelObject.numVertices = sizeof(vertices)/sizeof(VertexPos);

for(int i = 0; i < modelObject.numVertices; i += 3)
{
    D3DXVECTOR3 out;

    D3DXVECTOR3 v1 = vertices[0 + i].pos;
    D3DXVECTOR3 v2 = vertices[1 + i].pos;
    D3DXVECTOR3 v3 = vertices[2 + i].pos;

    D3DXVECTOR3 u = v2 - v1;
    D3DXVECTOR3 v = v3 - v1;

    D3DXVec3Cross(&out, &u, &v);
    D3DXVec3Normalize(&out, &out);

    vertices[0 + i].normal = out;
    vertices[1 + i].normal = out;
    vertices[2 + i].normal = out;
}

//Create buffer desc
D3D10_BUFFER_DESC bufferDesc;
bufferDesc.Usage = D3D10_USAGE_DEFAULT;
bufferDesc.ByteWidth = sizeof(VertexPos) * modelObject.numVertices;
bufferDesc.BindFlags = D3D10_BIND_VERTEX_BUFFER;
bufferDesc.CPUAccessFlags = 0;
bufferDesc.MiscFlags = 0;

D3D10_SUBRESOURCE_DATA initData;
initData.pSysMem = vertices;
//Create the buffer

HRESULT hr = mpD3DDevice->CreateBuffer(&bufferDesc, &initData, &modelObject.pVertexBuffer);
if(FAILED(hr))
    return false;

1 Ответ

3 голосов
/ 30 мая 2010

исправил проблему. Проблема была в моем входном описании макета до сих пор.

D3D10_INPUT_ELEMENT_DESC layout[] = {
    {"POSITION",0,DXGI_FORMAT_R32G32B32_FLOAT, 0 , 0, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"COLOR",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0 , 12, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"NORMAL",0,DXGI_FORMAT_R32G32B32A32_FLOAT, 0 , 28, D3D10_INPUT_PER_VERTEX_DATA, 0},
    {"TEXCOORD",0, DXGI_FORMAT_R32G32_FLOAT, 0 , 44, D3D10_INPUT_PER_VERTEX_DATA, 0}
};

моей нормальной вершине было присвоено значение rgba, когда оно определено в моей структуре вершин как rgb или xyz. делая это в точности. Мое смещение тоже было неверным

...