How to render 3D objects correctly using 2d GUI (nifty-gui)?

I have this code: https://github.com/magicgoose/lwjgl-nifty-test-project When I create only the GUI, it works as expected. When I try to display a triangle (in perspective view) and then render the GUI, everything fails, and even the graphical interface does not display correctly - I only get letters on a black background. (happens if I uncomment draw_something() in display(...) )

 private def draw_something() { glTranslatef(0, 0, -20) glBegin(GL_TRIANGLES) glVertex3f(0.0f, 1.0f, 0.0f) glVertex3f(-1.0f, -1.0f, 0.0f) glVertex3f(1.0f, -1.0f, 0.0f) glEnd() } 

What am I doing wrong? I tried to find working examples with nifty-gui and 3d graphics in the background, but no luck.

Update 1

I changed the code based on the datenwolf answer, now the GUI is displayed OK, but I only see a white triangle for a couple of milliseconds (maybe this is actually one frame?), It seems that the 3d installation is β€œdamaged” ... This happens , only if I render the GUI, if I comment out the line gui.render(false) , a white triangle will remain on the screen.

Update 2

I added some movement to the 3d part (see updates in the repository), now I see that the triangle is barely noticeable (it looks like a z-fight).

+4
source share
3 answers

You must switch between spelling and perspective. Two functions are created for this: display_ready2d (this sets up the orthoprojection matrix) and display_ready3d (this creates a perspective projection).

Unfortunately, the display_ready3d function does not reset the matrix stack before applying it. You must add glLoadIdentity before calling gluPerspective. Also, you should not clear the framebuffer in these functions, since you want to be able to switch between matrix settings. Therefore, change this to the following:

 private def display_ready3d(fov: Float, aspect: Float) { glMatrixMode(GL_PROJECTION) glLoadIdentity(); gluPerspective(fov, aspect, 0.01f, 100.0f) glMatrixMode(GL_MODELVIEW) glLoadIdentity() glEnable(GL_DEPTH_TEST) } 

display_ready2d before drawing the GUI and display_ready3d before draw_something. You should also put clear commands there, which should also cover the depth buffer; also, a clear color should have an alpha value of 1 (except that you created a transparent window).

 def display(width: Int, height: Int, AR: Float, gui: Nifty) { glViewport(0, 0, width, height) glClearDepth(1.) glClearColor(0., 0., 0., 1.) glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT) display_ready3d(90, width/height) draw_something() display_ready2d(width, height) gui.render(false) glFlush() Display.update() } 
+4
source

I used the following code in one of my projects to achieve this. The main difference between the datenwolf solution is that you do not need to adjust the 3D perspective every time you switch back from GUI mode. In my case, I have a rather complicated installation of a 3D perspective, and therefore it is more convenient to rely on this push / pop matrix approach.

If you have lightning enabled in 3D mode, you may also need to disable lightning for GUI mode to ensure that your graphic elements are not affected.

 def enableOrthoView(winW: Int, winH: Int) { glMatrixMode(GL_PROJECTION) // Select projection glPushMatrix() // Push the matrix glLoadIdentity() // Reset the matrix glOrtho(0, winW, winH, 0, -1, 1) // Select ortho mode glMatrixMode(GL_MODELVIEW) // Select Modelview matrix glPushMatrix() // Push the Matrix glLoadIdentity() // Reset the Matrix glDisable(GL_DEPTH_TEST) // Cure z-fighting glDisable(GL_LIGHTING) // Ensure unaffected colors } def disableOrthoView() { glMatrixMode(GL_PROJECTION) // Select projection glPopMatrix() // Pop the matrix glMatrixMode(GL_MODELVIEW) // Select Modelview glPopMatrix() // Pop the matrix glEnable(GL_DEPTH_TEST) glEnable(GL_LIGHTING) } 
+1
source

I found the source of the flickering / fading 3D graphics problem. In OpenGL, you must turn off texturing if you draw geometry without providing textures, so I wrote

glDisable(GL_TEXTURE_2D)

at the end of display_ready3d(...) , and now everything does what it should do. I accidentally realized this error while reading an answer on another related question: https://gamedev.stackexchange.com/questions/44437/lwjgl-mixing-2d-and-3d Also thanks to datenwolf and bluenote10 your help is invaluable.

0
source

All Articles