首页 > 解决方案 > 将鼠标悬停在白色窗口顶部句柄上时,GLFW(opengl)窗口关闭

问题描述

每当我使用 C++ 的 GLFW 库在 opengl 中为测试光线追踪器渲染一个窗口时,该窗口工作正常,如果我只是悬停,而不是悬停在窗口之外,而是悬停在其白色顶部手柄上,无论是移动窗口或最小化它,窗口和整个程序只是崩溃而没有错误输出,即使我没有在整个程序中使用任何 try-catch 块或任何 noexcept 关键字,并且我正在使用std::shared_ptr指针管理。

这是主要函数(不是来自 glfw 或 opengl 的以大写字母编写的变量在另一个文件中定义,但未初始化):

int main() {
  if (!glfwInit()) ErrorExec("Couldn't init GLFW Engine");

  glfwWindowHint(GLFW_RESIZABLE,GLFW_FALSE);
  MAIN_WINDOW = glfwCreateWindow(WIDTH,HEIGHT,"Raytracer test" , NULL , NULL);
  if (!MAIN_WINDOW) ErrorExec("Couldn't init Window");
  glfwMakeContextCurrent(MAIN_WINDOW);

  _InitGlobalVars();
  _SetupMainPixels();
  _SetupSkyboxPixels();
  _SetupScene();

  glfwSetCursorPosCallback(MAIN_WINDOW,mousePositionCallback);

  while(!glfwWindowShouldClose(MAIN_WINDOW)) {
      _UpdateKeyboardInput();

      glClear(GL_COLOR_BUFFER_BIT);
      RenderScene(MAIN_SCENE,RAY_BOUNCE_COUNT);
      glDrawPixels(WIDTH,HEIGHT,GL_RGB,GL_UNSIGNED_BYTE,MAIN_PIXELS);

      glfwSwapBuffers(MAIN_WINDOW);
      glfwPollEvents();
};

  glfwTerminate();
  std::cout << "Exited ok!" << std::endl;
  return 1;
};

这是光线追踪算法:

#pragma once

#include <vector>
#include <iostream>
#include <thread>
#include <memory>

#include "./Settings.hpp"
#include "./Vec3.hpp"
#include "./MathFunctions.hpp"
#include "./Pixels.hpp"
#include "./ErrorHandler.hpp"
#include "./Abstract.hpp"
#include "./Objects.hpp"
#include "./Scene.hpp"
#include "./GlobalVars.hpp"

struct ClosestReturn {
    Object* obj = nullptr;
    float dist = -1;
    int obj_index = -1;
};

//SCOPE
ClosestReturn findNearestObject(Ray& ray , Scene* scene);
Color colorAtScene(Ray& ray , Object& objHit , Vec3& hitPos , Scene* scene);
Color colorAtSkybox(Vec3& dir);
Color raytraceScene(Ray& ray , Scene* scene , float depth);
void renderScenePart(Scene* scene , unsigned int maxD , unsigned int minx , unsigned int miny , unsigned int maxx , unsigned int maxy , float AR);
void RenderScene(Scene* scene , int maxD);
//SCOPE

ClosestReturn findNearestObject(Ray& ray , Scene* scene) {
    Object* objHit = nullptr;
    float distMin = -1;

    int k = 0 , index = 0;

    for (Object* obj : scene->objects) {
        float dist = obj->getIntersection(ray);

        if (obj->isHittable() && dist>SURF_DIST && (objHit==nullptr || dist < distMin)) {
            distMin = dist;
            objHit = obj;
            index = k;
        };
        k++;
    };

    return {objHit , distMin , index};
};

Color colorAtScene(Ray& ray , Object* objHit , Vec3& hitPos , Scene* scene) {
    Material mat = objHit->getMaterial();
    hitPos = hitPos + (objHit->getNormal(hitPos,ray.dir) * (-abs(SURF_DIST)));
    Vec3 col = mat.col * mat.amb;
    for (Light* light : scene->lights) {
        Ray toLight = Ray(hitPos , (light->pos - hitPos).normalize());
        Vec3 normal = objHit->getNormal(hitPos,toLight.dir);
        ClosestReturn nearest = findNearestObject(toLight , scene);
        if (nearest.dist<0 && nearest.obj==nullptr) {
            //Light equation
            col = (col + (light->col * mat.diff * std::max(normal * toLight.dir,0.0f * static_cast<float>((float)1/LIGHT_SOFTNESS)) * static_cast<float>((float)1/(hitPos.distVec(light->pos)*LIGHT_DIST)))).constrainVec(0.0f,255.0f);
        };
    };

    return col.constrainVec(0.0f,255.0f);
};

Color colorAtSkybox(Vec3& dir) {

    // AUX => return (Color(mapVal(dir.x,-1,1,0,255),mapVal(dir.y,-1,1,0,255),mapVal(dir.z,-2,2,0,255)).constrainVec(0.0f,255.0f));

    int v = static_cast<int>(mapVal(static_cast<float>(0.5f + (atan2f(dir.z , dir.x) / (float)(2.0f * PI))),1,0,0,SKYBOX_WIDTH));
    int u = static_cast<int>(mapVal(static_cast<float>(0.5f - (asinf(dir.y) / (float)PI)),1,0,0,SKYBOX_HEIGHT));

    return (Color)GetSkyboxPixel(u,v);
};

Color raytraceScene(Ray& ray , Scene* scene , float depth) {
    depth = constrainVal(depth,0,10);
    Vec3 col = Vec3(0,0,0);

    ClosestReturn nearest = findNearestObject(ray,scene);

    if (nearest.obj==nullptr || nearest.obj_index < 0 || nearest.dist <= 0) return colorAtSkybox(ray.dir);
    float distHit = nearest.dist;
    Object* objHit = nearest.obj;

    Vec3 hitPos = ray.atLength(distHit);
    Vec3 hitNormal = objHit->getNormal(hitPos,ray.dir).normalize();

    col = col + (colorAtScene(ray,objHit,hitPos,scene));

    if (depth > 0) {
        Vec3 new_pos = hitPos + (hitNormal * (abs(SURF_DIST)));
        Vec3 new_dir = ray.dir.normalize().reflect(hitNormal);
        Ray new_ray = Ray(new_pos,new_dir);
        col = col + (raytraceScene(new_ray,scene,(depth-1)) * objHit->getMaterial().ref);
    };

    return ((col + (PIXEL_SKIP * 10)).constrainVec(0.0f,255.0f));
};

void renderScenePart(Scene* scene , unsigned int maxD , unsigned int minx , unsigned int miny , unsigned int maxx , unsigned int maxy , float AR) {
    for (unsigned int wy = miny ; wy < maxy ; wy+=PIXEL_SKIP) {
        float ry = static_cast<float>(mapVal(wy,0,HEIGHT,-1,1));

        for (unsigned int wx = minx ; wx < maxx ; wx+=PIXEL_SKIP) {
            float rx = static_cast<float>(mapVal(wx,0,WIDTH,-1-AR,1+AR));

            Ray shootRay = Ray(scene->camera.pos , Vec3(rx,ry,CAMERA_FOV).normalize().rotate(scene->camera.dir.x , scene->camera.dir.y));
            SetMainPixel(wx,wy,(raytraceScene(shootRay,scene,maxD)).constrainVec(0.0f,255.0f));
        };
        
    };
};

void RenderScene(Scene* scene , unsigned int maxD) {
    unsigned int partWidth = (unsigned int)WIDTH/THREAD_COUNT_SQ;
    unsigned int partHeight = (unsigned int)HEIGHT/THREAD_COUNT_SQ;
    float ar = (float)WIDTH/HEIGHT;

    std::thread threads[THREAD_COUNT];

    int k = 0;
    for (unsigned int y = 0 ; y < THREAD_COUNT_SQ ; y++) {
        for (unsigned int x = 0 ; x < THREAD_COUNT_SQ ; x++) {
            threads[k] = std::thread(renderScenePart,std::ref(scene),maxD,x*partWidth,y*partHeight,x*partWidth + partWidth , y*partHeight + partHeight , ar);
            //threads[k].join();
            k++;
        };
    };

    for (std::thread& t : threads) {
        t.join();
    };
};

最后一个是导致问题的那个。我正在g++ Main.cpp -o main.exe -lgdi32 -lopengl32 -lglfw3dll -lglu32 -pthread -Wall -Wextra使用 MinGW 编译器编译主文件。提前致谢!

标签: c++glfw

解决方案


感谢评论部分的退休忍者,我想我有点追查出了问题所在。我将代码减少到只有几行,忘记了网格系统和所有这些东西。显然,当我抓住窗口时,主线程似乎在睡觉,直到我完成移动它,并且在光线跟踪算法中我正在实例化新线程,所以当主线程睡觉时,大多数时候它不会等待(又名“join()”),因为我将指针传递给那些线程,这些似乎填满了内存并崩溃。我已经添加了一个系统来让工作线程在主线程这样做时也让工作线程休眠,它的工作原理就像一个魅力!现在我要看看如何在 cuda XD 中做到这一点。尽管如此,还是谢谢大家!


推荐阅读