Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED] Drawing in 2D causes 3D to disappear / Manage and Preserver States  (Read 2694 times)

0 Members and 1 Guest are viewing this topic.

Ronny

  • Newbie
  • *
  • Posts: 2
    • View Profile
Edit: Adding GetWindow().PreserveOpenGLStates(true); after initializing the RenderWindow and removing the commented section of the code blow resolved the issue although the wiki mentions that the function is bad for performance (Bottom of the page).

Edit2: Ok, found that SFML 2.0 (I was using 1.6) has a good explanation on managing the states so I think this issue is solved unless anyone wants to add anything. http://www.sfml-dev.org/tutorials/2.0/window-opengl.php

I've looked around the Web on how to draw a HUD over 3D views using OpenGL but the solutions have found have not been successful for me.

In the code below, with the lines commented the cube renders fine. With the depth disabling I get a weird semi-drawn cube. And with the buffer clear I get nothing. I've tried moving things around and pushing / poping but they have had no effect. The Render2D function draws some text using sf::String which also makes any previous 3D rendering disappear.

GetWindow().SetActive();
               
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
               
glClearDepth(1.f);
glClearColor(0.f, 0.f, 0.f, 0.f);

glEnable(GL_DEPTH_TEST);
glDepthMask(GL_TRUE);

glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluPerspective(90.f, 1.f, 1.f, 500.f);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
               
Render3D();
               
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0, GetWindow().GetWidth() , GetWindow().GetHeight() , 0, -1, 1);
               
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
               
//glDisable(GL_DEPTH_TEST);
//glDepthMask(GL_FALSE);
               
//glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
               
//Render2D();
               
Window->Display();
 

Some of the topics I found:
http://www.gamedev.net/topic/388298-opengl-hud/
http://stackoverflow.com/questions/5467218/opengl-2d-hud-over-3d
http://stackoverflow.com/questions/8370537/opengl-2d-hud-in-3d-application
Google Search

Any help on what obvious thing I'm forgetting to do would be appreciated.
« Last Edit: January 29, 2013, 05:38:06 am by Ronny »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: Drawing in 2D causes 3D to disappear
« Reply #1 on: January 29, 2013, 04:26:07 am »
Isn't it normal for the screen to be cleared if you use glClear?

Also, you should (probably) use pushGLStates and popGLStates, as shown in the second example in the sf::RenderWindow documentation.

Ronny

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: Drawing in 2D causes 3D to disappear
« Reply #2 on: January 29, 2013, 04:48:09 am »
Isn't it normal for the screen to be cleared if you use glClear?

I'm new to OpenGL so I'm not sure, it was just something I saw in other solutions so I thought I'd try it. I've left it commented out though.

Also, you should (probably) use pushGLStates and popGLStates, as shown in the second example in the sf::RenderWindow documentation.

This doesn't appear to be available in SFML 1.6, but there is the similar function PreserveOpenGLStates which I'm assuming does the same thing. I'm not using it though because tutorials advise against it due to performance and instead suggest maintaining the state yourself: http://www.sfml-dev.org/tutorials/1.6/graphics-window.php.

Code: [Select]
Preserving OpenGL states is very CPU consuming and will degrade performances, use it only if you really need it. Also consider using your own code to manage the states you need to preserve...

Edit: Adding GetWindow().PreserveOpenGLStates(true); did the trick but in that case I guess the question now is how would I go about managing the state myself in order to increase performance?

Edit2: Looked at this thread where someone suggested using SFML 2.0 instead and having now looked at the 2.0 tutorial on using OpenGL with the Window I think I'm going to upgrade and follow the tutorial.
« Last Edit: January 29, 2013, 05:25:16 am by Ronny »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Sorry, I had not noticed that you were using 1.6.

 

anything