#include "obj-loader.h"
// EXTERNAL INCLUDES
+#include <dali/integration-api/debug.h>
#include <string>
#include <sstream>
#include <string.h>
namespace Internal
{
+namespace
+{
+ const int MAX_POINT_INDICES = 4;
+}
using namespace Dali;
ObjLoader::ObjLoader()
mSceneLoaded = false;
mMaterialLoaded = false;
mHasTexturePoints = false;
- mHasNormalMap = false;
mHasDiffuseMap = false;
+ mHasNormalMap = false;
mHasSpecularMap = false;
mSceneAABB.Init();
}
Dali::Vector<Vector3>& normal,
Dali::Vector<Vector3>& tangent)
{
- normal.Clear();
- normal.Resize(vertex.Size());
-
Dali::Vector<Vector3> tangents;
tangents.Resize( vertex.Size() );
//we need to recalculate the normals too, because we need just one normal,tangent, bitangent per vertex
//In the case of a textureless object, we don't need tangents for our shader and so we skip this step
//TODO: Use a better function to calculate tangents
-
if( mTangents.Size() == 0 && mHasTexturePoints )
{
- mTangents.Resize( mNormals.Size() );
- mBiTangents.Resize( mNormals.Size() );
+ mNormals.Clear();
+
+ mNormals.Resize( mPoints.Size() );
+ mTangents.Resize( mPoints.Size() );
+ mBiTangents.Resize( mPoints.Size() );
+
CalculateTangentArray( mPoints, mTextures, mTriangles, mNormals, mTangents );
+
for ( unsigned int ui = 0 ; ui < mNormals.Size() ; ++ui )
{
mBiTangents[ui] = mNormals[ui].Cross(mTangents[ui]);
}
}
-bool ObjLoader::Load( char* objBuffer, std::streampos fileSize, std::string& materialFile )
+bool ObjLoader::LoadObject( char* objBuffer, std::streampos fileSize )
{
Vector3 point;
Vector2 texture;
- std::string vet[4], name;
- int ptIdx[4];
- int nrmIdx[4];
- int texIdx[4];
+ std::string vet[MAX_POINT_INDICES], name;
+ int ptIdx[MAX_POINT_INDICES];
+ int nrmIdx[MAX_POINT_INDICES];
+ int texIdx[MAX_POINT_INDICES];
TriIndex triangle,triangle2;
int pntAcum = 0, texAcum = 0, nrmAcum = 0;
bool iniObj = false;
}
int numIndices = 0;
- while( isline >> vet[numIndices] )
+ while( isline >> vet[numIndices] && numIndices < MAX_POINT_INDICES )
{
numIndices++;
}
{
isline >> name;
}
- else
- {
- }
}
if ( iniObj )
}
return false;
-
}
-void ObjLoader::LoadMaterial( char* objBuffer, std::streampos fileSize, std::string& texture0Url,
- std::string& texture1Url, std::string& texture2Url )
+void ObjLoader::LoadMaterial( char* objBuffer, std::streampos fileSize, std::string& diffuseTextureUrl,
+ std::string& normalTextureUrl, std::string& glossTextureUrl )
{
float fR,fG,fB;
{
isline >> info;
}
+ else if ( tag == "Ka" ) //ambient color
+ {
+ isline >> fR >> fG >> fB;
+ }
else if ( tag == "Kd" ) //diffuse color
{
isline >> fR >> fG >> fB;
}
- else if ( tag == "Kd" ) //Ambient color
+ else if ( tag == "Ks" ) //specular color
{
isline >> fR >> fG >> fB;
}
else if ( tag == "map_Kd" )
{
isline >> info;
- texture0Url = info;
+ diffuseTextureUrl = info;
mHasDiffuseMap = true;
}
else if ( tag == "bump" )
{
isline >> info;
- texture1Url = info;
+ normalTextureUrl = info;
mHasNormalMap = true;
}
else if ( tag == "map_Ks" )
{
isline >> info;
- texture2Url = info;
+ glossTextureUrl = info;
mHasSpecularMap = true;
}
}
mMaterialLoaded = true;
}
-Geometry ObjLoader::CreateGeometry( Toolkit::Model3dView::IlluminationType illuminationType )
+Geometry ObjLoader::CreateGeometry( int objectProperties )
{
Geometry surface = Geometry::New();
surface.AddVertexBuffer( surfaceVertices );
//Some need texture coordinates
- if( ( (illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP ) ||
- (illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_TEXTURE ) ) && mHasTexturePoints && mHasDiffuseMap )
+ if( ( objectProperties & TEXTURE_COORDINATES ) && mHasTexturePoints && mHasDiffuseMap )
{
Property::Map textureFormat;
textureFormat["aTexCoord"] = Property::VECTOR2;
}
//Some need tangent and bitangent
- if( illuminationType == Toolkit::Model3dView::DIFFUSE_WITH_NORMAL_MAP && mHasTexturePoints )
+ if( ( objectProperties & TANGENTS ) && ( objectProperties & BINOMIALS ) && mHasTexturePoints )
{
Property::Map vertexExtFormat;
vertexExtFormat["aTangent"] = Property::VECTOR3;
surface.AddVertexBuffer( extraVertices );
}
+ //If indices are required, we set them.
if ( indices.Size() )
{
surface.SetIndexBuffer ( &indices[0], indices.Size() );
}
- vertices.Clear();
- verticesExt.Clear();
- indices.Clear();
-
return surface;
}