Class Shader

java.lang.Object
com.iragui.objects.Shader

public class Shader extends Object
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
    Constructor
    Description
    Shader(String filePath)
    Creates a shader program by loading shader source code from a file.
    Shader(String source, boolean isString)
    Represents an OpenGL shader program composed of a vertex shader and a fragment shader.
  • Method Summary

    Modifier and Type
    Method
    Description
    void
    Compiles the vertex and fragment shaders and links them into a shader program.
    void
    Deactivates (unbinds) any currently active shader program.
    int
    Gets the OpenGL ID of this shader program.
    void
    uploadFloat(String varName, float val)
    Uploads a float value to a shader uniform.
    void
    uploadInt(String varName, int val)
    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.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Constructor Details

    • Shader

      public Shader(String source, boolean isString)
      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

      public Shader(String filePath)
      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

      public void uploadMat3f(String varName, org.joml.Matrix3f mat3)
      Uploads a 3x3 matrix to a shader uniform.
      Parameters:
      varName - the name of the uniform variable in the shader.
      mat3 - the Matrix3f to upload.
    • uploadMat4f

      public void uploadMat4f(String varName, org.joml.Matrix4f mat4)
      Uploads a 4x4 matrix to a shader uniform.
      Parameters:
      varName - the name of the uniform variable in the shader.
      mat4 - the Matrix4f to upload.
    • uploadVec2f

      public void uploadVec2f(String varName, org.joml.Vector2f vec)
      Uploads a 2D vector to a shader uniform.
      Parameters:
      varName - the name of the uniform variable in the shader.
      vec - the Vector2f to upload.
    • uploadVec3f

      public void uploadVec3f(String varName, org.joml.Vector3f vec)
      Uploads a 3D vector to a shader uniform.
      Parameters:
      varName - the name of the uniform variable in the shader.
      vec - the Vector3f to upload.
    • uploadVec4f

      public void uploadVec4f(String varName, org.joml.Vector4f vec)
      Uploads a 4D vector to a shader uniform.
      Parameters:
      varName - the name of the uniform variable in the shader.
      vec - the Vector4f to upload.
    • uploadFloat

      public void uploadFloat(String varName, float val)
      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

      public void uploadInt(String varName, int val)
      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

      public void uploadTexture(String varName, int slot)
      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

      public void uploadIntArray(String varName, int[] array)
      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.