rendering gun mesh infront of camera?
heres how i construct the camera movement
i want to render an object such as a box or gun to move forward with camera etc
video of what i am explaining,
YouTube - Anddos25's Channel
Code:
void Camera::update(float dt, Terrain* terrain, float offsetHeight)
{
// Find the net direction the camera is traveling in (since the
// camera could be running and strafing).
D3DXVECTOR3 dir(0.0f, 0.0f, 0.0f);
if( gDInput->keyDown(DIK_W) )
dir += mLookW;
if( gDInput->keyDown(DIK_S) )
dir -= mLookW;
if( gDInput->keyDown(DIK_D) )
dir += mRightW;
if( gDInput->keyDown(DIK_A) )
dir -= mRightW;
//if( gDInput->keyDown(DIK_R) )
// dir -= mUpW;
static float y = 2.5f;
if( gDInput->keyDown(DIK_LSHIFT))
{
mSpeed += 0.5f;
}
// Move at mSpeed along net direction.
D3DXVec3Normalize(&dir, &dir);
D3DXVECTOR3 newPos = mPosW + dir*mSpeed*dt;
if( terrain != 0)
{
// New position might not be on terrain, so project the
// point onto the terrain.
newPos.y = terrain->getHeight(newPos.x, newPos.z) + offsetHeight;
// Now the difference of the new position and old (current)
// position approximates a tangent vector on the terrain.
D3DXVECTOR3 tangent = newPos - mPosW;
D3DXVec3Normalize(&tangent, &tangent);
// Now move camera along tangent vector.
mPosW += tangent*mSpeed*dt;
// After update, there may be errors in the camera height since our
// tangent is only an approximation. So force camera to correct height,
// and offset by the specified amount so that camera does not sit
// exactly on terrain, but instead, slightly above it.
mPosW.y = terrain->getHeight(mPosW.x, mPosW.z) + offsetHeight;
}
else
{
mPosW = newPos;
}
static float pitch;
static float yAngle;
//if mouse button down then rotate
if(gDInput->mMouseState.rgbButtons[0] == 0x80) //Left mouse down
{
// We rotate at a fixed speed.
pitch = gDInput->mouseDY() / 30.0f;
yAngle = gDInput->mouseDX() / 30.0f;
}
// Rotate camera's look and up vectors around the camera's right vector.
D3DXMATRIX R;
D3DXMatrixRotationAxis(&R, &mRightW, pitch);
D3DXVec3TransformCoord(&mLookW, &mLookW, &R);
D3DXVec3TransformCoord(&mUpW, &mUpW, &R);
// Rotate camera axes about the world's y-axis.
D3DXMatrixRotationY(&R, yAngle);
D3DXVec3TransformCoord(&mRightW, &mRightW, &R);
D3DXVec3TransformCoord(&mUpW, &mUpW, &R);
D3DXVec3TransformCoord(&mLookW, &mLookW, &R);
// Rebuild the view matrix to reflect changes.
buildView();
mViewProj = mView * mProj;
}
void Camera::buildView()
{
// Keep camera's axes orthogonal to each other and of unit length.
D3DXVec3Normalize(&mLookW, &mLookW);
D3DXVec3Cross(&mUpW, &mLookW, &mRightW);
D3DXVec3Normalize(&mUpW, &mUpW);
D3DXVec3Cross(&mRightW, &mUpW, &mLookW);
D3DXVec3Normalize(&mRightW, &mRightW);
// Fill in the view matrix entries.
//float x = -D3DXVec3Dot(&mPosW, &mRightW);
//float y = -D3DXVec3Dot(&mPosW, &mUpW);
//float z = -D3DXVec3Dot(&mPosW, &mLookW);
float x = -D3DXVec3Dot(&mPosW, &mRightW);
float y = -D3DXVec3Dot(&mPosW, &mUpW);
float z = -D3DXVec3Dot(&mPosW, &mLookW);
mView(0,0) = mRightW.x;
mView(1,0) = mRightW.y;
mView(2,0) = mRightW.z;
mView(3,0) = x;
mView(0,1) = mUpW.x;
mView(1,1) = mUpW.y;
mView(2,1) = mUpW.z;
mView(3,1) = y;
mView(0,2) = mLookW.x;
mView(1,2) = mLookW.y;
mView(2,2) = mLookW.z;
mView(3,2) = z;
mView(0,3) = 0.0f;
mView(1,3) = 0.0f;
mView(2,3) = 0.0f;
mView(3,3) = 1.0f;
}