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

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

    GL3 gl3 = (GL3) gl;

    clearColor.put(new float[]{1.0f, 1.0f, 1.0f, 1.0f}).rewind();
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor);

    for (int y = 0; y < 4; ++y) {
        for (int x = 0; x < 4; ++x) {

            gl3.glViewport(x * windowSize.x / 4, y * windowSize.y / 4, windowSize.x / 4, windowSize.y / 4);
            gl3.glUniform1i(uniformLayer, x + y * 4);
            gl3.glDrawElementsInstancedBaseVertex(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1, 0);
        }
    }

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

    GL3 gl3 = (GL3) gl;

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

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearColor(1.0f, 0.5f, 0.0f, 1.0f);
    gl3.glClear(GL_COLOR_BUFFER_BIT);

    gl3.glUseProgram(programName);
    gl3.glUniform1i(uniformDiffuse, 0);
    gl3.glUniformMatrix4fv(uniformMvp, 1, false, mvp.toFa_(), 0);

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

    gl3.glBindVertexArray(vertexArrayName.get(0));

    gl3.glDrawArrays(GL_TRIANGLES, 0, vertexCount);

    return true;
}
 
Example 3
Source File: Gl_320_fbo_multisample_integer.java    From jogl-samples with MIT License 6 votes vote down vote up
private void renderFB(GL3 gl3, int textureName) {

        Mat4 perspective = glm.perspective_((float) Math.PI * 0.25f, (float) windowSize.x / windowSize.y, 0.1f, 100.0f);
        Mat4 model = new Mat4(1.0f);
        Mat4 mvp = perspective.mul(viewMat4()).mul(model);

        gl3.glUseProgram(programName[Program.SPLASH]);
        gl3.glUniform1i(uniformDiffuse[Program.SPLASH], 0);
        gl3.glUniformMatrix4fv(uniformMvp[Program.SPLASH], 1, false, mvp.toFa_(), 0);

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

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

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

        checkError(gl3, "renderFB");
    }
 
Example 4
Source File: Gl_330_texture_rect.java    From jogl-samples with MIT License 6 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    Mat4 projection = glm.ortho_(-1.0f, 1.0f, -1.0f, 1.0f, 1.0f, -1.0f);
    Mat4 model = new Mat4(1.0f);
    Mat4 mvp = projection.mul(new Mat4(1.0f)).mul(model);

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 0).put(1, 0).put(2, 0).put(3, 0));

    // Bind the program for use
    gl3.glUseProgram(programName);
    gl3.glUniform1i(uniformDiffuse, 0);
    gl3.glUniformMatrix4fv(uniformMvp, 1, false, mvp.toFa_(), 0);

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_RECTANGLE, textureRectName.get(0));

    gl3.glBindVertexArray(vertexArrayName.get(0));
    gl3.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);

    return true;
}
 
Example 5
Source File: NBodyVisualizer.java    From gpu-nbody with MIT License 5 votes vote down vote up
/**
 * Set up a default view for the given GLAutoDrawable
 * 
 * @param drawable
 *            The GLAutoDrawable to set the view for
 */
private void setupView(final GLAutoDrawable drawable) {
	final GL3 gl = (GL3) drawable.getGL();

	gl.glViewport(0, 0, drawable.getSurfaceWidth(), drawable.getSurfaceHeight());

	final float aspect = (float) drawable.getSurfaceWidth() / drawable.getSurfaceHeight();
	projectionMatrix = perspective(50, aspect, 0.1f, 400.0f);
}
 
Example 6
Source File: GLRenderer.java    From constellation with Apache License 2.0 5 votes vote down vote up
@Override
public void reshape(final GLAutoDrawable drawable, final int x, final int y, final int width, final int height) {
    final GL3 gl = drawable.getGL().getGL3();

    //  Windows-DPI-Scaling
    //
    // If JOGL is ever fixed or another solution is found, either change
    // needsManualDPIScaling to return false (so there is effectively no
    // DPI scaling here) or remove the scaled height and width below.         
    float dpiScaleX = 1.0f;
    float dpiScaleY = 1.0f;
    if (GLTools.needsManualDPIScaling()) {
        dpiScaleX = (float) ((Graphics2D) (parent.canvas).getGraphics()).getTransform().getScaleX();
        dpiScaleY = (float) ((Graphics2D) (parent.canvas).getGraphics()).getTransform().getScaleY();
    }

    // These need to be final as they are used in the lambda function below
    final int dpiScaledWidth = (int) (width * dpiScaleX);
    final int dpiScaledHeight = (int) (height * dpiScaleY);

    gl.glViewport(0, 0, dpiScaledWidth, dpiScaledHeight);

    // Create the projection matrix, and load it on the projection matrix stack.
    viewFrustum.setPerspective(FIELD_OF_VIEW, (float) dpiScaledWidth / (float) dpiScaledHeight, PERSPECTIVE_NEAR, PERSPECTIVE_FAR);

    projectionMatrix.set(viewFrustum.getProjectionMatrix());

    // A GLCanvas sets its minimum size to the preferred size when its redrawn. This means it will get bigger,
    // but never get smaller. Explicitly set the minimum size to get around this.
    ((Component) drawable).setMinimumSize(new Dimension(0, 0));

    renderables.forEach(renderable -> {
        renderable.reshape(x, y, dpiScaledWidth, dpiScaledHeight);
    });

    viewport[0] = x;
    viewport[1] = y;
    viewport[2] = dpiScaledWidth;
    viewport[3] = dpiScaledHeight;
}
 
Example 7
Source File: Gl_330_texture_swizzle.java    From jogl-samples with MIT License 5 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    Mat4 projection = glm.perspective_((float) Math.PI * 0.25f, (float) windowSize.x / windowSize.y, 0.1f, 1000.0f);
    Mat4 model = new Mat4(1.0f);
    Mat4 mvp = projection.mul(viewMat4()).mul(model);

    gl3.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 1).put(1, .5f).put(2, 0).put(3, 1));

    gl3.glUseProgram(programName);
    gl3.glUniformMatrix4fv(uniformMvp, 1, false, mvp.toFa_(), 0);
    gl3.glUniform1i(uniformDiffuse, 0);

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

    gl3.glBindVertexArray(vertexArrayName.get(0));

    for (int index = 0; index < Viewport.MAX; ++index) {
        gl3.glViewport(viewport[index].x, viewport[index].y, viewport[index].z, viewport[index].w);

        gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_R, swizzleR[index]);
        gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_G, swizzleG[index]);
        gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_B, swizzleB[index]);
        gl3.glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_SWIZZLE_A, swizzleA[index]);

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

    return true;
}
 
Example 8
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 9
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 10
Source File: Gl_320_primitive_shading.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.glBindVertexArray(vertexArrayName.get(0));
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.CONSTANT, bufferName.get(Buffer.CONSTANT));

    gl3.glBeginQuery(GL_PRIMITIVES_GENERATED, queryName.get(0));
    {
        gl3.glDrawElementsInstancedBaseVertex(GL_TRIANGLES, elementCount, GL_UNSIGNED_SHORT, 0, 1, 0);
    }
    gl3.glEndQuery(GL_PRIMITIVES_GENERATED);

    long[] primitivesGenerated = {0};
    gl3.glGetQueryObjectui64v(queryName.get(0), GL_QUERY_RESULT, primitivesGenerated, 0);

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

    GL3 gl3 = (GL3) gl;

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

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

    gl3.glDisable(GL_SCISSOR_TEST);
    Vec3 cameraPosition = cameraPosition().negate();
    //glm::vec3 CameraPosition(glm::vec4(glm::normalize(glm::vec3(1.0)), 1.0) * this->view());

    gl3.glUseProgram(programName);
    gl3.glUniform3fv(uniformCameraPosition, 1, cameraPosition.toFa_(), 0);
    gl3.glUniformMatrix4fv(uniformMv, 1, false, mv.toFa_(), 0);
    gl3.glUniformMatrix4fv(uniformMvp, 1, false, mvp.toFa_(), 0);

    gl3.glBindVertexArray(vertexArrayName.get(0));

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

    return true;
}
 
Example 12
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 13
Source File: Gl_320_draw_multiple.java    From jogl-samples with MIT License 4 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

    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);
    {
        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);

        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_DEPTH, 0, clearDepth);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor);

    gl3.glUseProgram(programName);

    // Attach the buffer to UBO binding point semantic::uniform::TRANSFORM0
    gl3.glBindBufferBase(GL_UNIFORM_BUFFER, Semantic.Uniform.TRANSFORM0, bufferName.get(Buffer.TRANSFORM));
    gl3.glBindVertexArray(vertexArrayName.get(0));
    /**
     * public void glMultiDrawElementsBaseVertex(int mode, IntBuffer count,
     * int type, PointerBuffer indices, int drawcount, IntBuffer basevertex)
     */
    gl3.glMultiDrawElementsBaseVertex(
            GL_TRIANGLES,
            count,
            GL_UNSIGNED_INT,
            indices,
            2,
            baseVertex);
    return true;
}
 
Example 14
Source File: Gl_330_query_conditional.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, (float) windowSize.x / windowSize.y, 0.1f, 100.0f);
        Mat4 model = new Mat4(1.0f);

        projection.mul(viewMat4()).mul(model).toDbb(pointer);

        gl3.glUnmapBuffer(GL_UNIFORM_BUFFER);
    }

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

    // Clear color buffer with black
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 0).put(1, 0).put(2, 0).put(3, 1));

    // Bind program
    gl3.glUseProgram(programName);
    gl3.glBindVertexArray(vertexArrayName.get(0));

    gl3.glBindBufferRange(GL_UNIFORM_BUFFER, Semantic.Uniform.MATERIAL, bufferName.get(Buffer.MATERIAL), 0, Vec4.SIZE);

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

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

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

    gl3.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.
    gl3.glBeginConditionalRender(queryName.get(0), GL_QUERY_WAIT);
    {
        // Clear color buffer with white
        gl3.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 1).put(1, 1).put(2, 1).put(3, 1));

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

    toggle = !toggle;

    return true;
}
 
Example 15
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 16
Source File: Gl_330_buffer_type.java    From jogl-samples with MIT License 4 votes vote down vote up
@Override
protected boolean render(GL gl) {

    GL3 gl3 = (GL3) gl;

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

    gl3.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 1).put(1, 1).put(2, 1).put(3, 1));

    gl3.glUseProgram(programName);
    gl3.glUniform4f(uniformDiffuse, 1.0f, 0.5f, 0.0f, 1.0f);
    gl3.glUniformMatrix4fv(uniformMvp, 1, false, mvp.toFa_(), 0);

    for (int index = 0; index < Buffer.MAX; ++index) {

        gl3.glViewport(viewport[index].x, viewport[index].y, viewport[index].z, viewport[index].w);

        gl3.glBindVertexArray(vertexArrayName.get(index));
        gl3.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);
    }

    return true;
}
 
Example 17
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 18
Source File: Gl_320_fbo.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, (float) windowSize.x / windowSize.y, 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.glEnable(GL_DEPTH_TEST);
        gl3.glDepthFunc(GL_LESS);

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

        gl3.glBindFramebuffer(GL_FRAMEBUFFER, framebufferName.get(0));
        //glEnable(GL_FRAMEBUFFER_SRGB);
        float[] depth = {1.0f};
        gl3.glClearBufferfv(GL_DEPTH, 0, depth, 0);
        gl3.glClearBufferfv(GL_COLOR, 0, new float[]{1.0f, 0.5f, 0.0f, 1.0f}, 0);

        gl3.glUseProgram(programName[Program.TEXTURE]);

        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.glViewport(0, 0, windowSize.x, windowSize.y);

        gl3.glBindFramebuffer(GL_FRAMEBUFFER, 0);
        //glDisable(GL_FRAMEBUFFER_SRGB);

        gl3.glUseProgram(programName[Program.SPLASH]);

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

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

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

    GL3 gl3 = (GL3) gl;

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

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glScissor(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 1).put(1, .5f).put(2, 0).put(3, 1));

    // Bind the program for use
    gl3.glUseProgram(programName);
    gl3.glUniformMatrix4fv(uniformMvp, 1, false, mvp.toFa_(), 0);

    gl3.glBindSampler(Viewport.V00, samplerName.get(Viewport.V00));
    gl3.glBindSampler(Viewport.V10, samplerName.get(Viewport.V10));
    gl3.glBindSampler(Viewport.V11, samplerName.get(Viewport.V11));
    gl3.glBindSampler(Viewport.V01, samplerName.get(Viewport.V01));

    gl3.glActiveTexture(GL_TEXTURE0);
    gl3.glBindTexture(GL_TEXTURE_2D, texture2dName.get(0));
    gl3.glActiveTexture(GL_TEXTURE1);
    gl3.glBindTexture(GL_TEXTURE_2D, texture2dName.get(0));
    gl3.glActiveTexture(GL_TEXTURE2);
    gl3.glBindTexture(GL_TEXTURE_2D, texture2dName.get(0));
    gl3.glActiveTexture(GL_TEXTURE3);
    gl3.glBindTexture(GL_TEXTURE_2D, texture2dName.get(0));

    gl3.glBindVertexArray(vertexArrayName.get(0));

    for (int index = 0; index < Viewport.MAX; ++index) {
        gl3.glUniform1i(uniformDiffuse, index);
        gl3.glScissor(viewport[index].x, viewport[index].y, viewport[index].z, viewport[index].w);
        gl3.glDrawArraysInstanced(GL_TRIANGLES, 0, vertexCount, 1);
    }

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

    GL3 gl3 = (GL3) gl;

    gl3.glViewport(0, 0, windowSize.x, windowSize.y);
    gl3.glClearBufferfv(GL_COLOR, 0, clearColor.put(0, 1).put(1, .5f).put(2, 0).put(3, 1));

    return true;
}