• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 java.util.Arrays;
20 
21 import com.badlogic.gdx.Gdx;
22 import com.badlogic.gdx.graphics.FPSLogger;
23 import com.badlogic.gdx.graphics.GL20;
24 import com.badlogic.gdx.graphics.PerspectiveCamera;
25 import com.badlogic.gdx.graphics.Pixmap.Format;
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.loader.ObjLoader;
32 import com.badlogic.gdx.graphics.glutils.FrameBuffer;
33 import com.badlogic.gdx.graphics.glutils.ShaderProgram;
34 import com.badlogic.gdx.math.Matrix4;
35 import com.badlogic.gdx.tests.utils.GdxTest;
36 
37 public class EdgeDetectionTest extends GdxTest {
38 
39 	FPSLogger logger;
40 	// ShaderProgram shader;
41 	Model scene;
42 	ModelInstance sceneInstance;
43 	ModelBatch modelBatch;
44 	FrameBuffer fbo;
45 	PerspectiveCamera cam;
46 	Matrix4 matrix = new Matrix4();
47 	float angle = 0;
48 	TextureRegion fboRegion;
49 	SpriteBatch batch;
50 	ShaderProgram batchShader;
51 
52 	float[] filter = {0, 0.25f, 0, 0.25f, -1f, 0.6f, 0, 0.25f, 0,};
53 
54 	float[] offsets = new float[18];
55 
create()56 	public void create () {
57 		ShaderProgram.pedantic = false;
58 		/*
59 		 * shader = new ShaderProgram(Gdx.files.internal("data/shaders/default.vert").readString(), Gdx.files.internal(
60 		 * "data/shaders/depthtocolor.frag").readString()); if (!shader.isCompiled()) { Gdx.app.log("EdgeDetectionTest",
61 		 * "couldn't compile scene shader: " + shader.getLog()); }
62 		 */
63 		batchShader = new ShaderProgram(Gdx.files.internal("data/shaders/batch.vert").readString(), Gdx.files.internal(
64 			"data/shaders/convolution.frag").readString());
65 		if (!batchShader.isCompiled()) {
66 			Gdx.app.log("EdgeDetectionTest", "couldn't compile post-processing shader: " + batchShader.getLog());
67 		}
68 
69 		ObjLoader objLoader = new ObjLoader();
70 		scene = objLoader.loadModel(Gdx.files.internal("data/scene.obj"));
71 		sceneInstance = new ModelInstance(scene);
72 		modelBatch = new ModelBatch();
73 		fbo = new FrameBuffer(Format.RGB565, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), true);
74 		cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
75 		cam.position.set(0, 0, 10);
76 		cam.lookAt(0, 0, 0);
77 		cam.far = 30;
78 		batch = new SpriteBatch();
79 		batch.setShader(batchShader);
80 		fboRegion = new TextureRegion(fbo.getColorBufferTexture());
81 		fboRegion.flip(false, true);
82 		logger = new FPSLogger();
83 		calculateOffsets();
84 	}
85 
86 	@Override
dispose()87 	public void dispose () {
88 		batchShader.dispose();
89 		scene.dispose();
90 		fbo.dispose();
91 		batch.dispose();
92 	}
93 
calculateOffsets()94 	private void calculateOffsets () {
95 		int idx = 0;
96 		for (int y = -1; y <= 1; y++) {
97 			for (int x = -1; x <= 1; x++) {
98 				offsets[idx++] = x / (float)Gdx.graphics.getWidth();
99 				offsets[idx++] = y / (float)Gdx.graphics.getHeight();
100 			}
101 		}
102 		System.out.println(Arrays.toString(offsets));
103 	}
104 
render()105 	public void render () {
106 		angle += 45 * Gdx.graphics.getDeltaTime();
107 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
108 
109 		cam.update();
110 		matrix.setToRotation(0, 1, 0, angle);
111 		cam.combined.mul(matrix);
112 
113 		fbo.begin();
114 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
115 		Gdx.gl.glEnable(GL20.GL_DEPTH_TEST);
116 		modelBatch.begin(cam);
117 		modelBatch.render(sceneInstance);
118 		modelBatch.end();
119 		fbo.end();
120 
121 		batch.begin();
122 		batch.disableBlending();
123 		batchShader.setUniformi("u_filterSize", filter.length);
124 		batchShader.setUniform1fv("u_filter", filter, 0, filter.length);
125 		batchShader.setUniform2fv("u_offsets", offsets, 0, offsets.length);
126 		batch.draw(fboRegion, 0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
127 		batch.end();
128 		logger.log();
129 	}
130 }
131