我正在利用Libgdx为3D Part制作一个关于Java的2D-3D游戏。到目前为止,我有纯菜写的菜单和2D游戏,我想用Libgdx做3D部分。
所以我设法启动了一个libgdx应用程序(用于桌面)并绘制了一个平面和一个盒子,盒子可以移动,就像相机一样。
问题是只有那个部分是在Libgdx的帮助下编写的。菜单和其他东西都是用Java编写的,我不想改变它。
所以我在Java菜单上点了一个Button,3D游戏就像这样开始:
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);
}
所以现在我得到了新的窗口,这是有效的。有了F1,我就像这样关闭游戏:
game3D.exit();
将Game3D作为main()返回的应用程序。
下一步是再次点击3D游戏按钮,窗口再次打开。虽然,这次,窗口不是我在配置中说的那个。这很奇怪。
无论如何,我尝试打开和关闭3D窗口几次,并且在某些时候,抛出异常:
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)
我查看了互联网,但似乎没有任何东西适合我的问题。除了exit()方法之外,我不会在SuperBomberman3D类之外调用任何方法。我不知道这个错误被抛出的地方或原因。无论如何,这里是SuperBomberman3D类,以防你在那里看到错误:
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();
}
}
所以,问题是为什么抛出错误,我如何把事情做好,另外说,为什么配置只设置好一次,当我调用game3D.exit()
退出3D应用程序时然后我用main()方法重做应用程序。