bekovski

p10

Jan 12th, 2018
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.58 KB | None | 0 0
  1. void stream(void*) {
  2.         for (;;) {
  3.             // to use a mutex, create a Mutex variable and call Create to initialize the mutex (see main()). Then you can use Lock/Unlock.
  4.             streamMutex.Lock();
  5.            
  6.             // load darmstadt.jpg files for near boxes
  7.             // reload darmstadt.jpg for every box, pretend that every box has a different texture (I don't want to upload 100 images though)
  8.             // feel free to create more versions of darmstadt.jpg at different sizes
  9.             // 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)
  10.  
  11.             // 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
  12.             // in another thread, access it's pixels in the main thread and put them in a Kore::Texture using lock/unlock.
  13.  
  14.             // first get the position of every box
  15.             // TODO: only consider those boxes that are visible to the camera
  16.             std::vector<vec3> temp;
  17.             for (int y = 0; y < 10; ++y) {
  18.                 for (int x = 0; x < 10; ++x) {
  19.                     // 0.. 99
  20.                     MeshObject** current = &objects[y * 10 + x];
  21.                     mat4 M = (*current)->M;
  22.                     vec3 currentPos(M.get(0, 3), M.get(1, 3), M.get(2, 3));
  23.                     if(currentPos.z() > position.z())   // TODO: not sufficient to filter out non-visible boxes
  24.                         temp.push_back(currentPos);
  25.                 }
  26.             }
  27.  
  28.             // now sort them by their distance to the camera position
  29.             struct distance_to_position {
  30.                 inline bool operator() (const vec3& objPosition1, const vec3& objPosition2) {
  31.                     float dist1 = position.distance(objPosition1);
  32.                     float dist2 = position.distance(objPosition2);
  33.  
  34.                     return dist1 < dist2;
  35.                 }
  36.             };
  37.             std::sort(temp.begin(), temp.end(), distance_to_position());
  38.  
  39.             // the closest boxes get high resolution: 3 * 512^2 = 786432
  40.             std::vector<vec3> nearestBoxes;
  41.             nearestBoxes.push_back(temp[0]);
  42.             nearestBoxes.push_back(temp[1]);
  43.             nearestBoxes.push_back(temp[2]);
  44.  
  45.             // meidum resolution: 2 * 256^2 = 131072
  46.             std::vector<vec3> second_nearestBoxes;
  47.             second_nearestBoxes.push_back(temp[3]);
  48.             second_nearestBoxes.push_back(temp[4]);
  49.             // second_nearestBoxes.push_back(temp[5]);
  50.  
  51.             // small resolution: 3 * 128^2 = 49152
  52.             std::vector<vec3> third_nearestBoxes;
  53.             third_nearestBoxes.push_back(temp[5]);
  54.             third_nearestBoxes.push_back(temp[6]);  // 6 and 7 are not visible to the camera
  55.             third_nearestBoxes.push_back(temp[7]);
  56.  
  57.  
  58.             // 100 - 8 = 92 * 16^2 = 23552
  59.             // total: 3*512^2 + 2*256^2 + 3*128^2 + 92*16^2 = 990208 < 1 million pixels
  60.  
  61.             for (int y = 0; y < 10; ++y) {
  62.                 for (int x = 0; x < 10; ++x) {
  63.                     MeshObject** current = &objects[y * 10 + x];
  64.                     mat4 M = (*current)->M;
  65.                     vec3 currentPos(M.get(0, 3), M.get(1, 3), M.get(2, 3));
  66.  
  67.                     // if close (at most 3? using 4 of these already over 1 million pixels)
  68.                     if (std::find(nearestBoxes.begin(), nearestBoxes.end(), currentPos) != nearestBoxes.end()) {
  69.                         (*current)->setTexture(new Graphics4::Texture("darmstadt.jpg", true));
  70.                     }
  71.                     else if (std::find(second_nearestBoxes.begin(), second_nearestBoxes.end(), currentPos) != second_nearestBoxes.end()) {
  72.                         (*current)->setTexture(new Graphics4::Texture("darmstadtmedium.jpg", true));
  73.                     }
  74.                     else if (std::find(third_nearestBoxes.begin(), third_nearestBoxes.end(), currentPos) != third_nearestBoxes.end()) {
  75.                         (*current)->setTexture(new Graphics4::Texture("darmstadtsmall.jpg", true));
  76.                     } else {
  77.                         (*current)->setTexture(new Graphics4::Texture("darmstadtmini.png", true));
  78.                     }
  79.                 }
  80.             }
  81.  
  82.             streamMutex.Unlock();
  83.         }
  84.     }
Advertisement
Add Comment
Please, Sign In to add comment