com.neovisionaries.android.opengl
Class Shader<TShader extends Shader<TShader>>

java.lang.Object
  extended by com.neovisionaries.android.opengl.Shader<TShader>
Direct Known Subclasses:
FragmentShader, VertexShader

public abstract class Shader<TShader extends Shader<TShader>>
extends Object

OpenGL ES shader.

 // E X A M P L E   1

 // Create a shader.
 Shader shader = new VertexShader();
 //Shader shader = new FragmentShader();

 // Set a shader source code. 
 String shaderSource = "...";
 shader.setSource(shaderSource);

 // Compile the shader source code.
 shader.compile();
 
 // E X A M P L E   2

 // Just one line.
 Shader shader = new VertexShader("...").compile();
 

Author:
Takahiko Kawasaki
See Also:
Program

Constructor Summary
protected Shader(ShaderType type)
          A constructor with a shader type.
protected Shader(ShaderType type, File file)
          A constructor with a shader type and a shader source file.
protected Shader(ShaderType type, InputStream in)
          This constructor is an alias of Shader(type, in, false).
protected Shader(ShaderType type, InputStream in, boolean close)
          A constructor with a shader type and an input stream from which a shader source should be read.
protected Shader(ShaderType type, String source)
          A constructor with a shader type and a shader source.
 
Method Summary
 TShader compile()
          Compile the source code given by setSource(String).
 TShader delete()
          Delete this shader.
 int getId()
          Get the shader ID which is a return value from glCreateShader().
 ShaderState getState()
          Get the state of this shader.
 boolean isAutoDeleted()
          Check if auto-deletion is enabled.
static void releaseCompiler()
          Tell the GLES implementation that resources used by the shader compiler can be released.
 TShader setAutoDeleted(boolean autoDeleted)
          Enable/disable auto-deletion.
 TShader setSource(File file)
          Set a shader source code.
 TShader setSource(String source)
          Set a shader source code.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Shader

protected Shader(ShaderType type)
          throws GLESException
A constructor with a shader type. After this constructor returns, the state of this instance is ShaderState.CREATED.

Parameters:
type - ShaderType.VERTEX or ShaderType.FRAGMENT.
Throws:
IllegalArgumentException - The given argument is null.
GLESException - glCreateShader() failed.
See Also:
VertexShader.VertexShader(), FragmentShader.FragmentShader(), glCreateShader

Shader

protected Shader(ShaderType type,
                 String source)
          throws GLESException
A constructor with a shader type and a shader source. After this constructor returns, the state of this instance is ShaderState.SOURCE_SET.

Parameters:
type - ShaderType.VERTEX or ShaderType.FRAGMENT.
source - A shader source code.
Throws:
IllegalArgumentException - Either or both of the arguments are null.
GLESException - glCreateShader() failed.
See Also:
VertexShader.VertexShader(String), FragmentShader.FragmentShader(String), glCreateShader, glShaderSource

Shader

protected Shader(ShaderType type,
                 File file)
          throws IOException,
                 GLESException
A constructor with a shader type and a shader source file. After this constructor returns, the state of this instance is ShaderState.SOURCE_SET.

Parameters:
type - ShaderType.VERTEX or ShaderType.FRAGMENT.
file - A file whose content is a shader source code.
Throws:
IllegalArgumentException - Either or both of the arguments are null.
IOException - Failed to read the content of the given file.
GLESException - glCreateShader() failed.
See Also:
VertexShader.VertexShader(File), FragmentShader.FragmentShader(File), glCreateShader, glShaderSource

Shader

protected Shader(ShaderType type,
                 InputStream in)
          throws IOException,
                 GLESException
This constructor is an alias of Shader(type, in, false).

Throws:
IOException
GLESException
See Also:
Shader(ShaderType, InputStream, boolean), VertexShader.VertexShader(InputStream), FragmentShader.FragmentShader(InputStream), glCreateShader, glShaderSource

Shader

protected Shader(ShaderType type,
                 InputStream in,
                 boolean close)
          throws IOException,
                 GLESException
A constructor with a shader type and an input stream from which a shader source should be read. After this constructor returns, the state of this instance is ShaderState.SOURCE_SET.

Parameters:
type - ShaderType.VERTEX or ShaderType.FRAGMENT.
in - An input stream that feeds a shader source code.
close - If true is given, the input stream is closed before this method returns.
Throws:
IllegalArgumentException - 'type' is null or 'in' is null.
IOException - Failed to read the content of the input stream.
GLESException - glCreateShader() failed.
See Also:
VertexShader.VertexShader(InputStream, boolean), FragmentShader.FragmentShader(InputStream, boolean), glCreateShader, glShaderSource
Method Detail

getId

public int getId()
Get the shader ID which is a return value from glCreateShader().

Returns:
The shader ID assigned to this shader.
See Also:
glCreateShader

getState

public ShaderState getState()
Get the state of this shader.

Returns:
The state of this shader.

delete

public TShader delete()
Delete this shader. If this shader has already been deleted, nothing is executed. After this method returns, the state of this instance is ShaderState.DELETED.

Note that calling this method detaches this shader from all the programs that this shader is currently attached to.

If auto-deletion is enabled by setAutoDeleted(true), this method is automatically called when this instance is detached from the last program that this instance is attached to.

Returns:
This Shader object.
See Also:
glDeleteShader, setAutoDeleted(boolean)

isAutoDeleted

public boolean isAutoDeleted()
Check if auto-deletion is enabled. The default value is false.

Returns:
True if auto-deletion is enabled. Otherwise, false.

setAutoDeleted

public TShader setAutoDeleted(boolean autoDeleted)
Enable/disable auto-deletion.

If 'true' is given to this method, auto-deletion is enabled, meaning that delete() is called automatically when this instance is detached from the last program that this instance was attached to.

Parameters:
autoDeleted - True to enable auto-deletion. False to disable it.
Returns:
This Shader object.

setSource

public TShader setSource(String source)
Set a shader source code. If this method returns without any execption, the state of this instance is ShaderState.SOURCE_SET.

Parameters:
source - A shader source code.
Returns:
This Shader object.
Throws:
IllegalArgumentException - The given argument is null.
IllegalStateException - This shader has already been deleted.
See Also:
glShaderSource

setSource

public TShader setSource(File file)
                                          throws IOException
Set a shader source code. If this method returns without any execption, the state of this instance is ShaderState.SOURCE_SET.

Parameters:
file - A file that contains a shader source code.
Returns:
This Shader object.
Throws:
IllegalArgumentException - The given argument is null.
IllegalStateException - This shader has already been deleted.
IOException - Failed to read the given file.
See Also:
glShaderSource

compile

public TShader compile()
                                        throws GLESException
Compile the source code given by setSource(String). If the current state of this shader is COMPILED, nothing is executed. If this method returns without any exception, the state of this instance is ShaderState.COMPILED.

Returns:
This Shader object.
Throws:
IllegalStateException - No shader source is set, or this shader has already been deleted.
GLESException - glCompileShader() failed.
See Also:
glCompileShader

releaseCompiler

public static void releaseCompiler()
Tell the GLES implementation that resources used by the shader compiler can be released.

See Also:
glReleaseShaderCompiler