00001 #include "winiv.h"
00002
00007 #ifdef GLUTM_OPEN_INVENTOR
00008
00009 #include <Inventor/SoDB.h>
00010 #include <Inventor/SoInteraction.h>
00011 #include <Inventor/nodekits/SoNodeKit.h>
00012
00013 #include <Inventor/events/SoLocation2Event.h>
00014 #include <Inventor/events/SoMouseButtonEvent.h>
00015 #include <Inventor/events/SoKeyboardEvent.h>
00016
00017 #include <cassert>
00018 using namespace std;
00019
00021
00022 bool GlutWindowInventor::_initOpenInventor = false;
00023
00024 void
00025 GlutWindowInventor::initOpenInventor()
00026 {
00027 if (!_initOpenInventor)
00028 {
00029
00030 SoDB::init();
00031 SoNodeKit::init();
00032 SoInteraction::init();
00033 _initOpenInventor = true;
00034 }
00035 }
00036
00037 static void
00038 renderCallback(void *userData, SoSceneManager *mgr)
00039 {
00040 GlutWindow *window = (GlutWindow *) userData;
00041 window->postRedisplay();
00042 }
00043
00044 GlutWindowInventor::GlutWindowInventor(const std::string &title,int width,int height,int x,int y,unsigned int displayMode)
00045 : GlutWindow(title,width,height,x,y,displayMode)
00046 {
00047 }
00048
00049 GlutWindowInventor::~GlutWindowInventor()
00050 {
00051 }
00052
00053 void
00054 GlutWindowInventor::OnOpen()
00055 {
00056 assert(_initOpenInventor);
00057 activate();
00058 setRenderCallback(renderCallback,this);
00059
00060 glEnable(GL_DEPTH_TEST);
00061 glEnable(GL_LIGHTING);
00062 }
00063
00064 void
00065 GlutWindowInventor::OnClose()
00066 {
00067 deactivate();
00068 setRenderCallback(NULL,NULL);
00069 }
00070
00071 void
00072 GlutWindowInventor::OnDisplay()
00073 {
00074 render();
00075 }
00076
00077 void
00078 GlutWindowInventor::inventorTick()
00079 {
00080 SoDB::getSensorManager()->processTimerQueue();
00081 SoDB::getSensorManager()->processDelayQueue(TRUE);
00082 }
00083
00084 void
00085 GlutWindowInventor::OnTick()
00086 {
00087 inventorTick();
00088 }
00089
00090 void
00091 GlutWindowInventor::OnReshape(int w, int h)
00092 {
00093 GlutWindow::OnReshape(w,h);
00094
00095 setWindowSize(SbVec2s(w, h));
00096 setSize(SbVec2s(w, h));
00097 setViewportRegion(SbViewportRegion(w, h));
00098 scheduleRedraw();
00099 inventorTick();
00100 }
00101
00102 void
00103 GlutWindowInventor::OnKeyboard(unsigned char key, int x, int y)
00104 {
00105 GlutWindow::OnKeyboard(key,x,y);
00106
00107
00108
00109 SoKeyboardEvent event;
00110 SoKeyboardEvent::Key k;
00111
00112 switch (key)
00113 {
00114
00115 case '0': k = SoKeyboardEvent::NUMBER_0; break;
00116 case '1': k = SoKeyboardEvent::NUMBER_1; break;
00117 case '2': k = SoKeyboardEvent::NUMBER_2; break;
00118 case '3': k = SoKeyboardEvent::NUMBER_3; break;
00119 case '4': k = SoKeyboardEvent::NUMBER_4; break;
00120 case '5': k = SoKeyboardEvent::NUMBER_5; break;
00121 case '6': k = SoKeyboardEvent::NUMBER_6; break;
00122 case '7': k = SoKeyboardEvent::NUMBER_7; break;
00123 case '8': k = SoKeyboardEvent::NUMBER_8; break;
00124 case '9': k = SoKeyboardEvent::NUMBER_9; break;
00125
00126 case 'a': case 'A': k = SoKeyboardEvent::A; break;
00127 case 'b': case 'B': k = SoKeyboardEvent::B; break;
00128 case 'c': case 'C': k = SoKeyboardEvent::C; break;
00129 case 'd': case 'D': k = SoKeyboardEvent::D; break;
00130 case 'e': case 'E': k = SoKeyboardEvent::E; break;
00131 case 'f': case 'F': k = SoKeyboardEvent::F; break;
00132 case 'g': case 'G': k = SoKeyboardEvent::G; break;
00133 case 'h': case 'H': k = SoKeyboardEvent::H; break;
00134 case 'i': case 'I': k = SoKeyboardEvent::I; break;
00135 case 'j': case 'J': k = SoKeyboardEvent::J; break;
00136 case 'k': case 'K': k = SoKeyboardEvent::K; break;
00137 case 'l': case 'L': k = SoKeyboardEvent::L; break;
00138 case 'm': case 'M': k = SoKeyboardEvent::M; break;
00139 case 'n': case 'N': k = SoKeyboardEvent::N; break;
00140 case 'o': case 'O': k = SoKeyboardEvent::O; break;
00141 case 'p': case 'P': k = SoKeyboardEvent::P; break;
00142 case 'q': case 'Q': k = SoKeyboardEvent::Q; break;
00143 case 'r': case 'R': k = SoKeyboardEvent::R; break;
00144 case 's': case 'S': k = SoKeyboardEvent::S; break;
00145 case 't': case 'T': k = SoKeyboardEvent::T; break;
00146 case 'u': case 'U': k = SoKeyboardEvent::U; break;
00147 case 'v': case 'V': k = SoKeyboardEvent::V; break;
00148 case 'w': case 'W': k = SoKeyboardEvent::W; break;
00149 case 'x': case 'X': k = SoKeyboardEvent::X; break;
00150 case 'y': case 'Y': k = SoKeyboardEvent::Y; break;
00151 case 'z': case 'Z': k = SoKeyboardEvent::Z; break;
00152
00153 case ' ': k = SoKeyboardEvent::SPACE; break;
00154 case '\n': k = SoKeyboardEvent::RETURN; break;
00155 case ',': k = SoKeyboardEvent::COMMA; break;
00156 case '-': k = SoKeyboardEvent::MINUS; break;
00157 case '.': k = SoKeyboardEvent::PERIOD; break;
00158 case '/': k = SoKeyboardEvent::SLASH; break;
00159 case ';': k = SoKeyboardEvent::SEMICOLON; break;
00160 case '=': k = SoKeyboardEvent::EQUAL; break;
00161 case '(': k = SoKeyboardEvent::BRACKETLEFT; break;
00162 case '\\': k = SoKeyboardEvent::BACKSLASH; break;
00163 case ')': k = SoKeyboardEvent::BRACKETRIGHT; break;
00164
00165 default:
00166 return;
00167 }
00168
00169 event.setKey(k);
00170 event.setPosition(SbVec2s(x,_viewport.height()-y));
00171 event.setState(SoButtonEvent::DOWN);
00172 processEvent(&event);
00173 inventorTick();
00174 }
00175
00176 void
00177 GlutWindowInventor::OnSpecial(int key, int x, int y)
00178 {
00179 GlutWindow::OnSpecial(key,x,y);
00180
00181 SoKeyboardEvent event;
00182 SoKeyboardEvent::Key k;
00183
00184 switch (key)
00185 {
00186
00187 case GLUT_KEY_F1: k = SoKeyboardEvent::F1; break;
00188 case GLUT_KEY_F2: k = SoKeyboardEvent::F2; break;
00189 case GLUT_KEY_F3: k = SoKeyboardEvent::F3; break;
00190 case GLUT_KEY_F4: k = SoKeyboardEvent::F4; break;
00191 case GLUT_KEY_F5: k = SoKeyboardEvent::F5; break;
00192 case GLUT_KEY_F6: k = SoKeyboardEvent::F6; break;
00193 case GLUT_KEY_F7: k = SoKeyboardEvent::F7; break;
00194 case GLUT_KEY_F8: k = SoKeyboardEvent::F8; break;
00195 case GLUT_KEY_F9: k = SoKeyboardEvent::F9; break;
00196 case GLUT_KEY_F10: k = SoKeyboardEvent::F10; break;
00197 case GLUT_KEY_F11: k = SoKeyboardEvent::F11; break;
00198 case GLUT_KEY_F12: k = SoKeyboardEvent::F12; break;
00199
00200 case GLUT_KEY_LEFT: k = SoKeyboardEvent::LEFT_ARROW; break;
00201 case GLUT_KEY_UP: k = SoKeyboardEvent::UP_ARROW; break;
00202 case GLUT_KEY_RIGHT: k = SoKeyboardEvent::RIGHT_ARROW; break;
00203 case GLUT_KEY_DOWN: k = SoKeyboardEvent::DOWN_ARROW; break;
00204
00205 case GLUT_KEY_PAGE_UP: k = SoKeyboardEvent::PAGE_UP; break;
00206 case GLUT_KEY_PAGE_DOWN: k = SoKeyboardEvent::PAGE_DOWN; break;
00207 case GLUT_KEY_HOME: k = SoKeyboardEvent::HOME; break;
00208 case GLUT_KEY_END: k = SoKeyboardEvent::END; break;
00209 case GLUT_KEY_INSERT: k = SoKeyboardEvent::INSERT; break;
00210
00211 default:
00212 return;
00213 }
00214
00215 event.setKey(k);
00216 event.setPosition(SbVec2s(x,_viewport.height()-y));
00217 event.setState(SoButtonEvent::DOWN);
00218 processEvent(&event);
00219 inventorTick();
00220 }
00221
00222 void
00223 GlutWindowInventor::OnMouse(int button, int state, int x, int y)
00224 {
00225 GlutWindow::OnMouse(button,state,x,y);
00226
00227 SoMouseButtonEvent event;
00228
00229 switch (button)
00230 {
00231 default:
00232 case GLUT_LEFT_BUTTON: event.setButton(SoMouseButtonEvent::BUTTON1); break;
00233 case GLUT_MIDDLE_BUTTON: event.setButton(SoMouseButtonEvent::BUTTON2); break;
00234 case GLUT_RIGHT_BUTTON: event.setButton(SoMouseButtonEvent::BUTTON3); break;
00235 }
00236
00237 switch (state)
00238 {
00239 default:
00240 case GLUT_UP: event.setState(SoButtonEvent::UP); break;
00241 case GLUT_DOWN: event.setState(SoButtonEvent::DOWN); break;
00242 }
00243
00244 event.setPosition(SbVec2s(x,_viewport.height()-y));
00245
00246 processEvent(&event);
00247 inventorTick();
00248 }
00249
00250 void
00251 GlutWindowInventor::OnMotion(int x, int y)
00252 {
00253 GlutWindow::OnMotion(x,y);
00254
00255 SoLocation2Event event;
00256 event.setPosition(SbVec2s(x,_viewport.height()-y));
00257 processEvent(&event);
00258 inventorTick();
00259 }
00260
00261 void
00262 GlutWindowInventor::OnPassiveMotion(int x, int y)
00263 {
00264 GlutWindow::OnPassiveMotion(x,y);
00265
00266 SoLocation2Event event;
00267 event.setPosition(SbVec2s(x,_viewport.height()-y));
00268 processEvent(&event);
00269 inventorTick();
00270 }
00271
00272 #endif