00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031 #include <wx/wx.h>
00032
00033 #include "../ogre_tools/wx_ogre_render_window.h"
00034 #include "../ogre_tools/grid.h"
00035 #include "../ogre_tools/fps_camera.h"
00036 #include "../ogre_tools/orbit_camera.h"
00037 #include "../ogre_tools/axes.h"
00038 #include "../ogre_tools/shape.h"
00039 #include "../ogre_tools/arrow.h"
00040 #include "../ogre_tools/point_cloud.h"
00041 #include "../ogre_tools/billboard_line.h"
00042 #include "../ogre_tools/initialization.h"
00043
00044 #include <OGRE/OgreRoot.h>
00045 #include <OGRE/OgreSceneManager.h>
00046 #include <OGRE/OgreViewport.h>
00047 #include <OGRE/OgreLight.h>
00048
00049 #include <ros/time.h>
00050
00051 #ifdef __WXMAC__
00052 #include <ApplicationServices/ApplicationServices.h>
00053 #endif
00054
00055 using namespace ogre_tools;
00056
00057 class MyFrame : public wxFrame
00058 {
00059 public:
00060 MyFrame(wxWindow* parent)
00061 : wxFrame(parent, -1, _("Grid Test App"), wxDefaultPosition, wxSize(800,600), wxDEFAULT_FRAME_STYLE)
00062 , left_mouse_down_( false )
00063 , middle_mouse_down_( false )
00064 , right_mouse_down_( false )
00065 , mouse_x_( 0 )
00066 , mouse_y_( 0 )
00067 {
00068 ogre_tools::initializeOgre();
00069
00070 root_ = Ogre::Root::getSingletonPtr();
00071
00072 try
00073 {
00074 scene_manager_ = root_->createSceneManager( Ogre::ST_GENERIC, "TestSceneManager" );
00075
00076 render_panel_ = new ogre_tools::wxOgreRenderWindow( root_, this );
00077 render_panel_->SetSize( this->GetSize() );
00078 render_panel_->setAutoRender(false);
00079
00080 ogre_tools::initializeResources( ogre_tools::V_string() );
00081
00082 camera_ = new ogre_tools::OrbitCamera( scene_manager_ );
00083 camera_->setPosition( 0, 0, 15 );
00084 camera_->getOgreCamera()->setNearClipDistance( 0.1 );
00085
00086 render_panel_->getViewport()->setCamera( camera_->getOgreCamera() );
00087
00088 Ogre::Light* directional_light = scene_manager_->createLight( "MainDirectional" );
00089 directional_light->setType( Ogre::Light::LT_DIRECTIONAL );
00090 directional_light->setDirection( Ogre::Vector3( 0, -1, 1 ) );
00091 directional_light->setDiffuseColour( Ogre::ColourValue( 1.0f, 1.0f, 1.0f ) );
00092
00093 #if 0
00094 ogre_tools::Grid* grid = new ogre_tools::Grid( scene_manager_, NULL, ogre_tools::Grid::Lines, 10, 1.0f, 0.02, Ogre::ColourValue(1.0f, 1.0f, 1.0f, 0.5f));
00095 grid->setHeight(4);
00096
00097
00098 ogre_tools::BillboardLine* line = new ogre_tools::BillboardLine( scene_manager_, NULL );
00099 line->setMaxPointsPerLine(105);
00100 for ( int i = -50; i < 50; ++i )
00101 {
00102 line->addPoint( Ogre::Vector3( i*2, 0.0f, -1.0f ) );
00103 }
00104
00105 for ( int i = 0; i < 5; ++i )
00106 {
00107 line->addPoint( Ogre::Vector3( 4.0f, 0.0f, i ) );
00108 }
00109
00110 line->setLineWidth( 0.05 );
00111 line->setColor( 0.0f, 1.0f, 0.0f, 0.5f );
00112
00113 Shape* sphere = new Shape(Shape::Sphere, scene_manager_);
00114 sphere->setPosition(Ogre::Vector3(0.0f, 0.0f, 2.0f));
00115 sphere->setColor(0.0f, 1.0f, 2.0f, 1.0f);
00116 Shape* cube = new Shape(Shape::Cube, scene_manager_);
00117 cube->setPosition(Ogre::Vector3(0.0f, 1.0f, 2.0f));
00118 cube->setColor(1.0f, 0.0f, 0.0f, 1.0f);
00119 Shape* cylinder = new Shape(Shape::Cylinder, scene_manager_);
00120 cylinder->setPosition(Ogre::Vector3(0.0f, 2.0f, 2.0f));
00121 cylinder->setColor(1.0f, 1.0f, 0.0f, 1.0f);
00122 Shape* cone = new Shape(Shape::Cone, scene_manager_);
00123 cone->setPosition(Ogre::Vector3(0.0f, 3.0f, 2.0f));
00124 cone->setColor(0.0f, 0.0f, 1.0f, 1.0f);
00125
00126 ogre_tools::Axes* axes = new ogre_tools::Axes( scene_manager_ );
00127
00128
00129
00130
00131
00132 ogre_tools::Arrow* arrow = new ogre_tools::Arrow( scene_manager_ );
00133 arrow->setHeadColor( 1.0f, 0.0f, 0.0f );
00134 arrow->setShaftColor( 0.0f, 0.0f, 1.0f );
00135 arrow->setOrientation( Ogre::Quaternion::IDENTITY );
00136
00137
00138 #endif
00139
00140 #if 01
00141 Ogre::SceneNode* scene_node = scene_manager_->getRootSceneNode()->createChildSceneNode();
00142 ogre_tools::PointCloud* pointCloud = new ogre_tools::PointCloud();
00143 pointCloud->setDimensions(0.05f, 0.05f, 0.05f);
00144
00145 pointCloud->setRenderMode(PointCloud::RM_BILLBOARDS);
00146 pointCloud->setCommonDirection(Ogre::Vector3(0.0, 1.0, 0.0));
00147 pointCloud->setCommonUpVector(Ogre::Vector3(0.0, 0.0, -1.0));
00148 pointCloud->setAlpha(1.0);
00149 std::vector<ogre_tools::PointCloud::Point> points;
00150 int32_t xcount = 200;
00151 int32_t ycount = 100;
00152 int32_t zcount = 100;
00153
00154 float factor = 0.1f;
00155 for (int32_t x = 0; x < xcount; ++x)
00156 {
00157 for (int32_t y = 0; y < ycount; ++y)
00158 {
00159 for (int32_t z = 0; z < zcount; ++z)
00160 {
00161
00162 ogre_tools::PointCloud::Point point;
00163 point.x = x * factor;
00164 point.y = y * factor;
00165 point.z = z * factor;
00166
00167 point.setColor(x * 0.1, y * 0.1, z * 0.1);
00168 points.push_back(point);
00169 }
00170 }
00171 }
00172
00173 printf("size: %d\n", points.size());
00174 pointCloud->addPoints( &points.front(), points.size() );
00175 scene_node->attachObject(pointCloud);
00176 #endif
00177 }
00178 catch ( Ogre::Exception& e )
00179 {
00180 printf( "Fatal error: %s\n", e.what() );
00181 exit(1);
00182 }
00183
00184 render_panel_->Connect( wxEVT_LEFT_DOWN, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00185 render_panel_->Connect( wxEVT_MIDDLE_DOWN, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00186 render_panel_->Connect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00187 render_panel_->Connect( wxEVT_MOTION, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00188 render_panel_->Connect( wxEVT_LEFT_UP, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00189 render_panel_->Connect( wxEVT_MIDDLE_UP, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00190 render_panel_->Connect( wxEVT_RIGHT_UP, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00191 render_panel_->Connect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00192
00193 wxTheApp->Connect(wxEVT_IDLE, wxIdleEventHandler( MyFrame::onRenderIdle ), NULL, this );
00194 }
00195
00196 ~MyFrame()
00197 {
00198 render_panel_->Disconnect( wxEVT_LEFT_DOWN, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00199 render_panel_->Disconnect( wxEVT_MIDDLE_DOWN, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00200 render_panel_->Disconnect( wxEVT_RIGHT_DOWN, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00201 render_panel_->Disconnect( wxEVT_MOTION, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00202 render_panel_->Disconnect( wxEVT_LEFT_UP, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00203 render_panel_->Disconnect( wxEVT_MIDDLE_UP, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00204 render_panel_->Disconnect( wxEVT_RIGHT_UP, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00205 render_panel_->Disconnect( wxEVT_MOUSEWHEEL, wxMouseEventHandler( MyFrame::OnMouseEvents ), NULL, this );
00206
00207 render_panel_->Destroy();
00208 }
00209
00210 void onRenderIdle(wxIdleEvent& e)
00211 {
00212 ros::WallTime start = ros::WallTime::now();
00213 root_->renderOneFrame();
00214 ros::WallTime end = ros::WallTime::now();
00215 printf("Render took [%f] msec\n", (end - start).toSec() * 1000.0f);
00216
00217 e.Skip();
00218 e.RequestMore();
00219 }
00220
00221 private:
00222
00223 void OnMouseEvents( wxMouseEvent& event )
00224 {
00225 int lastX = mouse_x_;
00226 int lastY = mouse_y_;
00227
00228 mouse_x_ = event.GetX();
00229 mouse_y_ = event.GetY();
00230
00231 if ( event.LeftDown() )
00232 {
00233 left_mouse_down_ = true;
00234 middle_mouse_down_ = false;
00235 right_mouse_down_ = false;
00236 }
00237 else if ( event.MiddleDown() )
00238 {
00239 left_mouse_down_ = false;
00240 middle_mouse_down_ = true;
00241 right_mouse_down_ = false;
00242 }
00243 else if ( event.RightDown() )
00244 {
00245 left_mouse_down_ = false;
00246 middle_mouse_down_ = false;
00247 right_mouse_down_ = true;
00248 }
00249 else if ( event.LeftUp() )
00250 {
00251 left_mouse_down_ = false;
00252 }
00253 else if ( event.MiddleUp() )
00254 {
00255 middle_mouse_down_ = false;
00256 }
00257 else if ( event.RightUp() )
00258 {
00259 right_mouse_down_ = false;
00260 }
00261 else if ( event.Dragging() )
00262 {
00263 int32_t diff_x = mouse_x_ - lastX;
00264 int32_t diff_y = mouse_y_ - lastY;
00265
00266 bool handled = false;
00267 if ( left_mouse_down_ )
00268 {
00269 camera_->mouseLeftDrag( diff_x, diff_y, event.CmdDown(), event.AltDown(), event.ShiftDown());
00270
00271 handled = true;
00272 }
00273 else if ( middle_mouse_down_ )
00274 {
00275 camera_->mouseMiddleDrag( diff_x, diff_y, event.CmdDown(), event.AltDown(), event.ShiftDown() );
00276
00277 handled = true;
00278 }
00279 else if ( right_mouse_down_ )
00280 {
00281 camera_->mouseRightDrag( diff_x, diff_y, event.CmdDown(), event.AltDown(), event.ShiftDown() );
00282
00283 handled = true;
00284 }
00285 }
00286
00287 if ( event.GetWheelRotation() != 0 )
00288 {
00289 camera_->scrollWheel( event.GetWheelRotation(), event.CmdDown(), event.AltDown(), event.ShiftDown() );
00290 }
00291 }
00292
00293 Ogre::Root* root_;
00294 Ogre::SceneManager* scene_manager_;
00295
00296 ogre_tools::wxOgreRenderWindow* render_panel_;
00297
00298 ogre_tools::Grid* grid_;
00299 ogre_tools::CameraBase* camera_;
00300
00301
00302 bool left_mouse_down_;
00303 bool middle_mouse_down_;
00304 bool right_mouse_down_;
00305 int mouse_x_;
00306 int mouse_y_;
00307 };
00308
00309
00310 class MyApp : public wxApp
00311 {
00312 public:
00313
00314 bool OnInit()
00315 {
00316 #ifdef __WXMAC__
00317 ProcessSerialNumber PSN;
00318 GetCurrentProcess(&PSN);
00319 TransformProcessType(&PSN,kProcessTransformToForegroundApplication);
00320 SetFrontProcess(&PSN);
00321 #endif
00322
00323 wxFrame* frame = new MyFrame(NULL);
00324 SetTopWindow(frame);
00325 frame->Show();
00326 return true;
00327 }
00328
00329 int OnExit()
00330 {
00331 cleanupOgre();
00332 return 0;
00333 }
00334 };
00335
00336 DECLARE_APP(MyApp);
00337 IMPLEMENT_APP(MyApp);