コード例 #1
0
ファイル: FreeCameraController.cpp プロジェクト: Muret/Zephyr
void FreeCameraController::set_frame(const D3DXMATRIX& value)
{
	camera_position_ = D3DXVECTOR4(value.m[3][0], value.m[3][1], value.m[3][2], 1);

	{
		D3DXVECTOR4 view_direction = D3DXVECTOR4(value.m[0][0], value.m[0][1], value.m[0][2], 0);
		view_direction.y = 0.0f;
		D3DXVec4Normalize(&view_direction, &view_direction);

		yaw_ = acos(D3DXVec4Dot(&view_direction, &default_forward_));
	}

	{
		D3DXVECTOR4 view_direction = D3DXVECTOR4(value.m[0][0], value.m[0][1], value.m[0][2], 0);
		D3DXVECTOR4 temp = view_direction;
		view_direction.y = 0.0f;

		D3DXVec4Normalize(&view_direction, &view_direction);
		D3DXVec4Normalize(&temp, &temp);

		pitch_ = acos(D3DXVec4Dot(&temp, &view_direction));
	}

	recompute_rotation();
}
コード例 #2
0
ファイル: GStillEntity.cpp プロジェクト: minuowa/Content
bool GStillEntity::pick ( const POINT& pt )
{

    D3DXMATRIX	matView, matProj;

    Content::Device.getD9Device()->GetTransform ( D3DTS_VIEW, &matView );
    Content::Device.getD9Device()->GetTransform ( D3DTS_PROJECTION, &matProj );

    ////射线的起点为眼睛(在视图空间中为坐标原点(0,0,0))
    D3DXVECTOR4 vOrigin ( 0, 0, 0, 1 );

    ////射线方向为眼睛看向鼠标的朝向
    D3DXVECTOR4 vDir;

    //将鼠标位置从2D平面转换到3D的视图空间中
    vDir.x = ( ( ( 2.0f * pt.x ) /  Content::Device.mWidth ) - 1 ) / matProj._11;
    vDir.y = - ( ( ( 2.0f * pt.y ) /  Content::Device.mHeight ) - 1 ) / matProj._22;
    vDir.z =  1.0f;
    vDir.w =  0.0f;

    D3DXMatrixInverse ( &matView, NULL, &matView );

    //将vDir和vPos转换到世界坐标系中
    D3DXVec4Transform ( &vDir, &vDir, &matView );
    D3DXVec4Normalize ( &vDir, &vDir );

    D3DXVec4Transform ( &vOrigin, &vOrigin, &matView );

    return checkIntersect ( vOrigin, vDir, false );

}
コード例 #3
0
ファイル: Material.cpp プロジェクト: H-EAL/assimp
//-------------------------------------------------------------------------------
int CMaterialManager::SetupMaterial (
    AssetHelper::MeshHelper* pcMesh,
    const aiMatrix4x4& pcProj,
    const aiMatrix4x4& aiMe,
    const aiMatrix4x4& pcCam,
    const aiVector3D& vPos)
{
    ai_assert(NULL != pcMesh);
    if (!pcMesh->piEffect)return 0;

    ID3DXEffect* piEnd = pcMesh->piEffect;

    piEnd->SetMatrix("WorldViewProjection",
        (const D3DXMATRIX*)&pcProj);

    piEnd->SetMatrix("World",(const D3DXMATRIX*)&aiMe);
    piEnd->SetMatrix("WorldInverseTranspose",
        (const D3DXMATRIX*)&pcCam);

    D3DXVECTOR4 apcVec[5];
    memset(apcVec,0,sizeof(apcVec));
    apcVec[0].x = g_avLightDirs[0].x;
    apcVec[0].y = g_avLightDirs[0].y;
    apcVec[0].z = g_avLightDirs[0].z;
    apcVec[0].w = 0.0f;
    apcVec[1].x = g_avLightDirs[0].x * -1.0f;
    apcVec[1].y = g_avLightDirs[0].y * -1.0f;
    apcVec[1].z = g_avLightDirs[0].z * -1.0f;
    apcVec[1].w = 0.0f;
    D3DXVec4Normalize(&apcVec[0],&apcVec[0]);
    D3DXVec4Normalize(&apcVec[1],&apcVec[1]);
    piEnd->SetVectorArray("afLightDir",apcVec,5);

    apcVec[0].x = ((g_avLightColors[0] >> 16)   & 0xFF) / 255.0f;
    apcVec[0].y = ((g_avLightColors[0] >> 8)    & 0xFF) / 255.0f;
    apcVec[0].z = ((g_avLightColors[0])         & 0xFF) / 255.0f;
    apcVec[0].w = 1.0f;

    if( g_sOptions.b3Lights)
    {
        apcVec[1].x = ((g_avLightColors[1] >> 16) & 0xFF) / 255.0f;
        apcVec[1].y = ((g_avLightColors[1] >> 8) & 0xFF) / 255.0f;
        apcVec[1].z = ((g_avLightColors[1]) & 0xFF) / 255.0f;
        apcVec[1].w = 0.0f;
    } else
コード例 #4
0
ファイル: Exercise01_D3D10.cpp プロジェクト: KNeal/Oculus
//--------------------------------------------------------------------------------------
// Render the scene using the D3D9 device
//--------------------------------------------------------------------------------------
void CALLBACK OnD3D10FrameRender( ID3D10Device* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
    HRESULT hr = S_OK;

    float ClearColor[4] = { 0,0,0,0 };
    ID3D10RenderTargetView* pRTV = DXUTGetD3D10RenderTargetView();
    pd3dDevice->ClearRenderTargetView( pRTV, ClearColor );
    ID3D10DepthStencilView* pDSV = DXUTGetD3D10DepthStencilView();
    pd3dDevice->ClearDepthStencilView( pDSV, D3D10_CLEAR_DEPTH, 1.0, 0 );

    // If the settings dialog is being shown, then render it instead of rendering the app's scene
    if( g_SettingsDlg.IsActive() )
    {
        g_SettingsDlg.OnRender( fElapsedTime );
        return;
    }

    // Render the scene
    {
        D3DXVECTOR4 vLightDir( -1,1,-1,1 );
        D3DXVec4Normalize( &vLightDir, &vLightDir );
        g_pvWorldLightDir->SetFloatVector( ( float* )&vLightDir );
        g_pfTime->SetFloat( ( float )fTime );
        g_pfElapsedTime->SetFloat( fElapsedTime );

        VisibilityCullTiles();

        DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"Render Sky" );
        RenderSky( pd3dDevice );
        DXUT_EndPerfEvent();

        DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"Render Terrain" );
        RenderTerrain( pd3dDevice );
        DXUT_EndPerfEvent();

        if( g_bRenderBalls )
        {
            DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"Render Balls" );
            RenderBalls( pd3dDevice );
            DXUT_EndPerfEvent();
        }

        if( g_bRenderGrass )
        {
            DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"Render Grass" );
            RenderGrass( pd3dDevice );
            DXUT_EndPerfEvent();
        }
		
        DXUT_BeginPerfEvent( DXUT_PERFEVENTCOLOR, L"HUD / Stats" ); // These events are to help PIX identify what the code is doing
        RenderText();
        V( g_HUD.OnRender( fElapsedTime ) );
        V( g_SampleUI.OnRender( fElapsedTime ) );
        DXUT_EndPerfEvent();
    }
}
コード例 #5
0
ファイル: Object3D.cpp プロジェクト: ServerGame/ATools
void CObject3D::Render(const D3DXMATRIX* world, int lod, float currentFrame, int nextFrame, int textureEx, int alpha)
{
	m_group = m_LOD ? &m_groups[lod] : &m_groups[0];

	D3DXMATRIX mat;
	int i;

	if (textureEx != m_group->currentTextureEx)
		_setTextureEx(textureEx);

	if (m_group->updates)
		_animate(currentFrame, nextFrame);

	if (m_frameCount && !m_externBones && m_motion)
	{
		if (m_motion)
			m_motion->Animate(m_baseBones, currentFrame, nextFrame);

		if (m_sendVS)
		{
			for (i = 0; i < m_boneCount; i++)
			{
				mat = m_baseInvBones[i] * m_baseBones[i];
				D3DXMatrixTranspose(&mat, &mat);
				m_device->SetVertexShaderConstantF(i * 3, (float*)&mat, 3);
			}
		}

		mat = *world * g_global3D.view * g_global3D.proj;
		D3DXMatrixTranspose(&mat, &mat);
		m_device->SetVertexShaderConstantF(84, (float*)&mat, 4);

		D3DXVECTOR4 vLight;
		D3DXMatrixInverse(&mat, null, world);
		D3DXVec4Transform(&vLight, &g_global3D.lightVec, &mat);
		D3DXVec4Normalize(&vLight, &vLight);
		m_device->SetVertexShaderConstantF(92, (float*)&vLight, 1);
	}

	for (i = 0; i < m_group->objectCount; i++)
	{
		const GMObject& obj = m_group->objects[i];

		if (obj.type == GMT_SKIN)
			_renderSkin(obj, world, alpha);
		else
		{
			if (!g_global3D.night && obj.light)
				continue;

			mat = m_group->updates[i] * *world;
			_renderNormal(obj, &mat, alpha);
		}
	}
}
コード例 #6
0
void SkinnedMesh::DrawFrame( LPD3DXFRAME pFrame )
{
	DDCamera* camera = GPlayerManager->GetCamera();
	if ( !camera )
		return;

	D3DXMATRIXA16 matProj = camera->GetMatProj();

	// Set the projection matrix for the vertex shader based skinning method
	if ( FAILED( m_pEffect->SetMatrix( "mViewProj", &matProj ) ) )
	{
		assert( false );
		return;
	}

	// Set Light for vertex shader
	// 조심해!! 
	// 전역 라이트 속성 가져올 것
	D3DXVECTOR4 vLightDir( 0.0f, 1.0f, -1.0f, 0.0f );
	D3DXVec4Normalize( &vLightDir, &vLightDir );
	if ( FAILED( DDRenderer::GetInstance()->GetDevice()->SetVertexShaderConstantF( 1, (float*)&vLightDir, 1 ) ) )
	{
		assert( false );
		return;
	}

	if ( FAILED( m_pEffect->SetVector( "lhtDir", &vLightDir ) ) )
	{
		assert( false );
		return;
	}

	LPD3DXMESHCONTAINER pMeshContainer;

	pMeshContainer = pFrame->pMeshContainer;
	while ( pMeshContainer != NULL )
	{
		DrawMeshContainer( pMeshContainer, pFrame );

		pMeshContainer = pMeshContainer->pNextMeshContainer;
	}

	if ( pFrame->pFrameSibling != NULL )
	{
		DrawFrame( pFrame->pFrameSibling );
	}

	if ( pFrame->pFrameFirstChild != NULL )
	{
		DrawFrame( pFrame->pFrameFirstChild );
	}
}
コード例 #7
0
void GameObject::Draw(){

	if( pDrawData == NULL || pShader == NULL || pAlcroDirectX == NULL )
		return;



	//pShader->Begin(pDeviceContext, "");
	//struct LambertConstantBuffer{
	//	XMMATRIX Transform;
	//	XMFLOAT4 LightNormal;
	//};

	//LambertConstantBuffer buffer;
	//buffer.Transform = pCamera->GetTransform(transform->Position,transform->Rotation, transform->Scale);
	//pShader->SetConstantBufferValue(0, &buffer, pDeviceContext);



	D3DXVECTOR4 light = D3DXVECTOR4(100, 10, -30, 0);
	D3DXVec4Normalize( &light, &light);

	XMMATRIX matWVP = pCamera->GetTransform(transform->Position,transform->Rotation, transform->Scale);

	XMMATRIX matWV  = pCamera->GetWorld(transform->Position,transform->Rotation, transform->Scale) * pCamera->GetViewMatrix();

	matWV = XMMatrixTranspose(matWV);

	//g_pSSAO->SetCBStep01( 
	//	pDeviceContext, 
	//	&matWVP, 
	//	&matWV,
	//	&light,
	//	hpShaderResourceView );


	g_GrayBlueColorMaterial->EnableShader();

	g_GrayBlueColorMaterial->SetConstantBuffer( this );

	pAlcroDirectX->Draw3DData(pDrawData);

	g_GrayBlueColorMaterial->DisableShader();

	//pShader->End();
}
コード例 #8
0
void MyAppInit()
{
	// TODO: Perform any application-level initialization here


	D3DXVECTOR3 vecEye (-10.0f, 5.0f, 45.0f);
	D3DXVECTOR3 vecAt (47.0f, 0.0f, 1.0f);
	g_Camera.SetViewParams( &vecEye, &vecAt );

	g_Camera.SetEnableYAxisMovement( true );
	g_Camera.SetRotateButtons( false, false, true );
	g_Camera.SetScalers( 0.01f, 50.0f );



	for (s32 i = 0; i < NUM_LIGHT; i++)
		D3DXVec4Normalize(&vLightWorld[i], &vLightWorld[i]);

	//OnMyAppDestroyDevice(g_pD3dDevice);

	sample.Initialise();

};
コード例 #9
0
ファイル: main.cpp プロジェクト: jjuiddong/Dx9-Shader
//-------------------------------------------------------------
// Name: Render()
// Desc: 화면 렌더
//-------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
    D3DXMATRIX m, mT, mR, mL, mView, mProj;
	D3DXVECTOR4 v, light_pos;
	D3DMATERIAL9 *pMtrl;
	LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
	D3DVIEWPORT9 oldViewport;
	DWORD i;

	// 뷰포트
	D3DVIEWPORT9 viewport = {0,0      // 좌측상단
					, MAP_WIDTH  // 폭
					, MAP_HEIGHT // 높이
					, 0.0f,1.0f};     // 전면,후면

	//-------------------------------------------------
	// 렌더링타겟 보존
	//-------------------------------------------------
	m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
	m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
	m_pd3dDevice->GetViewport(&oldViewport);

	TVERTEX Vertex[4] = {
		//         x                  y       z rhw tu tv
		{                0,                 0,0, 1, 0, 0,},
		{oldViewport.Width,                 0,0, 1, 1, 0,},
		{oldViewport.Width,oldViewport.Height,0, 1, 1, 1,},
		{                0,oldViewport.Height,0, 1, 0, 1,},
	};

	//---------------------------------------------------------
	// 렌더
	//---------------------------------------------------------
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
    {
		// 렌더링타겟 변경
		m_pd3dDevice->SetRenderTarget(0, m_pSurfBg);
		m_pd3dDevice->SetDepthStencilSurface(m_pZBg);
		m_pd3dDevice->SetViewport(&viewport);
		m_pd3dDevice->Clear(0L, NULL
				, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
				, D3DCOLOR_RGBA(0,0,0,0), 1.0f, 0L);

		//-------------------------------------------------
		// 배경렌더
		//-------------------------------------------------
		m_pd3dDevice->SetTransform( D3DTS_WORLD,  &m_mWorld);
		m_pd3dDevice->SetTransform( D3DTS_VIEW,  &m_mView );
		m_pd3dDevice->SetTransform( D3DTS_PROJECTION,  &m_mProj );

		TSS( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
		TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		m_pMeshBg->Render( m_pd3dDevice );
		
		//-------------------------------------------------
		// 배경을 프레임버퍼에 렌더
		//-------------------------------------------------
		// 렌더링타겟 복구
		m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
		m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
		m_pd3dDevice->SetViewport(&oldViewport);

		RS( D3DRS_ZENABLE, FALSE);
		m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
		m_pd3dDevice->SetTexture( 0, m_pTexBg );
		m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
							, 2, Vertex, sizeof( TVERTEX ) );
		RS( D3DRS_ZENABLE, TRUE);

		// 비행모델 렌더
		TSS( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
		TSS( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );
		TSS( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
		TSS( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );

		if( m_pEffect != NULL )
		{
			m_pEffect->SetTechnique( m_hTechnique );
			m_pEffect->Begin( NULL, 0 );
			
			// 로컬-월드 행렬
			D3DXMatrixTranslation( &m, 1.0f, 0.0f ,0.0f );
			D3DXMatrixRotationY( &mR,  m_fTime );
			D3DXMatrixTranslation( &mT, 1.0f, 1.0f ,0.0f );
			mL = m * mR * mT * m_mWorld;

			// 광원방향
			D3DXMatrixInverse( &m, NULL, &mL);
			light_pos = D3DXVECTOR4(1,1,-1,0);
			D3DXVec4Transform( &v, &light_pos, &m );
			v.w = 0;
			D3DXVec4Normalize( &v, &v );
			m_pEffect->SetVector( m_hvLightDir, &v );
			// 시점
			D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
			D3DXVec3Transform( &v, &vFromPt, &m );
			m_pEffect->SetVector( m_hvEyePos, &v );
			// 로컬-투영변환행렬
			m = mL * m_mView;
			m_pEffect->SetMatrix( m_hmWV, &m );
			m_pEffect->SetMatrix( m_hmVP, &m_mProj );
			// 1프레임 직전 행렬
			m_pEffect->SetMatrix( m_hmLastWV, &m_mLastWV );
			m_mLastWV = m;

			for(int pass = 0; pass<2; pass++){
				m_pEffect->Pass( pass );// 패스를 바꿔서 출력
				switch(pass){
				case 0:
					// 렌더링타겟 변경
					m_pd3dDevice->SetRenderTarget(0, m_pSurfObj);
					m_pd3dDevice->SetDepthStencilSurface(m_pZObj);
					m_pd3dDevice->Clear(0L, NULL
							, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
							, D3DCOLOR_RGBA(0,0,0,0), 1.0f, 0L);
					break;
				case 1:
					// 렌더링타겟 변경
					m_pd3dDevice->SetRenderTarget(0, m_pSurfBg);
					m_pd3dDevice->SetDepthStencilSurface(m_pZBg);
					m_pd3dDevice->Clear(0L, NULL
							, D3DCLEAR_TARGET
							, D3DCOLOR_RGBA(0,0,0,0), 1.0f, 0L);

					m_pEffect->SetTexture(m_htSrcMap, m_pTexObj);
					break;
				}
				// 모델렌더
				pMtrl = m_pMesh->m_pMaterials;
				for( i=0; i<m_pMesh->m_dwNumMaterials; i++ ) {
					v.x = pMtrl->Diffuse.r;
					v.y = pMtrl->Diffuse.g;
					v.z = pMtrl->Diffuse.b;
					v.w = pMtrl->Diffuse.a;
					m_pEffect->SetVector( m_hvCol, &v );
					m_pMesh->m_pLocalMesh->DrawSubset( i );  // 렌더
					pMtrl++;
				}
			}

			m_pEffect->End();
		}
		// 렌더링타겟 복구
		m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
		m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
		m_pd3dDevice->SetViewport(&oldViewport);
		
		// 생성한 화면 합성
		RS( D3DRS_ZENABLE, FALSE);
		RS( D3DRS_ALPHABLENDENABLE, TRUE);
		RS( D3DRS_SRCBLEND,   D3DBLEND_ONE);
		RS( D3DRS_DESTBLEND,  D3DBLEND_INVSRCALPHA);
		TSS( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
		TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		TSS( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
		TSS( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );

		m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
		m_pd3dDevice->SetTexture( 0, m_pTexBg );
		m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
							, 2, Vertex, sizeof( TVERTEX ) );

		RS( D3DRS_ZENABLE, TRUE);
		RS( D3DRS_ALPHABLENDENABLE, FALSE);

		RenderText();				// 도움말 출력

#if 0 // 디버그용 텍스처 출력
		{
		m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,	D3DTOP_SELECTARG1);
		m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,	D3DTA_TEXTURE);
		m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
		m_pd3dDevice->SetVertexShader(NULL);
		m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
		m_pd3dDevice->SetPixelShader(0);
		float scale = 128.0f;
		for(DWORD i=0; i<2; i++){
			TVERTEX Vertex[4] = {
				// x  y  z rhw tu tv
				{    0,(i+0)*scale,0, 1, 0, 0,},
				{scale,(i+0)*scale,0, 1, 1, 0,},
				{scale,(i+1)*scale,0, 1, 1, 1,},
				{    0,(i+1)*scale,0, 1, 0, 1,},
			};
			if(0==i) m_pd3dDevice->SetTexture( 0, m_pTexObj );
			if(1==i) m_pd3dDevice->SetTexture( 0, m_pTexBg  );
			m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
		}
		}
#endif		
        m_pd3dDevice->EndScene();	// 렌더종료
    }

	pOldBackBuffer->Release();
	pOldZBuffer->Release();
	
	Sleep(50);

    return S_OK;
}
コード例 #10
0
ファイル: GraphicalPlane.cpp プロジェクト: nuponsalt/Game001
	void GraphicalPlane::Render(const Camera* camera) 
	{
		// 描画しないならここで関数終了
		if(!_renders)
			return;
		// 分割読み込みした場合の画像範囲選択
		if(_previousNumber != _number)
		{
			Vertex* vertex;
			_mesh->GetMesh()->LockVertexBuffer( 0, (void**)&vertex );
			vertex[0]._uv.x = (float)_rects[_number].left		/	_textures[0]->GetImageInfo().Width;
			vertex[0]._uv.y = (float)_rects[_number].bottom	/	_textures[0]->GetImageInfo().Height;
			vertex[1]._uv.x = (float)_rects[_number].right	/	_textures[0]->GetImageInfo().Width;
			vertex[1]._uv.y = (float)_rects[_number].bottom	/	_textures[0]->GetImageInfo().Height;
			vertex[2]._uv.x = (float)_rects[_number].left		/	_textures[0]->GetImageInfo().Width;
			vertex[2]._uv.y = (float)_rects[_number].top		/	_textures[0]->GetImageInfo().Height;
			vertex[3]._uv.x = (float)_rects[_number].right	/	_textures[0]->GetImageInfo().Width;
			vertex[3]._uv.y = (float)_rects[_number].top		/	_textures[0]->GetImageInfo().Height;
			_mesh->GetMesh()->UnlockIndexBuffer();
			_previousNumber = _number;
		}
		// ワールド行列設定
		Matrix SclMtx, RotMtx, PosMtx, WldMtx, WVPMtx;
		// 拡縮
		D3DXMatrixScaling(&SclMtx, _scale.x, _scale.y, _scale.z);
		// 回転
		// クォータニオンか回転行列かXYZ指定か
		this->Evaluate();
		RotMtx = _worldRotationMatrix;
		// ビルボードの場合
		if(_isBillBoard)
		{
			Vector3 cameraPosition = camera->GetEye() ;
			GetBillBoardRotation(&_position, &cameraPosition, &RotMtx);
		}
		// 位置
		D3DXMatrixTranslation(&PosMtx, _position.x, _position.y, _position.z);
		// カリングを設定
		GraphicsManager::_device->SetRenderState(D3DRS_CULLMODE, _cullingState);
		// デバッグ用
		//GraphicsManager::_device->SetRenderState( D3DRS_FILLMODE, D3DFILL_WIREFRAME );
		// シェーダを使用する場合カメラのビュー行列(0)、プロジェクション行列(1)をワールド行列に合成
		WldMtx = SclMtx * RotMtx * PosMtx;
		WVPMtx = WldMtx * camera->GetMatrix(ViewBehavior::VIEW) * camera->GetMatrix(ViewBehavior::PROJECTION);
		// カメラの座標をシェーダに使用するための行列変換
		Matrix CamMtx = WldMtx * camera->GetMatrix(ViewBehavior::VIEW);
		D3DXMatrixInverse(&CamMtx, NULL, &CamMtx);
		Vector4 EyePos = Vector4(
			camera->GetEye().x, 
			camera->GetEye().y, 
			camera->GetEye().z, 
			1
			);
		EyePos.Transform(CamMtx);
		D3DXVec4Normalize((D3DXVECTOR4*)&EyePos, (D3DXVECTOR4*)&EyePos);
		// シェーダ設定
		_shader->SetTechnique();
		// シェーダにワールド * ビュー * プロジェクション行列を渡す
		_shader->SetWVPMatrix(WVPMtx);
		// シェーダー特有の値の設定
		_shader->ApplyEffect(RotMtx, EyePos);

		HRESULT hr;
		// 3D モデルのパーツ分ループして描画
		for(size_t i = 0 ; i < _mesh->GetMaterialNumber(); i++)
		{
			// テクスチャが存在しない場合のカラー
			D3DXVECTOR4 vec4 = D3DXVECTOR4(1.0,1.0,1.0,1.0);
			// 格パーツに対応するテクスチャを設定
			// シェーダにテクスチャを渡す
			if(NULL != _textures[i])
			{
				LPDIRECT3DTEXTURE9 texture = _textures[i]->GetTextureData();
				// シェーダにカラーを渡す
				_shader->SetColor(_colorRGBA);
				_shader->SetTexture(texture);
			}else
				_shader->SetColor(vec4);

			// シェーダの使用開始
			_shader->BeginShader();
			// シェーダのパス設定
			_shader->BeginPass(_addsBlend);
			// パーツの描画	
			if(SUCCEEDED(GraphicsManager::_device->BeginScene()))
			{
				_mesh->GetMesh()->DrawSubset(i); 
				V(GraphicsManager::_device->EndScene());
			}
			// パス終了
			_shader->EndPass();
			// シェーダ終了
			_shader->EndShader();
		}
	}
コード例 #11
0
ファイル: Test_Math_Vector4.cpp プロジェクト: lriki/Lumino
//------------------------------------------------------------------------------
TEST_F(Test_Vector4, Basic)
{
	Vector4 sample1(1, 2, 3, 4);

	// コンストラクタ
	{
		Vector4 v1;
		Vector4 v2(1, 2, 3, 4);
		Vector4 v3(Vector2(1, 2), 3, 4);
		Vector4 v4(Vector3(1, 2, 3), 4);
		ASSERT_VEC4_NEAR(0, 0, 0, 0, v1);
		ASSERT_VEC4_NEAR(1, 2, 3, 4, v2);
		ASSERT_VEC4_NEAR(1, 2, 3, 4, v3);
		ASSERT_VEC4_NEAR(1, 2, 3, 4, v4);
	}
	// this->Set
	{
		Vector4 v1;
		v1.set(1, 2, 3, 4);
		ASSERT_VEC4_NEAR(1, 2, 3, 4, v1);
	}
	// this->xy
	{
		Vector2 v = sample1.xy();
		ASSERT_VEC2_NEAR(1, 2, v);
	}
	// this->xyz
	{
		Vector3 v = sample1.xyz();
		ASSERT_VEC3_NEAR(1, 2, 3, v);
	}
	// this->GetLength / lengthSquared
	{
		ASSERT_NEAR(5.477226, sample1.length(), LN_FLOAT_THRESHOLD);
		ASSERT_NEAR(30.000000, sample1.lengthSquared(), LN_FLOAT_THRESHOLD);
	}
	// this->Clamp
	{
		Vector4 v1(1, 2, 3, 4);
		v1.clamp(Vector4(0, 4, 2, 3), Vector4(0.5, 5, 3, 8));
		ASSERT_VEC4_NEAR(0.5, 4, 3, 4, v1);

		Vector4 v2(1, 2, 3, 4);
		v2.clamp(2, 3);
		ASSERT_VEC4_NEAR(2, 2, 3, 3, v2);
	}
	// this->IsNaNOrInf
	{
		Vector4 v(1, 2, 3, 4);
		ASSERT_FALSE(v.isNaNOrInf());
		volatile  float d = 0.0f;
		v.x /= d;
		ASSERT_TRUE(v.isNaNOrInf());
	}

	// Vector4::normalize
	{
		Vector4 v1 = Vector4::normalize(sample1);
		ASSERT_VEC4_NEAR(0.182574, 0.365148, 0.547723, 0.730297, v1);
	}
	// Vector4::dot
	{
		Vector4 v1(5, 6, 7, 8);
		float d = Vector4::dot(sample1, v1);
		ASSERT_FLOAT_EQ(70.000000, d);
	}
	// Vector4::min / max
	{
		Vector4 v1 = Vector4::min(Vector4(1, 3, 5, 7), Vector4(4, 2, 6, 8));
		ASSERT_VEC4_NEAR(1, 2, 5, 7, v1);

		Vector4 v2 = Vector4::max(Vector4(1, 3, 5, 7), Vector4(4, 2, 6, 8));
		ASSERT_VEC4_NEAR(4, 3, 6, 8, v2);
	}
	// Vector4::transform
	{
		Matrix m = Matrix::makeRotationYawPitchRoll(1, 2, 3);
		Vector4 v1 = Vector4::transform(sample1, m);
		ASSERT_VEC4_NEAR(-3.144919, -1.962654, -0.507415, 4.000000, v1);
	}
	// Vector4::Lerp()
	{
		Vector4 v1 = Vector4::lerp(
			Vector4(1, 2, 3, 4),
			Vector4(3, 4, 7, 8),
			0.75);
		ASSERT_VEC4_NEAR(2.500000, 3.500000, 6.000000, 7.000000, v1);
	}
	// Vector4::Hermite()
	{
		Vector4 v1 = Vector4::hermite(
			Vector4(1, 2, 3, 4),
			Vector4(3, 4, 7, 8),
			Vector4(0.3f, 0.4f, -0.5f, -0.7f),
			Vector4(0.03f, 0.04f, -1.5f, 2.5f),
			0.75);
		ASSERT_VEC4_NEAR(0.545781, 0.831875, 0.585938, 0.057813, v1);
	}
	// Vector4::CatmullRom()
	{
		Vector4 v1 = Vector4::catmullRom(
			Vector4(1, 2, 3, 4),
			Vector4(3, 4, 7, 8),
			Vector4(0.3f, 0.4f, -0.5f, -0.7f),
			Vector4(0.03f, 0.04f, -1.5f, 2.5f),
			0.75);
		ASSERT_VEC4_NEAR(0.914297, 1.203437, 1.187500, 0.935937, v1);
	}

	// assign operator
	{
		Vector4 v1;

		v1.set(1, 2, 3, 4);
		v1 += Vector4(1, 2, 3, 4);
		ASSERT_VEC4_NEAR(2, 4, 6, 8, v1);
		v1.set(1, 2, 3, 4);
		v1 += 5;
		ASSERT_VEC4_NEAR(6, 7, 8, 9, v1);

		v1.set(1, 2, 3, 4);
		v1 -= Vector4(1, 2, 3, 4);
		ASSERT_VEC4_NEAR(0, 0, 0, 0, v1);
		v1.set(1, 2, 3, 4);
		v1 -= 5;
		ASSERT_VEC4_NEAR(-4, -3, -2, -1, v1);

		v1.set(1, 2, 3, 4);
		v1 *= Vector4(5, 6, 7, 8);
		ASSERT_VEC4_NEAR(5, 12, 21, 32, v1);
		v1.set(1, 2, 3, 4);
		v1 *= 5;
		ASSERT_VEC4_NEAR(5, 10, 15, 20, v1);

		v1.set(1, 2, 3, 4);
		v1 /= Vector4(1, 2, 3, 4);
		ASSERT_VEC4_NEAR(1, 1, 1, 1, v1);
		v1.set(10, 20, 30, 40);
		v1 /= 5;
		ASSERT_VEC4_NEAR(2, 4, 6, 8, v1);
	}
	// binary operator
	{
		Vector4 v1;

		v1 = Vector4(1, 2, 3, 4) + Vector4(1, 2, 3, 4);
		ASSERT_VEC4_NEAR(2, 4, 6, 8, v1);
		v1 = Vector4(1, 2, 3, 4) + 5;
		ASSERT_VEC4_NEAR(6, 7, 8, 9, v1);
		v1 = 6 + Vector4(1, 2, 3, 4);
		ASSERT_VEC4_NEAR(7, 8, 9, 10, v1);

		v1 = Vector4(1, 2, 3, 4) - Vector4(1, 2, 3, 4);
		ASSERT_VEC4_NEAR(0, 0, 0, 0, v1);
		v1 = Vector4(1, 2, 3, 4) - 5;
		ASSERT_VEC4_NEAR(-4, -3, -2, -1, v1);
		v1 = 6 - Vector4(1, 2, 3, 4);
		ASSERT_VEC4_NEAR(5, 4, 3, 2, v1);

		v1 = Vector4(1, 2, 3, 4) * Vector4(5, 6, 7, 8);
		ASSERT_VEC4_NEAR(5, 12, 21, 32, v1);
		v1 = Vector4(1, 2, 3, 4) * 5;
		ASSERT_VEC4_NEAR(5, 10, 15, 20, v1);
		v1 = 6 * Vector4(1, 2, 3, 4);
		ASSERT_VEC4_NEAR(6, 12, 18, 24, v1);

		v1 = Vector4(10, 20, 30, 40) / Vector4(10, 20, 30, 40);
		ASSERT_VEC4_NEAR(1, 1, 1, 1, v1);
		v1 = Vector4(10, 20, 30, 40) / 5;
		ASSERT_VEC4_NEAR(2, 4, 6, 8, v1);
		v1 = 20 / Vector4(1, 2, 4, 5);
		ASSERT_VEC4_NEAR(20, 10, 5, 4, v1);

		v1.set(2, 4, 6, 8);
		ASSERT_TRUE(v1 == Vector4(2, 4, 6, 8));
		ASSERT_FALSE(v1 != Vector4(2, 4, 6, 8));
	}
	// unary operator
	{
		Vector4 v1(1, 2, 3, 4);
		v1 = -v1;
		ASSERT_VEC4_NEAR(-1, -2, -3, -4, v1);
	}

#ifdef D3DX9_TEST
	D3DXVECTOR4 dxsample1(1, 2, 3, 4);

	// D3DXVec4Length
	{
		//dumpFLOAT("D3DXVec4Length", D3DXVec4Length(&dxsample1));
		//dumpFLOAT("D3DXVec4LengthSq", D3DXVec4LengthSq(&dxsample1));
	}
	// D3DXVec4Normalize
	{
		D3DXVECTOR4 v1;
		D3DXVec4Normalize(&v1, &dxsample1);
		//dumpD3DXVECTOR4("D3DXVec4Normalize", v1);
	}
	// D3DXVec4Dot
	{
		D3DXVECTOR4 v1(5, 6, 7, 8);
		//dumpFLOAT("D3DXVec4Dot", D3DXVec4Dot(&dxsample1, &v1));
	}
	// D3DXVec4Transform
	{
		D3DXMATRIX m1;
		D3DXMatrixRotationYawPitchRoll(&m1, 1, 2, 3);
		D3DXVECTOR4 v1;
		D3DXVec4Transform(&v1, &dxsample1, &m1);
	}
	// D3DXVec4Lerp
	{
		D3DXVECTOR4 dxv;
		D3DXVECTOR4 dxv1(1, 2, 3, 4);
		D3DXVECTOR4 dxv2(3, 4, 7, 8);
		D3DXVec4Lerp(&dxv, &dxv1, &dxv2, 0.75);
		//dumpD3DXVECTOR4("D3DXVec4Lerp", dxv);
	}
	// D3DXVec4Hermite
	{
		D3DXVECTOR4 dxv;
		D3DXVECTOR4 dxv1(1, 2, 3, 4);
		D3DXVECTOR4 dxv2(3, 4, 7, 8);
		D3DXVECTOR4 dxv3(0.3f, 0.4f, -0.5f, -0.7f);
		D3DXVECTOR4 dxv4(0.03f, 0.04f, -1.5f, 2.5f);
		D3DXVec4Hermite(&dxv, &dxv1, &dxv2, &dxv3, &dxv4, 0.75);
		//dumpD3DXVECTOR4("D3DXVec4Hermite", dxv);
	}
	// D3DXVec4CatmullRom
	{
		D3DXVECTOR4 dxv;
		D3DXVECTOR4 dxv1(1, 2, 3, 4);
		D3DXVECTOR4 dxv2(3, 4, 7, 8);
		D3DXVECTOR4 dxv3(0.3f, 0.4f, -0.5f, -0.7f);
		D3DXVECTOR4 dxv4(0.03f, 0.04f, -1.5f, 2.5f);
		D3DXVec4CatmullRom(&dxv, &dxv1, &dxv2, &dxv3, &dxv4, 0.75);
		//dumpD3DXVECTOR4("D3DXVec4CatmullRom", dxv);
	}
	
#endif
}
コード例 #12
0
HRESULT RacorX8::RestoreDeviceObjects()
{
	HRESULT hr;

	IDirect3DDevice8* device;
	hr = m_spD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, m_hWnd, m_iVP, &m_dpps, &device);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateDevice failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spDevice.reset(device, [](IDirect3DDevice8* device) { device->Release(); });

	//CreateSphere();

	LoadXFile("sphere.x");

	IDirect3DVertexBuffer8* vbNormal;
	hr = m_spDevice->CreateVertexBuffer(m_iNumVertices * 2 * sizeof SimpleVertex, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_MANAGED, &vbNormal);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateVertexBuffer failed!", L"Error", 0);
		return E_FAIL;
	}
	hr = CreateNormal<ShaderVertex, SimpleVertex>(m_spVB.get(), vbNormal);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateNormal failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spVBNormal.reset(vbNormal, [](IDirect3DVertexBuffer8* vbNormal){vbNormal->Release(); });

	IDirect3DVertexBuffer8* vbTangent;
	hr = m_spDevice->CreateVertexBuffer(m_iNumVertices * 2 * sizeof SimpleVertex, D3DUSAGE_WRITEONLY, D3DFVF_XYZ, D3DPOOL_MANAGED, &vbTangent);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateVertexBuffer failed!", L"Error", 0);
		return E_FAIL;
	}
	hr = CreateTangent<ShaderVertex, SimpleVertex>(m_spVB.get(), vbTangent);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateTangent failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spVBTangent.reset(vbTangent, [](IDirect3DVertexBuffer8* vbTangent){vbTangent->Release(); });

	//DWORD dwDecl[MAX_FVF_DECL_SIZE];
	//ZeroMemory(dwDecl, sizeof dwDecl);
	//pSphere->GetDeclaration(dwDecl);
	DWORD decl[] = {
		D3DVSD_STREAM(0),
		D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),
		D3DVSD_REG(D3DVSDE_NORMAL, D3DVSDT_FLOAT3),
		D3DVSD_REG(D3DVSDE_TEXCOORD0, D3DVSDT_FLOAT2),
		D3DVSD_REG(D3DVSDE_TEXCOORD1, D3DVSDT_FLOAT3),
		D3DVSD_END()
	};
	hr = CreateVSFromBinFile(m_spDevice.get(), decl, L"specular.vso", &m_dwVSH);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}

	hr = CreatePSFromBinFile(m_spDevice.get(), L"specular.pso", &m_dwPSH);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}

	hr = CreatePSFromBinFile(m_spDevice.get(), L"specularBump.pso", &m_dwPSHBump);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}

	if (m_bPS14Avaliable)
	{
		hr = CreatePSFromBinFile(m_spDevice.get(), L"specularBump14.pso", &m_dwPSHBump14);
		if (FAILED(hr))
		{
			MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0);
			return E_FAIL;
		}
	}
	DWORD declLine[] = {
		D3DVSD_STREAM(0),
		D3DVSD_REG(D3DVSDE_POSITION, D3DVSDT_FLOAT3),
		D3DVSD_END()
	};
	hr = CreateVSFromBinFile(m_spDevice.get(), declLine, L"line.vso", &m_dwVSHLine);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreateVSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}
	hr = CreatePSFromBinFile(m_spDevice.get(), L"line.pso", &m_dwVPHLine);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"CreatePSFromBinFile failed!", L"Error", 0);
		return E_FAIL;
	}

	IDirect3DTexture8* color_map;
	hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("earth.bmp"), &color_map);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spColorMap.reset(color_map, [](IDirect3DTexture8* color_map){ color_map->Release(); });

	IDirect3DTexture8* heightMap;
	hr = D3DXCreateTextureFromFile(m_spDevice.get(), _T("earthbump.bmp"), &heightMap);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXCreateTextureFromFile failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spHeightMap.reset(heightMap, [](IDirect3DTexture8* heightMap) { heightMap->Release(); });

	D3DSURFACE_DESC desc;
	m_spHeightMap->GetLevelDesc(0, &desc);

	IDirect3DTexture8* normalMap;
	hr = D3DXCreateTexture(m_spDevice.get(), desc.Width, desc.Height, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &normalMap);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXCreateTexture failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spNormalMap.reset(normalMap, [](IDirect3DTexture8* normalMap){ normalMap->Release(); });

	hr = D3DXComputeNormalMap(m_spNormalMap.get(), m_spHeightMap.get(), NULL, 0, D3DX_CHANNEL_RED, 10);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXComputeNormalMap failed!", L"Error", 0);
		return E_FAIL;
	}
	
	IDirect3DTexture8* power;
	hr = D3DXCreateTexture(m_spDevice.get(), 256, 256, 0, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, &power);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXCreateTexture failed!", L"Error", 0);
		return E_FAIL;
	}
	m_spPower.reset(power, [](IDirect3DTexture8* power){ power->Release();  });
	FLOAT fPower = 16.0f;
	hr = D3DXFillTexture(power, LightEval, &fPower);
	if (FAILED(hr))
	{
		MessageBox(m_hWnd, L"D3DXFillTexture failed!", L"Error", 0);
		return E_FAIL;
	}

	m_spDevice->SetViewport(&m_Viewport);

	m_spDevice->SetRenderState(D3DRS_DITHERENABLE, TRUE);
	m_spDevice->SetRenderState(D3DRS_ZENABLE, TRUE);
	
	m_spDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_CCW);

	m_spDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
	m_spDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, FALSE);

	//m_spDevice->SetRenderState(D3DRS_SHADEMODE, D3DSHADE_GOURAUD);
	//m_spDevice->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
	//m_spDevice->SetRenderState(D3DRS_SPECULARENABLE, TRUE);

	//m_spDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_MODULATE);
	//m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	//m_spDevice->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	//m_spDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_DISABLE);
	//m_spDevice->SetTextureStageState(1, D3DTSS_ALPHAOP, D3DTOP_DISABLE);
	m_spDevice->SetTextureStageState(0, D3DTSS_MINFILTER, D3DTEXF_LINEAR);
	m_spDevice->SetTextureStageState(0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR);
	//m_spDevice->SetTextureStageState(0, D3DTSS_MIPFILTER, D3DTEXF_LINEAR);
	//m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
	//m_spDevice->SetTextureStageState(0, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);

	m_spDevice->SetTextureStageState(2, D3DTSS_ADDRESSU, D3DTADDRESS_CLAMP);
	m_spDevice->SetTextureStageState(2, D3DTSS_ADDRESSV, D3DTADDRESS_CLAMP);

	D3DXVECTOR4 vLight(0.0f, 0.0f, 1.0f, 0.0f);
	D3DXVec4Normalize(&vLight, &vLight);
	m_spDevice->SetVertexShaderConstant(12, &vLight, 1);

	D3DXVECTOR4 half(0.5f, 0.5f, 0.5f, 0.5f);
	m_spDevice->SetVertexShaderConstant(33, &half, 1);

	return S_OK;
}
コード例 #13
0
ファイル: Terrain.cpp プロジェクト: Siergiej29/PhysX-Mgr
bool Terrain::HeightMapLoad(char* filename, float sx, float sz, float maxy)
{
	//FILE *filePtr;							// Point to the current position in the file
	//BITMAPFILEHEADER bitmapFileHeader;		// Structure which stores information about file
	//BITMAPINFOHEADER bitmapInfoHeader;		// Structure which stores information about image
	int imageSize, index;
	unsigned char height;

	// Open the file
	//filePtr = fopen(filename,"rb");
	//if (filePtr == NULL)
	//	return 0;

	dx = sz;
	dz = sz;
	dy = maxy;

	

	// Get the width and height (width and length) of the image
	hminfo.terrainWidth = 145;// bitmapInfoHeader.biWidth;
	hminfo.terrainHeight = 145;// bitmapInfoHeader.biHeight;


	// Initialize the heightMap array (stores the vertices of our terrain)
	hminfo.heightMap = new IntV3[hminfo.terrainWidth * hminfo.terrainHeight];

	// We use a greyscale image, so all 3 rgb values are the same, but we only need one for the height
	// So we use this counter to skip the next two components in the image data (we read R, then skip BG)
	int k=0;

	// Read the image data into our heightMap array
	for(int j=0; j< hminfo.terrainHeight; j++)
	{
		for(int i=0; i< hminfo.terrainWidth; i++)
		{
			height = rand()%50+1;

			index = ( hminfo.terrainWidth * (hminfo.terrainHeight - 1 - j)) + i;

			hminfo.heightMap[index].x = i - (hminfo.terrainWidth - 1)/2;
			hminfo.heightMap[index].y = height;
			hminfo.heightMap[index].z = j - (hminfo.terrainHeight - 1)/2;

			k+=3;
		}
		k++;
	}


	int cols = hminfo.terrainWidth;
	int rows = hminfo.terrainHeight;

	//Create the grid
	NumVertices = 2 * rows * cols;
	NumFaces  = (rows-1)*(cols-1)*2;

	v = new struct HeightFieldVertex[NumVertices];

	for(DWORD i = 0; i < rows; ++i)
	{
		for(DWORD j = 0; j < cols; ++j)
		{
			v[i*cols+j].pos.x = hminfo.heightMap[i*cols+j].x * dx;
			v[i*cols+j].pos.y = (float(hminfo.heightMap[i*cols+j].y)/255.0) * dy;
			v[i*cols+j].pos.z = hminfo.heightMap[i*cols+j].z * dz;
			v[i*cols+j].texCoord = D3DXVECTOR2(j, i);
		}
	}

	indices = new DWORD[NumFaces * 3];

	k = 0;
	for(DWORD i = 0; i < rows-1; i++)
	{
		for(DWORD j = 0; j < cols-1; j++)
		{
			indices[k]   = i*cols+j;		// Bottom left of quad
			indices[k+1] = i*cols+j+1;		// Bottom right of quad
			indices[k+2] = (i+1)*cols+j;	// Top left of quad
			indices[k+3] = (i+1)*cols+j;	// Top left of quad
			indices[k+4] = i*cols+j+1;		// Bottom right of quad
			indices[k+5] = (i+1)*cols+j+1;	// Top right of quad

			k += 6; // next quad
		}
	}

	//normals & tangents
    std::vector<D3DXVECTOR3> tempNormal;

    //normalized and unnormalized normals
    D3DXVECTOR3 unnormalized(0.0f, 0.0f, 0.0f);

    //tangent stuff
    std::vector<D3DXVECTOR3> tempTangent;

    D3DXVECTOR3 tangent(0.0f, 0.0f, 0.0f);
    float tcU1, tcV1, tcU2, tcV2;

    //Used to get vectors (sides) from the position of the verts
    float vecX, vecY, vecZ;

    //Two edges of our triangle
    D3DXVECTOR3 edge1(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 edge2(0.0f, 0.0f, 0.0f);

    //Compute face normals
    //And Tangents
    for(int i = 0; i < NumFaces; ++i)
    {
        //Get the vector describing one edge of our triangle (edge 0,2)
        vecX = v[indices[(i*3)+1]].pos.x - v[indices[(i*3)]].pos.x;
        vecY = v[indices[(i*3)+1]].pos.y - v[indices[(i*3)]].pos.y;
        vecZ = v[indices[(i*3)+1]].pos.z - v[indices[(i*3)]].pos.z;       
        edge1 = D3DXVECTOR3(vecX, vecY, vecZ);    //Create our first edge

        //Get the vector describing another edge of our triangle (edge 2,1)
        vecX = v[indices[(i*3)+2]].pos.x - v[indices[(i*3)]].pos.x;
        vecY = v[indices[(i*3)+2]].pos.y - v[indices[(i*3)]].pos.y;
        vecZ = v[indices[(i*3)+2]].pos.z - v[indices[(i*3)]].pos.z;     
        edge2 = D3DXVECTOR3(vecX, vecY, vecZ);    //Create our second edge

        //Cross multiply the two edge vectors to get the un-normalized face normal
		D3DXVec3Cross(&unnormalized, &edge1, &edge2);
        tempNormal.push_back(unnormalized);

        //Find first texture coordinate edge 2d vector
        tcU1 = v[indices[(i*3)+1]].texCoord.x - v[indices[(i*3)]].texCoord.x;
        tcV1 = v[indices[(i*3)+1]].texCoord.y - v[indices[(i*3)]].texCoord.y;

        //Find second texture coordinate edge 2d vector
        tcU2 = v[indices[(i*3)+2]].texCoord.x - v[indices[(i*3)]].texCoord.x;
        tcV2 = v[indices[(i*3)+2]].texCoord.y - v[indices[(i*3)]].texCoord.y;

        //Find tangent using both tex coord edges and position edges
        tangent.x = (tcV2 * edge1.x - tcV1 * edge2.x)  / (tcU1 * tcV2 - tcU2 * tcV1);
        tangent.y = (tcV2 * edge1.y - tcV1 * edge2.y)  / (tcU1 * tcV2 - tcU2 * tcV1);
        tangent.z = (tcV2 * edge1.z - tcV1 * edge2.z)  / (tcU1 * tcV2 - tcU2 * tcV1);

        tempTangent.push_back(tangent);
    }

    //Compute vertex normals (normal Averaging)
    D3DXVECTOR4 normalSum(0.0f, 0.0f, 0.0f, 0.0f);
    D3DXVECTOR4 tangentSum(0.0f, 0.0f, 0.0f, 0.0f);
    int facesUsing = 0;
    float tX, tY, tZ;   //temp axis variables

    //Go through each vertex
    for(int i = 0; i < NumVertices; ++i)
    {
        //Check which triangles use this vertex
        for(int j = 0; j < NumFaces; ++j)
        {
            if(indices[j*3] == i ||
                indices[(j*3)+1] == i ||
                indices[(j*3)+2] == i)
            {
                tX = normalSum.x + tempNormal[j].x;
                tY = normalSum.y + tempNormal[j].y;
                tZ = normalSum.z + tempNormal[j].z;

                normalSum = D3DXVECTOR4(tX, tY, tZ, 0.0f);  //If a face is using the vertex, add the unormalized face normal to the normalSum
    
                facesUsing++;
            }
        }
        //Get the actual normal by dividing the normalSum by the number of faces sharing the vertex
        normalSum = normalSum / facesUsing;

		facesUsing = 0;
        //Check which triangles use this vertex
        for(int j = 0; j < NumFaces; ++j)
        {
            if(indices[j*3] == i ||
                indices[(j*3)+1] == i ||
                indices[(j*3)+2] == i)
            {
   
                //We can reuse tX, tY, tZ to sum up tangents
                tX = tangentSum.x + tempTangent[j].x;
                tY = tangentSum.y + tempTangent[j].y;
                tZ = tangentSum.z + tempTangent[j].z;

                tangentSum = D3DXVECTOR4(tX, tY, tZ, 0.0f); //sum up face tangents using this vertex

                facesUsing++;
            }
        }
        //Get the actual normal by dividing the normalSum by the number of faces sharing the vertex
        tangentSum = tangentSum / facesUsing;

        //Normalize the normalSum vector and tangent
		D3DXVec4Normalize(&normalSum, &normalSum);
		D3DXVec4Normalize(&tangentSum, &tangentSum);

        //Store the normal and tangent in our current vertex
        v[i].normal.x = normalSum.x;
        v[i].normal.y = normalSum.y;
        v[i].normal.z = normalSum.z;

        v[i].tangent.x = tangentSum.x;
        v[i].tangent.y = tangentSum.y;
        v[i].tangent.z = tangentSum.z;

		D3DXVECTOR3 bit;

		D3DXVec3Cross(&bit, &v[i].normal, &v[i].tangent);
		v[i].bitangent = -1.0 * bit;

        //Clear normalSum, tangentSum and facesUsing for next vertex
        normalSum = D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f);
        tangentSum = D3DXVECTOR4(0.0f, 0.0f, 0.0f, 0.0f);
        facesUsing = 0;
    }

	////terrain AABB
	//MinX =  -1.0 * dx * (hminfo.terrainWidth - 1)/2;
	//MinY = 0.0;
	//MinZ =  -1.0 * dz * (hminfo.terrainHeight - 1)/2;
	//MaxX =  dx * (hminfo.terrainWidth - 1)/2;
	//MaxY = dy;
	//MaxZ =  dz * (hminfo.terrainHeight - 1)/2;

 	return true;
}
コード例 #14
0
void SpaceShip::Update(float RelativeTime)
{
	m_RelativeTime = RelativeTime;

	if (!m_Moving)
	{
		// Allow friction to slow the ship down
		float NewSpeed = m_Speed - m_Speed * m_Acceleration * m_RelativeTime * 0.2f;

		if (m_Speed > 0)
			m_Speed = (NewSpeed < 0 ? 0 : NewSpeed);
		else
			m_Speed = (NewSpeed > 0 ? 0 : NewSpeed);
	}

	if (!m_Turning)
	{
		// Rotate back to normal
		float NewRot = abs(m_RotZ) - 0.03f * m_RelativeTime;
		if (NewRot < 0)
			NewRot = 0;

		if (m_RotZ > 0)
			m_RotZ = NewRot;
		else
			m_RotZ = -NewRot;
	}

	// Set y-rotation
	m_RotY -= 0.01f * m_RotZ * m_Speed * m_RelativeTime;

	// Rotate ship
	D3DXMATRIX matrix;
	D3DXQuaternionRotationAxis(&m_QuatY, new D3DXVECTOR3(0, 1, 0), m_RotY);
	D3DXQuaternionRotationAxis(&m_QuatZ, new D3DXVECTOR3(0, 0, 1), m_RotZ);

	D3DXVECTOR4 vec = D3DXVECTOR4(0, 0, 1, 1);
	D3DXVec4Transform(&vec, &vec, D3DXMatrixRotationQuaternion(&matrix, &m_QuatY));
	D3DXVec4Normalize(&vec, &vec);

	// Move ship
	Vector3D OldPosition = m_Position;
	D3DXVECTOR4 newvec = vec * m_Speed * m_RelativeTime;
	m_Position += Vector3D(newvec.x, newvec.y, newvec.z);

	FollowTerrain();

	// Compare current position height and new position height
	float Scale = 1.0f;
	if (OldPosition.Y > m_Position.Y) // Ship is going down
		Scale += (OldPosition.Y - m_Position.Y);
	else if (OldPosition.Y < m_Position.Y) // Ship is going up
		Scale -= (m_Position.Y - OldPosition.Y) * 0.1f;

	// Move ship
	vec *= m_Speed * Scale * m_RelativeTime;
	m_Position = OldPosition + Vector3D(vec.x, vec.y, vec.z);

	// Set kilometres per hour
	m_NextKPH++;
	if (m_NextKPH == m_NumKPH)
		m_NextKPH = 0;

	m_KPH[m_NextKPH] = abs((int)(m_Speed * Scale * 100.0f));
	
	FollowTerrain();

	// See if the player is going off the edge
	Rect bounds = m_Terrain->GetBounds();

	if (m_Position.X < bounds.X)
		m_Position.X = bounds.X;
	else if (m_Position.X > bounds.X + bounds.W)
		m_Position.X = bounds.X + bounds.W;

	if (m_Position.Z < bounds.Y)
		m_Position.Z = bounds.Y;
	else if (m_Position.Z > bounds.Y + bounds.H)
		m_Position.Z = bounds.Y + bounds.H;

	// Update camera
	UpdateCamera();

	// Set mesh position
	Matrix pos, rot, out;
	Quaternion quat = m_QuatZ * m_QuatY;
	D3DXMatrixTranslation(&pos, m_Position.X, m_Position.Y, m_Position.Z);
	D3DXMatrixRotationQuaternion(&rot, &quat);
	out = rot * pos;

	m_Mesh->SetWorldMatrix(&out);
}
コード例 #15
0
ファイル: d3dx_vector.hpp プロジェクト: psychi/fragments
/// @copydoc psyq::geometry::vector::normalize
inline D3DXVECTOR4 normalize(D3DXVECTOR4 const& in_vector)
{
    D3DXVECTOR4 local_vector;
    D3DXVec4Normalize(&local_vector, &in_vector);
    return local_vector;
}
コード例 #16
0
ファイル: main.cpp プロジェクト: jjuiddong/Dx9-Shader
//-------------------------------------------------------------
// Name: Render()
// Desc: È­¸é ·»´õ
//-------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
    D3DXMATRIX m, mT, mR, mW, mView, mProj;
	DWORD i, j;
	D3DXVECTOR4 v;
	LPDIRECT3DSURFACE9 pOldBackBuffer, pOldZBuffer;
	D3DVIEWPORT9 oldViewport;
	D3DMATERIAL9 *pMtrl;

	//---------------------------------------------------------
	// ·»´õ
	//---------------------------------------------------------
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
    {
		//-------------------------------------------------
		// ·»´õ¸µÅ¸°Ù º¸Á¸
		//-------------------------------------------------
		m_pd3dDevice->GetRenderTarget(0, &pOldBackBuffer);
		m_pd3dDevice->GetDepthStencilSurface(&pOldZBuffer);
		m_pd3dDevice->GetViewport(&oldViewport);

		if( m_pEffect != NULL ) 
		{
			//-------------------------------------------------
			// ¼ÎÀÌ´õ¼³Á¤
			//-------------------------------------------------
			m_pEffect->SetTechnique( m_hTechnique );
			m_pEffect->Begin( NULL, 0 );
			m_pEffect->Pass( 0 );

			//-------------------------------------------------
			// ·»´õ¸µÅ¸°Ù º¯°æ
			//-------------------------------------------------
			m_pd3dDevice->SetRenderTarget(0, m_pOriginalSurf);
			m_pd3dDevice->SetRenderTarget(1, m_pNormalSurf);
			m_pd3dDevice->SetDepthStencilSurface(m_pMapZ);
			// ºäÆ÷Æ®º¯°æ
			D3DVIEWPORT9 viewport = {0,0      // ÁÂÃø»ó´Ü
							, MAP_WIDTH  // Æø
							, MAP_HEIGHT // ³ôÀÌ
							, 0.0f,1.0f};     // Àü¸é,Èĸé
			m_pd3dDevice->SetViewport(&viewport);

			// ·»´õ¸µÅ¸°Ù Ŭ¸®¾î
			m_pd3dDevice->Clear(0L, NULL
							, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
							, 0xff000000, 1.0f, 0L);

			//-------------------------------------------------
			// 1Æнº:·»´õ¸µÅ¸°Ù »ý¼º
			//-------------------------------------------------

			m = m_mWorld * m_mView * m_mProj;
			m_pEffect->SetMatrix( m_hmWVP, &m );
			
			// ¹è°æ·»´õ
			pMtrl = m_pMeshBg->m_pMaterials;
			for( i=0; i<m_pMeshBg->m_dwNumMaterials; i++ ) {
				m_pEffect->SetTexture(m_htSrcTex
								, m_pMeshBg->m_pTextures[i] );
				m_pMeshBg->m_pLocalMesh->DrawSubset( i );	// •`‰æ
				pMtrl++;
			}

			// ¸ðµ¨·»´õ
			m_pEffect->Pass( 1 );

			D3DXMatrixTranslation( &mT, 0.0f, 0.6f, 0.0f );
			mW = mT * m_mWorld;
			m = mW * m_mView * m_mProj;
			m_pEffect->SetMatrix( m_hmWVP, &m );
			// ±¤¿ø¼³Á¤
			D3DXMatrixInverse( &m, NULL, &mW );
			D3DXVec3Transform( &v, &m_LighPos, &m );
			D3DXVec4Normalize( &v, &v );v.w = 0.5f;
			m_pEffect->SetVector( m_hvDir, &v );

			m_pMesh  ->Render( m_pd3dDevice );
			pMtrl = m_pMesh->m_pMaterials;
			for( j=0; j<m_pMesh->m_dwNumMaterials; j++ ) {
				v.x = pMtrl->Diffuse.r;
				v.y = pMtrl->Diffuse.g;
				v.z = pMtrl->Diffuse.b;
				v.w = pMtrl->Diffuse.a;
				m_pEffect->SetVector( m_hvCol, &v );// Á¤Á¡»ö
				m_pEffect->SetTexture(m_htSrcTex
								, m_pMesh->m_pTextures[j] );
				m_pMesh->m_pLocalMesh->DrawSubset( j );	// ·»´õ
				pMtrl++;
			}
			m_pEffect->End();
		}

		//-----------------------------------------------------
		// ·»´õ¸µÅ¸°Ù º¹±¸
		//-----------------------------------------------------
		m_pd3dDevice->SetRenderTarget(0, pOldBackBuffer);
		m_pd3dDevice->SetRenderTarget(1, NULL);
		m_pd3dDevice->SetDepthStencilSurface(pOldZBuffer);
		m_pd3dDevice->SetViewport(&oldViewport);
		pOldBackBuffer->Release();
		pOldZBuffer->Release();

		//-----------------------------------------------------
		// ±×´ë·Î ºÙÀδÙ
		//-----------------------------------------------------
		RS( D3DRS_ZENABLE, FALSE );
		RS( D3DRS_LIGHTING, FALSE );

		FLOAT w = (FLOAT)oldViewport.Width;
		FLOAT h = (FLOAT)oldViewport.Height;
		TVERTEX Vertex1[4] = {
			//x  y   z    rhw  tu tv
			{ 0, 0, 0.1f, 1.0f, 0, 0,},
			{ w, 0, 0.1f, 1.0f, 1, 0,},
			{ w, h, 0.1f, 1.0f, 1, 1,},
			{ 0, h, 0.1f, 1.0f, 0, 1,},
		};
		m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
		m_pd3dDevice->SetTexture(0, m_pOriginalTex);
		m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
							, 2, Vertex1, sizeof( TVERTEX ) );

		//-----------------------------------------------------
		// À±°ûÀ» ºÙÀδÙ
		//-----------------------------------------------------
		if( m_pEffect != NULL ) 
		{
			//-------------------------------------------------
			// ¼ÎÀÌ´õ¼³Á¤
			//-------------------------------------------------
			m_pEffect->SetTechnique( m_hTechnique );
			m_pEffect->Begin( NULL, 0 );
			m_pEffect->Pass(2);
			
			RS(D3DRS_ALPHABLENDENABLE, TRUE);
			RS(D3DRS_SRCBLEND , D3DBLEND_ZERO);
			RS(D3DRS_DESTBLEND , D3DBLEND_SRCALPHA);

			FLOAT du = 1.5f/MAP_WIDTH;
			FLOAT dv = 1.5f/MAP_HEIGHT;

			T4VERTEX Vertex[4] = {
				//   x    y   z    rhw     tu       tv
				{ 0.0f,   0, 0.1f, 1.0f,  0.0f-du, 0.0f-dv // ÁÂÃø»ó´Ü
										, 0.0f+du, 0.0f+dv // ¿ìÃøÇÏ´Ü
										, 0.0f-du, 0.0f+dv // ÁÂÃøÇÏ´Ü
										, 0.0f+du, 0.0f-dv},//¿ìÃø»ó´Ü
				{    w,   0, 0.1f, 1.0f,  1.0f-du, 0.0f-dv
										, 1.0f+du, 0.0f+dv
										, 1.0f-du, 0.0f+dv
										, 1.0f+du, 0.0f-dv, },
				{    w,   h, 0.1f, 1.0f,  1.0f-du, 1.0f-dv
										, 1.0f+du, 1.0f+dv
										, 1.0f-du, 1.0f+dv
										, 1.0f+du, 1.0f-dv, },
				{ 0.0f,   h, 0.1f, 1.0f,  0.0f-du, 1.0f-dv
										, 0.0f+du, 1.0f+dv
										, 0.0f-du, 1.0f+dv
										, 0.0f+du, 1.0f-dv, },
			};
			m_pEffect->SetTexture( m_htSrcTex,   m_pNormalTex );
			m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX4 );
			m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN
								, 2, Vertex, sizeof( T4VERTEX ) );
			m_pEffect->End();
		}

		RS( D3DRS_ZENABLE, TRUE );
		RS( D3DRS_LIGHTING, TRUE );
		RS(D3DRS_ALPHABLENDENABLE, FALSE);

		RenderText();				// µµ¿ò¸» Ãâ·Â

#if 1 // µð¹ö±×¿ë ÅؽºÃ³ Ãâ·Â
		{
		m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLOROP,	D3DTOP_SELECTARG1);
		m_pd3dDevice->SetTextureStageState(0,D3DTSS_COLORARG1,	D3DTA_TEXTURE);
		m_pd3dDevice->SetTextureStageState(1,D3DTSS_COLOROP,    D3DTOP_DISABLE);
		m_pd3dDevice->SetVertexShader(NULL);
		m_pd3dDevice->SetFVF( D3DFVF_XYZRHW | D3DFVF_TEX1 );
		m_pd3dDevice->SetPixelShader(0);
		float scale = 128.0f;
		for(DWORD i=0; i<1; i++){
			TVERTEX Vertex[4] = {
				// x  y  z rhw tu tv
				{    0,(i+0)*scale,0, 1, 0, 0,},
				{scale,(i+0)*scale,0, 1, 1, 0,},
				{scale,(i+1)*scale,0, 1, 1, 1,},
				{    0,(i+1)*scale,0, 1, 0, 1,},
			};
			if(0==i) m_pd3dDevice->SetTexture( 0, m_pOriginalTex );
			m_pd3dDevice->DrawPrimitiveUP( D3DPT_TRIANGLEFAN, 2, Vertex, sizeof( TVERTEX ) );
		}
		}
#endif		

        m_pd3dDevice->EndScene();	// ·»´õÁ¾·á
    }

    return S_OK;
}
コード例 #17
0
ファイル: float4.cpp プロジェクト: JohanMes/JohanEngine
float4 float4::Normalize() {
	D3DXVec4Normalize(this,this);
	return *this;
}
コード例 #18
0
ファイル: LnXSkinIns.cpp プロジェクト: GALICSOFT/glc220_src
void CLcXSkinIns::Render()
{
	HRESULT hr=-1;

	LPDIRECT3DDEVICE9 pDev = (LPDIRECT3DDEVICE9)LcDev_GetD3Device();


	CLcXSkinSrc*	pOrg = (CLcXSkinSrc*)m_pOrg;

	ID3DXEffect*	pEft	= pOrg->GetEffect();

	// Setup the projection matrix
	D3DXMATRIX matProj;
	pDev->GetTransform(D3DTS_PROJECTION, &matProj);




	D3DLIGHT9 light;
	D3DXVECTOR3 vecLightDirUnnormalized(0.0f, -1.0f, 1.0f);
	ZeroMemory( &light, sizeof(D3DLIGHT9) );
	light.Type        = D3DLIGHT_DIRECTIONAL;
	light.Diffuse.r   = 1.0f;
	light.Diffuse.g   = 1.0f;
	light.Diffuse.b   = 1.0f;
	D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecLightDirUnnormalized );
	light.Position.x   = 0.0f;
	light.Position.y   = -1.0f;
	light.Position.z   = 1.0f;
	light.Range        = 1000.0f;

	pDev->SetLight(0, &light );
	pDev->LightEnable(0, TRUE );
	
	
	// Set Light for vertex shader
	D3DXVECTOR4 vLightDir( 0.0f, 1.0f, -1.0f, 0.0f );
	D3DXVec4Normalize( &vLightDir, &vLightDir );
	
	
	// for HLSL
	{
		pEft->SetMatrix( "mViewProj", &matProj);
		pEft->SetVector( "lhtDir", &vLightDir);
	}
	
	
	
	// for shader
	{
		// set the projection matrix for the vertex shader based skinning method
		D3DXMatrixTranspose(&matProj, &matProj);
		pDev->SetVertexShaderConstantF(2, (float*)&matProj, 4);
		pDev->SetVertexShaderConstantF(1, (float*)&vLightDir, 1);
	}

	if(m_pAC)
		m_pAC->AdvanceTime(m_fElapse, NULL);

	pOrg->UpdateFrameMatrices(m_pFrameOrg, &m_mtWld);	
	pOrg->DrawFrame(m_pFrameOrg);

	static	D3DXMATRIX	mtI(1,0,0,0,  0,1,0,0,  0,0,1,0,  0,0,0,1);
	pDev->SetTransform(D3DTS_WORLD, &mtI);
}
コード例 #19
0
ファイル: ModelRenderer.cpp プロジェクト: nuponsalt/Game001
	void ModelRenderer::Render(const Camera* camera)
	{
		if(!_renders)
			return;
		// ワールド行列設定
		Matrix SclMtx, RotMtx, PosMtx, WldMtx, WVPMtx;
		// 拡縮
		D3DXMatrixScaling(&SclMtx, _scale.x, _scale.y, _scale.z);
		// 回転 : switch-case…クォータニオンか回転行列かXYZ指定か
		this->Evaluate();
		RotMtx = this->_worldRotationMatrix;
		// 位置
		D3DXMatrixTranslation(&PosMtx, _position.x, _position.y, _position.z);
		GraphicsManager::_device->SetRenderState(D3DRS_CULLMODE, _cullingState);
		// デバッグ用
		//GraphicsManager::_device->SetRenderState(D3DRS_FILLMODE, D3DFILL_WIREFRAME);
		// シェーダを使用する場合カメラのビュー行列(0)、プロジェクション行列(1)をワールド行列に合成
		WldMtx = SclMtx * RotMtx * PosMtx;
		WVPMtx = WldMtx * camera->GetMatrix(ViewBehavior::VIEW) * camera->GetMatrix(ViewBehavior::PROJECTION);
		// カメラの座標をシェーダに使用するための行列変換
		Matrix CamMtx = WldMtx * camera->GetMatrix(ViewBehavior::VIEW);
		D3DXMatrixInverse(&CamMtx, NULL, &CamMtx);
		auto eye = camera->GetEye();
		Vector4 EyePos = Vector4(eye.x, eye.y, eye.z, 1);
		EyePos.Transform(CamMtx);
		D3DXVec4Normalize((D3DXVECTOR4*)&EyePos, (D3DXVECTOR4*)&EyePos);
		// シェーダ設定
		_shader->SetTechnique();
		// シェーダにワールド * ビュー * プロジェクション行列を渡す
		_shader->SetWVPMatrix(WVPMtx);
		// シェーダー特有の値の設定
		_shader->ApplyEffect(RotMtx, EyePos);
		
		HRESULT hr;
		// 3D モデルのパーツ分ループして描画
		for(size_t i = 0 ; i < _mesh->GetMaterialNumber(); i++)
		{
			// テクスチャが存在しない場合のカラー
			D3DXVECTOR4 color = D3DXVECTOR4(1.0,1.0,1.0,1.0);
			// 格パーツに対応するテクスチャを設定
			// シェーダにテクスチャを渡す
			if(NULL != _textures[i])
			{
				LPDIRECT3DTEXTURE9 texture = _textures[i]->GetTextureData();
				// シェーダにカラーを渡す
				_shader->SetColor(_colorRGBA);
				_shader->SetTexture(texture);
			}else
				_shader->SetColor(color);
			// シェーダの使用開始
			_shader->BeginShader();
			// シェーダのパス設定
			_shader->BeginPass(_addsBlend);
			// パーツの描画	
			if(SUCCEEDED(GraphicsManager::_device->BeginScene()))
			{
				_mesh->GetMesh()->DrawSubset(i); 
				V(GraphicsManager::_device->EndScene());
			}
			// パス終了
			_shader->EndPass();
			// シェーダ終了
			_shader->EndShader();
		}
	}
コード例 #20
0
ファイル: main.cpp プロジェクト: jjuiddong/Dx9-Shader
//-------------------------------------------------------------
// Name: Render()
// Desc: 화면 렌더
//-------------------------------------------------------------
HRESULT CMyD3DApplication::Render()
{
    D3DXMATRIX m, mT, mR, mL, mView, mProj;
	D3DXVECTOR4 v, light_pos;
	D3DMATERIAL9 *pMtrl;
	DWORD i;

	//---------------------------------------------------------
	// 렌더
	//---------------------------------------------------------
    if( SUCCEEDED( m_pd3dDevice->BeginScene() ) )
    {
		m_pd3dDevice->Clear(0L, NULL
						, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER
						, D3DCOLOR_RGBA(0,0,0,0), 1.0f, 0L);

		//-------------------------------------------------
		// 그냥 평범하게 렌더
		//-------------------------------------------------
		m_pd3dDevice->SetTransform( D3DTS_WORLD,  &m_mWorld);
		m_pd3dDevice->SetTransform( D3DTS_VIEW,  &m_mView );
		m_pd3dDevice->SetTransform( D3DTS_PROJECTION,  &m_mProj );

		TSS( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
		TSS( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
		m_pMeshBg->Render( m_pd3dDevice );
		
		// 비행모델 렌더
		TSS( 0, D3DTSS_COLOROP,   D3DTOP_SELECTARG1 );
		TSS( 0, D3DTSS_COLORARG1, D3DTA_DIFFUSE );
		TSS( 0, D3DTSS_ALPHAOP,   D3DTOP_SELECTARG1 );
		TSS( 0, D3DTSS_ALPHAARG1, D3DTA_DIFFUSE );

		//-------------------------------------------------
		// 확대한 모델 출력
		//-------------------------------------------------
		if( m_pEffect != NULL )
		{
			m_pEffect->SetTechnique( m_hTechnique );
			m_pEffect->Begin( NULL, 0 );
			
			// 로컬-월드 행렬
			D3DXMatrixTranslation( &m, 1.0f, 0.0f ,0.0f );
			D3DXMatrixRotationY( &mR,  3.0f*m_fTime );
			D3DXMatrixTranslation( &mT, 1.0f, 1.0f ,0.0f );
			mL = m * mR * mT * m_mWorld;

			// 광원방향
			D3DXMatrixInverse( &m, NULL, &mL);
			light_pos = D3DXVECTOR4(1,1,-1,0);
			D3DXVec4Transform( &v, &light_pos, &m );
			v.w = 0;
			D3DXVec4Normalize( &v, &v );
			m_pEffect->SetVector( m_hvLightDir, &v );
			// 시점
			D3DXVECTOR3 vFromPt   = D3DXVECTOR3( 0.0f, 0.0f, -m_fViewZoom );
			D3DXVec3Transform( &v, &vFromPt, &m );
			m_pEffect->SetVector( m_hvEyePos, &v );
			// 로컬-투영변환행렬
			m = mL * m_mView;
			m_pEffect->SetMatrix( m_hmWV, &m );
			m_pEffect->SetMatrix( m_hmVP, &m_mProj );
			// 1프레임 직전 행렬
			m_pEffect->SetMatrix( m_hmLastWV, &m_mLastWV );
			m_mLastWV = m;

			for(int pass = 0; pass<2; pass++){
				m_pEffect->Pass( pass );// 패스를 바꿔서 출력
				if(1==pass){
					RS( D3DRS_ZWRITEENABLE, FALSE);
					RS( D3DRS_ALPHABLENDENABLE, TRUE);
					RS( D3DRS_SRCBLEND,   D3DBLEND_SRCALPHA);
					RS( D3DRS_DESTBLEND,  D3DBLEND_INVSRCALPHA);
				}

				pMtrl = m_pMesh->m_pMaterials;
				for( i=0; i<m_pMesh->m_dwNumMaterials; i++ ) {
					v.x = pMtrl->Diffuse.r;
					v.y = pMtrl->Diffuse.g;
					v.z = pMtrl->Diffuse.b;
					v.w = pMtrl->Diffuse.a;
					m_pEffect->SetVector( m_hvCol, &v );
					m_pMesh->m_pLocalMesh->DrawSubset( i );  // 렌더
					pMtrl++;
				}
			}

			m_pEffect->End();

			RS( D3DRS_ALPHABLENDENABLE, FALSE);
			RS( D3DRS_ZWRITEENABLE, TRUE);
		}
		RenderText();				// 도움말 출력

        m_pd3dDevice->EndScene();	// 렌더종료
    }
	Sleep(100);

    return S_OK;
}
コード例 #21
0
ファイル: main.cpp プロジェクト: jjuiddong/Dx9-Shader
//-------------------------------------------------------------
// ������ �� ����
//-------------------------------------------------------------
VOID CMyD3DApplication::DrawModel( int pass )
{
    D3DXMATRIX m, mW, mS, mT, mR;
	D3DXVECTOR3 vDir;
	D3DXVECTOR4 v;
	D3DMATERIAL9 *pMtrl;
	DWORD i;
	
	//---------------------------------------------------------
	// ��� ����
	//---------------------------------------------------------
	// ����-���� ���
	D3DXMATRIX mVP = m_mWorld * m_mView * m_mProj;
	
	// ������������ �ؽ�ó�������� ��ȯ
	float fOffsetX = 0.5f + (0.5f / (float)SHADOW_MAP_SIZE);
	float fOffsetY = 0.5f + (0.5f / (float)SHADOW_MAP_SIZE);
	D3DXMATRIX mScaleBias(	0.5f,     0.0f,     0.0f,   0.0f,
							0.0f,    -0.5f,     0.0f,   0.0f,
							0.0f,     0.0f,     0.0f,	0.0f,
							fOffsetX, fOffsetY, 0.0f,   1.0f );

	//---------------------------------------------------------
	// UFO
	//---------------------------------------------------------
	// ������� ����
	D3DXMatrixTranslation( &mW, m_pos.x, m_pos.y, m_pos.z );
	
	switch(pass){
	case 0:// �׸��ڸ� ����
		// �������������� �������
		m = mW * m_mLightVP;
        m_pEffect->SetMatrix( m_hmWLP, &m );
        
		m_pMesh  ->Render( m_pd3dDevice );// ����
		break;
	default:// ��鷻��
		m_pEffect->Pass( 2 );	// �н�(2) ����
		m = mW * mVP;       // �Ϲ��������
        m_pEffect->SetMatrix( m_hmWVP, &m );
        
		m = mW * m_mLightVP;// �������������� �������
        m_pEffect->SetMatrix( m_hmWLP, &m );
        
		m = m * mScaleBias; // �ؽ�ó���������� �������
        m_pEffect->SetMatrix( m_hmWLPB, &m );
        
        // ������ǥ�迡���� ��������
		D3DXMatrixInverse( &m, NULL, &mW);
		D3DXVec3Transform( &v, &m_LighPos, &m );
		D3DXVec4Normalize( &v, &v );v.w = 0;
		m_pEffect->SetVector( m_hvDir, &v );

		// �޽ó��� ������ ��ǰ�� ����
		pMtrl = m_pMesh->m_pMaterials;
        for( i=0; i<m_pMesh->m_dwNumMaterials; i++ ) {
			v.x = pMtrl->Diffuse.r;
			v.y = pMtrl->Diffuse.g;
			v.z = pMtrl->Diffuse.b;
			v.w = pMtrl->Diffuse.a;
			m_pEffect->SetVector( m_hvCol, &v );        // ������
            m_pMesh->m_pLocalMesh->DrawSubset( i );	// ����
			pMtrl++;
        }
		break;
	}

	//---------------------------------------------------------
	// ����
	//---------------------------------------------------------
	// ������� ����
	D3DXMatrixIdentity( &mW );
	switch(pass){
	case 0:// �׸��ڸ� ����
		// �������������� �������
		m = mW * m_mLightVP;
        m_pEffect->SetMatrix( m_hmWLP, &m );
		m_pMeshBg->Render( m_pd3dDevice );// ����
		break;
	case 1:// ��鷻��
		m_pEffect->Pass( 1 );	// �н�(1) ����
		m = mW * mVP;       // �Ϲ� �������
        m_pEffect->SetMatrix( m_hmWVP, &m );
        
		m = mW * m_mLightVP;// �������������� �������
        m_pEffect->SetMatrix( m_hmWLP, &m );
		
		m = m * mScaleBias; // �ؽ�ó���������� �������
        m_pEffect->SetMatrix( m_hmWLPB, &m );
        
        // ������ǥ�迡���� ��������
		D3DXMatrixInverse( &m, NULL, &mW);
		D3DXVec3Transform( &v, &m_LighPos, &m );
		D3DXVec4Normalize( &v, &v );v.w = 0;
		m_pEffect->SetVector( m_hvDir, &v );
		
		// �޽� ������ ������ �κ��� ����
		pMtrl = m_pMeshBg->m_pMaterials;
        for( i=0; i<m_pMeshBg->m_dwNumMaterials; i++ ) {
			v.x = pMtrl->Diffuse.r;
			v.y = pMtrl->Diffuse.g;
			v.z = pMtrl->Diffuse.b;
			v.w = pMtrl->Diffuse.a;
			m_pEffect->SetVector( m_hvCol, &v );       // ������
			m_pEffect->SetTexture("DecaleMap", m_pMeshBg->m_pTextures[i] );
            m_pMeshBg->m_pLocalMesh->DrawSubset(i); // ����
			pMtrl++;
        }
		break;
	}

}