2013-02-24 5 views
4

텍스처를 LibGDX의 Pixmap으로 변환하려고하는데 일부 실험에서 ByteBuffer로 픽셀 데이터를 모두 가져올 수 있고 내가 말할 수있는 것부터이 작업을 수행 할 수 있어야합니다. 다음을 수행하는 것은 :LibGDX 텍스처 by ByteBuffer

Pixmap pixmap = textureName.getTexture().getTextureData().consumePixmap(); 
ByteBuffer data = pixmap.getPixels(); 

이것은 제대로 픽스맵을 크기와의 ByteBuffer는 잘 만든 것 같다 않는 반환하는 것 않지만, 빈, 투명한 이미지의 결과로, 제로로 완전히 채워집니다. 이 점과 몇 가지 다른 테스트를 통해 Pixmap 자체가 완전히 투명하게 보이는 이미지로 생성된다는 것을 알게되었지만 그 원인을 찾을 수는 없습니다. 이 작업을 방해하는 일종의 제한이 있습니까? 아니면 단순히 확실하지 않은 것이 있습니까?

답변

7

API (consumePixmap())는 Texture 클래스가 Pixmap을 GPU로 푸시 할 때 Pixmap에서 데이터를 추출하는 것을 의미합니다.
Libgdx Texture 개체는 GPU의 텍스처 데이터를 나타내므로 기본 데이터를 CPU로 가져 오는 것은 일반적으로 그리 좋지 않습니다 (렌더링 API의 "잘못된"방향). http://code.google.com/p/libgdx/wiki/GraphicsPixmap

또한 consumePixmap()의 설명서에는 작동하기 전에 requires a prepare() call이 나와 있습니다.

텍스처에서 픽셀 정보를 추출하려면 FrameBuffer 객체를 만들고 텍스처를 렌더 한 다음 픽셀을 추출합니다 (ScreenUtils 참조).

다른 방향 (바이트 배열 ->Pixmap -> ->Texture)으로 이동 한 다음 변환을 다시 수행하면 효과가있을 수 있습니다.

+0

텍스쳐는 간단한 페인트와 같은 인터페이스로 응용 프로그램 내에서 사용자가 생성하므로 사용자가 직접 데이터를 가지고 있지 않습니다. 그러나 완벽한 질감을 생성하는 데 사용하는 FrameBuffer에서 정보를 추출 할 수 있다면 화면에 그려지는 기본 FrameBuffer에서만이 작업을 수행 할 수 있다는 인상하에있었습니다. 제 경우에는 텍스처가 화면의 해상도를 초과 할 가능성이 있으므로이 경로를 따라 이미지가 잘 리거나 왜곡됩니다. 안타깝게도 Pixmap 자체를 드로잉에 사용하는 것도 옵션이 아닙니다. – Shamrock

7

수준 편집기에서 만든 PNG 파일을 만들 때 비슷한 문제가 발생했습니다. 레벨은 제작자가 배치 한 타일로 만들어지며 전체 레벨은 게임에 사용되는 .png 파일로 저장됩니다.

해결 방법 P.T. 설명합니다. 같은 문제에 부딪 치는 다른 사람들에게 유용하기를 바랍니다.

응용 프로그램 구성에서 : cfg.useGL20 = true; GL20은

 public boolean exportLevel(){ 

      //width and height in pixels 
      int width = (int)lba.getPrefWidth(); 
      int height = (int)lba.getPrefHeight(); 

      //Create a SpriteBatch to handle the drawing. 
      SpriteBatch sb = new SpriteBatch(); 

      //Set the projection matrix for the SpriteBatch. 
      Matrix4 projectionMatrix = new Matrix4(); 

      //because Pixmap has its origin on the topleft and everything else in LibGDX has the origin left bottom 
      //we flip the projection matrix on y and move it -height. So it will end up side up in the .png 
      projectionMatrix.setToOrtho2D(0, -height, width, height).scale(1,-1,1); 

      //Set the projection matrix on the SpriteBatch 
      sb.setProjectionMatrix(projectionMatrix); 

      //Create a frame buffer. 
      FrameBuffer fb = new FrameBuffer(Pixmap.Format.RGBA8888, width, height, false); 

      //Call begin(). So all next drawing will go to the new FrameBuffer. 
      fb.begin(); 

      //Set up the SpriteBatch for drawing. 
      sb.begin(); 

      //Draw all the tiles. 
      BuildTileActor[][] btada = lba.getTiles(); 
      for(BuildTileActor[] btaa: btada){ 
       for(BuildTileActor bta: btaa){ 
        bta.drawTileOnly(sb); 
       } 
      } 

      //End drawing on the SpriteBatch. This will flush() any sprites remaining to be drawn as well. 
      sb.end(); 

      //Then retrieve the Pixmap from the buffer. 
      Pixmap pm = ScreenUtils.getFrameBufferPixmap(0, 0, width, height); 

      //Close the FrameBuffer. Rendering will resume to the normal buffer. 
      fb.end(); 

      //Save the pixmap as png to the disk. 
      FileHandle levelTexture = Gdx.files.local("levelTexture.png"); 
      PixmapIO.writePNG(levelTexture, pm); 

      //Dispose of the resources. 
      fb.dispose(); 
      sb.dispose(); 

주 프레임 버퍼를 구축 할 수 있도록 필요합니다 그래서이이 작업을 수행 할 수있는 산뜻한 방법이 될 수 없습니다 LibGDX에 새로운 오전.

+0

정확히 내가 찾던 것이 었습니다. 고마워요. – LukTar

관련 문제