Premetto che sono un neofita nell'utilizzo di Visual C++ e delle DirectX.
Ho il seguente codice:
//-----------------------------------------------------------------------
#include <windows.h>
#include <d3d8.h>
//---------------------------------------------------------------
// Puntatori globali. In questi puntatori metteremo gli indirizzi
// dell'oggetto Direct3D e della 3dDevice
LPDIRECT3D8 g_pD3D = NULL;
LPDIRECT3DDEVICE8 g_pd3dDevice = NULL;
// buffer per i vertici del nostro oggetto
LPDIRECT3DVERTEXBUFFER8 g_pVB = NULL;
// descrizione del singolo vertice - 3 coordinate ed un colore
#define D3DFVF_MIOVERTICE (D3DFVF_XYZ | D3DFVF_DIFFUSE)
// struttura per un singolo vertice - 3 coordinate ed un colore
struct MIOVERTICE
{
FLOAT x, y, z; // Posizione 3D del vertice
DWORD color; // Colore del vertice
};
// definizione dei colori principali
#define COLORE_ROSSO 0xFFFF0000
#define COLORE_VERDE 0xFF00FF00
#define COLORE_BLU 0xFF0000FF
#define COLORE_BIANCO 0xFFFFFFFF
#define COLORE_NERO 0xFF000000
// posizioni e colori dei vertici
MIOVERTICE Vertices[] =
{
{ -1.0f,-1.0f, 0.0f, COLORE_ROSSO, },
{ 1.0f,-1.0f, 0.0f, COLORE_BLU, },
{ 0.0f, 1.0f, 0.0f, COLORE_BIANCO, },
};
// --------------- Start3D() -----------------
// Inizializza la libreria Direct3D
HRESULT InitD3D(HWND hWnd )
{
// Crea l'oggetto Direct3D
if ( (g_pD3D = Direct3DCreate8( D3D_SDK_VERSION)) == NULL)
return E_FAIL;
// Trova il modo video
D3DDISPLAYMODE d3ddm;
if ( FAILED( g_pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT, &d3ddm)))
return E_FAIL;
// Crea il 3dDevice, usando un sacco di parametri di default. Vedi la lezione 4.
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = d3ddm.Format;
if ( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice)))
return E_FAIL;
// Lezione 6: crea il vertex buffer
if ( FAILED( g_pd3dDevice->CreateVertexBuffer( sizeof(MIOVERTICE) *3, 0, D3DFVF_MIOVERTICE, D3DPOOL_DEFAULT, &g_pVB )))
return E_FAIL;
// Turn off culling, so we see the front and back of the triangle
g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
// Turn off D3D lighting, since we are providing our own vertex colors
g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, FALSE );
return S_OK;
}
//------------- CleanUp() ----------
// Rilascia tutti gli oggetti creati. E' un passo fondamentale
// per un'uscita "pulita" del programma.
void CleanUp()
{
if ( g_pVB != NULL)
g_pVB->Release();
if( g_pd3dDevice)
g_pd3dDevice->Release();
if( g_pD3D)
g_pD3D->Release();
}
//------------- Render() ---------------
// Disegna la scena.
void Render()
{
static int color=0;
// Assicurati che il 3dDevice esista
if ( g_pd3dDevice == NULL)
return;
// Pulisce il backbuffer con un colore noto
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0, color), 1.0f, 0 );
// Inizia la scena
g_pd3dDevice->BeginScene();
// Lezione 6: Deforma il triangolo
Vertices[0].x *= 0.9999f;
Vertices[1].x *= 0.9999f;
Vertices[2].y *= 0.9999f;
// Lezione 6: riempi il vertex buffer
VOID* pointer;
if( FAILED( g_pVB->Lock( 0, sizeof(Vertices), (BYTE**)&pointer, 0 )))
return;
memcpy( pointer, Vertices, sizeof(Vertices) );
g_pVB->Unlock();
// Lezione 6: Disegna un oggetto
g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(MIOVERTICE) );
g_pd3dDevice->SetVertexShader( D3DFVF_MIOVERTICE );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
// Finisce la scena
g_pd3dDevice->EndScene();
// Mostra la scena (il backbuffer) sullo schermo
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
// Cambia il colore per dare un po' di vita
color = (color+1) % 256;
}
//----------- MsgProc() ---------------
// Procedura dei messaggi
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 );
}
//-------- WinMain() ------------------
// Funzione principale di Windows, tutta roba molto comune...
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
// Registra la classe della finestra.
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D", NULL };
RegisterClassEx( &wc );
// Crea la finestra
HWND hWnd = CreateWindow( "D3D", "Corso di DirecX: lezione 6",
WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
// Fai partire il Direct3d
if( SUCCEEDED( InitD3D( hWnd)))
{
// Mostra la finestra
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
// Loop dei messaggi. Questo è speciale per i nostri scopi
// (Vedi la lezione 4).
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();
}
}
// Ripulisci il tutto ed esci
CleanUp();
UnregisterClass( "D3D", wc.hInstance );
return 0;
}
Ottengo un errore in fase di compilazione.....non so dirvi di più
tulipan