Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7181903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T17:39:18+00:00 2026-05-28T17:39:18+00:00

I use SFML to create the window. In this screenshot the cube should be

  • 0

I use SFML to create the window.

In this screenshot the cube should be behind the pyramid but it just doesn’t work.
Screenshot

Here is the minimal code I used:

#include <OpenGL/gl.h>
#include <OpenGL/glu.h>

#include <SFML/Graphics.hpp>
#include "ResourcePath.hpp"

void resize();
void drawScene();
void initGL();

float rtri = 0;
float rquad = 0;
float z = -10.0f;
int main (int argc, const char * argv[])
{
    // Create the main window
    sf::RenderWindow *window = new sf::RenderWindow();
    window->Create( sf::VideoMode( 800, 600, 32 ), "Collision Detection", sf::Style::Close );

    sf::Event event;

    bool run = true;
    initGL();
    resize();
    while( run ) {
        window->PollEvent( event );
        if( event.Type == sf::Event::Closed ) {
            run = false;
        }

        drawScene();
        window->Display();
//        z+= 0.001f;
    }

    return EXIT_SUCCESS;
}

void resize() {
    glViewport(0,0, 800,600);

    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();

    // Calculate The Aspect Ratio Of The Window
    gluPerspective(45.0f,800/600,0.1f,100.0f);

    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
}

void drawScene() {
    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
    glLoadIdentity();                   // Reset The Current Modelview Matrix
    glTranslatef(0.0f,0.0f,-6.0f);      // Move Left 1.5 Units And Into The Screen 6.0
    glRotatef(rtri,0.0f,1.0f,0.0f);     // Rotate The Triangle On The Y axis ( NEW )
    glBegin(GL_TRIANGLES);  
    glColor3f(1.0f,0.0f,0.0f);
    glVertex3f( 0.0f, 1.0f, 0.0f);
    glColor3f(0.0f,1.0f,0.0f);  
    glVertex3f(-1.0f,-1.0f, 1.0f);
    glColor3f(0.0f,0.0f,1.0f);      
    glVertex3f( 1.0f,-1.0f, 1.0f);  
    glColor3f(1.0f,0.0f,0.0f);      
    glVertex3f( 0.0f, 1.0f, 0.0f);  
    glColor3f(0.0f,0.0f,1.0f);      
    glVertex3f( 1.0f,-1.0f, 1.0f);  
    glColor3f(0.0f,1.0f,0.0f);      
    glVertex3f( 1.0f,-1.0f, -1.0f); 
    glColor3f(1.0f,0.0f,0.0f);      
    glVertex3f( 0.0f, 1.0f, 0.0f);  
    glColor3f(0.0f,1.0f,0.0f);      
    glVertex3f( 1.0f,-1.0f, -1.0f); 
    glColor3f(0.0f,0.0f,1.0f);      
    glVertex3f(-1.0f,-1.0f, -1.0f); 
    glColor3f(1.0f,0.0f,0.0f);      
    glVertex3f( 0.0f, 1.0f, 0.0f);  
    glColor3f(0.0f,0.0f,1.0f);      
    glVertex3f(-1.0f,-1.0f,-1.0f);  
    glColor3f(0.0f,1.0f,0.0f);      
    glVertex3f(-1.0f,-1.0f, 1.0f);  
    glEnd();                        

    glLoadIdentity();               // Reset The Current Modelview Matrix
    glTranslatef(0.0f,0.0f,z);      // Move Right 1.5 Units And Into The Screen 7.0
    glRotatef(rquad,1.0f,1.0f,z);   // Rotate The Quad On The X axis ( NEW )
    glBegin(GL_QUADS);          
    glColor3f(0.0f,1.0f,0.0f);  
    glVertex3f( 1.0f, 1.0f,-1.0f);
    glVertex3f(-1.0f, 1.0f,-1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f( 1.0f, 1.0f, 1.0f);
    glColor3f(1.0f,0.5f,0.0f);  
    glVertex3f( 1.0f,-1.0f, 1.0f);
    glVertex3f(-1.0f,-1.0f, 1.0f);
    glVertex3f(-1.0f,-1.0f,-1.0f);
    glVertex3f( 1.0f,-1.0f,-1.0f);  
    glColor3f(1.0f,0.0f,0.0f);      
    glVertex3f( 1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f,-1.0f, 1.0f);
    glVertex3f( 1.0f,-1.0f, 1.0f);
    glColor3f(1.0f,1.0f,0.0f);  
    glVertex3f( 1.0f,-1.0f,-1.0f);
    glVertex3f(-1.0f,-1.0f,-1.0f);
    glVertex3f(-1.0f, 1.0f,-1.0f);
    glVertex3f( 1.0f, 1.0f,-1.0f);
    glColor3f(0.0f,0.0f,1.0f);  
    glVertex3f(-1.0f, 1.0f, 1.0f);
    glVertex3f(-1.0f, 1.0f,-1.0f);
    glVertex3f(-1.0f,-1.0f,-1.0f);
    glVertex3f(-1.0f,-1.0f, 1.0f);
    glColor3f(1.0f,0.0f,1.0f);  
    glVertex3f( 1.0f, 1.0f,-1.0f);
    glVertex3f( 1.0f, 1.0f, 1.0f);
    glVertex3f( 1.0f,-1.0f, 1.0f);
    glVertex3f( 1.0f,-1.0f,-1.0f);
    glEnd();                                            // Done Drawing The Quad

    rtri+=0.2f;                                         // Increase The Rotation Variable For The Triangle ( NEW )
    rquad-=0.15f;
    z-=0.01;
}

void initGL() {
    glShadeModel(GL_SMOOTH);                            // Enable Smooth Shading
    glClearColor(0.0f, 0.0f, 0.0f, 0.5f);               // Black Background
    glClearDepth(1.0f);                                 // Depth Buffer Setup
    glEnable(GL_DEPTH_TEST);                            // Enables Depth Testing
    glDepthFunc(GL_LEQUAL);                             // The Type Of Depth Testing To Do
    glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    glEnable( GL_CULL_FACE );

    /* Position the camera */
    glTranslatef(0, 0, -5);
}

I’ve tried different depth functions, GL_LESS, GL_EQUAL, I’ve tried them all. Also enabling and disabling depth testing on different places, nothing seems to work.

I’m running Mac OS X 10.7 ( Lion ), not sure if that is important, though I didn’t seem to have any trouble with these kind of things before upgrading.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T17:39:19+00:00Added an answer on May 28, 2026 at 5:39 pm

    Your code looks okay. I suspect that your window simply does not have a depth buffer. You’re using sf::RenderWindow, whose documentation says (emphasis mine):

    Simple wrapper for sf::Window that allows easy 2D rendering.

    I don’t know SFML, but this tutorial suggests to create your window like this:

    sf::WindowSettings Settings;
    Settings.DepthBits         = 24; // Request a 24 bits depth buffer
    Settings.StencilBits       = 8;  // Request a 8 bits stencil buffer
    Settings.AntialiasingLevel = 2;  // Request 2 levels of antialiasing
    sf::Window App(sf::VideoMode(800, 600, 32), "SFML OpenGL", sf::Style::Close, Settings);
    

    You could set StencilBits and AntialiasingLevel to 0 since this example doesn’t need them.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

use this website a lot but first time posting. My program creates a number
Use case: I've just entered insert mode, and typed some text. Now I want
I'm using SFML, and I want to use Qt Creator in conjunction with it.
I've encountered a problem drawing SFML Text. In my application, I use views as
I recently got started trying to use SFML. For some reason my simple program
I recently got started trying to use SFML. For some reason my simple program
Use to work with a MySQL + + (library for C + +) The
Use: The user searches for a partial postcode such as 'RG20' which should then
Use trap to capture signals like this: i=-1;while((++i<33)); do trap echo $i >> log.txt
Use case: we have some project meta-data files which we want tracked, but are

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.