Commit 4c74d89e authored by hybrid's avatar hybrid

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