home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Game Programming for Teens / VBGPFT.cdr / DirectX8 / dx8a_sdk.exe / samples / multimedia / directshow / players / texture3d / textures.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-11-04  |  11.9 KB  |  335 lines

  1. //-----------------------------------------------------------------------------
  2. // File: Textures.cpp
  3. //
  4. // Desc: DirectShow sample code - uses the Direct3D Textures Tutorial05 as 
  5. //       a base to create an application that uses DirectShow to draw a video 
  6. //       on a DirectX 8.0 Texture surface.
  7.  
  8. // Copyright (c) 2000 Microsoft Corporation.  All rights reserved.
  9. //-----------------------------------------------------------------------------
  10.  
  11.  
  12. #include "textures.h"
  13. #include "resource.h"
  14.  
  15. //-----------------------------------------------------------------------------
  16. // Global variables
  17. //-----------------------------------------------------------------------------
  18. LPDIRECT3D8             g_pD3D       = NULL; // Used to create the D3DDevice
  19. LPDIRECT3DDEVICE8       g_pd3dDevice = NULL; // Our rendering device
  20. LPDIRECT3DVERTEXBUFFER8 g_pVB        = NULL; // Buffer to hold vertices
  21. LPDIRECT3DTEXTURE8      g_pTexture   = NULL; // Our texture
  22.  
  23. // A structure for our custom vertex type. We added texture coordinates
  24. struct CUSTOMVERTEX
  25. {
  26.     D3DXVECTOR3 position; // The position
  27.     D3DCOLOR    color;    // The color
  28.     FLOAT       tu, tv;   // The texture coordinates
  29. };
  30.  
  31. // Our custom FVF, which describes our custom vertex structure
  32. #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE|D3DFVF_TEX1)
  33. #define CLASSNAME   "DShow Texture3D Sample"
  34.  
  35.  
  36. //-----------------------------------------------------------------------------
  37. // Name: InitD3D()
  38. // Desc: Initializes Direct3D
  39. //-----------------------------------------------------------------------------
  40. HRESULT InitD3D( HWND hWnd )
  41. {
  42.     HRESULT hr;
  43.  
  44.     // Create the D3D object.
  45.     if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
  46.         return E_FAIL;
  47.  
  48.     // Get the current desktop display mode, so we can set up a back
  49.     // buffer of the same format
  50.     D3DDISPLAYMODE d3ddm;
  51.     if ( FAILED( hr = g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm ) ) )
  52.     {
  53.         Msg(TEXT("Could not read adapter display mode!  hr=0x%x"), hr);
  54.         return hr;
  55.     }
  56.  
  57.     // Set up the structure used to create the D3DDevice. Since we are now
  58.     // using more complex geometry, we will create a device with a zbuffer.
  59.     D3DPRESENT_PARAMETERS d3dpp;
  60.     ZeroMemory( &d3dpp, sizeof(d3dpp) );
  61.     d3dpp.Windowed               = TRUE;
  62.     d3dpp.SwapEffect             = D3DSWAPEFFECT_COPY_VSYNC;
  63.     d3dpp.BackBufferFormat       = d3ddm.Format;
  64.     d3dpp.EnableAutoDepthStencil = TRUE;
  65.     d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
  66.  
  67.     // Create the D3DDevice
  68.     hr = g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
  69.                                D3DCREATE_SOFTWARE_VERTEXPROCESSING | D3DCREATE_MULTITHREADED,
  70.                                &d3dpp, &g_pd3dDevice );                                     
  71.     if (FAILED(hr))                                      
  72.     {
  73.         Msg(TEXT("Could not create the D3D device!  hr=0x%x\r\n\r\n")
  74.             TEXT("This sample is attempting to create a buffer that might not\r\n")
  75.             TEXT("be supported by your video card in its current mode.\r\n\r\n")
  76.             TEXT("You may want to reduce your screen resolution or bit depth\r\n")
  77.             TEXT("and try to run this sample again."), hr);
  78.         return hr;
  79.     }
  80.  
  81.     // Turn off culling
  82.     hr = g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  83.  
  84.     // Turn off D3D lighting
  85.     hr = g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
  86.  
  87.     // Turn on the zbuffer
  88.     hr = g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
  89.  
  90.     return hr;
  91. }
  92.  
  93.  
  94.  
  95. //-----------------------------------------------------------------------------
  96. // Name: InitGeometry()
  97. // Desc: Create the textures and vertex buffers
  98. //-----------------------------------------------------------------------------
  99. HRESULT InitGeometry()
  100. {
  101.     HRESULT hr;
  102.  
  103.     // DShow: Set up filter graph with our custom renderer.
  104.     if( FAILED( InitDShowTextureRenderer(g_pTexture ) ) )
  105.         return E_FAIL;
  106.  
  107.     // Create the vertex buffer.
  108.     if( FAILED( hr = g_pd3dDevice->CreateVertexBuffer( 50*2*sizeof(CUSTOMVERTEX),
  109.                                       0, D3DFVF_CUSTOMVERTEX,
  110.                                       D3DPOOL_DEFAULT, &g_pVB ) ) )
  111.     {
  112.         Msg(TEXT("Could not create a vertex buffer!  hr=0x%x"), hr);
  113.         return E_FAIL;
  114.     }
  115.  
  116.     // Fill the vertex buffer. We are setting the tu and tv texture
  117.     // coordinates, which range from 0.0 to 1.0
  118.     CUSTOMVERTEX* pVertices;
  119.     if ( FAILED( hr = g_pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ) ) )
  120.     {
  121.         Msg(TEXT("Could not lock the vertex buffer!  hr=0x%x"), hr);
  122.         return E_FAIL;
  123.     }
  124.  
  125.     for( DWORD i=0; i<50; i++ )
  126.     {
  127.         FLOAT theta = (2*D3DX_PI*i)/(50-1);
  128.  
  129.         pVertices[2*i+0].position = D3DXVECTOR3( sinf(theta),-1.0f, cosf(theta) );
  130.         pVertices[2*i+0].color    = 0xffffffff;
  131.         pVertices[2*i+0].tu       = ((FLOAT)i)/(50-1);
  132.         pVertices[2*i+0].tv       = 1.0f;
  133.  
  134.         pVertices[2*i+1].position = D3DXVECTOR3( sinf(theta), 1.0f, cosf(theta) );
  135.         pVertices[2*i+1].color    = 0xff808080;
  136.         pVertices[2*i+1].tu       = ((FLOAT)i)/(50-1);
  137.         pVertices[2*i+1].tv       = 0.0f;
  138.     }
  139.  
  140.     g_pVB->Unlock();
  141.     return S_OK;
  142. }
  143.  
  144.  
  145.  
  146. //-----------------------------------------------------------------------------
  147. // Name: Cleanup()
  148. // Desc: Releases all previously initialized objects
  149. //-----------------------------------------------------------------------------
  150. VOID Cleanup()
  151. {
  152.     CleanupDShow();
  153.  
  154.     if( g_pTexture != NULL )   
  155.         g_pTexture->Release();
  156.         
  157.     if( g_pVB != NULL )
  158.         g_pVB->Release();
  159.         
  160.     if( g_pd3dDevice != NULL ) 
  161.         g_pd3dDevice->Release();
  162.         
  163.     if( g_pD3D != NULL )
  164.         g_pD3D->Release();
  165. }
  166.  
  167.  
  168.  
  169. //-----------------------------------------------------------------------------
  170. // Name: SetupMatrices()
  171. // Desc: Sets up the world, view, and projection transform matrices.
  172. //-----------------------------------------------------------------------------
  173. VOID SetupMatrices()
  174. {
  175.     HRESULT hr;
  176.  
  177.     // For our world matrix, we will just leave it as the identity
  178.     D3DXMATRIX matWorld;
  179.     D3DXMatrixIdentity( &matWorld );
  180.     D3DXMatrixRotationX( &matWorld, timeGetTime()/2000.0f );
  181.     hr = g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  182.     if (FAILED(hr))                                      
  183.     {
  184.         Msg(TEXT("Could not set D3DTS_WORLD transform!  hr=0x%x"), hr);
  185.     }
  186.  
  187.     // Set up our view matrix. A view matrix can be defined given an eye point,
  188.     // a point to lookat, and a direction for which way is up. Here, we set the
  189.     // eye five units back along the z-axis and up three units, look at the
  190.     // origin, and define "up" to be in the y-direction.
  191.     D3DXMATRIX matView;
  192.     D3DXMatrixLookAtLH( &matView, &D3DXVECTOR3( 0.0f, 3.0f,-5.0f ),
  193.                                   &D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
  194.                                   &D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
  195.     hr = g_pd3dDevice->SetTransform( D3DTS_VIEW, &matView );
  196.     if (FAILED(hr))                                      
  197.     {
  198.         Msg(TEXT("Could not set D3DTS_VIEW transform!  hr=0x%x"), hr);
  199.     }
  200.  
  201.     // For the projection matrix, we set up a perspective transform (which
  202.     // transforms geometry from 3D view space to 2D viewport space, with
  203.     // a perspective divide making objects smaller in the distance). To build
  204.     // a perpsective transform, we need the field of view (1/4 pi is common),
  205.     // the aspect ratio, and the near and far clipping planes (which define at
  206.     // what distances geometry should be no longer be rendered).
  207.     D3DXMATRIX matProj;
  208.     D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
  209.     hr = g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  210.     if (FAILED(hr))                                      
  211.     {
  212.         Msg(TEXT("Could not set D3DTS_PROJECTION transform!  hr=0x%x"), hr);
  213.     }
  214. }
  215.  
  216.  
  217.  
  218. //-----------------------------------------------------------------------------
  219. // Name: Render()
  220. // Desc: Draws the scene
  221. //-----------------------------------------------------------------------------
  222. VOID Render()
  223. {
  224.     // Clear the backbuffer and the zbuffer
  225.     g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
  226.                          D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
  227.  
  228.     // Begin the scene
  229.     g_pd3dDevice->BeginScene();
  230.  
  231.     // Setup the world, view, and projection matrices
  232.     SetupMatrices();
  233.  
  234.     // Setup our texture. Using textures introduces the texture stage states,
  235.     // which govern how textures get blended together (in the case of multiple
  236.     // textures) and lighting information. In this case, we are modulating
  237.     // (blending) our texture with the diffuse color of the vertices.
  238.     g_pd3dDevice->SetTexture( 0, g_pTexture );
  239.     g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP,   D3DTOP_MODULATE );
  240.     g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
  241.     g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
  242.     g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP,   D3DTOP_DISABLE );
  243.  
  244.     // Render the vertex buffer contents
  245.     g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
  246.     g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
  247.     g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2*50-2 );
  248.  
  249.     // End the scene
  250.     g_pd3dDevice->EndScene();
  251.  
  252.     // Present the backbuffer contents to the display
  253.     g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
  254.  
  255.     // Check to see if we need to restart the movie
  256.     CheckMovieStatus();
  257. }
  258.  
  259.  
  260.  
  261. //-----------------------------------------------------------------------------
  262. // Name: MsgProc()
  263. // Desc: The window's message handler
  264. //-----------------------------------------------------------------------------
  265. LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  266. {
  267.     switch( msg )
  268.     {
  269.         case WM_DESTROY:
  270.             PostQuitMessage( 0 );
  271.             return 0;
  272.     }
  273.  
  274.     return DefWindowProc( hWnd, msg, wParam, lParam );
  275. }
  276.  
  277.  
  278.  
  279. //-----------------------------------------------------------------------------
  280. // Name: WinMain()
  281. // Desc: The application's entry point
  282. //-----------------------------------------------------------------------------
  283. INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
  284. {
  285.     // Initialize COM
  286.     CoInitialize (NULL);
  287.  
  288.     // Register the window class
  289.     WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
  290.                       GetModuleHandle(NULL), 
  291.                       LoadIcon(hInst, MAKEINTRESOURCE(IDI_TEXTURES)), 
  292.                       NULL, NULL, NULL,
  293.                       CLASSNAME, NULL };
  294.     RegisterClassEx( &wc );
  295.  
  296.     // Create the application's window
  297.     HWND hWnd = CreateWindow( CLASSNAME, "DShow Texture3D Sample",
  298.                               WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
  299.                               GetDesktopWindow(), NULL, wc.hInstance, NULL );
  300.  
  301.     // Initialize Direct3D
  302.     if( SUCCEEDED( InitD3D( hWnd ) ) )
  303.     {
  304.         // Create the scene geometry
  305.         if( SUCCEEDED( InitGeometry() ) )
  306.         {
  307.             // Show the window
  308.             ShowWindow( hWnd, SW_SHOWDEFAULT );
  309.             UpdateWindow( hWnd );
  310.  
  311.             // Enter the message loop
  312.             MSG msg;
  313.             ZeroMemory( &msg, sizeof(msg) );
  314.             while( msg.message!=WM_QUIT )
  315.             {
  316.                 if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
  317.                 {
  318.                     TranslateMessage( &msg );
  319.                     DispatchMessage( &msg );
  320.                 }
  321.                 else
  322.                     Render();
  323.             }
  324.         }
  325.     }
  326.  
  327.     // Clean up everything and exit the app
  328.     Cleanup();
  329.     UnregisterClass( CLASSNAME, wc.hInstance );
  330.     CoUninitialize();
  331.     return 0L;
  332. }
  333.  
  334.  
  335.