四叉树程序引起的蓝屏死机

时间:2012-09-09 05:24:34

标签: c++ opengl c-preprocessor quadtree bsod

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

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

为什么会这样?我该如何解决呢? enter image description here 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;
}

2 个答案:

答案 0 :(得分:6)

无论你做什么,一个蓝色的死亡屏幕都不可能形成一个常规的用户空间计划。

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

这意味着您正在使用错误的参数调用OpenGL,并且您的视频卡的驱动程序有一个错误,而不是检测到问题并重新调整失败代码,它只会占用机器。

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

跟踪和解决可能是一个难题。

另一种选择是使用不同的OpenGL实现(如Mesa)。可能与另一个实现调用更好地检查,你可以找到错误参数的调用。

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

答案 1 :(得分:0)

实际上答案很简单。在Windows上的Code :: Blocks中,调试器确实存在问题。我见过蓝屏多系统。切换到使用输出语句或其他IDE。