c++macosopenglglutfltk

Fl_Gl_Window sample program fails to draw in that window


I got this program to build and run:

#define GL_SILENCE_DEPRECATION

#include <FL/Fl.H>
#include <FL/Fl_Window.H>
#include <FL/Fl_Gl_Window.H>
#include <FL/gl.h>
#include <OpenGL/glu.h>

void glutWireSphere (GLdouble radius, GLint slices, GLint stacks);

class MyGlWindow : public Fl_Gl_Window {
public:
    MyGlWindow(int x, int y, int w, int h, const char* l = 0) : Fl_Gl_Window(x, y, w, h, l) {}

    void draw() override {
        if (!valid()) {
            glLoadIdentity();
            glViewport(0, 0, w(), h());
            glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0);
            glEnable(GL_DEPTH_TEST);
            valid(1);
        }

        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();

        // Set up the camera
        gluLookAt(0.0, 0.0, 3.0,  // eye position
                  0.0, 0.0, 0.0,  // look-at position
                  0.0, 1.0, 0.0); // up direction

        // Draw a sphere
        glColor3f(0.5, 1.0, 1.0);
        glutWireSphere(1.0, 20, 20);

        glFlush();
    }
};

int main() {
    Fl_Window* window = new Fl_Window(400, 400, "FLTK Sphere Example");
    MyGlWindow* glWindow = new MyGlWindow(10, 10, window->w() - 20, window->h() - 20);
    window->resizable(glWindow);
    window->end();
    window->show();
    return Fl::run();
}

...with this CMakeLists.txt:

cmake_minimum_required(VERSION 3.5)
project(HelloFLTK)

set(CMAKE_PREFIX_PATH  "/opt/homebrew")
find_package(FLTK REQUIRED)
include_directories(${FLTK_INCLUDE_DIRS} /opt/homebrew/include)
link_directories(${FLTK_LIBRARY_DIRS})

add_executable(HelloFLTK flsphere.cpp)
target_compile_features(HelloFLTK PUBLIC cxx_std_11)
target_link_libraries(HelloFLTK ${FLTK_LIBRARIES})

...but it draws nothing, the Fl_Gl_Window remains black.

Any ideas? Recommendations for diagnostic steps? I have already tried changing the color from white to glColor3f(0.5, 1.0, 1.0) to no avail.

BTW GL_SILENCE_DEPRECATION is needed because Apple, in their infinite wisdom, is threatening to get rid of OpenGL entirely. I am trying hard to resurrect and enjoy a piece of legacy code before that happens!

Environment:


Solution

  • Position of wire sphere is (0,0,0).

    Position of camera is (0,0,3).

    You tried to define clipping volume as cuboid with farPlane shifted relative to camera's position by 1 toward negative Z values:

           |
           |
    <-(c)--|--(w)---[z]
           |   |
           |   |
          fp  [x]
        
    c (camera), w (wireSphere), fp is clipping plane that defines frustum
    

    You see nothing because your sphere doesn't contain in clipping volume.

    If camera's position has Zcoord as 3, you have to define farPlane shifted at least 3 units along Z axis to see half of your sphere.

    Also when setting projection matrix, matrix mode has to be set as GL_PROJECTION (by default mode is GL_MODELVIEW):

    if (!valid()) {
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        glViewport(0, 0, w(), h());
        glOrtho(-1.0, 1.0, -1.0, 1.0, -1.0, 4.0);
        glEnable(GL_DEPTH_TEST);
        valid(1);
    }
    

    With these modifications you can get:

    Output