Skip to content

Commit

Permalink
Begin implementing particle drawing.
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian authored and Christian committed Feb 3, 2015
1 parent 3a4e152 commit 79d7fd1
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 10 deletions.
8 changes: 8 additions & 0 deletions PlanetRendering.xcodeproj/project.pbxproj
Expand Up @@ -17,6 +17,8 @@
AA68DB6E19D8404F00B073C6 /* OpenGL.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AA68DB6D19D8404F00B073C6 /* OpenGL.framework */; settings = {ATTRIBUTES = (Required, ); }; };
AA68DB7119D8407700B073C6 /* fragmentShader.glsl in Resources */ = {isa = PBXBuildFile; fileRef = AA68DB6F19D8407700B073C6 /* fragmentShader.glsl */; };
AA68DB7419D8408A00B073C6 /* vertexShader.glsl in Resources */ = {isa = PBXBuildFile; fileRef = AA68DB7319D8408A00B073C6 /* vertexShader.glsl */; };
AA6FE7BB1A807528009B5E8E /* vertexShaderParticles.glsl in Resources */ = {isa = PBXBuildFile; fileRef = AA6FE7BA1A807528009B5E8E /* vertexShaderParticles.glsl */; };
AA6FE7BD1A80755F009B5E8E /* fragmentShaderParticles.glsl in Resources */ = {isa = PBXBuildFile; fileRef = AA6FE7BC1A80755F009B5E8E /* fragmentShaderParticles.glsl */; };
AA852B2C1A64BC200048A87C /* RandomUtils.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AA852B2B1A64BC200048A87C /* RandomUtils.cpp */; };
AACC3ED319DCE87500FEDC84 /* MainGame_SDL.cpp in Sources */ = {isa = PBXBuildFile; fileRef = AACC3ED119DCE87500FEDC84 /* MainGame_SDL.cpp */; };
AACC3ED519DCE8B700FEDC84 /* SDL2.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = AACC3ED419DCE8B700FEDC84 /* SDL2.framework */; };
Expand Down Expand Up @@ -72,6 +74,8 @@
AA68DB6D19D8404F00B073C6 /* OpenGL.framework */ = {isa = PBXFileReference; lastKnownFileType = wrapper.framework; name = OpenGL.framework; path = System/Library/Frameworks/OpenGL.framework; sourceTree = SDKROOT; };
AA68DB6F19D8407700B073C6 /* fragmentShader.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; lineEnding = 0; path = fragmentShader.glsl; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.glsl; };
AA68DB7319D8408A00B073C6 /* vertexShader.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = vertexShader.glsl; sourceTree = "<group>"; };
AA6FE7BA1A807528009B5E8E /* vertexShaderParticles.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = vertexShaderParticles.glsl; sourceTree = "<group>"; };
AA6FE7BC1A80755F009B5E8E /* fragmentShaderParticles.glsl */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = text; path = fragmentShaderParticles.glsl; sourceTree = "<group>"; };
AA852B2B1A64BC200048A87C /* RandomUtils.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = RandomUtils.cpp; sourceTree = "<group>"; };
AACC3ED119DCE87500FEDC84 /* MainGame_SDL.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = MainGame_SDL.cpp; sourceTree = "<group>"; };
AACC3ED219DCE87500FEDC84 /* MainGame_SDL.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = MainGame_SDL.h; sourceTree = "<group>"; };
Expand Down Expand Up @@ -201,6 +205,8 @@
AAE3CF761A01D434004D10E5 /* postFragmentShader.glsl */,
AAE3CF781A01D441004D10E5 /* postVertexShader.glsl */,
CA29317B1A03E51E00C45002 /* README.txt */,
AA6FE7BA1A807528009B5E8E /* vertexShaderParticles.glsl */,
AA6FE7BC1A80755F009B5E8E /* fragmentShaderParticles.glsl */,
);
name = Resources;
sourceTree = "<group>";
Expand Down Expand Up @@ -265,6 +271,8 @@
files = (
AAE3CF791A01D441004D10E5 /* postVertexShader.glsl in Resources */,
CA29317C1A03E51E00C45002 /* README.txt in Resources */,
AA6FE7BD1A80755F009B5E8E /* fragmentShaderParticles.glsl in Resources */,
AA6FE7BB1A807528009B5E8E /* vertexShaderParticles.glsl in Resources */,
CA80FD4319F6BB2E009AA760 /* atmosphericFrag.glsl in Resources */,
AA4BABD719DA04B10022C8BC /* glm in Resources */,
AA68DB7119D8407700B073C6 /* fragmentShader.glsl in Resources */,
Expand Down
68 changes: 68 additions & 0 deletions PlanetRendering/ParticleSystem.cpp
Expand Up @@ -7,3 +7,71 @@
//

#include "ParticleSystem.h"

#include "RandomUtils.h"

Particle::Particle(glm::dvec3 position, glm::dvec3 velocity, double mass) : PhysicsObject(position, velocity, mass) {}


ParticleSystem::ParticleSystem(int numParticles) : NUM_PARTICLES(numParticles), particles(NUM_PARTICLES), drawArray(NUM_PARTICLES)
{
generateVBO();

for (int i = 0; i<NUM_PARTICLES;i++)
{
particles[i].Position = glm::vec3(
RandomUtils::Normal<float>(0, 0.1),
RandomUtils::Normal<float>(0, 0.1),
RandomUtils::Normal<float>(0, 0.1));
}
}

void ParticleSystem::generateVBO()
{
glGenVertexArrays(1, &vao);
glBindVertexArray(vao);

glGenBuffers(1, &vbo);
glBindBuffer(GL_ARRAY_BUFFER, vbo);


glEnableVertexAttribArray(0);
glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, sizeof(glm::vec3), (void*)0);

glBindVertexArray(0);
printf("GL error1: %i\n", glGetError());
}

void ParticleSystem::updateVBO()
{
for (int i = 0; i<NUM_PARTICLES;i++)
{
drawArray[i] = glm::vec3(particles[i].Position);
}
glBindVertexArray(vao);
glBufferData(GL_ARRAY_BUFFER, sizeof(glm::vec3) * drawArray.size(), &drawArray[0], GL_DYNAMIC_DRAW);
glBindVertexArray(0);
}

void ParticleSystem::Draw()
{
updateVBO();

//todo: fix: this drawing method causes crashes.
glBindVertexArray(vao);

glDrawArrays(GL_POINTS, 0, drawArray.size());

glBindVertexArray(0);

}

void ParticleSystem::Update(double timeStep)
{
// for (Particle& p:particles)
// {
//
// p.UpdatePhysics(timeStep);
// }
}

30 changes: 26 additions & 4 deletions PlanetRendering/ParticleSystem.h
Expand Up @@ -6,13 +6,15 @@
// Copyright (c) 2015 Christian. All rights reserved.
//
#pragma once
#include <OpenGL/gl3.h>
#include "PhysicsObject.h"
#include <vector>
#include "glm/glm.hpp"
struct Particle : PhysicsObject
{
bool Inactive = true;
Particle(glm::dvec3 position, glm::dvec3 velocity, double mass);
Particle() : PhysicsObject(glm::dvec3(), glm::dvec3(), 0.0) {}
};


Expand All @@ -23,12 +25,32 @@ class ParticleSystem
const int NUM_PARTICLES;


ParticleSystem();
ParticleSystem(int numParticles);
inline void AddParticle(const Particle& particle);
void Update(double timeStep);
void Draw();

private:

void generateVBO();

void updateVBO();

GLuint vao, vbo;
std::vector<glm::vec3> drawArray;

int currParticle;

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

inline void AddParticle(const Particle& particle);
};
};

void ParticleSystem::AddParticle(const Particle &particle)
{
for (int i = 0; i<NUM_PARTICLES && !particles[currParticle].Inactive; i++)
{
currParticle++;
currParticle%=NUM_PARTICLES;
}
particles[currParticle] = particle;
}
6 changes: 1 addition & 5 deletions PlanetRendering/PhysicsObject.h
Expand Up @@ -5,9 +5,7 @@
// Created by Christian Cosgrove on 10/23/14.
// Copyright (c) 2014 Christian. All rights reserved.
//

#ifndef __PlanetRendering__PhysicsObject__
#define __PlanetRendering__PhysicsObject__
#pragma once
#include "glm/glm.hpp"
#include "typedefs.h"

Expand All @@ -31,5 +29,3 @@ class PhysicsObject
private:

};

#endif /* defined(__PlanetRendering__PhysicsObject__) */
14 changes: 13 additions & 1 deletion PlanetRendering/SolarSystem.cpp
Expand Up @@ -8,7 +8,8 @@

#include "SolarSystem.h"
#include "RandomUtils.h"
SolarSystem::SolarSystem(Player& _player, GLManager& _glManager, int windowWidth, int windowHeight, const std::string& resourcePath) : player(_player), glManager(_glManager),
#include "glm/gtc/type_ptr.hpp"
SolarSystem::SolarSystem(Player& _player, GLManager& _glManager, int windowWidth, int windowHeight, const std::string& resourcePath) : player(_player), glManager(_glManager), particleSystem(100),
PhysicalSystem(8.,0.001, resourcePath), planets{
new Planet(0,glm::vec3(0,-2,0), 1, 100, RandomUtils::Uniform<vfloat>(-15,25), _player, _glManager, 0.3 + 0*RandomUtils::Uniform<float>(0.05f, 0.8f)),
new Planet(1,glm::vec3(0,2, 0), 1, 100, RandomUtils::Uniform<vfloat>(-25,25), _player, _glManager, 0.3 + 0*RandomUtils::Uniform<float>(0.05f, 0.8f)),
Expand All @@ -17,6 +18,10 @@ SolarSystem::SolarSystem(Player& _player, GLManager& _glManager, int windowWidth
#ifdef POSTPROCESSING
generateRenderTexture(windowWidth,windowHeight);
#endif

glManager.AddProgram("fragmentShaderParticles.glsl", "vertexShaderParticles.glsl");


for (auto& p : planets) objects.push_back(p);
planets[0]->Velocity=glm::dvec3(0,0,10);
planets[1]->Velocity=glm::dvec3(0,0,-10);
Expand All @@ -27,6 +32,7 @@ SolarSystem::SolarSystem(Player& _player, GLManager& _glManager, int windowWidth
void SolarSystem::Update()
{
PhysicalSystem::Update();
particleSystem.Update(TimeStep);
}

#include <iostream>
Expand All @@ -49,6 +55,12 @@ void SolarSystem::Draw(int windowWidth, int windowHeight)
glDrawArrays(GL_TRIANGLES, 0, 6);
glBindVertexArray(0);
#endif


glManager.Programs[3].Use();
glManager.Programs[3].SetMatrix4("transformMatrix", glm::value_ptr(player.Camera.GetTransformMatrix()));
particleSystem.Draw();
glManager.Programs[0].Use();
}

SolarSystem::~SolarSystem()
Expand Down
5 changes: 5 additions & 0 deletions PlanetRendering/SolarSystem.h
Expand Up @@ -9,6 +9,7 @@
#include "PhysicalSystem.h"
#include "Planet.h"
#include <array>
#include "ParticleSystem.h"
class SolarSystem : public PhysicalSystem
{
public:
Expand All @@ -27,5 +28,9 @@ class SolarSystem : public PhysicalSystem
GLuint screenVAO;
GLuint texLocation;
Planet::RenderMode currentRenderMode;

//temp
ParticleSystem particleSystem;

void generateRenderTexture(int windowWidth, int windowHeight);
};
7 changes: 7 additions & 0 deletions PlanetRendering/fragmentShaderParticles.glsl
@@ -0,0 +1,7 @@
#version 330 core
out vec3 color;

void main()
{
color = vec3(1,0,0);
}
10 changes: 10 additions & 0 deletions PlanetRendering/vertexShaderParticles.glsl
@@ -0,0 +1,10 @@
#version 330 core

layout(location=0) in vec3 vertexPosition;
uniform mat4 transformMatrix;

void main()
{
gl_Position = transformMatrix * vec4(vertexPosition, 1);

}

0 comments on commit 79d7fd1

Please sign in to comment.