mirror of
https://github.com/ApfelTeeSaft/vb01.git
synced 2026-08-27 03:43:42 +00:00
beginning of a model reader
This commit is contained in:
@@ -9,7 +9,6 @@
|
||||
#include"animation.h"
|
||||
|
||||
#include<vector>
|
||||
#include<iostream>
|
||||
#include<Importer.hpp>
|
||||
#include<scene.h>
|
||||
#include<postprocess.h>
|
||||
@@ -146,211 +145,6 @@ namespace vb01{
|
||||
continue;
|
||||
}
|
||||
|
||||
if(stage==ARMATURE){
|
||||
enum ArmatureStage{BONES,ANIMATIONS};
|
||||
|
||||
ArmatureStage armatureStage;
|
||||
|
||||
if(preColon=="bones"){
|
||||
numBones=atoi(postColon.c_str());
|
||||
armatureStage=BONES;
|
||||
continue;
|
||||
}
|
||||
else if(preColon=="animations"){
|
||||
armatureStage=ANIMATIONS;
|
||||
continue;
|
||||
}
|
||||
|
||||
switch(armatureStage){
|
||||
case BONES:
|
||||
{
|
||||
int numData=11;
|
||||
string data[numData];
|
||||
getLineData(postColon,data,numData);
|
||||
|
||||
float length = atof(data[1].c_str());
|
||||
Vector3 head=Vector3(atof(data[2].c_str()),atof(data[3].c_str()),atof(data[4].c_str()));
|
||||
Vector3 xAxis=Vector3(atof(data[5].c_str()),atof(data[6].c_str()),atof(data[7].c_str()));
|
||||
Vector3 yAxis=Vector3(atof(data[8].c_str()),atof(data[9].c_str()),atof(data[10].c_str()));
|
||||
Vector3 zAxis=xAxis.cross(yAxis);
|
||||
Vector3 pos = head;
|
||||
|
||||
string parName=string(data[0].c_str());
|
||||
Node *parent=skeleton->getBone(parName);
|
||||
if(!parent){
|
||||
parent=this;
|
||||
swap(xAxis.y,xAxis.z);
|
||||
xAxis.z=-xAxis.z;
|
||||
swap(yAxis.y,yAxis.z);
|
||||
yAxis.z=-yAxis.z;
|
||||
swap(zAxis.y,zAxis.z);
|
||||
zAxis.z=-zAxis.z;
|
||||
}
|
||||
else
|
||||
pos = pos + Vector3(0,((Bone*)parent)->getLength(),0);
|
||||
|
||||
Bone *bone=new Bone(preColon,length,pos);
|
||||
skeleton->addBone(bone,(Bone*)parent);
|
||||
bone->lookAt(zAxis,yAxis,parent);
|
||||
|
||||
break;
|
||||
}
|
||||
case ANIMATIONS:
|
||||
{
|
||||
if(preColon=="animationName"){
|
||||
animName=postColon;
|
||||
skeleton->addAnimation(new Animation(animName));
|
||||
continue;
|
||||
}
|
||||
Bone *b=skeleton->getBone(preColon);
|
||||
if(b){
|
||||
animBone=b;
|
||||
Animation *anim=skeleton->getAnimation(animName);
|
||||
Animation::KeyframeGroup *group=anim->getKeyframeGroup(animBone);
|
||||
if(!group){
|
||||
Animation::KeyframeGroup kg;
|
||||
kg.bone=animBone;
|
||||
anim->addKeyframeGroup(kg);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
else{
|
||||
Animation::KeyframeGroup::Keyframe::Type type;
|
||||
Animation::KeyframeGroup::Keyframe::Interpolation interp;
|
||||
if(preColon=="pos_x")
|
||||
type=Animation::KeyframeGroup::Keyframe::Type::POS_X;
|
||||
else if(preColon=="pos_y")
|
||||
type=Animation::KeyframeGroup::Keyframe::Type::POS_Y;
|
||||
else if(preColon=="pos_z")
|
||||
type=Animation::KeyframeGroup::Keyframe::Type::POS_Z;
|
||||
else if(preColon=="rot_w")
|
||||
type=Animation::KeyframeGroup::Keyframe::Type::ROT_W;
|
||||
else if(preColon=="rot_x")
|
||||
type=Animation::KeyframeGroup::Keyframe::Type::ROT_X;
|
||||
else if(preColon=="rot_y")
|
||||
type=Animation::KeyframeGroup::Keyframe::Type::ROT_Y;
|
||||
else if(preColon=="rot_z")
|
||||
type=Animation::KeyframeGroup::Keyframe::Type::ROT_Z;
|
||||
else if(l==""){
|
||||
skeletons.push_back(skeleton);
|
||||
break;
|
||||
}
|
||||
else{
|
||||
int numData=3;
|
||||
string data[numData];
|
||||
|
||||
getLineData(l,data,numData);
|
||||
|
||||
float value=atof(data[0].c_str()),frame=atoi(data[1].c_str());
|
||||
interp=(Animation::KeyframeGroup::Keyframe::Interpolation)atoi(data[2].c_str());
|
||||
|
||||
Animation::KeyframeGroup::Keyframe keyframe;
|
||||
keyframe.type=type;
|
||||
keyframe.interpolation=interp;
|
||||
keyframe.value=value;
|
||||
keyframe.frame=frame;
|
||||
skeleton->getAnimation(animName)->getKeyframeGroup(animBone)->keyframes.push_back(keyframe);
|
||||
|
||||
}
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else{
|
||||
enum MeshStage{VERTICES,FACES,GROUPS,SHAPE_KEYS};
|
||||
|
||||
MeshStage meshStage;
|
||||
|
||||
if(preColon=="vertices"){
|
||||
const int numData=4;
|
||||
string data[numData];
|
||||
getLineData(postColon,data,numData);
|
||||
numFaces=atoi(data[1].c_str());
|
||||
numGroups=atoi(data[2].c_str());
|
||||
vertices=new Mesh::Vertex[numFaces*numVertsPerFace];
|
||||
indices=new u32[numFaces*numVertsPerFace];
|
||||
meshStage=VERTICES;
|
||||
continue;
|
||||
}
|
||||
else if(preColon=="faces"){
|
||||
meshStage=FACES;
|
||||
continue;
|
||||
}
|
||||
else if(preColon=="groups"){
|
||||
groups=new string[numGroups];
|
||||
meshStage=GROUPS;
|
||||
continue;
|
||||
}
|
||||
else if(preColon=="shapeKeys"){
|
||||
meshStage=SHAPE_KEYS;
|
||||
continue;
|
||||
}
|
||||
switch(meshStage){
|
||||
{
|
||||
case VERTICES:
|
||||
int numData = 6 + numBones;
|
||||
string data[numData];
|
||||
getLineData(l,data,numData);
|
||||
Vector3 vPos=Vector3(atof(data[0].c_str()),atof(data[2].c_str()),-atof(data[1].c_str()));
|
||||
Vector3 vNorm=Vector3(atof(data[3].c_str()),atof(data[5].c_str()),-atof(data[4].c_str()));
|
||||
vertPos.push_back(vPos);
|
||||
vertNorm.push_back(vNorm);
|
||||
for(int i=0;i<numBones;i++)
|
||||
vertWeights.push_back(atof(data[6 + i].c_str()));
|
||||
break;
|
||||
}
|
||||
{
|
||||
case FACES:
|
||||
int numData=9;
|
||||
string data[numData];
|
||||
getLineData(l,data,numData);
|
||||
int index=atoi(data[0].c_str());
|
||||
Vector2 uv=Vector2(atof(data[1].c_str()),atof(data[2].c_str()));
|
||||
Vector3 tan=Vector3(atof(data[3].c_str()),atof(data[5].c_str()),-atof(data[4].c_str()));
|
||||
Vector3 biTan=Vector3(atof(data[6].c_str()),atof(data[8].c_str()),-atof(data[7].c_str()));
|
||||
|
||||
Mesh::Vertex vert;
|
||||
vert.pos=vertPos[index];
|
||||
vert.norm=vertNorm[index];
|
||||
vert.tan=tan;
|
||||
vert.biTan=biTan;
|
||||
vert.uv=uv;
|
||||
for(int i = 0, boneIndex = 0; i < numBones; i++){
|
||||
if(vertWeights[index * numBones + i] > 0){
|
||||
vert.boneIndices[boneIndex] = i;
|
||||
vert.weights[boneIndex] = vertWeights[index * numBones + i];
|
||||
boneIndex++;
|
||||
}
|
||||
}
|
||||
indices[vertId]=vertId;
|
||||
vertices[vertId]=vert;
|
||||
|
||||
vertId++;
|
||||
break;
|
||||
}
|
||||
{
|
||||
case GROUPS:
|
||||
if(l==""){
|
||||
meshes.push_back(new Mesh(vertices,indices,numFaces,groups,numGroups,shapeKeys,numShapeKeys,name));
|
||||
break;
|
||||
}
|
||||
else{
|
||||
groups[groupId]=l;
|
||||
groupId++;
|
||||
}
|
||||
break;
|
||||
}
|
||||
{
|
||||
case SHAPE_KEYS:
|
||||
if(l==""){
|
||||
meshes.push_back(new Mesh(vertices,indices,numFaces,groups,numGroups,shapeKeys,numShapeKeys,name));
|
||||
break;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
for(Mesh *mesh : meshes){
|
||||
Node *meshNode=new Node(localPos,localRot,localScale);
|
||||
@@ -421,26 +215,6 @@ namespace vb01{
|
||||
Node::update();
|
||||
}
|
||||
|
||||
void Model::getLineData(string &line,string data[],int numData,int offset){
|
||||
int offsetComma=0,c1=0;
|
||||
for(int i=0;i<line.length()&&offsetComma<offset;i++)
|
||||
if(line.c_str()[i]==' '){
|
||||
c1=i+1;
|
||||
offsetComma++;
|
||||
}
|
||||
int c2=c1;
|
||||
for(int i=0;i<numData;i++){
|
||||
for(int j=c1;j<line.length();j++)
|
||||
if(line.c_str()[j]==' '){
|
||||
c2=j;
|
||||
break;
|
||||
}
|
||||
data[i]=line.substr(c1,c2-c1);
|
||||
c2++;
|
||||
c1=c2;
|
||||
}
|
||||
}
|
||||
|
||||
void Model::setMaterial(Material *mat){
|
||||
vector<Node*> descendants;
|
||||
getDescendants(this,descendants);
|
||||
|
||||
Reference in New Issue
Block a user