fix spark
This commit is contained in:
parent
b855cff0e6
commit
115cbbb7fa
90
Game.h
90
Game.h
@ -9,59 +9,59 @@
|
||||
namespace ZL {
|
||||
|
||||
|
||||
struct BoxCoords
|
||||
{
|
||||
Vector3f pos;
|
||||
Matrix3f m;
|
||||
};
|
||||
struct BoxCoords
|
||||
{
|
||||
Vector3f pos;
|
||||
Matrix3f m;
|
||||
};
|
||||
|
||||
|
||||
class Game {
|
||||
public:
|
||||
Game();
|
||||
~Game();
|
||||
|
||||
void setup();
|
||||
void update();
|
||||
void render();
|
||||
|
||||
bool shouldExit() const { return Environment::exitGameLoop; }
|
||||
class Game {
|
||||
public:
|
||||
Game();
|
||||
~Game();
|
||||
|
||||
private:
|
||||
void processTickCount();
|
||||
void drawScene();
|
||||
void drawCubemap();
|
||||
void drawEffects();
|
||||
void drawShip();
|
||||
void drawBoxes();
|
||||
void setup();
|
||||
void update();
|
||||
void render();
|
||||
|
||||
SDL_Window* window;
|
||||
SDL_GLContext glContext;
|
||||
Renderer renderer;
|
||||
|
||||
size_t newTickCount;
|
||||
size_t lastTickCount;
|
||||
|
||||
static const size_t CONST_TIMER_INTERVAL = 10;
|
||||
static const size_t CONST_MAX_TIME_INTERVAL = 1000;
|
||||
bool shouldExit() const { return Environment::exitGameLoop; }
|
||||
|
||||
std::shared_ptr<Texture> sparkTexture;
|
||||
std::shared_ptr<Texture> spaceshipTexture;
|
||||
std::shared_ptr<Texture> cubemapTexture;
|
||||
VertexDataStruct spaceshipBase;
|
||||
VertexRenderStruct spaceship;
|
||||
private:
|
||||
void processTickCount();
|
||||
void drawScene();
|
||||
void drawCubemap();
|
||||
void drawEffects();
|
||||
void drawShip();
|
||||
void drawBoxes();
|
||||
|
||||
VertexRenderStruct cubemap;
|
||||
SDL_Window* window;
|
||||
SDL_GLContext glContext;
|
||||
Renderer renderer;
|
||||
|
||||
std::shared_ptr<Texture> boxTexture;
|
||||
VertexDataStruct boxBase;
|
||||
size_t newTickCount;
|
||||
size_t lastTickCount;
|
||||
|
||||
std::vector<BoxCoords> boxCoordsArr;
|
||||
std::vector<VertexRenderStruct> boxRenderArr;
|
||||
static const size_t CONST_TIMER_INTERVAL = 10;
|
||||
static const size_t CONST_MAX_TIME_INTERVAL = 1000;
|
||||
|
||||
//VertexRenderStruct singleSpark;
|
||||
SparkEmitter sparkEmitter;
|
||||
VertexRenderStruct sparkQuad;
|
||||
};
|
||||
std::shared_ptr<Texture> sparkTexture;
|
||||
std::shared_ptr<Texture> spaceshipTexture;
|
||||
std::shared_ptr<Texture> cubemapTexture;
|
||||
VertexDataStruct spaceshipBase;
|
||||
VertexRenderStruct spaceship;
|
||||
|
||||
VertexRenderStruct cubemap;
|
||||
|
||||
std::shared_ptr<Texture> boxTexture;
|
||||
VertexDataStruct boxBase;
|
||||
|
||||
std::vector<BoxCoords> boxCoordsArr;
|
||||
std::vector<VertexRenderStruct> boxRenderArr;
|
||||
|
||||
//VertexRenderStruct singleSpark;
|
||||
SparkEmitter sparkEmitter;
|
||||
VertexRenderStruct sparkQuad;
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
296
SparkEmitter.cpp
296
SparkEmitter.cpp
@ -5,143 +5,207 @@
|
||||
|
||||
namespace ZL {
|
||||
|
||||
SparkEmitter::SparkEmitter()
|
||||
: emissionRate(100.0f), isActive(true) {
|
||||
particles.resize(200);
|
||||
lastEmissionTime = std::chrono::steady_clock::now();
|
||||
}
|
||||
SparkEmitter::SparkEmitter()
|
||||
: emissionRate(100.0f), isActive(true), buffersDirty(true), maxParticles(200) {
|
||||
particles.resize(maxParticles);
|
||||
positionBuffer.resize(maxParticles * 4, 0.0f);
|
||||
texCoordBuffer.resize(maxParticles * 2, 0.0f);
|
||||
lastEmissionTime = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
SparkEmitter::SparkEmitter(const std::vector<Vector3f>& positions, float rate)
|
||||
: emissionPoints(positions), emissionRate(rate), isActive(true) {
|
||||
particles.resize(positions.size() * 100);
|
||||
lastEmissionTime = std::chrono::steady_clock::now();
|
||||
}
|
||||
SparkEmitter::SparkEmitter(const std::vector<Vector3f>& positions, float rate)
|
||||
: emissionPoints(positions), emissionRate(rate), isActive(true),
|
||||
buffersDirty(true), maxParticles(positions.size() * 100) {
|
||||
particles.resize(maxParticles);
|
||||
positionBuffer.resize(maxParticles * 4, 0.0f);
|
||||
texCoordBuffer.resize(maxParticles * 2, 0.0f);
|
||||
lastEmissionTime = std::chrono::steady_clock::now();
|
||||
}
|
||||
|
||||
void SparkEmitter::setEmissionPoints(const std::vector<Vector3f>& positions) {
|
||||
emissionPoints = positions;
|
||||
particles.resize(positions.size() * 100);
|
||||
}
|
||||
void SparkEmitter::setEmissionPoints(const std::vector<Vector3f>& positions) {
|
||||
emissionPoints = positions;
|
||||
maxParticles = positions.size() * 100;
|
||||
particles.resize(maxParticles);
|
||||
positionBuffer.resize(maxParticles * 4, 0.0f);
|
||||
texCoordBuffer.resize(maxParticles * 2, 0.0f);
|
||||
buffersDirty = true;
|
||||
}
|
||||
|
||||
void SparkEmitter::setEmissionRate(float rateMs) {
|
||||
emissionRate = rateMs;
|
||||
}
|
||||
void SparkEmitter::updateBuffers() {
|
||||
if (!buffersDirty) return;
|
||||
|
||||
void SparkEmitter::setActive(bool active) {
|
||||
isActive = active;
|
||||
}
|
||||
size_t bufferIndex = 0;
|
||||
for (const auto& particle : particles) {
|
||||
if (particle.active) {
|
||||
positionBuffer[bufferIndex * 4] = particle.position.v[0];
|
||||
positionBuffer[bufferIndex * 4 + 1] = particle.position.v[1];
|
||||
positionBuffer[bufferIndex * 4 + 2] = particle.position.v[2];
|
||||
positionBuffer[bufferIndex * 4 + 3] = particle.scale;
|
||||
|
||||
void SparkEmitter::update(float deltaTimeMs) {
|
||||
auto currentTime = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
currentTime - lastEmissionTime).count();
|
||||
texCoordBuffer[bufferIndex * 2] = 0.0f;
|
||||
texCoordBuffer[bufferIndex * 2 + 1] = 0.0f;
|
||||
|
||||
if (isActive && elapsed >= emissionRate) {
|
||||
emit();
|
||||
lastEmissionTime = currentTime;
|
||||
}
|
||||
bufferIndex++;
|
||||
}
|
||||
}
|
||||
|
||||
for (auto& particle : particles) {
|
||||
if (particle.active) {
|
||||
particle.position.v[0] += particle.velocity.v[0] * deltaTimeMs / 1000.0f;
|
||||
particle.position.v[1] += particle.velocity.v[1] * deltaTimeMs / 1000.0f;
|
||||
particle.position.v[2] += particle.velocity.v[2] * deltaTimeMs / 1000.0f;
|
||||
for (size_t i = bufferIndex; i < maxParticles; i++) {
|
||||
positionBuffer[i * 4] = 0.0f;
|
||||
positionBuffer[i * 4 + 1] = 0.0f;
|
||||
positionBuffer[i * 4 + 2] = 0.0f;
|
||||
positionBuffer[i * 4 + 3] = 0.0f;
|
||||
texCoordBuffer[i * 2] = 0.0f;
|
||||
texCoordBuffer[i * 2 + 1] = 0.0f;
|
||||
}
|
||||
|
||||
particle.lifeTime += deltaTimeMs;
|
||||
buffersDirty = false;
|
||||
}
|
||||
|
||||
if (particle.lifeTime >= particle.maxLifeTime) {
|
||||
particle.active = false;
|
||||
}
|
||||
void SparkEmitter::update(float deltaTimeMs) {
|
||||
auto currentTime = std::chrono::steady_clock::now();
|
||||
auto elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(
|
||||
currentTime - lastEmissionTime).count();
|
||||
|
||||
float lifeRatio = particle.lifeTime / particle.maxLifeTime;
|
||||
particle.scale = 1.0f - lifeRatio * 0.8f;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (isActive && elapsed >= emissionRate) {
|
||||
emit();
|
||||
lastEmissionTime = currentTime;
|
||||
}
|
||||
|
||||
void SparkEmitter::emit() {
|
||||
if (emissionPoints.empty()) return;
|
||||
bool anyChanged = false;
|
||||
for (auto& particle : particles) {
|
||||
if (particle.active) {
|
||||
Vector3f oldPosition = particle.position;
|
||||
|
||||
for (int i = 0; i < emissionPoints.size(); ++i) {
|
||||
bool particleFound = false;
|
||||
particle.position.v[0] += particle.velocity.v[0] * deltaTimeMs / 1000.0f;
|
||||
particle.position.v[1] += particle.velocity.v[1] * deltaTimeMs / 1000.0f;
|
||||
particle.position.v[2] += particle.velocity.v[2] * deltaTimeMs / 1000.0f;
|
||||
|
||||
for (auto& particle : particles) {
|
||||
if (!particle.active) {
|
||||
initParticle(particle, i);
|
||||
particle.active = true;
|
||||
particle.lifeTime = 0;
|
||||
particle.position = emissionPoints[i];
|
||||
particle.emitterIndex = i;
|
||||
particleFound = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
particle.lifeTime += deltaTimeMs;
|
||||
|
||||
if (!particleFound && !particles.empty()) {
|
||||
size_t oldestIndex = 0;
|
||||
float maxLifeTime = 0;
|
||||
if (particle.lifeTime >= particle.maxLifeTime) {
|
||||
particle.active = false;
|
||||
anyChanged = true;
|
||||
}
|
||||
else {
|
||||
float lifeRatio = particle.lifeTime / particle.maxLifeTime;
|
||||
particle.scale = 1.0f - lifeRatio * 0.8f;
|
||||
|
||||
for (size_t j = 0; j < particles.size(); ++j) {
|
||||
if (particles[j].lifeTime > maxLifeTime) {
|
||||
maxLifeTime = particles[j].lifeTime;
|
||||
oldestIndex = j;
|
||||
}
|
||||
}
|
||||
if (oldPosition.v[0] != particle.position.v[0] ||
|
||||
oldPosition.v[1] != particle.position.v[1] ||
|
||||
oldPosition.v[2] != particle.position.v[2]) {
|
||||
anyChanged = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
initParticle(particles[oldestIndex], i);
|
||||
particles[oldestIndex].active = true;
|
||||
particles[oldestIndex].lifeTime = 0;
|
||||
particles[oldestIndex].position = emissionPoints[i];
|
||||
particles[oldestIndex].emitterIndex = i;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (anyChanged) {
|
||||
buffersDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
void SparkEmitter::initParticle(SparkParticle& particle, int emitterIndex) {
|
||||
particle.velocity = getRandomVelocity(emitterIndex);
|
||||
particle.scale = 1.0f;
|
||||
particle.maxLifeTime = 800.0f + (rand() % 400); // От 800 до 1200 мс
|
||||
particle.emitterIndex = emitterIndex;
|
||||
}
|
||||
void SparkEmitter::emit() {
|
||||
if (emissionPoints.empty()) return;
|
||||
bool emitted = false;
|
||||
|
||||
Vector3f SparkEmitter::getRandomVelocity(int emitterIndex) {
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
static std::uniform_real_distribution<> angleDist(0, 2 * M_PI);
|
||||
static std::uniform_real_distribution<> speedDist(0.5f, 2.0f);
|
||||
static std::uniform_real_distribution<> zSpeedDist(1.0f, 3.0f);
|
||||
for (int i = 0; i < emissionPoints.size(); ++i) {
|
||||
bool particleFound = false;
|
||||
|
||||
float angle = angleDist(gen);
|
||||
float speed = speedDist(gen);
|
||||
float zSpeed = zSpeedDist(gen);
|
||||
for (auto& particle : particles) {
|
||||
if (!particle.active) {
|
||||
initParticle(particle, i);
|
||||
particle.active = true;
|
||||
particle.lifeTime = 0;
|
||||
particle.position = emissionPoints[i];
|
||||
particle.emitterIndex = i;
|
||||
particleFound = true;
|
||||
emitted = true;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (emitterIndex == 0) {
|
||||
return Vector3f{
|
||||
cosf(angle) * speed - 0.3f,
|
||||
sinf(angle) * speed,
|
||||
zSpeed
|
||||
};
|
||||
}
|
||||
else {
|
||||
return Vector3f{
|
||||
cosf(angle) * speed + 0.3f,
|
||||
sinf(angle) * speed,
|
||||
zSpeed
|
||||
};
|
||||
}
|
||||
}
|
||||
if (!particleFound && !particles.empty()) {
|
||||
size_t oldestIndex = 0;
|
||||
float maxLifeTime = 0;
|
||||
|
||||
const std::vector<SparkParticle>& SparkEmitter::getParticles() const {
|
||||
return particles;
|
||||
}
|
||||
for (size_t j = 0; j < particles.size(); ++j) {
|
||||
if (particles[j].lifeTime > maxLifeTime) {
|
||||
maxLifeTime = particles[j].lifeTime;
|
||||
oldestIndex = j;
|
||||
}
|
||||
}
|
||||
|
||||
size_t SparkEmitter::getActiveParticleCount() const {
|
||||
size_t count = 0;
|
||||
for (const auto& particle : particles) {
|
||||
if (particle.active) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
initParticle(particles[oldestIndex], i);
|
||||
particles[oldestIndex].active = true;
|
||||
particles[oldestIndex].lifeTime = 0;
|
||||
particles[oldestIndex].position = emissionPoints[i];
|
||||
particles[oldestIndex].emitterIndex = i;
|
||||
emitted = true;
|
||||
}
|
||||
}
|
||||
|
||||
if (emitted) {
|
||||
buffersDirty = true;
|
||||
}
|
||||
}
|
||||
|
||||
void SparkEmitter::initParticle(SparkParticle& particle, int emitterIndex) {
|
||||
particle.velocity = getRandomVelocity(emitterIndex);
|
||||
particle.scale = 1.0f;
|
||||
particle.maxLifeTime = 800.0f + (rand() % 400);
|
||||
particle.emitterIndex = emitterIndex;
|
||||
}
|
||||
|
||||
Vector3f SparkEmitter::getRandomVelocity(int emitterIndex) {
|
||||
static std::random_device rd;
|
||||
static std::mt19937 gen(rd());
|
||||
static std::uniform_real_distribution<> angleDist(0, 2 * M_PI);
|
||||
static std::uniform_real_distribution<> speedDist(0.5f, 2.0f);
|
||||
static std::uniform_real_distribution<> zSpeedDist(1.0f, 3.0f);
|
||||
|
||||
float angle = angleDist(gen);
|
||||
float speed = speedDist(gen);
|
||||
float zSpeed = zSpeedDist(gen);
|
||||
|
||||
if (emitterIndex == 0) {
|
||||
return Vector3f{
|
||||
cosf(angle) * speed - 0.3f,
|
||||
sinf(angle) * speed,
|
||||
zSpeed
|
||||
};
|
||||
}
|
||||
else {
|
||||
return Vector3f{
|
||||
cosf(angle) * speed + 0.3f,
|
||||
sinf(angle) * speed,
|
||||
zSpeed
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const std::vector<SparkParticle>& SparkEmitter::getParticles() const {
|
||||
return particles;
|
||||
}
|
||||
|
||||
size_t SparkEmitter::getActiveParticleCount() const {
|
||||
size_t count = 0;
|
||||
for (const auto& particle : particles) {
|
||||
if (particle.active) {
|
||||
count++;
|
||||
}
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
size_t SparkEmitter::getActiveParticleBufferIndex(size_t particleIndex) const {
|
||||
size_t activeIndex = 0;
|
||||
for (size_t i = 0; i <= particleIndex; i++) {
|
||||
if (particles[i].active) {
|
||||
if (i == particleIndex) return activeIndex;
|
||||
activeIndex++;
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
} // namespace ZL
|
||||
@ -6,45 +6,56 @@
|
||||
|
||||
namespace ZL {
|
||||
|
||||
struct SparkParticle {
|
||||
Vector3f position;
|
||||
Vector3f velocity;
|
||||
float scale;
|
||||
float lifeTime;
|
||||
float maxLifeTime;
|
||||
bool active;
|
||||
int emitterIndex;
|
||||
struct SparkParticle {
|
||||
Vector3f position;
|
||||
Vector3f velocity;
|
||||
float scale;
|
||||
float lifeTime;
|
||||
float maxLifeTime;
|
||||
bool active;
|
||||
int emitterIndex;
|
||||
Vector2f texCoord;
|
||||
|
||||
SparkParticle() : position({ 0,0,0 }), velocity({ 0,0,0 }), scale(1.0f),
|
||||
lifeTime(0), maxLifeTime(1000.0f), active(false), emitterIndex(0) {
|
||||
}
|
||||
};
|
||||
SparkParticle() : position({ 0,0,0 }), velocity({ 0,0,0 }), scale(1.0f),
|
||||
lifeTime(0), maxLifeTime(1000.0f), active(false), emitterIndex(0) {
|
||||
}
|
||||
};
|
||||
|
||||
class SparkEmitter {
|
||||
private:
|
||||
std::vector<SparkParticle> particles;
|
||||
std::vector<Vector3f> emissionPoints;
|
||||
std::chrono::steady_clock::time_point lastEmissionTime;
|
||||
float emissionRate;
|
||||
bool isActive;
|
||||
class SparkEmitter {
|
||||
private:
|
||||
std::vector<SparkParticle> particles;
|
||||
std::vector<Vector3f> emissionPoints;
|
||||
std::chrono::steady_clock::time_point lastEmissionTime;
|
||||
float emissionRate;
|
||||
bool isActive;
|
||||
|
||||
public:
|
||||
SparkEmitter();
|
||||
SparkEmitter(const std::vector<Vector3f>& positions, float rate = 100.0f);
|
||||
std::vector<float> positionBuffer;
|
||||
std::vector<float> texCoordBuffer;
|
||||
bool buffersDirty;
|
||||
int maxParticles;
|
||||
|
||||
void setEmissionPoints(const std::vector<Vector3f>& positions);
|
||||
void setEmissionRate(float rateMs);
|
||||
void setActive(bool active);
|
||||
public:
|
||||
SparkEmitter();
|
||||
SparkEmitter(const std::vector<Vector3f>& positions, float rate = 100.0f);
|
||||
|
||||
void update(float deltaTimeMs);
|
||||
void emit();
|
||||
void setEmissionPoints(const std::vector<Vector3f>& positions);
|
||||
|
||||
const std::vector<SparkParticle>& getParticles() const;
|
||||
size_t getActiveParticleCount() const;
|
||||
void update(float deltaTimeMs);
|
||||
void emit();
|
||||
|
||||
private:
|
||||
void initParticle(SparkParticle& particle, int emitterIndex);
|
||||
Vector3f getRandomVelocity(int emitterIndex);
|
||||
};
|
||||
const std::vector<SparkParticle>& getParticles() const;
|
||||
size_t getActiveParticleCount() const;
|
||||
|
||||
const float* getPositionBuffer() const { return positionBuffer.data(); }
|
||||
const float* getTexCoordBuffer() const { return texCoordBuffer.data(); }
|
||||
size_t getBufferSize() const { return positionBuffer.size() / 4; }
|
||||
void updateBuffers();
|
||||
|
||||
size_t getActiveParticleBufferIndex(size_t particleIndex) const;
|
||||
|
||||
private:
|
||||
void initParticle(SparkParticle& particle, int emitterIndex);
|
||||
Vector3f getRandomVelocity(int emitterIndex);
|
||||
};
|
||||
|
||||
} // namespace ZL
|
||||
Loading…
Reference in New Issue
Block a user