Suicidal
Suicidal

Reputation: 55

3D model looks transparent-like

Trying to draw wavefront obj files using OpenGL but it seems there is a depth-buffer problem.

Problem

Source:

// Default constructor
Engine::Engine()
{
    initialize();
    loadModel();    
    start();
}

// Initialize OpenGL
void Engine::initialize()
{     
    // Enable depth test
    glEnable(GL_DEPTH_TEST);
    // Enable depth write
    glDepthMask(GL_TRUE);
}

void Engine::start()
{
    // Main loop
    while(isOpen())
    {
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        // Draw 3D model to screen
        draw();
    }
}

Upvotes: 2

Views: 593

Answers (4)

damix911
damix911

Reputation: 4443

Are you using vertex shaders? This thing would happen if at the exit of vertex shader gl_Position.z is accidentally set to 0. Or to any value between -1 and 1 I believe.

It would also happen if all vertices had equal z value before the input stage, albeit there must be something wrong with your transformation matrices in this case. Or simply you may be doing something very exotic with your transformation matrices, and the model is fine. Have you set up both MODELVIEW and PROJECTION, and multiplied them accordingly? Either in the vertex shader or in the FFP?

Upvotes: 0

genpfault
genpfault

Reputation: 52093

Request a GL context with a depth buffer and glEnable(GL_DEPTH_TEST).

Try this:

mainWindow.create
    (
    sf::VideoMode
        (
        settings.getWidth(), 
        settings.getHeight()
        ), 
    "", 
    sf::Style::Resize,
    sf::ContextSettings( 16, 0, 0, 2, 0 )
    );

Upvotes: 1

rekire
rekire

Reputation: 47965

Did you activated the depth test?

glEnable(GL_DEPTH_TEST);

Upvotes: 1

Tim
Tim

Reputation: 35943

Things to check:

  • Make sure depth test is enabled glEnable(GL_DEPTH_TEST)
  • Make sure depth write is enabled glDepthMask(true)
  • Make sure your context has a depth buffer Assert(glGetIntegerv(GL_DEPTH_BITS) != 0))

Upvotes: 2

Related Questions