libgui/source/gui/GuiParticleImage.cpp

125 lines
5.4 KiB
C++
Raw Normal View History

2017-10-29 10:28:14 +01:00
/****************************************************************************
* Copyright (C) 2015 Dimok
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/
2022-02-05 14:28:08 +01:00
#include "utils/utils.h"
2018-06-21 20:44:58 +02:00
#include <gui/GuiParticleImage.h>
2019-08-14 23:24:55 +02:00
#include <gui/video/CVideo.h>
#include <gui/video/shaders/ColorShader.h>
2017-10-29 10:28:14 +01:00
2022-02-05 14:28:08 +01:00
#define CIRCLE_VERTEX_COUNT 36
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
static inline float getRandZeroToOneF32() {
2017-10-29 10:28:14 +01:00
return (rand() % 10000) * 0.0001f;
}
2018-06-21 20:44:58 +02:00
static inline float getRandMinusOneToOneF32() {
2017-10-29 10:28:14 +01:00
return getRandZeroToOneF32() * 2.0f - 1.0f;
}
2018-06-21 20:44:58 +02:00
GuiParticleImage::GuiParticleImage(int32_t w, int32_t h, uint32_t particleCount, float minRadius, float maxRadius, float minSpeed, float maxSpeed)
2022-02-05 14:28:08 +01:00
: GuiImage(NULL) {
width = w;
height = h;
imgType = IMAGE_COLOR;
2018-06-21 20:44:58 +02:00
this->minRadius = minRadius;
this->maxRadius = maxRadius;
2022-02-05 14:28:08 +01:00
this->minSpeed = minSpeed;
this->maxSpeed = maxSpeed;
2017-10-29 10:28:14 +01:00
2022-02-05 14:28:08 +01:00
posVertexs = (float *) memalign(GX2_VERTEX_BUFFER_ALIGNMENT, ColorShader::cuVertexAttrSize * CIRCLE_VERTEX_COUNT);
2018-06-21 20:44:58 +02:00
colorVertexs = (uint8_t *) memalign(GX2_VERTEX_BUFFER_ALIGNMENT, ColorShader::cuColorAttrSize * CIRCLE_VERTEX_COUNT);
2017-10-29 10:28:14 +01:00
2020-08-13 12:38:07 +02:00
for (uint32_t i = 0; i < CIRCLE_VERTEX_COUNT; i++) {
2017-10-29 10:28:14 +01:00
posVertexs[i * 3 + 0] = cosf(DegToRad(i * 360.0f / CIRCLE_VERTEX_COUNT));
posVertexs[i * 3 + 1] = sinf(DegToRad(i * 360.0f / CIRCLE_VERTEX_COUNT));
posVertexs[i * 3 + 2] = 0.0f;
colorVertexs[i * 4 + 0] = 0xff;
colorVertexs[i * 4 + 1] = 0xff;
colorVertexs[i * 4 + 2] = 0xff;
colorVertexs[i * 4 + 3] = 0xff;
}
2018-06-21 20:44:58 +02:00
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_ATTRIBUTE_BUFFER, posVertexs, ColorShader::cuVertexAttrSize * CIRCLE_VERTEX_COUNT);
GX2Invalidate(GX2_INVALIDATE_MODE_CPU_ATTRIBUTE_BUFFER, colorVertexs, ColorShader::cuColorAttrSize * CIRCLE_VERTEX_COUNT);
2017-10-29 10:28:14 +01:00
particles.resize(particleCount);
2020-08-13 12:38:07 +02:00
for (uint32_t i = 0; i < particleCount; i++) {
2017-10-29 10:28:14 +01:00
particles[i].position.x = getRandMinusOneToOneF32() * getWidth() * 0.5f;
particles[i].position.y = getRandMinusOneToOneF32() * getHeight() * 0.5f;
particles[i].position.z = 0.0f;
2022-02-05 14:28:08 +01:00
particles[i].colors = glm::vec4(1.0f, 1.0f, 1.0f, (getRandZeroToOneF32() * 0.6f) + 0.05f);
particles[i].radius = getRandZeroToOneF32() * (maxRadius - minRadius) + minRadius;
particles[i].speed = (getRandZeroToOneF32() * (maxSpeed - minSpeed)) + minSpeed;
particles[i].direction = getRandMinusOneToOneF32();
2017-10-29 10:28:14 +01:00
}
}
2018-06-21 20:44:58 +02:00
GuiParticleImage::~GuiParticleImage() {
2017-10-29 10:28:14 +01:00
free(posVertexs);
free(colorVertexs);
}
2018-06-21 20:44:58 +02:00
void GuiParticleImage::draw(CVideo *pVideo) {
2020-08-13 12:58:19 +02:00
if (!this->isVisible()) {
2018-06-21 20:44:58 +02:00
return;
2020-08-13 12:58:19 +02:00
}
2017-10-29 10:28:14 +01:00
2018-06-21 20:44:58 +02:00
float currScaleX = getScaleX();
float currScaleY = getScaleY();
2017-10-29 10:28:14 +01:00
positionOffsets[2] = getDepth() * pVideo->getDepthScaleFactor() * 2.0f;
scaleFactor[2] = getScaleZ();
//! add other colors intensities parameters
colorIntensity[3] = getAlpha();
2020-08-13 12:38:07 +02:00
for (uint32_t i = 0; i < particles.size(); ++i) {
if (particles[i].position.y > (getHeight() * 0.5f + 30.0f)) {
2017-10-29 10:28:14 +01:00
particles[i].position.x = getRandMinusOneToOneF32() * getWidth() * 0.5f;
particles[i].position.y = -getHeight() * 0.5f - 30.0f;
2022-02-05 14:28:08 +01:00
particles[i].colors = glm::vec4(1.0f, 1.0f, 1.0f, (getRandZeroToOneF32() * 0.6f) + 0.05f);
particles[i].radius = getRandZeroToOneF32() * (maxRadius - minRadius) + minRadius;
particles[i].speed = (getRandZeroToOneF32() * (maxSpeed - minSpeed)) + minSpeed;
particles[i].direction = getRandMinusOneToOneF32();
2017-10-29 10:28:14 +01:00
}
2020-08-13 12:38:07 +02:00
if (particles[i].position.x < (-getWidth() * 0.5f - 50.0f)) {
2017-10-29 10:28:14 +01:00
particles[i].position.x = -particles[i].position.x;
}
particles[i].direction += getRandMinusOneToOneF32() * 0.03f;
particles[i].position.x += particles[i].speed * particles[i].direction;
particles[i].position.y += particles[i].speed;
positionOffsets[0] = (getCenterX() + particles[i].position.x) * pVideo->getWidthScaleFactor() * 2.0f;
positionOffsets[1] = (getCenterY() + particles[i].position.y) * pVideo->getHeightScaleFactor() * 2.0f;
scaleFactor[0] = currScaleX * particles[i].radius * pVideo->getWidthScaleFactor();
scaleFactor[1] = currScaleY * particles[i].radius * pVideo->getHeightScaleFactor();
ColorShader::instance()->setShaders();
ColorShader::instance()->setAttributeBuffer(colorVertexs, posVertexs, CIRCLE_VERTEX_COUNT);
ColorShader::instance()->setAngle(0.0f);
ColorShader::instance()->setOffset(positionOffsets);
ColorShader::instance()->setScale(scaleFactor);
ColorShader::instance()->setColorIntensity(colorIntensity * particles[i].colors);
2018-06-21 20:44:58 +02:00
ColorShader::instance()->draw(GX2_PRIMITIVE_MODE_TRIANGLE_FAN, CIRCLE_VERTEX_COUNT);
2017-10-29 10:28:14 +01:00
}
}