Search in sources :

Example 16 with PipelineRenderingContext

use of com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext in project gdx-graph by MarcinSc.

the class ShadowShaderTestScene method loadPipelineRenderer.

private PipelineRenderer loadPipelineRenderer() {
    PipelineRenderer pipelineRenderer = PipelineLoader.loadPipelineRenderer(Gdx.files.local("test/shadow-shader-test.json"), timeKeeper);
    pipelineRenderer.setPipelineProperty("Camera", camera);
    pipelineRenderer.getPluginData(RenderCallbackPublicData.class).setRenderCallback("Callback", new RenderCallback() {

        @Override
        public void renderCallback(RenderPipeline renderPipeline, PipelineDataProvider pipelineDataProvider, PipelineRenderingContext pipelineRenderingContext, PipelineNode.PipelineRequirementsCallback pipelineRequirementsCallback) {
            RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
            Directional3DLight firstLight = environment.getDirectionalLights().get(0);
            RenderPipelineBuffer shadowFrameBuffer = firstLight.getShadowFrameBuffer();
            renderPipeline.drawTexture(shadowFrameBuffer, currentBuffer, pipelineRenderingContext, pipelineDataProvider.getFullScreenRender());
        }
    });
    return pipelineRenderer;
}
Also used : PipelineDataProvider(com.gempukku.libgdx.graph.pipeline.producer.node.PipelineDataProvider) Directional3DLight(com.gempukku.libgdx.graph.plugin.lighting3d.Directional3DLight) RenderCallbackPublicData(com.gempukku.libgdx.graph.plugin.callback.RenderCallbackPublicData) RenderCallback(com.gempukku.libgdx.graph.plugin.callback.RenderCallback) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) PipelineNode(com.gempukku.libgdx.graph.pipeline.producer.node.PipelineNode)

Example 17 with PipelineRenderingContext

use of com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext in project gdx-graph by MarcinSc.

the class ModelShaderRendererPipelineNodeProducer method createNodeForSingleInputs.

@Override
public PipelineNode createNodeForSingleInputs(JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
    final ShaderContextImpl shaderContext = new ShaderContextImpl(pluginPrivateDataSource);
    final ObjectMap<String, ShaderGroup> shaderGroups = new ObjectMap<>();
    final Array<String> allShaderTags = new Array<>();
    final Array<String> depthDrawingShaderTags = new Array<>();
    final JsonValue shaderDefinitions = data.get("shaders");
    RenderOrder renderOrder = RenderOrder.valueOf(data.getString("renderOrder", "Shader_Unordered"));
    final ModelRenderingStrategy renderingStrategy = createRenderingStrategy(renderOrder);
    final RenderingStrategyCallback colorStrategyCallback = new RenderingStrategyCallback(shaderContext, new Function<String, GraphShader>() {

        @Override
        public GraphShader apply(String s) {
            return shaderGroups.get(s).getColorShader();
        }
    });
    final RenderingStrategyCallback depthStrategyCallback = new RenderingStrategyCallback(shaderContext, new Function<String, GraphShader>() {

        @Override
        public GraphShader apply(String s) {
            return shaderGroups.get(s).getDepthShader();
        }
    });
    final ObjectMap<String, PipelineNode.FieldOutput<?>> result = new ObjectMap<>();
    final DefaultFieldOutput<RenderPipeline> output = new DefaultFieldOutput<>(PipelineFieldType.RenderPipeline);
    result.put("output", output);
    return new SingleInputsPipelineNode(result) {

        private FullScreenRender fullScreenRender;

        private TimeProvider timeProvider;

        private GraphModelsImpl models;

        private WhitePixel whitePixel;

        @Override
        public void initializePipeline(PipelineDataProvider pipelineDataProvider) {
            fullScreenRender = pipelineDataProvider.getFullScreenRender();
            timeProvider = pipelineDataProvider.getTimeProvider();
            models = pipelineDataProvider.getPrivatePluginData(GraphModelsImpl.class);
            whitePixel = pipelineDataProvider.getWhitePixel();
            for (JsonValue shaderDefinition : shaderDefinitions) {
                ShaderGroup shaderGroup = new ShaderGroup(shaderDefinition);
                shaderGroup.initialize(whitePixel);
                allShaderTags.add(shaderGroup.getTag());
                shaderGroups.put(shaderGroup.getTag(), shaderGroup);
                if (shaderGroup.getColorShader().isDepthWriting())
                    depthDrawingShaderTags.add(shaderGroup.getTag());
            }
            for (ShaderGroup shaderGroup : shaderGroups.values()) {
                GraphShader shader = shaderGroup.getColorShader();
                models.registerTag(shader.getTag(), shader);
            }
        }

        private void initializeDepthShaders() {
            for (String depthDrawingShaderTag : depthDrawingShaderTags) {
                shaderGroups.get(depthDrawingShaderTag).initializeDepthShader(whitePixel);
            }
        }

        private boolean needsDepth() {
            for (ShaderGroup shaderGroup : shaderGroups.values()) {
                GraphShader colorShader = shaderGroup.getColorShader();
                if (colorShader.isUsingDepthTexture() && models.hasModelWithTag(colorShader.getTag()))
                    return true;
            }
            return false;
        }

        private boolean isRequiringSceneColor() {
            for (ShaderGroup shaderGroup : shaderGroups.values()) {
                GraphShader colorShader = shaderGroup.getColorShader();
                if (colorShader.isUsingColorTexture() && models.hasModelWithTag(colorShader.getTag()))
                    return true;
            }
            return false;
        }

        @Override
        public void processPipelineRequirements(PipelineRequirements pipelineRequirements) {
            if (needsDepth())
                pipelineRequirements.setRequiringDepthTexture();
        }

        @Override
        public void executeNode(PipelineRenderingContext pipelineRenderingContext, PipelineRequirementsCallback pipelineRequirementsCallback) {
            final PipelineNode.FieldOutput<Boolean> processorEnabled = (PipelineNode.FieldOutput<Boolean>) inputs.get("enabled");
            final PipelineNode.FieldOutput<Camera> cameraInput = (PipelineNode.FieldOutput<Camera>) inputs.get("camera");
            final PipelineNode.FieldOutput<RenderPipeline> renderPipelineInput = (PipelineNode.FieldOutput<RenderPipeline>) inputs.get("input");
            RenderPipeline renderPipeline = renderPipelineInput.getValue();
            boolean enabled = processorEnabled == null || processorEnabled.getValue();
            if (enabled) {
                boolean needsToDrawDepth = pipelineRequirementsCallback.getPipelineRequirements().isRequiringDepthTexture();
                if (needsToDrawDepth)
                    initializeDepthShaders();
                boolean usesDepth = needsDepth();
                boolean needsSceneColor = isRequiringSceneColor();
                RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
                Camera camera = cameraInput.getValue();
                shaderContext.setCamera(camera);
                shaderContext.setTimeProvider(timeProvider);
                shaderContext.setRenderWidth(currentBuffer.getWidth());
                shaderContext.setRenderHeight(currentBuffer.getHeight());
                if (needsToDrawDepth || usesDepth) {
                    renderPipeline.enrichWithDepthBuffer(currentBuffer);
                    shaderContext.setDepthTexture(currentBuffer.getDepthBufferTexture());
                }
                RenderPipelineBuffer sceneColorBuffer = null;
                if (needsSceneColor)
                    sceneColorBuffer = setupColorTexture(renderPipeline, currentBuffer, pipelineRenderingContext);
                // Drawing models on color buffer
                colorStrategyCallback.prepare(pipelineRenderingContext, models);
                currentBuffer.beginColor();
                renderingStrategy.processModels(models, allShaderTags, camera, colorStrategyCallback);
                currentBuffer.endColor();
                if (needsToDrawDepth) {
                    // Drawing models on depth buffer
                    depthStrategyCallback.prepare(pipelineRenderingContext, models);
                    currentBuffer.beginDepth();
                    renderingStrategy.processModels(models, depthDrawingShaderTags, camera, depthStrategyCallback);
                    currentBuffer.endDepth();
                }
                if (sceneColorBuffer != null)
                    renderPipeline.returnFrameBuffer(sceneColorBuffer);
            }
            output.setValue(renderPipeline);
        }

        private RenderPipelineBuffer setupColorTexture(final RenderPipeline renderPipeline, final RenderPipelineBuffer currentBuffer, PipelineRenderingContext pipelineRenderingContext) {
            RenderPipelineBuffer sceneColorBuffer = renderPipeline.getNewFrameBuffer(currentBuffer, Color.BLACK);
            shaderContext.setColorTexture(sceneColorBuffer.getColorBufferTexture());
            renderPipeline.drawTexture(currentBuffer, sceneColorBuffer, pipelineRenderingContext, fullScreenRender);
            return sceneColorBuffer;
        }

        @Override
        public void dispose() {
            for (ShaderGroup shaderGroup : shaderGroups.values()) {
                shaderGroup.dispose();
            }
        }
    };
}
Also used : ShaderContextImpl(com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.ShaderContextImpl) FullScreenRender(com.gempukku.libgdx.graph.pipeline.producer.FullScreenRender) GraphModelsImpl(com.gempukku.libgdx.graph.plugin.models.impl.GraphModelsImpl) ObjectMap(com.badlogic.gdx.utils.ObjectMap) GraphShader(com.gempukku.libgdx.graph.shader.GraphShader) Camera(com.badlogic.gdx.graphics.Camera) WhitePixel(com.gempukku.libgdx.graph.util.WhitePixel) TimeProvider(com.gempukku.libgdx.graph.time.TimeProvider) RenderPipelineBuffer(com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer) JsonValue(com.badlogic.gdx.utils.JsonValue) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) Array(com.badlogic.gdx.utils.Array) RenderOrder(com.gempukku.libgdx.graph.pipeline.RenderOrder) RenderPipeline(com.gempukku.libgdx.graph.pipeline.RenderPipeline)

Example 18 with PipelineRenderingContext

use of com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext in project gdx-graph by MarcinSc.

the class MapsLayersRendererPipelineNodeProducer method createNodeForSingleInputs.

@Override
public PipelineNode createNodeForSingleInputs(final JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
    final String mapId = data.getString("id");
    final String[] layerNames = data.getString("layers").split(",");
    final int[] ids = new int[layerNames.length];
    final ObjectMap<String, PipelineNode.FieldOutput<?>> result = new ObjectMap<>();
    final DefaultFieldOutput<RenderPipeline> output = new DefaultFieldOutput<>(PipelineFieldType.RenderPipeline);
    result.put("output", output);
    return new SingleInputsPipelineNode(result) {

        private MapsPluginPrivateData mapsPluginData;

        @Override
        public void initializePipeline(PipelineDataProvider pipelineDataProvider) {
            mapsPluginData = pipelineDataProvider.getPrivatePluginData(MapsPluginPrivateData.class);
        }

        @Override
        public void executeNode(PipelineRenderingContext pipelineRenderingContext, PipelineRequirementsCallback pipelineRequirementsCallback) {
            final PipelineNode.FieldOutput<Boolean> processorEnabled = (PipelineNode.FieldOutput<Boolean>) inputs.get("enabled");
            final PipelineNode.FieldOutput<Camera> cameraInput = (PipelineNode.FieldOutput<Camera>) inputs.get("camera");
            final PipelineNode.FieldOutput<RenderPipeline> renderPipelineInput = (PipelineNode.FieldOutput<RenderPipeline>) inputs.get("input");
            RenderPipeline renderPipeline = renderPipelineInput.getValue();
            Camera camera = cameraInput.getValue();
            boolean enabled = processorEnabled == null || processorEnabled.getValue();
            Map map = mapsPluginData.getMap(mapId);
            MapRenderer mapRenderer = mapsPluginData.getMapRenderer(mapId);
            if (enabled && map != null) {
                // Sadly need to switch off (and then on) the RenderContext
                pipelineRenderingContext.getRenderContext().end();
                RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
                for (int i = 0; i < layerNames.length; i++) {
                    ids[i] = map.getLayers().getIndex(layerNames[i].trim());
                }
                currentBuffer.beginColor();
                mapRenderer.setView((OrthographicCamera) camera);
                mapRenderer.render(ids);
                currentBuffer.endColor();
                pipelineRenderingContext.getRenderContext().begin();
            }
            output.setValue(renderPipeline);
        }
    };
}
Also used : ObjectMap(com.badlogic.gdx.utils.ObjectMap) Camera(com.badlogic.gdx.graphics.Camera) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) MapsPluginPrivateData(com.gempukku.libgdx.graph.plugin.maps.MapsPluginPrivateData) RenderPipelineBuffer(com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) MapRenderer(com.badlogic.gdx.maps.MapRenderer) ObjectMap(com.badlogic.gdx.utils.ObjectMap) Map(com.badlogic.gdx.maps.Map) RenderPipeline(com.gempukku.libgdx.graph.pipeline.RenderPipeline)

Example 19 with PipelineRenderingContext

use of com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext in project gdx-graph by MarcinSc.

the class MapsRendererPipelineNodeProducer method createNodeForSingleInputs.

@Override
public PipelineNode createNodeForSingleInputs(JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
    final String mapId = data.getString("id");
    final ObjectMap<String, PipelineNode.FieldOutput<?>> result = new ObjectMap<>();
    final DefaultFieldOutput<RenderPipeline> output = new DefaultFieldOutput<>(PipelineFieldType.RenderPipeline);
    result.put("output", output);
    return new SingleInputsPipelineNode(result) {

        private MapsPluginPrivateData mapsPluginData;

        @Override
        public void initializePipeline(PipelineDataProvider pipelineDataProvider) {
            mapsPluginData = pipelineDataProvider.getPrivatePluginData(MapsPluginPrivateData.class);
        }

        @Override
        public void executeNode(PipelineRenderingContext pipelineRenderingContext, PipelineRequirementsCallback pipelineRequirementsCallback) {
            final PipelineNode.FieldOutput<Boolean> processorEnabled = (PipelineNode.FieldOutput<Boolean>) inputs.get("enabled");
            final PipelineNode.FieldOutput<Camera> cameraInput = (PipelineNode.FieldOutput<Camera>) inputs.get("camera");
            final PipelineNode.FieldOutput<RenderPipeline> renderPipelineInput = (PipelineNode.FieldOutput<RenderPipeline>) inputs.get("input");
            RenderPipeline renderPipeline = renderPipelineInput.getValue();
            Camera camera = cameraInput.getValue();
            boolean enabled = processorEnabled == null || processorEnabled.getValue();
            Map map = mapsPluginData.getMap(mapId);
            MapRenderer mapRenderer = mapsPluginData.getMapRenderer(mapId);
            if (enabled && map != null) {
                // Sadly need to switch off (and then on) the RenderContext
                pipelineRenderingContext.getRenderContext().end();
                RenderPipelineBuffer currentBuffer = renderPipeline.getDefaultBuffer();
                currentBuffer.beginColor();
                mapRenderer.setView((OrthographicCamera) camera);
                mapRenderer.render();
                currentBuffer.endColor();
                pipelineRenderingContext.getRenderContext().begin();
            }
            output.setValue(renderPipeline);
        }
    };
}
Also used : MapsPluginPrivateData(com.gempukku.libgdx.graph.plugin.maps.MapsPluginPrivateData) RenderPipelineBuffer(com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) ObjectMap(com.badlogic.gdx.utils.ObjectMap) MapRenderer(com.badlogic.gdx.maps.MapRenderer) Camera(com.badlogic.gdx.graphics.Camera) OrthographicCamera(com.badlogic.gdx.graphics.OrthographicCamera) ObjectMap(com.badlogic.gdx.utils.ObjectMap) Map(com.badlogic.gdx.maps.Map) RenderPipeline(com.gempukku.libgdx.graph.pipeline.RenderPipeline)

Example 20 with PipelineRenderingContext

use of com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext in project gdx-graph by MarcinSc.

the class DistancePipelineNodeProducer method createNodeForSingleInputs.

@Override
public PipelineNode createNodeForSingleInputs(JsonValue data, ObjectMap<String, String> inputTypes, ObjectMap<String, String> outputTypes) {
    final String resultType = inputTypes.get("p0");
    final Object resultValue = createResult(resultType);
    final ObjectMap<String, PipelineNode.FieldOutput<?>> result = new ObjectMap<>();
    final DefaultFieldOutput resultOutput = new DefaultFieldOutput(resultType);
    result.put("output", resultOutput);
    return new SingleInputsPipelineNode(result) {

        @Override
        public void executeNode(PipelineRenderingContext pipelineRenderingContext, PipelineRequirementsCallback pipelineRequirementsCallback) {
            FieldOutput<?> aFunction = inputs.get("p0");
            FieldOutput<?> bFunction = inputs.get("p1");
            Object a = aFunction.getValue();
            Object b = bFunction.getValue();
            Object returnValue;
            if (a instanceof Float) {
                returnValue = Math.abs(((float) a) - ((float) b));
            } else if (a instanceof Vector2) {
                returnValue = ((Vector2) resultValue).dst((Vector2) b);
            } else if (a instanceof Vector3) {
                returnValue = ((Vector3) resultValue).dst((Vector3) b);
            } else if (a instanceof Color) {
                Color aColor = (Color) a;
                Color bColor = (Color) b;
                final float p1 = aColor.r - bColor.r;
                final float p2 = aColor.g - bColor.g;
                final float p3 = aColor.b - bColor.b;
                final float p4 = aColor.a - bColor.a;
                returnValue = (float) Math.sqrt(p1 * p1 + p2 * p2 + p3 * p3 + p4 * p4);
            } else {
                throw new IllegalArgumentException("Not matching type for function");
            }
            resultOutput.setValue(returnValue);
        }
    };
}
Also used : Color(com.badlogic.gdx.graphics.Color) Vector3(com.badlogic.gdx.math.Vector3) PipelineRenderingContext(com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext) DefaultFieldOutput(com.gempukku.libgdx.graph.pipeline.producer.node.DefaultFieldOutput) ObjectMap(com.badlogic.gdx.utils.ObjectMap) Vector2(com.badlogic.gdx.math.Vector2) DefaultFieldOutput(com.gempukku.libgdx.graph.pipeline.producer.node.DefaultFieldOutput) SingleInputsPipelineNode(com.gempukku.libgdx.graph.pipeline.producer.node.SingleInputsPipelineNode)

Aggregations

PipelineRenderingContext (com.gempukku.libgdx.graph.pipeline.producer.PipelineRenderingContext)29 ObjectMap (com.badlogic.gdx.utils.ObjectMap)26 RenderPipeline (com.gempukku.libgdx.graph.pipeline.RenderPipeline)14 RenderPipelineBuffer (com.gempukku.libgdx.graph.pipeline.RenderPipelineBuffer)13 Vector2 (com.badlogic.gdx.math.Vector2)12 DefaultFieldOutput (com.gempukku.libgdx.graph.pipeline.producer.node.DefaultFieldOutput)12 SingleInputsPipelineNode (com.gempukku.libgdx.graph.pipeline.producer.node.SingleInputsPipelineNode)10 Camera (com.badlogic.gdx.graphics.Camera)8 FullScreenRender (com.gempukku.libgdx.graph.pipeline.producer.FullScreenRender)8 Vector3 (com.badlogic.gdx.math.Vector3)6 Array (com.badlogic.gdx.utils.Array)6 Color (com.badlogic.gdx.graphics.Color)5 ShaderProgram (com.badlogic.gdx.graphics.glutils.ShaderProgram)4 JsonValue (com.badlogic.gdx.utils.JsonValue)4 OpenGLContext (com.gempukku.libgdx.graph.libgdx.context.OpenGLContext)4 ShaderContextImpl (com.gempukku.libgdx.graph.pipeline.producer.rendering.producer.ShaderContextImpl)4 RenderCallback (com.gempukku.libgdx.graph.plugin.callback.RenderCallback)4 TimeProvider (com.gempukku.libgdx.graph.time.TimeProvider)4 OrthographicCamera (com.badlogic.gdx.graphics.OrthographicCamera)3 Map (com.badlogic.gdx.maps.Map)3