I created a 3D game like Minecraft in the Java language using the LWJGL library. I can also move around the world, look up, down, look around, and so on. And how to display the panel, like the one that I highlighted in red in the attached figure?

Minecraft screenshot

Suppose I drew these squares in Paint and saved them in a file panel.png , but how can I display it?

Write a sample source code.

  • I am not well versed in the subject, but maybe I just need a plain that is right next to the camera? draw on it on ui. - noadev 2:41 pm
  • Do you mean GUI? Then maybe it is. I approximately understand what needs to be done, but I cannot find such questions on the Internet. I do not know how other developers do it. - nick
  • (yi \ ui \ gui) yes, I mean it :) I see it myself like this: i.gyazo.com/331fcc41370eeb22a0412cb4a7eb913f.png but because it didn’t develop 3d, it’s just my vision \ guess and I don’t know how . red is the plane on which we draw gui - noadev

4 answers 4

In the screen initialization code (before the main loop) write the following code:

 GL11.glEnable(GL11.GL_TEXTURE_2D); GL11.glEnable(GL11.GL_BLEND); GL11.glBlendFunc(GL11.GL_SRC_ALPHA, GL11.GL_ONE_MINUS_SRC_ALPHA); GL11.glMatrixMode(GL11.GL_MODELVIEW); GL11.glMatrixMode(GL11.GL_PROJECTION); GL11.glLoadIdentity(); GL11.glOrtho(0, Display.getWidth(), Display.getHeight(), 0, 1, -1); GL11.glMatrixMode(GL11.GL_MODELVIEW); Texture i4 = TextureLoader.getTexture("PNG", new FileInputStream("textures" + File.separator + "myTexture.png")); 

And in the main loop the following:

 while (!Display.isCloseRequested()) { GL11.glClear(GL11.GL_COLOR_BUFFER_BIT); i4.bind(); GL11.glBegin(GL11.GL_QUADS); // Координаты и разрешение текстуры GL11.glTexCoord2f(0F, 0F); GL11.glVertex2f(100F, 100F); GL11.glTexCoord2f(1F, 0F); GL11.glVertex2f(100F + (float) i4.getTextureWidth(), 100F); GL11.glTexCoord2f(1F, 1F); GL11.glVertex2f(100F + (float) i4.getTextureWidth(), 100F + (float) i4.getTextureHeight()); GL11.glTexCoord2f(0F, 1F); GL11.glVertex2f(100F, 100F + (float) i4.getTextureHeight()); GL11.glEnd(); } 
  • I wrote everything I needed to do in the question. Works great. - nick

Create a separate container for sprites, fixed relative to the camera and place in it the sprites of the user interface. As a container, you can use a porosity rectangle with a texture (canvas). With texture (canvas), you can operate through PixelWriter and PixelReader, redrawing areas on the plane.

Or you can make an analogue of Group from JavaFX, just fix its copy relative to the camera and add 3D interface elements.

UPD: See the discussion here https://github.com/LWJGL/lwjgl3/issues/101

UPD2: Here the question has already been discussed https://gamedev.stackexchange.com/questions/18468/making-a-hud-gui-with-opengl-lwjgl (In short: a person draws the world, and then the interface is on top of the world ).

UPD3: And one more recommendation here https://gamedev.stackexchange.com/a/29284 - use the ready-made GUI library in LWJGL - Nifty ( http://void256.imtqy.com/blog/ -> https: // github .com / nifty-gui / nifty-gui )

    In general, the approach is as follows:

    1. Draw a game in the volume projection (perspective, etc.)
    2. Switch the projection to orthographic
    3. Turn off the depth buffer
    4. We draw the interface
    • My main question was not what is needed for that, but how to do it, that is, approximate source code. It is clear to me that you need to remove the depth and move from 3D to 2D mode. How to do it? Source code is needed. - nick
    • @Lesperanza so specify it in the question - Kromster
    • Now pointed. So you still know how to do it? - nick

    Android has a FrameLayout for displaying one content on top of the second, I think there’s such a thing in your industry.