00001
00002 #include <cmath>
00003 #include "rtc/rtcRenderManager.h"
00004 #include "rtc/rtcRenderer.h"
00005
00006
00007 #define DEFAULT_ZOOM_SENSITIVITY 0.2
00008 #define DEFAULT_ROTATE_SENSITIVITY 0.50
00009 #define DEFAULT_MOVE_SENSITIVITY 0.001
00010 #define DEFAULT_ZOOM_SENSITIVITY_2D 0.02
00011 #define DEFAULT_ROTATE_SENSITIVITY_2D 0.0050
00012 #define DEFAULT_MOVE_SENSITIVITY_2D 1.0
00013
00014 #define DEFAULT_MIN_ZOOM_RANGE 0.5
00015 #define DEFAULT_CAMERA_FOV 30.0
00016 #define DEFAULT_MIN_CLIP_RANGE 1.0
00017 #define DEFAULT_MAX_CLIP_RANGE 400.0
00018
00019 #define KEY_ROTATE_AMOUNT 5.0
00020 #define KEY_MOVE_AMOUNT 10.0
00021 #define KEY_ZOOM_AMOUNT 5.0
00022
00023
00024 #define D2R 0.0174532925199432957692
00025
00026 #define R2D 57.295779513082320876
00027
00028
00029 namespace rtc {
00030
00031
00032 const int num_arrow_triangle_faces = 8;
00033 const int num_arrow_triangle_vertices = num_arrow_triangle_faces*3;
00034
00035
00036
00037
00038 const GLfloat arrow_triangle_vertices[num_arrow_triangle_vertices*3] = {
00039 0.000000f,0.040000f,-0.800000f,0.028284f,0.028284f,-0.800000f,
00040 0.000000f,0.000000f,-1.000000f,0.028284f,0.028284f,-0.800000f,
00041 0.040000f,0.000000f,-0.800000f,0.000000f,0.000000f,-1.000000f,
00042 0.040000f,0.000000f,-0.800000f,0.028284f,-0.028284f,-0.800000f,
00043 0.000000f,0.000000f,-1.000000f,0.028284f,-0.028284f,-0.800000f,
00044 0.000000f,-0.040000f,-0.800000f,0.000000f,0.000000f,-1.000000f,
00045 0.000000f,-0.040000f,-0.800000f,-0.028284f,-0.028284f,-0.800000f,
00046 0.000000f,0.000000f,-1.000000f,-0.028284f,-0.028284f,-0.800000f,
00047 -0.040000f,0.000000f,-0.800000f,0.000000f,0.000000f,-1.000000f,
00048 -0.040000f,0.000000f,-0.800000f,-0.028284f,0.028284f,-0.800000f,
00049 0.000000f,0.000000f,-1.000000f,-0.028284f,0.028284f,-0.800000f,
00050 0.000000f,0.040000f,-0.800000f,0.000000f,0.000000f,-1.000000f
00051 };
00052 const GLfloat arrow_triangle_normals[num_arrow_triangle_vertices*3] = {
00053 0.000000f,0.980581f,-0.196116f,0.693375f,0.693375f,-0.196116f,
00054 0.357407f,0.862856f,-0.357407f,0.693375f,0.693375f,-0.196116f,
00055 0.980581f,0.000000f,-0.196116f,0.862856f,0.357407f,-0.357407f,
00056 0.980581f,0.000000f,-0.196116f,0.693375f,-0.693375f,-0.196116f,
00057 0.862856f,-0.357407f,-0.357407f,0.693375f,-0.693375f,-0.196116f,
00058 0.000000f,-0.980581f,-0.196116f,0.357407f,-0.862856f,-0.357407f,
00059 0.000000f,-0.980581f,-0.196116f,-0.693375f,-0.693375f,-0.196116f,
00060 -0.357407f,-0.862856f,-0.357407f,-0.693375f,-0.693375f,-0.196116f,
00061 -0.980581f,0.000000f,-0.196116f,-0.862856f,-0.357407f,-0.357407f,
00062 -0.980581f,0.000000f,-0.196116f,-0.693375f,0.693375f,-0.196116f,
00063 -0.862856f,0.357407f,-0.357407f,-0.693375f,0.693375f,-0.196116f,
00064 0.000000f,0.980581f,-0.196116f,-0.357407f,0.862856f,-0.357407f
00065 };
00066
00067 const int num_arrow_quad_faces = 16;
00068 const int num_arrow_quad_vertices = num_arrow_quad_faces*4;
00069 const GLfloat arrow_quad_vertices[num_arrow_quad_vertices*3] = {
00070 0.000000f,0.010000f,0.000000f,0.007000f,0.007000f,0.000000f,
00071 0.007000f,0.007000f,-0.800000f,0.000000f,0.010000f,-0.800000f,
00072 0.000000f,-0.010000f,0.000000f,-0.007000f,-0.007000f,0.000000f,
00073 -0.007000f,-0.007000f,-0.800000f,0.000000f,-0.010000f,-0.800000f,
00074 -0.007000f,-0.007000f,0.000000f,-0.010000f,0.000000f,0.000000f,
00075 -0.010000f,0.000000f,-0.800000f,-0.007000f,-0.007000f,-0.800000f,
00076 -0.010000f,0.000000f,0.000000f,-0.007000f,0.007000f,0.000000f,
00077 -0.007000f,0.007000f,-0.800000f,-0.010000f,0.000000f,-0.800000f,
00078 -0.007000f,0.007000f,0.000000f,0.000000f,0.010000f,0.000000f,
00079 0.000000f,0.010000f,-0.800000f,-0.007000f,0.007000f,-0.800000f,
00080 0.007000f,0.007000f,0.000000f,0.010000f,0.000000f,0.000000f,
00081 0.010000f,0.000000f,-0.800000f,0.007000f,0.007000f,-0.800000f,
00082 0.010000f,0.000000f,0.000000f,0.007000f,-0.007000f,0.000000f,
00083 0.007000f,-0.007000f,-0.800000f,0.010000f,0.000000f,-0.800000f,
00084 0.007000f,-0.007000f,0.000000f,0.000000f,-0.010000f,0.000000f,
00085 0.000000f,-0.010000f,-0.800000f,0.007000f,-0.007000f,-0.800000f,
00086 -0.007000f,0.007000f,-0.800000f,-0.028284f,0.028284f,-0.800000f,
00087 -0.040000f,0.000000f,-0.800000f,-0.010000f,0.000000f,-0.800000f,
00088 -0.010000f,0.000000f,-0.800000f,-0.040000f,0.000000f,-0.800000f,
00089 -0.028284f,-0.028284f,-0.800000f,-0.007000f,-0.007000f,-0.800000f,
00090 -0.007000f,-0.007000f,-0.800000f,-0.028284f,-0.028284f,-0.800000f,
00091 0.000000f,-0.040000f,-0.800000f,0.000000f,-0.010000f,-0.800000f,
00092 0.000000f,-0.010000f,-0.800000f,0.000000f,-0.040000f,-0.800000f,
00093 0.028284f,-0.028284f,-0.800000f,0.007000f,-0.007000f,-0.800000f,
00094 0.028284f,-0.028284f,-0.800000f,0.040000f,0.000000f,-0.800000f,
00095 0.010000f,0.000000f,-0.800000f,0.007000f,-0.007000f,-0.800000f,
00096 0.040000f,0.000000f,-0.800000f,0.028284f,0.028284f,-0.800000f,
00097 0.007000f,0.007000f,-0.800000f,0.010000f,0.000000f,-0.800000f,
00098 0.007000f,0.007000f,-0.800000f,0.028284f,0.028284f,-0.800000f,
00099 0.000000f,0.040000f,-0.800000f,0.000000f,0.010000f,-0.800000f,
00100 0.000000f,0.010000f,-0.800000f,0.000000f,0.040000f,-0.800000f,
00101 -0.028284f,0.028284f,-0.800000f,-0.007000f,0.007000f,-0.800000f
00102 };
00103 const GLfloat arrow_quad_normals[num_arrow_quad_vertices*3] = {
00104 0.000000f,1.000000f,0.000000f,0.707107f,0.707107f,0.000000f,
00105 0.707107f,0.707107f,0.000000f,0.000000f,1.000000f,0.000000f,
00106 0.000000f,-1.000000f,0.000000f,-0.707107f,-0.707107f,0.000000f,
00107 -0.707107f,-0.707107f,0.000000f,0.000000f,-1.000000f,0.000000f,
00108 -0.707107f,-0.707107f,0.000000f,-1.000000f,0.000000f,0.000000f,
00109 -1.000000f,0.000000f,0.000000f,-0.707107f,-0.707107f,0.000000f,
00110 -1.000000f,0.000000f,0.000000f,-0.707107f,0.707107f,0.000000f,
00111 -0.707107f,0.707107f,0.000000f,-1.000000f,0.000000f,0.000000f,
00112 -0.707107f,0.707107f,0.000000f,0.000000f,1.000000f,0.000000f,
00113 0.000000f,1.000000f,0.000000f,-0.707107f,0.707107f,0.000000f,
00114 0.707107f,0.707107f,0.000000f,1.000000f,0.000000f,0.000000f,
00115 1.000000f,0.000000f,0.000000f,0.707107f,0.707107f,0.000000f,
00116 1.000000f,0.000000f,0.000000f,0.707107f,-0.707107f,0.000000f,
00117 0.707107f,-0.707107f,0.000000f,1.000000f,0.000000f,0.000000f,
00118 0.707107f,-0.707107f,0.000000f,0.000000f,-1.000000f,0.000000f,
00119 0.000000f,-1.000000f,0.000000f,0.707107f,-0.707107f,0.000000f,
00120 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00121 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00122 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00123 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00124 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00125 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00126 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00127 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00128 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00129 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00130 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00131 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00132 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00133 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00134 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f,
00135 0.000000f,0.000000f,1.000000f,0.000000f,0.000000f,1.000000f
00136 };
00137
00138
00139
00140
00141
00142 Renderer::Renderer(RenderManager* render_manager, int x, int y, int w, int h, const char *title)
00143 :received_input(false), screen_shot_number(0), arrow_initialized(false)
00144 {
00145
00146 initialize(render_manager,x,y,w,h,title);
00147
00148 is_initialized = true;
00149
00150 m_frame = 0;
00151 m_time = 0;
00152 m_timebase = 0;
00153 m_fps = 0;
00154
00155 ilGenImages(1,&image_id);
00156 ilBindImage(image_id);
00157 }
00158
00159
00160 Renderer::Renderer()
00161 {
00162 is_initialized = false;
00163 }
00164
00165
00166 Renderer::~Renderer()
00167 {
00168 }
00169
00170 void Renderer::initialize(RenderManager* render_manager,
00171 int x, int y,
00172 int w, int h,
00173 const char *title)
00174 {
00175 float light_ambient[] = { 0, 0, 0, 0 };
00176 float light_diffuse[] = { 1, 1, 1, 1 };
00177 float light_specular[] = { 1, 1, 1, 1 };
00178 float light_position[] = { 0, 0, 100, 0 };
00179
00180
00181
00182
00183
00184 camera_pose.state = IDLE;
00185 camera_pose.pan = 0;
00186 camera_pose.tilt = 0;
00187 camera_pose.distance = 10.0;
00188 camera_pose.x_offset = 0;
00189 camera_pose.y_offset = 0;
00190 camera_pose.z_offset = 0;
00191
00192 camera_pose.x_offset_2D = 0;
00193 camera_pose.y_offset_2D = 0;
00194 camera_pose.rotation_2D = 0;
00195 camera_pose.zoom = 1;
00196 camera_pose.warp_x = 1;
00197 camera_pose.warp_y = 1;
00198
00199 window_width = w;
00200 window_height = h;
00201
00202 fps = 30;
00203
00204 user_idle_func = NULL;
00205 user_timer_func = NULL;
00206 user_display_func = NULL;
00207 user_keyboard_func = NULL;
00208 user_mouse_func = NULL;
00209 user_motion_func = NULL;
00210
00211 zoom_sensitivity = DEFAULT_ZOOM_SENSITIVITY;
00212 rotate_sensitivity = DEFAULT_ROTATE_SENSITIVITY;
00213 move_sensitivity = DEFAULT_MOVE_SENSITIVITY;
00214 min_zoom_range = DEFAULT_MIN_ZOOM_RANGE;
00215 camera_fov = DEFAULT_CAMERA_FOV;
00216 min_clip_range = DEFAULT_MIN_CLIP_RANGE;
00217 max_clip_range = DEFAULT_MAX_CLIP_RANGE;
00218
00219 zoom_sensitivity_2D = DEFAULT_ZOOM_SENSITIVITY_2D;
00220 rotate_sensitivity_2D = DEFAULT_ROTATE_SENSITIVITY_2D;
00221 move_sensitivity_2D = DEFAULT_MOVE_SENSITIVITY_2D;
00222
00223
00224 glutInitDisplayMode(GLUT_RGBA | GLUT_DOUBLE | GLUT_DEPTH);
00225 glutInitWindowSize(window_width, window_height);
00226 glutInitWindowPosition(x, y);
00227
00228
00229 window_id = render_manager->createWindow(title,this);
00230
00231
00232 glClearColor(0, 0, 0, 0);
00233
00234
00235 glEnable(GL_DEPTH_TEST);
00236
00237
00238 glEnable(GL_COLOR_MATERIAL);
00239 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
00240
00241
00242 glEnable(GL_BLEND);
00243 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
00244
00245
00246 glShadeModel(GL_SMOOTH);
00247
00248
00249 glEnable(GL_LIGHTING);
00250 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient);
00251 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse);
00252 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular);
00253 glLightfv(GL_LIGHT0, GL_POSITION, light_position);
00254 glEnable(GL_LIGHT0);
00255 glEnable(GL_NORMALIZE);
00256
00257
00258
00259
00260
00261
00262
00263
00264
00265
00266
00267 mode = GUI_MODE_3D;
00268 }
00269
00270
00271
00272
00273
00274
00275
00276
00277
00278
00279
00280
00281
00282
00283
00284
00285
00286 void Renderer::setIdleFunc(idle_func idle)
00287 {
00288 user_idle_func = idle;
00289 }
00290
00291 void Renderer::setTimerFunc(timer_func timer)
00292 {
00293 user_timer_func = timer;
00294 }
00295
00296 void Renderer::setDisplayFunc(display_func display)
00297 {
00298 user_display_func = display;
00299 }
00300
00301 void Renderer::setKeyboardFunc(keyboard_func keyboard)
00302 {
00303 user_keyboard_func = keyboard;
00304 }
00305
00306 void Renderer::setMouseFunc(mouse_func mouse)
00307 {
00308 user_mouse_func = mouse;
00309 }
00310
00311 void Renderer::setMotionFunc(motion_func motion)
00312 {
00313 user_motion_func = motion;
00314 }
00315
00316 void Renderer::setPassiveMotionFunc(passive_motion_func passive_motion)
00317 {
00318 user_passive_motion_func = passive_motion;
00319 }
00320
00321 void Renderer::clear()
00322 {
00323
00324 }
00325
00326 void Renderer::setRenderMode(eRenderMode render_mode)
00327 {
00328 render_mode = render_mode;
00329 }
00330
00331 eRenderMode Renderer::getRenderMode()
00332 {
00333 return render_mode;
00334 }
00335
00336 void Renderer::addRenderNode(RenderNode* node)
00337 {
00338 render_nodes.push_back(node);
00339 }
00340
00341 void Renderer::clearRenderNode()
00342 {
00343 render_nodes.clear();
00344 }
00345
00346 void Renderer::setCameraParams(double _zoom_sensitivity,
00347 double _rotate_sensitivity,
00348 double _move_sensitivity,
00349 double _min_zoom_range,
00350 double _camera_fov,
00351 double _min_clip_range,
00352 double _max_clip_range)
00353 {
00354 zoom_sensitivity = _zoom_sensitivity;
00355 rotate_sensitivity = _rotate_sensitivity;
00356 move_sensitivity = _move_sensitivity;
00357 min_zoom_range = _min_zoom_range;
00358 camera_fov = _camera_fov;
00359 min_clip_range = _min_clip_range;
00360 max_clip_range = _max_clip_range;
00361 received_input = true;
00362 }
00363
00364 void Renderer::set2DCameraParams(double _zoom_sensitivity,
00365 double _rotate_sensitivity,
00366 double _move_sensitivity)
00367 {
00368 zoom_sensitivity_2D = _zoom_sensitivity;
00369 rotate_sensitivity_2D = _rotate_sensitivity;
00370 move_sensitivity_2D = _move_sensitivity;
00371 received_input = true;
00372 }
00373
00374 void Renderer::rotate_camera(double dx, double dy)
00375 {
00376 camera_pose.pan -= dx * rotate_sensitivity;
00377 camera_pose.tilt += dy * rotate_sensitivity;
00378 if(camera_pose.tilt < 0)
00379 camera_pose.tilt = 0;
00380 else if(camera_pose.tilt > 89.0)
00381 camera_pose.tilt = 89.0;
00382
00383
00384
00385
00386
00387
00388 }
00389
00390 void Renderer::zoom_camera(double dy)
00391 {
00392 if (dy>0) camera_pose.distance = camera_pose.distance / (1 + zoom_sensitivity_2D * dy);
00393 else camera_pose.distance = camera_pose.distance * ( 1 - dy * zoom_sensitivity_2D);
00394
00395 if(camera_pose.distance < min_zoom_range)
00396 camera_pose.distance = min_zoom_range;
00397 }
00398
00399 void Renderer::move_camera(double dx, double dy)
00400 {
00401 camera_pose.x_offset += -dy * cos(D2R*(camera_pose.pan)) * move_sensitivity * camera_pose.distance;
00402 camera_pose.y_offset += -dy * sin(D2R*(camera_pose.pan)) * move_sensitivity * camera_pose.distance;
00403 camera_pose.x_offset += dx * cos(D2R*(camera_pose.pan - 90.0)) * move_sensitivity * camera_pose.distance;
00404 camera_pose.y_offset += dx * sin(D2R*(camera_pose.pan - 90.0)) * move_sensitivity * camera_pose.distance;
00405 }
00406
00407 void Renderer::move_camera_2D(double dx, double dy)
00408 {
00409 double tempx, tempy;
00410
00411 tempx = dx / (float)camera_pose.zoom;
00412 tempy = -dy / (float)camera_pose.zoom;
00413
00414 camera_pose.x_offset_2D -=
00415 (tempx * cos(-camera_pose.rotation_2D) -
00416 tempy * sin(-camera_pose.rotation_2D)) /
00417 camera_pose.warp_x * move_sensitivity_2D;
00418 camera_pose.y_offset_2D -=
00419 (tempx * sin(-camera_pose.rotation_2D) +
00420 tempy * cos(-camera_pose.rotation_2D)) /
00421 camera_pose.warp_y * move_sensitivity_2D;
00422 }
00423
00424 void Renderer::rotate_camera_2D(double dx)
00425 {
00426 camera_pose.rotation_2D += dx * rotate_sensitivity_2D;
00427 }
00428
00429 void Renderer::zoom_camera_2D(double dx)
00430 {
00431 if (dx>0) camera_pose.zoom = camera_pose.zoom * (1 + zoom_sensitivity_2D * dx);
00432 else camera_pose.zoom = camera_pose.zoom / ( 1 - dx * zoom_sensitivity_2D);
00433 if(camera_pose.zoom > float(1e7))
00434 camera_pose.zoom = float(1e7);
00435 if(camera_pose.zoom < float(1e-12))
00436 camera_pose.zoom = float(1e-12);
00437 }
00438
00439
00440 void Renderer::setMode2D(void)
00441 {
00442 mode = GUI_MODE_2D;
00443 }
00444
00445 void Renderer::setMode3D(void)
00446 {
00447 mode = GUI_MODE_3D;
00448 }
00449
00450 int Renderer::getMode(void)
00451 {
00452 return mode;
00453 }
00454
00455 void Renderer::setMode(int _mode)
00456 {
00457 mode = _mode;
00458 }
00459
00460 void Renderer::switchTo2DMode(void) {
00461 setDisplayMode2D(window_width, window_height);
00462
00463 glTranslatef(window_width / 2.0, window_height / 2.0, 0.0);
00464 glScalef(camera_pose.zoom, camera_pose.zoom, 1.0);
00465 glRotatef(R2D*(camera_pose.rotation_2D), 0, 0, 1);
00466 glScalef(camera_pose.warp_x, camera_pose.warp_y, 1);
00467 glTranslatef(-camera_pose.x_offset_2D, -camera_pose.y_offset_2D, 0.0);
00468 }
00469
00470 void Renderer::switchTo3DMode(void)
00471 {
00472 float cpan, ctilt, camera_x, camera_y, camera_z;
00473
00474
00475 cpan = camera_pose.pan * M_PI / 180.0;
00476 ctilt = camera_pose.tilt * M_PI / 180.0;
00477 camera_x = camera_pose.distance * cos(cpan) * cos(ctilt);
00478 camera_y = camera_pose.distance * sin(cpan) * cos(ctilt);
00479 camera_z = camera_pose.distance * sin(ctilt);
00480 setDisplayMode3D(window_width, window_height, camera_fov, min_clip_range, max_clip_range);
00481 glViewport(0, 0, (GLsizei)window_width, (GLsizei)window_height);
00482 gluLookAt(camera_x + camera_pose.x_offset,
00483 camera_y + camera_pose.y_offset,
00484 camera_z + camera_pose.z_offset,
00485 camera_pose.x_offset,
00486 camera_pose.y_offset,
00487 camera_pose.z_offset,
00488 0, 0, 1);
00489 }
00490
00491
00492 void Renderer::setInitialCameraPos(double pan, double tilt, double range,
00493 double x_offset, double y_offset,
00494 double z_offset)
00495 {
00496 camera_pose.pan = pan;
00497 camera_pose.tilt = tilt;
00498 camera_pose.distance = range;
00499 camera_pose.x_offset = x_offset;
00500 camera_pose.y_offset = y_offset;
00501 camera_pose.z_offset = z_offset;
00502 }
00503
00504 void Renderer::setInitial2DCameraPos(double x_offset, double y_offset,
00505 double rotation, double zoom)
00506 {
00507 camera_pose.x_offset_2D = x_offset;
00508 camera_pose.y_offset_2D = y_offset;
00509 camera_pose.rotation_2D = rotation;
00510 camera_pose.zoom = zoom;
00511 }
00512
00513
00514
00515
00516
00517
00518
00519 void Renderer::forceRedraw(void)
00520 {
00521 received_input = true;
00522 }
00523
00524 void Renderer::set2DWarping(double warp_x, double warp_y)
00525 {
00526 camera_pose.warp_x = warp_x;
00527 camera_pose.warp_y = warp_y;
00528 }
00529
00530 void Renderer::recenter(void)
00531 {
00532 camera_pose.x_offset = 0;
00533 camera_pose.y_offset = 0;
00534 camera_pose.z_offset = 0;
00535 }
00536
00537 void Renderer::recenter2D(void)
00538 {
00539 camera_pose.x_offset_2D = 0;
00540 camera_pose.y_offset_2D = 0;
00541 }
00542
00543 void Renderer::pickPoint(int mouse_x, int mouse_y, double *scene_x, double *scene_y)
00544 {
00545 double cx = window_width / 2.0;
00546 double cy = window_height / 2.0;
00547 double pan = D2R*(-90.0 - camera_pose.pan);
00548 double tilt = D2R*(90.0 - camera_pose.tilt);
00549 double d = camera_pose.distance;
00550 double f = cy / tan(D2R*(camera_fov / 2.0));
00551
00552
00553 double px = (mouse_x - cx) * cos(tilt) * d / (cos(tilt) * f + sin(tilt) * mouse_y - sin(tilt) * cy);
00554 double py = -(mouse_y - cy) * d / (cos(tilt) * f + sin(tilt) * mouse_y - sin(tilt) * cy);
00555
00556
00557 *scene_x = px * cos(pan) + py * sin(pan) + camera_pose.x_offset;
00558 *scene_y = -px * sin(pan) + py * cos(pan) + camera_pose.y_offset;
00559 }
00560
00561 void Renderer::get2DPosition(int x, int y, double *xout, double *yout)
00562 {
00563 double dx, dy, ctheta, stheta;
00564
00565 dx = (x - window_width / 2.0 - GLUI_x_offset) / camera_pose.zoom;
00566 dy = (window_height / 2.0 - y + GLUI_y_offset) / camera_pose.zoom;
00567 ctheta = cos(-camera_pose.rotation_2D);
00568 stheta = sin(-camera_pose.rotation_2D);
00569 *xout = camera_pose.x_offset_2D + ctheta * dx - stheta * dy;
00570 *yout = camera_pose.y_offset_2D + stheta * dx + ctheta * dy;
00571 }
00572
00573 void Renderer::get3DPosition(double x, double y, double z, double *xout, double *yout, double *zout)
00574 {
00575 GLint viewport[4];
00576 GLdouble modelview[16];
00577 GLdouble projection[16];
00578 glGetIntegerv(GL_VIEWPORT,viewport);
00579 glGetDoublev(GL_MODELVIEW_MATRIX,modelview);
00580 glGetDoublev(GL_PROJECTION_MATRIX,projection);
00581 gluUnProject(x,y,z,modelview,projection,viewport,xout,yout,zout);
00582 }
00583
00584 void Renderer::setDisplayMode2D(int w, int h)
00585 {
00586 glDisable(GL_DEPTH_TEST);
00587 glMatrixMode(GL_PROJECTION);
00588 glLoadIdentity();
00589 gluOrtho2D(0.0, (GLfloat)w, 0.0, (GLfloat)h);
00590 glMatrixMode(GL_MODELVIEW);
00591 glLoadIdentity();
00592 }
00593
00594 void Renderer::setDisplayMode3D(int w, int h, float fovy, float zNear, float zFar)
00595 {
00596 glEnable(GL_DEPTH_TEST);
00597 glMatrixMode(GL_PROJECTION);
00598 glLoadIdentity();
00599 gluPerspective(fovy, w / (float)h, zNear, zFar);
00600 glMatrixMode(GL_MODELVIEW);
00601 glLoadIdentity();
00602 }
00603
00604
00605
00606
00607
00608 void Renderer::special(int key, int x, int y)
00609 {
00610 int mod;
00611 double dx = 0, dy = 0;
00612
00613 mod = glutGetModifiers();
00614 if(mode == GUI_MODE_3D) {
00615 if(mod & GLUT_ACTIVE_CTRL) {
00616 switch(key) {
00617 case GLUT_KEY_LEFT:
00618 dx = -KEY_ROTATE_AMOUNT;
00619 dy = 0;
00620 break;
00621 case GLUT_KEY_RIGHT:
00622 dx = KEY_ROTATE_AMOUNT;
00623 dy = 0;
00624 break;
00625 case GLUT_KEY_UP:
00626 dx = 0;
00627 dy = KEY_ROTATE_AMOUNT;
00628 break;
00629 case GLUT_KEY_DOWN:
00630 dx = 0;
00631 dy = -KEY_ROTATE_AMOUNT;
00632 break;
00633 }
00634 if(dx != 0 || dy != 0)
00635 rotate_camera(dx, dy);
00636 }
00637 else if(mod & GLUT_ACTIVE_ALT) {
00638 switch(key) {
00639 case GLUT_KEY_UP:
00640 dy = KEY_ZOOM_AMOUNT;
00641 break;
00642 case GLUT_KEY_DOWN:
00643 dy = -KEY_ZOOM_AMOUNT;
00644 break;
00645 }
00646 if(dy != 0)
00647 zoom_camera(dy);
00648 }
00649 else {
00650 switch(key) {
00651 case GLUT_KEY_LEFT:
00652 dx = KEY_MOVE_AMOUNT;
00653 dy = 0;
00654 break;
00655 case GLUT_KEY_RIGHT:
00656 dx = -KEY_MOVE_AMOUNT;
00657 dy = 0;
00658 break;
00659 case GLUT_KEY_UP:
00660 dx = 0;
00661 dy = KEY_MOVE_AMOUNT;
00662 break;
00663 case GLUT_KEY_DOWN:
00664 dx = 0;
00665 dy = -KEY_MOVE_AMOUNT;
00666 break;
00667 }
00668 if(dx != 0 || dy != 0)
00669 move_camera(dx, dy);
00670 }
00671 }
00672 else {
00673 if(mod & GLUT_ACTIVE_CTRL) {
00674 switch(key) {
00675 case GLUT_KEY_LEFT:
00676 dx = KEY_ROTATE_AMOUNT;
00677 dy = 0;
00678 break;
00679 case GLUT_KEY_RIGHT:
00680 dx = -KEY_ROTATE_AMOUNT;
00681 dy = 0;
00682 break;
00683 }
00684 if(dx != 0)
00685 rotate_camera_2D(dx);
00686 }
00687 else if(mod & GLUT_ACTIVE_ALT) {
00688 switch(key) {
00689 case GLUT_KEY_UP:
00690 dy = KEY_ZOOM_AMOUNT;
00691 break;
00692 case GLUT_KEY_DOWN:
00693 dy = -KEY_ZOOM_AMOUNT;
00694 break;
00695 }
00696 if(dy != 0)
00697 zoom_camera_2D(dy);
00698 }
00699 else {
00700 switch(key) {
00701 case GLUT_KEY_LEFT:
00702 dx = KEY_MOVE_AMOUNT;
00703 dy = 0;
00704 break;
00705 case GLUT_KEY_RIGHT:
00706 dx = -KEY_MOVE_AMOUNT;
00707 dy = 0;
00708 break;
00709 case GLUT_KEY_UP:
00710 dx = 0;
00711 dy = KEY_MOVE_AMOUNT;
00712 break;
00713 case GLUT_KEY_DOWN:
00714 dx = 0;
00715 dy = -KEY_MOVE_AMOUNT;
00716 break;
00717 }
00718 if(dx != 0 || dy != 0)
00719 move_camera_2D(dx, dy);
00720 }
00721 }
00722 received_input = true;
00723 }
00724
00725 void Renderer::keyboard(unsigned char key, int x, int y)
00726 {
00727 received_input = true;
00728 if(user_keyboard_func)
00729 user_keyboard_func(key, x, y);
00730 }
00731
00732 void Renderer::mouse(int button, int state, int x, int y)
00733 {
00734 if(state == GLUT_DOWN) {
00735 last_mouse_x = x;
00736 last_mouse_y = y;
00737 if(button == GLUT_LEFT_BUTTON)
00738 camera_pose.state = ROTATING;
00739 else if(button == GLUT_MIDDLE_BUTTON)
00740 camera_pose.state = MOVING;
00741 else if(button == GLUT_RIGHT_BUTTON)
00742 camera_pose.state = ZOOMING;
00743 }
00744 else if(state == GLUT_UP)
00745 camera_pose.state = IDLE;
00746
00747 received_input = true;
00748 if(user_mouse_func)
00749 user_mouse_func(button, state, x, y);
00750 }
00751
00752 void Renderer::motion(int x, int y)
00753 {
00754 int dx, dy;
00755
00756 dx = x - last_mouse_x;
00757 dy = y - last_mouse_y;
00758
00759 if(mode == GUI_MODE_3D) {
00760 if(camera_pose.state == ROTATING)
00761 rotate_camera(dx, dy);
00762 else if(camera_pose.state == MOVING)
00763 move_camera(dx, dy);
00764 else if(camera_pose.state == ZOOMING)
00765 zoom_camera(dy);
00766 }
00767 else if(mode == GUI_MODE_2D) {
00768 if(camera_pose.state == ROTATING) {
00769
00770
00771
00772 if(x > window_width / 2)
00773 dy *= -1;
00774
00775 rotate_camera_2D(dx);
00776 }
00777 else if(camera_pose.state == MOVING)
00778 move_camera_2D(dx, dy);
00779 else if(camera_pose.state == ZOOMING)
00780 zoom_camera_2D(dy);
00781 }
00782
00783 last_mouse_x = x;
00784 last_mouse_y = y;
00785
00786 received_input = true;
00787
00788 if(user_motion_func)
00789 user_motion_func(x, y);
00790 }
00791
00792 void Renderer::reshape(int w, int h)
00793 {
00794 int tx = 0, ty = 0, tw = w, th = h;
00795
00796 GLUI_Master.get_viewport_area(&tx, &ty, &tw, &th);
00797 GLUI_x_offset = tx;
00798 GLUI_y_offset = (h - th) - ty;
00799 glViewport(tx, ty, tw, th);
00800
00801 window_width = tw;
00802 window_height = th;
00803
00804 if(mode == GUI_MODE_3D)
00805 setDisplayMode3D(tw, th, camera_fov, min_clip_range,
00806 max_clip_range);
00807 else if(mode == GUI_MODE_2D)
00808 setDisplayMode2D(tw, th);
00809 }
00810
00811 void Renderer::display()
00812 {
00813 glutSetWindow(window_id);
00814
00815
00816 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
00817 if(mode == GUI_MODE_3D) {
00818
00819 switchTo3DMode();
00820 }
00821 else if(mode == GUI_MODE_2D) {
00822 switchTo2DMode();
00823 }
00824
00825
00826 glColor3f(1,1,1);
00827 int mode_save = mode;
00828 TRenderNodeList::iterator it, it_end;
00829 for(it=render_nodes.begin(), it_end=render_nodes.end();it!=it_end;++it)
00830 {
00831 (*it)->preRender();
00832 (*it)->render();
00833 (*it)->postRender();
00834 }
00835 mode = mode_save;
00836
00837
00838 if(user_display_func)
00839 user_display_func();
00840
00841
00842 m_frame++;
00843 m_time=glutGet(GLUT_ELAPSED_TIME);
00844 if (m_time - m_timebase > 1000)
00845 {
00846 m_fps = m_frame*1000.0/(m_time-m_timebase);
00847 m_timebase = m_time;
00848 m_frame = 0;
00849 }
00850
00851
00852
00853
00854
00855
00856
00857
00858
00859 glFlush();
00860 glutSwapBuffers();
00861 }
00862
00863 void Renderer::passive_motion(int x, int y)
00864 {
00865
00866 }
00867
00868 void Renderer::visibility(int visible)
00869 {
00870
00871 }
00872
00873 void Renderer::timer()
00874 {
00875 if(received_input) {
00876 glutSetWindow(window_id);
00877 glutPostRedisplay();
00878 received_input = false;
00879 }
00880 if (user_timer_func)
00881 user_timer_func();
00882 }
00883
00884 void Renderer::idle()
00885 {
00886 if (user_idle_func)
00887 user_idle_func();
00888 }
00889
00890 camera_pose_p Renderer::getCameraPose()
00891 {
00892 return &camera_pose;
00893 }
00894
00895 void Renderer::setCameraPose(camera_pose_t camera_pose)
00896 {
00897 camera_pose = camera_pose;
00898 }
00899
00900 int Renderer::getWindowId()
00901 {
00902 return window_id;
00903 }
00904
00905 int Renderer::getWindowWidth()
00906 {
00907 return window_width;
00908 }
00909
00910 int Renderer::getWindowHeight()
00911 {
00912 return window_height;
00913 }
00914
00915 void Renderer::renderBitmapString(float x, float y, void *font, char *string)
00916 {
00917 char *c;
00918
00919 glPushMatrix();
00920 glRasterPos2d(x, y);
00921 for(c = string; *c != '\0'; c++)
00922 glutBitmapCharacter(font, *c);
00923 glPopMatrix();
00924 }
00925
00926 int Renderer::bitmapStringWidth(void *font, char *string)
00927 {
00928 int width = 0;
00929 char *c;
00930
00931 for(c = string; *c != '\0'; c++)
00932 width += glutBitmapWidth(font, *c);
00933 return width;
00934 }
00935
00936 void Renderer::renderBitmapString3D(float x, float y, float z, void *font, char *string)
00937 {
00938 char *c;
00939
00940 glPushMatrix();
00941 glRasterPos3d(x, y, z);
00942 glBitmap(0, 0, 0, 0, -bitmapStringWidth(font, string) / 2.0, 0, NULL);
00943
00944 for(c = string; *c != '\0'; c++)
00945 glutBitmapCharacter(font, *c);
00946 glPopMatrix();
00947 }
00948
00949 void Renderer::renderBitmapStringCentered(float x, float y, void *font, char *string)
00950 {
00951 char *c;
00952 int vert_offset = -5;
00953
00954 glPushMatrix();
00955 glRasterPos2d(x, y);
00956 if(font == GLUT_BITMAP_HELVETICA_18)
00957 vert_offset = -7;
00958 else if(font == GLUT_BITMAP_HELVETICA_12)
00959 vert_offset = -4;
00960 else if(font == GLUT_BITMAP_HELVETICA_10)
00961 vert_offset = -3;
00962 glBitmap(0, 0, 0, 0,
00963 -bitmapStringWidth(font, string) / 2.0, vert_offset, NULL);
00964 for(c = string; *c != '\0'; c++)
00965 glutBitmapCharacter(font, *c);
00966 glPopMatrix();
00967 }
00968
00969
00970 void Renderer::takeScreenShot(const char *filename)
00971 {
00972 ilutRenderer(ILUT_OPENGL);
00973 ilutGLScreen();
00974 ilEnable(IL_FILE_OVERWRITE);
00975 ilSaveImage((const ILstring)filename);
00976 screen_shot_number++;
00977 }
00978
00979 void Renderer::takeScreenShot()
00980 {
00981 char buf[255];
00982 sprintf(buf,"screenshot_%03d.png",screen_shot_number);
00983 takeScreenShot(buf);
00984 }
00985
00986
00987 void Renderer::initializeArrow()
00988 {
00989 if (!GLEW_ARB_vertex_buffer_object)
00990 {
00991 arrow_initialized = true;
00992 return;
00993 }
00994
00995 glGenBuffersARB(1, &arrow_triangle_vertices_vbo);
00996 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrow_triangle_vertices_vbo);
00997 glBufferDataARB(GL_ARRAY_BUFFER_ARB, num_arrow_triangle_vertices*3*sizeof(GLfloat), arrow_triangle_vertices, GL_STATIC_DRAW_ARB);
00998
00999 glGenBuffersARB(1, &arrow_triangle_normals_vbo);
01000 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrow_triangle_normals_vbo);
01001 glBufferDataARB(GL_ARRAY_BUFFER_ARB, num_arrow_triangle_vertices*3*sizeof(GLfloat), arrow_triangle_normals, GL_STATIC_DRAW_ARB);
01002
01003 glGenBuffersARB(1, &arrow_quad_vertices_vbo);
01004 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrow_quad_vertices_vbo);
01005 glBufferDataARB(GL_ARRAY_BUFFER_ARB, num_arrow_quad_vertices*3*sizeof(GLfloat), arrow_quad_vertices, GL_STATIC_DRAW_ARB);
01006
01007 glGenBuffersARB(1, &arrow_quad_normals_vbo);
01008 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrow_quad_normals_vbo);
01009 glBufferDataARB(GL_ARRAY_BUFFER_ARB, num_arrow_quad_vertices*3*sizeof(GLfloat), arrow_quad_normals, GL_STATIC_DRAW_ARB);
01010
01011 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
01012 arrow_initialized = true;
01013 };
01014
01015
01016
01017 void Renderer::drawCoordinateFrame(double scale)
01018 {
01019 double a_axisThicknessScale = 3*scale;
01020
01021
01022 if(!arrow_initialized)
01023 initializeArrow();
01024
01025
01026 glEnable(GL_COLOR_MATERIAL);
01027 glColorMaterial(GL_FRONT, GL_AMBIENT_AND_DIFFUSE);
01028 glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
01029
01030 int k;
01031 for(k=0; k<3; k++) {
01032
01033 glPushMatrix();
01034
01035
01036 if (k==0) {
01037 glRotatef(-90.0,0,1,0);
01038 glColor3f(1.0f,0.0f,0.0f);
01039 }
01040 else if (k==1) {
01041 glRotatef(90.0,1,0,0);
01042 glColor3f(0.0f,1.0f,0.0f);
01043 }
01044 else {
01045 glRotatef(180.0,1,0,0);
01046 glColor3f(0.0f,0.0f,1.0f);
01047 }
01048
01049 glScaled(a_axisThicknessScale,a_axisThicknessScale,scale);
01050
01051 if (!GLEW_ARB_vertex_buffer_object)
01052 {
01053 glBegin(GL_TRIANGLES);
01054 for (int i=0;i<num_arrow_triangle_vertices;i++)
01055 {
01056 glVertex3d(arrow_triangle_vertices[i*3],arrow_triangle_vertices[i*3+1],arrow_triangle_vertices[i*3+2]);
01057 glNormal3d(arrow_triangle_normals[i*3],arrow_triangle_normals[i*3+1],arrow_triangle_normals[i*3+2]);
01058 }
01059 glEnd();
01060
01061 glBegin(GL_QUADS);
01062 for (int i=0;i<num_arrow_quad_vertices;i++)
01063 {
01064 glVertex3d(arrow_quad_vertices[i*3],arrow_quad_vertices[i*3+1],arrow_quad_vertices[i*3+2]);
01065 glNormal3d(arrow_quad_normals[i*3],arrow_quad_normals[i*3+1],arrow_quad_normals[i*3+2]);
01066 }
01067 glEnd();
01068 }
01069 else
01070 {
01071 glEnableClientState(GL_NORMAL_ARRAY);
01072 glEnableClientState(GL_VERTEX_ARRAY);
01073 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrow_triangle_vertices_vbo);
01074 glVertexPointer(3, GL_FLOAT, 0, 0);
01075 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrow_triangle_normals_vbo);
01076 glNormalPointer(GL_FLOAT, 0, 0);
01077 glDrawArrays(GL_TRIANGLES, 0, num_arrow_triangle_vertices);
01078
01079 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrow_quad_vertices_vbo);
01080 glVertexPointer(3, GL_FLOAT, 0, 0);
01081 glBindBufferARB(GL_ARRAY_BUFFER_ARB, arrow_quad_normals_vbo);
01082 glNormalPointer(GL_FLOAT, 0, 0);
01083 glDrawArrays(GL_QUADS, 0, num_arrow_quad_vertices);
01084
01085 glBindBufferARB(GL_ARRAY_BUFFER_ARB, 0);
01086
01087 glDisableClientState(GL_NORMAL_ARRAY);
01088 glDisableClientState(GL_VERTEX_ARRAY);
01089 }
01090
01091 glPopMatrix();
01092 }
01093 }
01094
01095 void Renderer::drawGrid(double center_x, double center_y, int size, double resolution) const
01096 {
01097 double grid_x, grid_y;
01098 double size_2=(double)size/2.0;
01099
01100 glLineWidth(0.5);
01101 glEnable(GL_BLEND);
01102 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
01103
01104 glEnable(GL_LINE_SMOOTH);
01105 glBegin(GL_LINES);
01106 for(grid_x = -size_2; grid_x < size_2; grid_x+=resolution) {
01107 glVertex3f(grid_x - center_x, -size_2 - center_y, 0);
01108 glVertex3f(grid_x - center_x, size_2 - center_y, 0);
01109 }
01110 for(grid_y = -size_2; grid_y < size_2; grid_y+=resolution) {
01111 glVertex3f(-size_2 - center_x, grid_y - center_y, 0);
01112 glVertex3f( size_2 - center_x, grid_y - center_y, 0);
01113 }
01114 glEnd();
01115 glDisable(GL_LINE_SMOOTH);
01116 glDisable(GL_BLEND);
01117 }
01118
01119 void Renderer::drawDashedLine3D(double x1, double y1, double z1, double x2, double y2, double z2, double stripe_len) const
01120 {
01121 double frac, dx, dy, dz, x, y, z;
01122 int i;
01123
01124 frac = stripe_len / sqrt(rtc_sqr(x2-x1)+rtc_sqr(y2-y1)+rtc_sqr(z2-z1));
01125 dx = frac * (x2 - x1);
01126 dy = frac * (y2 - y1);
01127 dz = frac * (z2 - z1);
01128 x = x1;
01129 y = y1;
01130 z = z1;
01131 glBegin(GL_LINES);
01132 for(i = 0; i < (int)floor(1 / frac); i++) {
01133 if(i % 2 == 0) {
01134 glVertex3f(x, y, z);
01135 glVertex3f(x + dx, y + dy, z + dz);
01136 }
01137 x += dx;
01138 y += dy;
01139 z += dz;
01140 }
01141 glEnd();
01142 }
01143
01144 Vec3d Renderer::unProject(double winx, double winy, double winz)
01145 {
01146 Vec3d pos;
01147 GLint viewport[4];
01148 GLdouble modelview[16];
01149 GLdouble projection[16];
01150 glGetDoublev( GL_MODELVIEW_MATRIX, modelview );
01151 glGetDoublev( GL_PROJECTION_MATRIX, projection );
01152 glGetIntegerv( GL_VIEWPORT, viewport );
01153 gluUnProject( winx, winy, winz, modelview, projection, viewport, &pos[0], &pos[1], &pos[2]);
01154 return pos;
01155 }
01156
01157 void Renderer::setRasterPos(double winx, double winy, double winz)
01158 {
01159 Vec3d pos=unProject(winx,winy,winz);
01160 glRasterPos3dv(pos.x);
01161 }
01162
01163
01164 }
01165
01166