Hi!
I have this code that create a Cubemap from six viewports.The rootNode scene has attached to the viewports, but only the top viewport shows the scene:
The code:
[java]public class HelloReflection extends SimpleApplication{
DirectionalLight sun;
Material mirror_mat;
public static void main(String[] args)
{
HelloReflection app = new HelloReflection();
app.start();
}
public void simpleInitApp()
{
initCamera();
rootNode.attachChild(initSkyBox());
initLight();
initMaterial();
initGeometry();
}
protected void initCamera()
{
flyCam.setMoveSpeed(50);
}
protected Spatial initSkyBox()
{
return SkyFactory.createSky(
assetManager,
"Textures/Sky/Bright/BrightSky.dds",
false);
}
protected void initLight()
{
sun = new DirectionalLight();
sun.setDirection(new Vector3f(1,0,-2).normalizeLocal());
sun.setColor(ColorRGBA.White);
rootNode.addLight(sun);
}
protected void initMaterial()
{
mirror_mat = new Material(assetManager,
"Shaders/Reflection/Reflection.j3md");
Camera top_cam = new Camera(512,512);
top_cam.setFrustumPerspective(45f, 1f, 1f, 1000f);
top_cam.setLocation(new Vector3f(0f, 5f, 0f));
top_cam.lookAtDirection(new Vector3f(0,1,0), Vector3f.UNIT_Y);
Camera bottom_cam = new Camera(512,512);
top_cam.setFrustumPerspective(45f, 1f, 1f, 1000f);
top_cam.setLocation(new Vector3f(0f, -5f, 0f));
top_cam.lookAtDirection(new Vector3f(0,-1,0), Vector3f.UNIT_Y);
Camera front_cam = new Camera(512,512);
top_cam.setFrustumPerspective(45f, 1f, 1f, 1000f);
top_cam.setLocation(new Vector3f(0f, 0f, -5f));
top_cam.lookAtDirection(new Vector3f(0,0,-1), Vector3f.UNIT_Y);
Camera back_cam = new Camera(512,512);
top_cam.setFrustumPerspective(45f, 1f, 1f, 1000f);
top_cam.setLocation(new Vector3f(0f, 0f, 5f));
top_cam.lookAtDirection(new Vector3f(0,0, 1), Vector3f.UNIT_Y);
Camera left_cam = new Camera(512,512);
top_cam.setFrustumPerspective(45f, 1f, 1f, 1000f);
top_cam.setLocation(new Vector3f(-5f, 0f, 0f));
top_cam.lookAtDirection(new Vector3f(-1,0,0), Vector3f.UNIT_Y);
Camera right_cam = new Camera(512,512);
top_cam.setFrustumPerspective(45f, 1f, 1f, 1000f);
top_cam.setLocation(new Vector3f(5f, 0f, 0f));
top_cam.lookAtDirection(new Vector3f(1,0,0), Vector3f.UNIT_Y);
ViewPort top_view = renderManager.createPreView("Top View", top_cam);
top_view.setClearFlags(true, true, true);
top_view.setBackgroundColor(ColorRGBA.Blue);
ViewPort bottom_view = renderManager.createPreView("Bottom View", bottom_cam);
bottom_view.setClearFlags(true, true, true);
bottom_view.setBackgroundColor(ColorRGBA.Red);
ViewPort front_view = renderManager.createPreView("Front View", front_cam);
front_view.setClearFlags(true, true, true);
front_view.setBackgroundColor(ColorRGBA.Green);
ViewPort back_view = renderManager.createPreView("Back View", back_cam);
back_view.setClearFlags(true, true, true);
back_view.setBackgroundColor(ColorRGBA.Pink);
ViewPort left_view = renderManager.createPreView("Left View", left_cam);
left_view.setClearFlags(true, true, true);
left_view.setBackgroundColor(ColorRGBA.Yellow);
ViewPort right_view = renderManager.createPreView("Right View", right_cam);
right_view.setClearFlags(true, true, true);
right_view.setBackgroundColor(ColorRGBA.Gray);
TextureCubeMap cube_Tex = new TextureCubeMap(512, 512, Format.RGBA8);
cube_Tex.setMinFilter(Texture.MinFilter.Trilinear);
cube_Tex.setMagFilter(Texture.MagFilter.Bilinear);
FrameBuffer top_Buffer = new FrameBuffer(512, 512, 1);
top_Buffer.setDepthBuffer(Format.Depth);
top_view.setOutputFrameBuffer(top_Buffer);
FrameBuffer bottom_Buffer = new FrameBuffer(512, 512, 1);
bottom_Buffer.setDepthBuffer(Format.Depth);
bottom_view.setOutputFrameBuffer(bottom_Buffer);
FrameBuffer front_Buffer = new FrameBuffer(512, 512, 1);
front_Buffer.setDepthBuffer(Format.Depth);
front_view.setOutputFrameBuffer(front_Buffer);
FrameBuffer back_Buffer = new FrameBuffer(512, 512, 1);
back_Buffer.setDepthBuffer(Format.Depth);
back_view.setOutputFrameBuffer(back_Buffer);
FrameBuffer left_Buffer = new FrameBuffer(512, 512, 1);
[/java] left_Buffer.setDepthBuffer(Format.Depth);
left_view.setOutputFrameBuffer(left_Buffer);
FrameBuffer right_Buffer = new FrameBuffer(512, 512, 1);
right_Buffer.setDepthBuffer(Format.Depth);
right_view.setOutputFrameBuffer(right_Buffer);
top_Buffer.addColorTexture(cube_Tex, TextureCubeMap.Face.NegativeX);
bottom_Buffer.addColorTexture(cube_Tex, TextureCubeMap.Face.PositiveX);
front_Buffer.addColorTexture(cube_Tex, TextureCubeMap.Face.NegativeY);
back_Buffer.addColorTexture(cube_Tex, TextureCubeMap.Face.PositiveY);
left_Buffer.addColorTexture(cube_Tex, TextureCubeMap.Face.NegativeZ);
right_Buffer.addColorTexture(cube_Tex, TextureCubeMap.Face.PositiveZ);
top_view.attachScene(rootNode);
bottom_view.attachScene(rootNode);
front_view.attachScene(rootNode);
back_view.attachScene(rootNode);
left_view.attachScene(rootNode);
right_view.attachScene(rootNode);
mirror_mat.setTexture("Texture", cube_Tex);
}
protected void initGeometry()
{
Box box2 = new Box(Vector3f.ZERO,
1,1,1);
Geometry cube = new Geometry("Box",box2);
Material mat_stl = new Material(assetManager,"Common/MatDefs/Misc/Unshaded.j3md");
Texture tex_ml = assetManager.loadTexture("Interface/Logo/Monkey.jpg");
mat_stl.setTexture("ColorMap", tex_ml);
cube.setMaterial(mat_stl);
cube.move(1, 7, 1);
rootNode.attachChild(cube);
Box box1 = new Box(Vector3f.ZERO,
1,1,1);
Geometry blue = new Geometry("Box",box1);
blue.setMaterial(mirror_mat);
blue.move(0,0,0);
blue.scale(5);
rootNode.attachChild(blue);
}
}