Jump to content

  • Log In with Google      Sign In   
  • Create Account

We're offering banner ads on our site from just $5!

1. Details HERE. 2. GDNet+ Subscriptions HERE. 3. Ad upload HERE.


Imported OBJ Looks Funky... :(


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
9 replies to this topic

#1 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 01 August 2012 - 02:15 AM

Below is a screen shot of my imported OBJ inside my editor. I've been looking at several tutorials and for now - I just want to get a simple OBJ cube loaded with a color shader file. I am think I'm confused on the whole Index section, because I think this is where it's going haywire. I'll try to chainge the topology to trianglelist because now I have it set to trianglestrip.

Inside the debug console it writes the OBJ structure. 8 vertices and 12 faces. the OBJ import reverse the Index backwards also. It has to be the part where it loads the vertex and the index. Whenever ever someone has time - could they at least if I'm doing something wrong in codewise.


    // Read in the vertices, texture coordinates, and normals into the data structures.
    // Important: Also convert to left hand coordinate system since Maya uses right hand coordinate system.
    fin.get(input);
    while(!fin.eof())
    {
        if(input == 'v')
        {
            fin.get(input);

            // Read in the vertices.
            if(input == ' ')
            {
                fin >> vertices[vertexIndex].x >> vertices[vertexIndex].y >> vertices[vertexIndex].z;

                // Invert the Z vertex to change to left hand system.
                vertices[vertexIndex].z = vertices[vertexIndex].z * -1.0f;
                vertexIndex++;
            }
           


            // Read in the texture uv coordinates.
            if(input == 't')
            {
                fin >> texcoords[texcoordIndex].x >> texcoords[texcoordIndex].y;

                // Invert the V texture coordinates to left hand system.
                texcoords[texcoordIndex].y = 1.0f - texcoords[texcoordIndex].y;
                texcoordIndex++;
            }

            // Read in the normals.
            if(input == 'n')
            {
                fin >> normals[normalIndex].x >> normals[normalIndex].y >> normals[normalIndex].z;

                // Invert the Z normal to change to left hand system.
                normals[normalIndex].z = normals[normalIndex].z * -1.0f;
                normalIndex++;
            }
        }

        // Read in the faces.
        if(input == 'f')
        {
            fin.get(input);
            if(input == ' ')
            {
                // Read the face data in backwards to convert it to a left hand system from right hand system.
                fin >> faces[faceIndex].vIndex3 >> input2 >> faces[faceIndex].tIndex3 >> input2 >> faces[faceIndex].nIndex3
                    >> faces[faceIndex].vIndex2 >> input2 >> faces[faceIndex].tIndex2 >> input2 >> faces[faceIndex].nIndex2
                    >> faces[faceIndex].vIndex1 >> input2 >> faces[faceIndex].tIndex1 >> input2 >> faces[faceIndex].nIndex1;
                faceIndex++;
            }
        }

        // Read in the remainder of the line.
        while(input != '\n')
        {
            fin.get(input);
        }

        // Start reading the beginning of the next line.
        fin.get(input);
    }

    // Close the file.
    fin.close();

this part gathers the stored index count and literates the the file again - grabbing vertex data and such.

D3D11_BUFFER_DESC vertexBufferDesc;
    ZeroMemory( &vertexBufferDesc, sizeof(vertexBufferDesc) );

    vertexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    vertexBufferDesc.ByteWidth = sizeof(VertexType) * vCount;
    vertexBufferDesc.BindFlags = D3D11_BIND_VERTEX_BUFFER;
    vertexBufferDesc.CPUAccessFlags = 0;
    vertexBufferDesc.MiscFlags = 0;
    ///////////////**************new**************////////////////////

    D3D11_SUBRESOURCE_DATA vertexBufferData;

    ZeroMemory( &vertexBufferData, sizeof(vertexBufferData) );
    vertexBufferData.pSysMem = &vertices[0];
    HR( dev->CreateBuffer( &vertexBufferDesc, &vertexBufferData, &objVertexBuffer));

    //Set the vertex buffer
    UINT stride = sizeof(VertexType);
    UINT offset = 0;
    devcon->IASetVertexBuffers( 0, 1, &objVertexBuffer, &stride, &offset );

    Debug::WriteLine("OBJ Vertex Data Loaded Successfully!");
   
    D3D11_BUFFER_DESC indexBufferDesc;
    ZeroMemory( &indexBufferDesc, sizeof(indexBufferDesc) );

    indexBufferDesc.Usage = D3D11_USAGE_DEFAULT;
    indexBufferDesc.ByteWidth = sizeof(FaceType) * 3 * 12;
    indexBufferDesc.BindFlags = D3D11_BIND_INDEX_BUFFER;
    indexBufferDesc.CPUAccessFlags = 0;
    indexBufferDesc.MiscFlags = 0;

    D3D11_SUBRESOURCE_DATA iinitData;

    iinitData.pSysMem = &faces[0];
    HR(dev->CreateBuffer(&indexBufferDesc, &iinitData, &objIndexBuffer));

    devcon->IASetIndexBuffer( objIndexBuffer, DXGI_FORMAT_R32_UINT, 0);

    Debug::WriteLine("Index Buffer from OBJ loaded Successfully!");

   

then when it's drawn - it looks like what's below. Additional, here's a wireframe shot within the editor.
OBJGoneWrong.PNG
BadOBJWireFrame.PNG

Thanks for the help guys! Much appreciated!

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


Sponsor:

#2 BitMaster   Crossbones+   -  Reputation: 4274

Like
5Likes
Like

Posted 01 August 2012 - 02:23 AM

I have not looked at the code in detail but the absolutely most common error when loading .obj files which might produce such errors is that the indices in there are one-based while just about every rendering system will expect zero-based indices.

#3 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 01 August 2012 - 02:50 AM

would you suggest I create my own format or look into another format like FBX? Since FBX is popular but I read on google there's some problems - so I wasn't sure if I should go ahead with importing FBX files. Thanks for the response BitMaster.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#4 BitMaster   Crossbones+   -  Reputation: 4274

Like
2Likes
Like

Posted 01 August 2012 - 03:09 AM

The huge advantage of .obj files is that they are human readable (a real blessing for anything you need to debug) and relatively simple (usually, they can contain some weird stuff but you usually never encounter those bits in 'normal' files).
Unless you would rather use a library (like Assimp) I would strongly advise sticking with .obj files until loading and display works as intended. When that is working, consider a different format.

That said, I would advise against writing your own loaders (unless that is the point of the exercise). It is a rather lengthy and not very interesting work that has already been solved a lot of times.

#5 powly k   Members   -  Reputation: 653

Like
3Likes
Like

Posted 01 August 2012 - 03:10 AM

No, he suggests that you substract one (1) from your indices, because .obj lists them as 1,2,3... and everyone else lists them 0,1,2...

Which was my thought too, just based on the topic headline. Didn't dig into your code yet, though.

#6 scniton   Members   -  Reputation: 252

Like
1Likes
Like

Posted 01 August 2012 - 10:50 AM

In addition to what has been said about the indexing base, you code makes several assumptions about the content of obj file, in particular:
  • Triangles only (i.e. always 3 sets of indices for faces, no more, no less)
  • Texture AND normal data is always present (i.e. face indices are always of the form: #/#/# )
I would check the file by hand to make sure these assumptions are not violated.

Also, I'm not sure what you mean by changing the topology to triangle list, given your above assumptions the trivial way of rendering the parsed obj data is as a triangle list.

Edited by scniton, 01 August 2012 - 10:50 AM.

Stop twiddling your bits and use them already!

#7 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 01 August 2012 - 01:25 PM

Alright, I'm gotta study more about setting up Index Buffers a bit more. If I look back now, it doesn't really make sense in Index Buffer's mind because it's like, "You want me to draw what again? Okay cool, I'll draw this but you'll not like it!" So, this reminds me to do more studing before tackling the fun stuff.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#8 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 01 August 2012 - 02:05 PM

OH YEAH BABY! A successful Obj Import without texture just pretty hot red! Okay, so the code now just picks out the vertices and the indices! Check it out - wireframe and solid.

SuccessfulOBJImport-SOLID.PNG SuccessfulOBJImport-WIRE.PNG

You guy's da BEST!

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#9 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 01 August 2012 - 02:46 PM

Okay, a cube - so what right? Well, I did some revisions to the code to automatically calculate the vertices and indices. In the drawIndex section I did the same calculation again and this is the final result of a imported obj sphere. Looks like a egg but eggs are good too!

Successful_OBJ_Sphere_Import-SOLID.PNG Successful_OBJ_Sphere_Import-Wire.PNG

Game Engine's WIP Videos - http://www.youtube.com/sicgames88


#10 SIC Games   Members   -  Reputation: 617

Like
0Likes
Like

Posted 01 August 2012 - 04:20 PM

BitMaster was right that I had to substract fromt he Index Collection in order to draw this on screen. Scniton was correct at the code's assumption. With this in mind - I came to the conclusion...if it's reading the index and collecting the Index Verteices and the normal Indexes and the Texture Indexes. I better filter the one's I just need for now then improve upon. I justed wanted to challenge myself to get a simple obj file imported. The sphere looks squashed so I'm working on this and addiotnally I'm working on the importing side. I got so excited I forgot to thank everyone.

Game Engine's WIP Videos - http://www.youtube.com/sicgames88





Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS