Sign in to follow this  

My Matrices not working

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

#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...

Share this post


Link to post
Share on other sites
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


Share this post


Link to post
Share on other sites

This topic is 4303 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this