⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 camera.h

📁 小游戏 有碰撞检测和音乐 使用键盘openal
💻 H
字号:
#pragma once
#include "vector.h"

class CCamera
{
	public:
	CVector pos, view, up;
	float velocity;
	CCamera(){}
	CCamera( float px, float py, float pz, float vx, float vy, float vz,
	float ux, float uy, float uz, float vel )
	{
		pos.x = px; pos.y = py; pos.z = pz;
		view.x = vx; view.y = vy; view.z = vz;
		up.x = ux; up.y = uy; up.z = uz;
		if ( py > 1.2 ) pos.y = 1.2f;
		velocity = vel;
	}
	void Set( CVector _pos, CVector _view, CVector _up, float vel )
	{
		view = _view; pos =_pos; up =_up; velocity = vel;
	}
	void LookAt()
	{
		gluLookAt( pos.x, pos.y, pos.z, view.x, view.y, view.z, up.x, up.y, up.z );
	}
	void Rotate( float angle, float x, float y, float z )
	{
		// get direction facing
		CVector dir = pos - view ;
		// calculate sine, cosine of angle
		float cos_angle = (float) cos( angle );
		float sin_angle = (float) sin( angle );
		// calculate new dir after rotation
		CVector newdir;
		newdir.x = (cos_angle + (1 - cos_angle) * x) * dir.x;
		newdir.x += ((1 - cos_angle) * x * y - z * sin_angle) * dir.y;
		newdir.x += ((1 - cos_angle) * x * z + y * sin_angle) * dir.z;
		newdir.y = ((1 - cos_angle) * x * y + z * sin_angle) * dir.x;
		newdir.y += (cos_angle + (1 - cos_angle) * y) * dir.y;
		newdir.y += ((1 - cos_angle) * y * z - x * sin_angle) * dir.z;
		newdir.z = ((1 - cos_angle) * x * z - y * sin_angle) * dir.x;
		newdir.z += ((1 - cos_angle) * y * z + x * sin_angle) * dir.y;
		newdir.z += (cos_angle + (1 - cos_angle) * z) * dir.z;
		pos = view + newdir;
	}

	void Up()
	{
		if ( pos.y > 1.2 ) return;
		pos.y += velocity;
		CVector dir;
		dir.x = view.x - pos.x;
		dir.z = view.z - pos.z;
		CVector sd = ( dir ^ up ).Normalize();
		// rotate camera by angle moved
		// along side direction for up-down rotation
		Rotate( -velocity, sd.x, sd.y, sd.z );
	}
	void Down()
	{
		if ( pos.y < 0.1 ) return;
		pos.y -= velocity;
		CVector dir;
		dir.x = view.x - pos.x;
		dir.z = view.z - pos.z;
		CVector sd = ( dir ^ up ).Normalize();
		// rotate camera by angle moved
		// along side direction for up-down rotation
		Rotate( velocity, sd.x, sd.y, sd.z );
	}
};

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -