Search code examples
javaopenglrenderinglwjgl

LWJGL Flickering Output When Attempting to Draw Triangle


I've been trying to get into OpenGL with LWJGL and I've run into an issue that I cannot find a solution to. When trying to draw a triangle with the code below, the window opens correctly and begins flashing a shape that isn't necessarily the intended triangle (sometimes it appears briefly, but often there are rectangles in one of the quadrants of the window).

Part of my hesitation is in how OpenGL, by my reading of various posts and docs online, has changed within recent memory to use a less functional and more an object-oriented approach (VBOs and GLSL?) with GL4. Am I correct in this understanding and what are the preferred resources for learning this newer OpenGL for LWJGL?

Thank you in advance!

import org.lwjgl.BufferUtils;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;

import java.nio.*;

import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;

public class Main {
  private long windowID;

  private float[] tri = {0.0f, 0.0f, 1.0f, 0.0f, 0.0f, 1.0f};

  public static void main(String[] args) {
    new Main().run();
  }

  public void run() { // Useful for making an instance class as opposed to a static main class?
    init();
    loop();

    glfwFreeCallbacks(windowID);
    glfwDestroyWindow(windowID);

    glfwTerminate();
    glfwSetErrorCallback(null).free();
  }

  public void init() { // Initializes all LWJGL components
    GLFWErrorCallback.createPrint(System.err).set(); // Create error callback route for GL

    if (!glfwInit()) { // Init GLFW
      throw new IllegalStateException("Failed to initialize GLFW!");
    } else {
      System.out.println("GLFW successfully initialized!");
    }

    windowID = glfwCreateWindow(640, 480, "Creating Window", NULL, NULL);

    if (windowID == NULL) { // Verify window creation
      throw new IllegalStateException("Failed to create window!");
    } else {
      System.out.println("Successfully created window!");
    }

    glfwDefaultWindowHints(); // Set window Proporties
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    glfwSetKeyCallback(windowID, (window, key, scancode, action, mods) -> { // Key callback for closing the window
      if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE)
        glfwSetWindowShouldClose(window, true);
    });

    try (MemoryStack stack = stackPush()) { // Center the window
      IntBuffer pWidth = stack.mallocInt(1);
      IntBuffer pHeight = stack.mallocInt(1);

      glfwGetWindowSize(windowID, pWidth, pHeight);

      GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

      glfwSetWindowPos( // Center the window
          windowID, 
          (vidmode.width() - pWidth.get(0)) / 2,
          (vidmode.height() - pHeight.get(0)) / 2
          );
    }

    glfwMakeContextCurrent(windowID); // Make the window current
    glfwSwapInterval(0); // Sets the min num of pushed frames before buffers are swaped (Likely prevents horizontal tearing)
    glfwShowWindow(windowID); // Unhides the window
  }

  private void loop() {
    GL.createCapabilities();
    glClearColor(0.0f, 0.0f, 0.0f, 0.0f); // The color to clear the buffers with

    while(!glfwWindowShouldClose(windowID)) { // If the window is allowed to live
      glClear(GL_COLOR_BUFFER_BIT); // The OR is nessesary for some reason

      FloatBuffer vBuff = BufferUtils.createFloatBuffer(6);
      vBuff.put(tri);
      glEnableClientState(GL_VERTEX_ARRAY);
      glVertexPointer(2, GL_FLOAT, 0, vBuff);
      glDrawArrays(GL_TRIANGLES, 0, 6);
      glDisableClientState(GL_VERTEX_ARRAY);

      glfwSwapBuffers(windowID);
      glfwPollEvents();
    } 
  }
}

Solution

  • You missed vBuff.flip() after the buffer was crated and filled.

    vBuff.put(tri) transfers the the data to the buffer, beginning at the current position (which is the start of the buffer in this case). The buffer position is incremented by the size of the data. So the new buffer position is at the end of the new data.

    flip() sets the limit (length) of the buffer to the current position and then the position is set to zero.

    Further, it is not necessary to create and fill the buffer continuously in the loop, it would be sufficient to do that once before the loop:

    FloatBuffer vBuff = BufferUtils.createFloatBuffer(6);
    vBuff.put(tri);
    vBuff.flip();
    
    while(!glfwWindowShouldClose(windowID)) {
        glClear(GL_COLOR_BUFFER_BIT);
    
        glEnableClientState(GL_VERTEX_ARRAY);
        glVertexPointer(2, GL_FLOAT, 0, vBuff);
        glDrawArrays(GL_TRIANGLES, 0, 6);
        glDisableClientState(GL_VERTEX_ARRAY);
    
        glfwSwapBuffers(windowID);
        glfwPollEvents();
    }