Modify the material parsing

This commit is contained in:
liuliang 2014-08-16 10:41:42 +08:00
parent 1b92409d78
commit 21dcc51c0f
4 changed files with 121 additions and 30 deletions

View File

@ -373,25 +373,25 @@ bool Bundle3D::loadMaterialsJson(MaterialDatas& materialdatas)
for (rapidjson::SizeType i = 0; i < material_array.Size(); i++)
{
//TODO, FIXME
// NMaterialData materialData;
// const rapidjson::Value& material_val = material_array[i];
// materialData.id = material_val[ID].GetString();
//
// if (material_val.HasMember(MATERIALDATA_TEXTURES))
// {
// const rapidjson::Value& testure_array = material_val[MATERIALDATA_TEXTURES];
// for (rapidjson::SizeType j = 0; j < testure_array.Size(); j++)
// {
// NTextureData textureData;
// const rapidjson::Value& texture_val = testure_array[j];
// std::string filename = texture_val[MATERIALDATA_FILENAME].GetString();
// textureData.filename = _modelRelativePath + filename;
// textureData.wrapS = parseGLType(texture_val["wrapModeU"].GetString());
// textureData.wrapT = parseGLType(texture_val["wrapModeV"].GetString());
// materialData.textures.push_back(textureData);
// }
// }
// materialdatas.materials.push_back(materialData);
NMaterialData materialData;
const rapidjson::Value& material_val = material_array[i];
materialData.id = material_val[ID].GetString();
if (material_val.HasMember(MATERIALDATA_TEXTURES))
{
const rapidjson::Value& testure_array = material_val[MATERIALDATA_TEXTURES];
for (rapidjson::SizeType j = 0; j < testure_array.Size(); j++)
{
NTextureData textureData;
const rapidjson::Value& texture_val = testure_array[j];
std::string filename = texture_val[MATERIALDATA_FILENAME].GetString();
textureData.filename = _modelRelativePath + filename;
textureData.type = parseGLTextureType(texture_val["type"].GetString());
textureData.wrapS = parseGLType(texture_val["wrapModeU"].GetString());
textureData.wrapT = parseGLType(texture_val["wrapModeV"].GetString());
materialData.textures.push_back(textureData);
}
}
materialdatas.materials.push_back(materialData);
}
return true;
}
@ -1144,7 +1144,54 @@ GLenum Bundle3D::parseGLType(const std::string& str)
return 0;
}
}
NTextureData::Usage Bundle3D::parseGLTextureType(const std::string& str)
{
if (str == "AMBIENT")
{
return NTextureData::Usage::Ambient;
}
else if(str == "BUMP")
{
return NTextureData::Usage::Bump;
}
else if(str == "DIFFUSE")
{
return NTextureData::Usage::Diffuse;
}
else if(str == "EMISSIVE")
{
return NTextureData::Usage::Emissive;
}
else if(str == "NONE")
{
return NTextureData::Usage::None;
}
else if (str == "NORMAL")
{
return NTextureData::Usage::Normal;
}
else if (str == "REFLECTION")
{
return NTextureData::Usage::Reflection;
}
else if (str == "SHININESS")
{
return NTextureData::Usage::Shininess;
}
else if (str == "SPECULAR")
{
return NTextureData::Usage::Specular;
}
else if (str == "TRANSPARENCY")
{
return NTextureData::Usage::Transparency;
}
else
{
CCASSERT(false, "Wrong GL type");
return NTextureData::Usage::Unknown;
}
}
unsigned int Bundle3D::parseGLProgramAttribute(const std::string& str)
{
if (str == "VERTEX_ATTRIB_POSITION")

View File

@ -159,6 +159,12 @@ protected:
*/
GLenum parseGLType(const std::string& str);
/**
* get define data type
* @param str The type in string
*/
NTextureData::Usage parseGLTextureType(const std::string& str);
/**
* get vertex attribute type
* @param str The type in string

View File

@ -283,28 +283,48 @@ struct MaterialData
/**new material, since 3.3 */
struct NTextureData
{
enum class Usage {
Unknown = 0,
None = 1,
Diffuse = 2,
Emissive = 3,
Ambient = 4,
Specular = 5,
Shininess = 6,
Normal = 7,
Bump = 8,
Transparency = 9,
Reflection = 10
};
std::string id;
std::string filename;
Usage type;
GLenum wrapS;
GLenum wrapT;
} ;
struct NMaterialData
{
std::vector<NTextureData> textures;
std::string id;
std::string fileName;
GLuint wrapS;
GLuint wrapT;
const NTextureData* getTextureData(NTextureData::Usage& type) const
{
for(const auto& it : textures)
{
if (it.type == type)
return &it;
}
return nullptr;
}
};
/** material datas, since 3.3 */
struct MaterialDatas
{
std::vector<NTextureData> materials;
std::vector<NMaterialData> materials;
void resetData()
{
materials.clear();
}
const NTextureData* getMaterialData(const std::string& materialid) const
const NMaterialData* getMaterialData(const std::string& materialid) const
{
for(const auto& it : materials)
{
@ -314,7 +334,6 @@ struct MaterialDatas
return nullptr;
}
};
/**animation data*/
struct Animation3DData
{

View File

@ -180,7 +180,7 @@ bool Sprite3D::loadFromC3x_0_3(const std::string& path)
ret = bundle->loadMaterials(materialdatas);
if (ret)
{
return false;
}
NodeDatas nodeDatas;
bundle->loadNodes(nodeDatas);
@ -345,8 +345,27 @@ void Sprite3D::createNode(NodeData* nodedata, Node* root, const MaterialDatas& m
subMeshState->setSubMesh(getSubMesh(modelNodeData->subMeshId));
auto skin = MeshSkin::create(_skeleton, modelNodeData->bones, modelNodeData->invBindPose);
subMeshState->setSkin(skin);
auto texpath = matrialdatas.getMaterialData(modelNodeData->matrialId)->filename;
subMeshState->setTexture(texpath);
const NMaterialData* materialData=matrialdatas.getMaterialData(modelNodeData->matrialId);
if(materialData)
{
NTextureData::Usage type = NTextureData::Usage::Diffuse;
const NTextureData* textureData = materialData->getTextureData(type);
if(textureData)
{
auto tex = Director::getInstance()->getTextureCache()->addImage(textureData->filename);
if(tex)
{
Texture2D::TexParams texParams;
texParams.minFilter=GL_LINEAR;
texParams.magFilter=GL_LINEAR;
texParams.wrapS=textureData->wrapS;
texParams.wrapT=textureData->wrapT;
tex->setTexParameters(texParams);
subMeshState->setTexture(tex);
}
}
}
//set wrap s, t
}
}