Original Post
Greetings to everyone. I recently got a task to draw a sphere. So i figured that the only normal way to do it is to use DrawIndexedPrimitive(UP). So i decided to start by drawing a rectangle from 2 triangles. Here's the code. It doesn't work and i guess that's because of DIP function. What else...I'm familiar with DX sdk so cut it out. Also i used google and the only way it helped me is that it ran me into this place. Sorry for the size of code, but due to my beginning nature i thought i would be able to make mistakes anywhere. Thanx.
//-----------------------------------------------------------------------------
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>
LPDIRECT3D9 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE9 g_pd3dDevice = NULL; // Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB = NULL; // Buffer to hold vertices
struct CUSTOMVERTEX
{
FLOAT x, y, z,rhw;
DWORD color;
};
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW|D3DFVF_DIFFUSE)
HRESULT InitD3D( HWND hWnd )
{
if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
return E_FAIL;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferFormat = D3DFMT_UNKNOWN;
if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice ) ) )
{
return E_FAIL;
}
return S_OK;
}
HRESULT InitGeometry()
{
CUSTOMVERTEX g_Vertices[4];
g_Vertices[0].x = 150.0f;
g_Vertices[0].y = 50.0f;
g_Vertices[0].z = 0.5f;
g_Vertices[0].rhw = 1.0f;
g_Vertices[0].color = 0xffff0000;
g_Vertices[1].x = 250.0f;
g_Vertices[1].y = 250.0f;
g_Vertices[1].z = 0.5f;
g_Vertices[1].rhw = 1.0f;
g_Vertices[1].color = 0xff00ff00;
g_Vertices[2].x = 50.0f;
g_Vertices[2].y = 250.0f;
g_Vertices[2].z = 0.5f;
g_Vertices[2].rhw = 1.0f;
g_Vertices[2].color = 0xff00ffff;
g_Vertices[3].x = 100.0f;
g_Vertices[3].y = 150.0f;
g_Vertices[3].z = 0.5f;
g_Vertices[3].rhw = 1.0f;
g_Vertices[3].color = 0;
if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(CUSTOMVERTEX),
0, D3DFVF_CUSTOMVERTEX,
D3DPOOL_DEFAULT, &g_pVB, NULL)))
{
return E_FAIL;
}
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;
}
VOID Cleanup()
{
if( g_pVB != NULL )
g_pVB->Release();
if( g_pd3dDevice != NULL )
g_pd3dDevice->Release();
if( g_pD3D != NULL )
g_pD3D->Release();
}
VOID Render()
{
WORD ind[] = {0, 1, 2, 1, 2, 3};
g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
if( SUCCEEDED( g_pd3dDevice->BeginScene()))
{
g_pd3dDevice->SetStreamSource( 0, g_pVB, 0, sizeof(CUSTOMVERTEX));
g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX );
g_pd3dDevice->DrawIndexedPrimitiveUP(D3DPT_TRIANGLELIST, 0, 6,
2, ind, D3DFMT_INDEX16, &g_pVB[0], sizeof(CUSTOMVERTEX));
g_pd3dDevice->EndScene();
}
g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
Cleanup();
PostQuitMessage( 0 );
return 0;
}
return DefWindowProc( hWnd, msg, wParam, lParam );
}
INT WINAPI WinMain( HINSTANCE hInst, HINSTANCE, LPSTR, INT )
{
WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
"D3D", NULL };
RegisterClassEx( &wc );
HWND hWnd = CreateWindow( "D3D", "D_3_D",
WS_OVERLAPPEDWINDOW, 100, 100, 256, 256,
GetDesktopWindow(), NULL, wc.hInstance, NULL );
if( SUCCEEDED( InitD3D( hWnd ) ) )
{
if( SUCCEEDED( InitGeometry() ) )
{
ShowWindow( hWnd, SW_SHOWDEFAULT );
UpdateWindow( hWnd );
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();
}
}
}
UnregisterClass( "D3D", wc.hInstance );
return 0;
}