Fl_Gl_Window 示例程序无法在该窗口中绘制

问题描述 投票:0回答:1

我构建并运行了这个程序:

#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();
}

...有了这个

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})

...但它什么也没绘制,Fl_Gl_Window 保持黑色。

有什么想法吗?诊断步骤的建议?我已经尝试将颜色从白色更改为

glColor3f(0.5, 1.0, 1.0)
,但无济于事。

顺便说一句,GL_SILENCE_DEPRECATION 是必要的,因为苹果以其无限的智慧威胁要完全摆脱 OpenGL。我正努力在这一切发生之前复活并享受一段遗留代码!

环境:

  • C++11
  • MacOS 14.2.1
  • FLTK库1.3.9
c++ macos opengl glut fltk
1个回答
0
投票

线球的位置为(0,0,0)。

相机位置为(0,0,3)。

您尝试将裁剪体积定义为长方体,其中远平面沿负 Z 值相对于相机位置移动 1:

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

您什么也看不到,因为您的球体不包含在剪切体积中。

如果相机的位置 Z 坐标为 3,则必须定义

farPlane
沿 Z 轴移动至少 3 个单位才能看到半个球体。

设置投影矩阵时,矩阵模式必须设置为

GL_PROJECTION
(默认模式为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);
    }

通过这些修改,您可以获得: Output

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.