I wrote this program to display some dots that are stored in a quad tree. Essentially, I am trying to simulate a galaxy following the Barnes Hut algorithm. Before going further, I'm not quite sure if my quad tree is coded properly, and I want to display all the dots in a window using OpenGL. The only area that is being drawn on the screen is at the top right square and I can't seem to figure out why that is.
Here is my code. Please let me know if clarifications are needed:
quadtree.h
#pragma once
#include <vector>
using namespace std;
class Point
{
public:
Point(double x, double y);
double x;
double y;
};
class Rectangle
{
public:
Rectangle(double x, double y, double width, double height);
double x;
double y;
double width;
double height;
bool contains(Point* p);
};
class QuadTree
{
public:
QuadTree();
QuadTree(Rectangle* boundary, int capacity);
Rectangle* boundary;
int capacity;
vector<Point*> points;
QuadTree* NE;
QuadTree* NW;
QuadTree* SW;
QuadTree* SE;
bool divided;
void subdivide();
void buildTree();
bool insertToNode(Point* p);
};
quadtree.cpp
#include "quadtree.h"
#include <random>
#include <GLFW/glfw3.h>
#include <iostream>
#include <chrono>
using namespace std;
Point::Point(double x, double y)
{
this->x = x;
this->y = y;
}
Rectangle::Rectangle(double x, double y, double width, double height)
{
this->x = x;
this->y = y;
this->width = width;
this->height = height;
}
bool Rectangle::contains(Point* p)
{
return (p->x >= this->x - this->width &&
p->x < this->x + this->width &&
p->y >= this->y - this->height &&
p->y < this->y + this->height);
}
QuadTree::QuadTree()
{
this->boundary = new Rectangle(1, 1, 1, 1);
this->capacity = 5000;
this->divided = false;
Rectangle* ne = new Rectangle(this->boundary->x + (this->boundary->width / 2), this->boundary->y - (this->boundary->height / 2), this->boundary->width / 2, this->boundary->height / 2);
Rectangle* nw = new Rectangle(this->boundary->x - (this->boundary->width / 2), this->boundary->y - (this->boundary->height / 2), this->boundary->width / 2, this->boundary->height / 2);
Rectangle* sw = new Rectangle(this->boundary->x - (this->boundary->width / 2), this->boundary->y + (this->boundary->height / 2), this->boundary->width / 2, this->boundary->height / 2);
Rectangle* se = new Rectangle(this->boundary->x + (this->boundary->width / 2), this->boundary->y + (this->boundary->height / 2), this->boundary->width / 2, this->boundary->height / 2);
this->NE = new QuadTree(ne, this->capacity);
this->NW = new QuadTree(nw, this->capacity);
this->SW = new QuadTree(sw, this->capacity);
this->SE = new QuadTree(se, this->capacity);
this->divided = true;
}
QuadTree::QuadTree(Rectangle* boundary, int capacity)
{
this->boundary = boundary;
this->capacity = 5000;
this->divided = false;
}
void QuadTree::subdivide()
{
Rectangle* ne = new Rectangle(this->boundary->x + (this->boundary->width / 2), this->boundary->y - (this->boundary->height / 2), this->boundary->width / 2, this->boundary->height / 2);
Rectangle* nw = new Rectangle(this->boundary->x - (this->boundary->width / 2), this->boundary->y - (this->boundary->height / 2), this->boundary->width / 2, this->boundary->height / 2);
Rectangle* sw = new Rectangle(this->boundary->x - (this->boundary->width / 2), this->boundary->y + (this->boundary->height / 2), this->boundary->width / 2, this->boundary->height / 2);
Rectangle* se = new Rectangle(this->boundary->x + (this->boundary->width / 2), this->boundary->y + (this->boundary->height / 2), this->boundary->width / 2, this->boundary->height / 2);
this->NE = new QuadTree(ne, this->capacity);
this->NW = new QuadTree(nw, this->capacity);
this->SW = new QuadTree(sw, this->capacity);
this->SE = new QuadTree(se, this->capacity);
this->divided = true;
}
void QuadTree::buildTree()
{
std::default_random_engine generator(std::chrono::system_clock::now().time_since_epoch().count());
std::uniform_real_distribution<double> distribution(-1.0, 1.0);
double x;
double y;
for (int i = 0; i < 5000; i++)
{
x = distribution(generator);
y = distribution(generator);
this->insertToNode(new Point(x, y));
}
}
bool QuadTree::insertToNode(Point* p)
{
if (!this->boundary->contains(p))
{
return false;
}
if (this->points.size() < this->capacity)
{
this->points.push_back(p);
return true;
}
else
{
if (!this->divided)
{
this->subdivide();
}
if (this->NE->insertToNode(p))
{
return true;
}
else if (this->NW->insertToNode(p))
{
return true;
}
else if (this->SW->insertToNode(p))
{
return true;
}
else if (this->SE->insertToNode(p))
{
return true;
}
}
}
main.cpp
#include <iostream>
#include "quadtree.h"
#include <GLFW/glfw3.h>
using namespace std;
QuadTree* aTree = new QuadTree();
void display()
{
glColor3f(1.0, 1.0, 1.0);
glBegin(GL_POINTS);
for (int i = 0; i < aTree->points.size(); i++)
{
glColor3f(0.8, 0.196078, 0.6);
glVertex2d(aTree->points[i]->x, aTree->points[i]->y);
}
glEnd();
glFlush();
}
int main()
{
aTree->buildTree();
GLFWwindow* window;
if (!glfwInit()) {
cout << "Error initializing GLFW" << std::endl;
return -1;
}
window = glfwCreateWindow(800, 800, "Barne's Hut", NULL, NULL);
if (!window)
{
cout << "Error creating window" << std::endl;
glfwTerminate();
return -1;
}
glfwMakeContextCurrent(window);
while (!glfwWindowShouldClose(window))
{
display();
glfwSwapBuffers(window);
glfwPollEvents();
}
glfwTerminate();
system("pause");
return 0;
}
If you don't set a projection matrix, then the view volume which is projected to the view port is the normalized device space. This is a cube with a left bottom near of (-1, -1, -1) and a right top far of (1, 1, 1).
Use glOrtho
to define a cuboid view volume (orthographic projection) of your needs.
Set the range from the let to the right to [0, 1] and the range from the bottom to the top too.
Add the following lies of code right before the render loop:
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );
while (!glfwWindowShouldClose(window))
{
....
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.