Java Code Examples for com.jogamp.opengl.GL4#glBeginConditionalRender()

The following examples show how to use com.jogamp.opengl.GL4#glBeginConditionalRender() . 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_430_query_conditional.java    From jogl-samples with MIT License 4 votes vote down vote up
@Override
    protected boolean render(GL gl) {

        GL4 gl4 = (GL4) gl;

        {
            gl4.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
            ByteBuffer pointer = gl4.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, (float) windowSize.x / windowSize.y, 0.1f, 100.0f);
            Mat4 model = new Mat4(1.0f);

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

            gl4.glUnmapBuffer(GL_UNIFORM_BUFFER);
        }

        // Set the display viewport
        gl4.glViewport(0, 0, windowSize.x, windowSize.y);
        gl4.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));

        // Clear color buffer with black
        gl4.glClearBufferfv(GL_COLOR, 0, new float[]{0.0f, 0.0f, 0.0f, 1.0f}, 0);

        gl4.glBindProgramPipeline(pipelineName.get(0));
        gl4.glBindVertexArray(vertexArrayName.get(0));
        gl4.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.MATERIAL, bufferName.get(Buffer.MATERIAL), 0, Vec4.SIZE);

        // The first orange quad is not written in the framebuffer.
//        gl4.glColorMaski(0, false, false, false, false);

        // Beginning of the samples count query
        gl4.glBeginQuery(GL_ANY_SAMPLES_PASSED_CONSERVATIVE, queryName.get(0));
        {
            if (toggle) {
                // To test the condional rendering, comment this line, the next draw call won't happen.
                gl4.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);
            }
        }// End of the samples count query
        gl4.glEndQuery(GL_ANY_SAMPLES_PASSED_CONSERVATIVE);

        // The second blue quad is written in the framebuffer only if a sample pass the occlusion query.
        gl4.glColorMaski(0, true, true, true, true);

        gl4.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.MATERIAL, bufferName.get(Buffer.MATERIAL),
                uniformMaterialOffset, Vec4.SIZE);

        // Draw only if one sample went through the tests, 
        // we don't need to get the query result which prevent the rendering pipeline to stall.
        gl4.glBeginConditionalRender(queryName.get(0), GL_QUERY_WAIT);
        {
            // Clear color buffer with white
            gl4.glClearBufferfv(GL_COLOR, 0, new float[]{1.0f, 1.0f, 1.0f, 1.0f}, 0);

//            gl4.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);
        }
        gl4.glEndConditionalRender();

        toggle = !toggle;

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

    GL4 gl4 = (GL4) gl;

    {
        gl4.glBindBuffer(GL_UNIFORM_BUFFER, bufferName.get(Buffer.TRANSFORM));
        ByteBuffer pointer = gl4.glMapBufferRange(GL_UNIFORM_BUFFER,
                0, uniformTransformOffset * 2, GL_MAP_WRITE_BIT | GL_MAP_INVALIDATE_BUFFER_BIT);

        Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, (float) windowSize.x / windowSize.y, 0.1f, 10.0f);
        /**
         * A translation of Vec3(0.0f, 0.0f, 0.0f) pushes the square outside the screen, it makes no sense.
         */
        Mat4 model0 = new Mat4(1.0f).translate(new Vec3(0.0f, 0.0f, 0.0f));
        Mat4 model1 = new Mat4(1.0f);

        pointer.position(uniformTransformOffset * 0);
        pointer.asFloatBuffer().put(projection.mul_(viewMat4()).mul(model0).toFa_());
        pointer.position(uniformTransformOffset * 1);
        pointer.asFloatBuffer().put(projection.mul(viewMat4()).mul(model1).toFa_());
        pointer.rewind();

        gl4.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

    // Set the display viewport
    gl4.glViewport(0, 0, windowSize.x, windowSize.y);

    // Clear color buffer with black
    gl4.glClearBufferfv(GL_COLOR, 0, blackColor);

    gl4.glBindProgramPipeline(pipelineName.get(0));
    gl4.glBindVertexArray(vertexArrayName.get(0));
    gl4.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.MATERIAL, bufferName.get(Buffer.MATERIAL), 0,
            Vec4.SIZE);
    gl4.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM), 0,
            Mat4.SIZE);

    // The first orange quad is not written in the framebuffer.
    gl4.glColorMaski(0, false, false, false, false);

    // Beginning of the samples count query
    gl4.glBeginQuery(GL_ANY_SAMPLES_PASSED_CONSERVATIVE, queryName.get(0));
    {
        if (toggle) {
            // To test the condional rendering, comment this line, the next draw call won't happen.
            gl4.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);
            // End of the samples count query
        }
    }
    gl4.glEndQuery(GL_ANY_SAMPLES_PASSED_CONSERVATIVE);

    // The second blue quad is written in the framebuffer only if a sample pass the occlusion query.
    gl4.glColorMaski(0, true, true, true, true);

    gl4.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM),
            uniformTransformOffset, Mat4.SIZE);

    // Draw only if one sample went through the tests, 
    // we don't need to get the query result which prevent the rendering pipeline to stall.
    gl4.glBeginConditionalRender(queryName.get(0), GL_QUERY_WAIT);
    {	// Clear color buffer with white
        gl4.glClearBufferfv(GL_COLOR, 0, whiteColor);

        gl4.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.MATERIAL, bufferName.get(Buffer.MATERIAL), 0,
                Vec4.SIZE);
        gl4.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);
    }
    gl4.glEndConditionalRender();

    gl4.glBeginConditionalRender(queryName.get(0), GL_QUERY_WAIT_INVERTED);
    {	// Clear color buffer with black
        gl4.glClearBufferfv(GL_COLOR, 0, blackColor);

        gl4.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.MATERIAL, bufferName.get(Buffer.MATERIAL),
                uniformMaterialOffset, Vec4.SIZE);
        gl4.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);
    }
    gl4.glEndConditionalRender();

    toggle = !toggle;

    return true;
}