Camera...



  • Hi Jungs

    Ich habe vor geraumer Zeit ein Softwareprojekt gestartet, was jetzt 5 Monate auf Eis lag. Mein Problem ist meien cameraklasse, wenn ich die Maus in y-Richtung Bewege, dürfte sich das Blickfeld nur heben bzw senken (in z Richtung) jedoch verzieht es sich außerdem nach links bzw rechts.

    Test:

    nput::get_instance().update();
    
    	if(Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_UP, false) ||
    	   Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_W, false))
    		cam.move_forward(0.01f);
    
    	if(Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_DOWN, false) ||
    	   Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_S, false))
    		cam.move_backward(0.01f);
    
    	if(Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_LEFT, false) ||
    	   Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_A, false))
    		cam.move_left(0.01f);
    
    	if(Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_RIGHT, false) ||
    	   Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_D, false))
    		cam.move_right(0.01f);
    
    	if(Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_O, false))
    		cam.move_up(0.01f);
    
    	if(Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_L, false))
    		cam.move_down(0.01f);
    
    	if(Input::get_instance().is_pressed(INPUT_DEVICE_KEYBOARD, KEY_ESCAPE, true))
    		PostQuitMessage(0);
    
    	float y = static_cast<float>(deg_to_rad<float>(Input::get_instance().get_position(INPUT_DEVICE_MOUSE, POSITION_DELTA, AXIS_Y)));
    	float z = static_cast<float>(deg_to_rad<float>(Input::get_instance().get_position(INPUT_DEVICE_MOUSE, POSITION_DELTA, AXIS_X)));
    
    	static float cur_rot_x = 0;
    	cur_rot_x -= y;
    
    	Vector3<float> axis = cross_product(cam.get_center() - cam.get_position(), cam.get_up());
    	axis.normalize();
    	cam.rotate_view(y, axis);
    	cam.rotate_view(z, Vector3<float>(0.0f, 0.0f, 1.0f));
    
    	cam.look_at_scene();[cpp]
    
    Klasse:
    [cpp]
    
    #include "Camera.hpp"
    
    namespace dme
    {
    
    Camera::Camera(void) : position_(Vector3<float>(0.0f, 0.0f, 1.80f)),
    center_(Vector3<float>(0.0f, 1.0f, 1.80f)), up_(Vector3<float>(0.0f, 0.0f, 1.0f))
    {
    	normalize();
    }
    
    Camera::Camera(const Vector3<float>& position, const Vector3<float>& center, 
    			   const Vector3<float>& up) : position_(position), center_(center), up_(up)
    {
    	normalize();
    }
    
    Camera::Camera(const Camera& other) : position_(other.position_), center_(other.center_),
    up_(other.up_), view_(other.view_), normal_(other.normal_)
    {
    
    }
    
    Camera::~Camera(void)
    {
    
    }
    
    Camera& Camera::operator =(const Camera& other)
    {
    	if(&other != this)
    	{
    		position_	= other.position_;
    		center_		= other.center_;
    		up_			= other.up_;
    		view_		= other.view_;
    		normal_		= other.normal_;
    	}
    
    	return(*this);
    }
    
    void Camera::normalize(void) // ???
    {
    	view_ = center_ - position_;
    
    	normal_.x_ = (view_.y_ * up_.z_) - (up_.y_ * view_.z_);
    	normal_.y_ = (view_.z_ * up_.x_) - (up_.z_ * view_.x_);
    	normal_.z_ = (view_.x_ * up_.y_) - (up_.x_ * view_.y_);
    
    	normal_ /= sqrtf(normal_.x_ * normal_.x_ + 
    					 normal_.y_ * normal_.y_ + 
    					 normal_.z_ * normal_.z_);
    }
    
    void Camera::move_left(float distance)
    {
    	position_.x_	+= (normal_.x_ * -distance);
    	position_.y_	+= (normal_.y_ * -distance);
    	position_.z_	+= (normal_.z_ * -distance);
    
    	center_.x_		+= (normal_.x_ * -distance);
    	center_.y_		+= (normal_.y_ * -distance);
    	center_.z_		+= (normal_.z_ * -distance);
    }
    
    void Camera::move_right(float distance)
    {
    	position_.x_	+= (normal_.x_ * distance);
    	position_.y_	+= (normal_.y_ * distance);
    	position_.z_	+= (normal_.z_ * distance);
    
    	center_.x_		+= (normal_.x_ * distance);
    	center_.y_		+= (normal_.y_ * distance);
    	center_.z_		+= (normal_.z_ * distance);
    }
    
    void Camera::move_up(float distance)
    {
    	position_.x_	+= (up_.x_ * distance);
    	position_.y_	+= (up_.y_ * distance);
    	position_.z_	+= (up_.z_ * distance);
    
    	center_.x_		+= (up_.x_ * distance);
    	center_.y_		+= (up_.y_ * distance);
    	center_.z_		+= (up_.z_ * distance);
    }
    
    void Camera::move_down(float distance)
    {
    	position_.x_	+= (up_.x_ * -distance);
    	position_.y_	+= (up_.y_ * -distance);
    	position_.z_	+= (up_.z_ * -distance);
    
    	center_.x_		+= (up_.x_ * -distance);
    	center_.y_		+= (up_.y_ * -distance);
    	center_.z_		+= (up_.z_ * -distance);
    }
    
    void Camera::move_forward(float distance)
    {
    	position_.x_	+= (view_.x_ * distance);
    	position_.y_	+= (view_.y_ * distance);
    	position_.z_	+= (view_.z_ * distance);
    
    	center_.x_		+= (view_.x_ * distance);
    	center_.y_		+= (view_.y_ * distance);
    	center_.z_		+= (view_.z_ * distance);
    }
    
    void Camera::move_backward(float distance)
    {
    	position_.x_	+= (view_.x_ * -distance);
    	position_.y_	+= (view_.y_ * -distance);
    	position_.z_	+= (view_.z_ * -distance);
    
    	center_.x_		+= (view_.x_ * -distance);
    	center_.y_		+= (view_.y_ * -distance);
    	center_.z_		+= (view_.z_ * -distance);
    }
    
    void Camera::rotate_view(float angle, const Vector3<float>& axis) // ???
    {
    	Vector3<float>	new_view;
    	Vector3<float>	view = center_ - position_;
    
    	float cos_theta = cosf(angle);
    	float sin_theta = sinf(angle);
    
    	new_view.x_  = (cos_theta + (1 - cos_theta) * axis.x_ * axis.x_)			* view.x_;
    	new_view.x_ += ((1 - cos_theta) * axis.x_  * axis.y_ - axis.z_ * sin_theta) * view.y_;
    	new_view.x_ += ((1 - cos_theta) * axis.x_  * axis.z_ + axis.y_ * sin_theta) * view.z_;
    
    	new_view.y_  = ((1 - cos_theta) * axis.x_  * axis.y_ + axis.z_ * sin_theta) * view.x_;
    	new_view.y_ += (cos_theta + (1 - cos_theta) * axis.y_ * axis.y_)			* view.y_;
    	new_view.y_ += ((1 - cos_theta) * axis.y_  * axis.z_ - axis.x_ * sin_theta) * view.z_;
    
    	new_view.z_  = ((1 - cos_theta) * axis.x_  * axis.z_ - axis.y_ * sin_theta) * view.x_;
    	new_view.z_ += ((1 - cos_theta) * axis.y_  * axis.z_ + axis.x_ * sin_theta) * view.y_;
    	new_view.z_ += (cos_theta + (1 - cos_theta) * axis.z_ * axis.z_)			* view.z_;
    
    	center_ = position_ + new_view;
    
    	normalize();
    }
    
    void Camera::look_at_scene(void) const
    {
    	//glMatrixMode(GL_MODELVIEW);
    	glLoadIdentity();
    	gluLookAt(position_.x_, position_.y_,	position_.z_, 
    		      center_.x_,	center_.y_,		center_.z_,
    			  up_.x_,		up_.y_,			up_.z_);
    }
    
    void Camera::set_camera(const Vector3<float>& position, const Vector3<float>& 
    						center, const Vector3<float>& up)
    {
    	position_	= position;
    	center_		= center;
    	up_			= up;
    
    	normalize();
    }
    
    Vector3<float> Camera::get_position(void) const
    {
    	return(position_);
    }
    
    Vector3<float> Camera::get_center(void) const
    {
    	return(center_);
    }
    
    Vector3<float> Camera::get_up(void) const
    {
    	return(up_);
    }
    
    Vector3<float> Camera::get_view(void) const
    {
    	return(view_);
    }
    
    Vector3<float> Camera::get_normal(void) const
    {
    	return(normal_);
    }
    
    }
    
    Meiner Meinung nach liegt hier der Hund begraben:
    void Camera::rotate_view(float angle, const Vector3<float>& axis) // ???
    {
    	Vector3<float>	new_view;
    	Vector3<float>	view = center_ - position_;
    
    	float cos_theta = cosf(angle);
    	float sin_theta = sinf(angle);
    
    	new_view.x_  = (cos_theta + (1 - cos_theta) * axis.x_ * axis.x_)			* view.x_;
    	new_view.x_ += ((1 - cos_theta) * axis.x_  * axis.y_ - axis.z_ * sin_theta) * view.y_;
    	new_view.x_ += ((1 - cos_theta) * axis.x_  * axis.z_ + axis.y_ * sin_theta) * view.z_;
    
    	new_view.y_  = ((1 - cos_theta) * axis.x_  * axis.y_ + axis.z_ * sin_theta) * view.x_;
    	new_view.y_ += (cos_theta + (1 - cos_theta) * axis.y_ * axis.y_)			* view.y_;
    	new_view.y_ += ((1 - cos_theta) * axis.y_  * axis.z_ - axis.x_ * sin_theta) * view.z_;
    
    	new_view.z_  = ((1 - cos_theta) * axis.x_  * axis.z_ - axis.y_ * sin_theta) * view.x_;
    	new_view.z_ += ((1 - cos_theta) * axis.y_  * axis.z_ + axis.x_ * sin_theta) * view.y_;
    	new_view.z_ += (cos_theta + (1 - cos_theta) * axis.z_ * axis.z_)			* view.z_;
    
    	center_ = position_ + new_view;
    
    	normalize();
    }
    

    [/cpp]
    denn: lasse ich

    new_view.x_  = (cos_theta + (1 - cos_theta) * axis.x_ * axis.x_)			* view.x_;
    	new_view.x_ += ((1 - cos_theta) * axis.x_  * axis.y_ - axis.z_ * sin_theta) * view.y_;
    	new_view.x_ += ((1 - cos_theta) * axis.x_  * axis.z_ + axis.y_ * sin_theta) * view.z_;
    
    	new_view.y_  = ((1 - cos_theta) * axis.x_  * axis.y_ + axis.z_ * sin_theta) * view.x_;
    	new_view.y_ += (cos_theta + (1 - cos_theta) * axis.y_ * axis.y_)			* view.y_;
    	new_view.y_ += ((1 - cos_theta) * axis.y_  * axis.z_ - axis.x_ * sin_theta) * view.z_;
    

    weg, dann wird es nicht verzogen...

    Danke schonmal


Anmelden zum Antworten