Advertisement Jump to content
Sign in to follow this  
Kestli

Problems when rendering objects in Ogre using Assimp

This topic is 1823 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

When I try to view some 3D files (.3ds .dae etc ...) with Assimp, they are not displayed correctly. For example, "Jeep1.3ds" displayed me half the bodywork.
All nodes of information, meshes and normal were obtained correctly, and the number of vertices and faces are correct (I think).
Textures not meet the correct coordinates, I guess will be related to the above ...
In the picture you can "appreciate" my result and correct.
Thanks for any help or opinion.

 

Ogre

Ogre::SceneNode *OgreAPP::makeMesh(Ogre::String meshFile,Ogre::String entityName,Ogre::String meshName)
{
    Ogre::MeshPtr Mesh = Ogre::MeshManager::getSingleton().createManual(meshName,Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME);
    Ogre::SubMesh *subMesh = Mesh->createSubMesh("subMesh");
    Ogre::VertexDeclaration *vertexDeclaration;
    Ogre::HardwareVertexBufferSharedPtr  vertexBuffer;
    Ogre::HardwareIndexBufferSharedPtr   indexBuffer;
    size_t offset=0;

    Ogre::FileInfoListPtr fileInfoListPtr(Ogre::ResourceGroupManager::getSingleton().findResourceFileInfo(Ogre::ResourceGroupManager::DEFAULT_RESOURCE_GROUP_NAME,meshFile,false));
    Ogre::FileInfoList *fileInfoList = fileInfoListPtr.getPointer();
    Ogre::FileInfo &fileInfo = fileInfoList->front();
    stringBuffer << fileInfo.archive->getName().c_str() << meshFile;
    
    ModelManager::loadModel(stringBuffer.str());
    ModelManager::processData();
    std::vector<float>          *vData   = ModelManager::getVertexData();
    std::vector<unsigned short> *iData   = ModelManager::getIndexData();

   subMesh->vertexData = new Ogre::VertexData;
   vertexDeclaration = subMesh->vertexData->vertexDeclaration;
  

    vertexDeclaration->addElement(0,offset,Ogre::VET_FLOAT3,Ogre::VES_POSITION);
    offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);

    vertexDeclaration->addElement(0,offset,Ogre::VET_FLOAT3,Ogre::VES_NORMAL);
    offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT3);

    vertexDeclaration->addElement(0,offset,Ogre::VET_FLOAT2,Ogre::VES_TEXTURE_COORDINATES);
    offset += Ogre::VertexElement::getTypeSize(Ogre::VET_FLOAT2);


    vertexBuffer = Ogre::HardwareBufferManager::getSingleton().createVertexBuffer(offset,
                                                                                  ModelManager::modelInfo.numVertices,
                                                                                  Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY,
                                                                                  true);
    float *bufferPtr = static_cast<float*>(vertexBuffer.getPointer()->lock(Ogre::HardwareBuffer::HBL_DISCARD));
    memcpy(bufferPtr,vData->data(),offset*ModelManager::modelInfo.numVertices);
    vertexBuffer.getPointer()->unlock();

    indexBuffer = Ogre::HardwareBufferManager::getSingleton().createIndexBuffer(Ogre::HardwareIndexBuffer::IT_16BIT,
                                                                                ModelManager::modelInfo.numIndices,
                                                                                Ogre::HardwareBuffer::HBU_STATIC_WRITE_ONLY,
                                                                                true);
    unsigned short *indexPtr = static_cast<unsigned short*>(indexBuffer.getPointer()->lock(Ogre::HardwareBuffer::HBL_DISCARD));
    memcpy(indexPtr,iData->data(),sizeof(unsigned short)*ModelManager::modelInfo.numIndices);
    indexBuffer.getPointer()->unlock();

    subMesh->useSharedVertices = false;
    subMesh->vertexData->vertexBufferBinding->setBinding(0,vertexBuffer);
    subMesh->vertexData->vertexCount = vertexBuffer.getPointer()->getNumVertices();
    subMesh->indexData->indexBuffer = indexBuffer;
    subMesh->indexData->indexCount = indexBuffer.getPointer()->getNumIndexes();
    subMesh->indexData->indexStart = 0;


    Mesh->_setBounds(Ogre::AxisAlignedBox(-100000,-100000,-100000,100000,100000,100000));
    Mesh->_setBoundingSphereRadius(100000);
    Mesh->load();

    stringBuffer.str("");
    stringBuffer << entityName << "_n";
    oSceneManager->createEntity(entityName,meshName);
    oSceneManager->getEntity(entityName)->setMaterialName("material/textura");
    oSceneManager->getRootSceneNode()->createChildSceneNode(stringBuffer.str().c_str())->attachObject(oSceneManager->getEntity(entityName));
   oSceneManager->getSceneNode(stringBuffer.str().c_str())->setPosition(0,0,0);

    std::cout << "Nodes    : " << ModelManager::modelInfo.numNodes << "\n";
    std::cout << "Meshes   : " << ModelManager::modelInfo.numMeshes << "\n";
    std::cout << "Vertices : " << ModelManager::modelInfo.numVertices << "\n";
    std::cout << "Faces    : " << ModelManager::modelInfo.numFaces << "\n";
    std::cout << "Indices  : " << ModelManager::modelInfo.numIndices << "\n";

    return oSceneManager->getSceneNode(stringBuffer.str().c_str());
}

Assimp (CPP)

bool ModelManager::loadModel(std::string &file)
{
    modelScene = importer.ReadFile(file,aiProcess_Triangulate |
                                        aiProcess_GenNormals  |
                                        aiProcess_GenUVCoords);

    if (!modelScene)
    {
        MessageBoxA(NULL,importer.GetErrorString(),"oo",MB_ICONERROR);
        return false;
    }

    return true;
}


bool ModelManager::assimpGetMeshData(const aiMesh *mesh)
{
    aiFace *face;

    for (unsigned int v=0;v<mesh->mNumVertices;v++)
     {
        vertexBuff.push_back(mesh->mVertices[v].x);
        vertexBuff.push_back(mesh->mVertices[v].y);
        vertexBuff.push_back(mesh->mVertices[v].z);
        vertexBuff.push_back(mesh->mNormals[v].x);
        vertexBuff.push_back(mesh->mNormals[v].y);
        vertexBuff.push_back(mesh->mNormals[v].z);
        vertexBuff.push_back(mesh->mTextureCoords[0][v].x);
        vertexBuff.push_back(mesh->mTextureCoords[0][v].y);
     }

    for (unsigned int f=0;f<mesh->mNumFaces;f++)
     {
        face = &mesh->mFaces[f];
        indexBuff.push_back(face->mIndices[0]);
        indexBuff.push_back(face->mIndices[1]);
        indexBuff.push_back(face->mIndices[2]);
     }
    modelInfo.numMeshes++;
    modelInfo.numFaces+=mesh->mNumFaces;
    modelInfo.numVertices+=mesh->mNumVertices;
    modelInfo.numIndices+=(mesh->mNumFaces*3);
    return true;
}


bool ModelManager::processData()
{
    bool repeat=true;
    nodeBuff.push_back(modelScene->mRootNode);

    if (modelScene->mNumMeshes > 0)
    {
        for (unsigned int m=0;m<modelScene->mNumMeshes;m++)
            this->assimpGetMeshData(modelScene->mMeshes[m]);
    }

    while (repeat)
    { 
        for (unsigned int a=0;a<nodeBuff.size();a++)
        {
            modelNode = nodeBuff.at(a);
            if (modelNode->mNumChildren > 0)
                for (unsigned int c=0;c<modelNode->mNumChildren;c++)
                       nodeBuff.push_back(modelNode->mChildren[c]);
            else repeat=false;
        }
    }

    for (unsigned int a=0;a<nodeBuff.size();a++)
    {
        modelNode = nodeBuff.at(a);
        if (modelNode->mNumMeshes>0)
            for (unsigned int b=0;b<modelNode->mNumMeshes;b++)
                assimpGetMeshData(modelScene->mMeshes[modelNode->mMeshes[b]]);
    }
    modelInfo.numNodes = nodeBuff.size();
    return true;
}

std::vector<float> *ModelManager::getVertexData()
{
    return &vertexBuff;
}

std::vector<unsigned short> *ModelManager::getIndexData()
{
    return &indexBuff;
}

Assimp (H)

#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <assimp/cimport.h>

#include <Windows.h>
#include <iostream>
#include <stdint.h>
#include <vector>

class ModelManager
{
public:
    ModelManager();
    bool loadModel(std::string &);
    bool processData();
    std::vector<float> *getVertexData();
    std::vector<unsigned short> *getIndexData();
private:
    bool assimpGetMeshData(const aiMesh *);
private:
    Assimp::Importer            importer;
    const aiScene               *modelScene;
    const aiNode                *modelNode;
    const aiMesh                *modelMesh;
    const aiFace                *modelFace;
    std::vector<float>          vertexBuff;
    std::vector<unsigned short> indexBuff;
    std::vector<const aiNode*>  nodeBuff;

protected:
    struct{
        unsigned int numNodes;
        unsigned int numMeshes;
        unsigned int numVertices;
        unsigned int numFaces;
        unsigned int numIndices;
    }modelInfo;
};

[attachment=19510:jeep2.jpg]

Share this post


Link to post
Share on other sites
Advertisement

Ok ... errors, as they spend most of the time, in practice something small and basic but complex background.
"Simply" switch the coordinate system, when I import the file would have to be specified Assimp flags "aiProcess_MakeLeftHanded | aiProcess_FlipWindingOrder | aiProcess_FlipUVs" or "aiProcess_ConvertToLeftHanded".
Other than that I missed some transformation matrices (they had not considered), for that I use the flag "aiProcess_PreTransformVertices".
Apparently everything works fine ...

 

New Code

bool ModelManager::loadModel(std::string &file)
{
    modelScene = importer.ReadFile(file,aiProcess_MakeLeftHanded|aiProcess_FlipWindingOrder|aiProcess_FlipUVs|aiProcess_PreTransformVertices|
                                   aiProcess_CalcTangentSpace|
                                   aiProcess_GenSmoothNormals|
                                   aiProcess_Triangulate|
                                   aiProcess_FixInfacingNormals|
                                   aiProcess_FindInvalidData |
                                   aiProcess_ValidateDataStructure | 0
                                   );

    if (!modelScene)
    {
        MessageBoxA(NULL,importer.GetErrorString(),"Error: La concha de la lora...",MB_ICONERROR);
        return false;
    }

    return true;
}

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!