Wow, you are fast:)
Thank you for helping me out. I've tried your example and it works, I am not able to figure out the difference between your example and full mesh loader which does not work. I did not posted the full source because I did not want to clutter the forum but if you be so kind, please take a look:
YWFMeshLoader.h
Code: Select all
#ifndef __YWF_MESH_FILE_LOADER_H_INCLUDED__
#define __YWF_MESH_FILE_LOADER_H_INCLUDED__
#include "IMeshLoader.h"
#include "irrString.h"
#include "vector3d.h"
#include "irrlicht.h"
using namespace irr;
using namespace scene;
class YWFMeshFileLoader : public IMeshLoader
{
public:
//! returns true if the file maybe is able to be loaded by this class
//! based on the file extension (i.e. ".ywf")
virtual bool isALoadableFileExtension(const io::path& filename) const;
//! creates/loads an animated mesh from the file.
//! \return Pointer to the created mesh. Returns 0 if loading failed.
//! If you no longer need the mesh, you should call IAnimatedMesh::drop().
//! See IReferenceCounted::drop() for more information.
virtual IAnimatedMesh* createMesh(io::IReadFile* file);
private:
//! Check if the file has correct header and size, returns information how many indices and vertices are contained in file.
bool checkHeader(io::IReadFile* file, long& fileSize, u16& verticesCount, u16& indicesCount) const;
//! All vertices are loaded at once, returns fallse in case of error.
bool loadIndices(io::IReadFile* file, u16 count, core::array<u16> &indices) const;
//! All vertices are loaded at once, returns fallse in case of error.
bool loadVertices(io::IReadFile* file, u16 count, core::array<core::vector3df> &vertices) const;
};
#endif
YWFMeshLoader.cpp
Code: Select all
#include "stdafx.h"
#include "YWFMeshLoader.h"
#include "SMesh.h"
#include "SMeshBuffer.h"
#include "SAnimatedMesh.h"
#include "IReadFile.h"
#include "fast_atof.h"
#include "coreutil.h"
#include "os.h"
using namespace irr::core;
bool YWFMeshFileLoader::checkHeader(io::IReadFile* file, long& fileSize, u16& vertexCount, u16& indexCount) const
{
fileSize = file->getSize();
if (!fileSize)
return false;
if (file->read(&vertexCount, sizeof(vertexCount)) != sizeof(vertexCount))
return false;
if (file->read(&indexCount, sizeof(indexCount)) != sizeof(indexCount))
return false;
//Check if the file has expected size. fileSize should be unsigned long not long, typecast is safe here to get rid of warning.
if ((unsigned long)fileSize != vertexCount * 3 * sizeof(f32) + indexCount * sizeof(u16) + sizeof(vertexCount) + sizeof(indexCount))
return false;
return true;
}
bool YWFMeshFileLoader::isALoadableFileExtension(const io::path& filename) const
{
return core::hasFileExtension(filename, "ywf");
}
bool YWFMeshFileLoader::loadVertices(io::IReadFile* file, u16 count, core::array<core::vector3df> &vertices) const
{
core::vector3df vector;
for (size_t i = 1; i <= count; i++)
{
if (file->read(&vector.X, sizeof(vector.X)) == 0)
return false;
if (file->read(&vector.Y, sizeof(vector.Y)) == 0)
return false;
if (file->read(&vector.X, sizeof(vector.Z)) == 0)
return false;
vertices.push_back(vector);
}
return true;
}
bool YWFMeshFileLoader::loadIndices(io::IReadFile* file, u16 count, core::array<u16> &indices) const
{
u16 index;
for (size_t i = 1; i <= count; i++)
{
if (file->read(&index, sizeof(index)) == 0)
return false;
indices.push_back(index);
}
return true;
}
IAnimatedMesh* YWFMeshFileLoader::createMesh(io::IReadFile* file)
{
u16 vertexCount = 0;
u16 indexCount = 0;
long fileSize;
array<vector3df> vertices;
array<u16> indices; //Each index points to position in vertices.
//if (checkHeader(file, fileSize, vertexCount, indexCount) == false)
// return 0;
//if (loadVertices(file, vertexCount, vertices) == false)
// return 0;
//if (loadIndices(file, indexCount, indices) == false)
// return 0;
vertices.push_back(core::vector3df(-100, 0, 100));
vertices.push_back(core::vector3df(100, 0, 100));
vertices.push_back(core::vector3df(100, 0, -100));
indices.push_back(0);
indices.push_back(1);
indices.push_back(2);
SMesh* mesh = new SMesh();
SMeshBuffer* meshBuffer = new SMeshBuffer();
mesh->addMeshBuffer(meshBuffer);
video::SColor color(255, 255, 0, 0);
vector3df normal;
for (u32 i = 0; i != indices.size(); i = i + 3) {
u16 p1 = indices[i];
u16 p2 = indices[i + 1];
u16 p3 = indices[i + 2];
//Perhaps for smooth lighting I should calculate normal based on each neighbouring vertices.
normal = core::plane3df(vertices[p1], vertices[p2], vertices[p3]).Normal;
meshBuffer->Vertices.push_back(video::S3DVertex(vertices[p1], normal, color, core::vector2df()));
meshBuffer->Vertices.push_back(video::S3DVertex(vertices[p2], normal, color, core::vector2df()));
meshBuffer->Vertices.push_back(video::S3DVertex(vertices[p3], normal, color, core::vector2df()));
meshBuffer->Indices.push_back(p1);
meshBuffer->Indices.push_back(p2);
meshBuffer->Indices.push_back(p3);
}
meshBuffer->drop();
//Wrap mesh in animated mesh.
SAnimatedMesh* animMesh = 0;
if (0 != mesh->getMeshBufferCount())
{
mesh->recalculateBoundingBox();
animMesh = new SAnimatedMesh();
animMesh->Type = EAMT_OBJ;
animMesh->addMesh(mesh);
animMesh->recalculateBoundingBox();
}
mesh->drop();
return animMesh;
}
There is one thing I do not understand (apart from why it does not work). Why the triangle in your example is black, I thought that
Code: Select all
video::SColor color(255, 255, 0, 0);
will make it red?