void SE_Camera::rotateLocal(float angle, SE_AXIS_TYPE axis) { SE_Quat q; switch(axis) { case SE_AXIS_X: q.set(angle, SE_Vector3f(1, 0, 0)); break; case SE_AXIS_Y: q.set(angle, SE_Vector3f(0, 1, 0)); break; case SE_AXIS_Z: q.set(angle, SE_Vector3f(0, 0, 1)); break; } return rotateLocal(q); }
void SE_BipedController::createBoneToWorldMatrix(int frameindex) { //per frame refresh; mAfterTransformMatrixToWorld.clear(); for(int i = 0; i < oneBipAnimation.size(); ++i) { SE_Biped * bip = oneBipAnimation[i]; SE_Matrix4f transform; if(bip->animationInfo.size() == 0) { transform.identity(); } else { SE_Quat worldR = bip->animationInfo[frameindex]->rotateQ; SE_Vector3f worldT = bip->animationInfo[frameindex]->translate; SE_Vector3f worldS = bip->animationInfo[frameindex]->scale; //not use scale transform.set(worldR.toMatrix3f(),SE_Vector3f(1,1,1),worldT);//myTransform relate parent } SE_Matrix4f parentBoneToWorld; parentBoneToWorld.identity(); if(bip->parent) { parentBoneToWorld = *(bip->parent->boneToWorldPerFrame[frameindex]); } else { //this bip is ROOT,the transform is world relative; } SE_Matrix4f tranToWorld = parentBoneToWorld.mul(transform); mAfterTransformMatrixToWorld.push_back(tranToWorld); } }
void SE_Camera::rotateLocal(const SE_Quat& rotate) { SE_Vector3f localxAxis(1, 0, 0); SE_Vector3f localyAxis(0, 1, 0); SE_Vector3f localzAxis(0, 0, 1); localxAxis = rotate.map(localxAxis); localyAxis = rotate.map(localyAxis); //localzAxis = rotate.map(zAxis); SE_Matrix4f vtom = getViewToWorldMatrix(); SE_Vector4f worldxAxis = vtom.map(SE_Vector4f(localxAxis, 0)); SE_Vector4f worldyAxis = vtom.map(SE_Vector4f(localyAxis, 0)); //SE_Vector4f worldzAxis = vtom.map(SE_Vector4f(localzAxis, 0)); SE_Vector4f worldzAxis(worldxAxis.xyz().cross(worldyAxis.xyz()), 0); mAxisX = worldxAxis.normalize().xyz(); mAxisY = worldyAxis.normalize().xyz(); mAxisZ = worldzAxis.normalize().xyz(); mChanged = true; }
void SE_GeometryData::transform(SE_GeometryData* src, const SE_Vector3f& scale, const SE_Quat& rotate, const SE_Vector3f& translate, SE_GeometryData* dst) { SE_Vector3f* vertex = NULL; SE_Vector3i* faces = NULL; SE_Vector3f*facenormal = NULL; SE_Vector3f* facevertexnormal = NULL; int vertexNum = 0; int faceNum = 0; int normalNum = 0; int facevertexnormalNum = 0; if(src->vertexArray) { vertex = new SE_Vector3f[src->vertexNum]; for(int i = 0 ; i < src->vertexNum ; i++) { SE_Vector3f v = scale.mul(src->vertexArray[i]); v = rotate.map(v); v = v + translate; vertex[i] = v; } vertexNum = src->vertexNum; } /* if(src->faceArray) { faces = new SE_Vector3i[src->faceNum]; for(int i = 0 ; i < src->faceNum; i++) { faces[i] = src->faceArray[i]; } faceNum = src->faceNum; } */ if(src->faceNormalArray) { SE_Matrix3f t; t.setScale(scale.x, scale.y, scale.z); SE_Matrix3f m; m = rotate.toMatrix3f(); m = m.mul(t); if(m.hasInverse()) { SE_Matrix3f inverse = m.inverse(); m = m.transpose(); facenormal = new SE_Vector3f[src->faceNormalNum]; for(int i = 0 ; i < src->faceNormalNum ; i++) { facenormal[i] = m.map(src->faceNormalArray[i]); } normalNum = src->faceNormalNum; } } if(src->faceVertexNormalArray) { SE_Matrix3f t; t.setScale(scale.x, scale.y, scale.z); SE_Matrix3f m; m = rotate.toMatrix3f(); m = m.mul(t); if(m.hasInverse()) { SE_Matrix3f inverse = m.inverse(); m = m.transpose(); facevertexnormal = new SE_Vector3f[src->faceVertexNormalNum]; for(int i = 0 ; i < src->faceVertexNormalNum ; i++) { facevertexnormal[i] = m.map(src->faceVertexNormalArray[i]); } facevertexnormalNum = src->faceVertexNormalNum; } } dst->setVertexArray(vertex, vertexNum); dst->setFaceArray(src->faceArray, src->faceNum, false); dst->setFaceNormalArray(facenormal, normalNum); dst->setFaceVertexNormalArray(facevertexnormal,facevertexnormalNum); }
void SE_BipedController::fillFrame() { int maxFrame = findMaxFrameIndex(); std::vector<SE_BipedKeyFrame*> fullFrame; //fullFrame.resize(maxFrame,NULL); for(int i = 0; i < oneBipAnimation.size(); ++i) { //get bip one by one SE_Biped *bip = oneBipAnimation[i]; if(bip->animationInfo.size() == 0) { continue;//this bip has no keyframe } //generate 0 frame, bind_pos relative parent SE_Matrix4f bindposT; if(bip->parent) { bindposT = (bip->parent->bind_pose.inverse()).mul(bip->bind_pose); } else { //this bip is ROOT bindposT = bip->bind_pose; } SE_Quat rotate = bindposT.toMatrix3f().toQuat();//no rotate rotate = rotate.inverse(); SE_Vector3f translate = bindposT.getTranslate(); SE_Vector3f scale(1,1,1); //scale no use SE_BipedKeyFrame *zeroFrame = new SE_BipedKeyFrame(); zeroFrame->frameIndex = 0; zeroFrame->rotateQ = rotate; zeroFrame->scale = scale; zeroFrame->translate = translate; //push first (0) frame to target fullFrame.push_back(zeroFrame); std::vector<SE_BipedKeyFrame*>::iterator it_s = bip->animationInfo.begin(); std::vector<SE_BipedKeyFrame*>::iterator it_t = fullFrame.end(); -- it_t; SE_BipedKeyFrame* node_s = NULL; SE_BipedKeyFrame* node_t = NULL; while(it_s != bip->animationInfo.end()) { node_s = *it_s; node_t = *it_t; if(needInterpolation(node_t->frameIndex,node_s->frameIndex)) { //interpolation interpolation(it_s,it_t,fullFrame); } else if(node_s->frameIndex == node_t->frameIndex)//current source frame index == target frame index { //goto next frame ++it_s; } else //source frame index - target frame index == 1 { //push source frame to target frame vector SE_BipedKeyFrame *nextFrame = new SE_BipedKeyFrame(); //current source node is just next frame nextFrame->frameIndex = node_s->frameIndex; nextFrame->rotateQ = node_s->rotateQ; nextFrame->translate = node_s->translate; nextFrame->scale = node_s->scale; fullFrame.push_back(nextFrame); it_t = fullFrame.end(); --it_t; } } //total frame little than maxFrame? //1.yes fill last frame to vector int lastframe = bip->animationInfo.size() - 1; int lastframeIndex = bip->animationInfo[lastframe]->frameIndex; if(lastframeIndex < maxFrame) { SE_BipedKeyFrame * lastframenode = bip->animationInfo[lastframe]; for(int needfill = 0; needfill < maxFrame - lastframeIndex; ++needfill) { SE_BipedKeyFrame *fillFrame = new SE_BipedKeyFrame(); //next (maxFrame - lastframeIndex) frames are same fillFrame->frameIndex = lastframenode->frameIndex + needfill + 1; fillFrame->rotateQ = lastframenode->rotateQ; fillFrame->translate = lastframenode->translate; fillFrame->scale = lastframenode->scale; fullFrame.push_back(fillFrame); } } //2.no,finish this biped,copy target to source,clear target for(it_s = bip->animationInfo.begin(); it_s != bip->animationInfo.end(); ++it_s) { if(*it_s != NULL) { delete *it_s; } } bip->animationInfo.clear(); bip->animationInfo.resize(fullFrame.size()); //copy target to source std::copy(fullFrame.begin(),fullFrame.end(),bip->animationInfo.begin()); //clear fullFrame vector fullFrame.clear(); } }
SE_Vector3f SE_BipedController::convert(int vertexIndex,int frameindex,const char * objname, const SE_Vector3f& v) { SE_Vector4f input; input.set(v,1); SE_Vector4f result(0,0,0,0); SE_SkeletonUnit *su = findSU(objname); //bipedIndex.size is number that how many bips effact this vertext. int bipNumPerVertex = su->objVertexBlendInfo[vertexIndex]->bipedIndex.size(); //how many bips will take effact to one vertex for(int i = 0; i < bipNumPerVertex; ++i) { int bipIndex = su->objVertexBlendInfo[vertexIndex]->bipedIndex[i];//bipIndex is start from 1, not 0. int bipindexfromcache = su->bipCache[bipIndex-1]->bipIndexOnBipAnimation; SE_Biped *bip = oneBipAnimation[bipindexfromcache];// find bip from all bips if(bip->animationInfo.size() == 0) { continue; } #ifdef _FORDEBUG SE_Matrix4f bindpos = bip->bind_pose; SE_Matrix4f inversOfbp = bindpos.inverse(); SE_Quat worldR = bip->animationInfo[frameindex]->rotateQ; SE_Vector3f worldT = bip->animationInfo[frameindex]->translate; SE_Vector3f worldS = bip->animationInfo[frameindex]->scale; SE_Matrix4f transform; transform.identity(); //not use scale transform.set(worldR.toMatrix3f(),SE_Vector3f(1,1,1),worldT);//myTransform relate parent SE_Matrix4f parentBoneToWorld; parentBoneToWorld.identity(); if(bip->parent) { parentBoneToWorld = *(bip->parent->boneToWorldPerFrame[frameindex]); } else { //this bip is ROOT,the transform is world relative; } SE_Matrix4f m = parentBoneToWorld.mul(transform).mul(inversOfbp); SE_Matrix4f m = mAfterTransformMatrixToWorld[bipindexfromcache].mul(mBindposeMatrixInverse[bipindexfromcache]); #else SE_Matrix4f m = AllFrameFinalTransformMatrix[frameindex][bipindexfromcache]; #endif result = result + m.map(input) * su->objVertexBlendInfo[vertexIndex]->weight[i]; } return result.xyz(); }
void SE_BufferOutput::writeQuat(const SE_Quat& q) { writeVector4f(q.toVector4f()); }
void ASE_Loader::Write(SE_BufferOutput& output, SE_BufferOutput& outScene, const char* shaderPath) { int materialNum = mSceneObject->mMats.size(); int numWhichHasSubmaterial = 0; int materialRealNum = materialNum; int i; for(i = 0 ; i < materialNum ; i++) { ASE_Material* srcm = &mSceneObject->mMats[i]; materialRealNum += srcm->numsubmaterials; if(srcm->numsubmaterials > 0) { numWhichHasSubmaterial++; } } std::vector<_MaterialData> materialVector(materialRealNum); std::vector<int> indexWhichHasSubmaterial(numWhichHasSubmaterial); int l = 0; int mi = 0; for(i = 0 ; i < materialNum ; i++) { ASE_Material* srcm = &mSceneObject->mMats[i]; _MaterialData md; md.subMaterialNum = srcm->numsubmaterials; md.md = srcm->materialData; materialVector[mi++] = md; if(srcm->numsubmaterials > 0) { indexWhichHasSubmaterial[l++] = i; } } std::vector<int>::iterator it; for(it = indexWhichHasSubmaterial.begin() ; it != indexWhichHasSubmaterial.end() ; it++) { int index = *it; ASE_Material* m = &mSceneObject->mMats[index]; for(int j = 0 ; j < m->numsubmaterials ; j++) { _MaterialData md; md.subMaterialNum = 0; md.md = m->submaterials[j]; materialVector[mi++] = md; } } std::vector<_MaterialData>::iterator itMaterial; output.writeShort(SE_MATERIALDATA_ID); output.writeInt(materialVector.size()); int mmm = materialVector.size(); //for(itMaterial = materialVector.begin() ; itMaterial != materialVector.end() ; itMaterial++) for(i = 0 ; i < materialVector.size() ; i++) { SE_MaterialDataID mid = SE_Application::getInstance()->createCommonID(); mid.print(); SE_Util::sleep(SLEEP_COUNT); materialVector[i].mid = mid; mid.write(output); output.writeVector3f(materialVector[i].md.ambient); output.writeVector3f(materialVector[i].md.diffuse); output.writeVector3f(materialVector[i].md.specular); output.writeVector3f(SE_Vector3f(0, 0, 0)); } /////////////////////////////write texture data /////////////// output.writeShort(SE_IMAGEDATA_ID); int imageDataNum = 0; for(itMaterial = materialVector.begin() ; itMaterial != materialVector.end() ; itMaterial++) { std::string texStr(itMaterial->md.texName); if(texStr != "") { imageDataNum++; } } output.writeInt(imageDataNum); for(itMaterial = materialVector.begin() ; itMaterial != materialVector.end() ; itMaterial++) { std::string texStr(itMaterial->md.texName); if(texStr != "") { size_t pos = texStr.find('.'); std::string name = texStr.substr(0, pos); name = name + ".raw"; SE_ImageDataID tid = texStr.c_str(); itMaterial->tid = tid; tid.write(output); output.writeInt(0); // image data type output.writeString(name.c_str()); } } /////////////////////////////write geom data ///////////////////////////////////////////// output.writeShort(SE_GEOMETRYDATA_ID); int geomDataNum = mSceneObject->mGeomObjects.size(); output.writeInt(geomDataNum); std::vector<_GeomTexCoordData> geomTexCoordData(geomDataNum); std::list<ASE_GeometryObject*>::iterator itGeomObj; int n = 0; SE_Matrix4f modelToWorldM, worldToModelM; SE_Matrix3f rotateM; SE_Quat rotateQ; SE_Vector3f rotateAxis, scale, translate; for(itGeomObj = mSceneObject->mGeomObjects.begin(); itGeomObj != mSceneObject->mGeomObjects.end(); itGeomObj++) { ASE_GeometryObject* go = *itGeomObj; ASE_Mesh* mesh = go->mesh; SE_GeometryDataID gid = SE_Application::getInstance()->createCommonID(); SE_Util::sleep(SLEEP_COUNT); rotateAxis.x = go->rotateAxis[0]; rotateAxis.y = go->rotateAxis[1]; rotateAxis.z = go->rotateAxis[2]; scale.x = go->scale[0]; scale.y = go->scale[1]; scale.z = go->scale[2]; translate.x = go->translate[0]; translate.y = go->translate[1]; translate.z = go->translate[2]; rotateQ.set(go->rotateAngle, rotateAxis); rotateM = rotateQ.toMatrix3f();//.setRotateFromAxis(go->rotateAngle, rotateAxis); modelToWorldM.set(rotateM, scale, translate); worldToModelM = modelToWorldM.inverse(); geomTexCoordData[n++].geomID = gid; gid.write(output); output.writeInt(mesh->numVertexes); output.writeInt(mesh->numFaces); output.writeInt(0); int i; for(i = 0 ; i < mesh->numVertexes ; i++) { SE_Vector4f p(mesh->vertexes[i].x, mesh->vertexes[i].y, mesh->vertexes[i].z, 1.0f); p = worldToModelM.map(p); output.writeFloat(p.x); output.writeFloat(p.y); output.writeFloat(p.z); } for(i = 0 ; i < mesh->numFaces ; i++) { output.writeInt(mesh->faces[i].vi[0]); output.writeInt(mesh->faces[i].vi[1]); output.writeInt(mesh->faces[i].vi[2]); } } ////////////////////////write texture coordinate/////////////////////////////////////////////// output.writeShort(SE_TEXCOORDDATA_ID); output.writeInt(geomDataNum); n = 0; for(itGeomObj = mSceneObject->mGeomObjects.begin(); itGeomObj != mSceneObject->mGeomObjects.end(); itGeomObj++) { ASE_GeometryObject* go = *itGeomObj; ASE_Mesh* mesh = go->mesh; SE_TextureCoordDataID tcid = SE_Application::getInstance()->createCommonID(); SE_Util::sleep(SLEEP_COUNT); tcid.write(output); geomTexCoordData[n++].texCoordID = tcid; output.writeInt(mesh->numTVertexes); output.writeInt(mesh->numFaces); int i; for(i = 0 ; i < mesh->numTVertexes ; i++) { output.writeFloat(mesh->tvertexes[i].s); output.writeFloat(mesh->tvertexes[i].t); } for(i = 0 ; i < mesh->numFaces ; i++) { output.writeInt(mesh->tfaces[i].vi[0]); output.writeInt(mesh->tfaces[i].vi[1]); output.writeInt(mesh->tfaces[i].vi[2]); } } ///////////////////// write shader program //// output.writeShort(SE_SHADERPROGRAMDATA_ID); int spNum = 1; output.writeInt(spNum);// shader program num; std::vector<SE_ProgramDataID> programDataVector(spNum); for(i = 0 ; i < spNum ; i++) { SE_ProgramDataID proID = "main_vertex_shader"; programDataVector[i] = proID; SE_Util::sleep(SLEEP_COUNT); proID.write(output); std::string str(shaderPath); std::string vertexShaderPath = str + SE_SEP + "main_vertex_shader.glsl"; std::string fragmentShaderPath = str + SE_SEP + "main_fragment_shader.glsl"; char* vertexShader = NULL; int vertexShaderLen = 0; char* fragmentShader = NULL; int fragmentShaderLen = 0; SE_IO::readFileAll(vertexShaderPath.c_str(), vertexShader, vertexShaderLen); SE_IO::readFileAll(fragmentShaderPath.c_str(), fragmentShader, fragmentShaderLen); output.writeInt(vertexShaderLen); output.writeInt(fragmentShaderLen); output.writeBytes(vertexShader, vertexShaderLen); output.writeBytes(fragmentShader, fragmentShaderLen); delete[] vertexShader; delete[] fragmentShader; } ///////////////////// write mesh //////////////// std::vector<SE_MeshID> meshIDVector(geomDataNum); output.writeShort(SE_MESHDATA_ID); output.writeInt(geomDataNum); n = 0; for(itGeomObj = mSceneObject->mGeomObjects.begin(); itGeomObj != mSceneObject->mGeomObjects.end(); itGeomObj++) { ASE_GeometryObject* go = *itGeomObj; ASE_Mesh* mesh = go->mesh; SE_MeshID meshID = SE_Application::getInstance()->createCommonID(); SE_Util::sleep(SLEEP_COUNT); meshID.write(output); meshIDVector[n] = meshID; SE_GeometryDataID geomID = geomTexCoordData[n].geomID; SE_TextureCoordDataID texCoordID = geomTexCoordData[n].texCoordID; n++; geomID.write(output); output.writeFloat(go->wireframeColor[0]); output.writeFloat(go->wireframeColor[1]); output.writeFloat(go->wireframeColor[2]); int texNum = 0; int materialref = go->materialref; int startpos = 0; int subMaterialStartPos = 0; _MaterialData mdData; if(materialref == -1) { output.writeInt(texNum); goto WRIET_SURFACE; } mdData = materialVector[materialref]; if(mdData.subMaterialNum > 0) { int j; for(j = 0 ; j < (materialref - 1) ; j++) { _MaterialData d = materialVector[j]; startpos += d.subMaterialNum; } int k = startpos; for(int j = 0 ; j < mdData.subMaterialNum ; j++) { _MaterialData subMaterialData = materialVector[materialNum + k]; k++; std::string texStr(subMaterialData.md.texName); if(texStr != "") { texNum++; } } } else { std::string texStr(mdData.md.texName); if(texStr != "") { texNum = 1; } } output.writeInt(texNum); for(i = 0 ; i < texNum ; i++) { if(mdData.subMaterialNum > 0) { int j; for(j = 0 ; j < (materialref - 1) ; j++) { _MaterialData d = materialVector[j]; subMaterialStartPos += d.subMaterialNum; } for(int j = 0 ; j < mdData.subMaterialNum ; j++) { _MaterialData subMaterialData = materialVector[materialNum + subMaterialStartPos]; subMaterialStartPos++; std::string texStr(subMaterialData.md.texName); if(texStr != "") { output.writeInt(1);//current we just has one texture unit; output.writeInt(0);//texture unit type is TEXTURE0 texCoordID.write(output); output.writeInt(1);//image num use in the texture unit. current it is not mipmap. so the num is 1 subMaterialData.tid.write(output); } } } else { std::string texStr(mdData.md.texName); if(texStr != "") { output.writeInt(1);//current we just has one texture unit; output.writeInt(0);//texture unit type is TEXTURE0 texCoordID.write(output); output.writeInt(1);//image num use in the texture unit. current it is not mipmap. so the num is 1 mdData.tid.write(output); } } } ///write surface WRIET_SURFACE: if(mesh->numFaceGroup > 0) { SE_ASSERT(mesh->numFaceGroup == mesh->faceGroup.size()); output.writeInt(mesh->numFaceGroup); std::vector<std::list<int> >::iterator itFaceGroup; int indexM = startpos; int texIndex = 0; for(itFaceGroup = mesh->faceGroup.begin() ; itFaceGroup != mesh->faceGroup.end(); itFaceGroup++) { _MaterialData md = materialVector[materialNum + indexM]; std::string texStr(md.md.texName); md.mid.write(output); output.writeInt(itFaceGroup->size()); std::list<int>::iterator itFace; for(itFace = itFaceGroup->begin() ; itFace != itFaceGroup->end() ; itFace++) { output.writeInt(*itFace); } programDataVector[0].write(output); if(texStr != "") { output.writeInt(texIndex); } else { output.writeInt(-1); } indexM++; texIndex++; } } else { output.writeInt(1); //just has one surface std::string texStr(mdData.md.texName); mdData.mid.write(output); output.writeInt(mesh->numFaces); // facets num; for(int f = 0 ; f < mesh->numFaces ; f++) output.writeInt(f); programDataVector[0].write(output); if(texStr != "") { output.writeInt(0); // the texture index is 0; } else { output.writeInt(-1); } } } /////// create scene ////////// SE_SpatialID spatialID = SE_Application::getInstance()->createCommonID(); SE_Util::sleep(SLEEP_COUNT); SE_CommonNode* rootNode = new SE_CommonNode(spatialID, NULL); rootNode->setBVType(SE_BoundingVolume::AABB); n = 0; for(itGeomObj = mSceneObject->mGeomObjects.begin(); itGeomObj != mSceneObject->mGeomObjects.end(); itGeomObj++) { ASE_GeometryObject* go = *itGeomObj; ASE_Mesh* mesh = go->mesh; SE_MeshID meshID = meshIDVector[n++]; SE_SpatialID childID = SE_Application::getInstance()->createCommonID(); SE_Util::sleep(SLEEP_COUNT); SE_Geometry* child = new SE_Geometry(childID, rootNode); rootNode->addChild(child); SE_Vector3f translate, scale, rotateAxis; translate.x = go->translate[0]; translate.y = go->translate[1]; translate.z = go->translate[2]; scale.x = go->scale[0]; scale.y = go->scale[1]; scale.z = go->scale[2]; rotateAxis.x = go->rotateAxis[0]; rotateAxis.y = go->rotateAxis[1]; rotateAxis.z = go->rotateAxis[2]; child->setLocalTranslate(translate); //child->setLocalTranslate(SE_Vector3f(0, 0, 0)); child->setLocalScale(scale); //child->setLocalScale(SE_Vector3f(1.0, 1.0, 1.0)); SE_Quat q; q.set(go->rotateAngle, rotateAxis); child->setLocalRotate(q); //q.set(0, SE_Vector3f(0, 0, 0)); child->setBVType(SE_BoundingVolume::AABB); SE_MeshSimObject* meshObj = new SE_MeshSimObject(meshID); meshObj->setName(go->name); child->attachSimObject(meshObj); } SE_SceneID sceneID = SE_Application::getInstance()->createCommonID(); SE_Util::sleep(SLEEP_COUNT); sceneID.write(outScene); _WriteSceneTravel wst(outScene); rootNode->travel(&wst, true); LOGI("write end\n"); }