Archived

This topic is now archived and is closed to further replies.

Matty2003

Drawing multiple triangles in Direct draw

Recommended Posts

Matty2003    122
Hi How can I add another triangle shape using Direct draw to the code below? I''ve been trying to creat a square but the second triangle just doesn''t show up. Here''s the original code with the one triangle code:-------------------------------------------------------------------------------- #include <d3d8.h> #include <d3dx8tex.h> #include <mmsystem.h> HRESULT InitD3D( HWND hWnd); HRESULT InitVB(); VOID Cleanup(); VOID Render(); LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam); // A structure for our custom vertex type struct CUSTOMVERTEX { FLOAT x, y, z, rhw; // The transformed position for the vertex FLOAT tu,tv; // Texture coordinates }; // Our custom FVF, which describes our 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; // Our rendering device LPDIRECT3DSURFACE8 g_pBitmapSurface = NULL; // Object to hold image LPDIRECT3DVERTEXBUFFER8 g_pVB = NULL; // Buffer to hold vertices LPDIRECT3DTEXTURE8 g_pTexture = NULL; // Our 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 02: Vertices", 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[] = { { 20.0f, 110.0f, 0.0f, 1.0f,0.0f ,0.5f }, // x, y, z, rhw, color { 190.0f, 255.0f, 0.0f, 1.0f,1.0f ,1.0f }, { 20.0f, 255.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; } if( FAILED(D3DXCreateTextureFromFile(g_pd3dDevice, "image.jpg",&g_pTexture ) ) ) return S_OK; 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!

Share this post


Link to post
Share on other sites
PugPenguin    122
I have to go to bed so I dont have time to comb through your code, but I skim-read your code. Perhaps it something to do with this bit:

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();
}

More specifically:
g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 1);

The last parameter, 1, is the number of primitives you render. If you are drawing 2 triangles, that should read 2. If you are drawing 3 triangles, that should read 3. And so on. It says 1. That means that you are telling it to draw ONE triangle.

Square needs 2 triangles. So:

g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLELIST, 0, 2); // 2!!

might work. I dont know if you have problems in the rest of the code though.

Share this post


Link to post
Share on other sites