【问题标题】:GLFW (opengl) window closes when hovering mouse over the white window top handle将鼠标悬停在白色窗口顶部句柄上时,GLFW(opengl)窗口关闭
【发布时间】:2021-01-16 18:06:57
【问题描述】:

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

这里是主要函数(不是来自 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();
    };
};

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

【问题讨论】:

  • 你在调试器下运行过吗?
  • mousePositionCallback() 长什么样子?
  • 我还没有在调试器下运行它,虽然我为此使用了 vscode。这是函数(它可能被压扁,因为它是一个评论):void mousePositionCallback(GLFWwindow* _ , double xpos , double ypos) { MAIN_SCENE-&gt;camera.dir.x = mapVal((xpos)*CAMERA_SENSITIVITY,0,WIDTH,0,360*3); MAIN_SCENE-&gt;camera.dir.z = mapVal((xpos)*CAMERA_SENSITIVITY,0,WIDTH,0,360*3); MAIN_SCENE-&gt;camera.dir.y = mapVal((-ypos+HEIGHT/2)*CAMERA_SENSITIVITY,0,HEIGHT,0,360*3); };
  • 您应该在问题中编辑附加代码。很高兴看到minimal reproducible example。将代码精简为最小示例的过程可能会引导您找到解决方案。

标签: c++ glfw


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-05
    • 1970-01-01
    • 2014-10-19
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 2015-12-11
    相关资源
    最近更新 更多