Some minor changes. Simplified the .obj loader, also fixed some bugs.
git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@830 dfc29bdd-3216-0410-991c-e03cc46cb475master
parent
29e890bfc3
commit
4c74d89e82
|
@ -32,7 +32,7 @@ const float fast_atof_table[16] = { // we write [16] here instead of [] to work
|
|||
0.000000000000001f
|
||||
};
|
||||
|
||||
inline u32 strtol10( const char* in, const char* &out)
|
||||
inline u32 strtol10(const char* in, const char** out=0)
|
||||
{
|
||||
u32 value = 0;
|
||||
c8 symbol;
|
||||
|
@ -44,9 +44,10 @@ inline u32 strtol10( const char* in, const char* &out)
|
|||
break;
|
||||
|
||||
value = ( value * 10 ) + ( symbol - '0' );
|
||||
in += 1;
|
||||
++in;
|
||||
}
|
||||
out = in;
|
||||
if (out)
|
||||
*out = in;
|
||||
return value;
|
||||
}
|
||||
|
||||
|
@ -66,16 +67,14 @@ inline const char* fast_atof_move( const char* c, float& out)
|
|||
}
|
||||
|
||||
//f = (float)strtol(c, &t, 10);
|
||||
f = (float) strtol10 ( c, t );
|
||||
|
||||
c = t;
|
||||
f = (float) strtol10 ( c, &c );
|
||||
|
||||
if (*c == '.')
|
||||
{
|
||||
c++;
|
||||
|
||||
//float pl = (float)strtol(c, &t, 10);
|
||||
float pl = (float) strtol10 ( c, t );
|
||||
float pl = (float) strtol10 ( c, &t );
|
||||
pl *= fast_atof_table[t-c];
|
||||
|
||||
f += pl;
|
||||
|
@ -90,12 +89,11 @@ inline const char* fast_atof_move( const char* c, float& out)
|
|||
if (einv)
|
||||
c++;
|
||||
|
||||
float exp = (float)strtol10(c, t);
|
||||
float exp = (float)strtol10(c, &c);
|
||||
if (einv)
|
||||
exp *= -1.0f;
|
||||
|
||||
f *= (float)pow(10.0f, exp);
|
||||
c = t;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -36,7 +36,7 @@ CCubeSceneNode::CCubeSceneNode(f32 size, ISceneNode* parent, ISceneManager* mgr,
|
|||
setDebugName("CCubeSceneNode");
|
||||
#endif
|
||||
|
||||
u16 u[36] = { 0,2,1, 0,3,2, 1,5,4, 1,2,5, 4,6,7, 4,5,6,
|
||||
const u16 u[36] = { 0,2,1, 0,3,2, 1,5,4, 1,2,5, 4,6,7, 4,5,6,
|
||||
7,3,0, 7,6,3, 9,5,2, 9,8,5, 0,11,10, 0,10,7};
|
||||
|
||||
Buffer.Indices.set_used(36);
|
||||
|
|
|
@ -448,4 +448,4 @@ void CGUIStaticText::deserializeAttributes(io::IAttributes* in, io::SAttributeRe
|
|||
} // end namespace gui
|
||||
} // end namespace irr
|
||||
|
||||
#endif _IRR_COMPILE_WITH_GUI_
|
||||
#endif // _IRR_COMPILE_WITH_GUI_
|
||||
|
|
|
@ -71,10 +71,10 @@ IAnimatedMesh* COBJMeshFileLoader::createMesh(io::IReadFile* file)
|
|||
core::array<core::vector3df> vertexBuffer;
|
||||
core::array<core::vector2df> textureCoordBuffer;
|
||||
core::array<core::vector3df> normalsBuffer;
|
||||
SObjGroup * pCurrGroup = 0;
|
||||
SObjMtl * pCurrMtl = new SObjMtl();
|
||||
pCurrMtl->name="";
|
||||
materials.push_back(pCurrMtl);
|
||||
u32 smoothingGroup=0;
|
||||
|
||||
// ********************************************************************
|
||||
// Patch to locate the file in the same folder as the .obj.
|
||||
|
@ -143,19 +143,20 @@ IAnimatedMesh* COBJMeshFileLoader::createMesh(io::IReadFile* file)
|
|||
}
|
||||
break;
|
||||
|
||||
case 'g': // group
|
||||
// get name of group
|
||||
case 'g': // group names skipped
|
||||
{
|
||||
c8 groupName[WORD_BUFFER_LENGTH];
|
||||
pBufPtr = goAndCopyNextWord(groupName, pBufPtr, WORD_BUFFER_LENGTH, pBufEnd);
|
||||
pCurrGroup = findOrAddGroup(groupName);
|
||||
pBufPtr = goNextLine(pBufPtr, pBufEnd);
|
||||
}
|
||||
break;
|
||||
|
||||
case 's': // smoothing can be on or off
|
||||
case 's': // smoothing can be a group or off (equiv. to 0)
|
||||
{
|
||||
c8 smooth[WORD_BUFFER_LENGTH];
|
||||
pBufPtr = goAndCopyNextWord(smooth, pBufPtr, WORD_BUFFER_LENGTH, pBufEnd);
|
||||
if (core::stringc("off")==smooth)
|
||||
smoothingGroup=0;
|
||||
else
|
||||
smoothingGroup=core::strtol10(smooth, 0);
|
||||
}
|
||||
break;
|
||||
|
||||
|
@ -196,24 +197,21 @@ IAnimatedMesh* COBJMeshFileLoader::createMesh(io::IReadFile* file)
|
|||
// sends the buffer sizes and gets the actual indices
|
||||
// if index not set returns -1
|
||||
s32 Idx[3];
|
||||
Idx[0] = Idx[1] = Idx[2] = -1;
|
||||
Idx[1] = Idx[2] = -1;
|
||||
|
||||
// read in next vertex's data
|
||||
u32 wlength = copyWord(vertexWord, pLinePtr, WORD_BUFFER_LENGTH, pBufEnd);
|
||||
// this function will also convert obj's 1-based index to c++'s 0-based index
|
||||
retrieveVertexIndices(vertexWord, Idx, vertexWord+wlength+1, vertexBuffer.size());
|
||||
if ( -1 != Idx[0] )
|
||||
{
|
||||
retrieveVertexIndices(vertexWord, Idx, vertexWord+wlength+1, vertexBuffer);
|
||||
v.Pos = vertexBuffer[Idx[0]];
|
||||
}
|
||||
if ( -1 != Idx[1] )
|
||||
{
|
||||
v.TCoords = textureCoordBuffer[Idx[1]];
|
||||
}
|
||||
else
|
||||
v.TCoords.set(0.0f,0.0f);
|
||||
if ( -1 != Idx[2] )
|
||||
{
|
||||
v.Normal = normalsBuffer[Idx[2]];
|
||||
}
|
||||
else
|
||||
v.Normal.set(0.0f,0.0f,0.0f);
|
||||
pCurrMtl->pMeshbuffer->Vertices.push_back(v);
|
||||
++facePointCount;
|
||||
|
||||
|
@ -605,33 +603,6 @@ COBJMeshFileLoader::SObjMtl* COBJMeshFileLoader::findMtl(const c8* pMtlName)
|
|||
|
||||
|
||||
|
||||
COBJMeshFileLoader::SObjGroup * COBJMeshFileLoader::findGroup(const c8* pGroupName)
|
||||
{
|
||||
for (u32 i = 0; i < groups.size(); ++i)
|
||||
{
|
||||
if ( groups[i]->name == pGroupName )
|
||||
return groups[i];
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
COBJMeshFileLoader::SObjGroup * COBJMeshFileLoader::findOrAddGroup(const c8* pGroupName)
|
||||
{
|
||||
SObjGroup * pGroup = findGroup( pGroupName );
|
||||
if ( 0 != pGroup )
|
||||
{
|
||||
// group found, return it
|
||||
return pGroup;
|
||||
}
|
||||
// group not found, create a new group
|
||||
SObjGroup* group = new SObjGroup();
|
||||
group->name = pGroupName;
|
||||
groups.push_back(group);
|
||||
return group;
|
||||
}
|
||||
|
||||
|
||||
//! skip space characters and stop on first non-space
|
||||
const c8* COBJMeshFileLoader::goFirstWord(const c8* buf, const c8* const pBufEnd)
|
||||
{
|
||||
|
@ -720,12 +691,11 @@ const c8* COBJMeshFileLoader::goAndCopyNextWord(c8* outBuf, const c8* inBuf, u32
|
|||
}
|
||||
|
||||
|
||||
bool COBJMeshFileLoader::retrieveVertexIndices(c8* pVertexData, s32* pIdx, const c8* pBufEnd, u32 bufferSize)
|
||||
bool COBJMeshFileLoader::retrieveVertexIndices(c8* pVertexData, s32* pIdx, const c8* pBufEnd, const core::array<core::vector3df>& vbuffer)
|
||||
{
|
||||
c8 word[16] = "";
|
||||
const c8* pChar = goFirstWord(pVertexData, pBufEnd);
|
||||
u32 idxType = 0; // 0 = posIdx, 1 = texcoordIdx, 2 = normalIdx
|
||||
s32 index;
|
||||
|
||||
u32 i = 0;
|
||||
while ( pChar != pBufEnd )
|
||||
|
@ -735,17 +705,16 @@ bool COBJMeshFileLoader::retrieveVertexIndices(c8* pVertexData, s32* pIdx, const
|
|||
// build up the number
|
||||
word[i++] = *pChar;
|
||||
}
|
||||
else if ( *pChar == '/' || *pChar == '\0' )
|
||||
else if ( *pChar == '/' || *pChar == ' ' || *pChar == '\0' )
|
||||
{
|
||||
// number is completed. Convert and store it
|
||||
word[i] = '\0';
|
||||
// if no number was found index will become 0 and later on -1 by decrement
|
||||
index = atoi( word );
|
||||
const s32 index = core::strtol10(word, 0);
|
||||
if (index<0)
|
||||
index += bufferSize;
|
||||
pIdx[idxType] = index+vbuffer.size();
|
||||
else
|
||||
--index;
|
||||
pIdx[idxType] = index;
|
||||
pIdx[idxType] = index-1;
|
||||
|
||||
// reset the word
|
||||
word[0] = '\0';
|
||||
|
@ -760,7 +729,7 @@ bool COBJMeshFileLoader::retrieveVertexIndices(c8* pVertexData, s32* pIdx, const
|
|||
idxType = 0;
|
||||
}
|
||||
}
|
||||
else if (*pChar == '\0')
|
||||
else
|
||||
{
|
||||
// set all missing values to disable (=-1)
|
||||
while (++idxType < 3)
|
||||
|
@ -789,13 +758,6 @@ void COBJMeshFileLoader::cleanUp()
|
|||
}
|
||||
|
||||
materials.clear();
|
||||
|
||||
for (i = 0; i < groups.size(); ++i )
|
||||
{
|
||||
delete groups[i];
|
||||
}
|
||||
|
||||
groups.clear();
|
||||
}
|
||||
|
||||
|
||||
|
|
|
@ -43,30 +43,19 @@ private:
|
|||
struct SObjMtl
|
||||
{
|
||||
SObjMtl() : pMeshbuffer(0), illumination(0) {
|
||||
this->pMeshbuffer = new SMeshBuffer();
|
||||
this->pMeshbuffer->Material.Shininess = 0.0f;
|
||||
this->pMeshbuffer->Material.AmbientColor = video::SColorf(0.2f, 0.2f, 0.2f, 1.0f).toSColor();
|
||||
this->pMeshbuffer->Material.DiffuseColor = video::SColorf(0.8f, 0.8f, 0.8f, 1.0f).toSColor();
|
||||
this->pMeshbuffer->Material.SpecularColor = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f).toSColor();
|
||||
pMeshbuffer = new SMeshBuffer();
|
||||
pMeshbuffer->Material.Shininess = 0.0f;
|
||||
pMeshbuffer->Material.AmbientColor = video::SColorf(0.2f, 0.2f, 0.2f, 1.0f).toSColor();
|
||||
pMeshbuffer->Material.DiffuseColor = video::SColorf(0.8f, 0.8f, 0.8f, 1.0f).toSColor();
|
||||
pMeshbuffer->Material.SpecularColor = video::SColorf(1.0f, 1.0f, 1.0f, 1.0f).toSColor();
|
||||
};
|
||||
SObjMtl(SObjMtl& o) : pMeshbuffer(o.pMeshbuffer) { o.pMeshbuffer->grab(); };
|
||||
|
||||
~SObjMtl() { };
|
||||
SObjMtl(SObjMtl& o) : pMeshbuffer(o.pMeshbuffer), name(o.name), illumination(o.illumination) { o.pMeshbuffer->grab(); };
|
||||
|
||||
scene::SMeshBuffer *pMeshbuffer;
|
||||
core::stringc name;
|
||||
c8 illumination;
|
||||
};
|
||||
|
||||
struct SObjGroup
|
||||
{
|
||||
SObjGroup() {};
|
||||
SObjGroup(SObjGroup& o) {};
|
||||
~SObjGroup() { };
|
||||
|
||||
core::stringc name;
|
||||
};
|
||||
|
||||
// returns a pointer to the first printable character available in the buffer
|
||||
const c8* goFirstWord(const c8* buf, const c8* const pBufEnd);
|
||||
// returns a pointer to the first printable character after the first non-printable
|
||||
|
@ -80,7 +69,11 @@ private:
|
|||
// combination of goNextWord followed by copyWord
|
||||
const c8* goAndCopyNextWord(c8* outBuf, const c8* inBuf, u32 outBufLength, const c8* const pBufEnd);
|
||||
|
||||
//! Read the material from the given file
|
||||
void readMTL(const c8* pFileName, core::stringc relPath);
|
||||
//! Find and return the material with the given name
|
||||
SObjMtl * findMtl(const c8* pMtlName);
|
||||
|
||||
//! Read RGB color
|
||||
const c8* readColor(const c8* pBufPtr, video::SColor& color, const c8* const pBufEnd);
|
||||
//! Read 3d vector of floats
|
||||
|
@ -89,14 +82,11 @@ private:
|
|||
const c8* readVec2(const c8* pBufPtr, core::vector2df& vec, const c8* const pBufEnd);
|
||||
//! Read boolean value represented as 'on' or 'off'
|
||||
const c8* readBool(const c8* pBufPtr, bool& tf, const c8* const pBufEnd);
|
||||
SObjMtl * findMtl(const c8* pMtlName);
|
||||
SObjGroup * findGroup(const c8* pGroupName);
|
||||
SObjGroup * findOrAddGroup(const c8* pGroupName);
|
||||
|
||||
// reads and convert to integer the vertex indices in a line of obj file's face statement
|
||||
// -1 for the index if it doesn't exist
|
||||
// indices are changed to 0-based index instead of 1-based from the obj file
|
||||
bool retrieveVertexIndices(c8* pVertexData, s32* Idx, const c8* pBufEnd, u32 bufferSize);
|
||||
bool retrieveVertexIndices(c8* pVertexData, s32* Idx, const c8* pBufEnd, const core::array<core::vector3df>& vbuffer);
|
||||
|
||||
void cleanUp();
|
||||
|
||||
|
@ -104,7 +94,6 @@ private:
|
|||
video::IVideoDriver* Driver;
|
||||
|
||||
core::array<SObjMtl*> materials;
|
||||
core::array<SObjGroup*> groups;
|
||||
SMesh* Mesh;
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in New Issue