Hello,

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 ), this is whole code of 3d opengl - app 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);
        // Request a double-buffered window
        glfwWindowHint(GLFW_DOUBLEBUFFER, GL_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);
        glfwShowWindow(window);

        GL.createCapabilities();

        glEnable(GL_DEPTH_TEST);

        // 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);
    }

    private void loop() {
        // Set the clear color explicitly
        glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        while (!glfwWindowShouldClose(window)) {
            // Clear the color and depth buffers
            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>

You can try to only show the window when you are ready to draw. Runs fine.
Main.java:

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);
        // Request a double-buffered window
        glfwWindowHint(GLFW_DOUBLEBUFFER, GL_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);

        // 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);
    }

    private void loop() {
            // Set the clear color explicitly
             glfwHideWindow(window);
                glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        while (!glfwWindowShouldClose(window)) {
            // Clear the color and depth buffers
            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() {
        glfwShowWindow(window);
        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();
    }
}
commented: Superb, working +0

toneewa you are a very good programmer, your code is working properly ...

how you know what to implement ? to think that available AI chats (copilot, chatgpt 4 , and some others) wasnt able to help in such matter is unbeliveable .

so to sum up you just add

        glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE);

in init and clear color in loop

             glfwHideWindow(window);
                glClearColor(0.0f, 0.0f, 0.0f, 1.0f);

and at end in drawaxes you moved

        glfwShowWindow(window);

from above to that place.

Thanks, you helped me, but a question - is that necesary to add this 2 first codes , that is : " glfwHideWindow(window);
glClearColor(0.0f, 0.0f, 0.0f, 1.0f);" and " glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE);" ? i tried just move "glfwShowWindow(window); " into drawAxes() method without that two above codes and its work perfectly as well.. can i left it like that ? in future it will not cause any error in my further developed project ?

This is what was left overs, after several different iterations. My thoughts: The idea was to hide the window, color it black, and show it when ready. From the first impression I got, it was clearing the frame color or not setting it resulting in a long white screen before getting to black.

glfwWindowHint(GLFW_DOUBLEBUFFER, GL_TRUE); 

This was already there. This is suppose to help with animation and preventing flickering.

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

This clears the color and sets it to black. I added this back in, just in case you were going to change something with it.

glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

This clears the framebuffer to the set color, and we do it again in loop(), so I thought maybe we should do it in the init(), similar to not setting an array or string on initialization, - to be certain.

glfwHideWindow(window);

I chose this because I was trying to isolate where the white screen was coming from.
glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE) and window = glfwCreateWindow(800, 600, "3D Scene", NULL, NULL);
For me, there wasn't any reason to display anything prior to the drawAxes().

You could remove the extras. It still took me a few hours; from reading the Java, installing Maven and libraries, to installing, compiling, and running, with Visual Studio Code, that I've also never used. Interesting anyway. Cheers!

i noticed (based on your first answer you gave above) that simpliest solving of issue is just move glfwShowWindow(window); to whatever method for example to drawAxes or whatever other will me during code developing like for example AxisArrowHeads() . I didnt noticed any errors so far so it should work i think , you said about spending some hours but not for this i hope ? VSC is good choise i think, i am using mostly IntellJ (so far i have it free from school) ... to get answer to that you relied fully on your skills or you helped by some AI , tell me please, thank you for help anyway , before i tried so many approaches (also with AI ) and dont get any solution to the problem

Correct. glfwShowWindow(window); in drawAxes();

Yeah, many hours for this. Trouble-shooting skills, with trial and error.
I had more trouble getting the work envirnoment setup. The IntelliJ IDEA license was expired.

"AI" has a long way to go. E.g., missing headers, identifiers, and all 4 types of dementia.

Wow dont waste so many time for anonymouse guy from the internet who ask for help,
but really i appriciate that, thank you even much more by reading it , for your help and your time .

I just tell you what i am seeing now - Java is very weird lang, free dev programs like eclipse or vsc have plenty of implementing errors (and IntellJ licence is for paid) , libraries and so on , and one project can not work under other program even if you set it up. Before i was enthusiastic to java , now i see many flows of it, too many i would say. Java Script nowadays after it updates on recent years is much better organized in plenty ways and very much more understandible, easier to code. You are probbaly experienced developer, so for junior one,like me what language would you recommend as best chose - in place of mine - where you are at the middle of road and dont know what to take and what way to go in your computer knowledge programming development ...

I like C++. It has plenty of libraries. Like any language, find a good book or website with examples and write them. Understand how they work, then, try to create an example using the idea. It really depends on what you want to do with it.

Learn them all. I've only encountered the following: C, C++, SQL, Node.js/TypeScript, HTML/CSS, C#, Java, JavaScript, Python, SQL, Node.js/TypeScript, HTML/CSS

Do a job search, if that's what you want, and see what they are looking for. E.g., Microsoft Software Engineer: experience including, but not limited to: C, C++, C#, Java, JavaScript, Python.
Hope that helps.

Many thanks for your chat and help Toneewa 🙂🤗👍

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.