1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
|
#include "StdAfx.h"
#include <SDL_mouse.h>
#include <SDL_keysym.h>
#include "OrbitController.h"
#include "Game/Camera.h"
#include "Game/UI/MouseHandler.h"
#include "Map/Ground.h"
#include "LogOutput.h"
#include "ConfigHandler.h"
#include <boost/cstdint.hpp>
extern boost::uint8_t* keys;
#define DEG2RAD(a) ((a) * (3.141592653f / 180.0f))
#define RAD2DEG(a) ((a) * (180.0f / 3.141592653f))
static const float3 YVEC(0.0f, 1.0f, 0.0f);
COrbitController::COrbitController():
lastMouseMoveX(0), lastMouseMoveY(0),
lastMousePressX(0), lastMousePressY(0),
lastMouseButton(-1),
currentState(Orbiting),
distance(512.0f), cDistance(512.0f),
rotation(0.0f), cRotation(0.0f),
elevation(0.0f), cElevation(0.0f)
{
enabled = !!configHandler->Get("OrbitControllerEnabled", 1);
orbitSpeedFact = configHandler->Get("OrbitControllerOrbitSpeed", 0.25f);
panSpeedFact = configHandler->Get("OrbitControllerPanSpeed", 2.00f);
zoomSpeedFact = configHandler->Get("OrbitControllerZoomSpeed", 5.00f);
orbitSpeedFact = std::max(0.1f, std::min(10.0f, orbitSpeedFact));
panSpeedFact = std::max(0.1f, std::min(10.0f, panSpeedFact));
zoomSpeedFact = std::max(0.1f, std::min(10.0f, zoomSpeedFact));
}
void COrbitController::Init(const float3& p, const float3& tar)
{
CCamera* cam = camera;
const float l = (tar == ZeroVector)?
std::max(ground->LineGroundCol(p, p + cam->forward * 1024.0f), 512.0f):
(p - tar).Length();
const float3 t = (tar == ZeroVector)? (p + cam->forward * l): tar;
const float3 v = (t - p);
const float3 w = (v / v.Length()); // do not normalize v in-place
const float d = v.Length();
const float e = RAD2DEG(acos(v.Length2D() / d));
const float r = RAD2DEG(acos(w.x));
distance = cDistance = d;
elevation = cElevation = e;
rotation = cRotation = (v.z > 0.0f)? 180.0f + r: 180.0f - r;
cen = t;
}
void COrbitController::Update()
{
if (!keys[SDLK_LMETA]) {
return;
}
const int x = mouse->lastx;
const int y = mouse->lasty;
const int pdx = lastMousePressX - x;
const int pdy = lastMousePressY - y;
const int rdx = lastMouseMoveX - x;
const int rdy = lastMouseMoveY - y;
lastMouseMoveX = x;
lastMouseMoveY = y;
MyMouseMove(pdx, pdy, rdx, rdy, lastMouseButton);
}
void COrbitController::MousePress(int x, int y, int button)
{
lastMousePressX = x;
lastMousePressY = y;
lastMouseButton = button;
cDistance = distance;
cRotation = rotation;
cElevation = elevation;
switch (button) {
case SDL_BUTTON_LEFT: { currentState = Orbiting; } break;
case SDL_BUTTON_MIDDLE: { currentState = Panning; } break;
case SDL_BUTTON_RIGHT: { currentState = Zooming; } break;
}
}
void COrbitController::MouseRelease(int x, int y, int button)
{
lastMousePressX = x;
lastMousePressY = y;
lastMouseButton = -1;
currentState = None;
}
void COrbitController::MouseMove(float3 move)
{
// only triggers on SDL_BUTTON_MIDDLE (see CMouseHandler::MouseMove())
}
void COrbitController::MyMouseMove(int dx, int dy, int rdx, int rdy, int button)
{
switch (button) {
case SDL_BUTTON_LEFT: {
rotation = cRotation - (dx * orbitSpeedFact);
elevation = cElevation - (dy * orbitSpeedFact);
} break;
case SDL_BUTTON_RIGHT: {
distance = cDistance - (dy * zoomSpeedFact);
} break;
}
if (elevation > 89.0f) elevation = 89.0f;
if (elevation < -89.0f) elevation = -89.0f;
if (distance < 1.0f) distance = 1.0f;
switch (button) {
case SDL_BUTTON_LEFT: {
Orbit();
} break;
case SDL_BUTTON_MIDDLE: {
Pan(rdx, rdy);
} break;
case SDL_BUTTON_RIGHT: {
Zoom();
} break;
}
}
void COrbitController::Orbit()
{
CCamera* cam = camera;
cam->pos = cen + GetOrbitPos();
cam->pos.y = std::max(cam->pos.y, ground->GetHeight2(cam->pos.x, cam->pos.z));
cam->forward = (cen - cam->pos).ANormalize();
cam->up = YVEC;
}
void COrbitController::Pan(int rdx, int rdy)
{
CCamera* cam = camera;
// horizontal pan
cam->pos += (cam->right * -rdx * panSpeedFact);
cen += (cam->right * -rdx * panSpeedFact);
// vertical pan
cam->pos += (cam->up * rdy * panSpeedFact);
cen += (cam->up * rdy * panSpeedFact);
// don't allow orbit center or ourselves to drop below the terrain
const float camGH = ground->GetHeight2(cam->pos.x, cam->pos.z);
const float cenGH = ground->GetHeight2(cen.x, cen.z);
if (cam->pos.y < camGH) {
cam->pos.y = camGH;
}
if (cen.y < cenGH) {
cen.y = cenGH;
cam->forward = (cen - cam->pos).ANormalize();
Init(cam->pos, cen);
}
}
void COrbitController::Zoom()
{
camera->pos = cen - (camera->forward * distance);
}
void COrbitController::KeyMove(float3 move)
{
// higher framerate means we take smaller steps per frame
// (x and y are lastFrameTime in secs., 200FPS ==> 0.005s)
Pan(int(move.x * -1000), int(move.y * 1000));
}
void COrbitController::ScreenEdgeMove(float3 move)
{
Pan(int(move.x * -1000), int(move.y * 1000));
}
void COrbitController::MouseWheelMove(float move)
{
}
float3 COrbitController::GetPos()
{
return camera->pos;
}
void COrbitController::SetPos(const float3& newPos)
{
if (keys[SDLK_LMETA]) {
return;
}
// support minimap position hopping
const float dx = newPos.x - camera->pos.x;
const float dz = newPos.z - camera->pos.z;
cen.x += dx;
cen.z += dz;
cen.y = ground->GetHeight2(cen.x, cen.z);
camera->pos.x += dx;
camera->pos.z += dz;
Init(camera->pos, cen);
}
float3 COrbitController::GetDir()
{
return (cen - camera->pos).ANormalize();
}
float3 COrbitController::GetOrbitPos() const
{
const float beta = DEG2RAD(elevation);
const float gamma = DEG2RAD(rotation);
float cx = distance;
float cy = 0.0f;
float cz = 0.0f;
float tx = cx;
tx = cx;
cx = cx * cos(beta) + cy * sin(beta);
cy = tx * sin(beta) + cy * cos(beta);
tx = cx;
cx = cx * cos(gamma) - cz * sin(gamma);
cz = tx * sin(gamma) + cz * cos(gamma);
return float3(cx, cy, cz);
}
float3 COrbitController::SwitchFrom() const
{
return camera->pos;
}
void COrbitController::SwitchTo(bool showText)
{
if (showText) {
logOutput.Print("Switching to Orbit style camera");
}
Init(camera->pos, ZeroVector);
}
void COrbitController::GetState(StateMap& sm) const
{
sm["px"] = camera->pos.x;
sm["py"] = camera->pos.y;
sm["pz"] = camera->pos.z;
sm["tx"] = cen.x;
sm["ty"] = cen.y;
sm["tz"] = cen.z;
}
bool COrbitController::SetState(const StateMap& sm)
{
SetStateFloat(sm, "px", camera->pos.x);
SetStateFloat(sm, "py", camera->pos.y);
SetStateFloat(sm, "pz", camera->pos.z);
SetStateFloat(sm, "tx", cen.x);
SetStateFloat(sm, "ty", cen.y);
SetStateFloat(sm, "tz", cen.z);
return true;
}
|