Image does not display properly

Started by kleandros, August 21, 2005, 06:28:14

Previous topic - Next topic

kleandros

Hello,

i' m using the code snippet below to load 32bps images and display them with lwjgl.

public class TextureLoader2 {
    
    HashMap spriteCache = new HashMap();
    
    IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
    
    public TextureLoader2() {
        try {
            IL.create();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
    
    public Texture loadTexture(String path) {
        Texture tx = (Texture) spriteCache.get(path);
        if (tx != null) {
            return tx;
        }
        
        IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
        IL.ilGenImages(image);
        IL.ilBindImage(image.get(0));
        IL.ilLoadImage(path);
        IL.ilConvertImage(IL.IL_RGBA, IL.IL_BYTE);
        ByteBuffer scratch = ByteBuffer.allocateDirect(
            IL.ilGetInteger(IL.IL_IMAGE_WIDTH) * IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 4);
        IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH)
            , IL.ilGetInteger(IL.IL_IMAGE_HEIGHT)
            , 1, IL.IL_RGBA, IL.IL_BYTE, scratch);
        
        // Create A IntBuffer For Image Address In Memory
        GL11.glGenTextures(buf); // Create Texture In OpenGL

        GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
        // Typical Texture Generation Using Data From The Image

        // Linear Filtering
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
        GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
        
        // Generate The Texture
        GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGBA
            , IL.ilGetInteger(IL.IL_IMAGE_WIDTH)
            , IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 0
            , GL11.GL_RGBA, GL11.GL_UNSIGNED_BYTE, scratch);

        Texture texture = new Texture(GL11.GL_TEXTURE_2D, buf.get(0));
        texture.setWidth(IL.ilGetInteger(IL.IL_IMAGE_WIDTH));
        texture.setHeight(IL.ilGetInteger(IL.IL_IMAGE_HEIGHT));
        
        int texWidth = 2;
        int texHeight = 2;
        
        // find the closest power of 2 for the width and height
        // of the produced texture
        while (texWidth < IL.ilGetInteger(IL.IL_IMAGE_WIDTH)) {
            texWidth *= 2;
        }
        while (texHeight < IL.ilGetInteger(IL.IL_IMAGE_HEIGHT)) {
            texHeight *= 2;
        }
        
        texture.setTextureHeight(texHeight);
        texture.setTextureWidth(texWidth);
        
        spriteCache.put(path, texture);
        
        return texture;
    }
    
}


when i display the image (in display mode 1024 x 768 x 32) many of the color are displayed incorrectly; any ideas why?

Thanks kleandros.

markos

I also have the same problem!:

i use the following code, but cannot load the required texture. Any ideas?

   private void loadTextures() {
       int texture = loadTexture("Data/NeHe.bmp");
   }

private int loadTexture(String path) {
       IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
       IL.ilGenImages(image);
       IL.ilBindImage(image.get(0));
       IL.ilLoadImage(path);
       IL.ilConvertImage(IL.IL_RGB, IL.IL_BYTE);
       ByteBuffer scratch = ByteBuffer.allocateDirect(IL.ilGetInteger(IL.IL_IMAGE_WIDTH) * IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 3);
       IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH), IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 1, IL.IL_RGB, IL.IL_BYTE, scratch);
       
       // Create A IntBuffer For Image Address In Memory
       IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
       GL11.glGenTextures(buf); // Create Texture In OpenGL

       GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
       // Typical Texture Generation Using Data From The Image

       // Linear Filtering
       GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
       // Linear Filtering
       GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
       // Generate The Texture
       GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, GL11.GL_RGB, IL.ilGetInteger(IL.IL_IMAGE_WIDTH),
               IL.ilGetInteger(IL.IL_IMAGE_HEIGHT), 0, GL11.GL_RGB, GL11.GL_UNSIGNED_BYTE, scratch);

       return buf.get(0); // Return Image Address In Memory
   }

kleandros

the code below works for me (32bits)

/*
* TextureLoader2.java
*
* Created on August 20, 2005, 4:31 PM
*/

package rts.client.texture;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.IntBuffer;

import org.lwjgl.opengl.Display;
import org.lwjgl.opengl.DisplayMode;
import org.lwjgl.opengl.GL11;
import org.lwjgl.opengl.glu.GLU;
import org.lwjgl.devil.IL;
import org.lwjgl.devil.ILU;
import org.lwjgl.input.Keyboard;

import java.util.HashMap;
/**
*
* @author  ks
*/
public class TextureLoader2 {
   
   HashMap spriteCache = new HashMap();
   
   IntBuffer buf = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
   
   public TextureLoader2() {
       try {
           IL.create();
           ILU.create();
       }
       catch (Exception e) {
           e.printStackTrace();
       }
   }
   
   public Texture loadTexture(String path) {
       Texture tx = (Texture) spriteCache.get(path);
       if (tx != null) {
           return tx;
       }
       
       IntBuffer image = ByteBuffer.allocateDirect(4).order(ByteOrder.nativeOrder()).asIntBuffer();
       IL.ilGenImages(image);
       IL.ilBindImage(image.get(0));
       IL.ilLoadImage("./res/"+path);//D:/__source/lwjgl-win32-0.97
       IL.ilConvertImage(IL.ilGetInteger(IL.IL_IMAGE_FORMAT), IL.IL_BYTE);
       
       int image_width = IL.ilGetInteger(IL.IL_IMAGE_WIDTH);
       int image_height = IL.ilGetInteger(IL.IL_IMAGE_HEIGHT);
       
       convertToPowerOfTwoTexture();
       ByteBuffer scratch = IL.ilGetData();
       
       /*
       ByteBuffer scratch = ByteBuffer.allocateDirect(
           IL.ilGetInteger(IL.IL_IMAGE_WIDTH) * IL.ilGetInteger(IL.IL_IMAGE_HEIGHT) * 4 * 4);
       IL.ilCopyPixels(0, 0, 0, IL.ilGetInteger(IL.IL_IMAGE_WIDTH)
           , IL.ilGetInteger(IL.IL_IMAGE_HEIGHT)
           , 1, IL.ilGetInteger(IL.IL_IMAGE_FORMAT), IL.IL_BYTE, scratch);
        */
       
       // Create A IntBuffer For Image Address In Memory
       GL11.glGenTextures(buf); // Create Texture In OpenGL

       GL11.glBindTexture(GL11.GL_TEXTURE_2D, buf.get(0));
       // Typical Texture Generation Using Data From The Image

       // Linear Filtering
       //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_S, GL11.GL_REPEAT);
       //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_WRAP_T, GL11.GL_REPEAT);
       GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_LINEAR);
       GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_LINEAR);
       //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MAG_FILTER, GL11.GL_NEAREST);
       //GL11.glTexParameteri(GL11.GL_TEXTURE_2D, GL11.GL_TEXTURE_MIN_FILTER, GL11.GL_NEAREST);
       
       // Generate The Texture
       GL11.glTexImage2D(GL11.GL_TEXTURE_2D, 0, getSourcePixelFormat()
           , IL.ilGetInteger(IL.IL_IMAGE_WIDTH)
           , IL.ilGetInteger(IL.IL_IMAGE_HEIGHT)
           , 0
           , getSourcePixelFormat(), GL11.GL_UNSIGNED_BYTE, scratch);

       Texture texture = new Texture(GL11.GL_TEXTURE_2D, buf.get(0));
       texture.setHeight(image_height);
       texture.setWidth(image_width);
       texture.setTextureWidth(IL.ilGetInteger(IL.IL_IMAGE_WIDTH));
       texture.setTextureHeight(IL.ilGetInteger(IL.IL_IMAGE_HEIGHT));
       
       System.out.println("@@@IL "+path+" id="+texture.getTextureID()
       +" text:width="+texture.getWidth()+":"
       +texture.getImageWidth()+":"+texture.getTextureWidth()
       +" text:height="+texture.getHeight()+":"
       +texture.getImageHeight()+":"+texture.getTextureHeight()
       );
       
       spriteCache.put(path, texture);
       
       return texture;
   }
   
   private void convertToPowerOfTwoTexture() {
       int oldWidth = IL.ilGetInteger(IL.IL_IMAGE_WIDTH);
       int oldHeight = IL.ilGetInteger(IL.IL_IMAGE_HEIGHT);
       int width = getNextPowerOfTwo(oldWidth);
       int height = getNextPowerOfTwo(oldHeight);
       
       /*
       if (width < height) {
           width = height;
       }
       else {
           height = width;
       }
        */
       
       if(width != oldWidth || height != oldHeight) {
           ILU.iluEnlargeCanvas(width, height, 1);
       }
   }
   
   /**
    * finds the next highest number with a power of two.
    */
   private int getNextPowerOfTwo(int number) {
       int newNumber = 2;
       while (newNumber < number) {
           newNumber *= 2;
       }
       return newNumber;
   }
   
   private int getSourcePixelFormat() {
       int sourcePixelFormat = -1;
       if(IL.ilGetInteger(IL.IL_IMAGE_FORMAT) == IL.IL_RGBA)
           sourcePixelFormat = GL11.GL_RGBA;
       else if(IL.ilGetInteger(IL.IL_IMAGE_FORMAT) == IL.IL_RGB)
           sourcePixelFormat = GL11.GL_RGB;
       else if(IL.ilGetInteger(IL.IL_IMAGE_FORMAT) == IL.IL_LUMINANCE)
           sourcePixelFormat = GL11.GL_LUMINANCE_ALPHA;
       else
           System.out.println("unsupported (by this loader) source pixel format!");
       return sourcePixelFormat;
   }
   
}