Advertisement
Guest User

3d scene test

a guest
Jul 11th, 2015
35
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.50 KB | None | 0 0
  1. public class GameScreen implements Screen {
  2.    
  3.     public PerspectiveCamera cam;
  4.     public ModelInstance instance;
  5.     public Model model;
  6.     public Environment environment;
  7.     public ModelBatch modelBatch;
  8.    
  9.     public GameScreen() {
  10.    
  11.         cam = new PerspectiveCamera(67f, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());   
  12.         cam.position.set(0, 0, 0);
  13.         cam.lookAt(0,0,0);
  14.         cam.near = 0.1f;
  15.         cam.far = 250f;
  16.         cam.update();
  17.  
  18.         modelBatch = new ModelBatch();
  19.        
  20.         environment = new Environment();
  21.             environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.7f, 0.7f, 0.7f, 1f));
  22.     }
  23.  
  24.     @Override
  25.     public void render(float delta) {
  26.            
  27.          Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
  28.          Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
  29.  
  30.          modelBatch.begin(cam);
  31.  
  32.          Texture tex =  new Texture(Gdx.files.internal("grid.png"), true);
  33.          tex.setFilter(TextureFilter.MipMap, TextureFilter.MipMap);
  34.          modelBatch.render(this.createPlane("front",tex), environment);
  35.          modelBatch.render(this.createPlane("bottom", tex), environment);
  36.          modelBatch.render(this.createPlane("left", tex), environment);
  37.          modelBatch.render(this.createPlane("right", tex), environment);
  38.  
  39.          modelBatch.end();
  40.     }
  41.  
  42.     @Override
  43.     public void resize(int width, int height) {
  44.         float aspectRatio = (float) width / (float) height;
  45.         cam = new PerspectiveCamera(67, 2f * aspectRatio, 2f);
  46.        
  47.     }
  48.  
  49.     private ModelInstance createPlane(String wall, Texture texture) {
  50.        
  51.         float z = -3.5f;
  52.  
  53.             Vector3 _00, _01, _10, _11, norm;
  54.  
  55.             _00 = new Vector3();
  56.             _01 = new Vector3();
  57.             _10 = new Vector3();
  58.             _11 = new Vector3();
  59.             norm = new Vector3();
  60.        
  61.             // Wall data
  62.             HashMap<String, ArrayList<Vector3>> wallData = new HashMap<String, ArrayList<Vector3>>();
  63.        
  64.             // front
  65.             ArrayList<Vector3> front = new ArrayList<Vector3>();
  66.             front.add(new Vector3(1, 1, z));
  67.             front.add(new Vector3(-1, 1, z));
  68.             front.add(new Vector3(-1, -1, z));
  69.             front.add(new Vector3(1, -1, z));
  70.        
  71.             wallData.put("front", front);
  72.        
  73.             // bottom
  74.             ArrayList<Vector3> bottom = new ArrayList<Vector3>();
  75.             bottom.add(new Vector3(1, -1, z));
  76.             bottom.add(new Vector3(-1, -1, z));
  77.             bottom.add(new Vector3(-1f, -1, z+2));
  78.             bottom.add(new Vector3(1f, -1, z+2));
  79.        
  80.             wallData.put("bottom", bottom);
  81.  
  82.             // left
  83.             ArrayList<Vector3> left = new ArrayList<Vector3>();
  84.             left.add(new Vector3(-1, 1, z));
  85.             left.add(new Vector3(-1, 1, z+2));
  86.             left.add(new Vector3(-1, -1, z+2));
  87.             left.add(new Vector3(-1, -1, z));
  88.  
  89.             wallData.put("left", left);
  90.        
  91.             // right
  92.             ArrayList<Vector3> right = new ArrayList<Vector3>();
  93.             right.add(new Vector3(1, 1, z+2));
  94.             right.add(new Vector3(1, 1, z));
  95.             right.add(new Vector3(1, -1, z));
  96.             right.add(new Vector3(1, -1, z+2));
  97.  
  98.             wallData.put("right", right);
  99.  
  100.             Material material = new Material();
  101.             texture.setFilter(TextureFilter.Linear, TextureFilter.Nearest);
  102.  
  103.             TextureAttribute textureAttribute = new TextureAttribute(TextureAttribute.Diffuse, texture);
  104.             material.set(textureAttribute);
  105.  
  106.             ArrayList<Vector3> vertexData = wallData.get(wall);
  107.             Model model = null;
  108.             ModelBuilder mb = new ModelBuilder();
  109.             mb.begin();
  110.  
  111.             MeshPartBuilder part = mb.part("rect", GL20.GL_TRIANGLES,  VertexAttributes.Usage.Position | VertexAttributes.Usage.Normal | VertexAttributes.Usage.TextureCoordinates, material);
  112.  
  113.             _11.set(vertexData.get(0));         // right up
  114.             _01.set(vertexData.get(1));         // left up
  115.  
  116.             _00.set(vertexData.get(2));         // left bottom
  117.             _10.set(vertexData.get(3));         // right bottom
  118.  
  119.             norm = new Vector3(0, 0, -1);
  120.  
  121.             part.rect(_00, _10, _11, _01, norm);
  122.             model = mb.end();
  123.  
  124.             model.manageDisposable(texture);
  125.             ModelInstance modelInstance = new ModelInstance(model);
  126.  
  127.             modelInstance.transform.setToTranslation(0, 0, 0);
  128.  
  129.             return modelInstance;
  130.         }
  131.  
  132. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement