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

📄 fixedpoint.cpp

📁 WIndows mobile 5.0 pocket pc sdk sample for win32
💻 CPP
📖 第 1 页 / 共 2 页
字号:
        }
    
        g_pD3DM->Release();
    }
}




//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOID SetupMatrices()
{
    // For our world matrix, we will just leave it as the identity
    D3DMXMATRIXFXD matWorld;
    D3DMXMatrixIdentityFxd( &matWorld );
    D3DMXMatrixRotationXFxd( &matWorld, D3DMXToFixed(GetTickCount()/1000.0f) );
    g_pd3dmDevice->SetTransform( D3DMTS_WORLD, (D3DMMATRIX*)&matWorld, D3DMFMT_D3DMVALUE_FIXED );


    // Set up our view matrix. A view matrix can be defined given an eye point,
    // a point to lookat, and a direction for which way is up. Here, we set the
    // eye five units back along the z-axis and up three units, look at the
    // origin, and define "up" to be in the y-direction.
    D3DMXVECTOR3FXD vEyePt( 0.0f, 3.0f, -5.0f );
    D3DMXVECTOR3FXD vLookatPt( 0.0f, 0.0f, 0.0f );
    D3DMXVECTOR3FXD vUpVec( 0.0f, 1.0f, 0.0f );
    D3DMXMATRIXFXD matView;
    D3DMXMatrixLookAtLHFxd( &matView, &vEyePt, &vLookatPt, &vUpVec );
    g_pd3dmDevice->SetTransform( D3DMTS_VIEW, (D3DMMATRIX*)&matView, D3DMFMT_D3DMVALUE_FIXED );


    // For the projection matrix, we set up a perspective transform (which
    // transforms geometry from 3D view space to 2D viewport space, with
    // a perspective divide making objects smaller in the distance). To build
    // a perpsective transform, we need the field of view (1/4 pi is common),
    // the aspect ratio, and the near and far clipping planes (which define at
    // what distances geometry should be no longer be rendered).
    D3DMXMATRIXFXD matProj;
    D3DMXMatrixPerspectiveFovLHFxd( &matProj, D3DMXToFixed(D3DMX_PI/4.0f), D3DMXToFixed(1.0f), 
                            D3DMXToFixed(1.0f), D3DMXToFixed(100.0f) );
    g_pd3dmDevice->SetTransform( D3DMTS_PROJECTION, (D3DMMATRIX*)&matProj, D3DMFMT_D3DMVALUE_FIXED );
}




//-----------------------------------------------------------------------------
// Name: SetupLights()
// Desc: Sets up the lights and materials for the scene.
//-----------------------------------------------------------------------------
VOID SetupLights()
{
    // Set up a material. The material here just has the diffuse and ambient
    // colors set to white. Note that only one material can be used at a time.
    D3DMMATERIAL mtrl;
    memset( &mtrl, 0, sizeof(D3DMMATERIAL) );
    mtrl.Diffuse.r = mtrl.Ambient.r = (D3DMVALUE)D3DMXToFixed(1.0f);
    mtrl.Diffuse.g = mtrl.Ambient.g = (D3DMVALUE)D3DMXToFixed(1.0f);
    mtrl.Diffuse.b = mtrl.Ambient.b = (D3DMVALUE)D3DMXToFixed(1.0f);
    mtrl.Diffuse.a = mtrl.Ambient.a = (D3DMVALUE)D3DMXToFixed(1.0f);
    g_pd3dmDevice->SetMaterial( &mtrl, D3DMFMT_D3DMVALUE_FIXED );

    // Set up a white, directional light, with an oscillating direction.
    // Note that many lights may be active at a time (but each one slows down
    // the rendering of our scene). However, here we are just using one. Also,
    // we need to set the D3DMRS_LIGHTING renderstate to enable lighting
    D3DMXVECTOR3FXD vecDir;
    D3DMLIGHT light;
    memset( &light, 0, sizeof(D3DMLIGHT) );
    light.Type       = D3DMLIGHT_DIRECTIONAL;
    light.Diffuse.r  = (D3DMVALUE)D3DMXToFixed(1.0f);
    light.Diffuse.g  = (D3DMVALUE)D3DMXToFixed(1.0f);
    light.Diffuse.b  = (D3DMVALUE)D3DMXToFixed(1.0f);
    vecDir = D3DMXVECTOR3FXD((float)cos(GetTickCount()/350.0f),
                         1.0f,
                         (float)sin(GetTickCount()/350.0f) );
    D3DMXVec3NormalizeFxd((D3DMXVECTOR3FXD*)&light.Direction, &vecDir );
    // Light range is always floating point data
    light.Range       = 1000.0f; 
    g_pd3dmDevice->SetLight( 0, &light, D3DMFMT_D3DMVALUE_FIXED );
    g_pd3dmDevice->LightEnable( 0, TRUE );

    g_pd3dmDevice->SetRenderState( D3DMRS_LIGHTING, TRUE );

    // Finally, turn on some ambient light.
    g_pd3dmDevice->SetRenderState( D3DMRS_AMBIENT, 0x00202020 );
}




//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer and the zbuffer
    g_pd3dmDevice->Clear( 0, NULL, D3DMCLEAR_TARGET | D3DMCLEAR_ZBUFFER,
                         D3DMCOLOR_XRGB(0,0,255), 1.0f, 0 );

    // Begin the scene
    if( SUCCEEDED( g_pd3dmDevice->BeginScene() ) )
    {
        // Setup the lights and materials
        SetupLights();

        // Setup the world, view, and projection matrices
        SetupMatrices();

        // Setup our texture. Using textures introduces the texture stage states,
        // which govern how textures get blended together (in the case of multiple
        // textures) and lighting information. In this case, we are modulating
        // (blending) our texture with the diffuse color of the vertices.
        g_pd3dmDevice->SetTexture( 0, g_pTexture );
        g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLOROP,   D3DMTOP_MODULATE );
        g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLORARG1, D3DMTA_TEXTURE );
        g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_COLORARG2, D3DMTA_DIFFUSE );
        g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_ALPHAOP,   D3DMTOP_DISABLE );

    #ifdef SHOW_HOW_TO_USE_TCI
        // Note: to use D3DM texture coordinate generation, use the stage state
        // D3DMTSS_TEXCOORDINDEX, as shown below. In this example, we are using
        // the position of the vertex in camera space to generate texture
        // coordinates. The tex coord index (TCI) parameters are passed into a
        // texture transform, which is a 4x4 matrix which transforms the x,y,z
        // TCI coordinates into tu, tv texture coordinates.

        // In this example, the texture matrix is setup to 
        // transform the texture from (-1,+1) position coordinates to (0,1) 
        // texture coordinate space:
        //    tu =  0.5*x + 0.5
        //    tv = -0.5*y + 0.5
        D3DMXMATRIXFXD mat;
        mat._11 = D3DMXToFixed(0.25f); 
        mat._12 = D3DMXToFixed(0.00f); 
        mat._13 = D3DMXToFixed(0.00f); 
        mat._14 = D3DMXToFixed(0.00f);
        mat._21 = D3DMXToFixed(0.00f); 
        mat._22 = D3DMXToFixed(-0.25f); 
        mat._23 = D3DMXToFixed(0.00f); 
        mat._24 = D3DMXToFixed(0.00f);
        mat._31 = D3DMXToFixed(0.00f);
        mat._32 = D3DMXToFixed(0.00f);
        mat._33 = D3DMXToFixed(1.00f); 
        mat._34 = D3DMXToFixed(0.00f);
        mat._41 = D3DMXToFixed(0.50f); 
        mat._42 = D3DMXToFixed(0.50f); 
        mat._43 = D3DMXToFixed(0.00f);
        mat._44 = D3DMXToFixed(1.00f);

        g_pd3dmDevice->SetTransform( D3DMTS_TEXTURE0, (D3DMMATRIX*)&mat, D3DMFMT_D3DMVALUE_FIXED );
        g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_TEXTURETRANSFORMFLAGS, D3DMTTFF_COUNT2 );
        g_pd3dmDevice->SetTextureStageState( 0, D3DMTSS_TEXCOORDINDEX, D3DMTSS_TCI_CAMERASPACEPOSITION );
    #endif

        // Render the vertex buffer contents
        g_pd3dmDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
        g_pd3dmDevice->DrawPrimitive( D3DMPT_TRIANGLESTRIP, 0, 2*50-2 );
        
        // End the scene
        g_pd3dmDevice->EndScene();
    }

    // Present the backbuffer contents to the display
    g_pd3dmDevice->Present( NULL, NULL, NULL, NULL );
}




//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
    case WM_LBUTTONUP: 
        PostMessage(hWnd, WM_CLOSE, 0, 0);
        break;
    case WM_KEYDOWN:
        if (VK_ESCAPE == wParam)
        {
            PostMessage(hWnd, WM_CLOSE, 0, 0);
        }
        break;

    case WM_CLOSE:
        Cleanup();
        break;

    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;

    case WM_SETTINGCHANGE:
        //we don't support screen rotation
        if (IsScreenRotated())
        {
            PostMessage(hWnd, WM_CLOSE, 0, 0);
        }
        break;
    }

    return DefWindowProc( hWnd, msg, wParam, lParam );
}




//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPTSTR szCmd, INT )
{
    // Parse command line to determine if user wants to use
    // the D3DM reference driver instead of the default system driver
    if (0 == lstrcmp(szCmd, TEXT("-ref")))
        g_bUseRef = true;

    //we don't support screen rotation
    if (IsScreenRotated())
    {
        return 0;
    }
    
    // Register the window class
    WNDCLASS wc = { 0L, MsgProc, 0L, 0L, 
                      hInst, NULL, NULL, NULL, NULL,
                      TEXT("D3DM Tutorial") };
    RegisterClass( &wc );

    int iScreenWidth = GetSystemMetrics(SM_CXSCREEN);
    int iScreenHeight = GetSystemMetrics(SM_CYSCREEN);

    // Create the application's window
    HWND hWnd = CreateWindow( TEXT("D3DM Tutorial"), 
                              TEXT("D3DM Tutorial 06: Fixed Point"), 
                              WS_VISIBLE, 
                              0, 0, iScreenWidth, iScreenHeight,
                              NULL, NULL, wc.hInstance, NULL );

    SHFullScreen(hWnd, SHFS_HIDESIPBUTTON | SHFS_HIDETASKBAR);

    // Initialize Direct3D Mobile
    if( SUCCEEDED( InitD3DM( hWnd ) ) )
    { 
        // Create the vertex buffer
        if( SUCCEEDED( InitGeometry() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWNORMAL );
            UpdateWindow( hWnd );

            // Enter the message loop
            MSG msg;
            memset( &msg, 0, sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();
            }
        }
    }

    return 0;
}

⌨️ 快捷键说明

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