Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Direct X Vertices Help

Matt2002Matt2002 Member Posts: 4
Hi
I'm very new to Direct X and i'm trying to write a simple program in Direct X 8.1.
What I want is to have a background object which will be a loaded bitmap and then a forground object, which will be created in Direct Draw using vertices.
The Background object loads in fine but the foreground object which is just a simple triangle loads in white, even though i've set colours for the points.
So what I want is for the triangle in the foreground to be in color or to be able to load a texture into it.

I'm not sure what I'm doing wrong
Here is the code:

#include
#include
#include

HRESULT InitD3D( HWND hWnd);
HRESULT InitVB();
VOID Cleanup();
VOID Render();
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

// A structure for the custom vertex type
struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw; // The transformed position for the vertex
FLOAT tu,tv; // Texture coordinates
};

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


// Global variables
LPDIRECT3D8 g_pD3D = NULL; // Used to create the D3DDevice
LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; // the rendering device
LPDIRECT3DSURFACE8 g_pBitmapSurface = NULL; // Object to hold image
LPDIRECT3DVERTEXBUFFER8 g_pVB = NULL; // Buffer to hold vertices
LPDIRECT3DTEXTURE8 g_pTexture = NULL; //the texture

//HRESULT g_hResult=S_OK;
//WinMain()
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 01: CreateDevice",
WS_OVERLAPPEDWINDOW, 100, 100, 640, 480,
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;
}

//MsgProc()
LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
{
switch( msg )
{
case WM_DESTROY:
PostQuitMessage( 0 );
return 0;

case WM_PAINT:
Render();
ValidateRect( hWnd, NULL );
return 0;
}

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


//InitD3D()
HRESULT InitD3D( HWND hWnd )
{
// Create the D3D object, which is needed to create the D3DDevice.
if( NULL == ( g_pD3D = Direct3DCreate8( D3D_SDK_VERSION ) ) )
return E_FAIL;

// Get the current desktop display mode
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;



if( FAILED(g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&d3dpp, &g_pd3dDevice )) )
{
return E_FAIL;
}
if (FAILED(g_pd3dDevice->CreateImageSurface(640, 480,
D3DFMT_X8R8G8B8, &g_pBitmapSurface)))return E_FAIL;


if (FAILED(D3DXLoadSurfaceFromFile(g_pBitmapSurface, NULL, NULL,
"dxbitmap.bmp", NULL, D3DX_DEFAULT, 0, NULL)))return E_FAIL;
// Device state would normally be set here

return S_OK;
}



//Cleanup()
VOID Cleanup()
{
if( g_pd3dDevice != NULL)
g_pd3dDevice->Release();

if( g_pD3D != NULL)
g_pD3D->Release();
if( g_pBitmapSurface != NULL)
g_pBitmapSurface->Release();

}





//Render()
VOID Render()
{

IDirect3DSurface8* pBackBuffer = NULL;
g_pd3dDevice->BeginScene();
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLORARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_COLOROP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1 );
g_pd3dDevice->SetTextureStageState( 0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE );
g_pd3dDevice->SetVertexShader( D3DFVF_XYZRHW|D3DFVF_TEX1 );
g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(CUSTOMVERTEX) );
g_pd3dDevice->SetVertexShader( D3DFVF_XYZRHW|D3DFVF_TEX1 );
g_pd3dDevice->GetBackBuffer(0, D3DBACKBUFFER_TYPE_MONO, &pBackBuffer);
g_pd3dDevice->CopyRects(g_pBitmapSurface,NULL,0,pBackBuffer,NULL);
g_pd3dDevice->SetTexture( 0, g_pTexture );
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1 );
g_pd3dDevice->EndScene();
g_pd3dDevice->Present(NULL, NULL, NULL, NULL);
if (pBackBuffer)
pBackBuffer->Release();

}

//InitVB()
HRESULT InitVB()
{
// Initialize three vertices for rendering a triangle
CUSTOMVERTEX g_Vertices[] =
{
{ 150.0f, 50.0f, 0.0f, 1.0f,0.0f ,0.5f }, // x, y, z, rhw, color
{ 250.0f, 250.0f, 0.0f, 1.0f,1.0f ,1.0f },
{ 50.0f, 250.0f, 0.0f, 1.0f,0.0f ,1.0f },
};

// 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( 6*sizeof(CUSTOMVERTEX),
0, D3DFVF_XYZRHW|D3DFVF_TEX1,
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;
}



Any help is appreciated

Thank you!

Comments

  • CroWCroW Member Posts: 348
    Hi, i dont like to read your whole src ;),but you havent defined a color for each vertex.d3d needs at least a color or a normal to give a triangle a color.dont know exactly about pretransformed vertices,but i think its a good idea to give them a color and to set a straight-white material.the white material shouldnt change the appearing of the texture.

    struct VERTEX
    {
    float x,y,z,rhw;
    unsigned int dwColor;
    float u,v;
    };


Sign In or Register to comment.