Advertisement
Guest User

Untitled

a guest
Jul 16th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.09 KB | None | 0 0
  1. #include "src\base.h"
  2. #include "src\render.h"
  3. #include "src\glCommands.h"
  4. #include "src\render\meshGens.h"
  5. #include "src\render\assetLoader.h"
  6. #include "src\render\shaderFac.h"
  7.  
  8. #include <iostream>
  9. #include <vector>
  10.  
  11. #include "src\matrix\vec4.h"
  12. #include "src\matrix\vec3.h"
  13. #include "src\ext\math.h"
  14. #include "src\ext\handlerMap.h"
  15.  
  16. class Block {
  17. private:
  18.     typedef engine::Model Model;
  19.  
  20.     Model model;
  21.     matrix::Vec3 pos = {};
  22.     matrix::Vec3 vel = {};
  23.     matrix::Vec3 rotAxis = {};
  24.     f32 rotRate = 0.0;
  25.     f32 alive = 0.0;
  26.  
  27. public:
  28.     Block() : model(createModel()) {
  29.         using namespace ext::math;
  30.         vel[0] = randFloat(-1.0f, 1.0f);
  31.         vel[1] = randFloat(-1.0f, 1.0f);
  32.         vel[2] = randFloat(-1.0f, 1.0f);
  33.         //vel *= 5.0f;
  34.  
  35.         rotAxis[0] = randFloat(-1.0f, 1.0f);
  36.         rotAxis[1] = randFloat(-1.0f, 1.0f);
  37.         rotAxis[2] = randFloat(-1.0f, 1.0f);
  38.         rotAxis.normalize();
  39.         //std::cout << rotAxis.length() << "\n";
  40.         rotRate = randFloat(0.1f, 500.0f);
  41.  
  42.         alive = randFloat(-10.0f, 0.0f);
  43.         //alive = 0.0f;
  44.     }
  45.  
  46.     void update(number dt) {
  47.         //if (alive > 10.0f) {
  48.         //  model.clear();
  49.         //}
  50.  
  51.         alive += dt;
  52.  
  53.         model.pos += vel * dt;
  54.         //std::cout << rotRate * alive << "\n";
  55.         model.rot.axisAngle(rotAxis, rotRate * alive);
  56.         model.scl.setTo(0.1, 0.1, 0.1);
  57.     }
  58.  
  59.     void draw(const engine::BasicCamera& camera) {
  60.         model.drawModel(camera);
  61.     }
  62.  
  63. private:
  64.     static Model createModel() {
  65.         static auto layout = engine::AttrLayout{} //TODO: automagically generate chunk layout based on shader inputs
  66.             .attrPointer("a_pos", 3, DataType::F32)
  67.             .attrPointer("a_vertexColor", 4, DataType::U8, true)
  68.             .attrPointer("a_uvs", 2, DataType::U16, true)
  69.             ;
  70.         static engine::DrawBatch batch { layout };
  71.  
  72.         engine::Model model{ batch };
  73.         model.attachMesh(
  74.             [] {
  75.                 auto mesh = engine::createMesh::Plane();
  76.                 mesh.colors = {
  77.                     255, 0, 0, 255,
  78.                     0, 255, 0, 255,
  79.                     0, 0, 255, 255,
  80.                     255, 255, 0, 255,
  81.                 };
  82.                 return mesh;
  83.             }(),
  84.             [&] {
  85.                 engine::Material mat{};
  86.                 //mat.useTexture(engine::mat::TEXTURE_ADD, tex);
  87.                 mat.vertexColor = engine::mat::VERTEX_COLOR_ADD;
  88.                 return mat;
  89.             }()
  90.         );
  91.         return model;
  92.     }
  93. };
  94.  
  95.  
  96. int main(int argc, char *argv[])
  97. {
  98.     engine::Window window{};
  99.     engine::MainLoop loop{};
  100.     engine::BasicCamera camera{ window };
  101.     camera.getProjection().perspective(ext::math::toRadians(90.0), 640.0 / 480.0, 0.01, 1000.0);
  102.     camera.getView().setTranslation(0.0, 0.0, -5.0);
  103.     camera.madeChanges();
  104.     engine::AssetLoader loader{};
  105.     auto tex = loader.loadImg(R"(assets\smug.jpg)");
  106.        
  107.     engine::TextureObj tex_4Pixels{}; // testing a texture constructed by hand...
  108.     {
  109.         u8 arr[16] = {
  110.             196, 0, 0, 255,
  111.             0, 196, 0, 255,
  112.             0, 0, 196, 255,
  113.             196, 0, 196, 255
  114.         };
  115.         tex_4Pixels.asColorTexture(arr, 2, 2, 4);
  116.         tex_4Pixels.setMagFilter(GL_NEAREST);
  117.         tex_4Pixels.setMinFilter(GL_NEAREST);
  118.     }
  119.    
  120.     auto layout = engine::AttrLayout{} //TODO: automagically generate chunk layout based on shader inputs
  121.         .attrPointer("a_pos", 3, DataType::F32)
  122.         .attrPointer("a_vertexColor", 4, DataType::U8, true)
  123.         .attrPointer("a_uvs", 2, DataType::U16, true)
  124.         ;
  125.  
  126.     auto batch = engine::DrawBatch{ layout };
  127.  
  128.     engine::Model model{ batch };
  129.     model.attachMesh(
  130.         []{return engine::createMesh::Plane();}(),
  131.         [&]{
  132.             engine::Material mat{};
  133.             mat.useTexture(engine::mat::TEXTURE_ADD, tex_4Pixels);
  134.             return mat;
  135.         }()
  136.     );
  137.  
  138.     engine::Model model2{ batch };
  139.     model2.attachMesh(
  140.         [] {
  141.             auto mesh = engine::createMesh::Cube();
  142.             mesh.colors = {
  143.                 255, 0, 0, 255,
  144.                 0, 255, 0, 255,
  145.                 0, 0, 255, 255,
  146.                 255, 255, 0, 255,
  147.  
  148.                 255, 0, 0, 255,
  149.                 0, 255, 0, 255,
  150.                 0, 0, 255, 255,
  151.                 255, 255, 0, 255,
  152.  
  153.                 255, 0, 0, 255,
  154.                 0, 255, 0, 255,
  155.                 0, 0, 255, 255,
  156.                 255, 255, 0, 255,
  157.  
  158.                 255, 0, 0, 255,
  159.                 0, 255, 0, 255,
  160.                 0, 0, 255, 255,
  161.                 255, 255, 0, 255,
  162.  
  163.                 255, 0, 0, 255,
  164.                 0, 255, 0, 255,
  165.                 0, 0, 255, 255,
  166.                 255, 255, 0, 255,
  167.  
  168.                 255, 0, 0, 255,
  169.                 0, 255, 0, 255,
  170.                 0, 0, 255, 255,
  171.                 255, 255, 0, 255
  172.             };
  173.             return mesh;
  174.         }(),
  175.         [&] {
  176.             engine::Material mat{};
  177.             mat.useTexture(engine::mat::TEXTURE_ADD, tex);
  178.             mat.vertexColor = engine::mat::VERTEX_COLOR_ADD;
  179.             return mat;
  180.         }()
  181.     );
  182.  
  183.     std::vector<Block> blocks{};
  184.  
  185.     loop.attachMouseDownCallback([&](){
  186.         auto amt = 1000;
  187.         for (auto i = 0; i < amt; ++i) {
  188.             //auto h = blocks.acquire();
  189.             blocks.emplace_back();
  190.         }
  191.  
  192.         std::cout << "# of blocks: " << blocks.size() << "\n";
  193.     });
  194.  
  195.     glEnable(GL_DEPTH_TEST);
  196.  
  197.     auto time = 0.0f;
  198.     loop.flexStep([&](auto dt, auto ts) {
  199.         glClearColor(0.5, 0.5, 0.5, 1.0);
  200.         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
  201.  
  202.         model.rot.axisAngle({ 0.0f, 1.0f, 0.0f }, time * 2.0f);
  203.  
  204.         model.drawModel(camera);
  205.  
  206.         model2.pos[0] = (std::sin(time*0.5f));
  207.         model2.rot.axisAngle({ 0.0, 0.71f, 0.71f }, time * 5.0f);
  208.         model2.scl = matrix::Vec3{ 1.0f, 1.0f, 1.0f } * (std::sin(time)+1.0f);
  209.  
  210.         model2.drawModel(camera);
  211.  
  212.         for (auto& b : blocks) {
  213.             b.update(dt);
  214.             b.draw(camera);
  215.         }
  216.        
  217.  
  218.         window.swapBuffer();
  219.         time += static_cast<float>(dt);
  220.     });
  221.  
  222.     loop.start();
  223.     return 0;
  224. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement