65 if (!image ||
min(image->
getSize()) <= 0 || m_itemHash.contains(image))
68 m_itemHash.add(image, m_items.
getSize());
69 Item& item = m_items.
add();
73 item.size = image->
getSize() + border * 2;
87 const Item& item = m_items[*found];
88 return item.pos + item.border;
100 const Item& item = m_items[*found];
113 const Item& item = m_items[*found];
115 Vec2i pos = item.pos + item.border;
128 const Item& item = m_items[*found];
130 return Vec2f((
F32)(item.size.x) / (
F32)(size.x), (
F32)(item.size.y) / (
F32)(size.y));
135 void TextureAtlas::validate(
void)
137 if (!m_atlasTexture.
exists())
147 void TextureAtlas::layoutItems(
void)
152 for (
int i = 0; i < m_items.
getSize(); i++)
153 totalArea += m_items[i].
size.x * m_items[i].size.y;
159 for (
int i = 0; i < m_items.
getSize(); i++)
166 for (
int itemIdx = 0; itemIdx < m_items.
getSize(); itemIdx++)
168 Item& item = m_items[order[itemIdx].y];
169 const Vec2i& s = item.size;
175 for (
int i = 0; i < maxRects.getSize(); i++)
177 const Vec4i& r = maxRects[i];
178 if (r.x + s.x > r.z || r.y + s.y > r.w)
182 cost.x =
max(canvas.x, r.x + s.x) *
max(canvas.y, r.y + s.y);
186 if (cost.x < bestCost.x || (cost.x == bestCost.x && (cost.y < bestCost.y || (cost.y == bestCost.y && cost.z < bestCost.z))))
194 canvas =
max(canvas, bestPos + s);
195 Vec4i t(bestPos, bestPos + s);
199 for (
int i = maxRects.getSize() - 1; i >= 0; i--)
201 Vec4i r = maxRects[i];
202 if (t.x >= r.z || t.y >= r.w || t.z <= r.x || t.w <= r.y)
205 maxRects.removeSwap(i);
206 if (t.x > r.x) maxRects.add(
Vec4i(r.x, r.y, t.x, r.w));
207 if (t.y > r.y) maxRects.add(
Vec4i(r.x, r.y, r.z, t.y));
208 if (t.z < r.z) maxRects.add(
Vec4i(t.z, r.y, r.z, r.w));
209 if (t.w < r.w) maxRects.add(
Vec4i(r.x, t.w, r.z, r.w));
214 for (
int i = maxRects.getSize() - 1; i >= 0; i--)
216 const Vec4i& a = maxRects[i];
217 for (
int j = 0; j < maxRects.getSize(); j++)
219 const Vec4i& b = maxRects[j];
220 if (i != j && a.x >= b.x && a.y >= b.y && a.z <= b.z && a.w <= b.w)
222 maxRects.removeSwap(i);
232 for (
int i = 0; i < m_items.
getSize(); i++)
233 m_atlasSize =
max(m_atlasSize, m_items[i].pos + m_items[i].
size);
238 void TextureAtlas::createAtlas(
void)
241 m_atlasTexture =
Texture(image);
244 for (
int i = 0; i < m_items.
getSize(); i++)
246 const Item& item = m_items[i];
247 Vec2i pos = item.pos + item.border;
248 Vec2i size = item.size - item.border * 2;
249 Vec2i wrap = (item.wrap) ? size - 1 : 0;
251 image->
set(pos, *item.texture.getImage(), 0,
size);
252 for (
int j = 0; j < item.border; j++)
255 image->
set(pos +
Vec2i(size.x, 0), *image, pos +
Vec2i(size.x - 1 - wrap.x, 0),
Vec2i(1, size.y));
256 image->
set(pos +
Vec2i(-1, -1), *image, pos +
Vec2i(-1, wrap.y),
Vec2i(size.x + 2, 1));
257 image->
set(pos +
Vec2i(-1, size.y), *image, pos +
Vec2i(-1, size.y - 1 - wrap.y),
Vec2i(size.x + 2, 1));
const Image * getImage(void) const
Vec2f getTextureSizeF(const Texture &tex)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint border
#define FW_SORT_ARRAY(ARRAY, TYPE, COMPARE)
FW_CUDA_FUNC F32 sqrt(F32 a)
Vec2f getTexturePosF(const Texture &tex)
bool addTexture(const Texture &tex, int border=1, bool wrap=true)
void set(const Vec2i &dstPos, const Image &src, const Vec2i &srcPos, const Vec2i &size)
FW_CUDA_FUNC T min(const VectorBase< T, L, S > &v)
FW_CUDA_FUNC T max(const VectorBase< T, L, S > &v)
Vec2i getTextureSize(const Texture &tex)
FW_CUDA_FUNC Vec2i getXY(void) const
const Vec2i & getSize(void) const
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr const GLvoid GLenum usage GLuint shader GLenum type GLsizei const GLuint framebuffers GLsizei const GLuint renderbuffers GLuint v GLuint v GLenum GLenum GLenum GLuint GLint level GLsizei GLuint framebuffers GLuint const GLchar name GLenum GLintptr GLsizeiptr GLvoid data GLuint GLenum GLint param GLuint GLenum GLint param GLhandleARB programObj GLenum GLenum GLsizei GLsizei height GLenum GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void * image
Vec2i getTexturePos(const Texture &tex)
CUdevice int ordinal char int CUdevice dev CUdevprop CUdevice dev CUcontext ctx CUcontext ctx CUcontext pctx CUmodule const void image CUmodule const void fatCubin CUfunction CUmodule const char name void p CUfunction unsigned int bytes CUtexref pTexRef CUtexref CUarray unsigned int Flags CUtexref int CUaddress_mode am CUtexref unsigned int Flags CUaddress_mode CUtexref int dim CUarray_format int CUtexref hTexRef CUfunction unsigned int numbytes CUfunction int float value CUfunction int CUtexref hTexRef CUfunction int int grid_height CUevent unsigned int Flags CUevent hEvent CUevent hEvent CUstream unsigned int Flags CUstream hStream GLuint bufferobj unsigned int CUdevice dev CUdeviceptr unsigned int CUmodule const char name CUdeviceptr unsigned int bytesize CUdeviceptr dptr void unsigned int bytesize void CUdeviceptr unsigned int ByteCount CUarray unsigned int CUdeviceptr unsigned int ByteCount CUarray unsigned int const void unsigned int ByteCount CUarray unsigned int CUarray unsigned int unsigned int ByteCount void CUarray unsigned int unsigned int CUstream hStream const CUDA_MEMCPY2D pCopy CUdeviceptr const void unsigned int CUstream hStream const CUDA_MEMCPY2D CUstream hStream CUdeviceptr unsigned char unsigned int N CUdeviceptr unsigned int unsigned int N CUdeviceptr unsigned int unsigned short unsigned int unsigned int Height CUarray const CUDA_ARRAY_DESCRIPTOR pAllocateArray CUarray const CUDA_ARRAY3D_DESCRIPTOR pAllocateArray unsigned int CUtexref CUdeviceptr unsigned int bytes CUcontext unsigned int CUdevice device GLenum texture GLenum GLuint buffer GLenum GLuint renderbuffer GLenum GLsizeiptr size
TextureAtlas(const ImageFormat &format=ImageFormat::ABGR_8888)