Sign in to follow this  
Pushapjit

Moving Camera in direct3d

Recommended Posts

I am rendering a 3d model using .X file. model is rendering properly. -- The problem is that I was unbale to move the camera. my UpdateCamera() function is handling user input. I am calling this function from Rener() function
VOID CMyD3DApplication::UpdateCamera()
{
	FLOAT fElapsedTime;

    if( m_fElapsedTime > 0.0f )
        fElapsedTime = m_fElapsedTime;
    else
        fElapsedTime = 0.05f;

    FLOAT fSpeed        = 10.0f*fElapsedTime;
    FLOAT fAngularSpeed = 1.0f*fElapsedTime;

    // De-accelerate the camera movement (for smooth motion)
    m_vVelocity      *= 0.9f;
    m_fYawVelocity   *= 0.9f;
    m_fPitchVelocity *= 0.9f;

    // Process keyboard input
    if( m_bKey[VK_RIGHT] )    m_vVelocity.x    += fSpeed; // Slide Right
    if( m_bKey[VK_LEFT] )     m_vVelocity.x    -= fSpeed; // Slide Left
    if( m_bKey[VK_UP] )       m_vVelocity.y    += fSpeed; // Slide Up
    if( m_bKey[VK_DOWN] )     m_vVelocity.y    -= fSpeed; // Slide Down
    if( m_bKey['W'] )         m_vVelocity.z    += fSpeed; // Move Forward
    if( m_bKey['S'] )         m_vVelocity.z    -= fSpeed; // Move Backward
    if( m_bKey['E'] )         m_fYawVelocity   += fSpeed; // Turn Right
    if( m_bKey['Q'] )         m_fYawVelocity   -= fSpeed; // Turn Left
    if( m_bKey['Z'] )         m_fPitchVelocity += fSpeed; // Turn Down
    if( m_bKey['A'] )         m_fPitchVelocity -= fSpeed; // Turn Up
    
    // Update the position vector
    D3DXVECTOR3 vT = m_vVelocity * fSpeed;
    D3DXVec3TransformNormal( &vT, &vT, &m_matOrientation );
    m_vPosition += vT;
    if( m_vPosition.y < 1.0f )
        m_vPosition.y = 1.0f;

    // Update the yaw-pitch-rotation vector
    m_fYaw   += fAngularSpeed * m_fYawVelocity;
    m_fPitch += fAngularSpeed * m_fPitchVelocity;
   
	if( m_fPitch < 0.0f )      m_fPitch = 0.0f;
    if( m_fPitch > D3DX_PI/2 ) m_fPitch = D3DX_PI/2;

    // Set the view matrix
    D3DXQUATERNION qR;
    D3DXQuaternionRotationYawPitchRoll( &qR, m_fYaw, m_fPitch, 0.0f );
    D3DXMatrixAffineTransformation( &m_matOrientation, 1.25f, NULL, &qR, 
&m_vPosition );
    D3DXMatrixInverse( &matView, NULL, &m_matOrientation );
	
}

how should i solve this? thanxs -- P. S. Sidhu Centre for Development of Advanced computing Govt. of India [Edited by - Coder on November 7, 2005 5:02:41 AM]

Share this post


Link to post
Share on other sites
Thanxs for your reply. I was able to solve my problem.
I was missing the SetTransform() function. Now, My camera is moving according to inputs.

Can you please tell me which is the right place to call UpdateCamera() function?
Is is Render() or FrameMove(). Both the ways it is working.
But my model is moving very slowely. The size of .X file itself is more than 1.2 MB.
my Graphics card is Nvidia GeForce4 MX440 with 128MB mem.

Share this post


Link to post
Share on other sites
Well since you would ideally want to update the camera matrix only if a key is pressed (in your case), there is little point in calling it every frame. One way would be to call your UpdateCamera function in a UpdateInput function that checks if there is actually any valid camera transformation required according to input. You update the camera matrix (matView) only if a valid key is pressed.

This may not solve the problem with speed though! Speed issues could be due to various reasons. Some of them being:

1) There are too many render state changes happening every frame. You may want to optimize your mesh, re-order the subsets by material, and when rendering, render subsets by material.
2) Your scene/model has an extremely high polygon count. Try reducing the polygon count.
3) You may be using some complicated lighting like spotlights or too many light sources?
4) Your card (which is a low end card) may just not be able to keep up with the amount of data coming in. You may want to try view-frustrum culling.
5) Are you doing any ray intersection tests anywhere? If you are doing so on the entire scene/model, it may be slowing things down.

However, these are only suggestions based on assumptions. It may be the case that you may not need to do any of the above and there is something else hogging up the speed. Try code profiling and see if the Render function is indeed taking up the maximum time.

Share this post


Link to post
Share on other sites
a) Following are the render states and lights set by me in RestoreDeviceObjects():-

// Setup render state
m_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_DITHERENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
m_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x33333333 );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR );
m_pd3dDevice->SetSamplerState( 0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR );

// Setup the light
D3DLIGHT9 light;
D3DUtil_InitLight( light, D3DLIGHT_DIRECTIONAL, 0.0f,-1.0f, 1.0f );
m_pd3dDevice->SetLight(0, &light );
m_pd3dDevice->LightEnable(0, TRUE );



b) yes, my model is high on polygon counts
c) my card is 128MB
Will it make any difference if I use card with 256MB. Actually, my AGP bus is 4X, which only supports max of 258MB.

One more question - When I render my Model using DirectX code, my model is completely missing some textures? Why is it so?

Share this post


Link to post
Share on other sites
Your render states setup seems to be fine. However, what I mean was are you switching between many render states during the render loop? Would be better if you posted your render loop.

However, I think the problem is probably that your polygon count is too high and your card/computer is not able to keep up.

For textures you need to load them in and then SetTexture before you render the mesh. If your model has multiple textures (as I suspect it does) then you need to render the subsets by material and set the corresponding texture.

Check the DirectX article section to see more. You could also examine the DirectX sample apps to see how exactly it's done.

Share this post


Link to post
Share on other sites


//Begin the Scene
if(SUCCEEDED (m_pd3dDevice->BeginScene()) )
//set and draw each of the materials in the mesh
for( UINT i=0; i < m_dwNumMaterials; i++)
{
m_pd3dDevice->SetMaterial(&m_pMaterials[i].MatD3D);
m_pd3dDevice->SetTexture(0, m_ppTextures[i]);

m_pMeshEnhanced->DrawSubset(i);
}

// End the scene.
m_pd3dDevice->EndScene();


nevertheless, now I am able to render my model with all textures.
will Optimizing the mesh would serve any purpose? If yes, then which optimize version should I use - V Cache Optimize, Triangle list, Many strips etc.

Share this post


Link to post
Share on other sites

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