Package rosequartz.gfx
Class ShaderProgram
java.lang.Object
rosequartz.gfx.ShaderProgram
Represents a shader program, consisting of a vertex shader and a fragment shader.
Each shader may be made from a resource or a string containing the GLSL source code.
Shader source code must be written for "
OpenGL Wiki on GLSL
Every shader contains additional functions provided by rosequartz:
#version 300" (GLSL ES & OpenGL ES 3.0),
so using #version {version} or precision {precision} {type}; inside the shader source code is illegal.
OpenGL Wiki on GLSL
Every shader contains additional functions provided by rosequartz:
float getLightDiffuse(vec3 light_position, vec3 position, vec3 normal)
float getLightDiffuse(float ambient, vec3 light_position, vec3 position, vec3 normal)
float getLightDiffuse(vec3 light_position, float light_strength, vec3 position, vec3 normal)
float getLightDiffuse(float ambient, vec3 light_position, float light_strength, vec3 position, vec3 normal)
float random(vec2 xy, float seed) { return fract(tan(distance(xy * PHI, xy) * seed) * xy.x); }
-
Constructor Summary
ConstructorsConstructorDescriptionShaderProgram(String vertexShaderSrc, String fragmentShaderSrc)Creates a ShaderProgram from the vertex shader source code and the fragment shader source code.ShaderProgram(String vertexShaderSrc, Resource fragmentShader)Creates a ShaderProgram from the vertex shader source code and a fragment shader resource.ShaderProgram(Resource vertexShader, String fragmentShaderSrc)Creates a ShaderProgram from a vertex shader resource and the fragment shader source code.ShaderProgram(Resource vertexShader, Resource fragmentShader)Creates a ShaderProgram from a vertex shader resource and a fragment shader resource. -
Method Summary
Modifier and TypeMethodDescriptiondeselect()Uses no ShaderProgram from now on.static ShaderProgramGets the currently used shader program (or null if none is used).select()Uses this ShaderProgram when rendering from now on.setUniformFloat(String name, float value)Sets a uniform (type "float") with the given name and float value.setUniformFloat(String name, float[] value, int length)Sets a uniform (type "float[‹‹array_size››]") with the given name and float array value.setUniformInt(String name, int value)Sets a uniform (type "int") with the given name and integer value.setUniformInt(String name, int[] value, int length)Sets a uniform (type "int[‹‹array_size››]") with the given name and integer array value.setUniformIntVec(String name, int[] values)Sets a uniform (type "ivec‹‹vec_size››") with the given name and integer vector values array.setUniformIntVec(String name, int[][] values, int length)Sets a uniform (type "ivec‹‹vec_size››[‹‹array_size››]") with the given name and array of integer vector value arrays.setUniformMatrix4(String name, Mat4 mat)Sets a uniform (type "mat4") with the given name and 4x4 matrix value.setUniformMatrix4(String name, Mat4[] mats, int length)Sets a uniform (type "mat4[‹‹array_size››]") with the given name and 4x4 float matrix array value.setUniformTexture(String name, Texture texture)Sets a uniform (type sampler2D) with the given name and texture value.setUniformVec(String name, float[] values)Sets a uniform (type: "vec‹‹vec_size››") with the given name and float vector values array.setUniformVec(String name, float[][] values, int length)Sets a uniform (type: "vec‹‹vec_size››[‹‹array_size››]") with the given name and array of float vector value arrays.setUniformVec(String name, Vec2 vec)Sets a uniform (type: "vec2") with the given name and Vector2.setUniformVec(String name, Vec2[] vecs, int length)Sets a uniform (type: "vec2[‹‹array_size››]") with the given name and array of Vector2.setUniformVec(String name, Vec3 vec)Sets a uniform (type: "vec3") with the given name and Vector3.setUniformVec(String name, Vec3[] vecs, int length)Sets a uniform (type: "vec3[‹‹array_size››]") with the given name and array of Vector3.setUniformVec(String name, Vec4 vec)Sets a uniform (type: "vec4") with the given name and Vector4.setUniformVec(String name, Vec4[] vecs, int length)Sets a uniform (type: "vec4[‹‹array_size››]") with the given name and array of Vector4.toString()
-
Constructor Details
-
ShaderProgram
public ShaderProgram(Resource vertexShader, Resource fragmentShader) throws NotOnGraphicsThreadExceptionCreates a ShaderProgram from a vertex shader resource and a fragment shader resource.- Parameters:
vertexShader- the resource (text file) of the vertex shaderfragmentShader- the resource (text file) of the fragment shader- Throws:
NotOnGraphicsThreadException
-
ShaderProgram
public ShaderProgram(String vertexShaderSrc, Resource fragmentShader) throws NotOnGraphicsThreadExceptionCreates a ShaderProgram from the vertex shader source code and a fragment shader resource.- Parameters:
vertexShaderSrc- the source code of the vertex shaderfragmentShader- the resource (text file) of the fragment shader- Throws:
NotOnGraphicsThreadException
-
ShaderProgram
public ShaderProgram(Resource vertexShader, String fragmentShaderSrc) throws NotOnGraphicsThreadExceptionCreates a ShaderProgram from a vertex shader resource and the fragment shader source code.- Parameters:
vertexShader- the resource (text file) of the vertex shaderfragmentShaderSrc- the source code of the fragment shader- Throws:
NotOnGraphicsThreadException
-
ShaderProgram
public ShaderProgram(String vertexShaderSrc, String fragmentShaderSrc) throws NotOnGraphicsThreadExceptionCreates a ShaderProgram from the vertex shader source code and the fragment shader source code.- Parameters:
vertexShaderSrc- the source code of the vertex shaderfragmentShaderSrc- the source code of the fragment shader- Throws:
NotOnGraphicsThreadException
-
-
Method Details
-
getCurrent
Gets the currently used shader program (or null if none is used).- Returns:
- currently used shader program (or null if none used)
-
select
Uses this ShaderProgram when rendering from now on.- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
deselect
Uses no ShaderProgram from now on. A new ShaderProgram has to be selected before rendering the next time.- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformTexture
public ShaderProgram setUniformTexture(String name, Texture texture) throws NotOnGraphicsThreadExceptionSets a uniform (type sampler2D) with the given name and texture value.- Parameters:
name- the name of the uniform to settexture- the texture of the uniform- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformFloat
Sets a uniform (type "float") with the given name and float value.- Parameters:
name- the name of the uniform to setvalue- the float value of the uniform- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformFloat
public ShaderProgram setUniformFloat(String name, float[] value, int length) throws NotOnGraphicsThreadExceptionSets a uniform (type "float[‹‹array_size››]") with the given name and float array value.- Parameters:
name- the name of the uniform to setvalue- the float value of the uniformlength- length of the array as defined in the shader program- Returns:
- this
- Throws:
IllegalArgumentException- if length of array data is larger than given lengthNotOnGraphicsThreadException
-
setUniformVec
public ShaderProgram setUniformVec(String name, float[] values) throws NotOnGraphicsThreadExceptionSets a uniform (type: "vec‹‹vec_size››") with the given name and float vector values array.- Parameters:
name- the name of the uniform to setvalues- the vector values of the uniform- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformVec
Sets a uniform (type: "vec2") with the given name and Vector2.- Parameters:
name- the name of the uniform to setvec- the vector of the uniform- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformVec
Sets a uniform (type: "vec3") with the given name and Vector3.- Parameters:
name- the name of the uniform to setvec- the vector of the uniform- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformVec
Sets a uniform (type: "vec4") with the given name and Vector4.- Parameters:
name- the name of the uniform to setvec- the vector of the uniform- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformVec
public ShaderProgram setUniformVec(String name, float[][] values, int length) throws NotOnGraphicsThreadExceptionSets a uniform (type: "vec‹‹vec_size››[‹‹array_size››]") with the given name and array of float vector value arrays.- Parameters:
name- the name of the uniform to setvalues- the vector values of the uniformlength- length of the array as defined in the shader program- Returns:
- this
- Throws:
IllegalArgumentException- if length of array data is larger than given lengthNotOnGraphicsThreadException
-
setUniformVec
public ShaderProgram setUniformVec(String name, Vec2[] vecs, int length) throws NotOnGraphicsThreadExceptionSets a uniform (type: "vec2[‹‹array_size››]") with the given name and array of Vector2.- Parameters:
name- the name of the uniform to setvecs- the array vectors of the uniformlength- length of the array as defined in the shader program- Returns:
- this
- Throws:
IllegalArgumentException- if length of array data is larger than given lengthNotOnGraphicsThreadException
-
setUniformVec
public ShaderProgram setUniformVec(String name, Vec3[] vecs, int length) throws NotOnGraphicsThreadExceptionSets a uniform (type: "vec3[‹‹array_size››]") with the given name and array of Vector3.- Parameters:
name- the name of the uniform to setvecs- the array of vectors of the uniformlength- length of the array as defined in the shader program- Returns:
- this
- Throws:
IllegalArgumentException- if length of array data is larger than given lengthNotOnGraphicsThreadException
-
setUniformVec
public ShaderProgram setUniformVec(String name, Vec4[] vecs, int length) throws NotOnGraphicsThreadExceptionSets a uniform (type: "vec4[‹‹array_size››]") with the given name and array of Vector4.- Parameters:
name- the name of the uniform to setvecs- the array of vectors of the uniformlength- length of the array as defined in the shader program- Returns:
- this
- Throws:
IllegalArgumentException- if length of array data is larger than given lengthNotOnGraphicsThreadException
-
setUniformInt
Sets a uniform (type "int") with the given name and integer value.- Parameters:
name- the name of the uniform to setvalue- the integer value of the uniform- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformInt
public ShaderProgram setUniformInt(String name, int[] value, int length) throws NotOnGraphicsThreadExceptionSets a uniform (type "int[‹‹array_size››]") with the given name and integer array value.- Parameters:
name- the name of the uniform to setvalue- the integer value of the uniformlength- length of the array as defined in the shader program- Returns:
- this
- Throws:
IllegalArgumentException- if length of array data is larger than given lengthNotOnGraphicsThreadException
-
setUniformIntVec
public ShaderProgram setUniformIntVec(String name, int[] values) throws NotOnGraphicsThreadExceptionSets a uniform (type "ivec‹‹vec_size››") with the given name and integer vector values array.- Parameters:
name- the name of the uniform to setvalues- the integer vector values of the uniform- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformIntVec
public ShaderProgram setUniformIntVec(String name, int[][] values, int length) throws NotOnGraphicsThreadExceptionSets a uniform (type "ivec‹‹vec_size››[‹‹array_size››]") with the given name and array of integer vector value arrays.- Parameters:
name- the name of the uniform to setvalues- the integer vector values of the uniformlength- length of the array as defined in the shader program- Returns:
- this
- Throws:
IllegalArgumentException- if length of array data is larger than given lengthNotOnGraphicsThreadException
-
setUniformMatrix4
Sets a uniform (type "mat4") with the given name and 4x4 matrix value.- Parameters:
name- the name of the uniform to setmat- the 4x4 matrix of the uniform- Returns:
- this
- Throws:
NotOnGraphicsThreadException
-
setUniformMatrix4
public ShaderProgram setUniformMatrix4(String name, Mat4[] mats, int length) throws NotOnGraphicsThreadExceptionSets a uniform (type "mat4[‹‹array_size››]") with the given name and 4x4 float matrix array value.- Parameters:
name- the name of the uniform to setmats- the 4x4 matrices of the uniformlength- length of the array as defined in the shader program- Returns:
- this
- Throws:
IllegalArgumentException- if length of array data is larger than given lengthNotOnGraphicsThreadException
-
toString
-