OSDN Git Service

ran gdx-tools HeaderFixer tool
[mikumikustudio/libgdx-mikumikustudio.git] / tests / gdx-tests / src / com / badlogic / gdx / tests / g3d / voxel / VoxelTest.java
1 /*******************************************************************************\r
2  * Copyright 2011 See AUTHORS file.\r
3  * \r
4  * Licensed under the Apache License, Version 2.0 (the "License");\r
5  * you may not use this file except in compliance with the License.\r
6  * You may obtain a copy of the License at\r
7  * \r
8  *   http://www.apache.org/licenses/LICENSE-2.0\r
9  * \r
10  * Unless required by applicable law or agreed to in writing, software\r
11  * distributed under the License is distributed on an "AS IS" BASIS,\r
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
13  * See the License for the specific language governing permissions and\r
14  * limitations under the License.\r
15  ******************************************************************************/
16
17 package com.badlogic.gdx.tests.g3d.voxel;
18
19 import com.badlogic.gdx.Gdx;
20 import com.badlogic.gdx.graphics.Color;
21 import com.badlogic.gdx.graphics.GL10;
22 import com.badlogic.gdx.graphics.GL20;
23 import com.badlogic.gdx.graphics.Mesh;
24 import com.badlogic.gdx.graphics.PerspectiveCamera;
25 import com.badlogic.gdx.graphics.Texture;
26 import com.badlogic.gdx.graphics.VertexAttribute;
27 import com.badlogic.gdx.graphics.VertexAttributes.Usage;
28 import com.badlogic.gdx.graphics.g2d.BitmapFont;
29 import com.badlogic.gdx.graphics.g2d.SpriteBatch;
30 import com.badlogic.gdx.graphics.g2d.TextureRegion;
31 import com.badlogic.gdx.graphics.g3d.Environment;
32 import com.badlogic.gdx.graphics.g3d.Material;
33 import com.badlogic.gdx.graphics.g3d.Model;
34 import com.badlogic.gdx.graphics.g3d.ModelBatch;
35 import com.badlogic.gdx.graphics.g3d.ModelInstance;
36 import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
37 import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
38 import com.badlogic.gdx.graphics.g3d.shaders.DefaultShader;
39 import com.badlogic.gdx.graphics.g3d.shaders.GLES10Shader;
40 import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
41 import com.badlogic.gdx.graphics.g3d.utils.FirstPersonCameraController;
42 import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
43 import com.badlogic.gdx.math.MathUtils;
44 import com.badlogic.gdx.math.Vector3;
45 import com.badlogic.gdx.tests.utils.GdxTest;
46 import com.badlogic.gdx.tests.utils.PerspectiveCamController;
47
48 public class VoxelTest extends GdxTest {
49         SpriteBatch spriteBatch;
50         BitmapFont font;
51         ModelBatch modelBatch;
52         PerspectiveCamera camera;
53         Environment lights;
54         FirstPersonCameraController controller;
55         VoxelWorld voxelWorld;
56
57         @Override
58         public void create () {
59                 spriteBatch = new SpriteBatch();
60                 font = new BitmapFont();
61                 modelBatch = new ModelBatch();
62                 DefaultShader.defaultCullFace = GL20.GL_FRONT;
63                 GLES10Shader.defaultCullFace = GL20.GL_FRONT;
64                 camera = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
65                 camera.near = 0.5f;
66                 camera.far = 1000;
67                 controller = new FirstPersonCameraController(camera);
68                 Gdx.input.setInputProcessor(controller);
69                 
70                 lights = new Environment();
71                 lights.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.4f, 0.4f, 0.4f, 1.f));
72                 lights.add(new DirectionalLight().set(1, 1, 1, 0, -1, 0));
73                 
74                 Texture texture = new Texture(Gdx.files.internal("data/g3d/tiles.png"));
75                 TextureRegion[][] tiles = TextureRegion.split(texture, 32, 32);
76                 
77                 MathUtils.random.setSeed(0);
78                 voxelWorld = new VoxelWorld(tiles[0], 20, 4, 20);
79                 PerlinNoiseGenerator.generateVoxels(voxelWorld, 0, 63, 10);
80                 float camX = voxelWorld.voxelsX / 2f;
81                 float camZ = voxelWorld.voxelsZ / 2f;
82                 float camY = voxelWorld.getHighest(camX, camZ) + 1.5f;
83                 camera.position.set(camX, camY, camZ);
84         }
85
86         @Override
87         public void render () {
88                 Gdx.gl.glClearColor(0.4f, 0.4f, 0.4f, 1f);
89                 Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
90                 modelBatch.begin(camera);
91                 modelBatch.render(voxelWorld, lights);
92                 modelBatch.end();
93                 controller.update();
94                 
95                 spriteBatch.begin();
96                 font.draw(spriteBatch, "fps: " + Gdx.graphics.getFramesPerSecond() + ", #visible chunks: " + voxelWorld.renderedChunks + "/" + voxelWorld.numChunks, 0, 20);
97                 spriteBatch.end();
98         }
99         
100         @Override
101         public void resize (int width, int height) {
102                 spriteBatch.getProjectionMatrix().setToOrtho2D(0, 0, width, height);
103                 camera.viewportWidth = width;
104                 camera.viewportHeight = height;
105                 camera.update();
106         }
107
108         @Override
109         public boolean needsGL20 () {
110                 return false;
111         }
112 }