Search in sources :

Example 6 with GLFWErrorCallback

use of org.lwjgl.glfw.GLFWErrorCallback in project lwjgl3-demos by LWJGL.

the class EdgeShaderDemo20 method init.

void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {

        GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override
        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 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 && (EdgeShaderDemo20.this.width != width || EdgeShaderDemo20.this.height != height)) {
                EdgeShaderDemo20.this.width = width;
                EdgeShaderDemo20.this.height = height;
                EdgeShaderDemo20.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");
    }
    debugProc = GLUtil.setupDebugMessageCallback();
    // using alpha = 0.0 is important here for the outline to work!
    glClearColor(1.0f, 1.0f, 1.0f, 0.0f);
    glEnable(GL_DEPTH_TEST);
    glEnable(GL_CULL_FACE);
    /* Create all needed GL resources */
    createCube();
    createQuad();
    createNormalProgram();
    createEdgeProgram();
    createOutlineProgram();
    createTex();
    createFbo();
}
Also used : GLFWKeyCallback(org.lwjgl.glfw.GLFWKeyCallback) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) GLFWFramebufferSizeCallback(org.lwjgl.glfw.GLFWFramebufferSizeCallback) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Example 7 with GLFWErrorCallback

use of org.lwjgl.glfw.GLFWErrorCallback in project lwjgl3-demos by LWJGL.

the class SilhouetteDemo method init.

void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {

        GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override
        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();
}
Also used : GLFWKeyCallback(org.lwjgl.glfw.GLFWKeyCallback) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) GLFWFramebufferSizeCallback(org.lwjgl.glfw.GLFWFramebufferSizeCallback) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Example 8 with GLFWErrorCallback

use of org.lwjgl.glfw.GLFWErrorCallback in project jmonkeyengine by jMonkeyEngine.

the class LwjglWindow method createContext.

/**
     * Apply the settings, changing resolution, etc.
     *
     * @param settings the settings to apply when creating the context.
     */
protected void createContext(final AppSettings settings) {
    glfwSetErrorCallback(errorCallback = new GLFWErrorCallback() {

        @Override
        public void invoke(int error, long description) {
            final String message = GLFWErrorCallback.getDescription(description);
            listener.handleError(message, new Exception(message));
        }

        @Override
        public void close() {
            super.close();
        }

        @Override
        public void callback(long args) {
            super.callback(args);
        }
    });
    if (!glfwInit()) {
        throw new IllegalStateException("Unable to initialize GLFW");
    }
    glfwDefaultWindowHints();
    if (settings.getRenderer().equals(AppSettings.LWJGL_OPENGL3)) {
        glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
        glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GLFW_TRUE);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 2);
    } else {
        glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
        glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    }
    if (settings.getBoolean("RendererDebug")) {
        glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
    }
    if (settings.isGammaCorrection()) {
        glfwWindowHint(GLFW_SRGB_CAPABLE, GLFW_TRUE);
    }
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, settings.isResizable() ? GLFW_TRUE : GLFW_FALSE);
    glfwWindowHint(GLFW_DEPTH_BITS, settings.getDepthBits());
    glfwWindowHint(GLFW_STENCIL_BITS, settings.getStencilBits());
    glfwWindowHint(GLFW_SAMPLES, settings.getSamples());
    glfwWindowHint(GLFW_STEREO, settings.useStereo3D() ? GLFW_TRUE : GLFW_FALSE);
    glfwWindowHint(GLFW_REFRESH_RATE, settings.getFrequency());
    if (settings.getBitsPerPixel() == 24) {
        glfwWindowHint(GLFW_RED_BITS, 8);
        glfwWindowHint(GLFW_GREEN_BITS, 8);
        glfwWindowHint(GLFW_BLUE_BITS, 8);
    } else if (settings.getBitsPerPixel() == 16) {
        glfwWindowHint(GLFW_RED_BITS, 5);
        glfwWindowHint(GLFW_GREEN_BITS, 6);
        glfwWindowHint(GLFW_BLUE_BITS, 5);
    }
    glfwWindowHint(GLFW_ALPHA_BITS, settings.getAlphaBits());
    // TODO: Add support for monitor selection
    long monitor = NULL;
    if (settings.isFullscreen()) {
        monitor = glfwGetPrimaryMonitor();
    }
    final GLFWVidMode videoMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    if (settings.getWidth() <= 0 || settings.getHeight() <= 0) {
        settings.setResolution(videoMode.width(), videoMode.height());
    }
    window = glfwCreateWindow(settings.getWidth(), settings.getHeight(), settings.getTitle(), monitor, NULL);
    if (window == NULL) {
        throw new RuntimeException("Failed to create the GLFW window");
    }
    // Add a resize callback which delegates to the listener
    glfwSetWindowSizeCallback(window, windowSizeCallback = new GLFWWindowSizeCallback() {

        @Override
        public void invoke(final long window, final int width, final int height) {
            settings.setResolution(width, height);
            listener.reshape(width, height);
        }

        @Override
        public void close() {
            super.close();
        }

        @Override
        public void callback(long args) {
            super.callback(args);
        }
    });
    glfwSetWindowFocusCallback(window, windowFocusCallback = new GLFWWindowFocusCallback() {

        @Override
        public void invoke(final long window, final boolean focus) {
            if (wasActive != focus) {
                if (!wasActive) {
                    listener.gainFocus();
                    timer.reset();
                } else {
                    listener.loseFocus();
                }
                wasActive = !wasActive;
            }
        }

        @Override
        public void close() {
            super.close();
        }

        @Override
        public void callback(long args) {
            super.callback(args);
        }
    });
    // Center the window
    if (!settings.isFullscreen()) {
        glfwSetWindowPos(window, (videoMode.width() - settings.getWidth()) / 2, (videoMode.height() - settings.getHeight()) / 2);
    }
    // Make the OpenGL context current
    glfwMakeContextCurrent(window);
    // Enable vsync
    if (settings.isVSync()) {
        glfwSwapInterval(1);
    } else {
        glfwSwapInterval(0);
    }
    setWindowIcon(settings);
    showWindow();
    allowSwapBuffers = settings.isSwapBuffers();
}
Also used : GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) GLFWWindowSizeCallback(org.lwjgl.glfw.GLFWWindowSizeCallback) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode) GLFWWindowFocusCallback(org.lwjgl.glfw.GLFWWindowFocusCallback)

Example 9 with GLFWErrorCallback

use of org.lwjgl.glfw.GLFWErrorCallback in project lwjgl3-demos by LWJGL.

the class NoVerticesBSplineDemo method init.

void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {

        GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override
        public void invoke(int error, long description) {
            if (error == GLFW_VERSION_UNAVAILABLE)
                System.err.println("This demo requires OpenGL 3.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, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
    window = glfwCreateWindow(width, height, "No vertices cubic B-splines shader demo", 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 && (NoVerticesBSplineDemo.this.width != width || NoVerticesBSplineDemo.this.height != height)) {
                NoVerticesBSplineDemo.this.width = width;
                NoVerticesBSplineDemo.this.height = height;
            }
        }
    });
    System.out.println("Press 'arrow up' to increase the lod");
    System.out.println("Press 'arrow down' to decrease the lod");
    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {

        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
                glfwSetWindowShouldClose(window, true);
            } else if (key == GLFW_KEY_UP && (action == GLFW_RELEASE || action == GLFW_REPEAT)) {
                lod++;
                System.out.println("Increased LOD to " + lod);
            } else if (key == GLFW_KEY_DOWN && (action == GLFW_RELEASE || action == GLFW_REPEAT)) {
                lod = Math.max(1, lod - 1);
                System.out.println("Decreased LOD to " + lod);
            }
        }
    });
    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);
    glfwShowWindow(window);
    caps = GL.createCapabilities();
    debugProc = GLUtil.setupDebugMessageCallback();
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    // Create all needed GL resources
    createProgram();
    createUbo();
    // and set some GL state
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
Also used : GLFWKeyCallback(org.lwjgl.glfw.GLFWKeyCallback) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) GLFWFramebufferSizeCallback(org.lwjgl.glfw.GLFWFramebufferSizeCallback) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Example 10 with GLFWErrorCallback

use of org.lwjgl.glfw.GLFWErrorCallback in project lwjgl3-demos by LWJGL.

the class NoVerticesProjectedGridDemo method init.

void init() throws IOException {
    glfwSetErrorCallback(errCallback = new GLFWErrorCallback() {

        GLFWErrorCallback delegate = GLFWErrorCallback.createPrint(System.err);

        @Override
        public void invoke(int error, long description) {
            if (error == GLFW_VERSION_UNAVAILABLE)
                System.err.println("This demo requires OpenGL 3.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, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
    window = glfwCreateWindow(width, height, "No vertices projected grid shader demo", 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 && (NoVerticesProjectedGridDemo.this.width != width || NoVerticesProjectedGridDemo.this.height != height)) {
                NoVerticesProjectedGridDemo.this.width = width;
                NoVerticesProjectedGridDemo.this.height = height;
            }
        }
    });
    System.out.println("Press 'arrow right' to increase the grid size in X");
    System.out.println("Press 'arrow left' to decrease the grid size in X");
    System.out.println("Press 'arrow up' to increase the grid size in Y");
    System.out.println("Press 'arrow down' to decrease the grid size in Y");
    glfwSetKeyCallback(window, keyCallback = new GLFWKeyCallback() {

        @Override
        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
                glfwSetWindowShouldClose(window, true);
            }
            if (key == GLFW_KEY_LEFT && (action == GLFW_RELEASE || action == GLFW_REPEAT)) {
                sizeX = Math.max(1, sizeX - 1);
            } else if (key == GLFW_KEY_RIGHT && (action == GLFW_RELEASE || action == GLFW_REPEAT)) {
                sizeX++;
            } else if (key == GLFW_KEY_DOWN && (action == GLFW_RELEASE || action == GLFW_REPEAT)) {
                sizeY = Math.max(1, sizeY - 1);
            } else if (key == GLFW_KEY_UP && (action == GLFW_RELEASE || action == GLFW_REPEAT)) {
                sizeY++;
            }
        }
    });
    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);
    glfwMakeContextCurrent(window);
    glfwSwapInterval(0);
    glfwShowWindow(window);
    caps = createCapabilities();
    debugProc = GLUtil.setupDebugMessageCallback();
    glClearColor(1.0f, 1.0f, 1.0f, 1.0f);
    // Create all needed GL resources
    createProgram();
    // and set some GL state
    glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
}
Also used : GLFWKeyCallback(org.lwjgl.glfw.GLFWKeyCallback) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) GLFWFramebufferSizeCallback(org.lwjgl.glfw.GLFWFramebufferSizeCallback) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode)

Aggregations

GLFWErrorCallback (org.lwjgl.glfw.GLFWErrorCallback)14 GLFWKeyCallback (org.lwjgl.glfw.GLFWKeyCallback)13 GLFWVidMode (org.lwjgl.glfw.GLFWVidMode)13 GLFWFramebufferSizeCallback (org.lwjgl.glfw.GLFWFramebufferSizeCallback)12 IntBuffer (java.nio.IntBuffer)3 GLCapabilities (org.lwjgl.opengl.GLCapabilities)2 FillLayout (org.eclipse.swt.layout.FillLayout)1 GLCanvas (org.eclipse.swt.opengl.GLCanvas)1 GLData (org.eclipse.swt.opengl.GLData)1 Display (org.eclipse.swt.widgets.Display)1 Event (org.eclipse.swt.widgets.Event)1 Listener (org.eclipse.swt.widgets.Listener)1 Shell (org.eclipse.swt.widgets.Shell)1 GLFWCursorPosCallback (org.lwjgl.glfw.GLFWCursorPosCallback)1 GLFWMouseButtonCallback (org.lwjgl.glfw.GLFWMouseButtonCallback)1 GLFWWindowFocusCallback (org.lwjgl.glfw.GLFWWindowFocusCallback)1 GLFWWindowSizeCallback (org.lwjgl.glfw.GLFWWindowSizeCallback)1 Callback (org.lwjgl.system.Callback)1