void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
{
    HRESULT hr;    // Clear the render target and the zbuffer 
    V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) );    // Render the scene
    if( SUCCEEDED( pd3dDevice->BeginScene() ) )
    {
//使用效果渲染场景
D3DXHANDLE hTechnique = 0;
hTechnique= g_pEffect->GetTechniqueByName("TO");
V(g_pEffect->SetTechnique( hTechnique )); UINT nPasses;
g_pEffect->Begin( &nPasses, 0 );
for( UINT iPass = 0; iPass < nPasses; iPass ++ )
{
V(g_pEffect->BeginPass( iPass )); V( g_pMesh->DrawSubset(0) ); g_pEffect->EndPass();
}

g_pEffect->End(); 
V( pd3dDevice->EndScene() );
    }
}程序一运行到 hTechnique= g_pEffect->GetTechniqueByName("TO")
就出错~~~哎~~请高手把我点醒吧!!最近不只是一次遇到这个问题了~

解决方案 »

  1.   

    全部代码:
    //--------------------------------------------------------------------------------------
    // File: EmptyProject.cpp
    //
    // Copyright (c) Microsoft Corporation. All rights reserved.
    //--------------------------------------------------------------------------------------#include "DXUT.h"
    #include "resource.h"
    //全局变量
    LPD3DXMESH                 g_pMesh          = NULL; //网格模型LPD3DXEFFECT               g_pEffect    = NULL;     //效果//--------------------------------------------------------------------------------------
    // Rejects any D3D9 devices that aren't acceptable to the app by returning false
    //--------------------------------------------------------------------------------------
    bool CALLBACK IsD3D9DeviceAcceptable( D3DCAPS9* pCaps, D3DFORMAT AdapterFormat, D3DFORMAT BackBufferFormat,
                                          bool bWindowed, void* pUserContext )
    {
        // Typically want to skip back buffer formats that don't support alpha blending
        IDirect3D9* pD3D = DXUTGetD3D9Object();
        if( FAILED( pD3D->CheckDeviceFormat( pCaps->AdapterOrdinal, pCaps->DeviceType,
                                             AdapterFormat, D3DUSAGE_QUERY_POSTPIXELSHADER_BLENDING,
                                             D3DRTYPE_TEXTURE, BackBufferFormat ) ) )
            return false;
    //检查顶点渲染器
    if( pCaps->VertexShaderVersion < D3DVS_VERSION(2,0) )
    return FALSE; //检查像素渲染器
    if( pCaps->PixelShaderVersion < D3DPS_VERSION(2,0) )
    return FALSE;    return true;
    }
    //--------------------------------------------------------------------------------------
    // Before a device is created, modify the device settings as needed
    //--------------------------------------------------------------------------------------
    bool CALLBACK ModifyDeviceSettings( DXUTDeviceSettings* pDeviceSettings, void* pUserContext )
    {
    return true;
    }
    //--------------------------------------------------------------------------------------
    // Create any D3D9 resources that will live through a device reset (D3DPOOL_MANAGED)
    // and aren't tied to the back buffer size
    //--------------------------------------------------------------------------------------
    HRESULT CALLBACK OnD3D9CreateDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
                                         void* pUserContext )
    { HRESULT hr; //加载茶壶
    V_RETURN( D3DXLoadMeshFromX( L"teapot.x", D3DXMESH_MANAGED, pd3dDevice, 
    NULL, NULL, NULL, NULL, &g_pMesh ) );
    //创建效果
    V_RETURN(D3DXCreateEffectFromFile( pd3dDevice, L"shader1.fx", NULL, NULL, 
    D3DXSHADER_DEBUG, NULL, &g_pEffect, NULL ));
    return S_OK;
    }
    //--------------------------------------------------------------------------------------
    // Create any D3D9 resources that won't live through a device reset (D3DPOOL_DEFAULT) 
    // or that are tied to the back buffer size 
    //--------------------------------------------------------------------------------------
    HRESULT CALLBACK OnD3D9ResetDevice( IDirect3DDevice9* pd3dDevice, const D3DSURFACE_DESC* pBackBufferSurfaceDesc,
                                        void* pUserContext )
    {
    HRESULT hr;
    //恢复效果对象
    if( g_pEffect )
    V_RETURN( g_pEffect->OnResetDevice() ); //构造世界矩阵
    D3DXMATRIX matWorld;
    D3DXMatrixIdentity( &matWorld ); //构造观察矩阵
    D3DXMATRIXA16 matView;
    D3DXVECTOR3 vEyePt( 3.0f, 0.0f,3.0f );
    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec ); //构造投影矩阵
    D3DXMATRIXA16 matProj;
    float fAspectRatio = (float)pBackBufferSurfaceDesc->Width / pBackBufferSurfaceDesc->Height;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, fAspectRatio, 1.0f, 100.0f ); //为效果设置组合变换矩阵
    D3DXMATRIX mWorldViewProj = matWorld * matView * matProj;
    g_pEffect->SetMatrix( "matWorldViewProj", &mWorldViewProj ); //设置剔出模式,为不剔出任何面
    hr = pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE ); return S_OK;
    }
    //--------------------------------------------------------------------------------------
    // Handle updates to the scene.  This is called regardless of which D3D API is used
    //--------------------------------------------------------------------------------------
    void CALLBACK OnFrameMove( double fTime, float fElapsedTime, void* pUserContext )
    { }
    //--------------------------------------------------------------------------------------
    // Render the scene using the D3D9 device
    //--------------------------------------------------------------------------------------
    void CALLBACK OnD3D9FrameRender( IDirect3DDevice9* pd3dDevice, double fTime, float fElapsedTime, void* pUserContext )
    {
        HRESULT hr;    // Clear the render target and the zbuffer 
        V( pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_ARGB( 0, 45, 50, 170 ), 1.0f, 0 ) );    // Render the scene
        if( SUCCEEDED( pd3dDevice->BeginScene() ) )
        {
    //使用效果渲染场景
    D3DXHANDLE hTechnique = 0;
    hTechnique= g_pEffect->GetTechniqueByName("TO");
    V(g_pEffect->SetTechnique( hTechnique )); UINT nPasses;
    g_pEffect->Begin( &nPasses, 0 );
    for( UINT iPass = 0; iPass < nPasses; iPass ++ )
    {
    V(g_pEffect->BeginPass( iPass )); V( g_pMesh->DrawSubset(0) ); g_pEffect->EndPass();
    }

    g_pEffect->End(); 
    V( pd3dDevice->EndScene() );
        }
    }
    //--------------------------------------------------------------------------------------
    // Handle messages to the application 
    //--------------------------------------------------------------------------------------
    LRESULT CALLBACK MsgProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam,
                              bool* pbNoFurtherProcessing, void* pUserContext )
    {
        return 0;
    }
    //--------------------------------------------------------------------------------------
    // Release D3D9 resources created in the OnD3D9ResetDevice callback 
    //--------------------------------------------------------------------------------------
    void CALLBACK OnD3D9LostDevice( void* pUserContext )
    {
    if( g_pEffect )
    g_pEffect->OnLostDevice();
    }
    //--------------------------------------------------------------------------------------
    // Release D3D9 resources created in the OnD3D9CreateDevice callback 
    //--------------------------------------------------------------------------------------
    void CALLBACK OnD3D9DestroyDevice( void* pUserContext )
    {
    SAFE_RELEASE(g_pVB);
    SAFE_RELEASE(g_pMesh);
    SAFE_RELEASE(g_pEffect);
    }
    //--------------------------------------------------------------------------------------
    // Initialize everything and go into a render loop
    //--------------------------------------------------------------------------------------
    INT WINAPI wWinMain( HINSTANCE, HINSTANCE, LPWSTR, int )
    {
        // Enable run-time memory check for debug builds.
    #if defined(DEBUG) | defined(_DEBUG)
        _CrtSetDbgFlag( _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF );
    #endif    // Set the callback functions
        DXUTSetCallbackD3D9DeviceAcceptable( IsD3D9DeviceAcceptable );
        DXUTSetCallbackD3D9DeviceCreated( OnD3D9CreateDevice );
        DXUTSetCallbackD3D9DeviceReset( OnD3D9ResetDevice );
        DXUTSetCallbackD3D9FrameRender( OnD3D9FrameRender );
        DXUTSetCallbackD3D9DeviceLost( OnD3D9LostDevice );
        DXUTSetCallbackD3D9DeviceDestroyed( OnD3D9DestroyDevice );
        DXUTSetCallbackDeviceChanging( ModifyDeviceSettings );
        DXUTSetCallbackMsgProc( MsgProc );
        DXUTSetCallbackFrameMove( OnFrameMove );    // TODO: Perform any application-level initialization here    // Initialize DXUT and create the desired Win32 window and Direct3D device for the application
        DXUTInit( true, true ); // Parse the command line and show msgboxes
        DXUTSetHotkeyHandling( true, true, true );  // handle the default hotkeys
        DXUTSetCursorSettings( true, true ); // Show the cursor and clip it when in full screen
        DXUTCreateWindow( L"EmptyProject" );
        DXUTCreateDevice( true, 640, 480 );    // Start the render loop
        DXUTMainLoop();    // TODO: Perform any application-level cleanup here    return DXUTGetExitCode();
    }
      

  2.   

    代码太长了!
    怀疑你 g_pEffect 没有初始化!
      

  3.   

    我遇到个无法计算指针表达式的值,如下if(*lpSrc==0),运行时提示出错,说是无法计算该表达式的值,请问这是为什么啊?