com.github.fauu.helix.core.Renderer.java Source code

Java tutorial

Introduction

Here is the source code for com.github.fauu.helix.core.Renderer.java

Source

/*
 * Copyright (C) 2014 Helix Engine Developers (http://github.com/fauu/HelixEngine)
 *
 * This software is licensed under the GNU General Public License
 * (version 3 or later). See the COPYING file in this distribution.
 *
 * You should have received a copy of the GNU Library General Public License
 * along with this software. If not, see <http://www.gnu.org/licenses/>.
 *
 * Authored by: Piotr Grabowski <fau999@gmail.com>
 */

package com.github.fauu.helix.core;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.GL20;
import com.badlogic.gdx.graphics.PerspectiveCamera;
import com.badlogic.gdx.graphics.Pixmap;
import com.badlogic.gdx.graphics.Texture;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.g3d.ModelBatch;
import com.badlogic.gdx.graphics.g3d.ModelInstance;
import com.badlogic.gdx.graphics.g3d.RenderableProvider;
import com.badlogic.gdx.graphics.g3d.decals.CameraGroupStrategy;
import com.badlogic.gdx.graphics.g3d.decals.Decal;
import com.badlogic.gdx.graphics.g3d.decals.DecalBatch;
import com.badlogic.gdx.graphics.g3d.utils.DefaultTextureBinder;
import com.badlogic.gdx.graphics.glutils.FrameBuffer;
import com.badlogic.gdx.math.Vector3;
import com.badlogic.gdx.utils.Array;

public class Renderer {

    private ShaderProvider shaderProvider;
    private ModelBatch modelBatch;
    private DecalBatch decalBatch;
    private com.badlogic.gdx.graphics.g3d.utils.RenderContext renderContext;
    private FrameBuffer fb;
    private SpriteBatch spriteBatch;

    private Pixmap reflectionPixmap;
    private Texture reflectionTexture;

    private CameraGroupStrategy defaultCameraGroupStrategy;
    private CameraGroupStrategy waterCameraGroupStrategy;

    private PerspectiveCamera waterCamera;

    public Renderer() {
        renderContext = new com.badlogic.gdx.graphics.g3d.utils.RenderContext(
                new DefaultTextureBinder(DefaultTextureBinder.WEIGHTED, 1));

        shaderProvider = new ShaderProvider();

        modelBatch = new ModelBatch(renderContext, shaderProvider);

        fb = new FrameBuffer(Pixmap.Format.RGBA8888, Gdx.graphics.getWidth(), Gdx.graphics.getHeight(), false);

        spriteBatch = new SpriteBatch();

        waterCamera = new PerspectiveCamera(50, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        waterCamera.position.set(0, 0, 0);
        waterCamera.rotateAround(new Vector3(0, 0, 0), new Vector3(0, 1, 0), -90);
        waterCamera.lookAt(0, 1f, -1);
        waterCamera.near = 0.01f;
        waterCamera.far = 300;
        waterCamera.position.add(11, 0, 22);
        waterCamera.update();
    }

    public void render(Camera camera, Array<RenderableProvider> renderableProviders, Array<Decal> decals) {
        final GL20 gl = Gdx.graphics.getGL20();

        gl.glViewport(0, 0, Gdx.graphics.getWidth(), Gdx.graphics.getHeight());
        gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        gl.glEnable(GL20.GL_BLEND);
        gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

        if (decalBatch == null) {
            defaultCameraGroupStrategy = new CameraGroupStrategy(camera);
            waterCameraGroupStrategy = new CameraGroupStrategy(waterCamera);

            decalBatch = new DecalBatch(defaultCameraGroupStrategy);
        }

        //waterCamera.moveTo(new Vector2(13, 19));
        /* Render objects to framebuffer */
        fb.begin();
        gl.glClearColor(0, 0, 0, 0);
        gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
        gl.glEnable(GL20.GL_BLEND);
        gl.glBlendFunc(GL20.GL_SRC_ALPHA, GL20.GL_ONE_MINUS_SRC_ALPHA);

        renderContext.begin();
        modelBatch.begin(waterCamera);
        for (int i = 1; i < renderableProviders.size - 1; i++) {
            modelBatch.render(renderableProviders.get(i));
        }
        modelBatch.end();
        renderContext.end();

        decalBatch.setGroupStrategy(waterCameraGroupStrategy);
        if (decals != null) {
            decals.first().rotateX(50);
            decalBatch.add(decals.first());
            decalBatch.flush();
            decals.first().rotateX(-50);
        }

        //    Vector3 waterPos = waterCamera.project(new Vector3(8, 0, 19));
        //    reflectionPixmap = ScreenUtils.getFrameBufferPixmap((int) waterPos.x, (int) waterPos.y, 348, 261);
        //    reflectionPixmap = ScreenUtils.getFrameBufferPixmap(0, 0, 800, 600);

        fb.end();

        //    if (reflectionTexture == null) {
        //      reflectionTexture = new Texture(reflectionPixmap);
        //    } else {
        //      reflectionTexture.draw(reflectionPixmap, 0, 0);
        //    }

        ((WaterData) ((ModelInstance) renderableProviders
                .get(renderableProviders.size - 1)).userData).reflectionTexture = fb.getColorBufferTexture();

        /* Render objects and terrain to the screen */
        renderContext.begin();
        modelBatch.begin(camera);
        modelBatch.render(renderableProviders);
        modelBatch.end();
        renderContext.end();
        decalBatch.setGroupStrategy(defaultCameraGroupStrategy);
        if (decals != null) {
            decalBatch.add(decals.first());
        }
        decalBatch.flush();

        spriteBatch.begin();
        spriteBatch.enableBlending();
        spriteBatch.draw(fb.getColorBufferTexture(), 0, 0, 800, 600);
        spriteBatch.end();

    }

}