Search in sources :

Example 1 with GLFWErrorCallback

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

the class Demo20 method init.

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

        private 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, "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();
}
Also used : GLFWKeyCallback(org.lwjgl.glfw.GLFWKeyCallback) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) GLCapabilities(org.lwjgl.opengl.GLCapabilities) GLFWCursorPosCallback(org.lwjgl.glfw.GLFWCursorPosCallback) GLFWFramebufferSizeCallback(org.lwjgl.glfw.GLFWFramebufferSizeCallback) IntBuffer(java.nio.IntBuffer) GLFWVidMode(org.lwjgl.glfw.GLFWVidMode) GLFWMouseButtonCallback(org.lwjgl.glfw.GLFWMouseButtonCallback)

Example 2 with GLFWErrorCallback

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

the class ImmediateModeDemo 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_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
    window = glfwCreateWindow(width, height, "Immediate mode 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 && (ImmediateModeDemo.this.width != width || ImmediateModeDemo.this.height != height)) {
                ImmediateModeDemo.this.width = width;
                ImmediateModeDemo.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(1);
    glfwShowWindow(window);
    caps = GL.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)

Example 3 with GLFWErrorCallback

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

the class NoVerticesGridDemo 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 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 && (NoVerticesGridDemo.this.width != width || NoVerticesGridDemo.this.height != height)) {
                NoVerticesGridDemo.this.width = width;
                NoVerticesGridDemo.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)

Example 4 with GLFWErrorCallback

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

the class NoVerticesPolygonDemo 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 polygon 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 && (NoVerticesPolygonDemo.this.width != width || NoVerticesPolygonDemo.this.height != height)) {
                NoVerticesPolygonDemo.this.width = width;
                NoVerticesPolygonDemo.this.height = height;
            }
        }
    });
    System.out.println("Press 'arrow up' to increase the polygon vertex count");
    System.out.println("Press 'arrow down' to decrease the polygon vertex count");
    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)) {
                count++;
            } else if (key == GLFW_KEY_DOWN && (action == GLFW_RELEASE || action == GLFW_REPEAT)) {
                count = Math.max(3, count - 1);
            }
        }
    });
    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();
    // 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 5 with GLFWErrorCallback

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

the class SwtAndGlfwDemo method main.

public static void main(String[] args) {
    // Create SWT window
    Display display = new Display();
    final Shell shell = new Shell(display);
    shell.setText("SWT window");
    shell.setLayout(new FillLayout());
    shell.addListener(SWT.Traverse, new Listener() {

        public void handleEvent(Event event) {
            switch(event.detail) {
                case SWT.TRAVERSE_ESCAPE:
                    shell.close();
                    event.detail = SWT.TRAVERSE_NONE;
                    event.doit = false;
                    break;
                default:
                    break;
            }
        }
    });
    GLData data = new GLData();
    data.doubleBuffer = true;
    GLCanvas swtCanvas = new GLCanvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE, data);
    int dw = shell.getSize().x - shell.getClientArea().width;
    int dh = shell.getSize().y - shell.getClientArea().height;
    shell.setSize(800 + dw, 600 + dh);
    swtCanvas.setCurrent();
    GLCapabilities swtCapabilities = createCapabilities();
    Callback swtDebugProc = GLUtil.setupDebugMessageCallback();
    shell.open();
    // Create GLFW window
    GLFWErrorCallback errorCallback;
    glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));
    if (!glfwInit())
        throw new IllegalStateException("Unable to initialize GLFW");
    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_OPENGL_DEBUG_CONTEXT, GLFW_TRUE);
    long glfwWindow = glfwCreateWindow(800, 600, "GLFW window", NULL, NULL);
    GLFWKeyCallback keyCallback;
    glfwSetKeyCallback(glfwWindow, keyCallback = new GLFWKeyCallback() {

        public void invoke(long window, int key, int scancode, int action, int mods) {
            if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
                glfwSetWindowShouldClose(window, true);
        }
    });
    glfwMakeContextCurrent(glfwWindow);
    GLCapabilities glfwCapabilities = createCapabilities();
    Callback glfwDebugProc = GLUtil.setupDebugMessageCallback();
    glfwShowWindow(glfwWindow);
    while (!shell.isDisposed() && !glfwWindowShouldClose(glfwWindow)) {
        // Process window messages (for both SWT _and_ GLFW)
        display.readAndDispatch();
        // Render to SWT window
        if (!swtCanvas.isDisposed()) {
            swtCanvas.setCurrent();
            setCapabilities(swtCapabilities);
            glClearColor(0.2f, 0.4f, 0.6f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);
            swtCanvas.swapBuffers();
        }
        // Render to GLFW window
        if (glfwGetWindowAttrib(glfwWindow, GLFW_VISIBLE) == GLFW_TRUE) {
            glfwMakeContextCurrent(glfwWindow);
            setCapabilities(glfwCapabilities);
            glClearColor(0.2f, 0.3f, 0.4f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT);
            glfwSwapBuffers(glfwWindow);
        }
    }
    // Dispose of SWT
    if (swtDebugProc != null)
        swtDebugProc.free();
    if (!shell.isDisposed())
        shell.dispose();
    display.dispose();
    // Dispose of GLFW
    if (glfwDebugProc != null)
        glfwDebugProc.free();
    keyCallback.free();
    glfwDestroyWindow(glfwWindow);
    glfwTerminate();
    errorCallback.free();
}
Also used : GLFWKeyCallback(org.lwjgl.glfw.GLFWKeyCallback) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) Listener(org.eclipse.swt.widgets.Listener) FillLayout(org.eclipse.swt.layout.FillLayout) GLCanvas(org.eclipse.swt.opengl.GLCanvas) Shell(org.eclipse.swt.widgets.Shell) GLCapabilities(org.lwjgl.opengl.GLCapabilities) GLFWErrorCallback(org.lwjgl.glfw.GLFWErrorCallback) Callback(org.lwjgl.system.Callback) GLFWKeyCallback(org.lwjgl.glfw.GLFWKeyCallback) GLData(org.eclipse.swt.opengl.GLData) Event(org.eclipse.swt.widgets.Event) Display(org.eclipse.swt.widgets.Display)

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