Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ImageRaster confusion with 3d textures #1021

Closed
riccardobl opened this issue Feb 11, 2019 · 2 comments
Closed

ImageRaster confusion with 3d textures #1021

riccardobl opened this issue Feb 11, 2019 · 2 comments

Comments

@riccardobl
Copy link
Member

riccardobl commented Feb 11, 2019

I want to split a 256x16 grid into a 16x16x16 3d texture.
This is my code

Image img=new Image(Format.RGB8, 16, 16, 
	null,
	ColorSpace.Linear
);
img.addData(
	BufferUtils.createByteBuffer(16*16*16*
	(Format.RGB8.getBitsPerPixel()/8))
);
img.setDepth(16);
Texture3D tx=new Texture3D(img);
for(int i=0;i<16;i++){
	ImageRaster ir=ImageRaster.create(img,i);
	for(int x=0;x<16;x++){
		for(int y=0;y<16;y++){
                        [...]
			ir.setPixel(x,y,color);							
		}
	}
}

This is the exception i get when the ImageRaster is created on the second layer (it works fine for i=0)

java.lang.NullPointerException: null
        at com.jme3.texture.image.ByteOffsetImageCodec.writeComponents(ByteOffsetImageCodec.java:85) ~[bin/:?]
        at com.jme3.texture.image.DefaultImageRaster.setPixel(DefaultImageRaster.java:152) ~[bin/:?]
[...]

Following the exception

--->

        codec.writeComponents(getBuffer(), x, y, width, offset, components, temp);

--->

 private ByteBuffer getBuffer(){
        if(buffer == null){
            this.buffer = image.getData(slice);
        }
        return buffer;
    }

--> image.getData(slice);
From the javadoc

  • slice Which slice to use. Only applies to 3D images, 2D image
    * arrays or cubemaps.

ImageRaster assumes that different layers of the 3d texture are stored in different ByteBuffers, while TextureUtil assumes that the layers are stored interleaved on a single bytebuffer.

     if (target == GL2.GL_TEXTURE_3D) {
                gl2.glTexImage3D(target,
                                 level,
                                 format.internalFormat,
                                 width,
                                 height,
                                 depth,
                                 0,
                                 format.format,
                                 format.dataType,
                                 data);
    } 
@riccardobl
Copy link
Member Author

riccardobl commented Feb 11, 2019

Possible patch
com.jme3.texture.image.DefaultImageRaster

@@ -155,7 +155,14 @@
     
     private ByteBuffer getBuffer(){
         if(buffer == null){
-            this.buffer = image.getData(slice);
+		if(image.getDepth()>1){
+			int skip = image.getWidth() * image.getHeight() * codec.bpp * slice;
+			this.buffer = image.getData(0);
+			this.buffer.position(skip);
+			this.buffer = this.buffer.slice();
+		}else{
+            		this.buffer = image.getData(slice);
+		}
         }
         return buffer;
     }

I need some feedback on this since i don't know if it could break the engine elsewhere.

@riccardobl
Copy link
Member Author

Fixed in d695088

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant