So i have been playing around a bit with cube cams. View ports and Render to texture stuff.
It mostly behaves the way i expect. Except if i access the cubemap on the first frame i get lots of artifacts. Like the skybox is lazy loaded or something. If i wait a frame everything is fine. However this feels very ugly.
Example code of the AppState:
protected void initialize(Application arg0) {
cubeMap = new TextureCubeMap(faceSize, faceSize, format);
cubeCamera = new Camera(faceSize, faceSize);
cubeCamera.setFrustumPerspective(90f, 1, 1, 100);
cubePort = new ViewPort("cubeCam", cubeCamera);
cubePort.setClearFlags(true, true, true);
cubePort.attachScene(scene);
cubeBuffer = new FrameBuffer(faceSize, faceSize, 1);
cubeBuffer.setDepthBuffer(Format.Depth);
cubePort.setOutputFrameBuffer(cubeBuffer);
}
public void render(RenderManager rm) {
for (int side = 0; side < 6; side++) {
cubeCamera.setAxes(axisX[side], axisY[side], axisZ[side]);
cubeBuffer.resetObject();
cubeBuffer.setDepthBuffer(Format.Depth);
cubeBuffer.setColorTexture(cubeMap, TextureCubeMap.Face.values()[side]);
cubePort.setOutputFrameBuffer(cubeBuffer);
rm.renderViewPort(cubePort, 0.16f);
}
}
In the simple App class
@Override
public void simpleInitApp() {
Box testBox = new Box(1, 1, 1);
Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
Geometry geo = new Geometry("box", testBox);
geo.setMaterial(mat);
mat.setColor("Color", ColorRGBA.Red);
Node node = new Node();
node.attachChild(geo);
boxNode=node;
rootNode.attachChild(node);
Geometry pbrBox = geo.clone(false);
Material pbrMat = assetManager.loadMaterial("Models/Tank/tank.j3m");
pbrBox.setMaterial(pbrMat);
Node pbrNode = new Node();
pbrNode.setLocalTranslation(new Vector3f(1, 1, 1));
pbrNode.attachChild(pbrBox);
rootNode.attachChild(pbrNode);
DirectionalLight dl = new DirectionalLight();
dl.setDirection(new Vector3f(-1, -1, -1));
rootNode.addLight(dl);
sky = SkyFactory.createSky(assetManager, "milky.jpg", SkyFactory.EnvMapType.EquirectMap);
rootNode.attachChild(sky);
ChaseCamera chaser = new ChaseCamera(cam, node, inputManager);
chaser.setDragToRotate(true);
chaser.setMinVerticalRotation(-FastMath.HALF_PI);
chaser.setMaxDistance(100);
chaser.setMinDistance(0.1f);
chaser.setSmoothMotion(true);
chaser.setRotationSensitivity(80);
chaser.setZoomSensitivity(5);
flyCam.setEnabled(false);
camTester=new OctEnvCamera(rootNode,new Vector3f(5,0,0),256);
stateManager.attach(camTester);
}
float rot=0;
@Override
public void simpleUpdate(float tpf) {
frame++;
rot+=tpf;
//System.out.println("Update frame " + frame);
if ( frame == 2) {
sky.removeFromParent();
TextureCubeMap cube=camTester.getCubeMap();
sky=SkyFactory.createSky(assetManager, cube, SkyFactory.EnvMapType.CubeMap);
rootNode.attachChild(sky);
System.out.println("RemoveOldSky");
}
boxNode.setLocalRotation(new Quaternion(new float[] {0,rot,0}));
}
if i use frame==1 i get the following:
I feel like i may be missing something. Since i am trying to do a few fancy things with viewports and stuff, i want to make sure i really understand what is going on.