Ich bin daran einen Assimp Loader zu schreiben. Ich will ihn mit einem Vertex- und IndexBuffer programmieren.
Mein Code sieht so aus:
Code: Alles auswählen
// AssimpLoader.h
//
#ifndef ASSIMPLOADER_HPP
#define ASSIMPLOADER_HPP
#include "D3D.h"
#include <assimp.h>
#include <assimp.hpp>
#include <aiScene.h>
#include <aiPostProcess.h>
#include <aiMaterial.h>
#include <aiVector2D.h>
#include <aiVector3D.h>
#include <string>
using namespace std;
#pragma comment(lib, "assimp.lib")
// VertexStruktur erzeugen
struct SMeshVertex
{
D3DXVECTOR3 vPosition;
D3DXVECTOR3 vTangent;
D3DXVECTOR3 vNormal;
D3DXVECTOR2 vTexture;
};
#define MeshFVF (D3DFVF_XYZ | D3DFVF_NORMAL | D3DFVF_TEX1)
// Klasse erzeugen
class CLoader
{
private:
const aiScene * m_Scene;
LPD3DXMESH m_pMesh;
LPDIRECT3DVERTEXBUFFER9 m_pVB;
LPDIRECT3DVERTEXBUFFER9 m_pIB;
public:
bool LoadFile(PDIRECT3DDEVICE9 Device, string &Filename);
void Render();
void Release();
D3DXVECTOR3 MakeToD3DXVector3(const aiVector3D & Vector);
D3DXVECTOR2 MakeToD3DXVector2(const aiVector3D & Vector);
};
#endif
Code: Alles auswählen
// AssimpLoader.cpp
//
#include "AssimpLoader.h"
bool CLoader::LoadFile(LPDIRECT3DDEVICE9 Device, string &file)
{
HRESULT hResult;
Assimp::Importer importer;
SMeshVertex* vertices;
UINT numVertices;
UINT numTriangles;
DWORD* indices;
UINT* attributeBuffer;
m_Scene = importer.ReadFile( file,
aiProcess_CalcTangentSpace |
aiProcess_Triangulate |
aiProcess_MakeLeftHanded |
aiProcess_JoinIdenticalVertices |
aiProcess_SortByPType |
aiProcess_CalcTangentSpace |
aiProcess_JoinIdenticalVertices |
aiProcess_GenSmoothNormals |
aiProcess_LimitBoneWeights |
aiProcess_RemoveRedundantMaterials |
aiProcess_OptimizeMeshes);
if(!m_Scene)
{
MessageBox(NULL, "There is a mesh missing. Please try again or reeinstall the game.", "Mesh missing", MB_OK);
PostQuitMessage(1);
}
aiMesh** meshes =m_Scene->mMeshes;
aiMaterial** materials = m_Scene->mMaterials;
UINT* meshID;
UINT materialID;
aiNode* sceneRoot = m_Scene->mRootNode;
aiNode** children = sceneRoot->mChildren;
aiNode* child;
UINT i = 0;
bool rootNode = true;
while(i < sceneRoot->mNumChildren)
{
if(rootNode)
{
child = sceneRoot;
rootNode = false;
} else
{
child = children[i];
i++;
}
if(!(child->mNumMeshes > 0))
continue;
meshID = child->mMeshes;
numVertices = meshes[meshID[0]]->mNumVertices;
numTriangles = meshes[meshID[0]]->mNumFaces;
if(FAILED(hResult = g_pD3DDevice->CreateVertexBuffer((numVertices * sizeof(SMeshVertex)),
D3DUSAGE_WRITEONLY,
MeshFVF,
D3DPOOL_MANAGED,
&m_pVB,
NULL)))
{
MessageBox(NULL, "Fehler beim Erzeugen des VertexBuffers vom Mesh", "Fehler aufgetreten",
MB_OK | MB_ICONEXCLAMATION);
return false;
}
vertices = new SMeshVertex[numVertices];
m_pVB->Lock(0, 0, (VOID**)&vertices, 0);
for(UINT j = 0; j < meshes[meshID[0]]->mNumVertices; ++j)
{
vertices[j].vPosition = MakeToD3DXVector3(meshes[meshID[0]]->mVertices[j]);
vertices[j].vTangent = MakeToD3DXVector3(meshes[meshID[0]]->mTangents[j]);
vertices[j].vNormal = MakeToD3DXVector3(meshes[meshID[0]]->mNormals[j]);
vertices[j].vTexture = MakeToD3DXVector2(meshes[meshID[0]]->mTextureCoords[0][j]);
}
m_pVB->Unlock();
indices = new DWORD[numTriangles*3];
attributeBuffer = new UINT[numTriangles];
m_pIB->Lock(0, 0, (VOID**)&indices, 0);
for(UINT j = 0; j < meshes[meshID[0]]->mNumFaces; j++)
{
indices[j*3+0] = meshes[meshID[0]]->mFaces[j].mIndices[0];
indices[j*3+1] = meshes[meshID[0]]->mFaces[j].mIndices[1];
indices[j*3+2] = meshes[meshID[0]]->mFaces[j].mIndices[2];
}
m_pIB->Unlock();
}
delete[] vertices;
delete[] indices;
delete[] attributeBuffer;
return true;
}
D3DXVECTOR3 CLoader::MakeToD3DXVector3(const aiVector3D & Vector)
{
D3DXVECTOR3 NewVector;
NewVector.x = Vector.x;
NewVector.y = Vector.y;
NewVector.z = Vector.z;
return NewVector;
}
D3DXVECTOR2 CLoader::MakeToD3DXVector2(const aiVector3D & Vector)
{
D3DXVECTOR2 NewVector;
NewVector.x = Vector.x;
NewVector.y = Vector.y;
return NewVector;
}
Code: Alles auswählen
1>------ Erstellen gestartet: Projekt: AssimpLoader, Konfiguration: Debug Win32 ------
1>AssimpLoader.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall Assimp::Importer::~Importer(void)" (??1Importer@Assimp@@QAE@XZ)" in Funktion ""public: bool __thiscall CLoader::LoadFile(struct IDirect3DDevice9 *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?LoadFile@CLoader@@QAE_NPAUIDirect3DDevice9@@AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)".
1>AssimpLoader.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: __thiscall Assimp::Importer::Importer(void)" (??0Importer@Assimp@@QAE@XZ)" in Funktion ""public: bool __thiscall CLoader::LoadFile(struct IDirect3DDevice9 *,class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char> > &)" (?LoadFile@CLoader@@QAE_NPAUIDirect3DDevice9@@AAV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@@Z)".
1>AssimpLoader.obj : error LNK2019: Verweis auf nicht aufgelöstes externes Symbol ""public: struct aiScene const * __thiscall Assimp::Importer::ReadFile(char const *,unsigned int)" (?ReadFile@Importer@Assimp@@QAEPBUaiScene@@PBDI@Z)" in Funktion ""public: struct aiScene const * __thiscall Assimp::Importer::ReadFile(class std::basic_string<char,struct std::char_traits<char>,class std::allocator<char>> const &,unsigned int)" (?ReadFile@Importer@Assimp@@QAEPBUaiScene@@ABV?$basic_string@DU?$char_traits@D@std@@V?$allocator@D@2@@std@@I@Z)".
1>c:\users\patrick egli\documents\visual studio 2010\Projects\AssimpLoader\Debug\AssimpLoader.exe : fatal error LNK1120: 3 nicht aufgelöste externe Verweise.
========== Erstellen: 0 erfolgreich, Fehler bei 1, 0 aktuell, 0 übersprungen ==========
Was habe ich vergessen?
Meine zweite Frage, habe ich die Init-Funktion richtig geschrieben, oder wird der das Modell falsch geladen, wenn ich es so programmiere?
LG Patrick