我编写了此程序来显示存储在四叉树中的一些点。本质上,我正在尝试按照Barnes Hut算法模拟银河系。在继续之前,我不确定四叉树是否正确编码,我想使用OpenGL在窗口中显示所有点。屏幕上唯一绘制的区域是右上角的正方形,我似乎无法弄清楚为什么。
这是我的代码。请让我知道是否需要澄清:
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;
}
答案 0 :(得分:1)
如果未设置投影矩阵,则投影到视口的视区就是标准化的设备空间。这是一个立方体,其左下角在(-1,-1,-1)附近,右上角在(1,1,1)附近。
使用glOrtho
定义您所需的长方体视图体积(正投影)。
设置从let到右边的范围为[0,1],也从底部到顶部为范围。
在渲染循环之前添加以下代码谎言:
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );
while (!glfwWindowShouldClose(window))
{
....
}