Login  Register

Order of operations for ShaderState?

Posted by Robert B on Oct 28, 2010; 11:05pm
URL: https://forum.jogamp.org/Order-of-operations-for-ShaderState-tp1789621.html

Hi all,

I'm working my way through the latest OpenGL SuperBible, where it talks about the author's GLTools and shaders. So I looked at the source of GLTools, trying to convert it to JOGL. Where I'm stuck is in the order to call ShaderState methods. I need to bind some attributes. It appears that ShaderState.glBindAttribLocation requires that the program be attached to the state, but not linked. However, ShaderState.attachShaderProgram (via ShaderProgram.glUseProgram) requires that the program be linked. So how can one both attach a program to the state and bind the attributes? Any way out of this dilemma?

Thanks,

--Rob

    public ShaderState gltLoadShaderPairSrcWithAttributes(GL2 gl, String vertexSrc, String fragmentSrc,
            HashMap<Integer, String> attributes)
    {
        ByteArrayOutputStream verboseOutBuffer = new ByteArrayOutputStream();
        PrintStream verboseOut = new PrintStream(verboseOutBuffer, true);

         // Create ShaderCode objects

        ShaderCode vertexShaderCode = new ShaderCode(GL2.GL_VERTEX_SHADER, 1,
                new String[][]{ { vertexSrc } });
        ShaderCode fragmentShaderCode = new ShaderCode(GL2.GL_FRAGMENT_SHADER, 1,
                new String[][]{ { fragmentSrc } });

        // Put them in a ShaderProgram

        ShaderProgram program = new ShaderProgram();
        program.add(vertexShaderCode);
        program.add(fragmentShaderCode);

        // Create a new ShaderState

        ShaderState shaderState = new ShaderState();

        // Link the programs here?

        if (!program.link(gl, verboseOut))
            throw new GLException("Failed to compile and link shaders: " + verboseOutBuffer.toString());

        // Attach the program to the state. Requires that the program be linked.

        shaderState.attachShaderProgram(gl, program);

        // Set some attributes. Requires that the program be attached BUT NOT LINKED???

        Set<Entry<Integer, String>> attributeSet = attributes.entrySet();
        Iterator<Entry<Integer, String>> iterator = attributeSet.iterator();
        while (iterator.hasNext())
        {
            Entry<Integer, String> entry = iterator.next();
            shaderState.glBindAttribLocation(gl, entry.getKey(), entry.getValue());
        }

        return shaderState;
}