Search code examples
javauser-interfacelwjglglfw

Color oscillates between black and red in open gl


i wrote a few java classes with lwjgl 3 and opengl that create a window with red color but the color actually oscillates probably each frame between red and back here's the code for window class

This window class is used as object in the main class which has a game loop

package engine.io.output;

import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.GL_DEPTH_BUFFER_BIT;

import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.*;

import engine.io.input.*;

public class Window {
public int width , height;
public String title;
long time;
int frames = 0;
private long window;
public KeyInput keyInputCallBack = new KeyInput();
MouseButtonInput mouseButtonInput = new MouseButtonInput();
MousePositionInput mPositionInput = new MousePositionInput();
public Window(int width , int height , String title) {
    this.width = width;
    this.height = height;
    this.title = title;
}
public void create() {
    time = System.currentTimeMillis();
    if (!glfwInit()) {
        System.err.println("Couldnt init");
        System.exit(-1);
    }
    window = glfwCreateWindow(width, height, title,0, 0);
    
    if (window == 0) {
        System.err.println("cannot create window");
        System.exit(-1);
    }
    GLFWVidMode vm = glfwGetVideoMode(glfwGetPrimaryMonitor());
    
    int windowXPos = vm.width()/2 - width/2;
    int windowYPos = vm.height()/2 - height/2;
    
    glfwSetWindowPos(window, windowXPos,windowYPos);
    
    addCallback(window);
    glfwMakeContextCurrent(window);
    
    GL.createCapabilities();
    GL11.glClearColor(1.0f, 0.0f, 0.0f, 0.0f);
    GL11.glClear(GL11.GL_COLOR_BUFFER_BIT |GL_DEPTH_BUFFER_BIT);
    glfwShowWindow(window);
    glfwSwapInterval(1);
    
}
private void addCallback(long window2) {
    glfwSetKeyCallback(window,keyInputCallBack.getKeyCallback());
    glfwSetMouseButtonCallback(window2, mouseButtonInput.getbuttonCallback());
    glfwSetCursorPosCallback(window2, mPositionInput.getMousePositionCallback());
}
public void update() {
    frames++;
    if (System.currentTimeMillis() >= time + 1000) {
        glfwSetWindowTitle(window,"fps is " + frames);
        frames = 0;
        time = System.currentTimeMillis();
    }
    glfwPollEvents();
}
public void render() {
    glfwSwapBuffers(window);
}
public boolean shouldClose() {
    return glfwWindowShouldClose(window);
    
}}

How can i stop the oscilation


Solution

  • As per the comments above, in your original code you seem to be only clearing once, when the window is created, whereas typically you'd clear once each frame. (I'm guessing the flickering you're seeing is due to only one of multiple frame buffers being cleared to red, but that's just a guess.)

    Note that although clearing in update() (as you mention above) may work, depending on how the environment is set up, the render() function would probably be a more appropriate place to do this. Most likely the context is current when update() is called, and therefore you can get away with rendering there, but conceptually at least, it's probably best to keep update logic and rendering in their respective dedicated functions.