Archived

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

chris1234

screen coordinates...

Recommended Posts

I''m want to draw a menu/item bar for a game to screen using screen coordinates (Transformed and Lit Vertices) I used an example from the directx SDK to transform it in a simple app to display a single transformed and lit square to screen not using textures yet...but it isn''t working at all, it just displays a sort of triangle. one point in the center of the screen the other two out of the top right of the screen. I''ve included the source code (below) of my test-app..can someone tell me what I''m doing wrong and how to fix this... Chris #include #include //----------------------------------------------------------------------------- // Global variables //----------------------------------------------------------------------------- LPDIRECT3D8 g_pD3D = NULL; LPDIRECT3DDEVICE8 g_pd3dDevice = NULL; LPDIRECT3DVERTEXBUFFER8 g_pVB = NULL; LPDIRECT3DVERTEXBUFFER8 g_pVB2 = NULL; LPDIRECT3DTEXTURE8 g_pTexture = NULL; // A structure for our custom vertex type. We added texture coordinates struct TLVertex { FLOAT x, y, z, rhw; D3DCOLOR Specular, Diffuse; FLOAT tu1, tv1; }; #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_DIFFUSE | D3DFVF_SPECULAR | D3DFVF_TEX1 ) //----------------------------------------------------------------------------- // Name: InitD3D() // Desc: Initializes Direct3D //----------------------------------------------------------------------------- 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; if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &g_pd3dDevice ) ) ) { return E_FAIL; } return S_OK; } HRESULT InitGeometry() { if( FAILED( g_pd3dDevice->CreateVertexBuffer( 4*sizeof(TLVertex), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &g_pVB ) ) ) return E_FAIL; TLVertex* pVertices; if( FAILED( g_pVB->Lock( 0, 0, (BYTE**)&pVertices, 0 ) ) ) return E_FAIL; pVertices[0].x = 40.0f; pVertices[0].y = 40.0f; pVertices[0].z = 0.0f; pVertices[0].rhw = 1.0f; pVertices[0].Diffuse = 0xffff0000; pVertices[0].Specular = 0xff00ff00; pVertices[0].tu1 = 0.0f; pVertices[0].tv1 = 0.0f; pVertices[1].x = 100.0f; pVertices[1].y = 40.0f; pVertices[1].z = 0.0f; pVertices[1].rhw = 1.0f; pVertices[1].Diffuse = 0xff00ff00; pVertices[1].Specular = 0xff00ffff; pVertices[1].tu1 = 0.0f; pVertices[1].tv1 = 0.0f; pVertices[2].x = 40.0f; pVertices[2].y = 100.0f; pVertices[2].z = 0.0f; pVertices[2].rhw = 1.0f; pVertices[2].Diffuse = 0xffff00ff; pVertices[2].Specular = 0xff000000; pVertices[2].tu1 = 0.0f; pVertices[2].tv1 = 0.0f; pVertices[3].x = 100.0f; pVertices[3].y = 100.0f; pVertices[3].z = 0.0f; pVertices[3].rhw = 1.0f; pVertices[3].Diffuse = 0xffffff00; pVertices[3].Specular = 0xffff0000; pVertices[3].tu1 = 0.0f; pVertices[3].tv1 = 0.0f; g_pVB->Unlock(); return S_OK; } 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(); } VOID Render() { g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,255,255), 1.0f, 0 ); g_pd3dDevice->BeginScene(); g_pd3dDevice->SetStreamSource( 0, g_pVB, sizeof(TLVertex) ); g_pd3dDevice->SetVertexShader( D3DFVF_CUSTOMVERTEX ); g_pd3dDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 4 ); g_pd3dDevice->EndScene(); 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", "", WS_OVERLAPPEDWINDOW, 100, 100, 800, 600, 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 ); // 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; }

Share this post


Link to post
Share on other sites
Hi!

In the function Render, you make a call to DrawPrimitive that is wrong. The last parameter of DrawPrimitive is the number of primitives you are drawing, in your case, you are drawing triangles, so the last parameter must been 2 instead 4 (You are using four vertices, but that''s not the info that DrawPrimitive expected).




"If you''''re gonna die, die with your boots on"

Share this post


Link to post
Share on other sites