Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (497)
Games in Android Showcase (114)
games submitted by our members
Games in WIP (563)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: Cero (posted 2014-05-05 03:59:33, viewed 106 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143  
package main;

import java.awt.GraphicsEnvironment;

import com.badlogic.gdx.ApplicationAdapter;
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.Input;
import com.badlogic.gdx.backends.lwjgl.LwjglApplication;
import com.badlogic.gdx.backends.lwjgl.LwjglApplicationConfiguration;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.VertexAttributes.Usage;
import com.badlogic.gdx.graphics.g3d.Environment;
import com.badlogic.gdx.graphics.g3d.Material;
import com.badlogic.gdx.graphics.g3d.Model;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.attributes.ColorAttribute;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalLight;
import com.badlogic.gdx.graphics.g3d.environment.DirectionalShadowLight;
import com.badlogic.gdx.graphics.g3d.utils.CameraInputController;
import com.badlogic.gdx.graphics.g3d.utils.DepthShaderProvider;
import com.badlogic.gdx.graphics.g3d.utils.MeshPartBuilder;
import com.badlogic.gdx.graphics.g3d.utils.ModelBuilder;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;

public class ShadowMapping extends ApplicationAdapter
{
   PerspectiveCamera cam;
   CameraInputController camController;
   ModelBatch modelBatch;
   public Array<ModelInstance> instances = new Array<ModelInstance>();
   Environment environment;
   ModelInstance sphereModelInstance;
   DirectionalShadowLight shadowLight;
   ModelBatch shadowBatch;

   public void create () {
      modelBatch = new ModelBatch();
      environment = new Environment();
      environment.set(new ColorAttribute(ColorAttribute.AmbientLight, .4f, .4f, .4f, 1f));
      environment.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
      environment.add((shadowLight = new DirectionalShadowLight(1024, 1024, 30f, 30f, 1f, 100f)).set(0.8f, 0.8f, 0.8f, -1f, -.8f, -.2f));
      environment.shadowMap = shadowLight;

      cam = new PerspectiveCamera(67, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      cam.position.set(0f, 7f, 10f);
      cam.lookAt(0, 0, 0);
      cam.near = 1f;
      cam.far = 50f;
      cam.update();
      
      ModelBuilder modelBuilder = new ModelBuilder();
      MeshPartBuilder mpb;

      
      modelBuilder.begin();
          mpb = modelBuilder.part("parts", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.Color, new Material(ColorAttribute.createDiffuse(Color.WHITE)));
         mpb.setColor(1f, 1f, 1f, 1f);
         mpb.box(0, -1.5f, 0, 10, 1, 10);
      instances.add(new ModelInstance(modelBuilder.end()));
      
      modelBuilder.begin();
         mpb = modelBuilder.part("parts", GL20.GL_TRIANGLES, Usage.Position | Usage.Normal | Usage.Color, new Material(ColorAttribute.createDiffuse(Color.WHITE)));
         mpb.setColor(1f, 0f, 1f, 1f);
         mpb.sphere(2f, 2f, 2f, 10, 10);
      sphereModelInstance = new ModelInstance(modelBuilder.end());
      instances.add(sphereModelInstance);
      
      
      shadowBatch = new ModelBatch(new DepthShaderProvider());

      Gdx.input.setInputProcessor(camController = new CameraInputController(cam));
   }
   
   public static void main(String args[])
   {
      LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
      config.samples = 0;
      config.resizable = false;
      config.vSyncEnabled = true;   
      config.width  = (int)(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getWidth()*0.97f);
      config.height = (int)(GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getDisplayMode().getHeight()*0.8f);
      
      config.fullscreen = false;
      config.title = "ShadowMapping";
      
      new LwjglApplication(new ShadowMapping(), config);
   }

   @Override
   public void render () {
      camController.update();

      Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
      Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
      
      if (Gdx.input.isKeyPressed(Input.Keys.PAGE_UP))      sphereModelInstance.transform.translate(0f, 0.2f, 0f);
      if (Gdx.input.isKeyPressed(Input.Keys.PAGE_DOWN))   sphereModelInstance.transform.translate(0f, -0.2f, 0f);
      if (Gdx.input.isKeyPressed(Input.Keys.RIGHT))      sphereModelInstance.transform.translate(0.2f, 0f, 0f);
      if (Gdx.input.isKeyPressed(Input.Keys.LEFT))      sphereModelInstance.transform.translate(-0.2f, 0f, 0f);
      if (Gdx.input.isKeyPressed(Input.Keys.DOWN))      sphereModelInstance.transform.translate(0f, 0f, 0.2f);
      if (Gdx.input.isKeyPressed(Input.Keys.UP))         sphereModelInstance.transform.translate(0f, 0f, -0.2f);

      shadowLight.begin(Vector3.Zero, cam.direction);
      shadowBatch.begin(shadowLight.getCamera());
      shadowBatch.render(instances);
      shadowBatch.end();
      shadowLight.end();

      Gdx.gl.glClearColor(0, 0, 0, 1);

      modelBatch.begin(cam);
      modelBatch.render(instances, environment);
      modelBatch.end();
   }

   @Override
   public void dispose () {
      modelBatch.dispose();
      for (ModelInstance ins : instances)
      {
         ins.model.dispose();
      }
      instances.clear();
   }

   public boolean needsGL20 () {
      return true;
   }

   public void resume () {
   }

   public void resize (int width, int height) {
   }

   public void pause () {
   }
   
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

BurntPizza (21 views)
2014-09-19 03:14:18

Dwinin (35 views)
2014-09-12 09:08:26

Norakomi (62 views)
2014-09-10 13:57:51

TehJavaDev (87 views)
2014-09-10 06:39:09

Tekkerue (42 views)
2014-09-09 02:24:56

mitcheeb (65 views)
2014-09-08 06:06:29

BurntPizza (47 views)
2014-09-07 01:13:42

Longarmx (35 views)
2014-09-07 01:12:14

Longarmx (40 views)
2014-09-07 01:11:22

Longarmx (36 views)
2014-09-07 01:10:19
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!