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

Author Topic: SFML+OpenGL Black Screen on Mac  (Read 2059 times)

0 Members and 1 Guest are viewing this topic.

d0m1n8

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
SFML+OpenGL Black Screen on Mac
« on: September 20, 2015, 05:49:53 am »
Hi,
I have trying to use SFML as a window context for OpenGL, but i get a black screen when i run the code

I have tried different code, but i still get the same result, when i run the same code using glut, it displays
NB: this is the same thing that happens when i run SDL+OpenGL

/*
 * Main.cpp
 *
 *  Created on: Sep 20, 2015
 *      Author: Damian-Machine
 */

#include <SFML/Window.hpp>
#include <SFML/OpenGL.hpp>
#include <OpenGL/gl.h>
#include <OpenGL/glu.h>
#include <math.h>
#include<iostream>
#define PI 3.14159265f

GLdouble clipAreaXLeft, clipAreaXRight, clipAreaYBottom, clipAreaYTop;

bool fullScreenMode = true; // Full-screen or windowed mode?

void initGL(){
        /*glClearColor(1.0 ,0.0, 0.0, .0);*/
        // Set color and depth clear value
        glClearDepth(1.f);
        glClearColor(1.f, 1.f, 1.f, 1.f);

        // Enable Z-buffer read and write
        glEnable(GL_DEPTH_TEST);
        glDepthMask(GL_TRUE);

        // Setup a perspective projection
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(90.f, 1.f, 1.f, 500.f);
}


void display(){
        //glClear(GL_COLOR_BUFFER_BIT);
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
                glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
        glTranslatef(0.f, 0.f, -2.f);

        glBegin(GL_QUADS);

            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f(-50.f,  50.f, -50.f);
            glVertex3f( 50.f,  50.f, -50.f);
            glVertex3f( 50.f, -50.f, -50.f);

            glVertex3f(-50.f, -50.f, 50.f);
            glVertex3f(-50.f,  50.f, 50.f);
            glVertex3f( 50.f,  50.f, 50.f);
            glVertex3f( 50.f, -50.f, 50.f);

            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f(-50.f,  50.f, -50.f);
            glVertex3f(-50.f,  50.f,  50.f);
            glVertex3f(-50.f, -50.f,  50.f);

            glVertex3f(50.f, -50.f, -50.f);
            glVertex3f(50.f,  50.f, -50.f);
            glVertex3f(50.f,  50.f,  50.f);
            glVertex3f(50.f, -50.f,  50.f);

            glVertex3f(-50.f, -50.f,  50.f);
            glVertex3f(-50.f, -50.f, -50.f);
            glVertex3f( 50.f, -50.f, -50.f);
            glVertex3f( 50.f, -50.f,  50.f);

            glVertex3f(-50.f, 50.f,  50.f);
            glVertex3f(-50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f, -50.f);
            glVertex3f( 50.f, 50.f,  50.f);

        glEnd();

}

void reshape(GLsizei width, GLsizei height){
        if(height == 0) height = 1;
        GLfloat aspect = (GLfloat)width / (GLfloat)height;
        glViewport(0, 0, width, height);

        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        if (width >= height) {
          clipAreaXLeft   = -1.0 * aspect;
          clipAreaXRight  = 1.0 * aspect;
          clipAreaYBottom = -1.0;
          clipAreaYTop    = 1.0;
   } else {
          clipAreaXLeft   = -1.0;
          clipAreaXRight  = 1.0;
          clipAreaYBottom = -1.0 / aspect;
          clipAreaYTop    = 1.0 / aspect;
   }
   gluOrtho2D(clipAreaXLeft, clipAreaXRight, clipAreaYBottom, clipAreaYTop);
   }

int main(){
        sf::ContextSettings settings;
        settings.depthBits = 32;
        settings.stencilBits = 8;
        settings.antialiasingLevel = 4;
        //settings.majorVersion = 2;
        //settings.minorVersion = 1;
        sf::Window window(sf::VideoMode(800, 600), "OpenGL", sf::Style::Default, settings);
        window.setVerticalSyncEnabled(true);
        window.setActive(true);

        // load resources, initialize the OpenGL states, ...

        // run the main loop
        bool running = true;
        initGL();
        while (running)
        {
                // handle events
                sf::Event event;
                while (window.pollEvent(event))
                {
                        if (event.type == sf::Event::Closed)
                        {
                                // end the program
                                running = false;
                        }
                        else if (event.type == sf::Event::Resized)
                        {
                                // adjust the viewport when the window is resized
                                //glViewport(0, 0, event.size.width, event.size.height);
                                reshape(event.size.width, event.size.height);

                        }
                }
                // clear the buffers
                display();
                // end the current frame (internally swaps the front and back buffers)
                window.display();
        }

        // release resources...

        return 0;
}




 

binary1248

  • SFML Team
  • Hero Member
  • *****
  • Posts: 1405
  • I am awesome.
    • View Profile
    • The server that really shouldn't be running
Re: SFML+OpenGL Black Screen on Mac
« Reply #1 on: September 20, 2015, 11:16:07 am »
You shouldn't count on always getting a Resized event when the application starts up. Try manually calling your reshape() function right after creating the window.
SFGUI # SFNUL # GLS # Wyrm <- Why do I waste my time on such a useless project? Because I am awesome (first meaning).

d0m1n8

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: SFML+OpenGL Black Screen on Mac
« Reply #2 on: September 24, 2015, 09:50:42 pm »
For anyone experiencing the same issue, it might be because you are using eclipse on mac, i switched the code base to xcode and it now works well, if i am able to come up with a solution for eclipse i would upload it here

Jesper Juhl

  • Hero Member
  • *****
  • Posts: 1405
    • View Profile
    • Email
Re: SFML+OpenGL Black Screen on Mac
« Reply #3 on: September 24, 2015, 10:07:01 pm »
That makes no sense at all. What IDE you use to write your code has no impact on the final executable what-so-ever.  If you use different compilers (linkers) then that may (will) change things, but still, if it changes something like what events are received then that smells like a bug somewhere.  Again, this makes no sense.
In any case. As binary1248 said, you cannot rely on getting a resize event on startup. Just do whatever you need to do at startup; event or no event.
« Last Edit: September 24, 2015, 11:23:28 pm by Jesper Juhl »

d0m1n8

  • Newbie
  • *
  • Posts: 3
    • View Profile
    • Email
Re: SFML+OpenGL Black Screen on Mac
« Reply #4 on: September 27, 2015, 08:08:21 am »
i wondered the same thing, but it still puzzles me why it works on xcode but not on eclipse, i've tried everything from every tutorial i have laid my hands on, it still displays a black screen, like i said before, i also changed the context to SDL, the same result, but as soon as i ported to xcode, it works without problems like a charm, so i still don't know whats happening