-
Notifications
You must be signed in to change notification settings - Fork 3
/
ParticleSystem.cpp
77 lines (58 loc) · 1.83 KB
/
ParticleSystem.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
//
// ParticleSystem.cpp
// PlanetRendering
//
// Created by Christian Cosgrove on 1/30/15.
// Copyright (c) 2015 Christian. All rights reserved.
//
#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);
// }
}