Problem with background color in OpenGL

I have simple problem for professionalist (that i think ) but i asked all available AI about it and none was able to help and solve problem. so nothing is able to help me in this.

I have simple app that show 3 colored axes and camera that move around this scene by mouse click-hold . the problem is that after run appear first white backfround of frame for a second then its normal black background. i dont want this white at beginning.
can you tell me what in code change to get rid of this white flash at beginning ? I tried already plenty things.

This is the project code IntellJ, java , maven , with additional newest libraries lwjgl (lwjgl-release-3.31) and joml ( joml-1.10.61 ), 2 classes : Main and Camera , code is :
“”

package org.example;
import org.joml.Matrix4f;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.glfw.GLFWErrorCallback;
import org.lwjgl.opengl.GL;
import org.lwjgl.system.MemoryStack;

import java.nio.FloatBuffer;
import java.nio.IntBuffer;
import java.util.Objects;

import static org.lwjgl.glfw.Callbacks.glfwFreeCallbacks;
import static org.lwjgl.glfw.GLFW.;
import static org.lwjgl.opengl.GL11.
;
import static org.lwjgl.system.MemoryStack.stackPush;
import static org.lwjgl.system.MemoryUtil.NULL;

public class Main {

private long window;
private Camera camera;
private float lastMouseX, lastMouseY;
private boolean firstMouse = true;
private boolean mousePressed = false;

public void run() {
    init();
    loop();

    glfwFreeCallbacks(window);
    glfwDestroyWindow(window);

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

private void init() {
    GLFWErrorCallback.createPrint(System.err).set();

    if (!glfwInit()) {
        throw new IllegalStateException("Unable to initialize GLFW");
    }

    glfwDefaultWindowHints();
    glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE);
    glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE);

    window = glfwCreateWindow(800, 600, "3D Scene", NULL, NULL);
    if (window == NULL) {
        throw new RuntimeException("Failed to create the GLFW window");
    }

    glfwSetCursorPosCallback(window, (window, xpos, ypos) -> {
        if (firstMouse) {
            lastMouseX = (float) xpos;
            lastMouseY = (float) ypos;
            firstMouse = false;
        }

        if (mousePressed) {
            float dx = (float) xpos - lastMouseX;
            float dy = (float) ypos - lastMouseY;
            lastMouseX = (float) xpos;
            lastMouseY = (float) ypos;

            camera.rotate(dy * 0.1f, dx * 0.1f);
        }
    });

    glfwSetMouseButtonCallback(window, (window, button, action, mods) -> {
        if (button == GLFW_MOUSE_BUTTON_LEFT) {
            if (action == GLFW_PRESS) {
                mousePressed = true;
                firstMouse = true; // Reset firstMouse when the button is pressed
            } else if (action == GLFW_RELEASE) {
                mousePressed = false;
            }
        }
    });

    glfwSetScrollCallback(window, (window, xoffset, yoffset) -> {
        camera.zoom((float) yoffset * -0.5f);
    });

    glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> {
        if (key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE) {
            glfwSetWindowShouldClose(window, true);
        }
    });

    try (MemoryStack stack = stackPush()) {
        IntBuffer pWidth = stack.mallocInt(1);
        IntBuffer pHeight = stack.mallocInt(1);

        glfwGetWindowSize(window, pWidth, pHeight);

        GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());

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

    glfwMakeContextCurrent(window);
    glfwSwapInterval(1);


    GL.createCapabilities();

    glEnable(GL_DEPTH_TEST);
    glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
    // Set the projection matrix
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
    float aspectRatio = 800.0f / 600.0f;
    glFrustum(-aspectRatio, aspectRatio, -1, 1, 1, 100);

    camera = new Camera(20, 30, 45);
    glfwShowWindow(window);
}

private void loop() {
    while (!glfwWindowShouldClose(window)) {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        Matrix4f viewMatrix = camera.getViewMatrix();
        FloatBuffer fb = org.lwjgl.BufferUtils.createFloatBuffer(16);
        viewMatrix.get(fb);

        glMatrixMode(GL_MODELVIEW);
        glLoadMatrixf(fb);

        drawAxes();

        glfwSwapBuffers(window);
        glfwPollEvents();
    }
}

private void drawAxes() {
    glBegin(GL_LINES);

    // X axis in red
    glColor3f(1.0f, 0.0f, 0.0f);
    glVertex3f(-20.0f, 0.0f, 0.0f);
    glVertex3f(20.0f, 0.0f, 0.0f);

    // Y axis in green
    glColor3f(0.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, -20.0f, 0.0f);
    glVertex3f(0.0f, 20.0f, 0.0f);

    // Z axis in yellow
    glColor3f(1.0f, 1.0f, 0.0f);
    glVertex3f(0.0f, 0.0f, -20.0f);
    glVertex3f(0.0f, 0.0f, 20.0f);

    glEnd();
}

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

}
"

and Camera code :
"

package org.example;
import org.joml.Matrix4f;
import org.joml.Vector3f;

public class Camera {

private Vector3f position;
private float pitch;
private float yaw;
private float distance;

public Camera(float distance, float pitch, float yaw) {
    this.position = new Vector3f(0, 0, 0);
    this.pitch = pitch;
    this.yaw = yaw;
    this.distance = distance;
}

public Matrix4f getViewMatrix() {
    Matrix4f viewMatrix = new Matrix4f();
    viewMatrix.identity();

    // Calculate the camera position based on spherical coordinates
    float x = (float) (distance * Math.sin(Math.toRadians(pitch)) * Math.cos(Math.toRadians(yaw)));
    float y = (float) (distance * Math.cos(Math.toRadians(pitch)));
    float z = (float) (distance * Math.sin(Math.toRadians(pitch)) * Math.sin(Math.toRadians(yaw)));

    position.set(x, y, z);

    viewMatrix.lookAt(position, new Vector3f(0, 0, 0), new Vector3f(0, 1, 0));
    return viewMatrix;
}

public void rotate(float pitchDelta, float yawDelta) {
    this.pitch += pitchDelta;
    this.yaw += yawDelta;
}

public void zoom(float distanceDelta) {
    this.distance += distanceDelta;
    if (this.distance < 1.0f) {
        this.distance = 1.0f; // Prevent camera from getting too close
    }
}

public Vector3f getPosition() {
    return position;
}

public float getPitch() {
    return pitch;
}

public float getYaw() {
    return yaw;
}

public float getDistance() {
    return distance;
}

}
"

and pom.xml file is : "
4.0.0

<groupId>org.example</groupId>
<artifactId>3dscene</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
</properties>

<dependencies>
    <!-- LWJGL dependencies -->
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-glfw</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-opengl</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-stb</artifactId>
        <version>3.3.1</version>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl</artifactId>
        <version>3.3.1</version>
        <classifier>natives-windows</classifier>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-glfw</artifactId>
        <version>3.3.1</version>
        <classifier>natives-windows</classifier>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-opengl</artifactId>
        <version>3.3.1</version>
        <classifier>natives-windows</classifier>
    </dependency>
    <dependency>
        <groupId>org.lwjgl</groupId>
        <artifactId>lwjgl-stb</artifactId>
        <version>3.3.1</version>
        <classifier>natives-windows</classifier>
    </dependency>

    <!-- JOML dependency -->
    <dependency>
        <groupId>org.joml</groupId>
        <artifactId>joml</artifactId>
        <version>1.10.5</version>
    </dependency>
</dependencies>

<repositories>
    <repository>
        <id>sonatype</id>
        <url>https://oss.sonatype.org/content/repositories/snapshots/</url>
    </repository>
</repositories>
"
1 Like

You need to call glClear at this point.

I think i already tried it before , anyway i tried it again I added this line in Main class in thet place but problem remain still same .
look yourself at wideo i made with your code implementation

first its white frame, with white color for less than second then it swich to black . I want to remove that white blink , made it black from beginning

1 Like

It appeared that the issue was that you were flipping the back buffer before clearing (at least on the first frame). But I am not familiar with lwjgl, it’s possible that there is a setting there or some reason they handle things differently than just using C++.

thank for answers , i already solved the problem - answer was simple, just move show windows into whatever method like draw arrowsheads or draw axis

its Java btw not C++

1 Like