Package com.badlogic.gdx.graphics.g3d
Interface Shader
-
- All Superinterfaces:
Disposable
- All Known Implementing Classes:
BaseShader,DefaultShader,DepthShader,ParticleShader
public interface Shader extends Disposable
Interface which is used to render one or moreRenderables. A Shader is responsible for the actual rendering of anRenderable. Typically, when using OpenGL ES 2.0 or higher, it encapsulates aShaderProgramand takes care of all OpenGL calls necessary to render theRenderable. When using OpenGL ES 1.x it takes care of the fixed pipeline. To start rendering thebegin(Camera, RenderContext)method must be called. After which theend()method must be called to stop rendering. In between one or more calls to therender(Renderable)method can be made to render aRenderable. Therender(Renderable)method must not be called before a call tobegin(Camera, RenderContext)or after a call toend(). Each Shader needs exclusive access to the OpenGL state andRenderContextbetween thebegin(Camera, RenderContext)andend()methods, therefore only one shader can be used at a time (they must not be nested). A specific Shader instance might be (and usually is) dedicated to a specific type ofRenderable. For example it might use aShaderProgramthat is compiled with uniforms (shader input) for specificAttributetypes. Therefore thecanRender(Renderable)method can be used to check if the Shader instance can be used for a specificRenderable. Rendering aRenderableusing a Shader for whichcanRender(Renderable)returns false might result in unpredicted behavior or crash the application. To manage multiple shaders and create a new shader when required, aShaderProvidercan be used. Therefore, in practice, a specific Shader implementation is usually accompanied by a specificShaderProviderimplementation (usually extendingBaseShaderProvider). When a Shader is constructed, theinit()method must be called before it can be used. Most commonly, theinit()method compiles theShaderProgram, fetches uniform locations and performs other preparations for usage of the Shader. When the shader is no longer needed, it must disposed using theDisposable.dispose()method. This, for example, disposed (unloads for memory) the usedShaderProgram.
-
-
Method Summary
All Methods Instance Methods Abstract Methods Modifier and Type Method Description voidbegin(Camera camera, RenderContext context)Initializes the context for exclusive rendering by this shader.booleancanRender(Renderable instance)Checks whether this shader is intended to render theRenderable.intcompareTo(Shader other)Compare this shader against the other, used for sorting, light weight shaders are rendered first.voidend()Cleanup the context so other shaders can render.voidinit()Initializes the Shader, must be called before the Shader can be used.voidrender(Renderable renderable)-
Methods inherited from interface com.badlogic.gdx.utils.Disposable
dispose
-
-
-
-
Method Detail
-
init
void init()
Initializes the Shader, must be called before the Shader can be used. This typically compiles aShaderProgram, fetches uniform locations and performs other preparations for usage of the Shader.
-
compareTo
int compareTo(Shader other)
Compare this shader against the other, used for sorting, light weight shaders are rendered first.
-
canRender
boolean canRender(Renderable instance)
Checks whether this shader is intended to render theRenderable. Use this to make sure a call to therender(Renderable)method will succeed. This is expected to be a fast, non-blocking method. Note that this method will only return true if it is intended to be used. Even when it returns false the Shader might still be capable of rendering, but it's not preferred to do so.- Parameters:
instance- The renderable to check against this shader.- Returns:
- true if this shader is intended to render the
Renderable, false otherwise.
-
begin
void begin(Camera camera, RenderContext context)
Initializes the context for exclusive rendering by this shader. Use therender(Renderable)method to render aRenderable. When done rendering theend()method must be called.- Parameters:
camera- The camera to use when renderingcontext- The context to be used, which must be exclusive available for the shader until the call to theend()method.
-
render
void render(Renderable renderable)
Renders theRenderable, must be called betweenbegin(Camera, RenderContext)andend(). The Shader instance might not be able to render every type ofRenderables. Use thecanRender(Renderable)method to check if the Shader is capable of rendering a specificRenderable.- Parameters:
renderable- The renderable to render, all required fields (e.g.Renderable.materialand others) must be set. TheRenderable.shaderfield will be ignored.
-
end
void end()
Cleanup the context so other shaders can render. Must be called when done rendering using therender(Renderable)method, which must be preceded by a call tobegin(Camera, RenderContext). After a call to this method an call to therender(Renderable)method will fail until thebegin(Camera, RenderContext)is called.
-
-