Corrected bone names in krobject file format

--HG--
extra : convert_revision : svn%3A7752d6cf-9f14-4ad2-affc-04f1e67b81a5/trunk%40186
This commit is contained in:
kearwood
2012-12-12 23:21:15 +00:00
parent fe7d4f8238
commit ec6bd06bd7
4 changed files with 44 additions and 7 deletions

View File

@@ -66,11 +66,31 @@ tinyxml2::XMLElement *KRInstance::saveXML( tinyxml2::XMLNode *parent)
void KRInstance::loadModel() {
if(m_models.size() == 0) {
m_models = m_pContext->getModelManager()->getModel(m_model_name.c_str()); // The model manager returns the LOD levels in sorted order, with the highest detail first
if(m_models.size() > 0) {
std::vector<KRModel *> models = m_pContext->getModelManager()->getModel(m_model_name.c_str()); // The model manager returns the LOD levels in sorted order, with the highest detail first
std::map<KRModel *, std::vector<KRBone *> > bones;
if(models.size() > 0) {
bool all_bones_found = true;
for(std::vector<KRModel *>::iterator model_itr = models.begin(); model_itr != models.end(); model_itr++) {
KRModel *model = *model_itr;
std::vector<KRBone *> model_bones;
int bone_count = model->getBoneCount();
for(int bone_index=0; bone_index < bone_count; bone_index++) {
KRBone *matching_bone = dynamic_cast<KRBone *>(getScene().getRootNode()->findChild(model->getBoneName(bone_index)));
if(matching_bone) {
model_bones.push_back(matching_bone);
} else {
all_bones_found = false; // Reject when there are any missing bones or multiple matches
}
}
bones[model] = model_bones;
}
if(all_bones_found) {
m_models = models;
m_bones = bones;
getScene().notify_sceneGraphModify(this);
}
}
}
}
#if TARGET_OS_IPHONE
@@ -117,7 +137,7 @@ void KRInstance::render(KRCamera *pCamera, std::vector<KRLight *> &lights, const
matModel = KRQuaternion(KRVector3::Forward(), KRVector3::Normalize(camera_pos - model_center)).rotationMatrix() * matModel;
}
pModel->render(pCamera, lights, viewport, matModel, m_pLightMap, renderPass);
pModel->render(pCamera, lights, viewport, matModel, m_pLightMap, renderPass, m_bones[pModel]);
}
}
}

View File

@@ -46,6 +46,7 @@
#import "KRContext.h"
#import "KRModel.h"
#import "KRTexture.h"
#import "KRBone.h"
class KRInstance : public KRNode {
@@ -68,6 +69,7 @@ public:
private:
std::vector<KRModel *> m_models;
std::map<KRModel *, std::vector<KRBone *> > m_bones; // Outer std::map connects model to set of bones
KRTexture *m_pLightMap;
std::string m_lightMap;
std::string m_model_name;

View File

@@ -112,7 +112,7 @@ void KRModel::loadPack(KRDataBlock *data) {
#if TARGET_OS_IPHONE
void KRModel::render(KRCamera *pCamera, std::vector<KRLight *> &lights, const KRViewport &viewport, const KRMat4 &matModel, KRTexture *pLightMap, KRNode::RenderPass renderPass) {
void KRModel::render(KRCamera *pCamera, std::vector<KRLight *> &lights, const KRViewport &viewport, const KRMat4 &matModel, KRTexture *pLightMap, KRNode::RenderPass renderPass, const std::vector<KRBone *> &bones) {
//fprintf(stderr, "Rendering model: %s\n", m_name.c_str());
if(renderPass != KRNode::RENDER_PASS_ADDITIVE_PARTICLES && renderPass != KRNode::RENDER_PASS_VOLUMETRIC_EFFECTS_ADDITIVE) {
@@ -480,7 +480,7 @@ KRModel::pack_header *KRModel::getHeader() const
KRModel::pack_bone *KRModel::getBone(int index)
{
pack_header *header = getHeader();
return (pack_bone *)((unsigned char *)m_pData->getStart()) + sizeof(pack_header) + sizeof(pack_material) * header->submesh_count;
return (pack_bone *)((unsigned char *)m_pData->getStart() + sizeof(pack_header) + sizeof(pack_material) * header->submesh_count + sizeof(pack_bone) * index);
}
unsigned char *KRModel::getVertexData() const {
@@ -669,3 +669,14 @@ size_t KRModel::AttributeOffset(__int32_t vertex_attrib, __int32_t vertex_attrib
}
return VertexSizeForAttributes(mask);
}
int KRModel::getBoneCount()
{
pack_header *header = getHeader();
return header->bone_count;
}
char *KRModel::getBoneName(int bone_index)
{
return getBone(bone_index)->szName;
}

View File

@@ -34,6 +34,7 @@
#import <string>
#import "KRVector2.h"
#import "KRContext.h"
#import "KRBone.h"
#import "KREngine-common.h"
@@ -71,7 +72,7 @@ public:
#if TARGET_OS_IPHONE
void render(KRCamera *pCamera, std::vector<KRLight *> &lights, const KRViewport &viewport, const KRMat4 &matModel, KRTexture *pLightMap, KRNode::RenderPass renderPass);
void render(KRCamera *pCamera, std::vector<KRLight *> &lights, const KRViewport &viewport, const KRMat4 &matModel, KRTexture *pLightMap, KRNode::RenderPass renderPass, const std::vector<KRBone *> &bones);
#endif
@@ -172,6 +173,9 @@ public:
static size_t VertexSizeForAttributes(__int32_t vertex_attrib_flags);
static size_t AttributeOffset(__int32_t vertex_attrib, __int32_t vertex_attrib_flags);
int getBoneCount();
char *getBoneName(int bone_index);
private:
int m_lodCoverage; // This LOD level is activated when the bounding box of the model will cover less than this percent of the screen (100 = highest detail model)
vector<KRMaterial *> m_materials;