I wrote this function I plan on using to load in .obj files from Blender into a DirectX application. The problem I'm having is that when when the model is loaded, it appears that every other triangle face is missing in a pattern, for every quad, only 1 traingle is visible. I've been trying to debug it for a while and still can't find the issue. According to the debugger it's loading the exact amount of vertecies & faces as Blender says are in the model.

I was having this issue before when using a triangle face method to load in a quad model, I fixed that issue by simply adding a Trianglulate Modifier, but at that time I was importing the model into Visual Studio and then exporting to make it readable for another function I wrote previously. Because the extra import/export step was getting annoying I wrote the function again to load in the .obj models directly from blender and I'm sure the models are triaglulated.

I can't seem to find what's causing the problem again. The faces seem to there but are not drawn or just overlapping

struct faceindexgroup { int v_index, tx_index, nrm_index;};

bool Model3D::successfullyLoadedDataFromBLENDER_OBJ(const QString &blenderfilename)
{

    QStringList filespacecuts = fileReadToLine(blenderfilename, " ").split(" ");

    QList <QVector3D> vertecies, normals;
    QList <QPointF> texcoords;
    QList <faceindexgroup> ifaces;

    const QString *cline = &filespacecuts.at(0); //isolated by spaces or a new line

    for(int c = 0, sz = filespacecuts.size(); c<sz; cline = &filespacecuts.at(c < sz ? c++ : 0)) {

        bool is_v = false, is_vt = false, is_vn = false;

        if((is_v = (*cline) == "v") or (is_vt = (*cline) == "vt") or (is_vn = (*cline) == "vn")) {

            float cx =  filespacecuts.at(c++).toFloat();
            float cy =  filespacecuts.at(c++).toFloat();
            float cz  = !is_vt ? filespacecuts.at(c++).toFloat() : 0;

            if(is_v)
                vertecies.append(QVector3D (cx, cy, cz));
            else if(is_vt)
                texcoords.append(QPointF(cx, cy));
            else if(is_vn)
                normals.append(QVector3D (cx, cy, cz));

        }

        else if((*cline) == "f") {

            IntList v1 = stringListToInts(filespacecuts.at(c++).split('/'));    
            IntList v2 = stringListToInts(filespacecuts.at(c++).split('/'));
            IntList v3 = stringListToInts(filespacecuts.at(c++).split('/'));

            faceindexgroup f;
            f.v_index = v1.at(0);
            f.tx_index = v1.at(1);
            f.nrm_index = v1.at(2);
            ifaces.append(f);
            f.v_index = v2.at(0);
            f.tx_index = v2.at(1);
            f.nrm_index = v2.at(2);
            ifaces.append(f);
            f.v_index = v3.at(0);
            f.tx_index = v3.at(1);
            f.nrm_index = v3.at(2);
            ifaces.append(f);

        }
    }

    if(texcoords.isEmpty())
        resizeList(texcoords, vertecies.size());

    for(int c = 0, sz = ifaces.size(); c<sz; c++) {
        const faceindexgroup *cf = &ifaces.at(c);
        const QVector3D
                *cv = &vertecies.at(cf->v_index - 1),
                *cn = &normals.at(cf->nrm_index - 1);
        const QPointF *ct = &texcoords.at(cf->tx_index - 1);


        //contains the vertex, texure uv coord, and a noraml | 3 per face
        ModelVertexProperty mv;

        mv.x = cv->x();
        mv.y = cv->y();
        mv.z = cv->z();

        mv.tu = ct->x();
        mv.tv = ct->y();

        mv.nx = cn->x();
        mv.ny = cn->y();
        mv.nz = cn->z();

        d.vertexproperties.append(mv);
    }

    d.vertexcount = vertecies.size();
    d.indexcount = d.vertexcount;
    return true;

    }

I'm using a few convieniece functions which I use a lot on difference occasions and after checking even them too, I'm sure they aren't causing the problem

Just one thing to make sure it, have you disabled back-face culling? It's a common mistake that can cause faces to not be drawn just because the vertices are wound in the wrong direction (cw / ccw).

Other than that, it is hard to see a problem with a partial piece of code. I think you should try to make a very simple model file (only a few triangles), and then, step through the loading using a debugger to see if it gets loaded correctly, and to see exactly what happens when it gets drawn.

I found the problem. It was this d.vertexcount = vertecies.size() /*the amount in the file*/; at the end of the function where I set the vertex count. I should've went to the new vertex set generated by the function for the model rather than the original vertex count in the file. With the fact each vertex is referenced usually 6 times by surrounding faces, I screwed myself by not considering that I was, in a way, extracting the vertices from the .obj files' index groups. As a result only 1/6 of the total vertices were drawn.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.