コード例 #1
0
ファイル: SE_MeshSimObject.cpp プロジェクト: wljcom/3DHomeHD
SE_SimObject * SE_MeshSimObject::clone(int index,bool createNewMesh)
{
    SE_MeshSimObject * dest = new SE_MeshSimObject();
    SE_Mesh* m = NULL;

    if(createNewMesh)
    {
        SE_MeshTransfer* meshTransfer = SE_Application::getInstance()->getResourceManager()->getMeshTransfer(mMeshID);
        m = mMesh->clone();
        if(m)
        {
            std::string newmeshid = mMeshID.getStr();
            newmeshid += SE_Util::intToString(index);
            SE_MeshID newid = newmeshid.c_str();
            m->setMeshID(newid);
            SE_Application::getInstance()->getResourceManager()->setObjectMesh(newid,m);

            //change imagekey
            SE_TextureUnit* tu = m->getSurface(0)->getTexture()->getTextureUnit(0);
            SE_ImageDataID *idarray = tu->getImageDataID();
            std::string cloneImageID = idarray[0].getStr();

            const char* path = SE_Application::getInstance()->getResourceManager()->getIdPath(cloneImageID.c_str());
            if(path) {
                std::string cloneIndex = SE_Util::intToString(index);
                std::string sep = "#";
                cloneImageID = cloneImageID + sep + cloneIndex;
                idarray[0] = cloneImageID.c_str();
                SE_Application::getInstance()->getResourceManager()->setIdPath(cloneImageID.c_str(), path, true);
                SE_ImageDataID imageDataid =  SE_ImageDataID(cloneImageID.c_str());
                SE_Application::getInstance()->getResourceManager()->registerRes(SE_ResourceManager::IMAGE_RES, &imageDataid);
            }
        }
    }
    else
    {
        m = this->getMesh();
    }
    dest->setMesh(m,NOT_OWN);

    //use same mesh to create clone object not renew

#if 0
    SE_MeshTransfer* meshTransfer = SE_Application::getInstance()->getResourceManager()->getMeshTransfer(mMeshID);

    if(meshTransfer && 0)
    {
        dest = new SE_MeshSimObject(mMeshID);//mesh transfer is exist,create object from mesh id.
    }
    else
    {
        SE_Mesh* mesh = mMesh->clone();
        dest = new SE_MeshSimObject(mesh,OWN);
    }

    if(!dest)
    {
        return NULL;
    }
#endif

    /* std::string preName = getPrefixName();

     char buff[512] = {0};
     sprintf(buff,"%d",index);
     std::string num(buff);
     std::string destPreName = preName + "_clone_" + num;
     std::string newDestName = objName.replace(0,preName.length(),destPreName);
     */

    char buff[512] = {0};
    sprintf(buff,"%d",index);
    std::string num(buff);

    std::string objName = getName();
    int x = objName.find("_clone_");
    std::string destName;
    if (x > 0) {
        destName = objName.substr(x + 7) + num;
    } else {
        destName = objName + "_clone_" + num;
    }
    dest->setName(destName.c_str());

    return dest;
}
コード例 #2
0
ファイル: SE_Ase.cpp プロジェクト: huhuhu1092/test-server
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");
}