com.neovisionaries.android.opengl
Class Program

java.lang.Object
  extended by com.neovisionaries.android.opengl.Program

public class Program
extends Object

OpenGL ES shader program.

 // E X A M P L E   1

 // Create a program.
 Program program = new Program();

 // Attach a vertex shader. 
 VertexShader vShader = new VertexShader("...").compile();
 program.attach(vShader);

 // Attach a fragment shader. 
 FragmentShader fShader = new FragmentShader("...").compile();
 program.attach(fShader);

 // Link the shaders.
 program.link();

 // Use the program.
 program.use();

 // Delete the program.
 program.delete();

 // Delete the shaders.
 vShader.delete();
 fShader.delete();
 
 // E X A M P L E   2

 // Create a program with shaders.
 Program program =
     new Program(
         new VertexShader("...").setAutoDeleted(true),
         new FragmentShader("...").setAutoDeleted(true)
     );

 // Use the program. Compiling and linking are performed
 // automatically because use() calls link() if not linked
 // yet and link() calls compile() of shaders if they are
 // not compiled yet.
 program.use();

 // Delete the program. Shaders are deleted automatically
 // because setAutoDeleted(true) of them has been called.
 program.delete();
 

Author:
Takahiko Kawasaki
See Also:
Shader, Attribute, Uniform

Constructor Summary
Program(Shader<?>... shaders)
          A constructor.
 
Method Summary
 Program attach(Shader<?> shader)
          Attach a shader to this program.
 void delete()
          Delete this program.
 Program detach(Shader<?> shader)
          Detach a shader from this program.
 Attribute getAttribute(String attributeName)
          Get an attribute.
 int getId()
          Get the program ID which is a return value from glCreateProgram().
 Sampler getSampler(String name)
          Get a Sampler object.
 ProgramState getState()
          Get the state of this program.
 Uniform getUniform(String name)
          Get a Uniform object.
 Program link()
          Link attached shaders.
 Program setAttribute(String attributeName, Attribute attribute)
          Set an attribute.
static void unuse()
          Call glUseProgram(0).
 Program use()
          Use this program.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

Program

public Program(Shader<?>... shaders)
        throws GLESException
A constructor. After this constructor returns, the state of this instance is NEEDS_LINKING.

Parameters:
shaders - Shaders to attach to this program. Shaders can be attached also by attach(Shader) later. Shaders that have already been deleted are not attached even if they appear in the given list.
Throws:
GLESException - glCreateProgram() failed.
See Also:
glCreateProgram, glAttachShader
Method Detail

getId

public int getId()
Get the program ID which is a return value from glCreateProgram().

Returns:
The program ID assigned to this program.
See Also:
glCreateProgram

getState

public ProgramState getState()
Get the state of this program.

Returns:
The state of this program.

delete

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

Attached shaders, if any, are detached from this program explicitly before this method returns.

See Also:
glDeleteProgram

attach

public Program attach(Shader<?> shader)
Attach a shader to this program. If this method returns without any exception, the state of this program is NEEDS_LINKING.

Parameters:
shader - A shader to attach to this program.
Returns:
This Program object.
Throws:
IllegalArgumentException -
  • The given shader is null.
  • The given shader has already been deleted.
  • The given shader has already been attached to this program.
IllegalStateException - This program has already been deleted.
See Also:
glAttachShader

detach

public Program detach(Shader<?> shader)
Detach a shader from this program. If this method returns without any exception, the state of this program is NEEDS_LINKING.

Returns:
This Program object.
Throws:
IllegalArgumentException - The given argument is null, or the shader is not attached to this program.
IllegalStateException - This program has already been deleted.
See Also:
glDetachShader

link

public Program link()
             throws GLESException
Link attached shaders. If the current state of this program is LINKED, nothing is executed. Before calling glLinkProgram(), shaders attached to this program are compiled if they have not been compiled yet. If this method returns without any exception, the state of this program is LINKED.

Returns:
This Program object.
Throws:
IllegalStateException -
  • This program has already been deleted.
  • Any source code is given to a shader that is attached to this program.
  • A shader
GLESException - glLinkProgram() or glCompileShader() failed.
See Also:
glLinkProgram

use

public Program use()
            throws GLESException
Use this program. If this program has not been linked yet, link() is executed before calling glUseProgram().

Returns:
This Program object.
Throws:
IllegalStateException - This program has already been deleted.
GLESException - Linking failed.
See Also:
glUseProgram

unuse

public static void unuse()
Call glUseProgram(0).

See Also:
glUseProgram

getUniform

public Uniform getUniform(String name)
                   throws GLESException
Get a Uniform object.

Parameters:
name - A name of a uniform variable.
Returns:
A Uniform object to manipulate the uniform variable.
Throws:
IllegalArgumentException - The argument is null.
GLESException - There is no such a uniform variable having the name.

getSampler

public Sampler getSampler(String name)
                   throws GLESException
Get a Sampler object.

Parameters:
name - A name of a uniform sampler variable.
Returns:
A Sampler object to manipulate the uniform sampler variable.
Throws:
IllegalArgumentException - The argument is null.
GLESException - There is no such a uniform variable having the name.

getAttribute

public Attribute getAttribute(String attributeName)
Get an attribute.

Parameters:
attributeName - Name of an attribute variable in this program.
Returns:
An Attribute object for the attribute variable. If an attribute variable having the name is not found in this program, null is returned.
Throws:
IllegalArgumentException - The arguments is null.
IllegalStateException - This program has already been deleted.
See Also:
glGetAttribLocation

setAttribute

public Program setAttribute(String attributeName,
                            Attribute attribute)
Set an attribute.

This method has the same effect of glBindAttribLocation(this.getId(), attribute.getIndex(), attributeName).

Parameters:
attributeName - The name of the attribute in this program.
attribute - The attribute data to set.
Returns:
This Program object.
Throws:
IllegalArgumentException - Either or both of the arguments are null.
IllegalStateException - This program has already been deleted.
See Also:
glBindAttribLocation