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>