• 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.g3d;
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.VertexAttributes.Usage;
24 import com.badlogic.gdx.graphics.g3d.Environment;
25 import com.badlogic.gdx.graphics.g3d.Material;
26 import com.badlogic.gdx.graphics.g3d.Model;
27 import com.badlogic.gdx.graphics.g3d.ModelBatch;
28 import com.badlogic.gdx.graphics.g3d.ModelInstance;
29 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
30 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
31 import com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight;
32 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
33 import com.badlogic.gdx.graphics.g3d.utils.DepthShaderProvider;
34 import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
35 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
36 import com.badlogic.gdx.math.Vector3;
37 import com.badlogic.gdx.tests.utils.GdxTest;
38 
39 public class ShadowMappingTest extends GdxTest {
40 	PerspectiveCamera cam;
41 	CameraInputController camController;
42 	ModelBatch modelBatch;
43 	Model model;
44 	ModelInstance instance;
45 	Environment environment;
46 	DirectionalShadowLight shadowLight;
47 	ModelBatch shadowBatch;
48 
49 	@Override
create()50 	public void create () {
51 		modelBatch = new ModelBatch();
52 		environment = new Environment();
53 		environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f));
54 		environment.add((shadowLight = new DirectionalShadowLight(1024, 1024, 30f, 30f, 1f, 100f)).set(0.8f, 0.8f, 0.8f, -1f, -.8f,
55 			-.2f));
56 		environment.shadowMap = shadowLight;
57 
58 		cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
59 		cam.position.set(0f, 7f, 10f);
60 		cam.lookAt(0, 0, 0);
61 		cam.near = 1f;
62 		cam.far = 50f;
63 		cam.update();
64 
65 		ModelBuilder modelBuilder = new ModelBuilder();
66 		modelBuilder.begin();
67 		MeshPartBuilder mpb = modelBuilder.part("parts", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.ColorUnpacked,
68 			new Material(ColorAttribute.createDiffuse(Color.WHITE)));
69 		mpb.setColor(1f, 1f, 1f, 1f);
70 		mpb.box(0, -1.5f, 0, 10, 1, 10);
71 		mpb.setColor(1f, 0f, 1f, 1f);
72 		mpb.sphere(2f, 2f, 2f, 10, 10);
73 		model = modelBuilder.end();
74 		instance = new ModelInstance(model);
75 
76 		shadowBatch = new ModelBatch(new DepthShaderProvider());
77 
78 		Gdx.input.setInputProcessor(camController = new CameraInputController(cam));
79 	}
80 
81 	@Override
render()82 	public void render () {
83 		camController.update();
84 
85 		Gdx.gl.glViewport(0, 0, Gdx.graphics.getBackBufferWidth(), Gdx.graphics.getBackBufferHeight());
86 		Gdx.gl.glClearColor(0, 0, 0, 1);
87 		Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
88 
89 		shadowLight.begin(Vector3.Zero, cam.direction);
90 		shadowBatch.begin(shadowLight.getCamera());
91 		shadowBatch.render(instance);
92 		shadowBatch.end();
93 		shadowLight.end();
94 
95 		modelBatch.begin(cam);
96 		modelBatch.render(instance, environment);
97 		modelBatch.end();
98 	}
99 
100 	@Override
dispose()101 	public void dispose () {
102 		modelBatch.dispose();
103 		model.dispose();
104 	}
105 
needsGL20()106 	public boolean needsGL20 () {
107 		return true;
108 	}
109 
resume()110 	public void resume () {
111 	}
112 
resize(int width, int height)113 	public void resize (int width, int height) {
114 	}
115 
pause()116 	public void pause () {
117 	}
118 }
119