1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.graphics.glutils; 18 19 import com.badlogic.gdx.Application.ApplicationType; 20 import com.badlogic.gdx.Gdx; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.Pixmap; 23 import com.badlogic.gdx.graphics.Pixmap.Blending; 24 import com.badlogic.gdx.graphics.Texture; 25 import com.badlogic.gdx.utils.GdxRuntimeException; 26 27 public class MipMapGenerator { 28 MipMapGenerator()29 private MipMapGenerator () { 30 // disallow, static methods only 31 } 32 33 private static boolean useHWMipMap = true; 34 setUseHardwareMipMap(boolean useHWMipMap)35 static public void setUseHardwareMipMap (boolean useHWMipMap) { 36 MipMapGenerator.useHWMipMap = useHWMipMap; 37 } 38 39 /** Sets the image data of the {@link Texture} based on the {@link Pixmap}. The texture must be bound for this to work. If 40 * <code>disposePixmap</code> is true, the pixmap will be disposed at the end of the method. 41 * @param pixmap the Pixmap */ generateMipMap(Pixmap pixmap, int textureWidth, int textureHeight)42 public static void generateMipMap (Pixmap pixmap, int textureWidth, int textureHeight) { 43 generateMipMap(GL20.GL_TEXTURE_2D, pixmap, textureWidth, textureHeight); 44 } 45 46 /** Sets the image data of the {@link Texture} based on the {@link Pixmap}. The texture must be bound for this to work. If 47 * <code>disposePixmap</code> is true, the pixmap will be disposed at the end of the method. */ generateMipMap(int target, Pixmap pixmap, int textureWidth, int textureHeight)48 public static void generateMipMap (int target, Pixmap pixmap, int textureWidth, int textureHeight) { 49 if (!useHWMipMap) { 50 generateMipMapCPU(target, pixmap, textureWidth, textureHeight); 51 return; 52 } 53 54 if (Gdx.app.getType() == ApplicationType.Android || Gdx.app.getType() == ApplicationType.WebGL 55 || Gdx.app.getType() == ApplicationType.iOS) { 56 generateMipMapGLES20(target, pixmap); 57 } else { 58 generateMipMapDesktop(target, pixmap, textureWidth, textureHeight); 59 } 60 } 61 generateMipMapGLES20(int target, Pixmap pixmap)62 private static void generateMipMapGLES20 (int target, Pixmap pixmap) { 63 Gdx.gl.glTexImage2D(target, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, 64 pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); 65 Gdx.gl20.glGenerateMipmap(target); 66 } 67 generateMipMapDesktop(int target, Pixmap pixmap, int textureWidth, int textureHeight)68 private static void generateMipMapDesktop (int target, Pixmap pixmap, int textureWidth, int textureHeight) { 69 if (Gdx.graphics.supportsExtension("GL_ARB_framebuffer_object") 70 || Gdx.graphics.supportsExtension("GL_EXT_framebuffer_object") || Gdx.gl30 != null) { 71 Gdx.gl.glTexImage2D(target, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, 72 pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); 73 Gdx.gl20.glGenerateMipmap(target); 74 } else { 75 generateMipMapCPU(target, pixmap, textureWidth, textureHeight); 76 } 77 } 78 generateMipMapCPU(int target, Pixmap pixmap, int textureWidth, int textureHeight)79 private static void generateMipMapCPU (int target, Pixmap pixmap, int textureWidth, int textureHeight) { 80 Gdx.gl.glTexImage2D(target, 0, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, 81 pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); 82 if ((Gdx.gl20 == null) && textureWidth != textureHeight) 83 throw new GdxRuntimeException("texture width and height must be square when using mipmapping."); 84 int width = pixmap.getWidth() / 2; 85 int height = pixmap.getHeight() / 2; 86 int level = 1; 87 Blending blending = Pixmap.getBlending(); 88 Pixmap.setBlending(Blending.None); 89 while (width > 0 && height > 0) { 90 Pixmap tmp = new Pixmap(width, height, pixmap.getFormat()); 91 tmp.drawPixmap(pixmap, 0, 0, pixmap.getWidth(), pixmap.getHeight(), 0, 0, width, height); 92 if (level > 1) pixmap.dispose(); 93 pixmap = tmp; 94 95 Gdx.gl.glTexImage2D(target, level, pixmap.getGLInternalFormat(), pixmap.getWidth(), pixmap.getHeight(), 0, 96 pixmap.getGLFormat(), pixmap.getGLType(), pixmap.getPixels()); 97 98 width = pixmap.getWidth() / 2; 99 height = pixmap.getHeight() / 2; 100 level++; 101 } 102 Pixmap.setBlending(blending); 103 } 104 } 105