Class Shader
java.lang.Object
com.iragui.objects.Shader
Represents an OpenGL shader program composed of a vertex shader and a fragment shader.
This class is responsible for loading shader source code (from a file or raw string), compiling shaders, linking them into a shader program, and providing utility methods to bind the shader and upload uniform variables.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionvoid
compile()
Compiles the vertex and fragment shaders and links them into a shader program.void
detach()
Deactivates (unbinds) any currently active shader program.int
getId()
Gets the OpenGL ID of this shader program.void
uploadFloat
(String varName, float val) Uploads a float value to a shader uniform.void
Uploads an integer value to a shader uniform.void
uploadIntArray
(String varName, int[] array) Uploads an array of integers to a shader uniform.void
uploadMat3f
(String varName, org.joml.Matrix3f mat3) Uploads a 3x3 matrix to a shader uniform.void
uploadMat4f
(String varName, org.joml.Matrix4f mat4) Uploads a 4x4 matrix to a shader uniform.void
uploadTexture
(String varName, int slot) Uploads a texture slot index to a shader uniform.void
uploadVec2f
(String varName, org.joml.Vector2f vec) Uploads a 2D vector to a shader uniform.void
uploadVec3f
(String varName, org.joml.Vector3f vec) Uploads a 3D vector to a shader uniform.void
uploadVec4f
(String varName, org.joml.Vector4f vec) Uploads a 4D vector to a shader uniform.void
use()
Activates (binds) this shader program for use.
-
Constructor Details
-
Shader
Represents an OpenGL shader program composed of a vertex shader and a fragment shader.This class is responsible for loading shader source code (from a file or raw string), compiling shaders, linking them into a shader program, and providing utility methods to bind the shader and upload uniform variables.
-
Shader
Creates a shader program by loading shader source code from a file.The file must contain both vertex and fragment shader code separated by lines starting with
#type vertex
or#type fragment
.- Parameters:
filePath
- The path to the shader file.
-
-
Method Details
-
compile
public void compile()Compiles the vertex and fragment shaders and links them into a shader program.This must be called before using the shader.
- Throws:
AssertionError
- if shader compilation or linking fails.
-
use
public void use()Activates (binds) this shader program for use.If the shader is already in use, this call does nothing.
-
detach
public void detach()Deactivates (unbinds) any currently active shader program. -
uploadMat3f
Uploads a 3x3 matrix to a shader uniform.- Parameters:
varName
- the name of the uniform variable in the shader.mat3
- theMatrix3f
to upload.
-
uploadMat4f
Uploads a 4x4 matrix to a shader uniform.- Parameters:
varName
- the name of the uniform variable in the shader.mat4
- theMatrix4f
to upload.
-
uploadVec2f
Uploads a 2D vector to a shader uniform.- Parameters:
varName
- the name of the uniform variable in the shader.vec
- theVector2f
to upload.
-
uploadVec3f
Uploads a 3D vector to a shader uniform.- Parameters:
varName
- the name of the uniform variable in the shader.vec
- theVector3f
to upload.
-
uploadVec4f
Uploads a 4D vector to a shader uniform.- Parameters:
varName
- the name of the uniform variable in the shader.vec
- theVector4f
to upload.
-
uploadFloat
Uploads a float value to a shader uniform.- Parameters:
varName
- the name of the uniform variable in the shader.val
- the float value to upload.
-
uploadInt
Uploads an integer value to a shader uniform.- Parameters:
varName
- the name of the uniform variable in the shader.val
- the integer value to upload.
-
uploadTexture
Uploads a texture slot index to a shader uniform.- Parameters:
varName
- the name of the uniform variable in the shader.slot
- the texture slot (e.g., 0 for GL_TEXTURE0).
-
uploadIntArray
Uploads an array of integers to a shader uniform.- Parameters:
varName
- the name of the uniform variable in the shader.array
- the array of integers to upload.
-
getId
public int getId()Gets the OpenGL ID of this shader program.- Returns:
- the shader program ID.
-