36 static const F32 s_mouseRotateSpeed = 0.005f;
37 static const F32 s_mouseStrafeSpeed = -0.005f;
38 static const F32 s_keyRotateSpeed = 1.0f;
39 static const F32 s_inclinationLimit = 0.2f;
44 : m_commonControls (commonControls),
45 m_features (features),
47 m_enableMovement (true),
57 m_features &= ~Feature_StereoControls;
70 if (!m_commonControls)
71 fail(
"CameraControls attached to a window without CommonControls!");
122 if (m_dragLeft) rotate += delta * s_mouseRotateSpeed;
123 if (m_dragMiddle) move += delta * m_speed * s_mouseStrafeSpeed;
124 if (m_dragRight) move +=
Vec3f(0.0
f, 0.0
f, (
F32)ev.
mouseDelta.y) * m_speed * s_mouseStrafeSpeed;
130 F32 timeDelta = m_timer.
end();
132 Vec3f rotateTmp = 0.0f;
149 move *= timeDelta * m_speed * boost;
150 rotate += rotateTmp * timeDelta * s_keyRotateSpeed * boost;
160 if (m_enableMovement)
163 m_position += orient * move;
165 if (rotate.x != 0.0f || rotate.y != 0.0f)
175 if (rotate.z != 0.0f && !m_keepAligned)
178 m_up = orient *
Vec3f(up.x *
cos(rotate.z) -
sin(rotate.z), up.x *
sin(rotate.z) + up.y *
cos(rotate.z), up.z);
214 d.
get(m_position,
"m_position");
215 d.
get(m_forward,
"m_forward");
217 d.
get(m_keepAligned,
"m_keepAligned");
218 d.
get(m_speed,
"m_speed");
219 d.
get(m_fov,
"m_fov");
220 d.
get(m_near,
"m_near");
221 d.
get(m_far,
"m_far");
222 d.
get(m_enableStereo,
"m_enableStereo");
223 d.
get(m_stereoSeparation,
"m_stereoSeparation");
224 d.
get(m_stereoConvergence,
"m_stereoConvergence");
233 d.
set(m_position,
"m_position");
234 d.
set(m_forward,
"m_forward");
236 d.
set(m_keepAligned,
"m_keepAligned");
237 d.
set(m_speed,
"m_speed");
238 d.
set(m_fov,
"m_fov");
239 d.
set(m_near,
"m_near");
240 d.
set(m_far,
"m_far");
241 d.
set(m_enableStereo,
"m_enableStereo");
242 d.
set(m_stereoSeparation,
"m_stereoSeparation");
243 d.
set(m_stereoConvergence,
"m_stereoConvergence");
288 m_position = m.
col(3).toCartesian();
299 m_position =
Vec3f(0.0
f, 0.0
f, 1.5
f);
300 m_forward =
Vec3f(0.0
f, 0.0
f, -1.0
f);
303 m_keepAligned =
false;
309 m_enableStereo =
false;
310 m_stereoSeparation = 0.004f;
311 m_stereoConvergence = 0.015f;
322 Vec3f center = (lo + hi) * 0.5
f;
327 m_position = center +
Vec3f(0.0
f, 0.0
f, size * 0.75
f);
328 m_forward = Vec3f(0.0f, 0.0f, -1.0f);
329 m_up = Vec3f(0.0f, 1.0f, 0.0f);
331 m_speed = size * 0.1f;
332 m_near = size * 0.0005f;
335 m_stereoSeparation = size * 0.002f;
344 encodeFloat(sig, m_position.x);
345 encodeFloat(sig, m_position.y);
346 encodeFloat(sig, m_position.z);
347 encodeDirection(sig, m_forward);
348 encodeDirection(sig, m_up);
349 encodeFloat(sig, m_speed);
350 encodeFloat(sig, m_fov);
351 encodeFloat(sig, m_near);
352 encodeFloat(sig, m_far);
353 encodeBits(sig, (m_keepAligned) ? 1 : 0);
362 const char* src = sig.
getPtr();
363 while (*src ==
' ' || *src ==
'\t' || *src ==
'\n') src++;
364 if (*src ==
'"') src++;
366 F32 px = decodeFloat(src);
367 F32 py = decodeFloat(src);
368 F32 pz = decodeFloat(src);
369 Vec3f forward = decodeDirection(src);
370 Vec3f up = decodeDirection(src);
371 F32 speed = decodeFloat(src);
372 F32 fov = decodeFloat(src);
373 F32 znear = decodeFloat(src);
374 F32 zfar = decodeFloat(src);
375 bool keepAligned = (decodeBits(src) != 0);
377 if (*src ==
'"') src++;
378 if (*src ==
',') src++;
379 while (*src ==
' ' || *src ==
'\t' || *src ==
'\n') src++;
381 setError(
"CameraControls: Invalid signature!");
386 m_position =
Vec3f(px, py, pz);
393 m_keepAligned = keepAligned;
406 printf(
" FOV %.2f , near %.2f , far %.2f\n", m_fov, m_near, m_far);
460 void CameraControls::encodeBits(
String& dst,
U32 v)
463 int base = (v < 12) ?
'/' : (v < 38) ?
'A' - 12 :
'a' - 38;
464 dst += (char)(v + base);
469 U32 CameraControls::decodeBits(
const char*& src)
471 if (*src >=
'/' && *src <=
':')
return *src++ -
'/';
472 if (*src >=
'A' && *src <=
'Z')
return *src++ -
'A' + 12;
473 if (*src >=
'a' && *src <=
'z')
return *src++ -
'a' + 38;
474 setError(
"CameraControls: Invalid signature!");
480 void CameraControls::encodeFloat(
String& dst,
F32 v)
483 for (
int i = 0; i < 32; i += 6)
484 encodeBits(dst, (bits >> i) & 0x3F);
489 F32 CameraControls::decodeFloat(
const char*& src)
492 for (
int i = 0; i < 32; i += 6)
493 bits |= decodeBits(src) << i;
499 void CameraControls::encodeDirection(
String& dst,
const Vec3f& v)
502 int axis = (a.x >=
max(a.y, a.z)) ? 0 : (a.y >= a.z) ? 1 : 2;
507 case 0: tuv =
v;
break;
508 case 1: tuv =
Vec3f(v.y, v.z, v.x);
break;
509 default: tuv =
Vec3f(v.z, v.x, v.y);
break;
512 int face = axis | ((tuv.x >= 0.0f) ? 0 : 4);
513 if (tuv.y == 0.0f && tuv.z == 0.0f)
515 encodeBits(dst, face | 8);
519 encodeBits(dst, face);
520 encodeFloat(dst, tuv.y /
abs(tuv.x));
521 encodeFloat(dst, tuv.z /
abs(tuv.x));
526 Vec3f CameraControls::decodeDirection(
const char*& src)
528 int face = decodeBits(src);
530 tuv.x = ((face & 4) == 0) ? 1.0f : -1.0f;
531 tuv.y = ((face & 8) == 0) ? decodeFloat(src) : 0.0
f;
532 tuv.z = ((face & 8) == 0) ? decodeFloat(src) : 0.0
f;
538 case 1:
return Vec3f(tuv.z, tuv.x, tuv.y);
539 default:
return Vec3f(tuv.y, tuv.z, tuv.x);
void addStateObject(StateObject *obj)
void addGUIControls(void)
const GLContext::Config & getGLConfig(void) const
Mat4f getWorldToCamera(void) const
void removeGUIControls(void)
#define FW_KEY_MOUSE_RIGHT
FW_CUDA_FUNC void setRow(int r, const VectorBase< T, L, V > &v)
const char * getPtr(void) const
CameraControls(CommonControls *commonControls=NULL, U32 features=Feature_Default)
void getBBox(Vec3f &lo, Vec3f &hi) const
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum GLenum const GLvoid pixels GLint GLsizei const GLfloat value GLint GLfloat GLfloat v1 GLint GLfloat GLfloat GLfloat v2 GLint GLsizei const GLfloat value GLint GLsizei GLboolean const GLfloat value GLuint program GLuint GLfloat GLfloat GLfloat z GLuint GLint GLenum GLboolean normalized
void setError(const char *fmt,...)
FW_CUDA_FUNC F64 cos(F64 a)
void addToggle(bool *target, const String &key, const String &title, bool *dirtyNotify=NULL)
void beginSliderStack(void)
void addSlider(F32 *target, F32 minValue, F32 maxValue, bool isExponential, const String &increaseKey, const String &decreaseKey, const String &format, F32 speed=0.25f, bool *dirtyNotify=NULL)
F32 getKeyBoost(void) const
void removeStateObject(StateObject *obj)
static bool isStereoAvailable(void)
#define FW_KEY_WHEEL_DOWN
FW_CUDA_FUNC bool isZero(void) const
FW_CUDA_FUNC T dot(const VectorBase< T, L, V > &v) const
Mat4f getCameraToWorld(void) const
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v
void set(const StateDump &other)
virtual void writeState(StateDump &d) const
FW_CUDA_FUNC S transposed(void) const
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
#define FW_KEY_MOUSE_MIDDLE
FW_CUDA_FUNC S32 abs(S32 a)
FW_CUDA_FUNC Vec3f cross(const Vec3f &v) const
bool isKeyDown(const String &key) const
String encodeSignature(void) const
Mat3f getOrientation(void) const
FW_CUDA_FUNC const Vector< T, L > & col(int c) const
void setCameraToWorld(const Mat4f &m)
void addButton(bool *target, const String &key, const String &title, bool *dirtyNotify=NULL)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction f
FW_CUDA_FUNC F32 cross(const Vec2f &a, const Vec2f &b)
void setGLConfig(const GLContext::Config &config)
void initForMesh(const MeshBase *mesh)
void printf(const char *fmt,...)
FW_CUDA_FUNC S normalize(const VectorBase< T, L, S > &v, T len=(T) 1)
void pushOwner(const String &id)
#define FW_KEY_MOUSE_LEFT
FW_CUDA_FUNC S normalized(T len=(T) 1) const
virtual void readState(StateDump &d)
FW_CUDA_FUNC F64 sin(F64 a)
const Array< U8 > * get(const String &id) const
void decodeSignature(const String &sig)
void fail(const char *fmt,...)
void endSliderStack(void)
FW_CUDA_FUNC void setCol(int c, const VectorBase< T, L, V > &v)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
void removeControl(const void *target)
virtual bool handleEvent(const Window::Event &ev)
virtual ~CameraControls(void)