00001
00002
00003
00004 #ifndef OglGui_Camera3DXYZ_h
00005 #define OglGui_Camera3DXYZ_h
00006
00007 #ifndef OglGui_Window_h
00008 #include "OglGui/Window.h"
00009 #endif
00010
00011 extern "C" {
00012 void OglRotate(float x, float y, float z, float angle,
00013 float *vX, float *vY, float *vZ);
00014 }
00015
00016 namespace OglGui
00017 {
00018 class Camera3DXYZ : public Window
00019 {
00020 public:
00021 Camera3DXYZ(int x, int y, int w, int h, Window* target=0) :
00022 Window(x, y, w, h)
00023 {
00024 Init(w, h, target);
00025 }
00026
00027 Camera3DXYZ(Window* parent, int w, int h, Window* target=0) :
00028 Window(parent, w, h)
00029 {
00030 Init(w, h, target);
00031 }
00032
00033 Camera3DXYZ(Window* parent, int x, int y, int w, int h, Window* target=0) :
00034 Window(parent, x, y, w, h)
00035 {
00036 Init(w, h, target);
00037 }
00038
00039 void StatePanCamera(int state) { mStatePanCamera = state; }
00040 void StateZoomCamera(int state) { mStateZoomCamera = state; }
00041 void StateRotateCameraXY(int state) { mStateRotateCameraXY = state; }
00042 void StateRotateCameraZ(int state) { mStateRotateCameraZ = state; }
00043
00044 void PanCameraXY(SCENE3D* cam, int w, int h)
00045 {
00046 float stepX = (mMouseX-w/2);
00047 float stepY = (mMouseY-h/2);
00048
00049 stepX *= abs(stepX) < 15 ? 1.f : abs(stepX)/15.f;
00050 stepY *= abs(stepY) < 15 ? 1.f : abs(stepY)/15.f;
00051
00052 stepX = mStepFactor * (abs(stepX) > 2 ? stepX : 0);
00053 stepY = mStepFactor * (abs(stepY) > 2 ? stepY : 0);
00054
00055 cam->camX += stepX * cam->rX + stepY * cam->uX;
00056 cam->camY += stepX * cam->rY + stepY * cam->uY;
00057 cam->camZ += stepX * cam->rZ + stepY * cam->uZ;
00058 if (!mTextFeedBack)
00059 return;
00060 oglSys.PosColPrintf(mOglWnd, mMouseX, mMouseY, oglRED, "Pan");
00061 }
00062
00063 void ZoomCameraZ(SCENE3D* cam, int w, int h)
00064 {
00065 float step = (mMouseY-h/2);
00066 step *= 2 * mStepFactor * (abs(step) < 15 ? 1.f : abs(step)/15.f);
00067 cam->camX += step * cam->tX;
00068 cam->camY += step * cam->tY;
00069 cam->camZ += step * cam->tZ;
00070 if (!mTextFeedBack)
00071 return;
00072 oglSys.PosColPrintf(mOglWnd, mMouseX, mMouseY, oglRED, "Zoom %s",
00073 step < 0 ? "Out" : (step>0) ? "In" : "");
00074 }
00075
00076 void RotateCameraXY(SCENE3D* cam, int w, int h)
00077 {
00078 float stepX = -(mMouseX-w/2);
00079 float stepY = (mMouseY-h/2);
00080
00081 stepX = 10 * mStepFactor * (fabs(stepX) > 2 ? stepX : 0);
00082 stepY = 10 * mStepFactor * (fabs(stepY) > 2 ? stepY : 0);
00083
00084 OglRotate(cam->rX,cam->rY,cam->rZ,stepY,&cam->uX,&cam->uY,&cam->uZ );
00085 OglRotate(cam->rX,cam->rY,cam->rZ,stepY,&cam->tX,&cam->tY,&cam->tZ );
00086
00087 OglRotate(cam->uX,cam->uY,cam->uZ,stepX,&cam->rX,&cam->rY,&cam->rZ );
00088 OglRotate(cam->uX,cam->uY,cam->uZ,stepX,&cam->tX,&cam->tY,&cam->tZ );
00089 if (!mTextFeedBack)
00090 return;
00091 oglSys.PosColPrintf(mOglWnd, mMouseX, mMouseY, oglRED, "XY-Rotate");
00092 }
00093
00094 void RotateCameraZ(SCENE3D* cam, int w, int h)
00095 {
00096 float step = (mMouseY-mLastY);
00097 if (mMouseX > w/2)
00098 step = -step;
00099 if (abs(mMouseX-mLastX) > abs(step))
00100 {
00101 step = (mMouseX-mLastX);
00102 if (mMouseY < h/2)
00103 step = -step;
00104 }
00105 step *= 0.5f;
00106 OglRotate( cam->tX, cam->tY, cam->tZ, step, &cam->rX, &cam->rY, &cam->rZ );
00107 OglRotate( cam->tX, cam->tY, cam->tZ, step, &cam->uX, &cam->uY, &cam->uZ );
00108 if (!mTextFeedBack)
00109 return;
00110 oglSys.PosColPrintf(mOglWnd, mMouseX, mMouseY, oglRED, "Z-Rotate");
00111 }
00112
00113 virtual void DisplayFunc()
00114 {
00115 Window::DisplayFunc();
00116
00117 float w = W(), h = H();
00118 OGC myOGC;
00119
00120 OGCSave(&myOGC);
00121 SetSolidLineColor(oglLIGHTBLUE);
00122 DrawLine(w/2-2, 0, w/2-2, h);
00123 DrawLine(w/2+2, 0, w/2+2, h);
00124 DrawLine(0, h/2-2, w, h/2-2);
00125 DrawLine(0, h/2+2, w, h/2+2);
00126
00127 SCENE3D* cam = mTarget ? (SCENE3D*) mTarget->GetOGLWND()->sceneInfo : 0;
00128
00129 if (mTarget && mPropagateMouse)
00130 {
00131 if (mMouseState == mStatePanCamera)
00132 PanCameraXY(cam, (int) w, (int) h);
00133 if (mMouseState == mStateZoomCamera)
00134 ZoomCameraZ(cam, (int) w, (int) h);
00135 if (mMouseState == mStateRotateCameraXY)
00136 RotateCameraXY(cam, (int) w, (int) h);
00137 if (mMouseState == mStateRotateCameraZ)
00138 RotateCameraZ(cam, (int) w, (int) h);
00139 }
00140 mLastX = mMouseX;
00141 mLastY = mMouseY;
00142
00143 if (mShowCamCoordinates)
00144 {
00145 oglSys.PosColPrintf(mOglWnd, 4,4, oglBLUE, "z:%.2f", cam->camZ);
00146 oglSys.PosColPrintf(mOglWnd, 4,24, oglBLUE, "y:%.2f", cam->camY);
00147 oglSys.PosColPrintf(mOglWnd, 4,44, oglBLUE, "x:%.2f", cam->camX);
00148 }
00149
00150 OGCRestore(&myOGC);
00151 }
00152
00153 virtual void MouseFunc(int msg, int btn, int state, int x, int y)
00154 {
00155 mMouseX = x;
00156 mMouseY = y;
00157 mMouseState = state;
00158
00159 Window::MouseFunc(msg, btn, state, x, y);
00160
00161 if (msg==oglMouseDown && mTarget)
00162 {
00163 mLastX = x;
00164 mLastY = y;
00165 mPropagateMouse = true;
00166 oglSys.SetAlwaysDraw(mOglWnd, 1);
00167 }
00168 if (!state || !mTarget)
00169 {
00170 mPropagateMouse = false;
00171 oglSys.SetAlwaysDraw(mOglWnd, 0);
00172 }
00173 }
00174
00175 virtual void KeyboardFunc(int c, int state)
00176 {
00177 if (c == 'i')
00178 mShowCamCoordinates = !mShowCamCoordinates;
00179 else if (mTarget)
00180 mTarget->KeyboardFunc(c, state);
00181 Window::KeyboardFunc(c, state);
00182 }
00183
00184 private:
00185 void Init(int w, int h, Window* target)
00186 {
00187 mTarget = target;
00188
00189 SetBorderType(BEV_LINE);
00190
00191 mPropagateMouse = false;
00192 mTextFeedBack = true;
00193 mShowCamCoordinates = true;
00194 mStepFactor = 0.001f;
00195
00196 StatePanCamera(oglLeftButton);
00197 StateZoomCamera(oglRightButton);
00198 StateRotateCameraXY(oglLeftButton|oglControl);
00199 StateRotateCameraZ(oglRightButton|oglControl);
00200 }
00201
00202 Window* mTarget;
00203
00204 bool mPropagateMouse;
00205 bool mTextFeedBack;
00206 bool mShowCamCoordinates;
00207
00208 int mStatePanCamera;
00209 int mStateRotateCameraXY;
00210 int mStateRotateCameraZ;
00211 int mStateZoomCamera;
00212
00213 int mMouseState;
00214 int mMouseX;
00215 int mMouseY;
00216 int mLastX, mLastY;
00217
00218 float mStepFactor;
00219 };
00220 }
00221
00222 #endif