Example usage for org.lwjgl.opengl GL createCapabilities

List of usage examples for org.lwjgl.opengl GL createCapabilities

Introduction

In this page you can find the example usage for org.lwjgl.opengl GL createCapabilities.

Prototype

public static GLCapabilities createCapabilities() 

Source Link

Document

Creates a new GLCapabilities instance for the OpenGL context that is current in the current thread.

Usage

From source file:org.lwjgl.demo.opengl.fbo.EdgeShaderMultisampleDemo20.java

License:Open Source License

void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
        GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override//from  ww  w  .  j a  v a 2 s.  c  o m
        public void invoke(int error, long description) {
            if (error == GLFW_VERSION_UNAVAILABLE)
                System.err.println("This demo requires OpenGL 2.0 or higher.");
            delegate.invoke(error, description);
        }

        @Override
        public void free() {
            delegate.free();
        }
    });

    if (!glfwInit())
        throw new IllegalStateException("Unable to initialize GLFW");

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    window = glfwCreateWindow(width, height,
            "Multisampled silhouette rendering with Sobel edge detection shader", NULL, NULL);
    if (window == NULL) {
        throw new AssertionError("Failed to create the GLFW window");
    }

    System.out.println("Press letter 'O' to toggle between outline/edges.");
    System.out.println("Press spacebar to show/hide edges.");

    glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
        @Override
        public void invoke(long window, int width, int height) {
            if (width > 0 && height > 0 && (EdgeShaderMultisampleDemo20.this.width != width
                    || EdgeShaderMultisampleDemo20.this.height != height)) {
                EdgeShaderMultisampleDemo20.this.width = width;
                EdgeShaderMultisampleDemo20.this.height = height;
                EdgeShaderMultisampleDemo20.this.resize = true;
            }
        }
    });

    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (action != GLFW_RELEASE)
                return;

            if (key == GLFW_KEY_ESCAPE) {
                glfwSetWindowShouldClose(window, true);
            } else if (key == GLFW_KEY_O) {
                outlineOnly = !outlineOnly;
            } else if (key == GLFW_KEY_SPACE) {
                showEdge = !showEdge;
            }
        }
    });

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);
    glfwShowWindow(window);
    caps = GL.createCapabilities();
    if (!caps.GL_EXT_framebuffer_object) {
        throw new AssertionError("This demo requires the EXT_framebuffer_object extension");
    }
    if (!caps.GL_EXT_framebuffer_multisample) {
        throw new AssertionError("This demo requires the EXT_framebuffer_multisample extension");
    }
    if (!caps.GL_EXT_framebuffer_blit) {
        throw new AssertionError("This demo requires the EXT_framebuffer_blit extension");
    }

    debugProc = GLUtil.setupDebugMessageCallback();

    glClearColor(1.0f, 1.0f, 1.0f, 0.0f); // using alpha = 0.0 is important here for the outline to work!
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    samples = Math.min(4, glGetInteger(GL_MAX_SAMPLES_EXT));

    /* Create all needed GL resources */
    createCube();
    createQuad();
    createNormalProgram();
    createEdgeProgram();
    createOutlineProgram();
    createTex();
    createFbos();
}

From source file:org.lwjgl.demo.opengl.fbo.MultisampledFbo2Demo.java

License:Open Source License

void renderLoop() {
    glfwMakeContextCurrent(window);/*from ww w  . j av  a2  s .c  o  m*/
    GL.createCapabilities();
    debugProc = GLUtil.setupDebugMessageCallback();
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glColor3f(0.1f, 0.1f, 0.1f);

    /* Query maximum sample count */
    samples = glGetInteger(GL_MAX_SAMPLES);
    System.err.println("Using " + samples + "x multisampling");

    /* Initially create the FBOs */
    createFBOs();

    long lastTime = System.nanoTime();
    while (!destroyed) {
        /* Update the FBO if the window changed in size */
        update();

        /* Render to multisampled FBO */
        glBindFramebuffer(GL_FRAMEBUFFER, multisampledFbo);
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glViewport(0, 0, width, height);

            long thisTime = System.nanoTime();
            float elapsed = (lastTime - thisTime) / 1E9f;

            /* Simple orthographic projection */
            float aspect = (float) width / height;
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(-1.0f * aspect, +1.0f * aspect, -1.0f, +1.0f, -1.0f, +1.0f);

            /* Rotate a bit and draw a quad */
            glMatrixMode(GL_MODELVIEW);
            glRotatef(elapsed * 1, 0, 0, 1);
            glBegin(GL_QUADS);
            glVertex2f(-0.5f, -0.5f);
            glVertex2f(+0.5f, -0.5f);
            glVertex2f(+0.5f, +0.5f);
            glVertex2f(-0.5f, +0.5f);
            glEnd();
        }
        glBindFramebuffer(GL_FRAMEBUFFER, 0);

        /* Resolve by blitting to non-multisampled FBO */
        glBindFramebuffer(GL_READ_FRAMEBUFFER, multisampledFbo);
        glBindFramebuffer(GL_DRAW_FRAMEBUFFER, fbo);
        glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
        glBindFramebuffer(GL_FRAMEBUFFER, 0);

        /* Now you can just read from the resolved colorTexture */

        /* But we will just draw it on the viewport using a fullscreen quad */
        glEnable(GL_TEXTURE_2D);
        glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_REPLACE);
        glBindTexture(GL_TEXTURE_2D, colorTexture);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glBegin(GL_QUADS);
        glTexCoord2f(0, 0);
        glVertex2f(-1, -1);
        glTexCoord2f(1, 0);
        glVertex2f(1, -1);
        glTexCoord2f(1, 1);
        glVertex2f(1, 1);
        glTexCoord2f(0, 1);
        glVertex2f(-1, 1);
        glEnd();
        glDisable(GL_TEXTURE_2D);

        synchronized (lock) {
            if (!destroyed) {
                glfwSwapBuffers(window);
            }
        }
    }
}

From source file:org.lwjgl.demo.opengl.fbo.MultisampledFboDemo.java

License:Open Source License

void renderLoop() {
    glfwMakeContextCurrent(window);//  ww w  .  j av  a 2  s .co m
    GL.createCapabilities();
    debugProc = GLUtil.setupDebugMessageCallback();
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glColor3f(0.1f, 0.1f, 0.1f);

    /* Query maximum sample count */
    samples = glGetInteger(GL_MAX_SAMPLES);
    System.err.println("Using " + samples + "x multisampling");

    /* Initially create the FBO with color texture and renderbuffer */
    createFramebufferObject();

    long lastTime = System.nanoTime();
    while (!destroyed) {
        /* Update the FBO if the window changed in size */
        update();

        /* Render to multisampled FBO */
        glBindFramebuffer(GL_FRAMEBUFFER, fbo);
        {
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
            glViewport(0, 0, width, height);

            long thisTime = System.nanoTime();
            float elapsed = (lastTime - thisTime) / 1E9f;
            lastTime = thisTime;

            /* Simple orthographic project */
            float aspect = (float) width / height;
            glMatrixMode(GL_PROJECTION);
            glLoadIdentity();
            glOrtho(-1.0f * aspect, +1.0f * aspect, -1.0f, +1.0f, -1.0f, +1.0f);

            /* Rotate a bit and draw a quad */
            glMatrixMode(GL_MODELVIEW);
            glRotatef(elapsed, 0, 0, 1);
            glBegin(GL_QUADS);
            glVertex2f(-0.5f, -0.5f);
            glVertex2f(+0.5f, -0.5f);
            glVertex2f(+0.5f, +0.5f);
            glVertex2f(-0.5f, +0.5f);
            glEnd();
        }
        glBindFramebuffer(GL_FRAMEBUFFER, 0);

        /* Blit to default framebuffer */
        glBindFramebuffer(GL_READ_FRAMEBUFFER, fbo);
        glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);

        synchronized (lock) {
            if (!destroyed) {
                glfwSwapBuffers(window);
            }
        }
    }
}

From source file:org.lwjgl.demo.opengl.fbo.ReadDepthBufferDemo.java

License:Open Source License

private void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
        private GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override//from ww w  .ja v  a  2  s . c  o m
        public void invoke(int error, long description) {
            if (error == GLFW_VERSION_UNAVAILABLE)
                System.err.println("This demo requires OpenGL 2.0 or higher.");
            delegate.invoke(error, description);
        }

        @Override
        public void free() {
            delegate.free();
        }
    });

    if (!glfwInit())
        throw new IllegalStateException("Unable to initialize GLFW");

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    window = glfwCreateWindow(width, height, "Sample depth buffer", NULL, NULL);
    if (window == NULL) {
        throw new AssertionError("Failed to create the GLFW window");
    }

    System.out.println("Press key 'V' to toggle between view-space and world-space reconstruction.");

    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (action != GLFW_RELEASE)
                return;

            if (key == GLFW_KEY_ESCAPE) {
                glfwSetWindowShouldClose(window, true);
            } else if (key == GLFW_KEY_V) {
                reconstructViewSpace = !reconstructViewSpace;
            }
        }
    });

    glfwSetCursorPosCallback(window, cpCallback = new GLFWCursorPosCallback() {
        @Override
        public void invoke(long window, double x, double y) {
            ReadDepthBufferDemo.this.mouseX = (float) x;
        }
    });

    glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
        @Override
        public void invoke(long window, int width, int height) {
            if (width > 0 && height > 0
                    && (ReadDepthBufferDemo.this.width != width || ReadDepthBufferDemo.this.height != height)) {
                ReadDepthBufferDemo.this.width = width;
                ReadDepthBufferDemo.this.height = height;
                ReadDepthBufferDemo.this.resetFramebuffer = true;
            }
        }
    });

    glfwSetMouseButtonCallback(window, mbCallback = new GLFWMouseButtonCallback() {
        @Override
        public void invoke(long window, int button, int action, int mods) {
            if (action == GLFW_PRESS) {
                ReadDepthBufferDemo.this.mouseDownX = ReadDepthBufferDemo.this.mouseX;
                ReadDepthBufferDemo.this.mouseDown = true;
            } else if (action == GLFW_RELEASE) {
                ReadDepthBufferDemo.this.mouseDown = false;
                ReadDepthBufferDemo.this.rotationAboutY = ReadDepthBufferDemo.this.currRotationAboutY;
            }
        }
    });

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);
    glfwShowWindow(window);

    IntBuffer framebufferSize = BufferUtils.createIntBuffer(2);
    nglfwGetFramebufferSize(window, memAddress(framebufferSize), memAddress(framebufferSize) + 4);
    width = framebufferSize.get(0);
    height = framebufferSize.get(1);

    GLCapabilities caps = GL.createCapabilities();
    if (!caps.GL_EXT_framebuffer_object) {
        throw new AssertionError("This demo requires the EXT_framebuffer_object extension");
    }
    debugProc = GLUtil.setupDebugMessageCallback();

    /* Create all needed GL resources */
    createDepthTexture();
    createFramebufferObject();
    createFullScreenVbo();
    createSceneVbo();
    createDepthOnlyProgram();
    createFullScreenQuadProgram();

    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
}

From source file:org.lwjgl.demo.opengl.geometry.GeometryShaderTest20.java

License:Open Source License

void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
        GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override//from   w  w w. jav  a 2 s. co m
        public void invoke(int error, long description) {
            if (error == GLFW_VERSION_UNAVAILABLE)
                System.err.println("This demo requires OpenGL 2.0 or higher.");
            delegate.invoke(error, description);
        }

        @Override
        public void free() {
            delegate.free();
        }
    });

    if (!glfwInit())
        throw new IllegalStateException("Unable to initialize GLFW");

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    window = glfwCreateWindow(width, height, "Antialiased wireframe rendering with geometry shader", NULL,
            NULL);
    if (window == NULL) {
        throw new AssertionError("Failed to create the GLFW window");
    }

    glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
        @Override
        public void invoke(long window, int width, int height) {
            if (width > 0 && height > 0 && (GeometryShaderTest20.this.width != width
                    || GeometryShaderTest20.this.height != height)) {
                GeometryShaderTest20.this.width = width;
                GeometryShaderTest20.this.height = height;
            }
        }
    });

    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (action != GLFW_RELEASE)
                return;

            if (key == GLFW_KEY_ESCAPE) {
                glfwSetWindowShouldClose(window, true);
            }
        }
    });

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);
    glfwShowWindow(window);
    caps = GL.createCapabilities();
    if (!caps.GL_EXT_geometry_shader4) {
        throw new AssertionError("This demo requires the EXT_geometry_shader4 extension");
    }

    debugProc = GLUtil.setupDebugMessageCallback();

    glClearColor(0.55f, 0.75f, 0.95f, 1.0f);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);

    /* Create all needed GL resources */
    createVao();
    createRasterProgram();
    initProgram();
}

From source file:org.lwjgl.demo.opengl.geometry.SilhouetteDemo.java

License:Open Source License

void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
        GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override/*from   w ww  . j a v  a  2  s.c  o m*/
        public void invoke(int error, long description) {
            if (error == GLFW_VERSION_UNAVAILABLE)
                System.err.println("This demo requires OpenGL 2.0 or higher.");
            delegate.invoke(error, description);
        }

        @Override
        public void free() {
            delegate.free();
        }
    });

    if (!glfwInit())
        throw new IllegalStateException("Unable to initialize GLFW");

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    window = glfwCreateWindow(width, height, "Silhouette rendering with geometry shader", NULL, NULL);
    if (window == NULL) {
        throw new AssertionError("Failed to create the GLFW window");
    }

    glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
        @Override
        public void invoke(long window, int width, int height) {
            if (width > 0 && height > 0
                    && (SilhouetteDemo.this.width != width || SilhouetteDemo.this.height != height)) {
                SilhouetteDemo.this.width = width;
                SilhouetteDemo.this.height = height;
            }
        }
    });

    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (action != GLFW_RELEASE)
                return;

            if (key == GLFW_KEY_ESCAPE) {
                glfwSetWindowShouldClose(window, true);
            }
        }
    });

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);
    glfwShowWindow(window);
    caps = GL.createCapabilities();
    if (!caps.GL_EXT_geometry_shader4) {
        throw new AssertionError("This demo requires the EXT_geometry_shader4 extension");
    }

    debugProc = GLUtil.setupDebugMessageCallback();

    glClearColor(0.55f, 0.75f, 0.95f, 1.0f);
    glEnable(GL_DEPTH_TEST);

    /* Create all needed GL resources */
    createBoxVao();
    createRasterProgram();
    initProgram();
}

From source file:org.lwjgl.demo.opengl.glfw.Multithreaded.java

License:Open Source License

void renderLoop() {
    glfwMakeContextCurrent(window);//  ww  w  .j a  va2  s.  c  om
    GL.createCapabilities();
    debugProc = GLUtil.setupDebugMessageCallback();
    glClearColor(0.3f, 0.5f, 0.7f, 0.0f);

    long lastTime = System.nanoTime();
    while (!destroyed) {
        glClear(GL_COLOR_BUFFER_BIT);
        glViewport(0, 0, width, height);

        long thisTime = System.nanoTime();
        float elapsed = (lastTime - thisTime) / 1E9f;
        lastTime = thisTime;

        float aspect = (float) width / height;
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glOrtho(-1.0f * aspect, +1.0f * aspect, -1.0f, +1.0f, -1.0f, +1.0f);

        glMatrixMode(GL_MODELVIEW);
        glRotatef(elapsed * 10.0f, 0, 0, 1);
        glBegin(GL_QUADS);
        glVertex2f(-0.5f, -0.5f);
        glVertex2f(+0.5f, -0.5f);
        glVertex2f(+0.5f, +0.5f);
        glVertex2f(-0.5f, +0.5f);
        glEnd();

        synchronized (lock) {
            if (!destroyed) {
                glfwSwapBuffers(window);
            }
        }
    }
}

From source file:org.lwjgl.demo.opengl.raytracing.AtomicDemo.java

License:Open Source License

private void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
        private GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override//from ww  w. ja  v  a2s  .  c  om
        public void invoke(int error, long description) {
            if (error == GLFW_VERSION_UNAVAILABLE)
                System.err.println(
                        "This demo requires OpenGL 4.3 or higher. The Demo33 version works on OpenGL 3.3 or higher.");
            delegate.invoke(error, description);
        }

        @Override
        public void free() {
            delegate.free();
        }
    });

    if (!glfwInit())
        throw new IllegalStateException("Unable to initialize GLFW");

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    window = glfwCreateWindow(width, height, "Raytracing Demo (compute shader + atomic counter)", NULL, NULL);
    if (window == NULL) {
        throw new AssertionError("Failed to create the GLFW window");
    }

    System.out.println("Press keypad '+' or 'page up' to increase the number of bounces.");
    System.out.println("Press keypad '-' or 'page down' to decrease the number of bounces.");
    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (action != GLFW_RELEASE) {
                return;
            }

            if (key == GLFW_KEY_ESCAPE) {
                glfwSetWindowShouldClose(window, true);
            } else if (key == GLFW_KEY_KP_ADD || key == GLFW_KEY_PAGE_UP) {
                int newBounceCount = Math.min(4, AtomicDemo.this.bounceCount + 1);
                if (newBounceCount != AtomicDemo.this.bounceCount) {
                    AtomicDemo.this.bounceCount = newBounceCount;
                    System.out.println("Ray bounce count is now: " + AtomicDemo.this.bounceCount);
                    AtomicDemo.this.frameNumber = 0;
                }
            } else if (key == GLFW_KEY_KP_SUBTRACT || key == GLFW_KEY_PAGE_DOWN) {
                int newBounceCount = Math.max(1, AtomicDemo.this.bounceCount - 1);
                if (newBounceCount != AtomicDemo.this.bounceCount) {
                    AtomicDemo.this.bounceCount = newBounceCount;
                    System.out.println("Ray bounce count is now: " + AtomicDemo.this.bounceCount);
                    AtomicDemo.this.frameNumber = 0;
                }
            }
        }
    });

    glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
        @Override
        public void invoke(long window, int width, int height) {
            if (width > 0 && height > 0
                    && (AtomicDemo.this.width != width || AtomicDemo.this.height != height)) {
                AtomicDemo.this.width = width;
                AtomicDemo.this.height = height;
                AtomicDemo.this.resetFramebuffer = true;
                AtomicDemo.this.frameNumber = 0;
            }
        }
    });

    glfwSetCursorPosCallback(window, cpCallback = new GLFWCursorPosCallback() {
        @Override
        public void invoke(long window, double x, double y) {
            AtomicDemo.this.mouseX = (float) x;
            if (mouseDown) {
                AtomicDemo.this.frameNumber = 0;
            }
        }
    });

    glfwSetMouseButtonCallback(window, mbCallback = new GLFWMouseButtonCallback() {
        @Override
        public void invoke(long window, int button, int action, int mods) {
            if (action == GLFW_PRESS) {
                AtomicDemo.this.mouseDownX = AtomicDemo.this.mouseX;
                AtomicDemo.this.mouseDown = true;
            } else if (action == GLFW_RELEASE) {
                AtomicDemo.this.mouseDown = false;
                AtomicDemo.this.rotationAboutY = AtomicDemo.this.currRotationAboutY;
            }
        }
    });

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);
    glfwShowWindow(window);

    IntBuffer framebufferSize = BufferUtils.createIntBuffer(2);
    nglfwGetFramebufferSize(window, memAddress(framebufferSize), memAddress(framebufferSize) + 4);
    width = framebufferSize.get(0);
    height = framebufferSize.get(1);

    GL.createCapabilities();
    debugProc = GLUtil.setupDebugMessageCallback();

    /* Create all needed GL resources */
    createFramebufferTexture();
    createSampler();
    quadFullScreenVao();
    createComputeProgram();
    initComputeProgram();
    createQuadProgram();
    initQuadProgram();
    createAtomicBuffer();
}

From source file:org.lwjgl.demo.opengl.raytracing.Demo.java

License:Open Source License

private void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
        private GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override/*from   w  w w  .  j ava 2 s .  c om*/
        public void invoke(int error, long description) {
            if (error == GLFW_VERSION_UNAVAILABLE)
                System.err.println(
                        "This demo requires OpenGL 4.3 or higher. The Demo33 version works on OpenGL 3.3 or higher.");
            delegate.invoke(error, description);
        }

        @Override
        public void free() {
            delegate.free();
        }
    });

    if (!glfwInit())
        throw new IllegalStateException("Unable to initialize GLFW");

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 4);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    window = glfwCreateWindow(width, height, "Raytracing Demo (compute shader)", NULL, NULL);
    if (window == NULL) {
        throw new AssertionError("Failed to create the GLFW window");
    }

    System.out.println("Press keypad '+' or 'page up' to increase the number of bounces.");
    System.out.println("Press keypad '-' or 'page down' to decrease the number of bounces.");
    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (action != GLFW_RELEASE) {
                return;
            }

            if (key == GLFW_KEY_ESCAPE) {
                glfwSetWindowShouldClose(window, true);
            } else if (key == GLFW_KEY_KP_ADD || key == GLFW_KEY_PAGE_UP) {
                int newBounceCount = Math.min(4, Demo.this.bounceCount + 1);
                if (newBounceCount != Demo.this.bounceCount) {
                    Demo.this.bounceCount = newBounceCount;
                    System.out.println("Ray bounce count is now: " + Demo.this.bounceCount);
                    Demo.this.frameNumber = 0;
                }
            } else if (key == GLFW_KEY_KP_SUBTRACT || key == GLFW_KEY_PAGE_DOWN) {
                int newBounceCount = Math.max(1, Demo.this.bounceCount - 1);
                if (newBounceCount != Demo.this.bounceCount) {
                    Demo.this.bounceCount = newBounceCount;
                    System.out.println("Ray bounce count is now: " + Demo.this.bounceCount);
                    Demo.this.frameNumber = 0;
                }
            }
        }
    });

    glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
        @Override
        public void invoke(long window, int width, int height) {
            if (width > 0 && height > 0 && (Demo.this.width != width || Demo.this.height != height)) {
                Demo.this.width = width;
                Demo.this.height = height;
                Demo.this.resetFramebuffer = true;
                Demo.this.frameNumber = 0;
            }
        }
    });

    glfwSetCursorPosCallback(window, cpCallback = new GLFWCursorPosCallback() {
        @Override
        public void invoke(long window, double x, double y) {
            Demo.this.mouseX = (float) x;
            if (mouseDown) {
                Demo.this.frameNumber = 0;
            }
        }
    });

    glfwSetMouseButtonCallback(window, mbCallback = new GLFWMouseButtonCallback() {
        @Override
        public void invoke(long window, int button, int action, int mods) {
            if (action == GLFW_PRESS) {
                Demo.this.mouseDownX = Demo.this.mouseX;
                Demo.this.mouseDown = true;
            } else if (action == GLFW_RELEASE) {
                Demo.this.mouseDown = false;
                Demo.this.rotationAboutY = Demo.this.currRotationAboutY;
            }
        }
    });

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);
    glfwShowWindow(window);

    IntBuffer framebufferSize = BufferUtils.createIntBuffer(2);
    nglfwGetFramebufferSize(window, memAddress(framebufferSize), memAddress(framebufferSize) + 4);
    width = framebufferSize.get(0);
    height = framebufferSize.get(1);

    GL.createCapabilities();
    debugProc = GLUtil.setupDebugMessageCallback();

    /* Create all needed GL resources */
    createFramebufferTexture();
    createSampler();
    quadFullScreenVao();
    createComputeProgram();
    initComputeProgram();
    createQuadProgram();
    initQuadProgram();

    firstTime = System.nanoTime();
}

From source file:org.lwjgl.demo.opengl.raytracing.Demo20.java

License:Open Source License

private void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {
        private GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override//from   w ww  . ja  v a2 s . c  o  m
        public void invoke(int error, long description) {
            if (error == GLFW_VERSION_UNAVAILABLE)
                System.err.println("This demo requires OpenGL 2.0 or higher.");
            delegate.invoke(error, description);
        }

        @Override
        public void free() {
            delegate.free();
        }
    });

    if (!glfwInit())
        throw new IllegalStateException("Unable to initialize GLFW");

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    window = glfwCreateWindow(width, height, "Raytracing Demo (fragment shader)", 0L, 0L);
    if (window == 0L) {
        throw new AssertionError("Failed to create the GLFW window");
    }

    System.out.println("Press keypad '+' or 'page up' to increase the number of bounces.");
    System.out.println("Press keypad '-' or 'page down' to decrease the number of bounces.");
    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {
        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (action != GLFW_RELEASE) {
                return;
            }

            if (key == GLFW_KEY_ESCAPE) {
                glfwSetWindowShouldClose(window, true);
            } else if (key == GLFW_KEY_KP_ADD || key == GLFW_KEY_PAGE_UP) {
                int newBounceCount = Math.min(4, Demo20.this.bounceCount + 1);
                if (newBounceCount != Demo20.this.bounceCount) {
                    Demo20.this.bounceCount = newBounceCount;
                    System.out.println("Ray bounce count is now: " + Demo20.this.bounceCount);
                    Demo20.this.frameNumber = 0;
                }
            } else if (key == GLFW_KEY_KP_SUBTRACT || key == GLFW_KEY_PAGE_DOWN) {
                int newBounceCount = Math.max(1, Demo20.this.bounceCount - 1);
                if (newBounceCount != Demo20.this.bounceCount) {
                    Demo20.this.bounceCount = newBounceCount;
                    System.out.println("Ray bounce count is now: " + Demo20.this.bounceCount);
                    Demo20.this.frameNumber = 0;
                }
            }
        }
    });

    glfwSetFramebufferSizeCallback(window, fbCallback = new GLFWFramebufferSizeCallback() {
        @Override
        public void invoke(long window, int width, int height) {
            if (width > 0 && height > 0 && (Demo20.this.width != width || Demo20.this.height != height)) {
                Demo20.this.width = width;
                Demo20.this.height = height;
                Demo20.this.resetFramebuffer = true;
                Demo20.this.frameNumber = 0;
            }
        }
    });

    glfwSetCursorPosCallback(window, cpCallback = new GLFWCursorPosCallback() {
        @Override
        public void invoke(long window, double x, double y) {
            Demo20.this.mouseX = (float) x;
            if (mouseDown) {
                Demo20.this.frameNumber = 0;
            }
        }
    });

    glfwSetMouseButtonCallback(window, mbCallback = new GLFWMouseButtonCallback() {
        @Override
        public void invoke(long window, int button, int action, int mods) {
            if (action == GLFW_PRESS) {
                Demo20.this.mouseDownX = Demo20.this.mouseX;
                Demo20.this.mouseDown = true;
            } else if (action == GLFW_RELEASE) {
                Demo20.this.mouseDown = false;
                Demo20.this.rotationAboutY = Demo20.this.currRotationAboutY;
            }
        }
    });

    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);
    glfwShowWindow(window);

    IntBuffer framebufferSize = BufferUtils.createIntBuffer(2);
    nglfwGetFramebufferSize(window, memAddress(framebufferSize), memAddress(framebufferSize) + 4);
    width = framebufferSize.get(0);
    height = framebufferSize.get(1);

    GLCapabilities caps = GL.createCapabilities();
    if (!caps.GL_EXT_framebuffer_object) {
        throw new AssertionError("This demo requires the EXT_framebuffer_object extensions");
    }
    if (!caps.GL_ARB_texture_float) {
        throw new AssertionError("This demo requires the ARB_texture_float extensions");
    }

    debugProc = GLUtil.setupDebugMessageCallback();

    /* Create all needed GL resources */
    createFramebufferTexture();
    createFrameBufferObject();
    quadFullScreenVbo();
    createBoxesTexture();
    createRayTracingProgram();
    initRayTracingProgram();
    createQuadProgram();
    initQuadProgram();

    firstTime = System.nanoTime();
}