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:voxengine.Engine.java

private void init() {
    /* Set the error callback */
    glfwSetErrorCallback(errorCallback);

    /* Initialize GLFW */
    if (glfwInit() != GL_TRUE) {
        throw new IllegalStateException("Unable to initialize GLFW");
    }// w ww.  j a v  a 2s .  c om

    /* Create window */
    glfwDefaultWindowHints();
    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
    glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_ANY_PROFILE);
    window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
    if (window == NULL) {
        glfwTerminate();
        throw new RuntimeException("Failed to create the GLFW window");
    }

    /* Center the window on screen */
    ByteBuffer vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    glfwSetWindowPos(window, (GLFWvidmode.width(vidmode) - 640) / 2, (GLFWvidmode.height(vidmode) - 480) / 2);

    /* Create OpenGL context */
    glfwMakeContextCurrent(window);

    /* Enable vertical synchronization */
    glfwSwapInterval(1);

    /* Set the key callback */
    glfwSetKeyCallback(window, keyCallback);

    /* Set the cursor mode */
    //glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);

    /* Initialize timer */
    timer.init();

    GL.createCapabilities();
}

From source file:worldeditor.WorldEditor.java

public static void main(String[] args) {
    int minClientWidth = 640;
    int minClientHeight = 480;
    final Display display = new Display();
    final Shell shell = new Shell(display);

    shell.setLayout(new FillLayout());
    shell.setText("Button Example");

    final Button button = new Button(shell, SWT.PUSH);
    button.setText("Click Me");

    final Text text = new Text(shell, SWT.SHADOW_IN);

    button.addSelectionListener(new SelectionListener() {

        public void widgetSelected(SelectionEvent event) {
            text.setText("No worries!");
        }//from   www  .  ja  v a2  s  . c om

        public void widgetDefaultSelected(SelectionEvent event) {
            text.setText("No worries!");
        }
    });
    shell.addKeyListener(new KeyAdapter() {

        public void keyPressed(KeyEvent e) {
            if (e.stateMask == SWT.ALT && (e.keyCode == SWT.KEYPAD_CR || e.keyCode == SWT.CR)) {
                shell.setFullScreen(!shell.getFullScreen());
            }
        }
    });
    int dw = shell.getSize().x - shell.getClientArea().width;
    int dh = shell.getSize().y - shell.getClientArea().height;
    shell.setMinimumSize(minClientWidth + dw, minClientHeight + dh);
    GLData data = new GLData();
    data.doubleBuffer = true;
    final GLCanvas canvas = new GLCanvas(shell, SWT.NO_BACKGROUND | SWT.NO_REDRAW_RESIZE, data);
    canvas.setCurrent();
    GL.createCapabilities();

    final Rectangle rect = new Rectangle(0, 0, 0, 0);
    canvas.addListener(SWT.Resize, new Listener() {
        public void handleEvent(Event event) {
            Rectangle bounds = canvas.getBounds();
            rect.width = bounds.width;
            rect.height = bounds.height;
        }
    });
    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;
            }
        }
    });

    glClearColor(0.3f, 0.5f, 0.8f, 1.0f);

    // Create a simple shader program
    int program = glCreateProgram();
    int vs = glCreateShader(GL_VERTEX_SHADER);
    glShaderSource(vs,
            "uniform float rot;" + "uniform float aspect;" + "void main(void) {" + "  vec4 v = gl_Vertex * 0.5;"
                    + "  vec4 v_ = vec4(0.0, 0.0, 0.0, 1.0);" + "  v_.x = v.x * cos(rot) - v.y * sin(rot);"
                    + "  v_.y = v.y * cos(rot) + v.x * sin(rot);" + "  v_.x /= aspect;" + "  gl_Position = v_;"
                    + "}");
    glCompileShader(vs);
    glAttachShader(program, vs);
    int fs = glCreateShader(GL_FRAGMENT_SHADER);
    glShaderSource(fs, "void main(void) {" + "  gl_FragColor = vec4(0.1, 0.3, 0.5, 1.0);" + "}");
    glCompileShader(fs);
    glAttachShader(program, fs);
    glLinkProgram(program);
    glUseProgram(program);
    final int rotLocation = glGetUniformLocation(program, "rot");
    final int aspectLocation = glGetUniformLocation(program, "aspect");

    // Create a simple quad
    int vbo = glGenBuffers();
    int ibo = glGenBuffers();
    float[] vertices = { -1, -1, 0, 1, -1, 0, 1, 1, 0, -1, 1, 0 };
    int[] indices = { 0, 1, 2, 2, 3, 0 };
    glBindBuffer(GL_ARRAY_BUFFER, vbo);
    glBufferData(GL_ARRAY_BUFFER,
            (FloatBuffer) BufferUtils.createFloatBuffer(vertices.length).put(vertices).flip(), GL_STATIC_DRAW);
    glEnableVertexAttribArray(0);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER,
            (IntBuffer) BufferUtils.createIntBuffer(indices.length).put(indices).flip(), GL_STATIC_DRAW);
    glVertexAttribPointer(0, 3, GL_FLOAT, false, 0, 0L);

    shell.setSize(800, 600);
    shell.open();

    display.asyncExec(new Runnable() {
        float rot;
        long lastTime = System.nanoTime();

        public void run() {
            if (!canvas.isDisposed()) {
                canvas.setCurrent();
                glClear(GL_COLOR_BUFFER_BIT);
                glViewport(0, 0, rect.width, rect.height);

                float aspect = (float) rect.width / rect.height;
                glUniform1f(aspectLocation, aspect);
                glUniform1f(rotLocation, rot);
                glDrawElements(GL11.GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);

                canvas.swapBuffers();
                display.asyncExec(this);

                long thisTime = System.nanoTime();
                float delta = (thisTime - lastTime) / 1E9f;
                rot += delta;
                if (rot > 2.0 * Math.PI) {
                    rot -= 2.0f * (float) Math.PI;
                }
                lastTime = thisTime;
            }
        }
    });

    while (!shell.isDisposed()) {
        if (!display.readAndDispatch()) {
            display.sleep();
        }
    }
    display.dispose();
}