-1
\$\begingroup\$

my attempt at loading obj and calculating normals per vertex is not working:

                getline(OBJfile, line); 
                istringstream iss(line);
                iss >> x >> y >> z;
                vData[vertexIndex].position = D3DXVECTOR3(x, y, z);
                vertexIndex++;

                D3DXVECTOR3 curNormal;
                D3DXVec3Normalize(&curNormal, &vData[normalIndex].position);
                vData[normalIndex].normal = curNormal;
                normalIndex++;

so it seems to load the first face and thats it. i had it loading meshes fine until i added normals.. i wasnt sure how to bind normals per vertex so i created a structure:

struct VertexData
{
D3DXVECTOR3 position;
D3DXVECTOR3 normal;
};

then for the indices i made a structure like so:

struct FaceType
{
int vIndex1, vIndex2, vIndex3; //vertex index per vertex per face
int nIndex1, nIndex2, nIndex3; //normal index per vertex per face
};

and loaded filled it like:

if (c == 'f')
        {
            getline(OBJfile, line); 

            istringstream iss(line);
            iss >> v1 >> v2 >> v3;
            faces[faceIndex].vIndex1 = v1 - 1;
            faces[faceIndex].vIndex2 = v2 - 1;
            faces[faceIndex].vIndex3 = v3 - 1;

            faces[faceIndex].nIndex1 = v1 - 1;
            faces[faceIndex].nIndex2 = v2 - 1;
            faces[faceIndex].nIndex3 = v3 - 1;
            faceIndex++;
        }

yet again i believe it is only drawing the first face it find because when i run it and try loading a cube from an obj file, i only get an extremly elongated triangle... please someone come through for me... ive been working on this for days now and would LOVE to moveon and fully understand this process

\$\endgroup\$
1
\$\begingroup\$

you are making the classic obj to opengl mistake, each int in the index relates to all data of each vertex in the buffers

you will want to duplicate the data on reading and fill a singular buffer see the code my answer on SO

\$\endgroup\$
  • \$\begingroup\$ this is directx 10 \$\endgroup\$ – Dave Mar 9 '14 at 17:06
  • \$\begingroup\$ @Dave I believe the principle is the same \$\endgroup\$ – ratchet freak Mar 9 '14 at 17:19
  • \$\begingroup\$ so what your saying is there is no need to use two indices(one for position and one for normal), but that it is only neccessary to have one index for both? if so i tried it and same results \$\endgroup\$ – Dave Mar 9 '14 at 20:29
  • \$\begingroup\$ @Dave yeah a cursory look through the api doesn't show me any way to interleave indexes, thus you need to pair each position and normal in the indexes \$\endgroup\$ – ratchet freak Mar 9 '14 at 20:33
  • \$\begingroup\$ what do you mean in each index... like each index would be "2 1" orrr......? \$\endgroup\$ – Dave Mar 9 '14 at 23:57

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.