Exemple #1
0
void SE_MeshSimObject::inflate()
{
    SE_MeshTransfer* meshTransfer = SE_Application::getInstance()->getResourceManager()->getMeshTransfer(mMeshID);
    if(!meshTransfer)
    {
        //mMeshID = SE_MeshID::INVALID;
        if(SE_Application::getInstance()->SEHomeDebug)
            LOGI("[%s] meshTransfer not found!!!!,mesh id is %s\n",this->getName(),mMeshID.getStr());
        return;
    }



    if(mMesh)
    {
        //the object has inflated
        return;
    }

    SE_ResourceManager* resourceManager = SE_Application::getInstance()->getResourceManager();


    mMesh = meshTransfer->createMesh(SE_Application::getInstance()->getResourceManager());

    if(mMesh)
    {
        resourceManager->setObjectMesh(mMeshID,mMesh);
        mMesh->setMeshID(mMeshID);


        resourceManager->registerRes(SE_ResourceManager::MESH_RES, &mMeshID);
        resourceManager->registerRes(SE_ResourceManager::OBJECT_MESH_RES, &mMeshID);
        mOwnMesh = OWN;
    }
    else
    {
        return;
    }

    SE_Geometry* sp = (SE_Geometry*)getSpatial();

    if(!sp)
    {
        if(SE_Application::getInstance()->SEHomeDebug)
            LOGI("Error !!!!Mesh simobject not attach to a spatial !!!!!!!!!\n");
    }

    bool mipmap = sp->isNeedGenerateMipMap();
    if(!mipmap)
    {
        int num = this->getMesh()->getSurfaceNum();
        for(int i = 0; i < num; ++i)
        {
            SE_Texture* tx = this->getMesh()->getSurface(i)->getTexture();
            if(!tx)
            {
                continue;
            }
            int txnum = tx->getTexUnitNum();
            for(int j = 0; j < txnum; ++j)
            {
                tx->getTextureUnit(j)->setSampleMin(LINEAR);
            }
        }
    }
    SE_Vector4f spatialData = sp->getEffectData();
    int num = this->getMesh()->getSurfaceNum();

    for(int i = 0; i < num; ++i)
    {
        this->getMesh()->getSurface(i)->getMaterialData()->shiny = spatialData.x;
        this->getMesh()->getSurface(i)->getMaterialData()->shinessStrength = spatialData.y;

    }

    this->getSpatial()->applyLight();

}
Exemple #2
0
SE_Spatial *SE_Geometry::clone(SE_SimObject *srcobj, int index)
{
#if 0
    SE_Geometry * dest = new SE_Geometry();

    SE_SimObject* destobj = srcobj->clone(index);
    
    std::string srcobjname = srcobj->getName();

    //attach obj
    SE_SimObjectManager* simObjectManager = SE_Application::getInstance()->getSimObjectManager();

    SE_SimObjectID id = SE_ID::createSimObjectID();
    destobj->setID(id);
    simObjectManager->set(id, destobj);

    

    //set spatial property
    SE_Spatial *srcSpatial = srcobj->getSpatial();

    SE_SpatialID destSpatialID = SE_ID::createSpatialID();

    dest->setSpatialID(destSpatialID);

    dest->setBVType(srcSpatial->getBVType());

    SE_Vector3f a = srcSpatial->getLocalTranslate();
    dest->setLocalTranslate(a);

    dest->setLocalRotate(srcSpatial->getLocalRotate());

    dest->setLocalScale(srcSpatial->getLocalScale());

    dest->setPrevMatrix(srcSpatial->getPrevMatrix());

    dest->setPostMatrix(srcSpatial->getPostMatrix());

    dest->setParent(srcSpatial->getParent());


    dest->setLocalLayer(srcSpatial->getLocalLayer());

    //set render state
    dest->setRenderState(DEPTHTESTSTATE ,this->getRenderState(DEPTHTESTSTATE));


    dest->attachSimObject(destobj);
    //SE_Application::getInstance()->getSceneManager()->updateSpatialIDMap();
    SE_Scene* scene = srcSpatial->getScene();
    scene->addSpatial(srcSpatial->getParent(), dest);
    srcSpatial->getParent()->updateWorldTransform();
    srcSpatial->getParent()->updateBoundingVolume();
    srcSpatial->getParent()->updateRenderState();
    srcSpatial->getParent()->updateWorldLayer();
    //return clone object
    return dest;
#endif
    return NULL;
}
Exemple #3
0
SE_Spatial *SE_Geometry::clone(SE_Spatial* parent,  int index,bool createNewMesh,const char* statuslist)
{
    if(!parent)
    {
        if(SE_Application::getInstance()->SEHomeDebug)
        LOGI("No parent,clone fail.!!!!");
        return NULL;
    }

    SE_Geometry * dest = new SE_Geometry();

    SE_SimObject* destobj = getCurrentAttachedSimObj()->clone(index,createNewMesh);

    dest->setSpatialName(this->getSpatialName());
    dest->setCloneIndex(index);
    
    
    //attach obj
    dest->attachSimObject(destobj);    

    //set spatial property

    dest->setBVType(getBVType());

    SE_Vector3f a = getLocalTranslate();
    dest->setLocalTranslate(a);

    dest->setLocalRotate(getLocalRotate());

    dest->setLocalScale(getLocalScale());

    dest->setPrevMatrix(getPrevMatrix());

    dest->setPostMatrix(getPostMatrix());

    dest->setParent(parent);

    SE_Layer* l = this->getWorldLayer();
    SE_Layer* destL = dest->getWorldLayer();
    destL->setLayer(l->getLayer());    

    //set render state
    dest->setRenderState(DEPTHTESTSTATE ,this->getRenderState(DEPTHTESTSTATE));
    

    //SE_Application::getInstance()->getSceneManager()->updateSpatialIDMap();
    SE_Scene* scene = getScene();
    scene->addSpatial(parent, dest);

    parent->updateWorldTransform();
    parent->updateBoundingVolume();
    parent->updateRenderState();
    //parent->updateWorldLayer();

    //copy geometry para
    dest->setHasInflate(true);
    
    dest->setAlpha(this->getAlpha());
    

    unsigned int status = 0;
    if(statuslist)
    {
        status = SE_Util::stringToInt(statuslist);
    }
    dest->setEffectState(status);

    dest->setSpatialState(this->getSpatialState());

    dest->setSpatialEffectData(this->getSpatialEffectData());

    std::vector<std::string> lights;
    this->getLightsNameList(lights);
    for(int i = 0; i < lights.size(); ++i)
    {
        dest->addLightNameToList(lights[i].c_str());
    }

    scene->sceneApplyLight();
    
    //return clone object
    return dest;
}
Exemple #4
0
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");
}