Rescaling textures

Hello!



I have noticed that jME rescales images that are not of the right format.

In which class does this rescaling take place?

Is it possible to set a maximum size of a texture (f.ex. 1024,768) so that it gets rescaled if it is above that size?

If I need to do this myself, where do I start looking?

The rescaling happens in LWJGLTextureState.apply (lines 306ff). It is done only if the graphics driver does not support non-power of two textures and the texture size is not power of 2. The scaling applied there is LWJGL specific (not recommended to rescale that way in the application itself).

The best way to go is to rescale the textures before loading them (in your favorite imaging tool).  If you still want to load an image and rescale it in memory I'd suggest to load and scale it via awt (see java.awt.Image, Graphics, BufferedImage etc.). Afterwards make a texture from the loaded image via TextureManager.loadTexture(Image,int,int,boolean).

Great, I'll do it that way then. Thanks, it really helps to know where to begin.

Wow, that worked well. If anyone else needs to do this here's the code:



public Image createImage(URL url){

Thanks for that  - just whipped up a quick mod - uses the image icon to get the image initially




import java.awt.Frame;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;

public class ImageRescale {
   
   
   public static Image createImage(Image image, int width, int height){
        Image returnImage = null;
        returnImage = new BufferedImage(width,height, BufferedImage.TYPE_INT_RGB);
        Graphics2D graphics2D = ((BufferedImage)returnImage).createGraphics();
        graphics2D.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
        RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        graphics2D.drawImage(image, 0, 0, width, height, null);       
        return returnImage;
    }
   
   public static void main(String [] args) {

        Image a = new ImageIcon("weirdMetal.png")).getImage();
        Image scaled = ImageRescale.createImage(a, 800, 400);
       
        Frame f = new Frame();
        f.setSize(800, 800);
        JPanel panel = new JPanel();
        JButton bBefore = new JButton(new ImageIcon(a, "Before"));
        bBefore.setToolTipText("Before");
        JButton bAfter = new JButton(new ImageIcon(scaled, "After"));
        bAfter.setToolTipText("After");
        f.add(panel);
        panel.add(bBefore);
        panel.add(bAfter);
        f.setVisible(true);       
   }
}