1 /******************************************************************************* 2 * Copyright 2011 See AUTHORS file. 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 ******************************************************************************/ 16 17 package com.badlogic.gdx.tests; 18 19 import com.badlogic.gdx.Gdx; 20 import com.badlogic.gdx.graphics.Color; 21 import com.badlogic.gdx.graphics.GL20; 22 import com.badlogic.gdx.graphics.PerspectiveCamera; 23 import com.badlogic.gdx.graphics.Texture; 24 import com.badlogic.gdx.graphics.Texture.TextureFilter; 25 import com.badlogic.gdx.graphics.g2d.BitmapFont; 26 import com.badlogic.gdx.graphics.g2d.SpriteBatch; 27 import com.badlogic.gdx.graphics.g2d.TextureRegion; 28 import com.badlogic.gdx.graphics.g3d.Model; 29 import com.badlogic.gdx.graphics.g3d.ModelBatch; 30 import com.badlogic.gdx.graphics.g3d.ModelInstance; 31 import com.badlogic.gdx.graphics.g3d.attributes.TextureAttribute; 32 import com.badlogic.gdx.graphics.g3d.loader.ObjLoader; 33 import com.badlogic.gdx.math.Vector3; 34 import com.badlogic.gdx.tests.utils.GdxTest; 35 import com.badlogic.gdx.utils.ScreenUtils; 36 37 public class FramebufferToTextureTest extends GdxTest { 38 39 TextureRegion fbTexture; 40 Texture texture; 41 Model mesh; 42 ModelInstance modelInstance; 43 ModelBatch modelBatch; 44 PerspectiveCamera cam; 45 SpriteBatch batch; 46 BitmapFont font; 47 Color clearColor = new Color(0.2f, 0.2f, 0.2f, 1); 48 float angle = 0; 49 50 @Override create()51 public void create () { 52 texture = new Texture(Gdx.files.internal("data/badlogic.jpg"), true); 53 texture.setFilter(TextureFilter.MipMap, TextureFilter.Linear); 54 ObjLoader objLoader = new ObjLoader(); 55 mesh = objLoader.loadModel(Gdx.files.internal("data/cube.obj")); 56 mesh.materials.get(0).set(new TextureAttribute(TextureAttribute.Diffuse, texture)); 57 modelInstance = new ModelInstance(mesh); 58 modelBatch = new ModelBatch(); 59 60 cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight()); 61 cam.position.set(3, 3, 3); 62 cam.direction.set(-1, -1, -1); 63 batch = new SpriteBatch(); 64 font = new BitmapFont(); 65 } 66 67 @Override render()68 public void render () { 69 Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight()); 70 Gdx.gl.glClearColor(clearColor.g, clearColor.g, clearColor.b, clearColor.a); 71 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT); 72 Gdx.gl.glEnable(GL20.GL_DEPTH_TEST); 73 74 cam.update(); 75 76 modelInstance.transform.rotate(Vector3.Y, 45 * Gdx.graphics.getDeltaTime()); 77 modelBatch.begin(cam); 78 modelBatch.render(modelInstance); 79 modelBatch.end(); 80 81 if (Gdx.input.justTouched() || fbTexture == null) { 82 if (fbTexture != null) fbTexture.getTexture().dispose(); 83 fbTexture = ScreenUtils.getFrameBufferTexture(); 84 } 85 86 batch.begin(); 87 if (fbTexture != null) { 88 batch.draw(fbTexture, 0, 0, 100, 100); 89 } 90 font.draw(batch, "Touch screen to take a snapshot", 10, 40); 91 batch.end(); 92 } 93 94 @Override pause()95 public void pause () { 96 fbTexture = null; 97 } 98 } 99