Check out and contribute to CodePedia, the wiki for developers.

DirectX

Moderators: Sephiroth
Number of threads: 90
Number of posts: 157

This Forum Only
Post New Thread

Report
view matrix resets itself. Posted by rickyoswald on 22 Nov 2009 at 10:03 AM
I have written a Camera class that utilises direct input to rotate view matrix. For some reason, the view rotates when I move the mouse but then suddenly snaps back to the original rotation. Any idea why?

Camera.cpp
#include "Camera.h"

Camera::Camera(D3DXVECTOR3 m_pos, LPDIRECT3DDEVICE9 m_pdxDevice, HINSTANCE hInst, HWND gameWindow)
{
	_position = m_pos;
	_look = D3DXVECTOR3(0,0,0);
	_up = D3DXVECTOR3(0,1,0);

	D3DXMatrixPerspectiveFovLH(&_projection, D3DX_PI/4, 500/500, 1, 500);
	m_pdxDevice->SetTransform(D3DTS_PROJECTION, &_projection);

	//----INITIALISE INPUT---------
	DirectInput8Create(hInst, DIRECTINPUT_VERSION, IID_IDirectInput8, (void**)&pDirectInput, NULL);
	pDirectInput->CreateDevice(GUID_SysKeyboard, &pKeyboard, NULL);
	pKeyboard->SetDataFormat(&c_dfDIKeyboard);
	pKeyboard->SetCooperativeLevel(gameWindow, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	pKeyboard->Acquire();

	pDirectInput->CreateDevice(GUID_SysMouse, &pMouse, NULL);
	pMouse->SetDataFormat(&c_dfDIMouse);
	pMouse->SetCooperativeLevel(gameWindow, DISCL_FOREGROUND | DISCL_NONEXCLUSIVE);
	pMouse->Acquire();
}

void Camera::ProcessKeyboard()
{
	char KeyboardState[256];

	pKeyboard->GetDeviceState(sizeof(KeyboardState),(LPVOID)&KeyboardState);
	if(KEYDOWN(KeyboardState, DIK_RIGHT)) { _position.x++; _look.x++; }
	if(KEYDOWN(KeyboardState, DIK_LEFT)) { _position.x--; _look.x--; }
	if(KEYDOWN(KeyboardState, DIK_UP)) { _position.z++; _look.z++; }
	if(KEYDOWN(KeyboardState, DIK_DOWN)) { _position.z--; _look.z--; }
}

void Camera::ProcessMouse()
{
	DIMOUSESTATE MouseState;
	pMouse->GetDeviceState(sizeof(MouseState),(LPVOID)&MouseState);

	_yaw = MouseState.lX / 80.0f;
	_pitch = MouseState.lY / 80.0f;
}

void Camera::Update(LPDIRECT3DDEVICE9 m_pdxDevice)
{
	ProcessKeyboard(); ProcessMouse();

	D3DXMatrixLookAtLH(&_view, &_position, &_look, &_up);
	m_pdxDevice->SetTransform(D3DTS_VIEW, &_view);

	_up=D3DXVECTOR3(0,1,0);
	_look=D3DXVECTOR3(0,0,1);
	_right=D3DXVECTOR3(1,0,0);

	D3DXMATRIX yawMatrix;
	D3DXMatrixRotationAxis(&yawMatrix, &_up, _yaw);
	// To apply yaw we rotate the m_look & m_right vectors about the m_up vector (using our yaw matrix)
	D3DXVec3TransformCoord(&_look, &_look, &yawMatrix); 
	D3DXVec3TransformCoord(&_right, &_right, &yawMatrix); 

	// Pitch is rotation around the x axis (m_right)
	// Create a matrix that can carry out this rotation
	D3DXMATRIX pitchMatrix;
	D3DXMatrixRotationAxis(&pitchMatrix, &_right, _pitch);
	// To apply pitch we rotate the m_look and m_up vectors about the m_right vector (using our pitch matrix)
	D3DXVec3TransformCoord(&_look, &_look, &pitchMatrix); 
	D3DXVec3TransformCoord(&_up, &_up, &pitchMatrix); 
		
	// Roll is rotation around the z axis (m_look)
	// Create a matrix that can carry out this rotation
	D3DXMATRIX rollMatrix;
	D3DXMatrixRotationAxis(&rollMatrix, &_look, _roll);
	// To apply roll we rotate up and right about the look vector (using our roll matrix)
	// Note: roll only really applies for things like aircraft unless you are implementing lean
	D3DXVec3TransformCoord(&_right, &_right, &rollMatrix); 
	D3DXVec3TransformCoord(&_up, &_up, &rollMatrix); 

	// Build the view matrix from the transformed camera axis
	D3DXMATRIX *m_view = &_view;
	D3DXMatrixIdentity(m_view);

	m_view->_11 = _right.x; m_view->_12 = _up.x; m_view->_13 = _look.x;
	m_view->_21 = _right.y; m_view->_22 = _up.y; m_view->_23 = _look.y;
	m_view->_31 = _right.z; m_view->_32 = _up.z; m_view->_33 = _look.z;
	
	m_view->_41 = - D3DXVec3Dot( &_position,&_right); 
	m_view->_42 = - D3DXVec3Dot( &_position,&_up);
	m_view->_43 = - D3DXVec3Dot( &_position,&_look);
}

void Camera::SetPosition(D3DXVECTOR3 m_pos)
{
	_position = m_pos;
}

Camera::~Camera(void)
{
}


Camera.h
#include<d3d9.h>
#include<d3dx9.h>
#include<dinput.h>
#define KEYDOWN(name, key) (name[key] & 0x80)
#pragma once

class Camera
{
public:
	Camera(D3DXVECTOR3 position, LPDIRECT3DDEVICE9 device, HINSTANCE hInst, HWND gameWindow);
	void Update(LPDIRECT3DDEVICE9 m_pdxDevice);

	void SetPosition(D3DXVECTOR3 position);

	float GetYaw() const {return _yaw;}
	float GetPitch() const {return _pitch;}
	float GetRoll() const {return _roll;}
	D3DXVECTOR3 GetPosition() const {return _position;}	
	D3DXMATRIX GetViewMatrix() const {return _view;}

	// Move operations
	void MoveForward(float amount);
	void MoveRight(float amount);
	void MoveUp(float amount);

	// Rotations
	void Yaw(float amount); // rotate around x axis
	void Pitch(float amount); // rotate around x axis
	void Roll(float amount); // rotate around z axis	
	
	~Camera(void);

private:
	D3DXVECTOR3 _position;
	D3DXVECTOR3 _up, _look, _right;
	float _yaw, _pitch, _roll;

	D3DXMATRIX _view;
	D3DXMATRIX _projection;	
	LPDIRECTINPUT8 pDirectInput; 
	LPDIRECTINPUTDEVICE8 pKeyboard;
	LPDIRECTINPUTDEVICE8 pMouse;

	void ProcessKeyboard(void);
	void ProcessMouse(void);

};




 
Popular resources and forums for programmers on Programmersheaven.com
Assembly, Basic, C, C#, C++, Delphi, Java, JavaScript, Pascal, Perl, PHP, Python, Ruby, Visual Basic
© Copyright 2009 Programmersheaven.com - All rights reserved.
Reproduction in whole or in part, in any form or medium without express written permission is prohibited.
Violators of this policy may be subject to legal action. Please read our Terms Of Use and Privacy Statement for more information.
Publisher: Lars Hagelin. Read the latest words from the publisher here.
Be the first to sign up for Lars Hagelin’s In-depth Outsourcing Newsletter here.
bootstrapLabs Logo A BootstrapLabs project.