My Matrices not working

Started by
1 comment, last by Supernat02 18 years, 1 month ago
#include <d3d9.h>
#include <Windows.h>
#include <mmsystem.h>
#include <d3dx9.h>

LPDIRECT3D9		d3d       =		NULL;
LPDIRECT3DDEVICE9	d3dd	  =		NULL;
LPDIRECT3DVERTEXBUFFER9	d3dvb	  =	        NULL;

struct		CUSTOMVERTEX
{
	float x,y,z,rhw;
	DWORD	color;
};


HRESULT InitD3D(HWND	hWnd)
{
	d3d	=Direct3DCreate9(D3D_SDK_VERSION	);
	
	D3DPRESENT_PARAMETERS			d3dpp;
	ZeroMemory(&d3dpp, sizeof(d3dpp));
	d3dpp.Windowed		=		TRUE;
	d3dpp.SwapEffect	  =		   D3DSWAPEFFECT_DISCARD;


	d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
			D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &d3dd);




	return S_OK;
}


HRESULT InitGEO()
{
    CUSTOMVERTEX vertices[] =
    {
        { 150.0f,  50.0f, 0.5f, 1.0f, 0xffff0000, }, // x, y, z, rhw, color
        { 250.0f, 250.0f, 0.5f, 1.0f, 0xff00ff00, },
        {  50.0f, 250.0f, 0.5f, 1.0f, 0xff00ffff, },
    };


	d3dd->CreateVertexBuffer(3 * sizeof(CUSTOMVERTEX), 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE, D3DPOOL_DEFAULT, &d3dvb, NULL);


	VOID* pVertices;

	d3dvb->Lock(0, sizeof(vertices), &pVertices, 0);
	memcpy(pVertices, vertices, sizeof(vertices));
	d3dvb->Unlock();

	return S_OK;
}

VOID SetupMatrices()
{
	
 
    D3DXMATRIXA16 matWorld;


    UINT  iTime  = timeGetTime() % 1000;
    FLOAT fAngle = iTime * (2.0f * D3DX_PI) / 1000.0f;
    D3DXMatrixRotationY( &matWorld, fAngle );
    d3dd->SetTransform( D3DTS_WORLD, &matWorld );


    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.0f );
    D3DXVECTOR3 vLookatPt( -1.0f, 0.0f, 0.0f );
    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMATRIXA16 matView;
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
    d3dd->SetTransform( D3DTS_VIEW, &matView );


    D3DXMATRIXA16 matProj;
    D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f );
    d3dd->SetTransform( D3DTS_PROJECTION, &matProj );
}

VOID Render()
{
	d3dd->Clear(0,NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(100,65,100), 1.0f, 0);

	d3dd->BeginScene();
	SetupMatrices();
	d3dd->SetStreamSource(0, d3dvb,0, sizeof(CUSTOMVERTEX));
	d3dd->SetFVF(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);
	d3dd->DrawPrimitive(D3DPT_TRIANGLELIST, 0,1);
	d3dd->EndScene();

	d3dd->Present(NULL,NULL,NULL,NULL);

}

void Cleanup()
{
	d3d->Release();
	d3dd->Release();
	d3dvb->Release();
}


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,
                      "My Prog", NULL };
    RegisterClassEx( &wc );


    HWND hWnd = CreateWindow( "My Prog", "MyProg",
                              WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
                              NULL, NULL, wc.hInstance, NULL );


    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
        
        if( SUCCEEDED( InitGEO() ) )
        {
            
            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( "My Prog", wc.hInstance );
    return 0;
}


i cant see: martix rotation and D3DXMatrixLookAtLH,D3DXMatrixPerspectiveFovLH not working , where is the problem? EDIT: 'source' tags instead of 'code' tags...
Advertisement
Quote:Original post by gmax
i cant see: martix rotation and D3DXMatrixLookAtLH,D3DXMatrixPerspectiveFovLH not working , where is the problem?
The problem is that you're using D3DFVF_XYZRHW. That vertex format tells Direct3D that you're passing in pre-transformed geometry and as a consequence it will skip all lighting and transformation stages in the pipeline. You've told Direct3D to ignore your matrices [wink]

Change it to a regular D3DFVF_XYZ type format and you should be fine.

hth
Jack


<hr align="left" width="25%" />
Jack Hoxley <small>[</small><small> Forum FAQ | Revised FAQ | MVP Profile | Developer Journal ]</small>

Also use the D3DXMatrixOrthoLH or D3DXMatrixOrthoOffCenterLH function if you want to still use the screen coordinates.
Chris ByersMicrosoft DirectX MVP - 2005

This topic is closed to new replies.

Advertisement