io7m
io7m-jcanephora 0.44.0
3.15
JCGLShadersType

- 3.15.1. Description
- 3.15.2. Usage examples
- 3.15.3. State

3.15.1
Description
1
The JCGLShadersType interface provides functions for compiling and using shading language programs.
3.15.2
Usage examples
1
To compile vertex, geometry, and fragment shaders, simply pass in lists of strings, with each string terminated with CRLF or LF characters:
3.15.2.1. Compiling shaders

    JCGLShadersType g;

    List<String> v_source = new ArrayList<String>();
    v_source.add("#version 330\n");
    v_source.add("in vec4 position;\n");
    v_source.add("void\n");
    v_source.add("main (void)\n");
    v_source.add("{\n");
    v_source.add("  gl_Position = position;\n");
    v_source.add("}\n");
    JCGLVertexShaderType vs = g.shaderCompileVertex("example-v", v_source);

    List<String> g_source = new ArrayList<String>();
    g_source.add("#version 330 core\n");
    g_source.add("layout(points) in;\n");
    g_source.add("layout(points, max_vertices = 1) out;\n");
    g_source.add("void\n");
    g_source.add("main ()\n");
    g_source.add("{\n");
    g_source.add("  gl_Position = gl_in[0].gl_Position;\n");
    g_source.add("  EmitVertex();\n");
    g_source.add("  EndPrimitive();\n");
    g_source.add("}\n");
    JCGLGeometryShaderType gs = g.shaderCompileGeometry("example-g", g_source);

    List<String> f_source = new ArrayList<String>();
    f_source.add("#version 330\n");
    f_source.add("uniform vec4 color;\n");
    f_source.add("out vec4 color0;\n");
    f_source.add("void\n");
    f_source.add("main (void)\n");
    f_source.add("{\n");
    f_source.add("  color0 = color;\n");
    f_source.add("}\n");
    JCGLFragmentShaderType fs = g.shaderCompileFragment("example-f", f_source);

2
To combine (attach and link) the shaders into a working program:
3.15.2.2. Producing a program

    JCGLShadersType g;

    JCGLProgramShaderType p = g.shaderLinkProgram("program", vs, Optional.of(gs), fs);

3
The program functions interrogate the compiled program when it is created and produce a set of mappings from names to active uniforms (parameters). A read-only view of these can be accessed via the program reference, and assigned values via the provided functions:
3.15.2.3. Program uniforms

    JCGLShadersType g;

    Map<String, JCGLProgramUniformType> uniforms = p.getUniforms();

    JCGLProgramUniformType p = uniforms.get("color");
    assert p != null;
    g.programActivate(p);
    p.programUniformPutVector4f(p, new VectorI4F(1.0f, 0.0f, 0.0f, 1.0f));

3.15.3
State
1
OpenGL specifies that at most one shading program can be active on a context at any given time. Many API functions operate on the currently active program and the jcanephora API avoids changing the current binding behind the programmer's back.