1 package com.badlogic.gdx.tests.g3d; 2 3 import com.badlogic.gdx.Gdx; 4 import com.badlogic.gdx.graphics.Camera; 5 import com.badlogic.gdx.graphics.Cubemap; 6 import com.badlogic.gdx.graphics.GL20; 7 import com.badlogic.gdx.graphics.PerspectiveCamera; 8 import com.badlogic.gdx.graphics.Pixmap.Format; 9 import com.badlogic.gdx.graphics.g3d.Model; 10 import com.badlogic.gdx.graphics.g3d.ModelBatch; 11 import com.badlogic.gdx.graphics.g3d.ModelInstance; 12 import com.badlogic.gdx.graphics.g3d.attributes.CubemapAttribute; 13 import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; 14 import com.badlogic.gdx.graphics.g3d.loader.ObjLoader; 15 import com.badlogic.gdx.graphics.g3d.utils.TextureDescriptor; 16 import com.badlogic.gdx.graphics.glutils.FrameBufferCubemap; 17 import com.badlogic.gdx.math.Quaternion; 18 import com.badlogic.gdx.math.Vector3; 19 20 /** 21 * Render a basic scene in a FrameBufferCubemap and displays it 22 * in a rotating cube. 23 */ 24 public class FrameBufferCubemapTest extends Basic3DSceneTest { 25 protected PerspectiveCamera camFb; 26 protected PerspectiveCamera camCube; 27 protected FrameBufferCubemap fb; 28 protected Cubemap cubemap; 29 protected Model cubeMesh; 30 protected ModelInstance cubeInstance; 31 protected ModelBatch cubeBatch; 32 33 @Override create()34 public void create () { 35 super.create(); 36 37 camFb = new PerspectiveCamera(90, 800, 800); 38 camFb.position.set(10f, 10f, 10f); 39 camFb.lookAt(0, 0, 0); 40 camFb.near = 0.1f; 41 camFb.far = 1000f; 42 camFb.update(); 43 44 fb = new FrameBufferCubemap(Format.RGBA8888, 800, 800, true); 45 46 ObjLoader objLoader = new ObjLoader(); 47 cubeMesh = objLoader.loadModel(Gdx.files.internal("data/cube.obj")); 48 cubeInstance = new ModelInstance(cubeMesh); 49 50 cubeBatch = new ModelBatch(Gdx.files.internal("data/shaders/cubemap-vert.glsl"), 51 Gdx.files.internal("data/shaders/cubemap-frag.glsl")); 52 53 cubeMesh.materials.get(0).set(new CubemapAttribute(CubemapAttribute.EnvironmentMap, cubemap)); 54 55 camCube = new PerspectiveCamera(67, Gdx.graphics.getWidth()*0.5f, Gdx.graphics.getHeight()*0.5f); 56 camCube.position.set(0f, 2f, 2f); 57 camCube.lookAt(0, 0, 0); 58 camCube.near = 1f; 59 camCube.far = 300f; 60 camCube.update(); 61 } 62 63 @Override render()64 public void render () { 65 renderScene(); 66 renderCube(); 67 } 68 renderScene()69 public void renderScene() { 70 Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight()); 71 Gdx.gl.glDisable(GL20.GL_SCISSOR_TEST); 72 73 // Render scene to screen 74 super.render(); 75 76 // Render scene to cubemap 77 camFb.position.set(cam.position); 78 camFb.near = cam.near; 79 camFb.far = cam.far; 80 fb.begin(); 81 while( fb.nextSide() ) { 82 fb.getSide().getUp(camFb.up); 83 fb.getSide().getDirection(camFb.direction); 84 camFb.update(); 85 86 Gdx.gl.glClearColor(1, 1, 1, 1); 87 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 88 89 modelBatch.begin(camFb); 90 for (ModelInstance instance : instances) 91 modelBatch.render(instance, lights); 92 if (space != null) modelBatch.render(space); 93 modelBatch.end(); 94 } 95 fb.end(); 96 cubemap = fb.getColorBufferTexture(); 97 } 98 99 float yaw, pitch, roll; renderCube()100 public void renderCube() { 101 int w = Gdx.graphics.getWidth(); 102 int h = Gdx.graphics.getHeight(); 103 int x = (int)(w - w*0.5f); 104 int y = (int)(h - h*0.5f); 105 w *= 0.5f; 106 h *= 0.5f; 107 108 Gdx.gl.glViewport(x, y, w, h); 109 Gdx.gl.glEnable(GL20.GL_SCISSOR_TEST); 110 Gdx.gl.glScissor(x, y, w, h); 111 Gdx.gl.glClearColor(1, 1, 1, 1); 112 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 113 114 pitch += 25 * Gdx.graphics.getDeltaTime(); 115 yaw += 45 * Gdx.graphics.getDeltaTime(); 116 cubeInstance.transform.setFromEulerAngles(yaw, pitch, roll); 117 cubeBatch.begin(camCube); 118 cubeBatch.render(cubeInstance); 119 cubeBatch.end(); 120 } 121 } 122