Skip to content

Commit

Permalink
Begin implementing ParticleSystem.
Browse files Browse the repository at this point in the history
  • Loading branch information
christiancosgrove committed Feb 1, 2015
1 parent 444cb47 commit 3a4e152
Show file tree
Hide file tree
Showing 9 changed files with 149 additions and 83 deletions.
6 changes: 6 additions & 0 deletions PlanetRendering.xcodeproj/project.pbxproj
Expand Up @@ -22,6 +22,7 @@
AACC3ED519DCE8B700FEDC84 /* SDL2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACC3ED419DCE8B700FEDC84 /* SDL2.framework */; };
AAE3CF771A01D434004D10E5 /* postFragmentShader.glsl in Resources */ = {isa = PBXBuildFile; fileRef = AAE3CF761A01D434004D10E5 /* postFragmentShader.glsl */; };
AAE3CF791A01D441004D10E5 /* postVertexShader.glsl in Resources */ = {isa = PBXBuildFile; fileRef = AAE3CF781A01D441004D10E5 /* postVertexShader.glsl */; };
CA214C6D1A7BF29100DF0CC0 /* ParticleSystem.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CA214C6B1A7BF29100DF0CC0 /* ParticleSystem.cpp */; };
CA29317C1A03E51E00C45002 /* README.txt in Resources */ = {isa = PBXBuildFile; fileRef = CA29317B1A03E51E00C45002 /* README.txt */; };
CA50C52819F960FC00FACF98 /* PhysicsObject.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CA50C52619F960FC00FACF98 /* PhysicsObject.cpp */; };
CA80FD3F19F6B79D009AA760 /* PlanetAtmosphere.cpp in Sources */ = {isa = PBXBuildFile; fileRef = CA80FD3D19F6B79D009AA760 /* PlanetAtmosphere.cpp */; };
Expand Down Expand Up @@ -77,6 +78,8 @@
AACC3ED419DCE8B700FEDC84 /* SDL2.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; path = SDL2.framework; sourceTree = "<group>"; };
AAE3CF761A01D434004D10E5 /* postFragmentShader.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = postFragmentShader.glsl; sourceTree = "<group>"; };
AAE3CF781A01D441004D10E5 /* postVertexShader.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = postVertexShader.glsl; sourceTree = "<group>"; };
CA214C6B1A7BF29100DF0CC0 /* ParticleSystem.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = ParticleSystem.cpp; sourceTree = "<group>"; };
CA214C6C1A7BF29100DF0CC0 /* ParticleSystem.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = ParticleSystem.h; sourceTree = "<group>"; };
CA29317B1A03E51E00C45002 /* README.txt */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = README.txt; sourceTree = "<group>"; };
CA50C52619F960FC00FACF98 /* PhysicsObject.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = PhysicsObject.cpp; sourceTree = "<group>"; };
CA50C52719F960FC00FACF98 /* PhysicsObject.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = PhysicsObject.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -171,6 +174,8 @@
CAADD54D1A6D330900EBC4CD /* TextureManager.h */,
CAA2A8FE1A700B48003003EA /* AABB.cpp */,
CAA2A8FF1A700B48003003EA /* AABB.h */,
CA214C6B1A7BF29100DF0CC0 /* ParticleSystem.cpp */,
CA214C6C1A7BF29100DF0CC0 /* ParticleSystem.h */,
);
path = PlanetRendering;
sourceTree = "<group>";
Expand Down Expand Up @@ -297,6 +302,7 @@
files = (
AA68DB6C19D83B5700B073C6 /* GLManager.cpp in Sources */,
CA50C52819F960FC00FACF98 /* PhysicsObject.cpp in Sources */,
CA214C6D1A7BF29100DF0CC0 /* ParticleSystem.cpp in Sources */,
CA80FD3F19F6B79D009AA760 /* PlanetAtmosphere.cpp in Sources */,
CA87707019F9CC37006F9708 /* SolarSystem.cpp in Sources */,
AACC3ED319DCE87500FEDC84 /* MainGame_SDL.cpp in Sources */,
Expand Down
4 changes: 2 additions & 2 deletions PlanetRendering/Camera.cpp
Expand Up @@ -5,15 +5,15 @@ Camera::Camera(int windowWidth, int windowHeight) :
aspectRatio(((float)windowWidth)/(float)windowHeight),
FieldOfView(60), XRotation(0.0f), ZRotation(90.0f), YRotation(0.0f),PlanetRotation(0.0),
NEAR_PLANE(0),//std::numeric_limits<float>::epsilon()),
FAR_PLANE(200.0f)
FAR_PLANE(2000.0f)
{
}

Camera::~Camera() {}

vmat4 Camera::GetViewMatrix()
{
return glm::rotate(glm::mat4(), glm::degrees(XRotation), glm::vec3(1,0,0)) * glm::rotate(glm::mat4(), glm::degrees(ZRotation), glm::vec3(0,0,1)) * glm::translate(-position);
return glm::rotate(vmat4(), glm::degrees(XRotation), vvec3(1,0,0)) * glm::rotate(vmat4(), glm::degrees(ZRotation), vvec3(0,0,1)) * glm::translate(-position);
}

void Camera::ResizedWindow(int windowWidth, int windowHeight)
Expand Down
13 changes: 12 additions & 1 deletion PlanetRendering/MainGame_SDL.cpp
Expand Up @@ -35,11 +35,22 @@ MainGame_SDL::MainGame_SDL() : gameState(GameState::PLAY)
SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, 4);
SDL_GL_SetSwapInterval(1);

const bool fullscreen=false;




//Initialize window and check for errors
SDL_DisplayMode currentMode;
SDL_GetCurrentDisplayMode(0, &currentMode);
window = SDL_CreateWindow("Planet Rendering", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, currentMode.w, currentMode.h, SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN_DESKTOP);

Uint32 windowFlags=SDL_WINDOW_OPENGL;
if (fullscreen) windowFlags|=SDL_WINDOW_FULLSCREEN_DESKTOP;

int width = fullscreen ? currentMode.w : 1280;
int height = fullscreen ? currentMode.h : 720;

window = SDL_CreateWindow("Planet Rendering", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, width,height, windowFlags);

WINDOW_WIDTH = currentMode.w;
WINDOW_HEIGHT = currentMode.h;
Expand Down
9 changes: 9 additions & 0 deletions PlanetRendering/ParticleSystem.cpp
@@ -0,0 +1,9 @@
//
// ParticleSystem.cpp
// PlanetRendering
//
// Created by Christian Cosgrove on 1/30/15.
// Copyright (c) 2015 Christian. All rights reserved.
//

#include "ParticleSystem.h"
34 changes: 34 additions & 0 deletions PlanetRendering/ParticleSystem.h
@@ -0,0 +1,34 @@
//
// ParticleSystem.h
// PlanetRendering
//
// Created by Christian Cosgrove on 1/30/15.
// Copyright (c) 2015 Christian. All rights reserved.
//
#pragma once
#include "PhysicsObject.h"
#include <vector>
#include "glm/glm.hpp"
struct Particle : PhysicsObject
{
bool Inactive = true;
Particle(glm::dvec3 position, glm::dvec3 velocity, double mass);
};


class ParticleSystem
{
public:

const int NUM_PARTICLES;


ParticleSystem();
private:

int currParticle;

std::vector<Particle> particles = std::vector<Particle>(NUM_PARTICLES);

inline void AddParticle(const Particle& particle);
};
141 changes: 73 additions & 68 deletions PlanetRendering/Planet.cpp
Expand Up @@ -106,15 +106,19 @@ vfloat pointLineDist(vvec2 point1, vvec2 point2, vvec2 point)


//This function accepts a boolean-valued function of displacement. If the function is true, the function will divide the given face into four subfaces, each of which will be recursively subjected to the same subdivision scheme. It is important that the input function terminates at a particular level of detail, or the program will crash.
bool Planet::trySubdivide(Face* iterator, const std::function<bool (Player&, const Face&)>& func, Player& player)
bool Planet::trySubdivide(Face* iterator, Player& player)
{
//perform horizon culling
// if (iterator->level!=0 && !inHorizon(*iterator)) return false;
if (iterator->level>MAX_LOD) return false;

if (closed) return false;

if (func(player, *iterator))
if (std::max(std::max(
glm::length(GetPlayerDisplacement() - iterator->vertices[0]),
glm::length(GetPlayerDisplacement() - iterator->vertices[1])),
glm::length(GetPlayerDisplacement() - iterator->vertices[2]))
< (vfloat)(1 << LOD_MULTIPLIER) / ((vfloat)(1 << (iterator->level))))
{
if (!iterator->AllChildrenNull())
return true;
Expand Down Expand Up @@ -146,9 +150,9 @@ bool Planet::trySubdivide(Face* iterator, const std::function<bool (Player&, con
// vvec2 p12((v1.x+v2.x) / 2,(v1.y+v2.y) / 2);
// vvec2 p13((v1.x+v3.x) / 2,(v1.y+v3.y) / 2);
// vvec2 p23((v2.x+v3.x) / 2,(v2.y+v3.y) / 2);
glm::dvec2 p12((v[0].x+v[1].x) / 2,(v[0].y+v[1].y) / 2);
glm::dvec2 p13((v[0].x+v[2].x) / 2,(v[0].y+v[2].y) / 2);
glm::dvec2 p23((v[1].x+v[2].x) / 2,(v[1].y+v[2].y) / 2);
glm::dvec2 p12(std::fmodf((v[0].x+v[1].x) / 2, M_PI),std::fmodf((v[0].y+v[1].y) / 2, M_2_PI));
glm::dvec2 p13(std::fmodf((v[0].x+v[2].x) / 2, M_PI),std::fmodf((v[0].y+v[2].y) / 2, M_2_PI));
glm::dvec2 p23(std::fmodf((v[1].x+v[2].x) / 2, M_PI),std::fmodf((v[1].y+v[2].y) / 2, M_2_PI));
//height scale of terrain
//proportional to 2^(-LOD) * nonlinear factor
//the nonlinear factor is LOD^(TERRAIN_REGULARITY)
Expand Down Expand Up @@ -182,15 +186,24 @@ bool Planet::trySubdivide(Face* iterator, const std::function<bool (Player&, con
return false;
}
//Similarly to trySubdivide, this function combines four faces into a larger face if a boolean-valued function is statisfied.
bool Planet::tryCombine(Face* iterator, const std::function<bool (Player&, const Face&)>& func, Player& player)
bool Planet::tryCombine(Face* iterator, Player& player)
{
if (closed) return false;
if (iterator==nullptr) return false;
if (iterator->AnyChildrenNull()) return false;
if ((func(player, *iterator) || func(player, *iterator->children[0])|| func(player, *iterator->children[1]) || func(player, *iterator->children[2]) || func(player, *iterator->children[3])) && iterator->level>0)



if (std::min(std::min(
glm::length(GetPlayerDisplacement() - iterator->vertices[0]),
glm::length(GetPlayerDisplacement() - iterator->vertices[1])),
glm::length(GetPlayerDisplacement() - iterator->vertices[2]))
>= (vfloat)(1 << (LOD_MULTIPLIER)) / ((vfloat)(1 << (iterator->level-1))) && iterator->level>0)
{
combineFace(iterator);

if (iterator->parent!=nullptr) tryCombine(iterator->parent, player);

return true;
}
return false;
Expand All @@ -211,39 +224,48 @@ void Planet::combineFace(Face* face)
//performed in background, manages terrain generation
void Planet::Update()
{

if (closed) return;
subdivided = false;
//iterate through faces and perform necessary generation checks


auto t = std::chrono::high_resolution_clock::now();

for (auto it = faces.begin();it!=faces.end();it++)
while (true)
{
if (recursiveCombine(&(*it), player))
subdivided=true;
if (recursiveSubdivide(&(*it), player))
subdivided=true;
}
// printf("1 time taken: %lli us\n", std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t).count());

//update vertices if changes were made

t = std::chrono::high_resolution_clock::now();

unsigned int indsize, vertsize;
GetIndicesVerticesSizes(indsize, vertsize);
if (subdivided || vertsize==0)
{
updateVBO(player);
lastPlayerUpdatePosition=player.Position;
if (closed) return;
subdivided = false;
//iterate through faces and perform necessary generation checks


// std::vector<Face*> rootFaces;
// getRootFaces(rootFaces, player);
// for (Face* f:rootFaces)
// tryCombine(f, [this](Player& player, Face f)->bool { return std::min(std::min(
// glm::length(GetPlayerDisplacement() - f.vertices[0]),
// glm::length(GetPlayerDisplacement() - f.vertices[1])),
// glm::length(GetPlayerDisplacement() - f.vertices[2]))
// >= (vfloat)(1 << (LOD_MULTIPLIER)) / ((vfloat)(1 << (f.level-1))); }, player);

auto t = std::chrono::high_resolution_clock::now();

for (auto it = faces.begin();it!=faces.end();it++)
{
if (recursiveCombine(&(*it), player))
subdivided=true;
if (recursiveSubdivide(&(*it), player))
subdivided=true;
}
// printf("1 time taken: %lli us\n", std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t).count());

//update vertices if changes were made

t = std::chrono::high_resolution_clock::now();

unsigned int indsize, vertsize;
GetIndicesVerticesSizes(indsize, vertsize);
if (subdivided || vertsize==0)
{
updateVBO(player);
lastPlayerUpdatePosition=player.Position;
}
// printf("2 time taken: %lli us\n", std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t).count());
// std::cout << "Height above earth surface: " << player.DistFromSurface * EARTH_DIAMETER << " m\n";
//repeat indefinitely (on separate thread)
}
// printf("2 time taken: %lli us\n", std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t).count());
// std::cout << "Height above earth surface: " << player.DistFromSurface * EARTH_DIAMETER << " m\n";
//repeat indefinitely (on separate thread)

Update();
}

void Planet::generateBuffers()
Expand Down Expand Up @@ -334,14 +356,7 @@ void Planet::recursiveUpdate(Face& face, unsigned int index1, unsigned int index

bool Planet::recursiveSubdivide(Face* face, Player& player)
{
if (trySubdivide(face,
[this](Player& player, Face f)->bool {
return std::max(std::max(
glm::length(GetPlayerDisplacement() - f.vertices[0]),
glm::length(GetPlayerDisplacement() - f.vertices[1])),
glm::length(GetPlayerDisplacement() - f.vertices[2]))
< (float)(1 << LOD_MULTIPLIER) / ((float)(1 << (f.level))); }
, player))
if (trySubdivide(face, player))
{
for (Face* f:face->children) recursiveSubdivide(f, player);
return true;
Expand All @@ -354,20 +369,10 @@ bool Planet::recursiveCombine(Face* face, Player& player)
{
if (closed) return false;
if (face==nullptr) return false;
if (!tryCombine(face,

[this](Player& player, Face f)->bool { return std::min(std::min(
glm::length(GetPlayerDisplacement() - f.vertices[0]),
glm::length(GetPlayerDisplacement() - f.vertices[1])),
glm::length(GetPlayerDisplacement() - f.vertices[2] - static_cast<vvec3>(Position)))
>= (double)(1 << (LOD_MULTIPLIER)) / ((double)(1 << (f.level-1))); }

, player))
if (!tryCombine(face, player))
{
if (face->parent!=nullptr)
{
recursiveCombine(face->parent, player);
}
for (auto& child:face->children)
recursiveCombine(child, player);
return false;
}
else return true;
Expand Down Expand Up @@ -424,7 +429,7 @@ void Planet::updateVBO(Player& player)

// return c1.x + 2 * Radius * c1.y + 4 * Radius * Radius * c1.z <
// c2.x + 2 * Radius * c2.y + 4 * Radius * Radius * c2.z;
return c1.x + 2*M_2_PI * c1.y < c2.x + 2*M_2_PI*c2.y;
return c1.x + 20*M_2_PI * c1.y < c2.x + 20*M_2_PI*c2.y; //this multiplier must be sufficiently large to separate the x and y components of the polar coordinates
};
// if (getPlayerDisplacementSquared(player)>displacementThreshold) return;
t = std::chrono::high_resolution_clock::now();
Expand All @@ -436,7 +441,7 @@ void Planet::updateVBO(Player& player)
std::sort(verticesSorted.begin(), verticesSorted.end(),func);
#endif
// if (getPlayerDisplacementSquared(player)>displacementThreshold) return;
printf("Time: %lli\n", std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()-t).count());
// printf("Time: %lli\n", std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now()-t).count());
for (int j = 0; j<verticesSorted.size();j++)
{
const int maxNeighbors=6;
Expand All @@ -446,7 +451,7 @@ void Planet::updateVBO(Player& player)

vvec3 normal = rootFaces.at(verticesSorted[j].second)->GetNormal();

newVertices.push_back(Vertex(rootFaces.at(verticesSorted[j].second)->vertices.at(verticesSorted[j].first),(vvec2)rootFaces.at(verticesSorted[j].second)->polarCoords.at(verticesSorted[j].first), glm::vec3()));
newVertices.push_back(Vertex(rootFaces.at(verticesSorted[j].second)->vertices.at(verticesSorted[j].first),(vvec2)rootFaces.at(verticesSorted[j].second)->polarCoords.at(verticesSorted[j].first), vvec3()));

rootFaces.at(verticesSorted[j].second)->indices.at(verticesSorted[j].first)=currentSize;
int neig=0;
Expand All @@ -456,7 +461,7 @@ void Planet::updateVBO(Player& player)
if (neig>maxNeighbors) break;
if (k==j) continue;
if (rootFaces.at(verticesSorted[k].second)->indices.at(verticesSorted[k].first)!=-1) continue;
glm::dvec2 v1 =rootFaces.at(verticesSorted[k].second)->polarCoords.at(verticesSorted[k].first);
glm::dvec2 v1 = rootFaces.at(verticesSorted[k].second)->polarCoords.at(verticesSorted[k].first);
glm::dvec2 v2 = rootFaces.at(verticesSorted[j].second)->polarCoords.at(verticesSorted[j].first);
if (glm::length2(v1-v2)<std::numeric_limits<double>::epsilon())
{
Expand Down Expand Up @@ -486,15 +491,15 @@ void Planet::updateVBO(Player& player)
newIndices.push_back(f->indices[2]);
}
}
{
std::ofstream stream(resourcePath() + performanceOutput, std::ios::out | std::ios::app);
stream << rootFaces.size() << "," << std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t).count() <<"\n";
}
#else
for (Face& f : faces)
recursiveUpdate(f, 0, 0, 0, player, newVertices, newIndices);
#endif
// if (getPlayerDisplacementSquared(player)>displacementThreshold) return;
{
std::ofstream stream(resourcePath() + performanceOutput, std::ios::out | std::ios::app);
stream << rootFaces.size() << "," << std::chrono::duration_cast<std::chrono::microseconds>(std::chrono::high_resolution_clock::now() - t).count() <<"\n";
}
if (!closed)
{
std::lock_guard<std::mutex> lock(renderMutex);
Expand Down Expand Up @@ -635,6 +640,6 @@ void Planet::setUniforms()
glManager.UpdateBuffer("planet_info", &PlanetInfo, sizeof(PlanetInfo));
// std::cout << "t: " << time << std::endl;

glManager.Programs[0].SetVector3("sunDir", glm::vec3(RotationMatrixInv * glm::vec4(0, 1,0.0,1.0)));
glManager.Programs[0].SetVector3("sunDir", glm::vec3(RotationMatrixInv * vvec4(0, 1,0.0,1.0)));
// player.Camera.PlanetRotation = CurrentRotationMode==RotationMode::ROTATION ? time*ROTATION_RATE : 0.0;
}

0 comments on commit 3a4e152

Please sign in to comment.