将鼠标悬停在白色窗口顶部手柄上时,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编译器编译主文件。提前感谢!

多亏了评论部分的"退役忍者",我想我找到了问题所在。我把代码简化为几行,忘记了网格系统和所有这些东西。显然,当我抓住窗口时,主线程似乎会休眠,直到我移动完它,而在光线跟踪算法中,我正在实例化新线程,所以当主线程休眠时,大多数时候它不会等待(又名"join(("(对于它们来说,因为我正在向这些线程传递指针,这些线程似乎会填满内存并崩溃。我添加了一个系统来睡眠工作线程,而主线程正在这样做,它的工作方式很有魅力!。现在我要看看如何在cuda XD中做到这一点。尽管如此,还是要感谢大家!

最新更新