159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta/* 259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * To change this template, choose Tools | Templates 359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * and open the template in the editor. 459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapackage jme3test.texture; 659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.app.SimpleApplication; 859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.bounding.BoundingBox; 959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.light.PointLight; 1059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.material.Material; 1159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.ColorRGBA; 1259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.math.Vector3f; 1359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.Geometry; 1459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.VertexBuffer; 1559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.VertexBuffer.Type; 1659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.VertexBuffer.Usage; 1759b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.scene.shape.Sphere; 1859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.texture.Image; 1959b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.texture.Image.Format; 2059b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.texture.Texture; 2159b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.texture.Texture3D; 2259b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport com.jme3.util.BufferUtils; 2359b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.io.IOException; 2459b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.ByteBuffer; 2559b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.nio.FloatBuffer; 2659b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartaimport java.util.ArrayList; 2759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 2859b2e6871c65f58fdad78cd7229c292f6a177578Scott Bartapublic class TestTexture3D extends SimpleApplication { 2959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public static void main(String[] args) { 3159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta TestTexture3D app = new TestTexture3D(); 3259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta app.start(); 3359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 3459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 3559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta @Override 3659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta public void simpleInitApp() { 3759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //mouseInput.setCursorVisible(true); 3859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta flyCam.setMoveSpeed(10); 3959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //creating a sphere 4059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Sphere sphere = new Sphere(32, 32, 1); 4159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //getting the boundingbox 4259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta sphere.updateBound(); 4359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta BoundingBox bb = (BoundingBox) sphere.getBound(); 4459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Vector3f min = bb.getMin(null); 4559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float[] ext = new float[]{bb.getXExtent() * 2, bb.getYExtent() * 2, bb.getZExtent() * 2}; 4659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //we need to change the UV coordinates (the sphere is assumet to be inside the 3D image box) 4759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta sphere.clearBuffer(Type.TexCoord); 4859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta VertexBuffer vb = sphere.getBuffer(Type.Position); 4959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta FloatBuffer fb = (FloatBuffer) vb.getData(); 5059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta float[] uvCoordinates = BufferUtils.getFloatArray(fb); 5159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //now transform the coordinates so that they are in the range of <0; 1> 5259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int i = 0; i < uvCoordinates.length; i += 3) { 5359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta uvCoordinates[i] = (uvCoordinates[i] - min.x) / ext[0]; 5459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta uvCoordinates[i + 1] = (uvCoordinates[i + 1] - min.y) / ext[1]; 5559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta uvCoordinates[i + 2] = (uvCoordinates[i + 2] - min.z) / ext[2]; 5659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 5759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //apply new texture coordinates 5859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta VertexBuffer uvCoordsBuffer = new VertexBuffer(Type.TexCoord); 5959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta uvCoordsBuffer.setupData(Usage.Static, 3, com.jme3.scene.VertexBuffer.Format.Float, 6059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta BufferUtils.createFloatBuffer(uvCoordinates)); 6159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta sphere.setBuffer(uvCoordsBuffer); 6259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //create geometry, and apply material and our 3D texture 6359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Geometry g = new Geometry("sphere", sphere); 6459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Material material = new Material(assetManager, "jme3test/texture/tex3D.j3md"); 6559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta try { 6659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta Texture texture = this.getTexture(); 6759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta material.setTexture("Texture", texture); 6859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } catch (IOException e) { 6959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta e.printStackTrace(); 7059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 7159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta g.setMaterial(material); 7259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.attachChild(g); 7359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta //add some light so that it is visible 7459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta PointLight light = new PointLight(); 7559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta light.setColor(ColorRGBA.White); 7659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta light.setPosition(new Vector3f(5, 5, 5)); 7759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta light.setRadius(20); 7859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.addLight(light); 7959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta light = new PointLight(); 8059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta light.setColor(ColorRGBA.White); 8159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta light.setPosition(new Vector3f(-5, -5, -5)); 8259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta light.setRadius(20); 8359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta rootNode.addLight(light); 8459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 8559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta 8659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta /** 8759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta * This method creates a RGB8 texture with the sizes of 10x10x10 pixels. 8859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta */ 8959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta private Texture getTexture() throws IOException { 9059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ArrayList<ByteBuffer> data = new ArrayList<ByteBuffer>(1); 9159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta ByteBuffer bb = BufferUtils.createByteBuffer(10 * 10 * 10 * 3);//all data must be inside one buffer 9259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int i = 0; i < 10; ++i) { 9359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta for (int j = 0; j < 10 * 10; ++j) { 9459b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta bb.put((byte) (255f*i/10f)); 9559b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta bb.put((byte) (255f*i/10f)); 9659b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta bb.put((byte) (255f)); 9759b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9859b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 9959b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta bb.rewind(); 10059b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta data.add(bb); 10159b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta return new Texture3D(new Image(Format.RGB8, 10, 10, 10, data)); 10259b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta } 10359b2e6871c65f58fdad78cd7229c292f6a177578Scott Barta}