protected static int convertToRepTex(GLContext ctx, int tex, int width, int height, boolean repeatX, boolean repeatY, boolean mipmapped) { int reptex = ctx.createTexture(width, height, repeatX, repeatY, mipmapped); int fbuf = ctx.createFramebuffer(reptex); ctx.pushFramebuffer(fbuf, width, height); try { // render the non-repeated texture into the framebuffer properly scaled ctx.clear(0, 0, 0, 0); GLShader shader = ctx.quadShader(null).prepareTexture(tex, Tint.NOOP_TINT); shader.addQuad(ctx.createTransform(), 0, height, width, 0, 0, 0, 1, 1); shader.flush(); // if we're mipmapped, we can now generate our mipmaps if (mipmapped) ctx.generateMipmap(reptex); return reptex; } finally { // we no longer need this framebuffer; rebind the previous framebuffer and delete ours ctx.popFramebuffer(); ctx.deleteFramebuffer(fbuf); } } }
private int scaleTexture() { int scaledWidth = scale.scaledCeil(width()); int scaledHeight = scale.scaledCeil(height()); // GL requires pow2 on axes that repeat int width = GLUtil.nextPowerOfTwo(scaledWidth), height = GLUtil.nextPowerOfTwo(scaledHeight); // TODO: if width/height > platform_max_size, repeatedly scale by 0.5 until within bounds // platform_max_size = 1024 for iOS, GL10.GL_MAX_TEXTURE_SIZE on android, etc. // no need to scale if our source data is already a power of two if ((width == 0) && (height == 0)) { int reptex = createPow2RepTex(scaledWidth, scaledHeight, repeatX, repeatY, mipmapped); if (mipmapped) ctx.generateMipmap(reptex); return reptex; } // otherwise we need to scale our non-repeated texture, so load that normally int tex = createMainTex(); // width/height == 0 => already a power of two. if (width == 0) width = scaledWidth; if (height == 0) height = scaledHeight; // create our texture and point a new framebuffer at it try { return convertToRepTex(ctx, tex, width, height, repeatX, repeatY, mipmapped); } finally { // delete the non-repeated texture ctx.destroyTexture(tex); } }
shader.flush(); if (mipmapped) ctx.generateMipmap(reptex); return reptex;