rendering gun mesh infront of camera?

This is a discussion on rendering gun mesh infront of camera? within the Game Programming forums, part of the General Programming Boards category; heres how i construct the camera movement i want to render an object such as a box or gun to ...

  1. #1
    Registered User
    Join Date
    Nov 2002
    Posts
    319

    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;
    }
    Last edited by Anddos; 06-01-2010 at 11:27 AM.

  2. #2
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    i want to render an object such as a box or gun to move forward with camera etc
    Well since nobody's replied with an actual answer I figure I'll give my two cents.

    I'd start by setting the gun's coordinates relative to the camera (so you'd transform to the camera pos, rot, scale, and then do local transforms on the gun from there).
    As for the gun itself, you have to take into account the clipping planes. So it has to be at least 1 unit from the camera, and depending on the scale you're working with (ie, what 1 unit represents in, say, meters) you might have to make the gun bigger than it would be otherwise.
    Also what I've noticed from other games is that the gun always has a minimal z-value. It's always displayed over every other object no matter where it is (except for the hud if there is one).

    Other than that it's just tweaking and your own preference.

    EDIT: And you might not have to make the whole gun - start up your favorite quake-based game, open up the console and type 'set fov 170'. Chances are you'll see the entire gun model, and that should give you a pretty good idea of how it works.
    Consider this post signed

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    1,262
    I only now read this thread...

    Is the gun a sprite or something similar, or an actual 3D object? Especially if it's just a sprite, it may be a lot easier to switch to orthogonal rendering temporarily.

  4. #4
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,596
    Gun = PlayerWorld * GunFrame

    Where GunFrame is the final frame matrix representing the local translation, rotation, scale, etc, for the gun.

    Essentially here PlayerWorld is the root matrix for GunFrame.

  5. #5
    Registered User
    Join Date
    Nov 2002
    Posts
    319
    I'd start by setting the gun's coordinates relative to the camera (so you'd transform to the camera pos, rot, scale, and then do local transforms on the gun from there).
    I am not sure what you mean by scaling the camera?, ive only scaled objects and what do you mean do local transforms on the gun ?

  6. #6
    Just a pushpin. bernt's Avatar
    Join Date
    May 2009
    Posts
    426
    what do you mean do local transforms on the gun
    I mean to say transformations on the local axes, so a coordinate system that has been translated, rotated, etc to the camera. Just a fancy way of saying
    Gun = PlayerWorld * GunFrame

    Where GunFrame is the final frame matrix representing the local translation, rotation, scale, etc, for the gun.
    in the sense that you have the camera transformations and the gun transformations relative to the camera and multiplying them together yields the effect you're looking for.

    I am not sure what you mean by scaling the camera
    Specifically I mean scaling the local axes. If you were to decide later on that you wanted the player to be bigger than he is now it would be much easier to just scale the camera rather than have to reposition the gun and everything. Like a scene graph.
    Consider this post signed

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. camera rotation matrix
    By Vick jr in forum Game Programming
    Replies: 5
    Last Post: 05-26-2009, 09:16 AM
  2. LNK2001 ERROR!!! need help
    By lifeafterdeath in forum C++ Programming
    Replies: 7
    Last Post: 05-27-2008, 06:05 PM
  3. OpenGL shooting game
    By sl4nted in forum Game Programming
    Replies: 6
    Last Post: 12-01-2006, 10:37 PM
  4. Mesh Rendering: Z-Buffer and Lighting
    By Epo in forum Game Programming
    Replies: 6
    Last Post: 04-20-2005, 08:11 AM
  5. Camera rotation/movement in 3D world
    By tegwin in forum Game Programming
    Replies: 11
    Last Post: 01-24-2003, 01:43 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21