go-gl / example

Example programs for the various go-gl packages.
MIT License
224 stars 60 forks source link

Cel Shader Example #83

Open shehackedyou opened 10 months ago

shehackedyou commented 10 months ago

I like how simple this project is go-gl isn't trying to be a full engine just control over gl, which is nice because I want to build a 3D scene to be streamed.

Any recommendations on how to implement custom shaders? Itd be nice to be able to switch between them, it would create a mechanic.

shehackedyou commented 10 months ago
    vertexShader, err := compileShader(vertexShaderSource, gl.VERTEX_SHADER)
    if err != nil {
        return 0, err
    }
gl.AttachShader(program, vertexShader)

# Then you can delete and presumably re-add to make the switch
gl.DeleteShader(program, vertexShader)
shehackedyou commented 10 months ago
func compileShader(source string, shaderType uint32) (uint32, error) {
    shader := gl.CreateShader(shaderType)

    csources, free := gl.Strs(source)
    gl.ShaderSource(shader, 1, csources, nil)
    free()
    gl.CompileShader(shader)

    var status int32
    gl.GetShaderiv(shader, gl.COMPILE_STATUS, &status)
    if status == gl.FALSE {
        var logLength int32
        gl.GetShaderiv(shader, gl.INFO_LOG_LENGTH, &logLength)

        log := strings.Repeat("\x00", int(logLength+1))
        gl.GetShaderInfoLog(shader, logLength, nil, gl.Str(log))

        return 0, fmt.Errorf("failed to compile %v: %v", source, log)
    }

    return shader, nil
}