3

遗憾的是,我终于设法在立方体上添加了纹理,但它显示不正确。如下图所示。

替代文字

无论如何,我不确定它可能是什么。我的第一个猜测是它可能是我的 uv 映射或我的顶点定位关闭。如果有人可以检查并确保那很好。第一个元素是顶点位置,第二个是颜色,第三个是uv纹理。

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

1 回答 1

3

解决了这个问题。问题仍然在我的输入布局描述中。

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 时。使其准确。我的偏移量也错了

于 2010-05-30T16:13:29.923 回答