3D tennis

Im using libGDX and this is a desktop project.

I have 2 models, one is a symbol and the other is a map, as you can see in the image below:

enter image description here

My question is:

How do I project a character's shadow onto the floor?

As you can see, the character has no shadow, thus the light of Atmina. What should I use or how should I achieve this? Should I fake the shadow or is there a real way to project shadows?

Any comments or suggestions are appreciated.

+3


source to share


2 answers


You can use the following code:



Environment environment;
DirectionalShadowLight shadowLight;

@Override
public void show() {
    modelBatch = new ModelBatch(); 
    environment = new Environment();
    environment.set(new ColorAttribute(ColorAttribute.AmbientLight, 1.0f, 1f, .6f, 1f));
    environment.add((shadowLight = new DirectionalShadowLight(1024, 1024, 60f, 60f, .1f, 50f))                  
                .set(1f, 1f, 1f, 40.0f, -35f, -35f));   
    environment.shadowMap = shadowLight; 

    shadowBatch = new ModelBatch(new DepthShaderProvider());
}

@Override
public void render(float delta) {
        //create shadow texture
        shadowLight.begin(Vector3.Zero, camera.direction);
        shadowBatch.begin(shadowLight.getCamera());

        shadowBatch.render(instances);

        shadowBatch.end();
        shadowLight.end();

        //render scene
        Gdx.gl.glClearColor(0, 0, 0, 1);
        Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);


        modelBatch.begin(cam);      
        modelBatch.render(instances, environment);  //environment has shadowMap!
        modelBatch.end();
}

      

+5


source


Sometime in 2015, a better shadow system was added to the libGDX benchmark library, separate from the "experimental, unused" stuff mentioned in @ Nolesh's answer.

The code is in libGDX tests , so you will need to copy it to your own project. A short description of how this works has been posted on the libGDX forums by the author, realitix. Note that there doesn't seem to be a fully executable example, but the JavaDoc for the interface ShadowSystem

contains the following:



// Init system:
Array<ModelBatch> passBatches = new Array<ModelBatch>();
ModelBatch mainBatch;
ShadowSystem system = new XXXShadowSystem();
system.init();
for (int i = 0; i < system.getPassQuantity(); i++) {
    passBatches.add(new ModelBatch(system.getPassShaderProvider(i)));
}
mainBatch = new ModelBatch(system.getShaderProvider());

// Render scene with shadows:
system.begin(camera, instances);
system.update();
for (int i = 0; i < system.getPassQuantity(); i++) {
    system.begin(i);
    Camera camera;
    while ((camera = system.next()) != null) {
        passBatches.get(i).begin(camera);
        passBatches.get(i).render(instances, environment);
        passBatches.get(i).end();
    }
    camera = null;
    system.end(i);
}
system.end();

HdpiUtils.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);

mainBatch.begin(cam);
mainBatch.render(instances, environment);
mainBatch.end();

      

This is not as good as having a proper shading API, but better than implementing the whole thing from scratch.

0


source







All Articles