fixed rectpacker
This commit is contained in:
@@ -3,8 +3,6 @@
|
||||
#include <vector>
|
||||
#include <algorithm>
|
||||
|
||||
#define RECTPACK_DISABLE_FLIP // I don't care for now and it complicate things
|
||||
|
||||
using namespace std;
|
||||
|
||||
namespace wiRectPacker
|
||||
@@ -101,25 +99,13 @@ namespace wiRectPacker
|
||||
if (id) return 0;
|
||||
int f = img.fits(rect_xywh(rc));
|
||||
|
||||
#ifdef RECTPACK_DISABLE_FLIP
|
||||
switch (f) {
|
||||
case 0: return 0;
|
||||
case 1: img.flipped = false; break;
|
||||
case 2: img.flipped = false; break;
|
||||
case 3: id = true; img.flipped = false; return this;
|
||||
case 4: id = true; img.flipped = false; return this;
|
||||
case 1: break;
|
||||
case 2: id = true; return this;
|
||||
}
|
||||
#else
|
||||
switch (f) {
|
||||
case 0: return 0;
|
||||
case 1: img.flipped = false; break;
|
||||
case 2: img.flipped = true; break;
|
||||
case 3: id = true; img.flipped = false; return this;
|
||||
case 4: id = true; img.flipped = true; return this;
|
||||
}
|
||||
#endif
|
||||
|
||||
int iw = (img.flipped ? img.h : img.w), ih = (img.flipped ? img.w : img.h);
|
||||
int iw = img.w, ih = img.h;
|
||||
|
||||
if (rc.w() - iw > rc.h() - ih) {
|
||||
c[0].set(rc.l, rc.t, rc.l + iw, rc.b);
|
||||
@@ -223,11 +209,6 @@ namespace wiRectPacker
|
||||
v[i]->x = ret->rc.l;
|
||||
v[i]->y = ret->rc.t;
|
||||
|
||||
if (v[i]->flipped) {
|
||||
v[i]->flipped = false;
|
||||
v[i]->flip();
|
||||
}
|
||||
|
||||
clip_x = std::max(clip_x, ret->rc.r);
|
||||
clip_y = std::max(clip_y, ret->rc.b);
|
||||
|
||||
@@ -235,8 +216,6 @@ namespace wiRectPacker
|
||||
}
|
||||
else {
|
||||
unsucc.push_back(v[i]);
|
||||
|
||||
v[i]->flipped = false;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -282,10 +261,8 @@ namespace wiRectPacker
|
||||
rect_wh::rect_wh(int w, int h) : w(w), h(h) {}
|
||||
|
||||
int rect_wh::fits(const rect_wh& r) const {
|
||||
if (w == r.w && h == r.h) return 3;
|
||||
if (h == r.w && w == r.h) return 4;
|
||||
if (w == r.w && h == r.h) return 2;
|
||||
if (w <= r.w && h <= r.h) return 1;
|
||||
if (h <= r.w && w <= r.h) return 2;
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -351,15 +328,8 @@ namespace wiRectPacker
|
||||
}
|
||||
|
||||
|
||||
rect_xywhf::rect_xywhf(const rect_ltrb& rr) : rect_xywh(rr), flipped(false) {}
|
||||
rect_xywhf::rect_xywhf(int x, int y, int width, int height) : rect_xywh(x, y, width, height), flipped(false) {}
|
||||
rect_xywhf::rect_xywhf() : flipped(false) {}
|
||||
|
||||
void rect_xywhf::flip() {
|
||||
#ifndef RECTPACK_DISABLE_FLIP
|
||||
flipped = !flipped;
|
||||
std::swap(w, h);
|
||||
#endif
|
||||
}
|
||||
rect_xywhf::rect_xywhf(const rect_ltrb& rr) : rect_xywh(rr) {}
|
||||
rect_xywhf::rect_xywhf(int x, int y, int width, int height) : rect_xywh(x, y, width, height) {}
|
||||
rect_xywhf::rect_xywhf() {}
|
||||
|
||||
}
|
||||
|
||||
@@ -9,7 +9,6 @@
|
||||
1. rect_xywhf - structure representing your rectangle object
|
||||
members:
|
||||
int x, y, w, h;
|
||||
bool flipped;
|
||||
|
||||
2. bin - structure representing resultant bin object
|
||||
3. bool pack(rect_xywhf* const * v, int n, int max_side, std::std::vector<bin>& bins) - actual packing function
|
||||
@@ -29,9 +28,8 @@ then for each bin iterate through its rectangles, typecast each one to your own
|
||||
to the array representing your texture atlas to the place specified by the rectangle, then finally upload it with glTexImage2D.
|
||||
|
||||
Algorithm doesn't create any new rectangles.
|
||||
You just pass an array of pointers - rectangles' x/y/w/h/flipped are modified in place.
|
||||
You just pass an array of pointers - rectangles' x/y/w/h are modified in place.
|
||||
There is a vector of pointers for every resultant bin to let you know which ones belong to the particular bin.
|
||||
The algorithm may swap the w and h fields for the sake of better fitting, the flag "flipped" will be set to true whenever this occurs.
|
||||
|
||||
For description how to tune the algorithm and how it actually works see the .cpp file.
|
||||
|
||||
@@ -49,7 +47,7 @@ namespace wiRectPacker
|
||||
rect_wh(const rect_xywh&);
|
||||
rect_wh(int w = 0, int h = 0);
|
||||
int w, h, area(), perimeter(),
|
||||
fits(const rect_wh& bigger) const; // 0 - no, 1 - yes, 2 - flipped, 3 - perfectly, 4 perfectly flipped
|
||||
fits(const rect_wh& bigger) const; // 0 - no, 1 - yes, 2 - perfectly
|
||||
};
|
||||
|
||||
// rectangle implementing left/top/right/bottom behaviour
|
||||
@@ -75,8 +73,6 @@ namespace wiRectPacker
|
||||
rect_xywhf(const rect_ltrb&);
|
||||
rect_xywhf(int x, int y, int width, int height);
|
||||
rect_xywhf();
|
||||
void flip();
|
||||
bool flipped;
|
||||
};
|
||||
|
||||
|
||||
|
||||
@@ -9,7 +9,7 @@ namespace wiVersion
|
||||
// minor features, major updates
|
||||
const int minor = 22;
|
||||
// minor bug fixes, alterations, refactors, updates
|
||||
const int revision = 14;
|
||||
const int revision = 15;
|
||||
|
||||
|
||||
long GetVersion()
|
||||
|
||||
Reference in New Issue
Block a user