bekovski

temp

Jan 15th, 2018
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.49 KB | None | 0 0
  1. #include "pch.h"
  2.  
  3. #include <Kore/IO/FileReader.h>
  4. #include <Kore/Math/Core.h>
  5. #include <Kore/System.h>
  6. #include <Kore/Input/Keyboard.h>
  7. #include <Kore/Input/Mouse.h>
  8. #include <Kore/Graphics1/Image.h>
  9. #include <Kore/Graphics4/Graphics.h>
  10. #include <Kore/Graphics4/PipelineState.h>
  11. #include <Kore/Threads/Thread.h>
  12. #include <Kore/Threads/Mutex.h>
  13. #include "Memory.h"
  14. #include "ObjLoader.h"
  15.  
  16. #include <vector>
  17. #include <algorithm>
  18.  
  19. using namespace Kore;
  20.  
  21. class MeshData {
  22. public:
  23.     MeshData(const char* meshFile, const Graphics4::VertexStructure& structure) {
  24.         mesh = loadObj(meshFile);
  25.        
  26.         vertexBuffer = new Graphics4::VertexBuffer(mesh->numVertices, structure);
  27.         float* vertices = vertexBuffer->lock();
  28.         for (int i = 0; i < mesh->numVertices; ++i) {
  29.             vertices[i * 8 + 0] = mesh->vertices[i * 8 + 0];
  30.             vertices[i * 8 + 1] = mesh->vertices[i * 8 + 1];
  31.             vertices[i * 8 + 2] = mesh->vertices[i * 8 + 2];
  32.             vertices[i * 8 + 3] = mesh->vertices[i * 8 + 3];
  33.             vertices[i * 8 + 4] = 1.0f - mesh->vertices[i * 8 + 4];
  34.             vertices[i * 8 + 5] = mesh->vertices[i * 8 + 5];
  35.             vertices[i * 8 + 6] = mesh->vertices[i * 8 + 6];
  36.             vertices[i * 8 + 7] = mesh->vertices[i * 8 + 7];
  37.         }
  38.         vertexBuffer->unlock();
  39.  
  40.         indexBuffer = new Graphics4::IndexBuffer(mesh->numFaces * 3);
  41.         int* indices = indexBuffer->lock();
  42.         for (int i = 0; i < mesh->numFaces * 3; i++) {
  43.             indices[i] = mesh->indices[i];
  44.         }
  45.         indexBuffer->unlock();
  46.     }
  47.  
  48.     Graphics4::VertexBuffer* vertexBuffer;
  49.     Graphics4::IndexBuffer* indexBuffer;
  50.     Mesh* mesh;
  51. };
  52.  
  53. class MeshObject {
  54. public:
  55.     MeshObject(MeshData* mesh, Graphics4::Texture* image, vec3 position) : mesh(mesh), image(image) {
  56.         M = mat4::Translation(position.x(), position.y(), position.z());
  57.     }
  58.  
  59.     void render(Graphics4::TextureUnit tex) {
  60.         Graphics4::setTexture(tex, image);
  61.         Graphics4::setVertexBuffer(*mesh->vertexBuffer);
  62.         Graphics4::setIndexBuffer(*mesh->indexBuffer);
  63.         Graphics4::drawIndexedVertices();
  64.     }
  65.  
  66.     void setTexture(Graphics4::Texture* tex) {
  67.         image = tex;
  68.     }
  69.  
  70.     Graphics4::Texture* getTexture() {
  71.         return image;
  72.     }
  73.  
  74.     mat4 M;
  75. private:
  76.     MeshData* mesh;
  77.     Graphics4::Texture* image;
  78. };
  79.  
  80. namespace {
  81.     const int width = 800;
  82.     const int height = 600;
  83.     double startTime;
  84.     Graphics4::Shader* vertexShader;
  85.     Graphics4::Shader* fragmentShader;
  86.     Graphics4::PipelineState* pipeline;
  87.  
  88.     // null terminated array of MeshObject pointers
  89.     MeshObject** objects;
  90.  
  91.     // The view projection matrix aka the camera
  92.     mat4 P;
  93.     mat4 V;
  94.  
  95.     vec3 position;
  96.     bool up = false, down = false, left = false, right = false;
  97.  
  98.     Thread* streamingThread;
  99.     Mutex streamMutex;
  100.     //
  101.     Mutex myMutex;
  102.     bool myPseudoMutex = false;
  103.  
  104.     // uniform locations - add more as you see fit
  105.     Graphics4::TextureUnit tex;
  106.     Graphics4::ConstantLocation pLocation;
  107.     Graphics4::ConstantLocation vLocation;
  108.     Graphics4::ConstantLocation mLocation;
  109.  
  110.     float angle;
  111.  
  112.     void stream(void*) {
  113.         for (;;) {
  114.             // to use a mutex, create a Mutex variable and call Create to initialize the mutex (see main()). Then you can use Lock/Unlock.
  115.             streamMutex.Lock();
  116.            
  117.             // load darmstadt.jpg files for near boxes
  118.             // reload darmstadt.jpg for every box, pretend that every box has a different texture (I don't want to upload 100 images though)
  119.             // feel free to create more versions of darmstadt.jpg at different sizes
  120.             // always use less than 1 million pixels of texture data (the example code uses 100 16x16 textures - that's 25600 pixels, darmstadt.jpg is 512x512 aka 262144 pixels)
  121.  
  122.             // Beware, neither OpenGL nor Direct3D is thread safe - you can't just create a Texture in a second thread. But you can create a Kore::Image
  123.             // in another thread, access it's pixels in the main thread and put them in a Kore::Texture using lock/unlock.
  124.  
  125.             // first get the position of every box
  126.             // TODO: only consider those boxes that are visible to the camera
  127.             std::vector<vec3> temp;
  128.             for (int y = 0; y < 10; ++y) {
  129.                 for (int x = 0; x < 10; ++x) {
  130.                     // 0.. 99
  131.                     MeshObject** current = &objects[y * 10 + x];
  132.                     mat4 M = (*current)->M;
  133.                     vec3 currentPos(M.get(0, 3), M.get(1, 3), M.get(2, 3));
  134.                     if (currentPos.z() > position.z())   // TODO: not sufficient to filter out non-visible boxes
  135.                         temp.push_back(currentPos);
  136.                 }
  137.             }
  138.  
  139.             // now sort them by their distance to the camera position
  140.             struct distance_to_position {
  141.                 inline bool operator() (const vec3& objPosition1, const vec3& objPosition2) {
  142.                     float dist1 = position.distance(objPosition1);
  143.                     float dist2 = position.distance(objPosition2);
  144.  
  145.                     return dist1 < dist2;
  146.                 }
  147.             };
  148.             std::sort(temp.begin(), temp.end(), distance_to_position());
  149.  
  150.             // the closest boxes get high resolution: 3 * 512^2 = 786432
  151.             std::vector<vec3> nearestBoxes;
  152.             nearestBoxes.push_back(temp[0]);
  153.             nearestBoxes.push_back(temp[1]);
  154.             nearestBoxes.push_back(temp[2]);
  155.  
  156.             // meidum resolution: 2 * 256^2 = 131072
  157.             std::vector<vec3> second_nearestBoxes;
  158.             second_nearestBoxes.push_back(temp[3]);
  159.             second_nearestBoxes.push_back(temp[4]);
  160.             // second_nearestBoxes.push_back(temp[5]);
  161.  
  162.             // small resolution: 3 * 128^2 = 49152
  163.             std::vector<vec3> third_nearestBoxes;
  164.             third_nearestBoxes.push_back(temp[5]);
  165.             third_nearestBoxes.push_back(temp[6]);  // 6 and 7 are not visible to the camera
  166.             third_nearestBoxes.push_back(temp[7]);
  167.  
  168.  
  169.             // 100 - 8 = 92 * 16^2 = 23552
  170.             // total: 3*512^2 + 2*256^2 + 3*128^2 + 92*16^2 = 990208 < 1 million pixels
  171.  
  172.             streamMutex.Unlock();
  173.         }
  174.     }
  175.  
  176.     void update() {
  177.         float t = (float)(System::time() - startTime);
  178.  
  179.         const float speed = 0.1f;
  180.         if (up) position.z() += speed;
  181.         if (down) position.z() -= speed;
  182.         if (left) position.x() -= speed;
  183.         if (right) position.x() += speed;
  184.        
  185.         Graphics4::begin();
  186.         Graphics4::clear(Graphics4::ClearColorFlag | Graphics4::ClearDepthFlag, 0xff9999FF, 1.0f);
  187.        
  188.         Graphics4::setPipeline(pipeline);
  189.  
  190.    
  191.         // set the camera
  192.         P = mat4::Perspective(pi / 4.0f, (float)width / (float)height, 0.1f, 100);
  193.         V = mat4::lookAt(position, vec3(0, 0, 1000), vec3(0, 1, 0));
  194.         Graphics4::setMatrix(pLocation, P);
  195.         Graphics4::setMatrix(vLocation, V);
  196.  
  197.  
  198.         angle = t;
  199.  
  200.  
  201.         //objects[0]->M = mat4::RotationY(angle) * mat4::RotationZ(Kore::pi / 4.0f);
  202.  
  203.         for (int y = 0; y < 10; ++y) {
  204.             for (int x = 0; x < 10; ++x) {
  205.                 MeshObject** current = &objects[y * 10 + x];
  206.  
  207.                 u8* texPointer = (*current)->getTexture.lock();
  208.                 Graphics1::Image img("darmstadt.jpg", true);
  209.  
  210.             }
  211.         }
  212.  
  213.         // iterate the MeshObjects
  214.         MeshObject** current = &objects[0];
  215.         while (*current != nullptr) {
  216.             // set the model matrix
  217.             Graphics4::setMatrix(mLocation, (*current)->M);
  218.  
  219.             (*current)->render(tex);
  220.             ++current;
  221.         }
  222.  
  223.         Graphics4::end();
  224.         Graphics4::swapBuffers();
  225.     }
  226.  
  227.     void mouseMove(int windowId, int x, int y, int movementX, int movementY) {
  228.  
  229.     }
  230.    
  231.     void mousePress(int windowId, int button, int x, int y) {
  232.  
  233.     }
  234.  
  235.     void mouseRelease(int windowId, int button, int x, int y) {
  236.  
  237.     }
  238.  
  239.     void keyDown(KeyCode code) {
  240.         switch (code) {
  241.         case KeyLeft:
  242.             left = true;
  243.             break;
  244.         case KeyRight:
  245.             right = true;
  246.             break;
  247.         case KeyUp:
  248.             up = true;
  249.             break;
  250.         case KeyDown:
  251.             down = true;
  252.             break;
  253.         }
  254.     }
  255.  
  256.     void keyUp(KeyCode code) {
  257.         switch (code) {
  258.         case KeyLeft:
  259.             left = false;
  260.             break;
  261.         case KeyRight:
  262.             right = false;
  263.             break;
  264.         case KeyUp:
  265.             up = false;
  266.             break;
  267.         case KeyDown:
  268.             down = false;
  269.             break;
  270.         }
  271.     }
  272.  
  273.  
  274.     void init() {
  275.         FileReader vs("shader.vert");
  276.         FileReader fs("shader.frag");
  277.         vertexShader = new Graphics4::Shader(vs.readAll(), vs.size(), Graphics4::VertexShader);
  278.         fragmentShader = new Graphics4::Shader(fs.readAll(), fs.size(), Graphics4::FragmentShader);
  279.  
  280.         // This defines the structure of your Vertex Buffer
  281.         Graphics4::VertexStructure structure;
  282.         structure.add("pos", Graphics4::Float3VertexData);
  283.         structure.add("tex", Graphics4::Float2VertexData);
  284.         structure.add("nor", Graphics4::Float3VertexData);
  285.  
  286.         pipeline = new Graphics4::PipelineState;
  287.         pipeline->inputLayout[0] = &structure;
  288.         pipeline->inputLayout[1] = nullptr;
  289.         pipeline->vertexShader = vertexShader;
  290.         pipeline->fragmentShader = fragmentShader;
  291.         pipeline->depthMode = Graphics4::ZCompareLess;
  292.         pipeline->depthWrite = true;
  293.         pipeline->compile();
  294.  
  295.         tex = pipeline->getTextureUnit("tex");
  296.         pLocation = pipeline->getConstantLocation("P");
  297.         vLocation = pipeline->getConstantLocation("V");
  298.         mLocation = pipeline->getConstantLocation("M");
  299.  
  300.         objects = new MeshObject*[101];
  301.         for (int i = 0; i < 101; ++i) objects[i] = nullptr;
  302.  
  303.         MeshData* mesh = new MeshData("box.obj", structure);
  304.         for (int y = 0; y < 10; ++y) {
  305.             for (int x = 0; x < 10; ++x) {
  306.                 objects[y * 10 + x] = new MeshObject(mesh, new Graphics4::Texture("darmstadtmini.png", true), vec3((x - 5.0f) * 10, 0, (y - 5.0f) * 10));
  307.             }
  308.         }
  309.  
  310.         angle = 0.0f;
  311.  
  312.         Graphics4::setTextureAddressing(tex, Graphics4::U, Graphics4::Repeat);
  313.         Graphics4::setTextureAddressing(tex, Graphics4::V, Graphics4::Repeat);
  314.     }
  315. }
  316.  
  317. int kore(int argc, char** argv) {
  318.     Kore::System::init("Exercise 10", width, height);
  319.    
  320.     Memory::init();
  321.     init();
  322.  
  323.     Kore::System::setCallback(update);
  324.  
  325.     startTime = System::time();
  326.    
  327.     Keyboard::the()->KeyDown = keyDown;
  328.     Keyboard::the()->KeyUp = keyUp;
  329.     Mouse::the()->Move = mouseMove;
  330.     Mouse::the()->Press = mousePress;
  331.     Mouse::the()->Release = mouseRelease;
  332.  
  333.     streamMutex.Create();
  334.     Kore::threadsInit();
  335.     streamingThread = Kore::createAndRunThread(stream, nullptr);
  336.  
  337.     Kore::System::start();
  338.    
  339.     return 0;
  340. }
Advertisement
Add Comment
Please, Sign In to add comment