Java Code Examples for com.jogamp.opengl.GL3#glUniformBlockBinding()

The following examples show how to use com.jogamp.opengl.GL3#glUniformBlockBinding() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Gl_320_fbo_shadow.java    From jogl-samples with MIT License 6 votes vote down vote up
private void renderFramebuffer(GL3 gl3) {

        gl3.glEnable(GL_DEPTH_TEST);
        gl3.glDepthFunc(GL_LESS);

        gl3.glViewport(0, 0, windowSize.x, windowSize.y);

        gl3.glBindFramebuffer(GL_FRAMEBUFFER, 0);
        gl3.glClearBufferfv(GL_DEPTH, 0, clearDepth);
        gl3.glClearBufferfv(GL_COLOR, 0, clearColor);

        gl3.glUseProgram(programName[Program.RENDER]);
        gl3.glUniform1i(uniformShadow, 0);
        gl3.glUniformBlockBinding(programName[Program.RENDER], uniformTransform[Program.RENDER],
                Semantic.Uniform.TRANSFORM0);

        gl3.glActiveTexture(GL_TEXTURE0);
        gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(0));

        gl3.glBindVertexArray(vertexArrayName.get(Program.RENDER));
        gl3.glDrawElementsInstancedBaseVertex(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1, 0);

        gl3.glDisable(GL_DEPTH_TEST);

        checkError(gl3, "renderFramebuffer");
    }
 
Example 2
Source File: Gl_320_texture_float.java    From jogl-samples with MIT License 5 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;

        if (validated) {

            ShaderCode vertShaderCode = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            ShaderCode fragShaderCode = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "frag", null, true);

            ShaderProgram shaderProgram = new ShaderProgram();
            shaderProgram.add(vertShaderCode);
            shaderProgram.add(fragShaderCode);

            shaderProgram.init(gl3);

            programName = shaderProgram.program();

            gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
            gl3.glBindAttribLocation(programName, Semantic.Attr.TEXCOORD, "texCoord");
            gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "color");

            shaderProgram.link(gl3, System.out);
        }
        if (validated) {

            gl3.glUniformBlockBinding(
                    programName,
                    gl3.glGetUniformBlockIndex(programName, "Transform"),
                    Semantic.Uniform.TRANSFORM0);

            gl3.glUseProgram(programName);
            gl3.glUniform1i(gl3.glGetUniformLocation(programName, "diffuse"), 0);
            uniformLayer = gl3.glGetUniformLocation(programName, "layer");
            gl3.glUseProgram(0);
        }

        return validated & checkError(gl3, "initProgram");
    }
 
Example 3
Source File: Gl_320_texture_streaming.java    From jogl-samples with MIT License 5 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    {
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        ByteBuffer pointer = gl3.glMapBufferRange(
                GL_UNIFORM_BUFFER, 0, Mat4.SIZE,
                GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f);
        Mat4 model = new Mat4(1.0f);
        Mat4 mvp = projection.mul(viewMat4()).mul(model);

        pointer.asFloatBuffer().put(mvp.toFa_());

        // Make sure the uniform buffer is uploaded
        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, new float[]{0.0f, 0.0f, 0.0f, 1.0f}, 0);

    gl3.glUseProgram(programName);
    gl3.glUniform1i(uniformDiffuse, 0);
    gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(0));
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));
    gl3.glBindVertexArray(vertexArrayName.get(0));

    gl3.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);

    return true;
}
 
Example 4
Source File: Gl_320_program.java    From jogl-samples with MIT License 5 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    {
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        ByteBuffer pointer = gl3.glMapBufferRange(
                GL_UNIFORM_BUFFER, 0, Mat4.SIZE,
                GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f);
        Mat4 model = new Mat4(1.0f);
        Mat4 mvp = projection.mul(viewMat4()).mul(model);

        pointer.asFloatBuffer().put(mvp.toFa_());

        // Make sure the uniform buffer is uploaded
        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, 0);
    }

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor);

    gl3.glUseProgram(programName[Program.USED]);
    gl3.glUniformBlockBinding(programName[Program.USED], uniformTransform, Semantic.Uniform.TRANSFORM0);
    gl3.glUniformBlockBinding(programName[Program.USED], uniformMaterial, Semantic.Uniform.MATERIAL);

    // Attach the buffer to UBO binding point semantic::uniform::TRANSFORM0
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));

    // Attach the buffer to UBO binding point semantic::uniform::MATERIAL
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.MATERIAL, bufferName.get(Buffer.MATERIAL));

    gl3.glBindVertexArray(vertexArrayName.get(0));
    gl3.glDrawElementsInstanced(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1);

    return true;
}
 
Example 5
Source File: Gl_320_buffer_uniform.java    From jogl-samples with MIT License 5 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;

        if (validated) {
            ShaderCode vertShader = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            ShaderCode fragShader = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null, 
                    SHADERS_SOURCE, "frag", null, true);

            ShaderProgram program = new ShaderProgram();
            program.add(vertShader);
            program.add(fragShader);

            program.init(gl3);

            programName = program.program();

            gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
            gl3.glBindAttribLocation(programName, Semantic.Attr.NORMAL, "normal");
            gl3.glBindAttribLocation(programName, Semantic.Attr.COLOR, "color");
            gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "color");

            program.link(gl3, System.out);
        }
        if (validated) {

            uniformPerDraw = gl3.glGetUniformBlockIndex(programName, "PerDraw");
            uniformPerPass = gl3.glGetUniformBlockIndex(programName, "PerPass");
            uniformPerScene = gl3.glGetUniformBlockIndex(programName, "PerScene");

            gl3.glUniformBlockBinding(programName, uniformPerDraw, Uniform.PER_DRAW);
            gl3.glUniformBlockBinding(programName, uniformPerPass, Uniform.PER_PASS);
            gl3.glUniformBlockBinding(programName, uniformPerScene, Uniform.PER_SCENE);
        }

        return validated & checkError(gl3, "initProgram");
    }
 
Example 6
Source File: Gl_320_draw_without_vertex_attrib.java    From jogl-samples with MIT License 5 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;

        // Create program
        if (validated) {

            ShaderCode vertShader = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            ShaderCode fragShader = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "frag", null, true);

            ShaderProgram program = new ShaderProgram();
            program.add(vertShader);
            program.add(fragShader);

            program.init(gl3);

            programName = program.program();

            gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "color");

            program.link(gl3, System.out);
        }
        // Get variables locations
        if (validated) {

            uniformTransform = gl3.glGetUniformBlockIndex(programName, "Transform");
            gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);
        }

        return validated & checkError(gl3, "initProgram");
    }
 
Example 7
Source File: Gl_320_draw_range_arrays.java    From jogl-samples with MIT License 5 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;

        checkError(gl3, "initProgram 0");
        // Create program
        if (validated) {
            ShaderCode vertShader = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            ShaderCode fragShader = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "frag", null, true);

            ShaderProgram program = new ShaderProgram();
            program.add(vertShader);
            program.add(fragShader);

            program.init(gl3);

            programName = program.program();

            gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
            gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "color");

            program.link(gl3, System.out);
        }
        // Get variables locations
        if (validated) {

            uniformTransform = gl3.glGetUniformBlockIndex(programName, "Transform");
            gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);
        }

        return validated & checkError(gl3, "initProgram");
    }
 
Example 8
Source File: Gl_320_draw_base_vertex.java    From jogl-samples with MIT License 5 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;
        // Create program
        if (validated) {
            ShaderCode vertShader = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            ShaderCode fragShader = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null, 
                    SHADERS_SOURCE, "frag", null, true);

            ShaderProgram program = new ShaderProgram();
            program.add(vertShader);
            program.add(fragShader);

            program.init(gl3);

            programName = program.program();

            gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
            gl3.glBindAttribLocation(programName, Semantic.Attr.COLOR, "color");
            gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "color");

            program.link(gl3, System.out);
        }
        // Get variables locations
        if (validated) {

            uniformTransform = gl3.glGetUniformBlockIndex(programName, "Transform");
            gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);
        }

        return validated & checkError(gl3, "initProgram");
    }
 
Example 9
Source File: Gl_320_draw_multiple.java    From jogl-samples with MIT License 5 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;

        if (validated) {

            ShaderCode vertShader = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            ShaderCode fragShader = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "frag", null, true);

            ShaderProgram program = new ShaderProgram();
            program.add(vertShader);
            program.add(fragShader);
            program.init(gl3);

            programName = program.program();

            gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
            gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "color");

            program.link(gl3, System.out);
        }
        if (validated) {

            uniformTransform = gl3.glGetUniformBlockIndex(programName, "Transform");
            gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);
        }
        return validated & checkError(gl3, "initProgram");
    }
 
Example 10
Source File: Gl_320_glsl_builtin_blocks.java    From jogl-samples with MIT License 5 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    {
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        ByteBuffer pointer = gl3.glMapBufferRange(
                GL_UNIFORM_BUFFER, 0, Mat4.SIZE,
                GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f);
        Mat4 model = new Mat4(1.0f);

        pointer.asFloatBuffer().put(projection.mul(viewMat4()).mul(model).toFa_());

        // Make sure the uniform buffer is uploaded
        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor);

    gl3.glUseProgram(programName);
    gl3.glUniform1i(uniformDiffuse, 0);
    gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(0));
    gl3.glBindVertexArray(vertexArrayName.get(0));
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));

    gl3.glDrawElementsInstancedBaseVertex(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1, 0);

    return true;
}
 
Example 11
Source File: Gl_320_glsl_input_struct.java    From jogl-samples with MIT License 5 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    {
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        ByteBuffer pointer = gl3.glMapBufferRange(
                GL_UNIFORM_BUFFER, 0, Mat4.SIZE,
                GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f);
        Mat4 model = new Mat4(1.0f);

        pointer.asFloatBuffer().put(projection.mul(viewMat4()).mul(model).toFa_());

        // Make sure the uniform buffer is uploaded
        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor);

    gl3.glUseProgram(programName);
    gl3.glUniform1i(uniformDiffuse, 0);
    gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(0));
    gl3.glBindVertexArray(vertexArrayName.get(0));
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));

    gl3.glDrawElementsInstancedBaseVertex(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1, 0);

    return true;
}
 
Example 12
Source File: Gl_320_texture_pixel_store.java    From jogl-samples with MIT License 5 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    {
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        ByteBuffer pointer = gl3.glMapBufferRange(
                GL_UNIFORM_BUFFER, 0, Mat4.SIZE,
                GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f);
        Mat4 model = new Mat4(1.0f);
        Mat4 mvp = projection.mul(viewMat4()).mul(model);

        pointer.asFloatBuffer().put(mvp.toFa_());

        // Make sure the uniform buffer is uploaded
        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    clearColor.put(new float[]{1.0f, 0.5f, 0.0f, 1.0f}).rewind();
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor);

    gl3.glUseProgram(programName);
    gl3.glUniform1i(uniformDiffuse, 0);
    gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(0));
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));
    gl3.glBindVertexArray(vertexArrayName.get(0));

    gl3.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);

    return true;
}
 
Example 13
Source File: Gl_320_query_conditional.java    From jogl-samples with MIT License 5 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;

        if (validated) {

            ShaderCode vertShaderCode = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            ShaderCode fragShaderCode = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "frag", null, true);

            ShaderProgram shaderProgram = new ShaderProgram();
            shaderProgram.add(vertShaderCode);
            shaderProgram.add(fragShaderCode);

            shaderProgram.init(gl3);

            programName = shaderProgram.program();

            gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
            gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "color");

            shaderProgram.link(gl3, System.out);
        }
        if (validated) {

            gl3.glUniformBlockBinding(
                    programName,
                    gl3.glGetUniformBlockIndex(programName, "Transform"),
                    Semantic.Uniform.TRANSFORM0);
            gl3.glUniformBlockBinding(
                    programName,
                    gl3.glGetUniformBlockIndex(programName, "Material"),
                    Semantic.Uniform.MATERIAL);
        }

        return validated & checkError(gl3, "initProgram");
    }
 
Example 14
Source File: Gl_320_buffer_uniform_shared.java    From jogl-samples with MIT License 4 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;
    {
        // Compute the MVP (Model View Projection matrix)
        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, (float) windowSize.x / windowSize.y, .1f, 100f);
        Mat4 model = new Mat4(1.0f);
        Mat4 mvp = projection.mul(viewMat4()).mul(model);

        float[] diffuse = new float[]{1f, .5f, 0f, 1f};

        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.UNIFORM));
        ByteBuffer pointer = gl3.glMapBufferRange(GL_UNIFORM_BUFFER, 0,
                uniformBlockSizeTransform.get(0) + Vec4.SIZE,
                GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        pointer.asFloatBuffer().put(mvp.toFa_());
        pointer.position(uniformBlockSizeTransform.get(0));
        pointer.asFloatBuffer().put(diffuse);
        pointer.rewind();

        // Make sure the uniform buffer is uploaded
        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, 0);
    }

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, new float[]{0.0f, 0.0f, 0.0f, 1.0f}, 0);

    gl3.glUseProgram(programName);
    gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);
    gl3.glUniformBlockBinding(programName, uniformMaterial, Semantic.Uniform.MATERIAL);

    // Attach the buffer to UBO binding point semantic::uniform::TRANSFORM0
    gl3.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.UNIFORM), 0,
            uniformBlockSizeTransform.get(0));
    // Attach the buffer to UBO binding point semantic::uniform::MATERIAL 
    gl3.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.MATERIAL, bufferName.get(Buffer.UNIFORM),
            uniformBlockSizeTransform.get(0), uniformBlockSizeMaterial.get(0));

    // Bind vertex array & draw 
    gl3.glBindVertexArray(vertexArrayName.get(0));
    gl3.glDrawElementsInstancedBaseVertex(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1, 0);
    return true;
}
 
Example 15
Source File: Gl_320_texture_2d.java    From jogl-samples with MIT License 4 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;

        ShaderCode[] shaderCodes = new ShaderCode[Shader.MAX];

        if (validated) {

            shaderCodes[Shader.VERT] = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            shaderCodes[Shader.FRAG] = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "frag", null, true);

            ShaderProgram shaderProgram = new ShaderProgram();
            shaderProgram.add(shaderCodes[Shader.VERT]);
            shaderProgram.add(shaderCodes[Shader.FRAG]);

            shaderProgram.init(gl3);

            programName = shaderProgram.program();

            gl3.glBindAttribLocation(programName, Semantic.Attr.POSITION, "position");
            gl3.glBindAttribLocation(programName, Semantic.Attr.TEXCOORD, "texCoord");
            gl3.glBindFragDataLocation(programName, Semantic.Frag.COLOR, "color");

            shaderProgram.link(gl3, System.out);
        }
        if (validated) {

            gl3.glUniformBlockBinding(
                    programName,
                    gl3.glGetUniformBlockIndex(programName, "Transform"),
                    Semantic.Uniform.TRANSFORM0);

            gl3.glUseProgram(programName);
            gl3.glUniform1i(gl3.glGetUniformLocation(programName, "diffuse"), 0);
            gl3.glUseProgram(0);
        }

        return validated & checkError(gl3, "initProgram");
    }
 
Example 16
Source File: Gl_320_program_uniform.java    From jogl-samples with MIT License 4 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    {
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        ByteBuffer pointer = gl3.glMapBufferRange(
                GL_UNIFORM_BUFFER, 0, Mat4.SIZE,
                GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.1f, 100.0f);
        Mat4 model = new Mat4(1.0f);
        Mat4 mvp = projection.mul(viewMat4()).mul(model);

        pointer.asFloatBuffer().put(mvp.toFa_());

        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, 0);
    }

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor);

    gl3.glUseProgram(programName);
    gl3.glUniformBlockBinding(programName, uniformTransform, Semantic.Uniform.TRANSFORM0);

    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));

    diffuse.put(new float[]{1.0f, 0.5f, 0.0f, 1.0f,/**/ 0.7f, 0.7f, 0.7f, 1.0f}).rewind();

    diffuse.position(0);
    gl3.glUniform4fv(uniformDiffuse0, 1, diffuse);
    diffuse.position(4);
    gl3.glUniform4fv(uniformDiffuse1, 1, diffuse);
    diffuse.rewind();
    //glUniform4fv(UniformDiffuse + 0, 1, &Diffuse[0][0]);
    //glUniform4fv(UniformDiffuse + 1, 1, &Diffuse[1][0]);

    gl3.glBindVertexArray(vertexArrayName.get(0));
    gl3.glDrawElementsInstanced(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1);

    return true;
}
 
Example 17
Source File: Gl_320_fbo_depth.java    From jogl-samples with MIT License 4 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    {
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        ByteBuffer pointer = gl3.glMapBufferRange(
                GL_UNIFORM_BUFFER, 0, Mat4.SIZE,
                GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.5f, 8.0f);
        Mat4 model = new Mat4(1.0f).scale(new Vec3(5.0f));
        pointer.asFloatBuffer().put(projection.mul(viewMat4()).mul(model).toFa_());

        // Make sure the uniform buffer is uploaded
        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

    gl3.glEnable(GL_DEPTH_TEST);
    gl3.glDepthFunc(GL_LESS);

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);

    gl3.glBindFramebuffer(GL_FRAMEBUFFER, framebufferName.get(0));
    float[] depth = {1.0f};
    gl3.glClearBufferfv(GL_DEPTH, 0, depth, 0);

    // Bind rendering objects
    gl3.glUseProgram(programName[Program.TEXTURE]);
    gl3.glUniformBlockBinding(programName[Program.TEXTURE], uniformTransform, Semantic.Uniform.TRANSFORM0);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(Texture.DIFFUSE));
    gl3.glBindVertexArray(vertexArrayName.get(Program.TEXTURE));
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));

    gl3.glDrawElementsInstancedBaseVertex(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1, 0);

    gl3.glDisable(GL_DEPTH_TEST);

    gl3.glBindFramebuffer(GL_FRAMEBUFFER, 0);
    gl3.glUseProgram(programName[Program.SPLASH]);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindVertexArray(vertexArrayName.get(Program.SPLASH));
    gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(Texture.RENDERBUFFER));

    gl3.glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1);

    return true;
}
 
Example 18
Source File: Gl_320_fbo_depth_multisample.java    From jogl-samples with MIT License 4 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    {
        gl3.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        ByteBuffer pointer = gl3.glMapBufferRange(
                GL_UNIFORM_BUFFER, 0, Mat4.SIZE,
                GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        //glm::mat4 Projection = glm::perspectiveFov(glm::pi<float>() * 0.25f, 640.f, 480.f, 0.1f, 100.0f);
        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, 4.0f / 3.0f, 0.1f, 8.0f);
        Mat4 model = new Mat4(1.0f).scale(new Vec3(5.0f));

        pointer.asFloatBuffer().put(projection.mul(viewMat4()).mul(model).toFa_());

        // Make sure the uniform buffer is uploaded
        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

    gl3.glEnable(GL_DEPTH_TEST);
    gl3.glDepthFunc(GL_LESS);

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);

    gl3.glBindFramebuffer(GL_FRAMEBUFFER, framebufferName.get(Framebuffer.DEPTH_MULTISAMPLE));
    gl3.glClearBufferfv(GL_DEPTH, 0, clearDepth);

    // Bind rendering objects
    gl3.glUseProgram(programName[Program.TEXTURE]);
    gl3.glUniformBlockBinding(programName[Program.TEXTURE],
            uniformTransform, Semantic.Uniform.TRANSFORM0);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_2D, textureName.get(Texture.DIFFUSE));
    gl3.glBindVertexArray(vertexArrayName.get(Program.TEXTURE));
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));

    gl3.glDrawElementsInstancedBaseVertex(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 2, 0);

    // Pass 2
    gl3.glDisable(GL_DEPTH_TEST);

    gl3.glBindFramebuffer(GL_FRAMEBUFFER, 0);
    gl3.glUseProgram(programName[Program.SPLASH]);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindVertexArray(vertexArrayName.get(Program.SPLASH));
    gl3.glBindTexture(GL_TEXTURE_2D_MULTISAMPLE, textureName.get(Texture.MULTISAMPLE));

    gl3.glDrawArraysInstanced(GL_TRIANGLES, 0, 3, 1);

    return true;
}
 
Example 19
Source File: Gl_330_query_occlusion.java    From jogl-samples with MIT License 3 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;

        if (validated) {

            ShaderProgram shaderProgram = new ShaderProgram();

            ShaderCode vertShaderCode = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            ShaderCode fragShaderCode = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "frag", null, true);

            shaderProgram.init(gl3);

            shaderProgram.add(vertShaderCode);
            shaderProgram.add(fragShaderCode);

            programName = shaderProgram.program();

            shaderProgram.link(gl3, System.out);
        }

        // Get variables locations
        if (validated) {

            gl3.glUniformBlockBinding(programName, gl3.glGetUniformBlockIndex(programName, "Transform"),
                    Semantic.Uniform.TRANSFORM0);
        }

        return validated & checkError(gl3, "initProgram");
    }
 
Example 20
Source File: Gl_330_query_conditional.java    From jogl-samples with MIT License 3 votes vote down vote up
private boolean initProgram(GL3 gl3) {

        boolean validated = true;

        if (validated) {

            ShaderProgram shaderProgram = new ShaderProgram();

            ShaderCode vertShaderCode = ShaderCode.create(gl3, GL_VERTEX_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "vert", null, true);
            ShaderCode fragShaderCode = ShaderCode.create(gl3, GL_FRAGMENT_SHADER, this.getClass(), SHADERS_ROOT, null,
                    SHADERS_SOURCE, "frag", null, true);

            shaderProgram.init(gl3);

            shaderProgram.add(vertShaderCode);
            shaderProgram.add(fragShaderCode);

            programName = shaderProgram.program();

            shaderProgram.link(gl3, System.out);
        }

        // Get variables locations
        if (validated) {

            gl3.glUniformBlockBinding(programName, gl3.glGetUniformBlockIndex(programName, "Transform"),
                    Semantic.Uniform.TRANSFORM0);
            gl3.glUniformBlockBinding(programName, gl3.glGetUniformBlockIndex(programName, "Material"),
                    Semantic.Uniform.MATERIAL);
        }

        return validated & checkError(gl3, "initProgram");
    }