~tomprogs/gewoopi/trunk

« back to all changes in this revision

Viewing changes to examples/gewoopi.cpp

  • Committer: Thomas Geymayer
  • Date: 2010-03-26 11:56:05 UTC
  • Revision ID: tomgey@gmail.com-20100326115605-iir4j91614620ij3
Fixed .obj loading, enable grabbing and general cleanup.

Show diffs side-by-side

added added

removed removed

Lines of Context:
13
13
#include <gewoopi/gewoopi.hpp>
14
14
#include <gewoopi/input.hpp>
15
15
 
16
 
#include <gewoopi/video/renderer.h>
 
16
#include <gewoopi/video/renderer.hpp>
17
17
#include <gewoopi/scene/mesh.hpp>
18
 
#include <gewoopi/scene/manager.h>
19
 
#include <gewoopi/scene/camera_perspective.h>
 
18
#include <gewoopi/scene/manager.hpp>
 
19
#include <gewoopi/scene/mesh_manager.hpp>
 
20
#include <gewoopi/scene/camera_perspective.hpp>
 
21
#include <gewoopi/video/texture.hpp>
20
22
 
21
23
using namespace gewoopi;
22
24
 
23
25
int main( int argc, char *argv[] )
24
26
{
25
 
  core::Application demo("gewoopi_demo",argc,argv);
26
 
 
27
 
  LOG_INFO("Start demo.");
28
 
 
29
 
  // input
30
 
  input::Button quit( demo.getInputManager()->getButton("quit") );
31
 
 
32
 
  input::Button left ( demo.getInputManager()->getButton("left" ) );
33
 
  input::Button right( demo.getInputManager()->getButton("right") );
34
 
 
35
 
  input::Button button_add( demo.getInputManager()->getButton("add") );
36
 
  input::Button button_sub( demo.getInputManager()->getButton("sub") );
37
 
 
38
 
  // add a few objects
39
 
  scene::Tree scene_tree = demo.getSceneManager()->getSceneTree();
40
 
  scene::Tree::iterator root = scene_tree.root();
41
 
 
42
 
  root.append_child( scene::Node() )->addMesh( scene::Mesh::createCube(1.5) );
43
 
  root.append_child( scene::Node( math::Vector3f(0,-5.75,0) ) )
44
 
                             ->addMesh( scene::Mesh::createCube(10) );
45
 
  scene::Tree::iterator cube = root.append_child( scene::Node( math::Vector3f(2,0,0) ) );
46
 
  cube->addMesh( scene::Mesh::createCube(0.5) );
47
 
 
48
 
  // create a perspective camera
49
 
  scene::CameraPerspectivePtr cam(
50
 
    new scene::CameraPerspective( math::Degreef(45),    // field of view
51
 
                                  4./3,                 // aspect ratio,
52
 
                                  0.7,                  // near clipping
53
 
                                  40 ) );               // far clipping
54
 
 
55
 
  video::RendererPtr renderer = demo.getRenderer();
56
 
  renderer->setCamera( cam );
57
 
 
58
 
  FTGLPixmapFont font("gotham_nights.ttf");
59
 
 
60
 
  if( font.Error() )
61
 
    std::cerr << "Font" << std::endl;
62
 
 
63
 
  font.FaceSize(36);
64
 
 
65
 
  while( demo.run() && !quit )
66
 
  {
67
 
    //--------------------------------------------------------------------------
68
 
 
69
 
    cube->addPosition( math::Vector3f( 0.1f * (left - right), 0, 0 ) );
70
 
 
71
 
    //--------------------------------------------------------------------------
72
 
    // some zooming and moving of the camera
73
 
 
74
 
    float scale = (cos(1.5*demo.getTimeFromStart()+2)+1) * (sin(0.81*demo.getTimeFromStart()-0.1)+1);
75
 
 
76
 
    cam->setFOV( cam->getFOV() + 0.5 * (button_add - button_sub) );
77
 
    cam->setTransform(
78
 
      math::Matrix4x4f::LookAtLH( math::Vector3f( (2+scale)*sin(demo.getTimeFromStart()),
79
 
                                                  1,
80
 
                                                  (2+scale)*cos(demo.getTimeFromStart())),
81
 
                                  math::Vector3f(0,0,0),
82
 
                                  math::Vector3f(0,1,0) ) );
83
 
 
84
 
      font.Render( ("FPS: " + utilities::toStr(demo.getFPS())).c_str(), -1, FTPoint(5,600-5-font.BBox("T").Upper().Y()) );
85
 
  }
86
 
 
87
 
  std::cout << boost::thread::hardware_concurrency() << std::endl;
 
27
  try
 
28
  {
 
29
    core::Application demo("gewoopi_demo",argc,argv);
 
30
 
 
31
    LOG_INFO("Start demo.");
 
32
 
 
33
    video::Texture* tex = demo.getRenderer()->getTexture( "data/tomprogs-80x80.png" );
 
34
    delete tex;
 
35
 
 
36
    // input
 
37
    input::Button quit( demo.getInputManager()->getButton("quit") );
 
38
 
 
39
    input::Button left   ( demo.getInputManager()->getButton("cam-left"   ) );
 
40
    input::Button right  ( demo.getInputManager()->getButton("cam-right"  ) );
 
41
    input::Button forward( demo.getInputManager()->getButton("cam-forward") );
 
42
    input::Button back   ( demo.getInputManager()->getButton("cam-back"   ) );
 
43
    input::Button rotate ( demo.getInputManager()->getButton("rotate" ) );
 
44
 
 
45
    input::Button up   ( demo.getInputManager()->getButton("up"   ) );
 
46
    input::Button down ( demo.getInputManager()->getButton("down" ) );
 
47
 
 
48
    input::Button button_add( demo.getInputManager()->getButton("add") );
 
49
    input::Button button_sub( demo.getInputManager()->getButton("sub") );
 
50
 
 
51
    // add a few objects
 
52
    scene::Tree& scene_tree = demo.getSceneManager()->getSceneTree();
 
53
    scene::Tree::iterator root = scene_tree.root();
 
54
 
 
55
    root.append_child( scene::Node() )->addMesh( scene::Mesh::createCube(1.5) );
 
56
    root.append_child( scene::Node( math::Vector3f(0,-5.75,0) ) )
 
57
                               ->addMesh( scene::MeshManager::instance()->load("data/kerli-castle.obj") /*scene::Mesh::createCube(10)*/ );
 
58
    scene::Tree::iterator cube = root.append_child( scene::Node( math::Vector3f(2,0,0) ) );
 
59
    cube->addMesh( scene::Mesh::createCube(0.5) );
 
60
 
 
61
    // create a perspective camera
 
62
    scene::CameraPerspectivePtr cam(
 
63
      new scene::CameraPerspective( math::Degreef(45),  // field of view
 
64
                                    4./3,               // aspect ratio,
 
65
                                    1,                  // near clipping
 
66
                                    2000 ) );           // far clipping
 
67
 
 
68
    video::RendererPtr renderer = demo.getRenderer();
 
69
    renderer->setCamera( cam );
 
70
    renderer->setCullMode(video::CCW);
 
71
 
 
72
    FTGLPixmapFont font("gotham_nights.ttf");
 
73
 
 
74
    if( font.Error() )
 
75
      std::cerr << "Font" << std::endl;
 
76
 
 
77
    font.FaceSize(36);
 
78
 
 
79
    math::Vector3f cam_pos;
 
80
    math::real rotation = 0;
 
81
 
 
82
    while( demo.nextFrame() && !quit )
 
83
    {
 
84
      //------------------------------------------------------------------------
 
85
 
 
86
      rotation += rotate * demo.getLastFrameTime();
 
87
      math::Vector3f look_dir(cos(rotation), 0, sin(rotation));
 
88
 
 
89
      cam_pos += (forward - back) * look_dir * demo.getLastFrameTime() * 30;
 
90
 
 
91
      cam_pos.y() += (up    - down) * demo.getLastFrameTime() * 20;
 
92
//      cam_pos.z() += (right - left) * demo.getLastFrameTime() * 20;
 
93
 
 
94
      //cube->addPosition( math::Vector3f( 0.1f * (left - right), 0, 0 ) );
 
95
 
 
96
      //------------------------------------------------------------------------
 
97
      // some zooming and moving of the camera
 
98
 
 
99
      //float scale = (cos(1.5*demo.getTimeFromStart()+2)+1) * (sin(0.81*demo.getTimeFromStart()-0.1)+1);
 
100
 
 
101
      cam->setFOV( cam->getFOV() + 0.5 * (button_add - button_sub) );
 
102
      cam->setTransform(
 
103
        math::Matrix4x4f::LookAtLH( cam_pos,
 
104
                                    cam_pos + look_dir,
 
105
                                    math::Vector3f(0,1,0) ) );
 
106
 
 
107
        font.Render( ("FPS: " + utilities::toStr(demo.getFPS())).c_str(), -1, FTPoint(5,600-5-font.BBox("T").Upper().Y()) );
 
108
    }
 
109
 
 
110
    std::cout << boost::thread::hardware_concurrency() << std::endl;
 
111
  }
 
112
  catch( boost::exception& ex )
 
113
  {
 
114
    std::cout << "boost::ex" << std::endl;
 
115
    LOG_ERROR( boost::diagnostic_information(ex) );
 
116
    LOG_EXIT();
 
117
  }
 
118
  catch( std::exception& ex )
 
119
  {
 
120
    std::cout << "ex" << std::endl;
 
121
    LOG_ERROR( ex.what() );
 
122
    LOG_EXIT();
 
123
  }
88
124
 
89
125
  return 0;
90
126
}