[英]QuadTree only draws on the top right corner of the window
我寫了這個程序來顯示存儲在四叉樹中的一些點。 本質上,我正在嘗試按照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;
}
如果未設置投影矩陣,則投影到視口的視區就是規范化的設備空間。 這是一個立方體,其左下角在(-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))
{
....
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.