Skip to main content
gamedev.net
🔒 Locked

Why doesn't this code work?

Started by Zoltan Oct 18, 2005 at 1:05 AM 1 replies 600+ views
Original Post
Zoltan
Zoltan
I can't get my camera to work!! I have a triangle render on screen. I just can't initialize my camera to move anywhere else. This is my code... int D3D9::D3DInit(HWND hWnd) { RECT rect; GetClientRect(hWnd, ▭); if( NULL == ( m_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) ) { return E_FAIL; } if((m_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&m_d3ddm))!= D3D_OK) {return 0;} ZeroMemory(&m_d3dpp,sizeof(D3DPRESENT_PARAMETERS)); m_d3dpp.Windowed = true; m_d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD; m_d3dpp.BackBufferFormat = m_d3ddm.Format; m_d3dpp.FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT; m_d3dpp.Flags = D3DPRESENTFLAG_LOCKABLE_BACKBUFFER; m_d3dpp.BackBufferCount = 1; m_d3dpp.MultiSampleType = D3DMULTISAMPLE_NONE; m_d3dpp.hDeviceWindow = hWnd; //Let Direct3d manage the depth buffer //m_d3dpp.EnableAutoDepthStencil = true; //Set the depth buffer to 16 bits //m_d3dpp.AutoDepthStencilFormat = D3DFMT_D16; if( FAILED( m_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING, &m_d3dpp, &m_pd3dDevice ) ) ) { return E_FAIL; } //Create Vertex Buffer if( FAILED( m_pd3dDevice->CreateVertexBuffer( 3*sizeof(CUSTOMVERTEX), 0 /*Usage*/, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &m_pVB, NULL ) ) ) { return E_FAIL; } //Set ViewPort ZeroMemory(&m_vp, sizeof(D3DVIEWPORT9)); m_vp.X = rect.left; m_vp.Y = rect.top; m_vp.Width = rect.right; m_vp.Height = rect.bottom; m_vp.MinZ = 0.0f; m_vp.MaxZ = 1.0f; m_pd3dDevice->SetViewport(&m_vp); //Set Projection Matrix D3DXMATRIX matProj; ZeroMemory(&matProj, sizeof(D3DXMATRIX)); D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f ); m_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj ); //Set View Matrix D3DXVECTOR3 vEyePt ( 0.0f, 3.0f,-5.0f ); D3DXVECTOR3 vLookatPt( 100.0f, 0.0f, 0.0f ); D3DXVECTOR3 vUpVec ( 0.0f, 1.0f, 0.0f ); D3DXMatrixLookAtLH( &m_matView, &vEyePt, &vLookatPt, &vUpVec ); m_pd3dDevice->SetTransform( D3DTS_VIEW, &m_matView ); return 1; }
Armadon
Armadon
Zoltan,
Like I mentioned before ... the pattern.
If you follow it you will come to very few problems.

1. Create the View transformation matrix and set it.
i.e.
D3DXMATRIX matProj;
D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/6, 4.0f/3.0f, 1.0f, 1000.0);
pDevice->SetTransform(D3DTS_PROJECTION, &matProj);

2. Create the Projection transformation matrix and set it.
i.e.
D3DXMATRIX matView;
D3DXVECTOR3 vEyePt ( 0.0f, 0.0f, -5.0f ); //pull the eye back 5 units
D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f ); //look at the center of the world
D3DXVECTOR3 vUpVec ( 0.0f, 1.0f, 0.0f ); //Up is in the positive y direction.
D3DXMatrixLookAtLH( &m_matView, &vEyePt, &vLookatPt, &vUpVec );
pDevice->SetTransform(D3DTS_VIEW, &matView);

3. Now most of the time each object that you will render will have a world transformation matrix associated with it.. for instance your triangle will have a transformation matrix associated with it.
i.e.
D3DXMATRIX matWorld;
D3DXMatrixTranslation(&matWorld, 0.0f, 0.0f, 0.0f); //set the matrix in the middle of your view.

4. When you are then rendering your object set the world matrix associated with the object and render your object.
i.e.
pDevice->SetTransform(D3DTS_WORLD, &matWorld);
//render your object

Much simpler you won't get.
I hope this helps
Take care.

PS: try and keep all associated posts and questions in the same thread. :)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.