⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 multitexture.cpp

📁 3d 游戏编程入门教程之例子源码--飞机绕茶壶
💻 CPP
📖 第 1 页 / 共 3 页
字号:
		  }
		  else if (i = 120)
		  {
			i = 0;
		  }

		 i++;
		}

		/****************************************
		Blending the texture with material color
		*****************************************/
		if (m_bTex3 == TRUE)
		{

			// Set texture for the cube
			m_pd3dDevice->SetTexture( 0, m_pWallTexture); 
	  	    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_ADD);
		}

		/*****************************************
		Darkmap blended with material color
		*****************************************/
		if (m_bTex4 == TRUE)
		{
			m_pd3dDevice->SetTexture( 0, m_pWallTexture); 
		    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); 
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE); 
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE);

	        // Set darkmap 
		    m_pd3dDevice->SetTexture(1, m_pEnvTexture); 
		    m_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0 );
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE); 
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_MODULATE);
		}
		/*****************************************
		Glow Mapping
		******************************************/
		if (m_bTex5 == TRUE)
		{
			m_pd3dDevice->SetTexture( 0, m_pWallTexture); 
		    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); 
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);

			// Set glow map 
			m_pd3dDevice->SetTexture(1, m_pEnvTexture); 
		    m_pd3dDevice->SetTextureStageState( 1, D3DTSS_TEXCOORDINDEX, 0 );
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE ); 
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT );
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADD);
		}

		/*****************************************
		Detail Mapping
		******************************************/
		if (m_bTex6 == TRUE)
		{
			m_pd3dDevice->SetTexture( 0, m_pWallTexture); 
		    m_pd3dDevice->SetTextureStageState( 0, D3DTSS_TEXCOORDINDEX, 0 );
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE ); 
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	
			// Set detail map 
			m_pd3dDevice->SetTexture(1, m_pDetailTexture); 
		    m_pd3dDevice->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 0 );
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE ); 
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT );
			m_pd3dDevice->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADDSIGNED);
		}


		/*****************************************
		Modulate Alpha
		******************************************/
		if (m_bTex7 == TRUE)
		{
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE); 

			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE);
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG2, D3DTA_DIFFUSE);
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
			m_pd3dDevice->SetTexture( 0, m_pWallTexture); 
			
		}

		/****************************************
		Blending with the frame buffer 
		*****************************************/
		if (m_bTex8 == TRUE)
		{
			m_pd3dDevice->SetRenderState (D3DRS_ALPHABLENDENABLE, TRUE);
			m_pd3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCCOLOR);
			m_pd3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCCOLOR);
			m_pd3dDevice->SetTexture( 0, m_pWallTexture); 
		}

		/***************************************
		Bilinear filtering
		***************************************/
/*		if (m_bFil1 == TRUE)
		{
			// bilineaer filtering
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
			m_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
		}
*/


		/***************************************
		Trilinear filtering
		***************************************/
		if (m_bFil1 == TRUE)
		{
		    m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_LINEAR );
		}

		/***************************************
		Anisotropic filtering
		***************************************/
		if (m_bFil2 == TRUE)
		{
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_ANISOTROPIC);
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_ANISOTROPIC);
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MIPFILTER, D3DTEXF_ANISOTROPIC);
			m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAXANISOTROPY, 8);
		}
		
		// Display the object
		m_pd3dDevice->SetStreamSource(0, m_pCubeVB, 0, sizeof(CUBEVERTEX) );
		m_pd3dDevice->SetFVF( FVF_CUBEVERTEX );
        m_pd3dDevice->SetIndices( m_pCubeIB );
		m_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 
											0,
											0, 
											24,  // number of vertices
                                            0, 
											36/3); // number of primitives
		if (m_bTex8 == TRUE)
		{
			// switch off alpha blending
			m_pd3dDevice->SetRenderState (D3DRS_ALPHABLENDENABLE, FALSE);
		}

        // Output statistics
        m_pFont->DrawText( 2,  0, D3DCOLOR_ARGB(255,255,255,0), m_strFrameStats );
        m_pFont->DrawText( 2, 20, D3DCOLOR_ARGB(255,255,255,0), m_strDeviceStats );
		// Draw help
		if( m_bDrawHelp )
		{
			m_pFontSmall->DrawText( 2, 40, 0xffaaffff,
                                _T("Keyboard controls:") );
			m_pFontSmall->DrawText( 20, 52, 0xffaaffff,
                                _T("Dark Mapping\n"
									"Animated Dark Mapping\n"
									"Blending with Material Diffuse Color\n"
									"Darkmap with Material Diffuse Color\n"
									"Glow Mapping\n"
									"Detail Mapping\n"
									"Modulate Alpha\n"
									"Blending with Frame Buffer\n\n"
									"Trilinear Filtering\n"
									"Anisotropic Filtering\n\n"
									"Clamp Addressing Mode\n"
									"Mirror Once\n"
									"Border Color\n\n"
                                    "Exit\n"
									"Toggle Menu") );
			m_pFontSmall->DrawText( 260, 52, 0xffaaffff,
                                _T("1\n"
									"2\n"
									"3\n"
									"4\n"
									"5\n"
									"6\n"
									"7\n"
									"8\n\n"
									"F5\n"
									"F6\n\n"
									"F9\n"
									"F11\n"
									"F12\n\n"
									"ESC\n"
									"F1"));
		}
		else
			m_pFontSmall->DrawText( 2, 40, 0xffffffff,
                                _T("Press F1 for help") );


      // End the scene.
      m_pd3dDevice->EndScene();
    }

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InitDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::InitDeviceObjects()
{
    m_pFont->InitDeviceObjects( m_pd3dDevice );
    m_pFontSmall->InitDeviceObjects( m_pd3dDevice );

    // Load in textures
    if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice, 
					_T("Lake.bmp"), &m_pBackgroundTexture)))
        return D3DAPPERR_MEDIANOTFOUND;
    
	if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice, 
					_T("wall.bmp"), &m_pWallTexture)))
        return D3DAPPERR_MEDIANOTFOUND;
    
	if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice, 
					_T("env0.bmp"), &m_pEnvTexture)))
        return D3DAPPERR_MEDIANOTFOUND;

    if( FAILED( D3DUtil_CreateTexture(m_pd3dDevice, 
					_T("detail.bmp"), &m_pDetailTexture)))
        return D3DAPPERR_MEDIANOTFOUND;

    // Generate the cube data
    CreateCube( m_pCubeVertices, m_pCubeIndices );

    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: RestoreDeviceObjects()
// Desc: Initialize scene objects.
//-----------------------------------------------------------------------------
HRESULT CMyD3DApplication::RestoreDeviceObjects()
{
    m_pFont->RestoreDeviceObjects();
    m_pFontSmall->RestoreDeviceObjects();


    // Set the transform matrices
    D3DXVECTOR3 vEyePt    = D3DXVECTOR3( 0.0f, 0.0f, 5.0f );
    D3DXVECTOR3 vLookatPt = D3DXVECTOR3( 0.0f, 0.0f, 1.0f );
    D3DXVECTOR3 vUpVec    = D3DXVECTOR3( 0.0f, 1.0f, 0.0f );
    D3DXMATRIX  matView, matProj;
    
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
    FLOAT fAspect = m_d3dsdBackBuffer.Width / (FLOAT)m_d3dsdBackBuffer.Height;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspect, 1.0f, 500.0f );
    
    m_pd3dDevice->SetTransform( D3DTS_VIEW,       &matView );
    m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );

    // Material
    D3DMATERIAL9 Material;
    D3DUtil_InitMaterial( Material, 1.0f, 1.0f, 1.0f, 1.0f );
    m_pd3dDevice->SetMaterial( &Material );

	// fill the vertex buffer with the new data
    // Initialize to render a quad
    VERTEX Vertices[] =
    {
		{  0.0f, (float)m_d3dsdBackBuffer.Height, 0.5f, 1.0f, -0.5f, 1.5f, }, // x, y, z, rhw, tu, tv
		{  0.0f, 0.0f, 0.5f, 1.0f, -0.5f, -0.5f, },
		{  (float)m_d3dsdBackBuffer.Width, (float)m_d3dsdBackBuffer.Height, 0.5f, 1.0f, 1.5f,1.5f, },
		{  (float)m_d3dsdBackBuffer.Width, 0.0f, 0.5f, 1.0f, 1.5f,-0.5f, },
    };

	m_dwSizeofVertices = sizeof(Vertices);

	// sixth parameter is new in DX9
	// Create a square for rendering the background
    if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofVertices,
                                                  D3DUSAGE_WRITEONLY, FVF,
                                                  D3DPOOL_MANAGED, &m_pVB, NULL ) ) )
        return E_FAIL;

    VERTEX* pVertices;
	// the third parameter changed from BYTE** to VOID** in DX9
    if( FAILED( m_pVB->Lock( 0, m_dwSizeofVertices, (VOID**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, Vertices, m_dwSizeofVertices);
    m_pVB->Unlock();

	// 
	// vertex and index buffer for the cube
	//
	m_dwSizeofCubeVertices = sizeof(m_pCubeVertices);

    // Create the vertex buffer
    if( FAILED( m_pd3dDevice->CreateVertexBuffer( m_dwSizeofCubeVertices,
                                                  0, FVF_CUBEVERTEX,

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -