Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[DirectX]Wersja June 2010 (9), a wersja (8)

Ostatnio zmodyfikowano 2011-09-29 19:21
Autor Wiadomość
Trix
Temat założony przez niniejszego użytkownika
[DirectX]Wersja June 2010 (9), a wersja (8)
» 2011-09-26 20:22:40
Mam taki kod:
C/C++
#include <d3dx8.h>
#include <mmsystem.h>
#include <direct.h>

#pragma comment(lib, "d3d8.lib")
#pragma comment(lib, "d3dx8.lib")
#pragma comment(lib, "winmm.lib")

LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER8 g_pVB = NULL; // Buffer to hold vertices
LPDIRECT3DTEXTURE8 g_pTexture = NULL; // Our texture

D3DXMATRIX matWorldX; // Matrix for translate via X axis
D3DXMATRIX matWorldY; // matrix for rotate via X and Y axis
D3DXMATRIX matWorldXT; // matrix for translate via X axis

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x;
    FLOAT y;
    FLOAT z;
    DWORD color;
    FLOAT tx;
    FLOAT ty;
};

#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 )

// Function name : InitD3D
// Description     :
// Return type : HRESULT
// Argument         : HWND hWnd
HRESULT InitD3D( HWND hWnd )
{
    if( NULL ==( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
         return E_FAIL;
   
    D3DDISPLAYMODE d3ddm;
    if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, & d3ddm ) ) )
         return E_FAIL;
   
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( & d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
   
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
    & d3dpp, & g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }
   
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
   
    return S_OK;
}

// Function name : InitGeometry
// Description     :
// Return type : HRESULT
HRESULT InitGeometry()
{
    CUSTOMVERTEX g_Vertices[] =
    {
        // front
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 3.0f, },
        { - 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 3.0f, 3.0f, },
       
        { - 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 3.0f, 3.0f, },
        { 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 3.0f, 0.0f, },
       
        // right
        { 1.0f, - 1.0f, - 1.0f, 0xFFFF0000, 0.0f, 1.0f, },
        { 1.0f, 1.0f, - 1.0f, 0xFFFF0000, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, 1.0f, 0xFFFF0000, 1.0f, 1.0f, },
       
        { 1.0f, 1.0f, - 1.0f, 0xFFFF0000, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, 1.0f, 0xFFFF0000, 1.0f, 1.0f, },
        { 1.0f, 1.0f, 1.0f, 0xFFFF0000, 1.0f, 0.0f, },
       
        // back
        { 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 1.0f, },
        { 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { - 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
       
        { 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { - 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 0.0f, },
       
        // left/ left
        { - 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 1.0f, },
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
       
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
        { - 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 0.0f, },
       
        // top/ top
        { - 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 1.0f, },
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
       
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
        { 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 0.0f, },
       
        // bootom/ bootom
        { - 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 1.0f, },
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
       
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
        { 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 0.0f, },
    };
   
    if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "Media/tex.bmp", & g_pTexture ) ) )
    {
        return E_FAIL;
    }
   
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 36 * sizeof( CUSTOMVERTEX ),
    0, D3DFVF_CUSTOMVERTEX,
    D3DPOOL_DEFAULT, & g_pVB ) ) )
    {
        return E_FAIL;
    }
   
    VOID * pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof( g_Vertices ),( BYTE ** ) & pVertices, 0 ) ) )
         return E_FAIL;
   
    memcpy( pVertices, g_Vertices, sizeof( g_Vertices ) );
    g_pVB->Unlock();
   
    return S_OK;
}

// Function name : Cleanup
// Description     :
// Return type : VOID
VOID Cleanup()
{
    if( g_pTexture != NULL )
         g_pTexture->Release();
   
    if( g_pVB != NULL )
         g_pVB->Release();
   
    if( g_pd3dDevice != NULL )
         g_pd3dDevice->Release();
   
    if( g_pD3D != NULL )
         g_pD3D->Release();
   
}

// Function name : SetupMatrices
// Description     :
// Return type : VOID
VOID SetupMatrices()
{
    D3DXMATRIX matView;
    D3DXMatrixLookAtLH( & matView, & D3DXVECTOR3( 0.0f, 0.0f, - 6.0f ),
    & D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
    & D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
    g_pd3dDevice->SetTransform( D3DTS_VIEW, & matView );
   
    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH( & matProj, D3DX_PI / 4, 400.0f / 400.0f, 1.0f, 100.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, & matProj );
}

// Function name : Render
// Description     :
// Return type : VOID
VOID Render()
{
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );
    g_pd3dDevice->BeginScene();
   
    D3DXMatrixRotationX( & matWorldX, timeGetTime() / 1500.0f );
    D3DXMatrixRotationY( & matWorldY, timeGetTime() / 1500.0f );
   
    g_pd3dDevice->SetTexture( 0, g_pTexture );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
   
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
   
    g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof( CUSTOMVERTEX ) );
    g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
    g_pd3dDevice->SetTransform( D3DTS_WORLD, &( matWorldX * matWorldY ) );
    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 12 ); // front
   
    g_pd3dDevice->EndScene();
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

// Function name : MsgProc
// Description     :
// Return type : LRESULT WINAPI
// Argument         :  HWND hWnd
// Argument         : UINT msg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;
       
    case WM_KEYDOWN:
        switch( wParam )
        {
        case VK_ESCAPE:
            PostQuitMessage( 0 );
            break;
        }
    }
   
    return DefWindowProc( hWnd, msg, wParam, lParam );
}

// Function name : WinMain
// Description     :
// Return type : INT WINAPI
// Argument         :  HINSTANCE hInst
// Argument         : HINSTANCE
// Argument         : LPSTR
// Argument         : INT
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
        GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
        "D3D Tutorial", NULL };
    RegisterClassEx( & wc );
   
    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 06 - Texture",
    WS_OVERLAPPEDWINDOW, 100, 100, 350, 350,
    GetDesktopWindow(), NULL, wc.hInstance, NULL );
   
    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the scene geometry
        if( SUCCEEDED( InitGeometry() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );
            SetupMatrices();
           
            // Enter the message loop
            MSG msg;
            ZeroMemory( & msg, sizeof( msg ) );
            while( msg.message != WM_QUIT )
            {
                if( PeekMessage( & msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( & msg );
                    DispatchMessage( & msg );
                }
                else
                     Render();
               
            }
        }
    }
   
    // Clean up everything and exit the app
    Cleanup();
    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}
Lecz na dysku mam wersje June 2010 (9)
Jak przejść między wersją 8 na 9?

W prościejszych kodach sobie radziłem lecz teraz błędy mi wywala :)

------ Build started: Project: ddraw- testy, Configuration: Debug Win32 ------
  main.cpp
c:\programy w c++\ddraw- testy\ddraw- testy\main.cpp(135): error C2660: 'IDirect3DDevice9::CreateVertexBuffer' : function does not take 5 arguments
c:\programy w c++\ddraw- testy\ddraw- testy\main.cpp(141): error C2664: 'IDirect3DVertexBuffer9::Lock' : cannot convert parameter 3 from 'BYTE **' to 'void **'
          Types pointed to are unrelated; conversion requires reinterpret_cast, C-style cast or function-style cast
c:\programy w c++\ddraw- testy\ddraw- testy\main.cpp(200): error C2065: 'D3DTSS_MINFILTER' : undeclared identifier
c:\programy w c++\ddraw- testy\ddraw- testy\main.cpp(201): error C2065: 'D3DTSS_MAGFILTER' : undeclared identifier
c:\programy w c++\ddraw- testy\ddraw- testy\main.cpp(203): error C2660: 'IDirect3DDevice9::SetStreamSource' : function does not take 3 arguments
c:\programy w c++\ddraw- testy\ddraw- testy\main.cpp(204): error C2664: 'IDirect3DDevice9::SetVertexShader' : cannot convert parameter 1 from 'int' to 'IDirect3DVertexShader9 *'
          Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Kod w którym wystąpił powyższy błąd(przekształcony ten pierwszy:

C/C++
#include <d3dx9.h>
#include <mmsystem.h>
#include <direct.h>

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#pragma comment(lib, "winmm.lib")

LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
LPDIRECT3DTEXTURE9 g_pTexture = NULL; // Our texture

D3DXMATRIX matWorldX; // Matrix for translate via X axis
D3DXMATRIX matWorldY; // matrix for rotate via X and Y axis
D3DXMATRIX matWorldXT; // matrix for translate via X axis

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x;
    FLOAT y;
    FLOAT z;
    DWORD color;
    FLOAT tx;
    FLOAT ty;
};

#define D3DFVF_CUSTOMVERTEX ( D3DFVF_XYZ | D3DFVF_DIFFUSE | D3DFVF_TEX1 )

// Function name : InitD3D
// Description     :
// Return type : HRESULT
// Argument         : HWND hWnd
HRESULT InitD3D( HWND hWnd )
{
    if( NULL ==( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
         return E_FAIL;
   
    D3DDISPLAYMODE d3ddm;
    if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, & d3ddm ) ) )
         return E_FAIL;
   
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( & d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = d3ddm.Format;
    d3dpp.EnableAutoDepthStencil = TRUE;
    d3dpp.AutoDepthStencilFormat = D3DFMT_D16;
   
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
    & d3dpp, & g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }
   
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
   
    return S_OK;
}

// Function name : InitGeometry
// Description     :
// Return type : HRESULT
HRESULT InitGeometry()
{
    CUSTOMVERTEX g_Vertices[] =
    {
        // front
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 3.0f, },
        { - 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 3.0f, 3.0f, },
       
        { - 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 3.0f, 3.0f, },
        { 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 3.0f, 0.0f, },
       
        // right
        { 1.0f, - 1.0f, - 1.0f, 0xFFFF0000, 0.0f, 1.0f, },
        { 1.0f, 1.0f, - 1.0f, 0xFFFF0000, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, 1.0f, 0xFFFF0000, 1.0f, 1.0f, },
       
        { 1.0f, 1.0f, - 1.0f, 0xFFFF0000, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, 1.0f, 0xFFFF0000, 1.0f, 1.0f, },
        { 1.0f, 1.0f, 1.0f, 0xFFFF0000, 1.0f, 0.0f, },
       
        // back
        { 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 1.0f, },
        { 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { - 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
       
        { 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { - 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 0.0f, },
       
        // left/ left
        { - 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 1.0f, },
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
       
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
        { - 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 0.0f, },
       
        // top/ top
        { - 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 1.0f, },
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
       
        { - 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
        { 1.0f, 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 0.0f, },
       
        // bootom/ bootom
        { - 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 0.0f, 1.0f, },
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
       
        { - 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 0.0f, 0.0f, },
        { 1.0f, - 1.0f, 1.0f, 0xFFFFFFFF, 1.0f, 1.0f, },
        { 1.0f, - 1.0f, - 1.0f, 0xFFFFFFFF, 1.0f, 0.0f, },
    };
   
    if( FAILED( D3DXCreateTextureFromFile( g_pd3dDevice, "Media/tex.bmp", & g_pTexture ) ) )
    {
        return E_FAIL;
    }
   
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 36 * sizeof( CUSTOMVERTEX ),
    0, D3DFVF_CUSTOMVERTEX,
    D3DPOOL_DEFAULT, & g_pVB ) ) )
    {
        return E_FAIL;
    }
   
    VOID * pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof( g_Vertices ),( BYTE ** ) & pVertices, 0 ) ) )
         return E_FAIL;
   
    memcpy( pVertices, g_Vertices, sizeof( g_Vertices ) );
    g_pVB->Unlock();
   
    return S_OK;
}

// Function name : Cleanup
// Description     :
// Return type : VOID
VOID Cleanup()
{
    if( g_pTexture != NULL )
         g_pTexture->Release();
   
    if( g_pVB != NULL )
         g_pVB->Release();
   
    if( g_pd3dDevice != NULL )
         g_pd3dDevice->Release();
   
    if( g_pD3D != NULL )
         g_pD3D->Release();
   
}

// Function name : SetupMatrices
// Description     :
// Return type : VOID
VOID SetupMatrices()
{
    D3DXMATRIX matView;
    D3DXMatrixLookAtLH( & matView, & D3DXVECTOR3( 0.0f, 0.0f, - 6.0f ),
    & D3DXVECTOR3( 0.0f, 0.0f, 0.0f ),
    & D3DXVECTOR3( 0.0f, 1.0f, 0.0f ) );
    g_pd3dDevice->SetTransform( D3DTS_VIEW, & matView );
   
    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH( & matProj, D3DX_PI / 4, 400.0f / 400.0f, 1.0f, 100.0f );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, & matProj );
}

// Function name : Render
// Description     :
// Return type : VOID
VOID Render()
{
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB( 0, 0, 0 ), 1.0f, 0 );
    g_pd3dDevice->BeginScene();
   
    D3DXMatrixRotationX( & matWorldX, timeGetTime() / 1500.0f );
    D3DXMatrixRotationY( & matWorldY, timeGetTime() / 1500.0f );
   
    g_pd3dDevice->SetTexture( 0, g_pTexture );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_MODULATE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG2, D3DTA_DIFFUSE );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_DISABLE );
   
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_MINFILTER, D3DTEXF_LINEAR );
    g_pd3dDevice->SetTextureStageState( 0, D3DTSS_MAGFILTER, D3DTEXF_LINEAR );
   
    g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof( CUSTOMVERTEX ) );
    g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX );
    g_pd3dDevice->SetTransform( D3DTS_WORLD, &( matWorldX * matWorldY ) );
    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 12 ); // front
   
    g_pd3dDevice->EndScene();
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

// Function name : MsgProc
// Description     :
// Return type : LRESULT WINAPI
// Argument         :  HWND hWnd
// Argument         : UINT msg
// Argument         : WPARAM wParam
// Argument         : LPARAM lParam
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
    switch( msg )
    {
    case WM_DESTROY:
        PostQuitMessage( 0 );
        return 0;
       
    case WM_KEYDOWN:
        switch( wParam )
        {
        case VK_ESCAPE:
            PostQuitMessage( 0 );
            break;
        }
    }
   
    return DefWindowProc( hWnd, msg, wParam, lParam );
}

// Function name : WinMain
// Description     :
// Return type : INT WINAPI
// Argument         :  HINSTANCE hInst
// Argument         : HINSTANCE
// Argument         : LPSTR
// Argument         : INT
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
        GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
        "D3D Tutorial", NULL };
    RegisterClassEx( & wc );
   
    // Create the application's window
    HWND hWnd = CreateWindow( "D3D Tutorial", "D3D Tutorial 06 - Texture",
    WS_OVERLAPPEDWINDOW, 100, 100, 350, 350,
    GetDesktopWindow(), NULL, wc.hInstance, NULL );
   
    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the scene geometry
        if( SUCCEEDED( InitGeometry() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );
            SetupMatrices();
           
            // Enter the message loop
            MSG msg;
            ZeroMemory( & msg, sizeof( msg ) );
            while( msg.message != WM_QUIT )
            {
                if( PeekMessage( & msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( & msg );
                    DispatchMessage( & msg );
                }
                else
                     Render();
               
            }
        }
    }
   
    // Clean up everything and exit the app
    Cleanup();
    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}


Kod który po przekształceniu działa poprawnie:
C/C++
#include <d3d9.h>

#pragma comment(lib, "d3d9.lib")

LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices

// A structure for our custom vertex type
struct CUSTOMVERTEX
{
    FLOAT x, y, z, rhw; // The transformed position for the vertex
    DWORD color; // The vertex color
};

// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)

// Desc: Initializes Direct3D
HRESULT InitD3D( HWND hWnd )
{
    // Create the D3D object.
    if( NULL ==( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
         return E_FAIL;
   
    // Get the current desktop display mode, so we can set up a back
    // buffer of the same format
    D3DDISPLAYMODE d3ddm;
    if( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, & d3ddm ) ) )
         return E_FAIL;
   
    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory( & d3dpp, sizeof( d3dpp ) );
    d3dpp.Windowed = TRUE;
    d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat = d3ddm.Format;
   
    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
    & d3dpp, & g_pd3dDevice ) ) )
    {
        return E_FAIL;
    }
   
    // Device state would normally be set here
   
    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: InitVB()
// Desc: Creates a vertex buffer and fills it with our vertices. The vertex
// buffer is basically just a chuck of memory that holds vertices. After
// creating it, we must Lock()/Unlock() it to fill it. For indices, D3D
// also uses index buffers. The special thing about vertex and index
// buffers is that the ycan be created in device memory, allowing some
// cards to process them in hardware, resulting in a dramatic
// performance gain.
//-----------------------------------------------------------------------------
HRESULT InitVB()
{
    // Initialize three vertices for rendering a triangle
    CUSTOMVERTEX g_Vertices[] =
    {
        { 150.0f, 50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
        { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
        { 50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
    };
   
    // Create the vertex buffer. Here we are allocating enough memory
    // (from the default pool) to hold all our 3 custom vertices. We also
    // specify the FVF, so the vertex buffer knows what data it contains.
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 3 * sizeof( CUSTOMVERTEX ),
    0, D3DFVF_CUSTOMVERTEX,
    D3DPOOL_DEFAULT, & g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }
   
    // Now we fill the vertex buffer. To do this, we need to Lock() the VB to
    // gain access to the vertices. This mechanism is required becuase vertex
    // buffers may be in device memory.
    VOID * pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof( g_Vertices ),( void ** ) & pVertices, 0 ) ) )
         return E_FAIL;
   
    memcpy( pVertices, g_Vertices, sizeof( g_Vertices ) );
    g_pVB->Unlock();
   
    return S_OK;
}

//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOID Cleanup()
{
    if( g_pVB != NULL )
         g_pVB->Release();
   
    if( g_pd3dDevice != NULL )
         g_pd3dDevice->Release();
   
    if( g_pD3D != NULL )
         g_pD3D->Release();
   
}

//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOID Render()
{
    // Clear the backbuffer to a blue color
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB( 0, 0, 255 ), 1.0f, 0 );
   
    // Begin the scene
    g_pd3dDevice->BeginScene();
   
    // Draw the triangles in the vertex buffer. This is broken into a few
    // steps. We are passing the vertices down a "stream", so first we need
    // to specify the source of that stream, which is our vertex buffer. Then
    // we need to let D3D know what vertex shader to use. Full, custom vertex
    // shaders are an advanced topic, but in most cases the vertex shader is
    // just the FVF, so that D3D knows what type of vertices we are dealing
    // with. Finally, we call DrawPrimitive() which does the actual rendering
    // of our geometry (in this case, just one triangle).
    g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof( CUSTOMVERTEX ) );
    g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
    g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
   
    // End the scene
    g_pd3dDevice->EndScene();
   
    // Present the backbuffer contents to the display
    g_pd3dDevice->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_DESTROY:
        PostQuitMessage( 0 );
        return 0;
    }
   
    return DefWindowProc( hWnd, msg, wParam, lParam );
}

//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = { sizeof( WNDCLASSEX ), CS_CLASSDC, MsgProc, 0L, 0L,
        GetModuleHandle( NULL ), NULL, NULL, NULL, NULL,
        "D3D Tutorial", NULL };
    RegisterClassEx( & wc );
   
    // Create the application's window
    HWND hWnd = CreateWindow( "D3D tutorial", "D3D tutorial 02 - Vertices",
    WS_OVERLAPPEDWINDOW, 100, 100, 350, 350,
    GetDesktopWindow(), NULL, wc.hInstance, NULL );
   
    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        // Create the vertex buffer
        if( SUCCEEDED( InitVB() ) )
        {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );
           
            // Enter the message loop
            MSG msg;
            ZeroMemory( & msg, sizeof( msg ) );
            while( msg.message != WM_QUIT )
            {
                if( PeekMessage( & msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( & msg );
                    DispatchMessage( & msg );
                }
                else
                     Render();
               
            }
        }
    }
   
    // Clean up everything and exit the app
    Cleanup();
    UnregisterClass( "D3D Tutorial", wc.hInstance );
    return 0;
}


Przepraszam za tak duże ilości kodu lecz chcę byście wszystko ujrzeli :)


P-41474
SeaMonster131
» 2011-09-26 20:56:12
Kod Ci nie działał i ok...przekształciłeś go i Ci działa...więc czego od nas oczekujesz? :)
P-41475
Trix
Temat założony przez niniejszego użytkownika
» 2011-09-27 18:18:04
Nie, nie, właśnie nie działa :)
Ten ostatni który działa to jest wersja lite czyli bez obiektów 3d (samo okienko):)
A gdy próbowałem przekształcić bardziej zaawansowany kod to już nie działał :)
I w tym mam problem :)

Chyba, że zainstalować wersję 8?
Ale skąd ją wziąć?
P-41492
SeaMonster131
» 2011-09-27 19:23:14
To nie przekształcaj kodu Direct8 na Direct9, tylko zobacz tutka odnośnie Direct9 :)
np http://www.two-kings.de/tutorials/dxgraphics/
P-41493
Trix
Temat założony przez niniejszego użytkownika
» 2011-09-28 18:19:17
Można i tak ale czasem mam problemy z odczytaniem treści w innym języku, zobaczymy jak mi pójdzie, jak na razie wszystko rozumiem (wstęp) :P



P.S.
Naukę angielskiego tak naprawdę mam od 3-4 tyg. gdyż w gimnazjum nauczycielka słabo uczyła :)
P-41515
akwes
» 2011-09-28 18:31:42
// offtop
Zatem trzeba było się go uczyć w domu : ) W pracy to żadna wymówka że nauczycielka nie powiedziała :P Jak ja bym się uczył programować ze szkoły... Ehhh : )
P-41516
pekfos
» 2011-09-28 18:36:47
Jak ja bym się uczył programować ze szkoły... Ehhh : )
Ja w takim przypadku nie wiedziałbym nawet, że jest coś takiego jak kompilator :P
P-41517
Trix
Temat założony przez niniejszego użytkownika
» 2011-09-29 19:21:14
@akwes
Wiem, że to żadna wymówka, lecz ja naukę nadal kontynuuję (jestem dopiero w 1 klasie Liceum) :)
A mój poprzedni post miał na celu powiadomienia was iż język jako tako umiem lecz by czytać dokumentacje itp. muszę niemal co 3 słówko sprawdzać w słowniku, a to strasznie zniechęca (progr. okienkowe c++ tak zaczynałem i kończyłem paręnaście razy :-p)
Dlatego wolałbym wszystkie kursy po polsku.
Na razie jest dobrze, przeczytałem 1 rozdział :P
Żmudnie mi to szło ale wreszcie się udało, lecz mam teraz problem z odpaleniem programu:P (założyłem nowy temat)
P-41539
« 1 »
  Strona 1 z 1