-1

我正在为一个行星写一个四叉树结构,当你远离四边形并接近它时,它会减少和增加细节。但是,我遇到了一些非常严重且令人讨厌的错误。

我有两个预处理器定义的常量,当我将值更改为除 32 以外的任何值(例如 16 或 64)时,确定四叉树的大小(QUAD_WIDTH 和 QUAD_HEIGHT)我得到一个蓝屏死机。我正在使用 code::blocks 作为我的 IDE,另一件事:每当我尝试在 code::blocks 中调试程序时,我也会出现蓝屏死机(常量是否为 32 无关紧要)

为什么会这样?我该如何解决它。 在此处输入图像描述 PQuad.cpp

#include "..\include\PQuad.h"
#include "..\include\Color3.h"

#include <iostream>
#include <vector>
#include <cmath>

#include <GL/glew.h>
#include <GL/glu.h>
#include <GL/gl.h>

#define QUAD_WIDTH 32
#define QUAD_HEIGHT 32

#define NUM_OF_CHILDREN 4

#define MAX_DEPTH 4

PQuad::PQuad(FaceDirection face_direction, float planet_radius)  {
    this->built = false;
    this->spherised = false;
    this->face_direction = face_direction;
    this->radius = planet_radius;
    this->planet_centre = glm::vec3(0, 0, 0);
}

PQuad::~PQuad()  {
}

std::vector<PQuad> PQuad::get_children()  {
    return children;
}

bool PQuad::get_built()  {
    return this->built;
}

int PQuad::get_depth()  {
    return this->depth;
}

float *PQuad::get_table()  {
    return tree;
}

float PQuad::get_element_width()  {
    return element_width;
}

glm::vec3 PQuad::get_position()  {
    return position;
}

glm::vec3 PQuad::get_centre()  {
    return centre;
}

void PQuad::get_recursive(glm::vec3 player_pos, std::vector<PQuad*>& out_children)  {
    for (size_t i = 0; i < children.size(); i++)  {
        children[i].get_recursive(player_pos, out_children);
    }

    if (this->should_draw(player_pos) ||
        this->depth == 0)  {
        out_children.emplace_back(this);
    }
}

GLuint PQuad::get_vertexbuffer()  {
    return vbo_vertices;
}

GLuint PQuad::get_colorbuffer()  {
    return vbo_colors;
}

GLuint PQuad::get_normalbuffer()  {
    return vbo_normals;
}

GLuint PQuad::get_elementbuffer()  {
    return ibo_elements;
}

void PQuad::set_parent(PQuad *quad)  {
    this->parent = quad;
}

void PQuad::set_child_index(int child_index)  {
    this->child_index = child_index;
}

void PQuad::set_depth(int depth)  {
    this->depth = depth;
}

void PQuad::set_root(bool root)  {
    this->root = root;
}

void PQuad::calculate_position()  {
    this->element_width = depth == 0 ? 1.0f : parent->get_element_width() / 2.0f;

    float quad_y = child_index / 2 == 0 ? 0 : element_width * QUAD_HEIGHT - element_width;
    float quad_x = child_index % 2 == 0 ? 0 : element_width * QUAD_WIDTH - element_width;

    if (this->depth != 0)  {
        quad_x += parent->get_position().x;
        quad_y += parent->get_position().y;
    }

    this->position = glm::vec3(quad_x, quad_y, 0);
}

void PQuad::construct()  {
    if (!this->built)  {
        std::vector<glm::vec3> vertices;
        std::vector<glm::vec3> normals;
        std::vector<Color3> colors;
        std::vector<GLushort> elements;

        construct_vertices(&vertices, &colors);
        construct_elements(&elements);
        spherise(&vertices, &normals);
        construct_normals(&vertices, &elements, &normals);

        construct_buffers(&vertices, &colors, &elements, &normals);

        float distance = radius;

        if (!spherised)  {
            distance = QUAD_WIDTH;
        }

        construct_depth_table(distance);

        this->built = true;
    }
}

void PQuad::construct_depth_table(float distance)  {
    tree[0] = -1;

    for (int i = 1; i < MAX_DEPTH; i++)  {
        tree[i] = distance;

        distance /= 2.0f;
    }
}

void PQuad::construct_children()  {
    calculate_position();

    if (depth < (int)MAX_DEPTH)  {
        children.reserve((int)NUM_OF_CHILDREN);

        for (int i = 0; i < (int)NUM_OF_CHILDREN; i++)  {
            children.emplace_back(PQuad(this->face_direction, this->radius));
            PQuad *child = &children.back();

            child->set_depth(depth + 1);
            child->set_child_index(i);
            child->set_parent(this);

            child->construct_children();
        }
    } else {
        leaf = true;
    }
}

void PQuad::construct_vertices(std::vector<glm::vec3> *vertices, std::vector<Color3> *colors)  {
    vertices->reserve(QUAD_WIDTH * QUAD_HEIGHT);

    for (int y = 0; y < QUAD_HEIGHT; y++)  {
        for (int x = 0; x < QUAD_WIDTH; x++)  {
            switch (face_direction)  {
                case YIncreasing:
                    vertices->emplace_back(glm::vec3(position.x + x * element_width, QUAD_HEIGHT - 1, -(position.y + y * element_width)));
                    break;
                case YDecreasing:
                    vertices->emplace_back(glm::vec3(position.x + x * element_width, 0, -(position.y + y * element_width)));
                    break;
                case XIncreasing:
                    vertices->emplace_back(glm::vec3(QUAD_WIDTH - 1, position.y + y * element_width, -(position.x + x * element_width)));
                    break;
                case XDecreasing:
                    vertices->emplace_back(glm::vec3(0, position.y + y * element_width, -(position.x + x * element_width)));
                    break;
                case ZIncreasing:
                    vertices->emplace_back(glm::vec3(position.x + x * element_width, position.y + y * element_width, 0));
                    break;
                case ZDecreasing:
                    vertices->emplace_back(glm::vec3(position.x + x * element_width, position.y + y * element_width, -(QUAD_WIDTH - 1)));
                    break;
            }

            // Position the bottom, right, front vertex of the cube from being (0,0,0) to (-16, -16, 16)
            (*vertices)[vertices->size() - 1] -= glm::vec3(QUAD_WIDTH / 2.0f, QUAD_WIDTH  / 2.0f, -(QUAD_WIDTH / 2.0f));

            colors->emplace_back(Color3(255.0f, 255.0f, 255.0f, false));
        }
    }

    switch (face_direction)  {
        case YIncreasing:
            this->centre = glm::vec3(position.x + QUAD_WIDTH / 2.0f, QUAD_HEIGHT - 1, -(position.y + QUAD_HEIGHT / 2.0f));
            break;
        case YDecreasing:
            this->centre = glm::vec3(position.x + QUAD_WIDTH / 2.0f, 0, -(position.y + QUAD_HEIGHT / 2));
            break;
        case XIncreasing:
            this->centre = glm::vec3(QUAD_WIDTH - 1, position.y + QUAD_HEIGHT / 2.0f, -(position.x + QUAD_WIDTH / 2.0f));
            break;
        case XDecreasing:
            this->centre = glm::vec3(0, position.y + QUAD_HEIGHT / 2.0f, -(position.x + QUAD_WIDTH / 2.0f));
            break;
        case ZIncreasing:
            this->centre = glm::vec3(position.x + QUAD_WIDTH / 2.0f, position.y + QUAD_HEIGHT / 2.0f, 0);
            break;
        case ZDecreasing:
            this->centre = glm::vec3(position.x + QUAD_WIDTH / 2.0f, position.y + QUAD_HEIGHT / 2.0f, -(QUAD_HEIGHT - 1));
            break;
    }

    this->centre -= glm::vec3(QUAD_WIDTH / 2.0f, QUAD_WIDTH  / 2.0f, -(QUAD_WIDTH / 2.0f));
}

void PQuad::construct_elements(std::vector<GLushort> *elements)  {
    int index = 0;

    elements->reserve((QUAD_WIDTH - 1) * (QUAD_HEIGHT - 1) * 6);

    for (int y = 0; y < QUAD_HEIGHT - 1; y++)  {
        for (int x = 0; x < QUAD_WIDTH - 1; x++)  {
            GLushort bottom_left = x + y * QUAD_WIDTH;
            GLushort bottom_right = (x + 1) + y * QUAD_WIDTH;
            GLushort top_left = x + (y + 1) * QUAD_WIDTH;
            GLushort top_right = (x + 1) + (y + 1) * QUAD_WIDTH;

            elements->emplace_back(top_left);
            elements->emplace_back(bottom_right);
            elements->emplace_back(bottom_left);

            elements->emplace_back(top_left);
            elements->emplace_back(top_right);
            elements->emplace_back(bottom_right);
        }
    }
}

void PQuad::construct_normals(std::vector<glm::vec3> *vertices, std::vector<GLushort> *elements, std::vector<glm::vec3> *normals)  {
    normals->reserve(QUAD_WIDTH * QUAD_HEIGHT);

    for (int i = 0; i < elements->size() / 3; i++)  {
         int index1 = elements->at(i * 3);
         int index2 = elements->at(i * 3 + 1);
         int index3 = elements->at(i * 3 + 2);

         glm::vec3 side1 = vertices->at(index1) - vertices->at(index3);
         glm::vec3 side2 = vertices->at(index1) - vertices->at(index2);
         glm::vec3 normal = glm::cross(side1, side2);
         normal = glm::normalize(normal);

         normals->emplace_back(normal);
         normals->emplace_back(normal);
         normals->emplace_back(normal);
    }
}

void PQuad::spherise(std::vector<glm::vec3> *vertices, std::vector<glm::vec3> *normals)  {
    for (int i = 0; i < QUAD_WIDTH * QUAD_HEIGHT; i++)  {
        glm::vec3 normal = glm::normalize(vertices->at(i) - planet_centre);

        (*vertices)[i] = (float)(radius) * normal;
    }

    glm::vec3 normal = glm::normalize(centre - planet_centre);

    centre = normal * (float)(radius);

    this->spherised = true;
}

void PQuad::construct_buffers(std::vector<glm::vec3> *vertices, std::vector<Color3> *colors, std::vector<GLushort> *elements, std::vector<glm::vec3> *normals)  {
    glGenBuffers(1, &vbo_vertices);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices->size(), &((*vertices)[0]), GL_STATIC_DRAW);

    glGenBuffers(1, &vbo_colors);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_colors);
    glBufferData(GL_ARRAY_BUFFER, sizeof(Color3) * colors->size(), &((*colors)[0]), GL_STATIC_DRAW);

    glGenBuffers(1, &vbo_normals);
    glBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
    glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * normals->size(), &((*normals)[0]), GL_STATIC_DRAW);

    glGenBuffers(1, &ibo_elements);
    glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
    glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * elements->size(), &((*elements)[0]), GL_STATIC_DRAW);
}

float distance3(glm::vec3 v1, glm::vec3 v2)  {
    return sqrt(pow(abs(v1.x - v2.x), 2) + pow(abs(v1.y - v2.y), 2) + pow(abs(v1.z - v2.z), 2));
}

bool PQuad::should_draw(glm::vec3 player_position)  {
    float distance = distance3(player_position, centre);

    if (distance < tree[depth])  {
        return true;
    }

    return false;
}
4

2 回答 2

6

普通的用户空间程序应该无法实现蓝屏死机……无论您做什么。

然而不幸的是,在编写与设备驱动程序进行大量交互的软件时,很容易遇到这种系统级错误,因为它们也是软件,而且它们不是没有错误的(设备驱动程序中的错误可能会导致整个系统崩溃)蓝屏死机)。

意思是您正在使用错误的参数调用 OpenGL,并且您的视频卡的驱动程序有错误,而不是检测问题并返回故障代码,它只是关闭机器。

您可以尝试使用操作日志,每一步都写入文件,因此在您收到 BSOD 并重新启动后,您可以检查写入文件的最后一条命令是什么。请注意,您应该在append中打开文件,写入日志行,然后关闭文件。即使这样也不能 100% 保证当您收到 BSOD 时文件的内容会真正写入磁盘,但在这种情况下,IMO 的可能性应该很高。更好的选择是通过串行线路或使用网络将日志消息发送到另一台计算机。

这可能是一个难以跟踪和解决的问题。

另一种选择是使用不同的 OpenGL 实现(如 Mesa)。可能会更好地检查另一个实现调用,您可以发现带有错误参数的调用是什么。

甚至可能是您的代码只是触发了视频驱动程序中的错误,而您的代码没有做错任何事情。然而,这应该是你最后的想法。

于 2012-09-09T05:44:26.153 回答
0

其实答案很简单。Windows 上的 Code::Blocks 中的调试器确实有问题。我已经看到它蓝屏多个系统。切换到使用输出语句或其他 IDE。

于 2012-09-09T15:21:25.927 回答