示例#1
0
//////////////////////////////////////////////////////////////////////////
//	CreateFromMeshNode
//////////////////////////////////////////////////////////////////////////
void	ZClothEmblem::CreateFromMeshNode( RMeshNode* pMeshNdoe_ , ZWorld* pWorld)
{
	int			i, nIndices;
	rvector		vecDistance;

	mpMeshNode	= pMeshNdoe_;

	m_nCntP	= mpMeshNode->m_point_num;
	m_nCntC	= mpMeshNode->m_face_num * 3 ;

	// Initialize
	m_pX		= new rvector[m_nCntP];
	m_pOldX		= new rvector[m_nCntP];
	m_pForce	= new rvector[m_nCntP];
	m_pHolds	= new int    [m_nCntP];
	m_pWeights	= new float  [m_nCntP];
	m_pNormal	= new rvector[m_nCntP];

	m_pConst	= new sConstraint[m_nCntC];

	memset( m_pForce    , 0, sizeof(rvector)* m_nCntP );
	memset( m_pHolds    , 0, sizeof(bool)   * m_nCntP );
	memset( m_pWeights  , 0, sizeof(float)  * m_nCntP );

	nIndices = pMeshNdoe_->m_face_num*3;

	//if( bHardwareBuffer )
	//{
	//	SAFE_RELEASE(mIndexBuffer);
	//	if( FAILED( RGetDevice()->CreateIndexBuffer(nIndices*sizeof(USHORT),0,D3DFMT_INDEX16,D3DPOOL_MANAGED,&mIndexBuffer)))
	//	{
	//		mlog( "Fail to Restore Index Buffer for Emblems..\n" );
	//		bHardwareBuffer = false;
	//	}
	//}

	rmatrix World;
	rvector Pos = mpMeshNode->m_mat_base;
	rvector Dir = rvector(0,-1,0);
	rvector Up  = rvector(0,0,1);

	MakeWorldMatrix(&World, Pos, Dir, Up);

	mWorldMat = mpMeshNode->m_mat_result * World;

	D3DXVECTOR4 rVec;
	for( i = 0 ; i < mpMeshNode->m_point_num; ++i )
	{
		D3DXVec3Transform( &rVec, &mpMeshNode->m_point_list[i], &mWorldMat );
		mpMeshNode->m_point_list[i] = (rvector)rVec;
	}

	//	Copy Vertex
	memcpy( m_pX, mpMeshNode->m_point_list, sizeof(rvector) * m_nCntP );
	memcpy( m_pOldX, mpMeshNode->m_point_list, sizeof(rvector) * m_nCntP );

	//	Build Constraints
	for( i = 0 ; i < mpMeshNode->m_face_num; ++i )
	{
		for( int j = 0 ; j < 3; ++j )
		{
			m_pConst[ i*3 + j ].refA = mpMeshNode->m_face_list[i].m_point_index[j];
			if( j + 1 >= 3 )
			{
				m_pConst[ i*3 + j ].refB = mpMeshNode->m_face_list[i].m_point_index[0];
			}
			else
			{
				m_pConst[ i*3 + j ].refB = mpMeshNode->m_face_list[i].m_point_index[j+1];
			}
			vecDistance = mpMeshNode->m_point_list[m_pConst[ i*3 + j ].refA] - mpMeshNode->m_point_list[m_pConst[ i*3 + j ].refB];
			m_pConst[ i*3 + j ].restLength = D3DXVec3Length(&vecDistance);
			
			//g_index_buffer[i*3+j]	= (USHORT)m_pConst[i*3+j].refA;
		}
	}

//	if( bHardwareBuffer )
//	{
//		void* pIndices;
//		if(FAILED( mIndexBuffer->Lock(0,sizeof(USHORT)*nIndices,(VOID**)&pIndices,0)))
//		{
//			bHardwareBuffer = false;
//			mlog("Fail to Lock Index Buffer\n");
//		}
//		else
//		{
//			memcpy( pIndices, g_index_buffer, sizeof(USHORT)*nIndices);
//			mIndexBuffer->Unlock();
//		}
//		
////		delete uIndexList;
//	}

	_ASSERT( mpMeshNode->m_point_color_num );

	for( i = 0 ; i < m_nCntP; ++i )
	{
		m_pHolds[i]	= CLOTH_VALET_ONLY;
		// 만약 붉은색 성분이 있으면 & Exclusive with other Attribute...
		if( mpMeshNode->m_point_color_list[i].x != 0 )
		{
			m_pHolds[i]	= CLOTH_HOLD;
		}
		else 
		{
			//	만약 파란색 성분이 있으면 : 힘!
			//if( mpMeshNode->m_point_color_list[i].z != 0 )
			{
				m_pHolds[i] |= CLOTH_FORCE;
			}
			//	만약 녹색 성분이 있으면 : 충돌!
			if( mpMeshNode->m_point_color_list[i].y != 0 )
			{
				m_pHolds[i] |= CLOTH_COLLISION;
			}
		}
	}

//	RLightList*	pLightList	= ZGetGame()->GetWorld()->GetBsp()->GetObjectLightList();
	m_pWorld = pWorld;
	RLightList*	pLightList	= m_pWorld->GetBsp()->GetObjectLightList();

	float	minDistance = 999999;
	float	fTemp;
	RLIGHT	*pSelectedLight = 0;
	
	for(RLightList::iterator itor = pLightList->begin();
		itor != pLightList->end(); ++itor )
	{
		RLIGHT *pLight	=	*itor;

		fTemp	= D3DXVec3Length( &(pLight->Position - m_pX[0]) );
		if( fTemp < minDistance )
		{
			minDistance		= fTemp;
			pSelectedLight	= pLight;
		}
	}

	mpLight	= new D3DLIGHT9;
	memset( mpLight, 0, sizeof(D3DLIGHT9));

	mpLight->Ambient.r = 0.3;
	mpLight->Ambient.g = 0.3;
	mpLight->Ambient.b = 0.3;

	if( pSelectedLight!=0 &&  minDistance < pSelectedLight->fAttnEnd	)
	{	
		mpLight->Type		= D3DLIGHT_POINT;

		mpLight->Diffuse.r	= pSelectedLight->Color.x * pSelectedLight->fIntensity;
		mpLight->Diffuse.g	= pSelectedLight->Color.y * pSelectedLight->fIntensity;
		mpLight->Diffuse.b	= pSelectedLight->Color.z * pSelectedLight->fIntensity;

		mpLight->Position	= pSelectedLight->Position;

		mpLight->Range		= pSelectedLight->fAttnEnd;
		mpLight->Attenuation1	= 0.0001f;
	}
	else	// 가까운 곳에 빛이 없으면 기본 라이팅 적용
	{
		mpLight->Type		= D3DLIGHT_DIRECTIONAL;
		
		mpLight->Diffuse.r	= 0.1f;
		mpLight->Diffuse.g	= 0.1f;
		mpLight->Diffuse.b	= 0.1f;

		mpLight->Direction	= rvector( 1, 1, 1 );
	
		mpLight->Attenuation1	= 0.0f;
		mpLight->Attenuation0	= 0.0f;	

		mpLight->Range		= 0.0f;
	}

	//PreCalculate AABB
	float	mostSmallX, mostSmallY, mostSmallZ;
	float	mostBigX, mostBigY, mostBigZ;

	mostSmallX	= mostSmallY	= mostSmallZ	= 9999999;
	mostBigX	= mostBigY		= mostBigZ		= -9999999;

	rvector* pCurr;
	for( i = 0 ; i < m_nCntP; ++i )
	{
		pCurr	= &mpMeshNode->m_point_list[i];
		
		mostSmallX	= min( mostSmallX, pCurr->x );
		mostSmallY	= min( mostSmallY, pCurr->y );
		mostSmallZ	= min( mostSmallZ, pCurr->z );

		mostBigX	= max( mostBigX, pCurr->x );
		mostBigY	= max( mostBigY, pCurr->y );
		mostBigZ	= max( mostBigZ, pCurr->z );
	}

	mAABB.vmin = rvector( mostSmallX - RESERVED_SPACE, mostSmallY - RESERVED_SPACE, mostSmallZ - RESERVED_SPACE );
	mAABB.vmax = rvector( mostBigX + RESERVED_SPACE, mostBigY + RESERVED_SPACE, mostBigZ + RESERVED_SPACE );

	mMyTime	= 0;
}
void ZCharacterObject::Draw_SetLight(const rvector& vPosition)
{
    //	±¤¿øÁ¤ÀÇ
    //	0¹ø - ambient : ij¸¯ÅÍÀÇ ±âº» ¹à±â
    //		- diffuse : ¼ø°£ÀûÀÎ ±¤¿ø( ÃÑ·ù¸¦ ¹ß»çÇÒ¶§ Àû¿ë)
    //	1¹ø - ambient : »ç¿ë¾ÈÇÔ
    //		- diffuse : ¸ÊÀÇ µðÇ»Áî ¶óÀÌÆ®
    //	specular, emit ¼Ó¼ºÀº »ç¿ë¾ÈÇÔ

#define CHARACTER_AMBIENT	0.0
    D3DLIGHT9 Light;
    rvector pos;
    rvector char_pos = vPosition;
    char_pos.z += 180.f;

    memset( &Light, 0, sizeof( D3DLIGHT9 ));	// ÃʱâÈ­

    //	0¹ø ¶óÀÌÆ® begine
    Light.Type		= D3DLIGHT_POINT;
    Light.Ambient.r = CHARACTER_AMBIENT;
    Light.Ambient.g = CHARACTER_AMBIENT;
    Light.Ambient.b = CHARACTER_AMBIENT;

    Light.Specular.r = 1.f;
    Light.Specular.g = 1.f;
    Light.Specular.b = 1.f;
    Light.Specular.a = 1.f;

    if( ZGetConfiguration()->GetVideo()->bDynamicLight && m_bDynamicLight )
    {
        m_vLightColor.x -= 0.03f;
        m_vLightColor.y -= 0.03f;
        m_vLightColor.z -= 0.03f;
        max( m_vLightColor.x, 0.0f );
        max( m_vLightColor.y, 0.0f );
        max( m_vLightColor.z, 0.0f );
        Light.Diffuse.r	= m_vLightColor.x;
        Light.Diffuse.g	= m_vLightColor.y;
        Light.Diffuse.b	= m_vLightColor.z;
        Light.Range = g_CharLightList[m_iDLightType].fRange;

        float lastTime	= m_fTime;
        m_fTime			= timeGetTime();
        float lap		= m_fTime - lastTime;
        m_fLightLife	-= lap;

        if( m_fLightLife <= 0.0f )
        {
            m_bDynamicLight = false;
            m_fLightLife	= 0;
        }
    }
    else
    {
        m_bDynamicLight = false;
        m_vLightColor.x = 0.0f;
        m_vLightColor.y = 0.0f;
        m_vLightColor.z = 0.0f;
    }

    if( IsDoubleGun() )
    {
        if( m_bLeftShot ) GetWeaponTypePos( weapon_dummy_muzzle_flash, &pos ,true);
        else			  GetWeaponTypePos( weapon_dummy_muzzle_flash, &pos );

        m_bLeftShot = !m_bLeftShot;
    }
    else
    {
        GetWeaponTypePos( weapon_dummy_muzzle_flash, &pos );
    }
    Light.Position.x	= pos.x ;
    Light.Position.y	= pos.y ;
    Light.Position.z	= pos.z ;

    Light.Attenuation1	= 0.05f;
    Light.Attenuation2	= 0.001f;

    if (IsNPC())
    {
        Light.Ambient.r = 0.4f;
        Light.Ambient.g = 0.4f;
        Light.Ambient.b = 0.4f;
        Light.Range = 2000.0f;

        Light.Attenuation0 = 1.0f;
        Light.Attenuation1 = 0.0f;
        Light.Attenuation2 = 0.0f;
    }
//	RGetDevice()->SetLight( 0, &Light );
//	RGetDevice()->LightEnable( 0, TRUE );

//	if( RShaderMgr::mbUsingShader )
//	{
//		RGetShaderMgr()->setLight( 0, &Light );
//		RGetShaderMgr()->LightEnable( 0, TRUE );
//	}

    m_pVMesh->SetLight(0,&Light,false);

    // 0¹ø ¶óÀÌÆ® end

    // 1¹ø ¶óÀÌÆ® begine
    ZeroMemory( &Light, sizeof(D3DLIGHT9) );
    RLightList* pLightList = 0;
    RLIGHT* pLight = 0;
    RLIGHT* pSelectedLight = 0;
    rvector sunDir;
    float distance;
    float SelectedLightDistance = FLT_MAX;
    pLightList = ZGetGame()->GetWorld()->GetBsp()->GetSunLightList();
    int nLight = (int)pLightList->size();

    Light.Specular.r = 1.f;
    Light.Specular.g = 1.f;
    Light.Specular.b = 1.f;
    Light.Specular.a = 1.f;

    if( nLight > 0 )
    {
        // Áß°£¿¡ ¸Ê ÇÈÅ·ÀÌ ¾ÈµÇ¸é..
        // ÇØ°¡ ¿©·¯°³À̸é? °Å¸®·Î ¼±ÅÃ..
        for(  RLightList::iterator iter = pLightList->begin(); iter != pLightList->end(); ++iter )
        {
            pLight	= *iter;
            sunDir	= pLight->sPosition - char_pos;
            distance	= D3DXVec3LengthSq( &sunDir );
            D3DXVec3Normalize( &sunDir, &sunDir );
            RBSPPICKINFO info;
            if( ZGetGame()->GetWorld()->GetBsp()->Pick( char_pos, sunDir, &info,RM_FLAG_ADDITIVE  ) )
            {
                if( distance > D3DXVec3LengthSq(&( char_pos - info.PickPos )) )
                {
                    continue;
                }
            }
            if( distance < SelectedLightDistance )
            {
                SelectedLightDistance = distance;
                pSelectedLight = pLight;
            }
        }
        Light.Type       = D3DLIGHT_POINT;
        Light.Attenuation1 = 0.00001f;
        if( pSelectedLight != 0 )
        {
            Light.Diffuse.r	= pSelectedLight->Color.x * pSelectedLight->fIntensity * 0.15f;
            Light.Diffuse.g  = pSelectedLight->Color.y * pSelectedLight->fIntensity * 0.15f;
            Light.Diffuse.b  = pSelectedLight->Color.z * pSelectedLight->fIntensity * 0.15f;
        }
    }

    SelectedLightDistance = FLT_MAX ;
    if( pSelectedLight == 0 )
    {
        pLightList	= ZGetGame()->GetWorld()->GetBsp()->GetObjectLightList();

        // ¿©±â ÃÖÀûÈ­°¡ ÂÀ ÇÊ¿äÇÏ´Ù
        for( RLightList::iterator itor = pLightList->begin(); itor != pLightList->end(); itor++ )
        {
            RLIGHT *pl = *itor;
            float fDist = Magnitude( pl->sPosition-char_pos );
            if( SelectedLightDistance > fDist )
            {
                SelectedLightDistance = fDist;
                pSelectedLight = pl;
            }
        }

        Light.Type       = D3DLIGHT_POINT;
        Light.Attenuation1 = 0.0025f;

        if( pSelectedLight != 0 )
        {
            Light.Diffuse.r  = pSelectedLight->Color.x * pSelectedLight->fIntensity;
            Light.Diffuse.g  = pSelectedLight->Color.y * pSelectedLight->fIntensity;
            Light.Diffuse.b  = pSelectedLight->Color.z * pSelectedLight->fIntensity;
        }
    }

    if( pSelectedLight != NULL )
    {
        Light.Position = pSelectedLight->sPosition;
        //	Light.Diffuse.r  = pSelectedLight->Color.x;
        //	Light.Diffuse.g  = pSelectedLight->Color.y;
        //	Light.Diffuse.b  = pSelectedLight->Color.z;
        Light.Range       = pSelectedLight->fAttnEnd;

        m_pVMesh->SetLight(1,&Light,false);

//		RGetDevice()->SetLight( 1, &Light );
//		RGetDevice()->LightEnable( 1, TRUE );

//		if( RShaderMgr::mbUsingShader )
//		{
//			RGetShaderMgr()->setLight( 1, &Light );
//			RGetShaderMgr()->LightEnable( 1, TRUE );
//			RGetShaderMgr()->setLight( 0x00cccccc );
//		}
    }
    else
    {
        m_pVMesh->SetLight(1,NULL,false);

        //	RGetDevice()->LightEnable( 1, FALSE );
        //	if( RShaderMgr::mbUsingShader )
        //	{
        //		RGetShaderMgr()->LightEnable( 1, FALSE );
        //	}
    }
    // 1¹ø ¶óÀÌÆ® end

    RGetShaderMgr()->setAmbient( 0x00cccccc );
    RGetDevice()->SetRenderState( D3DRS_LIGHTING, TRUE );
    RGetDevice()->SetRenderState( D3DRS_AMBIENT, 0x00cccccc );
}