Commit c525b00f authored by cutealien's avatar cutealien

mtl (obj) format reader and write now regards texture scaling and translation....

mtl (obj) format reader and write now regards texture scaling and translation. (thx @thanhle for noticing and patch proposal).


git-svn-id: svn://svn.code.sf.net/p/irrlicht/code/trunk@5029 dfc29bdd-3216-0410-991c-e03cc46cb475
parent ae476a0d
-------------------------- --------------------------
Changes in 1.9 (not yet released) Changes in 1.9 (not yet released)
- mtl (obj) format reader and write now regards texture scaling and translation. (thx @thanhle for noticing and patch proposal).
- Added Visual Studio 2013 project files. - Added Visual Studio 2013 project files.
- Added ability to set custom depth/stencil texture for render targets. - Added ability to set custom depth/stencil texture for render targets.
- Added new color formats: ECF_R8, ECF_R8G8, ECF_D16, ECF_D32, ECF_D24S8. - Added new color formats: ECF_R8, ECF_R8G8, ECF_D16, ECF_D32, ECF_D24S8.
......
...@@ -372,6 +372,11 @@ namespace core ...@@ -372,6 +372,11 @@ namespace core
\return Altered matrix */ \return Altered matrix */
CMatrix4<T>& setTextureTranslate( f32 x, f32 y ); CMatrix4<T>& setTextureTranslate( f32 x, f32 y );
//! Get texture transformation translation
/** \param x returns offset on x axis
\param y returns offset on y axis */
void getTextureTranslate( f32& x, f32& y ) const;
//! Set texture transformation translation, using a transposed representation //! Set texture transformation translation, using a transposed representation
/** Doesn't clear other elements than those affected. /** Doesn't clear other elements than those affected.
\param x Offset on x axis \param x Offset on x axis
...@@ -386,6 +391,11 @@ namespace core ...@@ -386,6 +391,11 @@ namespace core
\return Altered matrix. */ \return Altered matrix. */
CMatrix4<T>& setTextureScale( f32 sx, f32 sy ); CMatrix4<T>& setTextureScale( f32 sx, f32 sy );
//! Get texture transformation scale
/** \param sx Returns x axis scale factor
\param sy Returns y axis scale factor */
void getTextureScale( f32& sx, f32& sy ) const;
//! Set texture transformation scale, and recenter at (0.5,0.5) //! Set texture transformation scale, and recenter at (0.5,0.5)
/** Doesn't clear other elements than those affected. /** Doesn't clear other elements than those affected.
\param sx Scale factor on x axis \param sx Scale factor on x axis
...@@ -862,8 +872,8 @@ namespace core ...@@ -862,8 +872,8 @@ namespace core
//! Returns a rotation that is equivalent to that set by setRotationDegrees(). //! Returns a rotation that is equivalent to that set by setRotationDegrees().
/** This code was sent in by Chev. Note that it does not necessarily return /** This code was sent in by Chev. Note that it does not necessarily return
the *same* Euler angles as those set by setRotationDegrees(), but the rotation will the *same* Euler angles as those set by setRotationDegrees(), but the rotation will
be equivalent, i.e. will have the same result when used to rotate a vector or node. be equivalent, i.e. will have the same result when used to rotate a vector or node.
This code was orginally written by by Chev. This code was orginally written by by Chev.
*/ */
template <class T> template <class T>
inline core::vector3d<T> CMatrix4<T>::getRotationDegrees(const vector3d<T>& scale_) const inline core::vector3d<T> CMatrix4<T>::getRotationDegrees(const vector3d<T>& scale_) const
...@@ -923,7 +933,7 @@ namespace core ...@@ -923,7 +933,7 @@ namespace core
//! Returns a rotation that is equivalent to that set by setRotationDegrees(). //! Returns a rotation that is equivalent to that set by setRotationDegrees().
/** This code was sent in by Chev. Note that it does not necessarily return /** This code was sent in by Chev. Note that it does not necessarily return
the *same* Euler angles as those set by setRotationDegrees(), but the rotation will the *same* Euler angles as those set by setRotationDegrees(), but the rotation will
be equivalent, i.e. will have the same result when used to rotate a vector or node. be equivalent, i.e. will have the same result when used to rotate a vector or node.
This code was orginally written by by Chev. */ This code was orginally written by by Chev. */
template <class T> template <class T>
inline core::vector3d<T> CMatrix4<T>::getRotationDegrees() const inline core::vector3d<T> CMatrix4<T>::getRotationDegrees() const
...@@ -2150,6 +2160,12 @@ namespace core ...@@ -2150,6 +2160,12 @@ namespace core
return *this; return *this;
} }
template <class T>
inline void CMatrix4<T>::getTextureTranslate(f32& x, f32& y) const
{
x = (f32)M[8];
y = (f32)M[9];
}
template <class T> template <class T>
inline CMatrix4<T>& CMatrix4<T>::setTextureTranslateTransposed ( f32 x, f32 y ) inline CMatrix4<T>& CMatrix4<T>::setTextureTranslateTransposed ( f32 x, f32 y )
...@@ -2174,6 +2190,12 @@ namespace core ...@@ -2174,6 +2190,12 @@ namespace core
return *this; return *this;
} }
template <class T>
inline void CMatrix4<T>::getTextureScale ( f32& sx, f32& sy ) const
{
sx = (f32)M[0];
sy = (f32)M[5];
}
template <class T> template <class T>
inline CMatrix4<T>& CMatrix4<T>::setTextureScaleCenter( f32 sx, f32 sy ) inline CMatrix4<T>& CMatrix4<T>::setTextureScaleCenter( f32 sx, f32 sy )
......
...@@ -353,6 +353,8 @@ const c8* COBJMeshFileLoader::readTextures(const c8* bufPtr, const c8* const buf ...@@ -353,6 +353,8 @@ const c8* COBJMeshFileLoader::readTextures(const c8* bufPtr, const c8* const buf
f32 bumpiness = 6.0f; f32 bumpiness = 6.0f;
bool clamp = false; bool clamp = false;
core::vector3df tscale(1.f); //For map_Kd texture scaling
core::vector3df tpos(0.f); //For map_Kd texture translation
// handle options // handle options
while (textureNameBuf[0]=='-') while (textureNameBuf[0]=='-')
{ {
...@@ -391,25 +393,35 @@ const c8* COBJMeshFileLoader::readTextures(const c8* bufPtr, const c8* const buf ...@@ -391,25 +393,35 @@ const c8* COBJMeshFileLoader::readTextures(const c8* bufPtr, const c8* const buf
if (!strncmp(bufPtr,"-o",2)) // texture coord translation if (!strncmp(bufPtr,"-o",2)) // texture coord translation
{ {
bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
if (core::isdigit(textureNameBuf[0]) || (textureNameBuf[0] == '-' && core::isdigit(textureNameBuf[1])))
tpos.X = core::fast_atof(textureNameBuf);
// next parameters are optional, so skip rest of loop if no number is found // next parameters are optional, so skip rest of loop if no number is found
bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
if (!core::isdigit(textureNameBuf[0])) if (!(core::isdigit(textureNameBuf[0]) || (textureNameBuf[0] == '-' && core::isdigit(textureNameBuf[1]))))
continue; continue;
tpos.Y = core::fast_atof(textureNameBuf);
bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
if (!core::isdigit(textureNameBuf[0])) if (!(core::isdigit(textureNameBuf[0]) || (textureNameBuf[0] == '-' && core::isdigit(textureNameBuf[1]))))
continue; continue;
tpos.Z = core::fast_atof(textureNameBuf);
} }
else else
if (!strncmp(bufPtr,"-s",2)) // texture coord scale if (!strncmp(bufPtr,"-s",2)) // texture coord scale
{ {
bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
if (core::isdigit(textureNameBuf[0]) || (textureNameBuf[0] == '-' && core::isdigit(textureNameBuf[1])))
tscale.X = core::fast_atof(textureNameBuf);
// next parameters are optional, so skip rest of loop if no number is found // next parameters are optional, so skip rest of loop if no number is found
bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
if (!core::isdigit(textureNameBuf[0])) if (!(core::isdigit(textureNameBuf[0]) || (textureNameBuf[0] == '-' && core::isdigit(textureNameBuf[1]))))
continue; continue;
tscale.Y = core::fast_atof(textureNameBuf);
bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
if (!core::isdigit(textureNameBuf[0])) if (!(core::isdigit(textureNameBuf[0]) || (textureNameBuf[0] == '-' && core::isdigit(textureNameBuf[1]))))
continue; continue;
tscale.Z = core::fast_atof(textureNameBuf);
} }
else else
if (!strncmp(bufPtr,"-t",2)) if (!strncmp(bufPtr,"-t",2))
...@@ -417,10 +429,10 @@ const c8* COBJMeshFileLoader::readTextures(const c8* bufPtr, const c8* const buf ...@@ -417,10 +429,10 @@ const c8* COBJMeshFileLoader::readTextures(const c8* bufPtr, const c8* const buf
bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
// next parameters are optional, so skip rest of loop if no number is found // next parameters are optional, so skip rest of loop if no number is found
bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
if (!core::isdigit(textureNameBuf[0])) if (!(core::isdigit(textureNameBuf[0]) || (textureNameBuf[0] == '-' && core::isdigit(textureNameBuf[1]))))
continue; continue;
bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd); bufPtr = goAndCopyNextWord(textureNameBuf, bufPtr, WORD_BUFFER_LENGTH, bufEnd);
if (!core::isdigit(textureNameBuf[0])) if (!(core::isdigit(textureNameBuf[0]) || (textureNameBuf[0] == '-' && core::isdigit(textureNameBuf[1]))))
continue; continue;
} }
// get next word // get next word
...@@ -442,7 +454,15 @@ const c8* COBJMeshFileLoader::readTextures(const c8* bufPtr, const c8* const buf ...@@ -442,7 +454,15 @@ const c8* COBJMeshFileLoader::readTextures(const c8* bufPtr, const c8* const buf
if ( texture ) if ( texture )
{ {
if (type==0) if (type==0)
{
currMaterial->Meshbuffer->Material.setTexture(0, texture); currMaterial->Meshbuffer->Material.setTexture(0, texture);
bool needsTextureMatrix = tscale != core::vector3df(1.f) || tpos != core::vector3df(0.f);
if (needsTextureMatrix)
{
currMaterial->Meshbuffer->Material.getTextureMatrix(0).setTextureScale(tscale.X, tscale.Y);
currMaterial->Meshbuffer->Material.getTextureMatrix(0).setTextureTranslate(tpos.X, tpos.Y);
}
}
else if (type==1) else if (type==1)
{ {
if ( texture->getSource() == video::ETS_FROM_FILE) if ( texture->getSource() == video::ETS_FROM_FILE)
......
...@@ -193,6 +193,39 @@ bool COBJMeshWriter::writeMesh(io::IWriteFile* file, scene::IMesh* mesh, s32 fla ...@@ -193,6 +193,39 @@ bool COBJMeshWriter::writeMesh(io::IWriteFile* file, scene::IMesh* mesh, s32 fla
if (mat[i]->getTexture(0)) if (mat[i]->getTexture(0))
{ {
file->write("map_Kd ", 7); file->write("map_Kd ", 7);
f32 tposX, tposY, tscaleX, tscaleY;
const core::matrix4& textureMatrix = mat[i]->getTextureMatrix(0);
textureMatrix.getTextureTranslate(tposX, tposY);
textureMatrix.getTextureScale(tscaleX, tscaleY);
//Write texture translation values
if ( !core::equals(tposX, 0.f) || !core::equals(tposY, 0.f) )
{
file->write("-o ", 3);
core::stringc tx(tposX);
core::stringc ty(tposY);
file->write(tx.c_str(), tx.size());
file->write(" ", 1);
file->write(ty.c_str(), ty.size());
file->write(" ", 1);
}
//Write texture scaling values
if ( !core::equals(tscaleX, 1.f) || !core::equals(tscaleY, 1.f) )
{
file->write("-s ", 3);
core::stringc sx(tscaleX);
core::stringc sy(tscaleY);
file->write(sx.c_str(), sx.size());
file->write(" ", 1);
file->write(sy.c_str(), sy.size());
file->write(" ", 1);
}
io::path tname = FileSystem->getRelativeFilename(mat[i]->getTexture(0)->getName(), io::path tname = FileSystem->getRelativeFilename(mat[i]->getTexture(0)->getName(),
FileSystem->getFileDir(file->getFileName())); FileSystem->getFileDir(file->getFileName()));
// avoid blanks as .obj cannot handle strings with spaces // avoid blanks as .obj cannot handle strings with spaces
......
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