Пример #1
0
 private void InitGL() {
   GL.createCapabilities();
   System.out.println("OpenGL: " + glGetString(GL_VERSION));
   glEnable(GL13.GL_MULTISAMPLE);
   glEnable(GL_DEPTH_TEST);
   glViewport(0, 0, pix_width, pix_height);
 }
Пример #2
0
  private void init() {
    GLFW.glfwInit();
    window = GLFW.glfwCreateWindow(WIDTH, HEIGHT, "Robot Demo", 0, 0);
    GLFW.glfwMakeContextCurrent(window);
    GL.createCapabilities();

    robots = new Robot[2];
    robots[0] = new Robot();
    robots[0].setColor(0.4f, 0.1f, 0.0f);
    robots[1] = new Robot();
    robots[1].setColor(0.0f, 0.1f, 0.4f);
    selectedRobot = 0;

    GLFW.glfwSetKeyCallback(
        window,
        new GLFWKeyCallback() {
          @Override
          public void invoke(long window, int key, int scancode, int action, int mods) {
            if (key == GLFW.GLFW_KEY_ESCAPE) GLFW.glfwSetWindowShouldClose(window, GL11.GL_TRUE);
            if (key == GLFW.GLFW_KEY_P && action == GLFW.GLFW_PRESS)
              robots[selectedRobot].toggleBouncing();
            if (key == GLFW.GLFW_KEY_H && action == GLFW.GLFW_PRESS)
              robots[selectedRobot].toggleSelected("HEAD");
            if (key == GLFW.GLFW_KEY_N && action == GLFW.GLFW_PRESS)
              robots[selectedRobot].toggleSelected("NECK");
            if (key == GLFW.GLFW_KEY_T && action == GLFW.GLFW_PRESS)
              robots[selectedRobot].toggleSelected("TORSO");
            if (key == GLFW.GLFW_KEY_U && action == GLFW.GLFW_PRESS && mods != GLFW.GLFW_MOD_SHIFT)
              robots[selectedRobot].toggleSelected("UPPER_RIGHT_ARM");
            if (key == GLFW.GLFW_KEY_U && action == GLFW.GLFW_PRESS && mods == GLFW.GLFW_MOD_SHIFT)
              robots[selectedRobot].toggleSelected("UPPER_LEFT_ARM");
            if (key == GLFW.GLFW_KEY_L && action == GLFW.GLFW_PRESS && mods != GLFW.GLFW_MOD_SHIFT)
              robots[selectedRobot].toggleSelected("LOWER_RIGHT_ARM");
            if (key == GLFW.GLFW_KEY_L && action == GLFW.GLFW_PRESS && mods == GLFW.GLFW_MOD_SHIFT)
              robots[selectedRobot].toggleSelected("LOWER_LEFT_ARM");
            if (key == GLFW.GLFW_KEY_G && action == GLFW.GLFW_PRESS && mods != GLFW.GLFW_MOD_SHIFT)
              robots[selectedRobot].toggleSelected("RIGHT_THIGH");
            if (key == GLFW.GLFW_KEY_G && action == GLFW.GLFW_PRESS && mods == GLFW.GLFW_MOD_SHIFT)
              robots[selectedRobot].toggleSelected("LEFT_THIGH");
            if (key == GLFW.GLFW_KEY_C && action == GLFW.GLFW_PRESS && mods != GLFW.GLFW_MOD_SHIFT)
              robots[selectedRobot].toggleSelected("RIGHT_CALF");
            if (key == GLFW.GLFW_KEY_C && action == GLFW.GLFW_PRESS && mods == GLFW.GLFW_MOD_SHIFT)
              robots[selectedRobot].toggleSelected("LEFT_CALF");
            if (key == GLFW.GLFW_KEY_RIGHT && action != GLFW.GLFW_RELEASE)
              robots[selectedRobot].moveRight();
            if (key == GLFW.GLFW_KEY_LEFT && action != GLFW.GLFW_RELEASE)
              robots[selectedRobot].moveLeft();
            if (key == GLFW.GLFW_KEY_1 && action == GLFW.GLFW_PRESS) selectedRobot = 0;
            if (key == GLFW.GLFW_KEY_2 && action == GLFW.GLFW_PRESS) selectedRobot = 1;
          }
        });
  }
Пример #3
0
  public void init() {
    // if not true after being executed then it will fail. - THIS MUST RETURN TRUE IN ORDER TO
    // CONTINUE
    if (glfwInit() != GL_TRUE) {
      // Once GLFW is initialised - window can be created
      System.err.println("GLFW initialisation has failed");
      throw new IllegalStateException("Unable to initialize GLFW");
    }

    // indicate that the window we wish to create is to be made resizable
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE);

    //           glfwCreateWindow(width, height, title, monitor, share)
    window = glfwCreateWindow(800, 600, "Mikes Tester Window", NULL, NULL);

    // if window is not populated with necessary bytes - then it fails
    if (window == NULL) {
      System.err.println("Failed to create window");
      glfwTerminate();
      throw new RuntimeException("Failed to create the GLFW window");
    }

    glfwSetKeyCallback(window, keyCallback = new Input());

    // Returns the video resolution of primary monitor
    GLFWVidMode vidMode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    //

    int WIDTH = 300;
    int HEIGHT = 300;

    // Centre the window
    glfwSetWindowPos(window, (vidMode.width() - WIDTH) / 2, (vidMode.height() - HEIGHT) / 2);

    // Make the OpenGL context current
    glfwMakeContextCurrent(window);

    // library can detect the context and make the OpenGL bindings available for use.
    GL.createCapabilities();

    // Enable v-sync
    glfwSwapInterval(1);
    // show the window
    glfwShowWindow(window);

    // glClearColor(056f, 0.258f, 0.425f, 1.0f);

    glEnable(GL_DEPTH_TEST);

    System.out.println("OPenGL: " + glGetString(GL_VERSION));
  }
Пример #4
0
  private void loop() {
    GL.createCapabilities();
    System.out.println("OPENGL VERSION " + glGetString(GL_VERSION));
    glClearColor(0, 0, 0, 0);
    glEnable(GL_TEXTURE_2D);
    glDisable(GL_DEPTH_TEST);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    glOrtho(0, WORLD_X, WORLD_Y, 0, -1, 1);

    while (!glfwWindowShouldClose(winID)) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
      drawMe();
      glfwSwapBuffers(winID);
      glfwPollEvents();
    }
  }
Пример #5
0
  private static void initWindow(int w, int h) {
    HEIGHT = h;
    WIDTH = w;
    // Configure our window
    glfwDefaultWindowHints(); // optional, the current window hints are already the default
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
    glfwWindowHint(GLFW_RESIZABLE, GL_FALSE);
    glfwWindowHint(GLFW_DECORATED, GL_FALSE);
    glfwWindowHint(GLFW_DOUBLE_BUFFER, GL_TRUE);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 1);
    glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
    // glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
    // glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT, GL_TRUE);

    // Create the window
    window = glfwCreateWindow(WIDTH, HEIGHT, "GameEngine", glfwGetPrimaryMonitor(), NULL);
    if (window == NULL) throw new RuntimeException("Failed to create the GLFW window");

    // Get the resolution of the primary monitor
    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    // Center our window
    glfwSetWindowPos(window, (vidmode.width() - WIDTH) / 2, (vidmode.height() - HEIGHT) / 2);

    // Make the OpenGL context current
    glfwMakeContextCurrent(window);
    // GLContext.createFromCurrent();
    // Enable v-sync
    glfwSwapInterval(1);
    GL.createCapabilities();
    glEnable(GL_BLEND);
    glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
    // glEnable(GL_DEPTH_TEST);
    // glDepthFunc(GL_LESS);
    // glDepthMask(true);
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    // glDisable(GL_DEPTH_TEST);

    glClearColor(0.20f, 0.10f, 0.20f, 1.0f);
  }
Пример #6
0
  private void loop() {
    // This line is critical for LWJGL's interoperation with GLFW's
    // OpenGL context, or any context that is managed externally.
    // LWJGL detects the context that is current in the current thread,
    // creates the ContextCapabilities instance and makes the OpenGL
    // bindings available for use.
    GL.createCapabilities(true); // valid for latest build
    GLContext.createFromCurrent(); // use this line instead with the 3.0.0a build

    // Set the clear color
    glClearColor(1.0f, 0.0f, 0.0f, 0.0f);

    // Run the rendering loop until the user has attempted to close
    // the window or has pressed the ESCAPE key.
    while (glfwWindowShouldClose(window) == GL_FALSE) {
      update();
      render();

      // Poll for window events. The key callback above will only be
      // invoked during this call.
      glfwPollEvents();
    }
  }
Пример #7
0
  private void loop() {
    // This line is critical for LWJGL's interoperation with GLFW's
    // OpenGL context, or any context that is managed externally.
    // LWJGL detects the context that is current in the current thread,
    // creates the GLCapabilities instance and makes the OpenGL
    // bindings available for use.
    GL.createCapabilities();

    // Set the clear color
    glClearColor(0.1f, 0.7f, 1.0f, 0.0f);

    // Run the rendering loop until the user has attempted to close
    // the window or has pressed the ESCAPE key.
    while (glfwWindowShouldClose(window) == GLFW_FALSE) {
      glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer

      glfwSwapBuffers(window); // swap the color buffers

      // Poll for window events. The key callback above will only be
      // invoked during this call.
      glfwPollEvents();
    }
  }
Пример #8
0
  public void init() {
    // Setup an error callback. The default implementation
    // will print the error message in System.err.
    glfwSetErrorCallback(errorCallback = GLFWErrorCallback.createPrint(System.err));

    // Initialize GLFW. Most GLFW functions will not work before doing this.
    if (glfwInit() != GL11.GL_TRUE) {
      throw new IllegalStateException("Unable to initialize GLFW");
    }

    glfwDefaultWindowHints(); // optional, the current window hints are already the default
    glfwWindowHint(GLFW_VISIBLE, GL_FALSE); // the window will stay hidden after creation
    glfwWindowHint(GLFW_RESIZABLE, GL_TRUE); // the window will be resizable

    // Create the window
    windowHandle = glfwCreateWindow(width, height, title, NULL, NULL);
    if (windowHandle == NULL) {
      throw new RuntimeException("Failed to create the GLFW window");
    }

    // Setup resize callback
    glfwSetWindowSizeCallback(
        windowHandle,
        windowSizeCallback =
            new GLFWWindowSizeCallback() {
              @Override
              public void invoke(long window, int width, int height) {
                Window.this.width = width;
                Window.this.height = height;
                Window.this.setResized(true);
              }
            });
    // Setup a key callback. It will be called every time a key is pressed, repeated or released.
    glfwSetKeyCallback(
        windowHandle,
        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, GL_TRUE);
                }
              }
            });

    // Get the resolution of the primary monitor
    GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
    // Center our window
    glfwSetWindowPos(windowHandle, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2);

    // Make the OpenGL context current
    glfwMakeContextCurrent(windowHandle);
    // Enable v-sync
    glfwSwapInterval(1);

    // Make the window visible
    glfwShowWindow(windowHandle);

    GL.createCapabilities();

    // Set the clear color
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
    glEnable(GL_DEPTH_TEST);
  }
Пример #9
0
  public Mandelbrot(
      long platform,
      CLCapabilities platformCaps,
      GLFWWindow window,
      int deviceType,
      boolean debugGL,
      int maxIterations) {
    this.platform = platform;

    this.window = window;
    this.maxIterations = maxIterations;

    IntBuffer size = BufferUtils.createIntBuffer(2);

    nglfwGetWindowSize(window.handle, memAddress(size), memAddress(size) + 4);
    ww = size.get(0);
    wh = size.get(1);

    nglfwGetFramebufferSize(window.handle, memAddress(size), memAddress(size) + 4);
    fbw = size.get(0);
    fbh = size.get(1);

    glfwMakeContextCurrent(window.handle);
    GLCapabilities glCaps = GL.createCapabilities();
    if (!glCaps.OpenGL30) throw new RuntimeException("OpenGL 3.0 is required to run this demo.");

    debugProc = debugGL ? GLUtil.setupDebugMessageCallback() : null;

    glfwSwapInterval(0);

    errcode_ret = BufferUtils.createIntBuffer(1);

    try {
      // Find devices with GL sharing support
      {
        long device = getDevice(platform, platformCaps, deviceType);
        if (device == NULL) device = getDevice(platform, platformCaps, CL_DEVICE_TYPE_CPU);

        if (device == NULL)
          throw new RuntimeException("No OpenCL devices found with OpenGL sharing support.");

        this.device = device;
        this.deviceCaps = CL.createDeviceCapabilities(device, platformCaps);
      }

      // Create the context
      PointerBuffer ctxProps = BufferUtils.createPointerBuffer(7);
      switch (Platform.get()) {
        case WINDOWS:
          ctxProps
              .put(CL_GL_CONTEXT_KHR)
              .put(glfwGetWGLContext(window.handle))
              .put(CL_WGL_HDC_KHR)
              .put(wglGetCurrentDC());
          break;
        case LINUX:
          ctxProps
              .put(CL_GL_CONTEXT_KHR)
              .put(glfwGetGLXContext(window.handle))
              .put(CL_GLX_DISPLAY_KHR)
              .put(glfwGetX11Display());
          break;
        case MACOSX:
          ctxProps
              .put(APPLEGLSharing.CL_CONTEXT_PROPERTY_USE_CGL_SHAREGROUP_APPLE)
              .put(CGLGetShareGroup(CGLGetCurrentContext()));
      }
      ctxProps.put(CL_CONTEXT_PLATFORM).put(platform).put(NULL).flip();
      clContext =
          clCreateContext(
              ctxProps,
              device,
              clContextCB =
                  new CLContextCallback() {
                    @Override
                    public void invoke(long errinfo, long private_info, long cb, long user_data) {
                      log(String.format("cl_context_callback\n\tInfo: %s", memUTF8(errinfo)));
                    }
                  },
              NULL,
              errcode_ret);
      checkCLError(errcode_ret);

      // create command queues for every GPU, setup colormap and init kernels

      IntBuffer colorMapBuffer = BufferUtils.createIntBuffer(32 * 2);
      initColorMap(colorMapBuffer, 32, Color.BLUE, Color.GREEN, Color.RED);

      clColorMap =
          clCreateBuffer(
              clContext, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, colorMapBuffer, errcode_ret);
      checkCLError(errcode_ret);

      // create command queue and upload color map buffer
      clQueue = clCreateCommandQueue(clContext, device, NULL, errcode_ret);
      checkCLError(errcode_ret);

      // load program(s)
      if (deviceType == CL_DEVICE_TYPE_GPU)
        log("OpenCL Device Type: GPU (Use -forceCPU to use CPU)");
      else log("OpenCL Device Type: CPU");

      log("Max Iterations: " + maxIterations + " (Use -iterations <count> to change)");
      log("Display resolution: " + ww + "x" + wh + " (Use -res <width> <height> to change)");

      log("OpenGL glCaps.GL_ARB_sync = " + glCaps.GL_ARB_sync);
      log("OpenGL glCaps.GL_ARB_cl_event = " + glCaps.GL_ARB_cl_event);

      buildProgram();

      // Detect GLtoCL synchronization method
      syncGLtoCL = !glCaps.GL_ARB_cl_event; // GL3.2 or ARB_sync implied
      log(syncGLtoCL ? "GL to CL sync: Using clFinish" : "GL to CL sync: Using OpenCL events");

      // Detect CLtoGL synchronization method
      syncCLtoGL = !deviceCaps.cl_khr_gl_event;
      log(
          syncCLtoGL
              ? "CL to GL sync: Using glFinish"
              : "CL to GL sync: Using implicit sync (cl_khr_gl_event)");

      vao = glGenVertexArrays();
      glBindVertexArray(vao);

      vbo = glGenBuffers();
      glBindBuffer(GL_ARRAY_BUFFER, vbo);

      glBufferData(
          GL_ARRAY_BUFFER,
          stackPush()
              .floats(
                  0.0f, 0.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f, 0.0f, 1.0f, 1.0f,
                  1.0f, 1.0f, 1.0f),
          GL_STATIC_DRAW);
      stackPop();

      vsh = glCreateShader(GL_VERTEX_SHADER);
      glShaderSource(
          vsh,
          "#version 150\n"
              + "\n"
              + "uniform mat4 projection;\n"
              + "\n"
              + "uniform vec2 size;\n"
              + "\n"
              + "in vec2 posIN;\n"
              + "in vec2 texIN;\n"
              + "\n"
              + "out vec2 texCoord;\n"
              + "\n"
              + "void main(void) {\n"
              + "\tgl_Position = projection * vec4(posIN * size, 0.0, 1.0);\n"
              + "\ttexCoord = texIN;\n"
              + "}");
      glCompileShader(vsh);
      String log = glGetShaderInfoLog(vsh, glGetShaderi(vsh, GL_INFO_LOG_LENGTH));
      if (!log.isEmpty()) log(String.format("VERTEX SHADER LOG: %s", log));

      fsh = glCreateShader(GL_FRAGMENT_SHADER);
      glShaderSource(
          fsh,
          "#version 150\n"
              + "\n"
              + "uniform isampler2D mandelbrot;\n"
              + "\n"
              + "in vec2 texCoord;\n"
              + "\n"
              + "out vec4 fragColor;\n"
              + "\n"
              + "void main(void) {\n"
              + "\tfragColor = texture(mandelbrot, texCoord) / 255.0;\n"
              + "}");
      glCompileShader(fsh);
      log = glGetShaderInfoLog(fsh, glGetShaderi(fsh, GL_INFO_LOG_LENGTH));
      if (!log.isEmpty()) log(String.format("FRAGMENT SHADER LOG: %s", log));

      glProgram = glCreateProgram();
      glAttachShader(glProgram, vsh);
      glAttachShader(glProgram, fsh);
      glLinkProgram(glProgram);
      log = glGetProgramInfoLog(glProgram, glGetProgrami(glProgram, GL_INFO_LOG_LENGTH));
      if (!log.isEmpty()) log(String.format("PROGRAM LOG: %s", log));

      int posIN = glGetAttribLocation(glProgram, "posIN");
      int texIN = glGetAttribLocation(glProgram, "texIN");

      glVertexAttribPointer(posIN, 2, GL_FLOAT, false, 4 * 4, 0);
      glVertexAttribPointer(texIN, 2, GL_FLOAT, false, 4 * 4, 2 * 4);

      glEnableVertexAttribArray(posIN);
      glEnableVertexAttribArray(texIN);

      projectionUniform = glGetUniformLocation(glProgram, "projection");
      sizeUniform = glGetUniformLocation(glProgram, "size");

      glUseProgram(glProgram);

      glUniform1i(glGetUniformLocation(glProgram, "mandelbrot"), 0);
    } catch (Exception e) {
      // TODO: cleanup
      throw new RuntimeException(e);
    }

    glDisable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

    initGLObjects();
    glFinish();

    setKernelConstants();

    glfwSetWindowSizeCallback(
        window.handle,
        window.windowsizefun =
            new GLFWWindowSizeCallback() {
              @Override
              public void invoke(long window, final int width, final int height) {
                if (width == 0 || height == 0) return;

                events.add(
                    new Runnable() {
                      @Override
                      public void run() {
                        Mandelbrot.this.ww = width;
                        Mandelbrot.this.wh = height;

                        shouldInitBuffers = true;
                      }
                    });
              }
            });

    glfwSetFramebufferSizeCallback(
        window.handle,
        window.framebuffersizefun =
            new GLFWFramebufferSizeCallback() {
              @Override
              public void invoke(long window, final int width, final int height) {
                if (width == 0 || height == 0) return;

                events.add(
                    new Runnable() {
                      @Override
                      public void run() {
                        Mandelbrot.this.fbw = width;
                        Mandelbrot.this.fbh = height;

                        shouldInitBuffers = true;
                      }
                    });
              }
            });

    glfwSetKeyCallback(
        window.handle,
        window.keyfun =
            new GLFWKeyCallback() {
              @Override
              public void invoke(long window, int key, int scancode, int action, int mods) {
                switch (key) {
                  case GLFW_KEY_LEFT_CONTROL:
                  case GLFW_KEY_RIGHT_CONTROL:
                    ctrlDown = action == GLFW_PRESS;
                    return;
                }

                if (action != GLFW_PRESS) return;

                switch (key) {
                  case GLFW_KEY_ESCAPE:
                    glfwSetWindowShouldClose(window, GLFW_TRUE);
                    break;
                  case GLFW_KEY_D:
                    events.offer(
                        new Runnable() {
                          @Override
                          public void run() {
                            doublePrecision = !doublePrecision;
                            log("DOUBLE PRECISION IS NOW: " + (doublePrecision ? "ON" : "OFF"));
                            rebuild = true;
                          }
                        });
                    break;
                  case GLFW_KEY_HOME:
                    events.offer(
                        new Runnable() {
                          @Override
                          public void run() {
                            offsetX = -0.5;
                            offsetY = 0.0;
                            zoom = 1.0;
                          }
                        });
                    break;
                }
              }
            });

    glfwSetMouseButtonCallback(
        window.handle,
        window.mousebuttonfun =
            new GLFWMouseButtonCallback() {
              @Override
              public void invoke(long window, int button, int action, int mods) {
                if (button != GLFW_MOUSE_BUTTON_LEFT) return;

                dragging = action == GLFW_PRESS;

                if (dragging) {
                  dragging = true;

                  dragX = mouseX;
                  dragY = mouseY;

                  dragOffsetX = offsetX;
                  dragOffsetY = offsetY;
                }
              }
            });

    glfwSetCursorPosCallback(
        window.handle,
        window.cursorposfun =
            new GLFWCursorPosCallback() {
              @Override
              public void invoke(long window, double xpos, double ypos) {
                mouseX = xpos;
                mouseY = wh - ypos;

                if (dragging) {
                  offsetX = dragOffsetX + transformX(dragX - mouseX);
                  offsetY = dragOffsetY + transformY(dragY - mouseY);
                }
              }
            });

    glfwSetScrollCallback(
        window.handle,
        window.scrollfun =
            new GLFWScrollCallback() {
              @Override
              public void invoke(long window, double xoffset, double yoffset) {
                if (yoffset == 0) return;

                double scrollX = mouseX - ww * 0.5;
                double scrollY = mouseY - wh * 0.5;

                double zoomX = transformX(scrollX);
                double zoomY = transformY(scrollY);

                zoom *= (1.0 - yoffset * (ctrlDown ? 0.25 : 0.05));

                offsetX += zoomX - transformX(scrollX);
                offsetY += zoomY - transformY(scrollY);
              }
            });
  }
Пример #10
0
  static long createGlfwWindow(Lwjgl3ApplicationConfiguration config, long sharedContextWindow) {
    GLFW.glfwDefaultWindowHints();
    GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
    GLFW.glfwWindowHint(
        GLFW.GLFW_RESIZABLE, config.windowResizable ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);

    if (sharedContextWindow == 0) {
      GLFW.glfwWindowHint(GLFW.GLFW_RED_BITS, config.r);
      GLFW.glfwWindowHint(GLFW.GLFW_GREEN_BITS, config.g);
      GLFW.glfwWindowHint(GLFW.GLFW_BLUE_BITS, config.b);
      GLFW.glfwWindowHint(GLFW.GLFW_ALPHA_BITS, config.a);
      GLFW.glfwWindowHint(GLFW.GLFW_STENCIL_BITS, config.stencil);
      GLFW.glfwWindowHint(GLFW.GLFW_DEPTH_BITS, config.depth);
      GLFW.glfwWindowHint(GLFW.GLFW_SAMPLES, config.samples);
    }

    if (config.useGL30) {
      // GLFW.glfwWindowHint(GLFW.GLFW_CLIENT_API, GLFW.GLFW_OPENGL_API);
      GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MAJOR, config.gles30ContextMajorVersion);
      GLFW.glfwWindowHint(GLFW.GLFW_CONTEXT_VERSION_MINOR, config.gles30ContextMinorVersion);
      GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_FORWARD_COMPAT, GLFW.GLFW_TRUE);
      GLFW.glfwWindowHint(GLFW.GLFW_OPENGL_PROFILE, GLFW.GLFW_OPENGL_CORE_PROFILE);
    }

    long windowHandle = 0;

    if (config.fullscreenMode != null) {
      // glfwWindowHint(GLFW.GLFW_REFRESH_RATE, config.fullscreenMode.refreshRate);
      windowHandle =
          GLFW.glfwCreateWindow(
              config.fullscreenMode.width,
              config.fullscreenMode.height,
              config.title,
              config.fullscreenMode.getMonitor(),
              sharedContextWindow);
    } else {
      GLFW.glfwWindowHint(
          GLFW.GLFW_DECORATED, config.windowDecorated ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);
      windowHandle =
          GLFW.glfwCreateWindow(
              config.windowWidth, config.windowHeight, config.title, 0, sharedContextWindow);
    }
    if (windowHandle == 0) {
      throw new GdxRuntimeException("Couldn't create window");
    }
    if (config.fullscreenMode == null) {
      if (config.windowX == -1 && config.windowY == -1) {
        GLFWVidMode vidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
        GLFW.glfwSetWindowPos(
            windowHandle,
            vidMode.width() / 2 - config.windowWidth / 2,
            vidMode.height() / 2 - config.windowHeight / 2);
      } else {
        GLFW.glfwSetWindowPos(windowHandle, config.windowX, config.windowY);
      }
    }
    GLFW.glfwMakeContextCurrent(windowHandle);
    GLFW.glfwSwapInterval(config.vSyncEnabled ? 1 : 0);
    GL.createCapabilities();

    extractVersion();
    if (!isOpenGLOrHigher(2, 0))
      throw new GdxRuntimeException(
          "OpenGL 2.0 or higher with the FBO extension is required. OpenGL version: "
              + GL11.glGetString(GL11.GL_VERSION)
              + "\n"
              + glInfo());

    if (!supportsFBO()) {
      throw new GdxRuntimeException(
          "OpenGL 2.0 or higher with the FBO extension is required. OpenGL version: "
              + GL11.glGetString(GL11.GL_VERSION)
              + ", FBO extension: false\n"
              + glInfo());
    }

    for (int i = 0; i < 2; i++) {
      GL11.glClearColor(
          config.initialBackgroundColor.r,
          config.initialBackgroundColor.g,
          config.initialBackgroundColor.b,
          config.initialBackgroundColor.a);
      GL11.glClear(GL11.GL_COLOR_BUFFER_BIT);
      GLFW.glfwSwapBuffers(windowHandle);
    }
    return windowHandle;
  }
Пример #11
0
  public void initialize(
      float fieldOfView, int w, int h, float zn, float zf, boolean resizable, long share) {
    if (init) {
      return;
    }
    FOV = fieldOfView;
    zNear = zn;
    zFar = zf;
    width = w;
    height = h;

    Platform.setDefaultGLFWWindowConfigurations();
    GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, resizable ? GLFW.GLFW_TRUE : GLFW.GLFW_FALSE);

    window =
        GLFW.glfwCreateWindow(
            w,
            h,
            title,
            MemoryUtil.NULL,
            share == Registry.INVALID ? Nemgine.getOpenGLContext() : share);
    if (window == MemoryUtil.NULL) {
      new WindowException(Registry.WINDOW_EXCEPTION_FAILED_TO_CREATE).printStackTrace();
      return;
    }
    GLFWVidMode mode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
    GLFW.glfwSetWindowPos(window, (mode.width() - w) / 2, (mode.height() - h) / 2);

    sizeCallback =
        new GLFWWindowSizeCallback() {
          @Override
          public void invoke(long window, int wi, int he) {
            width = wi;
            height = he;
            resized = true;
          }
        };
    keyCallback =
        new GLFWKeyCallback() {
          @Override
          public void invoke(long window, int key, int scancode, int action, int mods) {
            if (input != null) {
              input.keyEvent(key, scancode, action, mods);
            }
          }
        };
    charCallback =
        new GLFWCharCallback() {
          @Override
          public void invoke(long window, int codepoint) {
            if (input != null) {
              input.charEvent((char) codepoint);
            }
          }
        };
    charModsCallback =
        new GLFWCharModsCallback() {
          @Override
          public void invoke(long window, int codepoint, int mods) {
            if (input != null) {
              input.charModsEvent((char) codepoint, mods);
            }
          }
        };

    GLFW.glfwSetWindowSizeCallback(window, sizeCallback);
    GLFW.glfwSetKeyCallback(window, keyCallback);
    GLFW.glfwSetCharCallback(window, charCallback);
    GLFW.glfwSetCharModsCallback(window, charModsCallback);
    GLFW.glfwMakeContextCurrent(window);
    GL.createCapabilities(Registry.OPENGL_FORWARD_COMPATIBLE);
    GLFW.glfwShowWindow(window);
    IntBuffer b1 = BufferUtils.createIntBuffer(1);
    IntBuffer b2 = BufferUtils.createIntBuffer(1);
    GLFW.glfwGetFramebufferSize(window, b1, b2);
    GL11.glViewport(0, 0, b1.get(), b2.get());
    GL11.glEnable(GL11.GL_DEPTH_TEST);
    GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA);
    resize();
    init = true;
  }