SFML community forums

Help => Graphics => Topic started by: Bogdan on June 11, 2017, 08:02:05 pm

Title: Opengl with SFML Walls Render Issue
Post by: Bogdan on June 11, 2017, 08:02:05 pm
Hi people,

I've got a strange problem with the depth buffer (?).
It's similar to: https://en.sfml-dev.org/forums/index.php?topic=20693.msg148603;topicseen#msg148603 (https://en.sfml-dev.org/forums/index.php?topic=20693.msg148603;topicseen#msg148603)

I've made a "room" (without ceiling and ground), but the walls are transparent and I don't know why.
I have tried the solution from the link from above, but it didn't work and changing the drawing order didn't either. What I am doing wrong? Many thanks in advance for any help. Sorry that the indentation is wrong, but I don't know how to fix it, because in my compiler it looks ok.

#include <SFML/Graphics.hpp>
#include <iostream>
#define _USE_MATH_DEFINES
#include <math.h>
#include <SFML/OpenGL.hpp>

void myGluPerspective(double fovy, double aspect, double zNear, double zFar)
{
    double f = 1.0 / tan(fovy * M_PI / 360);  // convert degrees to radians and divide by 2
    double xform[16] =
    {
        f / aspect, 0, 0, 0,
        0,          f, 0, 0,
        0,          0, (zFar + zNear)/(zFar - zNear), -1,
        0,          0, 2*zFar*zNear/(zFar - zNear), 0
    };
    glMultMatrixd(xform);
}

int main()
{
        sf::ContextSettings Settings;
        Settings.depthBits = 24;
        Settings.stencilBits = 8;
        Settings.antialiasingLevel = 2;
        sf::Window window(sf::VideoMode(800, 600, 32), "SFML OpenGL", sf::Style::Close, Settings);
       
        window.setFramerateLimit(40);

        glClearDepth(1.f);
        glClearColor(0.8f, 0.8f, 0.8f, 0);

        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();

        myGluPerspective(60,800/600,0,180);

        glTranslatef(0,0,-3);

    while (window.isOpen())
    {
        sf::Event event;
                while (window.pollEvent(event))
                {
                        switch (event.type)
                        {
                        case sf::Event::Closed:
                                window.close();
                                break;
                        }
                }
               
                window.setActive();
                glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

                glRotatef(1,0,0.5f,0);

                glBegin(GL_QUADS);

                glColor3f(1.0f,1.0f,1.0f);                              // white
                glVertex3f(-1,-1,-1);
                glVertex3f(1,-1,-1);
                glVertex3f(1,1,-1);
                glVertex3f(-1,1,-1);

                glColor3f(0.0f,1.0f,0.0f);                              // green
                glVertex3f(-1,1,1);
                glVertex3f(-1,-1,1);
                glVertex3f(-1,-1,-1);
                glVertex3f(-1,1,-1);

                glColor3f(0.0f,0.0f,1.0f);                              // blue
                glVertex3f(1,1,1);
                glVertex3f(1,-1,1);
                glVertex3f(1,-1,-1);
                glVertex3f(1,1,-1);

                glColor3f(0.0f,1.0f,1.0f);                              // cyan
                glVertex3f(-1,-1,1);
                glVertex3f(1,-1,1);
                glVertex3f(1,1,1);
                glVertex3f(-1,1,1);

                glEnd();

        window.display();
    }

    return 0;
}
 
Title: Re: Opengl with SFML Walls Render Issue
Post by: Hapax on June 11, 2017, 08:27:29 pm
Is it possible that you've just defined the vertices in the incorrect order? Whether they are clockwise or anti-(counter-)clockwise can determine which side of the face is shown (front) and which is not (back).

The indentation in the code is wrong because it looks like you have tabs set to 4 spaces wide in your editor and some of them are actually just four spaces whereas most of them are actually tabs. Since this forum shows tabs as 8 spaces wide, the indents of 4 spaces stays only 4 spaces whereas the tabs become double that.
Title: Re: Opengl with SFML Walls Render Issue
Post by: Bogdan on June 19, 2017, 07:51:56 pm
Indeed fixing the vertex order and "culling" the back faces did the trick. By the way I found out, that the z-axis directions were reversed.