///////////////////////////////////////////函数实现////////////////////////////////////////////////
bool LoadModels(ISceneNode* m_ICamera,std::map<std::string,IAnimatedMeshSceneNode*>& myNode)
{
	_finddata_t file; 
	long longf;   
  
    //读取文件夹下文件名字 
    if((longf = _findfirst("..\\model\\*.3ds", &file))==-1l)   
    {   
        std::cout<<"没有找到模型!\n";   
    }   
    else  
    {   
        std::cout<<"\n加载模型\n";   
		std::string tempName;
		tempName = "";   
        tempName = file.name;
		m_vecStrModelName.push_back(tempName);//加载第一个模型名字
        while( _findnext( longf, &file ) == 0 )   
        {     
			tempName = "";   
            tempName = file.name; 
            if (tempName == "..")   
            {   
                continue;   
            }   
			m_vecStrModelName.push_back(tempName);
        }   
    }   
  
    _findclose(longf); 

	//模型库模型节点的ID(从1000开始)
	int storeModelID=1000;
	//加载模型创建场景节点
   auto iter(m_vecStrModelName.begin());
   while(iter!=m_vecStrModelName.end())
   {
	   std::string myPath=*iter;
	   myPath="..\\model\\"+myPath;
	   scene::IAnimatedMesh* mesh = m_IrrSmgr->getMesh((char*)myPath.c_str());

	   //模型设为摄像头的子节点
       IAnimatedMeshSceneNode* modelNode = m_IrrSmgr->addAnimatedMeshSceneNode(mesh,m_ICamera);

	   // 设置自发光强度大小(即镜面渲染中的亮度大小)
       modelNode->getMaterial(0).Shininess = 20.0f; 

	   modelNode->setMaterialFlag(EMF_LIGHTING, true);

	   modelNode->setRotation(vector3df(90, 0, 0));
	   modelNode->setPosition(vector3df(90, 0, 0));

	   //创建三角选择器
	   m_ItriElector = m_IrrSmgr->createTriangleSelector(modelNode);
	   modelNode->setTriangleSelector(m_ItriElector);
	   m_ItriElector->drop(); // We're done with this m_ItriElector, so drop it now.

	   myNode[(*iter)]=modelNode;//插入模型名字和模型库结点对
	   iter++;
	   //设置模型为不可见
	   modelNode->setVisible(false);

	   //设置模型库模型ID
	   modelNode->setID(storeModelID);
	   ++storeModelID;
   }

   //计算模型库的页数(三个模型一页)
   m_iTotalPageNumber=m_vecStrModelName.size()/m_iPageModelCount;
   if(m_vecStrModelName.size()%m_iPageModelCount!=0)
   {
	   m_iTotalPageNumber+=1;
   }

   //添加翻页标志模型
   for(int i=0;i<2;i++)
   {
	   scene::IAnimatedMesh* mesh;
	   if(0==i)
	   {
		   mesh = m_IrrSmgr->getMesh("left.3ds");
	   }
	   else
	   {
		   mesh = m_IrrSmgr->getMesh("right.3ds");
	   }
	   //模型设为摄像头的子节点
		IAnimatedMeshSceneNode* modelNode = m_IrrSmgr->addAnimatedMeshSceneNode(mesh,m_ICamera);

		// 设置自发光强度大小(即镜面渲染中的亮度大小)
		modelNode->getMaterial(0).Shininess = 20.0f; 

		modelNode->setMaterialFlag(EMF_LIGHTING, true);

		modelNode->setRotation(vector3df(90, 0, 0));

		//创建三角选择器
		m_ItriElector = m_IrrSmgr->createTriangleSelector(modelNode);
		modelNode->setTriangleSelector(m_ItriElector);
		m_ItriElector->drop(); // We're done with this m_ItriElector, so drop it now.

		//设置模型为不可见
		modelNode->setVisible(false);

		//设置模型库模型ID
		if(0==i)
		{
			modelNode->setID(2000);
		}
		else
		{
			modelNode->setID(2001);
		}
   }
   
   return true;
}
Example #2
0
// registering the scene node for rendering, here we take the opportunity
// to make LOD adjustments to child nodes
void CLODSceneNode::OnRegisterSceneNode()
{
	//if this node is invisible forget any child nodes
	if (!IsVisible)
		return;

	// get a timer to calculate the amount to fade objects
	u32 time = device->getTimer()->getTime();

	// get the camera position
	ICameraSceneNode* cam = smgr->getActiveCamera();
	core::vector3df vectorCam = cam->getAbsolutePosition();

	// loop through all child nodes
	u32 i,j;
	u32 lod, fade;
	SMaterial * material;
	for (i=0; i < children.size(); ++i)
	{
		// get the node associated with this link
		SChildLink &child = children[i];
		IAnimatedMeshSceneNode *node = (IAnimatedMeshSceneNode *)child.node;

		// calculate the distance to the node. at the moment we do this the
		// slow linear way instead of using the distance squared method
		core::vector3df vectorNode = node->getAbsolutePosition();
		float distance = vectorNode.getDistanceFrom( vectorCam );

		// itterate through all of the LOD levels and find the lod distance
		// that is appropriate for this distance
		lod = 0xFFFFFFFF;
		for (j=0; j < lods.size(); ++j)
		{
			if ( distance >= lods[j].distance )
			{
				lod = j;
			}
		}

        // if a LOD level was found
		if ( lod != 0xFFFFFFFF )
		{
            // if this lod is associated with a mesh
            if ( lods[lod].mesh )
            {
                // if this lod differs from the child lod
                if ( lod != children[i].lod )
                {
                    children[i].lod = lod;

                    // if the node is an animated mesh node
                    if ( ( node->getType()) == ESNT_ANIMATED_MESH )
                    {
                        // set the new mesh to be used
                        node->setMeshClean( lods[lod].mesh );
                    }
                }
                // handle instances where the node is faded
                switch ( children[i].mode )
                {
                    case LOD_INVISIBLE:
                        // make the node visible
                        node->setVisible( true );
                        children[i].mode = LOD_FADE_IN;
                        children[i].fade = time;
                    break;

                    // we are partially faded we need to fade back in
                    case LOD_FADE_IN:
                        // fade the node in by 1 multiplied by the time passed
                        fade = (time - children[i].fade) / fadeScale;
                        if ( fade > 0xFF ) fade = 0xFF;

                        // if the node is fully opaque again
                        if ( fade == 0xFF )
                        {
                            // restore the origonal material type
                            node->setMaterialType(children[i].matType);
                            children[i].mode = LOD_OPAQUE;
                            if ( callback ) callback( true, node );
                        }

                        // fade the node through its materials
                        fade *= 0x01010101;
                        material = &node->getMaterial( 0 );
						if ( useAlpha )
						{
							material->DiffuseColor.set( fade );
							material->AmbientColor.set( fade );
						}
						else
						{
							material->DiffuseColor.set( fade & 0xFFFFFF );
							material->AmbientColor.set( fade & 0xFFFFFF );
						}

                    break;

					// we were in the process of fading out
					case LOD_FADE_OUT:
						children[i].fade = time - ( 0xFF * fadeScale - ( time - children[i].fade ));
						children[i].mode = LOD_FADE_IN;
					break;
                }
            }
			else
			{
                // we have a lod without a mesh, this is an instruction to fade
			    switch ( children[i].mode )
			    {
			        // the node is fully opaque start fading
			        case LOD_OPAQUE:
                        children[i].mode = LOD_FADE_OUT;
                        children[i].fade = time;

						// note the material type
                        children[i].matType = node->getMaterial(0).MaterialType;

						// set the material type based on mapping
						node->setMaterialType( matmap[children[i].matType] );
                    //break;

                    // the node is in the process of fading
                    case LOD_FADE_OUT:
                        // fade the node out by 1 multiplied by the time passed
                        fade = (time - children[i].fade) / fadeScale;
                        if ( fade > 0xFF ) fade = 0xFF;

                        // if the node is fully transparent
                        if ( fade == 0xFF )
                        {
                            // make it completely invisible
                            node->setVisible( false );
                            children[i].mode = LOD_INVISIBLE;
                            if ( callback ) callback( false, node );
                        }

                        // fade the node through its materials
						fade *= 0x01010101;
                        fade = 0xFFFFFFFF - fade;
                        material = &node->getMaterial( 0 );
						if ( useAlpha )
						{
							material->DiffuseColor.set( fade );
							material->AmbientColor.set( fade );
						}
						else
						{
							material->DiffuseColor.set( fade & 0xFFFFFF );
							material->AmbientColor.set( fade & 0xFFFFFF );
						}

                    break;

					// we were in the process of fading in
					case LOD_FADE_IN:
						children[i].fade = time - ( 0xFF * fadeScale - ( time - children[i].fade ));
						children[i].mode = LOD_FADE_OUT;
					break;
			    } // switch
			}
		}
		else
		{
            // handle instances where the node is faded
            switch ( children[i].mode )
            {
                case LOD_INVISIBLE:
                    // make the node visible
                    node->setVisible( true );
                    children[i].mode = LOD_FADE_IN;
                    children[i].fade = time;
                    if ( callback ) callback( true, node );
                break;

                // we are partially faded we need to fade back in
                case LOD_FADE_IN:
                    // fade the node in by 1 multiplied by the time passed
                    fade = (time - children[i].fade) / fadeScale;
                    if ( fade > 255 ) fade = 255;

                    // if the node is fully opaque again
                    if ( fade == 0xFF )
                    {
                        // restore the origonal material type
                        node->setMaterialType(children[i].matType);
                        children[i].mode = LOD_OPAQUE;
                    }

                    // fade the node through its materials
					fade *= 0x01010101;
                    material = &node->getMaterial( 0 );
					if ( useAlpha )
					{
						material->DiffuseColor.set( fade );
						material->AmbientColor.set( fade );
					}
					else
					{
						material->DiffuseColor.set( fade & 0xFFFFFF );
						material->AmbientColor.set( fade & 0xFFFFFF );
					}

                break;

				// we were in the process of fading out
				case LOD_FADE_OUT:
					children[i].fade = time - ( 0xFF * fadeScale - ( time - children[i].fade ));
					children[i].mode = LOD_FADE_IN;
				break;
            }
		}
	}

	ISceneNode::OnRegisterSceneNode();
}