Advertisement
Guest User

Untitled

a guest
Aug 18th, 2018
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.39 KB | None | 0 0
  1. #include "demo.hpp"
  2. #include <fge.hpp>
  3. #include <ode/ode.h>
  4.  
  5. using namespace fge;
  6.  
  7. namespace fge
  8. {
  9.     template<>
  10.     Format Format::arg<dVector3>(dVector3 const & val)
  11.     {
  12.         return this->arg(Format("({0},{1},{2})").arg(val[0]).arg(val[1]).arg(val[2]).value);
  13.     }
  14. }
  15.  
  16. glm::vec3 cPos;
  17. float cPan, cTilt;
  18.  
  19. Collider * trimesh;
  20.  
  21. void demo_init()
  22. {
  23.     fge::Initialize3D();
  24.  
  25.     fge::InitializePhysics();
  26.  
  27.     auto level = std::make_shared<Mesh>(fge::LoadStaticMesh("terrain.mdl"));
  28.     auto block = std::make_shared<Mesh>(fge::LoadStaticMesh("block.obj"));
  29.     auto sphere = std::make_shared<Mesh>(fge::LoadStaticMesh("sphere.mdl"));
  30.  
  31.  
  32.     Entity * ball = new Entity();
  33.     AddComponent<Geometry3D>(ball, sphere);
  34.     AddComponent<Transform3D>(ball)->position = glm::vec3(0, 150, 0);
  35.     auto * mtl = AddComponent<Material>(ball);
  36.     mtl->ambient = RGBA(0xFF0000);
  37.     mtl->diffuse = RGBA(0x00FF00);
  38.     mtl->specular = RGBA(0x0000FF);
  39.  
  40.     Entity * world = new Entity();
  41.     AddComponent<Geometry3D>(world, level);
  42.     AddComponent<Transform3D>(world);
  43.  
  44.     auto mesh = CreateTriMesh(*level);
  45.     trimesh = AddComponent<Collider>(world, dCreateTriMesh(
  46.         GetSpace(),
  47.         mesh,
  48.         nullptr,
  49.         nullptr,
  50.         nullptr));
  51.  
  52.     Entity * cube = new Entity();
  53.     AddComponent<Geometry3D>(cube, block);
  54.     AddComponent<Transform3D>(cube)->position = glm::vec3(0,20,0);
  55.     AddComponent<Collider>(cube, dCreateBox, 2.0, 2.0, 2.0);
  56.     auto body = AddComponent<RigidBody>(cube)->body.get();
  57.  
  58.     dMass mass;
  59.     dMassSetBoxTotal(&mass, 10.0, 1.0, 1.0, 1.0);
  60.     dBodySetMass(body, &mass);
  61.  
  62.     AddEventHandler(SDL_KEYDOWN, [=](SDL_Event const & ev)
  63.     {
  64.         if(ev.key.keysym.sym == SDLK_SPACE)
  65.         {
  66.             dBodyAddForce(body, 0, 10000, 0);
  67.             dBodyAddTorque(body, 20, 30, 40);
  68.         }
  69.     });
  70.     AddEventHandler(SDL_MOUSEMOTION, [](SDL_Event const & ev)
  71.     {
  72.         cPan  -= 0.005f * ev.motion.xrel;
  73.         cTilt -= 0.005f * ev.motion.yrel;
  74.     });
  75. }
  76.  
  77. glm::vec3 getForward()
  78. {
  79.     return glm::vec3(
  80.         glm::cos(cTilt) * glm::sin(cPan),
  81.         glm::sin(cTilt),
  82.         glm::cos(cTilt) * glm::cos(cPan));
  83. }
  84.  
  85. void demo_update()
  86. {
  87.     glm::vec3 fwd = getForward();
  88.     glm::vec3 right = glm::cross(fwd, glm::vec3(0,1,0));
  89.  
  90.     if(fge::IsPressed(SDLK_w))
  91.         cPos += fwd;
  92.     if(fge::IsPressed(SDLK_s))
  93.         cPos -= fwd;
  94.     if(fge::IsPressed(SDLK_d))
  95.         cPos += right;
  96.     if(fge::IsPressed(SDLK_a))
  97.         cPos -= right;
  98.  
  99.     fge::UpdatePhysics();
  100. }
  101.  
  102. void demo_render()
  103. {
  104.     glClearColor(0.0f, 0.0f, 0.5f, 1.0f);
  105.     glClearDepth(1.0);
  106.     glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  107.  
  108.     auto view = glm::lookAt(
  109.         cPos,
  110.         cPos + getForward(),
  111.         glm::vec3(0, 1, 0));
  112.     auto proj = glm::perspectiveFov(
  113.         radians(60.0f),
  114.         1280.0f,
  115.         720.0f,
  116.         0.1f,
  117.         10000.0f);
  118.  
  119.     fge::Render3D(view, proj);
  120.  
  121.     {
  122.         std::vector<fge::Vertex> lines;
  123.         auto mesh = trimesh->geom.get();
  124.  
  125.         int count = dGeomTriMeshGetTriangleCount(mesh);
  126.         for(int i = 0; i < count; i++)
  127.         {
  128.             dVector3 a, b, c;
  129.             dGeomTriMeshGetTriangle(mesh, i, &a, &b, &c);
  130.  
  131.             glm::vec3 _a(a[0], a[1], a[2]);
  132.             glm::vec3 _b(b[0], b[1], b[2]);
  133.             glm::vec3 _c(c[0], c[1], c[2]);
  134.  
  135.             lines.push_back(Vertex { _a, RGBA(0xFF0000) });
  136.             lines.push_back(Vertex { _b, RGBA(0xFF0000) });
  137.             lines.push_back(Vertex { _b, RGBA(0xFF0000) });
  138.             lines.push_back(Vertex { _c, RGBA(0xFF0000) });
  139.             lines.push_back(Vertex { _c, RGBA(0xFF0000) });
  140.             lines.push_back(Vertex { _a, RGBA(0xFF0000) });
  141.         }
  142.         fge::RenderRaw3D(view, proj, GL_LINES, lines.data(), lines.size());
  143.     }
  144. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement