title: Blue Screen of Death caused by Quad Tree Program
body: <p>I am writing a Quad tree structure for a planet, that decreases and in increases in detail when you are far away from the quad and close to it receptively. However, I am running into some really serious, and annoying bugs. </p>
  
  <p>I have two preprocessor defined constant that determines the size of the Quad tree (QUAD_WIDTH and QUAD_HEIGHT) when I change the value to anything but 32 (16 or 64 for example) I get a blue screen of death. I am using code::blocks as my IDE, another thing: Whenever I try to debug the program in code::blocks I also get a blue screen of death (Doesn't matter if the constants are 32 or not)</p>
  
  <p>Why is this the case? And how can I fix it.
  <img src="https://i.stack.imgur.com/BQarV.jpg" alt="enter image description here">
  <strong><em>PQuad.cpp</em></strong></p>
  
  <pre><code>#include "..\include\PQuad.h"
  #include "..\include\Color3.h"
  
  #include &lt;iostream&gt;
  #include &lt;vector&gt;
  #include &lt;cmath&gt;
  
  #include &lt;GL/glew.h&gt;
  #include &lt;GL/glu.h&gt;
  #include &lt;GL/gl.h&gt;
  
  #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-&gt;built = false;
      this-&gt;spherised = false;
      this-&gt;face_direction = face_direction;
      this-&gt;radius = planet_radius;
      this-&gt;planet_centre = glm::vec3(0, 0, 0);
  }
  
  PQuad::~PQuad()  {
  }
  
  std::vector&lt;PQuad&gt; PQuad::get_children()  {
      return children;
  }
  
  bool PQuad::get_built()  {
      return this-&gt;built;
  }
  
  int PQuad::get_depth()  {
      return this-&gt;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&lt;PQuad*&gt;&amp; out_children)  {
      for (size_t i = 0; i &lt; children.size(); i++)  {
          children[i].get_recursive(player_pos, out_children);
      }
  
      if (this-&gt;should_draw(player_pos) ||
          this-&gt;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-&gt;parent = quad;
  }
  
  void PQuad::set_child_index(int child_index)  {
      this-&gt;child_index = child_index;
  }
  
  void PQuad::set_depth(int depth)  {
      this-&gt;depth = depth;
  }
  
  void PQuad::set_root(bool root)  {
      this-&gt;root = root;
  }
  
  void PQuad::calculate_position()  {
      this-&gt;element_width = depth == 0 ? 1.0f : parent-&gt;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-&gt;depth != 0)  {
          quad_x += parent-&gt;get_position().x;
          quad_y += parent-&gt;get_position().y;
      }
  
      this-&gt;position = glm::vec3(quad_x, quad_y, 0);
  }
  
  void PQuad::construct()  {
      if (!this-&gt;built)  {
          std::vector&lt;glm::vec3&gt; vertices;
          std::vector&lt;glm::vec3&gt; normals;
          std::vector&lt;Color3&gt; colors;
          std::vector&lt;GLushort&gt; elements;
  
          construct_vertices(&amp;vertices, &amp;colors);
          construct_elements(&amp;elements);
          spherise(&amp;vertices, &amp;normals);
          construct_normals(&amp;vertices, &amp;elements, &amp;normals);
  
          construct_buffers(&amp;vertices, &amp;colors, &amp;elements, &amp;normals);
  
          float distance = radius;
  
          if (!spherised)  {
              distance = QUAD_WIDTH;
          }
  
          construct_depth_table(distance);
  
          this-&gt;built = true;
      }
  }
  
  void PQuad::construct_depth_table(float distance)  {
      tree[0] = -1;
  
      for (int i = 1; i &lt; MAX_DEPTH; i++)  {
          tree[i] = distance;
  
          distance /= 2.0f;
      }
  }
  
  void PQuad::construct_children()  {
      calculate_position();
  
      if (depth &lt; (int)MAX_DEPTH)  {
          children.reserve((int)NUM_OF_CHILDREN);
  
          for (int i = 0; i &lt; (int)NUM_OF_CHILDREN; i++)  {
              children.emplace_back(PQuad(this-&gt;face_direction, this-&gt;radius));
              PQuad *child = &amp;children.back();
  
              child-&gt;set_depth(depth + 1);
              child-&gt;set_child_index(i);
              child-&gt;set_parent(this);
  
              child-&gt;construct_children();
          }
      } else {
          leaf = true;
      }
  }
  
  void PQuad::construct_vertices(std::vector&lt;glm::vec3&gt; *vertices, std::vector&lt;Color3&gt; *colors)  {
      vertices-&gt;reserve(QUAD_WIDTH * QUAD_HEIGHT);
  
      for (int y = 0; y &lt; QUAD_HEIGHT; y++)  {
          for (int x = 0; x &lt; QUAD_WIDTH; x++)  {
              switch (face_direction)  {
                  case YIncreasing:
                      vertices-&gt;emplace_back(glm::vec3(position.x + x * element_width, QUAD_HEIGHT - 1, -(position.y + y * element_width)));
                      break;
                  case YDecreasing:
                      vertices-&gt;emplace_back(glm::vec3(position.x + x * element_width, 0, -(position.y + y * element_width)));
                      break;
                  case XIncreasing:
                      vertices-&gt;emplace_back(glm::vec3(QUAD_WIDTH - 1, position.y + y * element_width, -(position.x + x * element_width)));
                      break;
                  case XDecreasing:
                      vertices-&gt;emplace_back(glm::vec3(0, position.y + y * element_width, -(position.x + x * element_width)));
                      break;
                  case ZIncreasing:
                      vertices-&gt;emplace_back(glm::vec3(position.x + x * element_width, position.y + y * element_width, 0));
                      break;
                  case ZDecreasing:
                      vertices-&gt;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-&gt;size() - 1] -= glm::vec3(QUAD_WIDTH / 2.0f, QUAD_WIDTH  / 2.0f, -(QUAD_WIDTH / 2.0f));
  
              colors-&gt;emplace_back(Color3(255.0f, 255.0f, 255.0f, false));
          }
      }
  
      switch (face_direction)  {
          case YIncreasing:
              this-&gt;centre = glm::vec3(position.x + QUAD_WIDTH / 2.0f, QUAD_HEIGHT - 1, -(position.y + QUAD_HEIGHT / 2.0f));
              break;
          case YDecreasing:
              this-&gt;centre = glm::vec3(position.x + QUAD_WIDTH / 2.0f, 0, -(position.y + QUAD_HEIGHT / 2));
              break;
          case XIncreasing:
              this-&gt;centre = glm::vec3(QUAD_WIDTH - 1, position.y + QUAD_HEIGHT / 2.0f, -(position.x + QUAD_WIDTH / 2.0f));
              break;
          case XDecreasing:
              this-&gt;centre = glm::vec3(0, position.y + QUAD_HEIGHT / 2.0f, -(position.x + QUAD_WIDTH / 2.0f));
              break;
          case ZIncreasing:
              this-&gt;centre = glm::vec3(position.x + QUAD_WIDTH / 2.0f, position.y + QUAD_HEIGHT / 2.0f, 0);
              break;
          case ZDecreasing:
              this-&gt;centre = glm::vec3(position.x + QUAD_WIDTH / 2.0f, position.y + QUAD_HEIGHT / 2.0f, -(QUAD_HEIGHT - 1));
              break;
      }
  
      this-&gt;centre -= glm::vec3(QUAD_WIDTH / 2.0f, QUAD_WIDTH  / 2.0f, -(QUAD_WIDTH / 2.0f));
  }
  
  void PQuad::construct_elements(std::vector&lt;GLushort&gt; *elements)  {
      int index = 0;
  
      elements-&gt;reserve((QUAD_WIDTH - 1) * (QUAD_HEIGHT - 1) * 6);
  
      for (int y = 0; y &lt; QUAD_HEIGHT - 1; y++)  {
          for (int x = 0; x &lt; 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-&gt;emplace_back(top_left);
              elements-&gt;emplace_back(bottom_right);
              elements-&gt;emplace_back(bottom_left);
  
              elements-&gt;emplace_back(top_left);
              elements-&gt;emplace_back(top_right);
              elements-&gt;emplace_back(bottom_right);
          }
      }
  }
  
  void PQuad::construct_normals(std::vector&lt;glm::vec3&gt; *vertices, std::vector&lt;GLushort&gt; *elements, std::vector&lt;glm::vec3&gt; *normals)  {
      normals-&gt;reserve(QUAD_WIDTH * QUAD_HEIGHT);
  
      for (int i = 0; i &lt; elements-&gt;size() / 3; i++)  {
           int index1 = elements-&gt;at(i * 3);
           int index2 = elements-&gt;at(i * 3 + 1);
           int index3 = elements-&gt;at(i * 3 + 2);
  
           glm::vec3 side1 = vertices-&gt;at(index1) - vertices-&gt;at(index3);
           glm::vec3 side2 = vertices-&gt;at(index1) - vertices-&gt;at(index2);
           glm::vec3 normal = glm::cross(side1, side2);
           normal = glm::normalize(normal);
  
           normals-&gt;emplace_back(normal);
           normals-&gt;emplace_back(normal);
           normals-&gt;emplace_back(normal);
      }
  }
  
  void PQuad::spherise(std::vector&lt;glm::vec3&gt; *vertices, std::vector&lt;glm::vec3&gt; *normals)  {
      for (int i = 0; i &lt; QUAD_WIDTH * QUAD_HEIGHT; i++)  {
          glm::vec3 normal = glm::normalize(vertices-&gt;at(i) - planet_centre);
  
          (*vertices)[i] = (float)(radius) * normal;
      }
  
      glm::vec3 normal = glm::normalize(centre - planet_centre);
  
      centre = normal * (float)(radius);
  
      this-&gt;spherised = true;
  }
  
  void PQuad::construct_buffers(std::vector&lt;glm::vec3&gt; *vertices, std::vector&lt;Color3&gt; *colors, std::vector&lt;GLushort&gt; *elements, std::vector&lt;glm::vec3&gt; *normals)  {
      glGenBuffers(1, &amp;vbo_vertices);
      glBindBuffer(GL_ARRAY_BUFFER, vbo_vertices);
      glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * vertices-&gt;size(), &amp;((*vertices)[0]), GL_STATIC_DRAW);
  
      glGenBuffers(1, &amp;vbo_colors);
      glBindBuffer(GL_ARRAY_BUFFER, vbo_colors);
      glBufferData(GL_ARRAY_BUFFER, sizeof(Color3) * colors-&gt;size(), &amp;((*colors)[0]), GL_STATIC_DRAW);
  
      glGenBuffers(1, &amp;vbo_normals);
      glBindBuffer(GL_ARRAY_BUFFER, vbo_normals);
      glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * normals-&gt;size(), &amp;((*normals)[0]), GL_STATIC_DRAW);
  
      glGenBuffers(1, &amp;ibo_elements);
      glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, ibo_elements);
      glBufferData(GL_ELEMENT_ARRAY_BUFFER, sizeof(GLushort) * elements-&gt;size(), &amp;((*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 &lt; tree[depth])  {
          return true;
      }
  
      return false;
  }
  </code></pre>