I am working on a 2D-3D Game on Java, with the help of Libgdx for the 3D Part. By now, I have menus and the 2D game written purely on Java, and I want to do the 3D part with Libgdx.
So I have managed to start a libgdx application (for desktop) and paint a plane and a box, and the box can be moved around, like the camera.
The problem is that only that part is written with the help of Libgdx. The menus and other stuff are written with Java and I don't want to change that.
So I clic a Button on the Java menu and the 3D game starts like this:
public static LwjglApplication main() {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.forceExit = false;
config.height = Game.HEIGHT;
config.width = Game.WIDTH;
config.title = "Game 3D";
return new LwjglApplication(new SuperBomberman3D(), config);
}
So now I get the new window, and that works. With F1, I close the game like this:
game3D.exit();
being Game3D the application returned by main().
And the next step is clicking the 3D Game button again, and the window opens again. Though, this time, the window is not the size I have said in config. That's weird.
Anyway, I try to open and close the 3D window a couple of times, and at some point, and exception is thrown:
Exception in thread "LWJGL Application" java.lang.RuntimeException: No OpenGL context found in the current thread.
at org.lwjgl.opengl.GLContext.getCapabilities(GLContext.java:124)
at org.lwjgl.opengl.GL11.glViewport(GL11.java:3261)
at com.badlogic.gdx.backends.lwjgl.LwjglGL20.glViewport(LwjglGL20.java:839)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setWindowedMode(LwjglGraphics.java:491)
at com.badlogic.gdx.backends.lwjgl.LwjglGraphics.setupDisplay(LwjglGraphics.java:166)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication.mainLoop(LwjglApplication.java:142)
at com.badlogic.gdx.backends.lwjgl.LwjglApplication$1.run(LwjglApplication.java:124)
I have looked over the Internet, but nothing seems to fit in my problem. I don't call any method, besides the exit() method, outside the SuperBomberman3D class. I don't know where this error is being thrown or why. Anyway, here is the SuperBomberman3D class, in case you see something wrong there:
public class SuperBomberman3D extends ApplicationAdapter implements
ApplicationListener {
public static LwjglApplication main() {
LwjglApplicationConfiguration config = new LwjglApplicationConfiguration();
config.forceExit = false;
config.height = Game.HEIGHT;
config.width = Game.WIDTH;
config.title = "Super Bomberman 3D";
return new LwjglApplication(new SuperBomberman3D(), config);
}
public PerspectiveCamera cam;
public CameraInputController camController;
public Model bombermanModel;
public Model planeModel;
public Environment env;
public ModelBatch modelBatch;
public ModelInstance bomberman;
public ModelInstance plane;
private int FIELD_OF_VIEW = 67;
private Vector3 initialPosition = new Vector3(10f, 10f, 10f);
private Vector3 origin = new Vector3(0, 0, 0);
private float near = 1f;
private float far = 300f;
@Override
public void create() {
camera();
models();
environment();
}
private void camera() {
cam = new PerspectiveCamera(FIELD_OF_VIEW, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
cam.position.set(initialPosition);
cam.lookAt(origin);
cam.near = near;
cam.far = far;
cam.update();
camController = new CameraInputController(cam);
Gdx.input.setInputProcessor(camController);
}
private void models() {
modelBatch = new ModelBatch();
ModelBuilder mb = new ModelBuilder();
bombermanModel = mb.createBox(5f, 5f, 5f,
new Material(ColorAttribute.createDiffuse(Color.RED)),
Usage.Position | Usage.Normal);
bomberman = new ModelInstance(bombermanModel);
planeModel = mb.createRect(-10, 0, 10,
10, 0, 10,
10, 0, -10,
-10, 0, -10,
0, 1, 0,
GL20.GL_TRIANGLES,
new Material(
new ColorAttribute(
ColorAttribute.createDiffuse(Color.BLUE)),
new BlendingAttribute(
GL20.GL_SRC_ALPHA,
GL20.GL_ONE_MINUS_SRC_ALPHA)),
VertexAttributes.Usage.Position |
VertexAttributes.Usage.TextureCoordinates);
plane = new ModelInstance(planeModel);
}
private void environment() {
env = new Environment();
env.set(new ColorAttribute(ColorAttribute.AmbientLight, 0.2f, 0.2f,
0.2f, 1f));
env.add(new DirectionalLight().set(0.8f, 0.8f, 0.8f, -1f, -0.8f, -0.2f));
}
@Override
public void render() {
step();
camController.update();
Gdx.gl.glViewport(0, 0, Gdx.graphics.getWidth(),
Gdx.graphics.getHeight());
Gdx.gl.glClear(GL20.GL_COLOR_BUFFER_BIT | GL20.GL_DEPTH_BUFFER_BIT);
modelBatch.begin(cam);
modelBatch.render(bomberman, env);
modelBatch.render(plane, env);
modelBatch.end();
}
public void step() {
float x = 0;
float z = 0;
if(Gdx.input.isKeyPressed(Input.Keys.UP)){
x = -0.2f;
}
if(Gdx.input.isKeyPressed(Input.Keys.DOWN)){
x = 0.2f;
}
if(Gdx.input.isKeyPressed(Input.Keys.LEFT)){
z = 0.2f;
}
if(Gdx.input.isKeyPressed(Input.Keys.RIGHT)){
z = -0.2f;
}
bomberman.transform.translate(new Vector3(x, 0, z));
if(Gdx.input.isKeyPressed(Input.Keys.F1)){
StatesMachine.goToRoom(STATE.MAIN_MENU, false);
}
}
@Override
public void dispose() {
bombermanModel.dispose();
planeModel.dispose();
modelBatch.dispose();
}
}
So, the question is why is that error being thrown, how do i put things right, and, on other note, why the configuration is only set well once, when I call game3D.exit()
to exit the 3D application and I redo the application with the main() method.