Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- void stream(void*) {
- for (;;) {
- // to use a mutex, create a Mutex variable and call Create to initialize the mutex (see main()). Then you can use Lock/Unlock.
- streamMutex.Lock();
- // load darmstadt.jpg files for near boxes
- // reload darmstadt.jpg for every box, pretend that every box has a different texture (I don't want to upload 100 images though)
- // feel free to create more versions of darmstadt.jpg at different sizes
- // 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)
- // 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
- // in another thread, access it's pixels in the main thread and put them in a Kore::Texture using lock/unlock.
- // first get the position of every box
- // TODO: only consider those boxes that are visible to the camera
- std::vector<vec3> temp;
- for (int y = 0; y < 10; ++y) {
- for (int x = 0; x < 10; ++x) {
- // 0.. 99
- MeshObject** current = &objects[y * 10 + x];
- mat4 M = (*current)->M;
- vec3 currentPos(M.get(0, 3), M.get(1, 3), M.get(2, 3));
- if(currentPos.z() > position.z()) // TODO: not sufficient to filter out non-visible boxes
- temp.push_back(currentPos);
- }
- }
- // now sort them by their distance to the camera position
- struct distance_to_position {
- inline bool operator() (const vec3& objPosition1, const vec3& objPosition2) {
- float dist1 = position.distance(objPosition1);
- float dist2 = position.distance(objPosition2);
- return dist1 < dist2;
- }
- };
- std::sort(temp.begin(), temp.end(), distance_to_position());
- // the closest boxes get high resolution: 3 * 512^2 = 786432
- std::vector<vec3> nearestBoxes;
- nearestBoxes.push_back(temp[0]);
- nearestBoxes.push_back(temp[1]);
- nearestBoxes.push_back(temp[2]);
- // meidum resolution: 2 * 256^2 = 131072
- std::vector<vec3> second_nearestBoxes;
- second_nearestBoxes.push_back(temp[3]);
- second_nearestBoxes.push_back(temp[4]);
- // second_nearestBoxes.push_back(temp[5]);
- // small resolution: 3 * 128^2 = 49152
- std::vector<vec3> third_nearestBoxes;
- third_nearestBoxes.push_back(temp[5]);
- third_nearestBoxes.push_back(temp[6]); // 6 and 7 are not visible to the camera
- third_nearestBoxes.push_back(temp[7]);
- // 100 - 8 = 92 * 16^2 = 23552
- // total: 3*512^2 + 2*256^2 + 3*128^2 + 92*16^2 = 990208 < 1 million pixels
- for (int y = 0; y < 10; ++y) {
- for (int x = 0; x < 10; ++x) {
- MeshObject** current = &objects[y * 10 + x];
- mat4 M = (*current)->M;
- vec3 currentPos(M.get(0, 3), M.get(1, 3), M.get(2, 3));
- // if close (at most 3? using 4 of these already over 1 million pixels)
- if (std::find(nearestBoxes.begin(), nearestBoxes.end(), currentPos) != nearestBoxes.end()) {
- (*current)->setTexture(new Graphics4::Texture("darmstadt.jpg", true));
- }
- else if (std::find(second_nearestBoxes.begin(), second_nearestBoxes.end(), currentPos) != second_nearestBoxes.end()) {
- (*current)->setTexture(new Graphics4::Texture("darmstadtmedium.jpg", true));
- }
- else if (std::find(third_nearestBoxes.begin(), third_nearestBoxes.end(), currentPos) != third_nearestBoxes.end()) {
- (*current)->setTexture(new Graphics4::Texture("darmstadtsmall.jpg", true));
- } else {
- (*current)->setTexture(new Graphics4::Texture("darmstadtmini.png", true));
- }
- }
- }
- streamMutex.Unlock();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment