DirectX:纹理不对齐

时间:2015-01-09 21:39:40

标签: directx-11 texture2d

我正在尝试加载obj模型并对其应用纹理。我目前只使用D3d11。该模型正在从搅拌机中输出。

如图所示,我能够成功渲染模型的顶点和法线,但纹理坐标似乎未对齐。该模型在其他obj查看器中成功呈现,因此它必须是我的设置/代码。

我能想到为什么会发生这种情况的唯一原因是CreateWICTextureFromFile正在调整纹理文件的大小,导致U,V坐标不对齐。

loaded obj model

我使用以下内容加载模型(我将其从左手坐标转换为右手坐标):

void Model::init_from_obj(char *filename){
    std::vector<Vector2> vertex_texture;
    std::vector<Vector3> vertex_normal;

    float x, y, z;
    int a, b, c, d, e, f, g, h, i;
    std::string line;
    std::ifstream obj_file(filename);

    while (getline(obj_file, line)){
        if (line[0] == 'v' && line[1] == ' '){
            sscanf_s(&line[0], "v %f %f %f", &x, &y, &z);
            this->verts.push_back({ x, y, z  * -1.0f, 0.0f, 0.0f, 0.0f, 0.0f, 0.0f });
        }
        else if (line[0] == 'v' && line[1] == 't'){
            sscanf_s(&line[0], "vt %f %f", &x, &y);
            vertex_texture.push_back({ x, 1.0f - y });
        }
        else if (line[0] == 'v' && line[1] == 'n'){
            sscanf_s(&line[0], "v %f %f %f", &x, &y, &z);
            vertex_normal.push_back({ x, y, z * -1.0f});
        }
        else if (line[0] == 'f'){
            sscanf_s(&line[0], "f %d/%d/%d %d/%d/%d %d/%d/%d", &a, &d, &g, &b, &e, &h, &c, &f, &i);
            this->indices.push_back((a-1));

            this->verts[(a - 1)].tU = vertex_texture[(d - 1)].x;
            this->verts[(a - 1)].tV = vertex_texture[(d - 1)].y;

            this->verts[(a - 1)].nX = vertex_normal[(g - 1)].x;
            this->verts[(a - 1)].nY = vertex_normal[(g - 1)].y;
            this->verts[(a - 1)].nZ = vertex_normal[(g - 1)].z;

            this->indices.push_back((c - 1));

            this->verts[(c - 1)].tU = vertex_texture[(f - 1)].x;
            this->verts[(c - 1)].tV = vertex_texture[(f - 1)].y;

            this->verts[(c - 1)].nX = vertex_normal[(i - 1)].x;
            this->verts[(c - 1)].nY = vertex_normal[(i - 1)].y;
            this->verts[(c - 1)].nZ = vertex_normal[(i - 1)].z;

            this->indices.push_back((b - 1));

            this->verts[(b - 1)].tU = vertex_texture[(e - 1)].x;
            this->verts[(b - 1)].tV = vertex_texture[(e - 1)].y;

            this->verts[(b - 1)].nX = vertex_normal[(h - 1)].x;
            this->verts[(b - 1)].nY = vertex_normal[(h - 1)].y;
            this->verts[(b - 1)].nZ = vertex_normal[(h - 1)].z;
        }
    }
}

我使用以下内容加载纹理:

hr = DirectX::CreateWICTextureFromFile(directx.dev, this->texture_filename, nullptr, &texture);

if (FAILED(hr))
    debug_file << "FAILED to load model texture" << std::endl;

// Describe the Sample State
D3D11_SAMPLER_DESC sampDesc;
ZeroMemory(&sampDesc, sizeof(sampDesc));
sampDesc.Filter = D3D11_FILTER_MIN_MAG_MIP_LINEAR;
sampDesc.AddressU = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressV = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.AddressW = D3D11_TEXTURE_ADDRESS_WRAP;
sampDesc.ComparisonFunc = D3D11_COMPARISON_NEVER;
sampDesc.MinLOD = 0;
sampDesc.MaxLOD = D3D11_FLOAT32_MAX;

//Create the Sample State
hr = directx.dev->CreateSamplerState(&sampDesc, &SamplerState);

最后,我使用以下设置进行渲染:

directx.devcon->PSSetShaderResources(0, 1, &texture);
directx.devcon->PSSetSamplers(0, 1, &SamplerState);

如果我还需要提供其他任何内容,请告诉我。大约3天,我不知道。

0 个答案:

没有答案