TextureManager with ByteBuffer

Hi people.



I have I little problem with TextureManager.



TextureManager only works with Images files, not with ByteBuffer.



I need to use TextureManager or something similar to create a texture from a ByteBuffer, because I have my textures on an special package that I read with an special tool (decrypt/unpack/etc…)



I'm need to call a function like this.



protected int createTexture(int width, int height, int type, ByteBuffer byteTexture){
...
}



type is a lwjgl GL11 format like GL11.GL_RGB or GL11.GL_RGBA and byteTexture have the bytes at memory.

My first atent to do this is trying to create the texture "manually", but it doesn't work because I cannot set the file for the image, only the ByteBuffer.

I need to modify the TextureManager or create a TextureMemoryManager for this.

This is my first atempt:


protected int createTexture(int width, int height, int type, ByteBuffer byteTexture) {
      int salida=idTextura;
      Format formato=Format.Guess;
      boolean textureformatknown=true;
      switch(type){
      //Formatos RGB
      case GL11.GL_RGB:
      case GL11.GL_RGB8:   
         formato=Format.RGB8;
         break;
      case GL11.GL_RGB4:
         formato=Format.RGB4;
         break;
      case GL11.GL_RGB5:
         formato=Format.RGB5;
         break;
      case GL11.GL_RGB5_A1:
         formato=Format.RGB5A1;
         break;
      case GL11.GL_RGB10:
         formato=Format.RGB10;
         break;   
      case GL11.GL_RGB10_A2:
         formato=Format.RGB10A2;
         break;   
      case GL11.GL_RGB12:
         formato=Format.RGB12;
         break;   
      case GL11.GL_RGB16:
         formato=Format.RGB16;
         break;
         
      //Formatos RGBA   
      case GL11.GL_RGBA:
      case GL11.GL_RGBA8:
         formato=Format.RGBA8;
         break;
      case GL11.GL_RGBA2:
         formato=Format.RGBA2;
         break;
      case GL11.GL_RGBA4:
         formato=Format.RGBA4;
         break;
      case GL11.GL_RGBA12:
         formato=Format.RGBA12;
         break;
      case GL11.GL_RGBA16:
         formato=Format.RGBA16;
         break;   
      case GL11.GL_RGBA_MODE:
         //Mal royo
         System.err.println("Texture Format RGBA_MODE not supported");
         textureformatknown=false;
         break;
         
      //Formatos comprimidos
      case EXTTextureCompressionS3TC.GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
         formato=Format.RGB_TO_DXT1;
         break;
      case EXTTextureCompressionS3TC.GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
         formato=Format.RGBA_TO_DXT1;
         break;
      case EXTTextureCompressionS3TC.GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
         formato=Format.RGBA_TO_DXT3;
         break;   
      case EXTTextureCompressionS3TC.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
         formato=Format.RGBA_TO_DXT5;
         break;
      default:
         System.err.println("Unknown Texture format: "+type);
         textureformatknown=false;
      }
      
      if(textureformatknown){
         Image imagen= new Image(formato,width,height,byteTexture);
         Texture tex = new Texture2D();
         tex.setMinificationFilter(MinificationFilter.NearestNeighborNoMipMaps);
         tex.setMagnificationFilter(MagnificationFilter.NearestNeighbor);
         tex.setImage(imagen);
         TextureState ts=game.getDisplay().getRenderer().createTextureState();
         ts.setTexture(tex);
         System.err.println(tex.getTextureId());
         
      }
      return salida;
   }

Havent got the code with me right now - can you extend texture manager or write this as a facade around texturemanager

Something like this?



http://code.google.com/p/vlengine/source/browse/trunk/vle_cleanup/src/com/vlengine/resource/TextureReader.java



Its not exactly jME, but can help if you want to code it for yourself.

thanks vear, but this is not what I'm searching for.



I will try to to create my own TextManager. After I finish, I will share the code.

no extensions or facades needed. I do it like this:



// byte[] arr = yourByteArray
Image img = TextureManager.loadImage(extensions.get(idx++), new ByteArrayInputStream(arr), true);
TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
Texture t = new Texture();
t.setFilter(Texture.MM_LINEAR_LINEAR);
t.setImage(img);
t.setMipmapState(Texture.FM_LINEAR);
ts.setTexture(t);



This is 1.0 code but it should be easy to port to 2.0

so long,
Andy

After looking DDSLoader.java I found a method to create a Texture from my Byte buffer.


protected int createTexture(int width, int height, int type, ByteBuffer byteTexture) {
      int salida=idTextura;
      Format formato=Format.Guess;
      boolean textureformatknown=true;
      
      switch(type){
      //Formatos RGB
      case GL11.GL_RGB:
      case GL11.GL_RGB8:   
         formato=Format.RGB8;
         break;
      case GL11.GL_RGB4:
         formato=Format.RGB4;
         break;
      case GL11.GL_RGB5:
         formato=Format.RGB5;
         break;
      case GL11.GL_RGB5_A1:
         formato=Format.RGB5A1;
         break;
      case GL11.GL_RGB10:
         formato=Format.RGB10;
         break;   
      case GL11.GL_RGB10_A2:
         formato=Format.RGB10A2;
         break;   
      case GL11.GL_RGB12:
         formato=Format.RGB12;
         break;   
      case GL11.GL_RGB16:
         formato=Format.RGB16;
         break;
         
      //Formatos RGBA   
      case GL11.GL_RGBA:
      case GL11.GL_RGBA8:
         formato=Format.RGBA8;
         break;
      case GL11.GL_RGBA2:
         formato=Format.RGBA2;
         break;
      case GL11.GL_RGBA4:
         formato=Format.RGBA4;
         break;
      case GL11.GL_RGBA12:
         formato=Format.RGBA12;
         break;
      case GL11.GL_RGBA16:
         formato=Format.RGBA16;
         break;   
      case GL11.GL_RGBA_MODE:
         //Mal royo
         System.err.println("Texture Format RGBA_MODE not supported");
         textureformatknown=false;
         break;
         
      //Formatos comprimidos
      case EXTTextureCompressionS3TC.GL_COMPRESSED_RGB_S3TC_DXT1_EXT:
         formato=Format.RGB_TO_DXT1;
         break;
      case EXTTextureCompressionS3TC.GL_COMPRESSED_RGBA_S3TC_DXT1_EXT:
         formato=Format.RGBA_TO_DXT1;
         break;
      case EXTTextureCompressionS3TC.GL_COMPRESSED_RGBA_S3TC_DXT3_EXT:
         formato=Format.RGBA_TO_DXT3;
         break;   
      case EXTTextureCompressionS3TC.GL_COMPRESSED_RGBA_S3TC_DXT5_EXT:
         formato=Format.RGBA_TO_DXT5;
         break;
      default:
         System.err.println("Unknown Texture format: "+type);
         textureformatknown=false;
      }
      
      if(textureformatknown){
         
         Image img;
         img = new Image(formato,width,height,byteTexture);
         TextureState ts = DisplaySystem.getDisplaySystem().getRenderer().createTextureState();
         Texture tex = new Texture2D();
         tex.setMinificationFilter(MinificationFilter.NearestNeighborNoMipMaps);
         tex.setMagnificationFilter(MagnificationFilter.NearestNeighbor);
         tex.setImage(img);
         ts.setTexture(tex);
         
         System.err.println(tex.getTextureId());
         
      }
      return salida;
   }



Unfortunally I need to return the TextureId and this TextureID is alwais 0, I think this is because the texture is not loaded (variable id is transient)

I will try to investigate I little more.