0
\$\begingroup\$

I'm just loading a simple textured cube but it always has missing bits or warped textures. This is for directx 11 rendering by the way. Updated with more information. I think my problem is with duplicate verticies. Generally only 8 are needed to make the cube but there is more texture coordinates than that.

Cube

std::ifstream file(filepath);

if (file)
{
    std::string buffer;

    std::vector<DirectX::XMFLOAT3> vertexPositions;
    std::vector<VertexPositionTexture> verticies = std::vector<VertexPositionTexture>();
    std::vector<unsigned short> indicies = std::vector<unsigned short>();
    std::vector<DirectX::XMFLOAT2> textureCoordinates;
    std::vector<VertexTextureNormalIndex> faceIndicies;

    while (std::getline(file, buffer))
    {
        if (buffer[0] == 'v' && buffer[1] == ' ') {
            float tmpx, tmpy, tmpz;
            sscanf(buffer.c_str(), "v %f %f %f", &tmpx, &tmpy, &tmpz);
            vertexPositions.push_back(DirectX::XMFLOAT3(tmpx, tmpy, tmpz));
        }
        else if (buffer[0] == 'v' && buffer[1] == 't') {
            float vt1 = 0, vt2 = 0;
            sscanf(buffer.c_str(), "vt %f %f", &vt1, &vt2);
            textureCoordinates.push_back(DirectX::XMFLOAT2(vt1, vt2));
        }
        else if (buffer[0] == 'f') {
            int v1 = 0, v2 = 0, v3 = 0;
            int vt1 = 0, vt2 = 0, vt3 = 0;
            int vn1 = 0, vn2 = 0, vn3 = 0;

            if (buffer.find("//") != std::string::npos) {
                sscanf(buffer.c_str(), "f %d//%d %d//%d %d//%d", &v1, &vn1, &v2, &vn2, &v3, &vn3);
                faceIndicies.push_back({ v1 - 1, -1, vn1 - 1 });
                faceIndicies.push_back({ v2 - 1, -1, vn2 - 1 });
                faceIndicies.push_back({ v3 - 1, -1, vn3 - 1 });
            }
            if (buffer.find("/") != std::string::npos) {
                sscanf(buffer.c_str(), "f %d/%d/%d %d/%d/%d %d/%d/%d", &v1, &vt1, &vn1, &v2, &vt2, &vn2, &v3, &vt3, &vn3);
                faceIndicies.push_back({ v1 - 1, vt1 - 1, vn1 - 1 });
                faceIndicies.push_back({ v2 - 1, vt2 - 1, vn2 - 1 });
                faceIndicies.push_back({ v3 - 1, vt3 - 1, vn3 - 1 });
            }
            else
            {
                sscanf(buffer.c_str(), "f %d %d %d", &v1, &v2, &v3);
                faceIndicies.push_back({ v1 - 1, -1, -1 });
                faceIndicies.push_back({ v2 - 1, -1, -1 });
                faceIndicies.push_back({ v3 - 1, -1, -1 });
            }
        }
    }

    int faceCount = static_cast<int>(faceIndicies.size());

    if (faceCount > 0) {
        std::map<int, int> vertexMap;
        int nextIndex = 0;
        for (int f = 0; f < static_cast<int>(faceIndicies.size()); f++) {
            VertexTextureNormalIndex vtn = faceIndicies.at(f);

            std::map<int, int>::iterator iter = vertexMap.find(vtn.VertexIndex);

            // Check if index already exists
            if (iter != vertexMap.end())
            {
                indicies.push_back(iter->second);
            }
            else {
                DirectX::XMFLOAT2 textureCoords;

                if (vtn.TextureIndex > -1) {
                    textureCoords = textureCoordinates.at(vtn.TextureIndex);
                }
                else {
                    textureCoords = DirectX::XMFLOAT2(0, 0);
                }

                verticies.push_back({ vertexPositions.at(vtn.VertexIndex), textureCoords });
                vertexMap[vtn.VertexIndex] = nextIndex;
                indicies.push_back(nextIndex);
                nextIndex++;
            }
        }
    }

    mesh->verticies = verticies;
    mesh->indicies = indicies;
}
# Blender v2.80 (sub 75) OBJ File: 'SpaceBox.blend'
# www.blender.org
v 1.000000 1.000000 -1.000000
v 1.000000 -1.000000 -1.000000
v 1.000000 1.000000 1.000000
v 1.000000 -1.000000 1.000000
v -1.000000 1.000000 -1.000000
v -1.000000 -1.000000 -1.000000
v -1.000000 1.000000 1.000000
v -1.000000 -1.000000 1.000000
vt 0.257701 0.281720
vt 0.486975 0.052446
vt 0.257701 0.052446
vt 0.257701 0.510994
vt 0.486975 0.281720
vt 0.257701 0.281720
vt 0.257701 0.740269
vt 0.486975 0.510994
vt 0.257701 0.969543
vt 0.486975 0.740269
vt 0.028426 0.740269
vt 0.257701 0.510994
vt 0.028426 0.510994
vt 0.716249 0.510994
vt 0.486975 0.510994
vt 0.486975 0.281720
vt 0.486975 0.740269
vt 0.486975 0.969543
vt 0.257701 0.740269
vt 0.716249 0.740269
vn 0.0000 -1.0000 0.0000
vn 0.0000 0.0000 -1.0000
vn 1.0000 0.0000 0.0000
vn 0.0000 1.0000 0.0000
vn -1.0000 0.0000 0.0000
vn 0.0000 0.0000 1.0000
s off
f 3/1/1 5/2/1 1/3/1
f 8/4/2 3/5/2 4/6/2
f 6/7/3 7/8/3 8/4/3
f 8/9/4 2/10/4 6/7/4
f 4/11/5 1/12/5 2/13/5
f 2/10/6 5/14/6 6/15/6
f 3/1/1 7/16/1 5/2/1
f 8/4/2 7/8/2 3/5/2
f 6/7/3 5/17/3 7/8/3
f 8/9/4 4/18/4 2/10/4
f 4/11/5 3/19/5 1/12/5
f 2/10/6 1/20/6 5/14/6
\$\endgroup\$
  • \$\begingroup\$ Please remember to always share the specific symptoms of the problem: say, screenshots of which bits are missing, or how the textures are warped. Include the text of your input obj file too so we can walk through exactly how your routine performs on that input. \$\endgroup\$ – DMGregory Apr 13 at 11:41
  • \$\begingroup\$ If you are getting warped textures and missing bits, there's probably a problem with the texture coordinates. Could you include some screenshots of the warped textures and maybe the contents of your obj file? \$\endgroup\$ – The Digital Odyssey Apr 13 at 17:43
  • \$\begingroup\$ Added additional information. \$\endgroup\$ – Hayden Morris Apr 14 at 3:09
0
\$\begingroup\$

I don't know if this is true in DX11, but usually you do need one vertex per texture co-ordinate.

So you would need 4 vertices x 6 faces = 24 verts. It seems inefficient but it is the way to do it.

Also, you will find that each corner of the cube should have 3 vertex normals, so you will need the 24 verts to accommodate correct normals.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ How does that work with the index buffer? \$\endgroup\$ – Hayden Morris Apr 14 at 6:09
  • \$\begingroup\$ Not sure what you mean? You should make sure it's split when you export it so the tris should be on indices 1..24. There should still be the same amount of indices. \$\endgroup\$ – Jay Apr 14 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.