Fix some issues found in first-try

This commit is contained in:
Benau 2017-10-14 09:47:13 +08:00
parent 0066722dab
commit 0b9a3e1c69
7 changed files with 41 additions and 92 deletions

View File

@ -11,7 +11,6 @@ layout(location = 2) in float size;
layout(location = 3) in vec2 Texcoord;
layout(location = 4) in vec2 quadcorner;
layout(location = 5) in vec3 rotationvec;
layout(location = 6) in float anglespeed;
#else
@ -22,7 +21,6 @@ in float size;
in vec2 Texcoord;
in vec2 quadcorner;
in vec3 rotationvec;
float anglespeed;
#endif
@ -33,6 +31,7 @@ void main(void)
{
if (size == 0.0)
{
gl_Position = vec4(0.);
return;
}
tc = Texcoord;
@ -43,41 +42,13 @@ void main(void)
vec4 viewpos = vec4(0.);
if (flips == 1)
{
// from http://jeux.developpez.com/faq/math
float angle = lifetime * anglespeed;
float sin_a = sin(angle / 2.);
float cos_a = cos(angle / 2.);
vec4 quaternion = normalize(vec4(rotationvec * sin_a, cos_a));
float xx = quaternion.x * quaternion.x;
float xy = quaternion.x * quaternion.y;
float xz = quaternion.x * quaternion.z;
float xw = quaternion.x * quaternion.w;
float yy = quaternion.y * quaternion.y;
float yz = quaternion.y * quaternion.z;
float yw = quaternion.y * quaternion.w;
float zz = quaternion.z * quaternion.z;
float zw = quaternion.z * quaternion.w;
vec4 col1 = vec4(
1. - 2. * ( yy + zz ),
2. * ( xy + zw ),
2. * ( xz - yw ),
0.);
vec4 col2 = vec4(
2. * ( xy - zw ),
1. - 2. * ( xx + zz ),
2. * ( yz + xw ),
0.);
vec4 col3 = vec4(
2. * ( xz + yw ),
2. * ( yz - xw ),
1. - 2. * ( xx + yy ),
0.);
vec4 col4 = vec4(0., 0., 0., 1.);
mat4 rotationMatrix = mat4(col1, col2, col3, col4);
float sin_a = sin(mod(angle / 2.0, 6.283185307179586));
float cos_a = cos(mod(angle / 2.0, 6.283185307179586));
vec4 quat = normalize(vec4(vec3(0.0, 1.0, 0.0) * sin_a, cos_a));
vec3 newquadcorner = size * vec3(quadcorner, 0.);
newquadcorner = (rotationMatrix * vec4(newquadcorner, 0.)).xyz;
newquadcorner = newquadcorner + 2.0 * cross(cross(newquadcorner,
quat.xyz) + quat.w * newquadcorner, quat.xyz);
viewpos = ViewMatrix * vec4(Position + newquadcorner, 1.0);
}
else

View File

@ -11,7 +11,6 @@ layout(location = 2) in float size;
layout(location = 3) in vec2 Texcoord;
layout(location = 4) in vec2 quadcorner;
layout(location = 5) in vec3 rotationvec;
layout(location = 6) in float anglespeed;
#else
@ -22,11 +21,9 @@ in float size;
in vec2 Texcoord;
in vec2 quadcorner;
in vec3 rotationvec;
float anglespeed;
#endif
out float lf;
out vec2 tc;
out vec4 pc;
@ -34,52 +31,25 @@ void main(void)
{
if (size == 0.0)
{
gl_Position = vec4(0.);
return;
}
tc = Texcoord;
lf = lifetime;
pc = vec4(vec3(color_from + (color_to - color_from) * lf), 1.0) * smoothstep(1., 0.8, lf);
pc = vec4(vec3(color_from + (color_to - color_from) * lifetime), 1.0) *
smoothstep(1., 0.8, lifetime);
#if !defined(sRGB_Framebuffer_Usable) && !defined(Advanced_Lighting_Enabled)
pc.rgb = pow(pc.rgb, vec3(1. / 2.2));
#endif
vec4 viewpos = vec4(0.);
if (flips == 1)
{
// from http://jeux.developpez.com/faq/math
float angle = lifetime * anglespeed;
float sin_a = sin(angle / 2.);
float cos_a = cos(angle / 2.);
vec4 quaternion = normalize(vec4(rotationvec * sin_a, cos_a));
float xx = quaternion.x * quaternion.x;
float xy = quaternion.x * quaternion.y;
float xz = quaternion.x * quaternion.z;
float xw = quaternion.x * quaternion.w;
float yy = quaternion.y * quaternion.y;
float yz = quaternion.y * quaternion.z;
float yw = quaternion.y * quaternion.w;
float zz = quaternion.z * quaternion.z;
float zw = quaternion.z * quaternion.w;
vec4 col1 = vec4(
1. - 2. * ( yy + zz ),
2. * ( xy + zw ),
2. * ( xz - yw ),
0.);
vec4 col2 = vec4(
2. * ( xy - zw ),
1. - 2. * ( xx + zz ),
2. * ( yz + xw ),
0.);
vec4 col3 = vec4(
2. * ( xz + yw ),
2. * ( yz - xw ),
1. - 2. * ( xx + yy ),
0.);
vec4 col4 = vec4(0., 0., 0., 1.);
mat4 rotationMatrix = mat4(col1, col2, col3, col4);
float sin_a = sin(mod(angle / 2.0, 6.283185307179586));
float cos_a = cos(mod(angle / 2.0, 6.283185307179586));
vec4 quat = normalize(vec4(vec3(0.0, 1.0, 0.0) * sin_a, cos_a));
vec3 newquadcorner = size * vec3(quadcorner, 0.);
newquadcorner = (rotationMatrix * vec4(newquadcorner, 0.)).xyz;
newquadcorner = newquadcorner + 2.0 * cross(cross(newquadcorner,
quat.xyz) + quat.w * newquadcorner, quat.xyz);
viewpos = ViewMatrix * vec4(Position + newquadcorner, 1.0);
}
else

View File

@ -72,7 +72,6 @@ CPUParticleManager::~CPUParticleManager()
glDeleteVertexArrays(1, &std::get<0>(p.second));
glDeleteBuffers(1, &std::get<1>(p.second));
}
STKParticle::destroyFlipsBuffer();
} // ~CPUParticleManager
// ----------------------------------------------------------------------------
@ -182,11 +181,8 @@ void CPUParticleManager::uploadAll()
if (flips)
{
glBindBuffer(GL_ARRAY_BUFFER, STKParticle::getFlipsBuffer());
glEnableVertexAttribArray(5);
glVertexAttribPointer(5, 3, GL_FLOAT, GL_FALSE, 16, 0);
glVertexAttribDivisorARB(5, 1);
glEnableVertexAttribArray(6);
glVertexAttribPointer(6, 1, GL_FLOAT, GL_FALSE, 16, (void*)12);
glVertexAttribPointer(6, 1, GL_FLOAT, GL_FALSE, 4, 0);
glVertexAttribDivisorARB(6, 1);
}
glBindVertexArray(0);
@ -249,7 +245,8 @@ void CPUParticleManager::drawAll()
if (cur_mat->getShaderType() == Material::SHADERTYPE_ADDITIVE)
{
ParticleRenderer::getInstance()->use();
glDisable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_FALSE);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);
@ -260,7 +257,8 @@ void CPUParticleManager::drawAll()
->getShaderType() == Material::SHADERTYPE_ALPHA_BLEND)
{
ParticleRenderer::getInstance()->use();
glDisable(GL_DEPTH_TEST);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glDepthMask(GL_FALSE);
glDisable(GL_CULL_FACE);
glEnable(GL_BLEND);

View File

@ -607,6 +607,7 @@ DrawCalls::DrawCalls()
DrawCalls::~DrawCalls()
{
CPUParticleManager::kill();
STKParticle::destroyFlipsBuffer();
#if !defined(USE_GLES2)
delete m_solid_cmd_buffer;
delete m_shadow_cmd_buffer;

View File

@ -26,7 +26,7 @@
#include "../../lib/irrlicht/source/Irrlicht/os.h"
// ----------------------------------------------------------------------------
std::vector<Vec3> STKParticle::m_flips_data;
std::vector<float> STKParticle::m_flips_data;
GLuint STKParticle::m_flips_buffer = 0;
// ----------------------------------------------------------------------------
scene::IParticleSystemSceneNode* STKParticle::addParticleNode(
@ -353,11 +353,14 @@ void STKParticle::stimulateHeightMap(float dt, unsigned int active_count,
if (m_flips || new_size != 0.0f)
{
if (new_size != 0.0f)
{
Buffer->BoundingBox.addInternalPoint(new_particle_position);
}
if (out != NULL)
{
out->emplace_back(new_particle_position, new_lifetime,
new_size);
}
Buffer->BoundingBox.addInternalPoint(new_particle_position);
}
}
} // stimulateHeightMap
@ -423,7 +426,8 @@ void STKParticle::stimulateNormal(float dt, unsigned int active_count,
// artifacts when the framerate is low, and a more accurate
// formula would need more complex computations.
new_particle_position = updated_position + dt_from_last_frame * updated_direction;
new_particle_position = updated_position + dt_from_last_frame *
updated_direction;
new_particle_direction = updated_direction;
new_lifetime = glslFract(updated_lifetime);
@ -454,11 +458,14 @@ void STKParticle::stimulateNormal(float dt, unsigned int active_count,
if (m_flips || new_size != 0.0f)
{
if (new_size != 0.0f)
{
Buffer->BoundingBox.addInternalPoint(new_particle_position);
}
if (out != NULL)
{
out->emplace_back(new_particle_position, new_lifetime,
new_size);
}
Buffer->BoundingBox.addInternalPoint(new_particle_position);
}
}
} // stimulateNormal
@ -475,13 +482,13 @@ void STKParticle::updateFlips(unsigned maximum_particle_count)
}
updated = true;
// 3 half rotation during lifetime at max
m_flips_data.emplace_back(0.0f, 1.0f, 0.0f,
3.14f * 3.0f * (2.0f * os::Randomizer::frand() - 1.0f));
m_flips_data.push_back(3.14f * 3.0f * (2.0f * os::Randomizer::frand()
- 1.0f));
}
if (updated)
{
glBindBuffer(GL_ARRAY_BUFFER, m_flips_buffer);
glBufferData(GL_ARRAY_BUFFER, m_flips_data.size() * 16,
glBufferData(GL_ARRAY_BUFFER, m_flips_data.size() * 4,
m_flips_data.data(), GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
}

View File

@ -21,7 +21,6 @@
#define HEADER_STK_PARTICLE_HPP
#include "graphics/gl_headers.hpp"
#include "utils/vec3.hpp"
#include "../lib/irrlicht/source/Irrlicht/CParticleSystemSceneNode.h"
#include <vector>
@ -72,7 +71,7 @@ private:
/** Maximum count of particles. */
unsigned m_max_count;
static std::vector<Vec3> m_flips_data;
static std::vector<float> m_flips_data;
static GLuint m_flips_buffer;
@ -151,8 +150,11 @@ public:
// ------------------------------------------------------------------------
static void destroyFlipsBuffer()
{
glDeleteBuffers(1, &m_flips_buffer);
m_flips_buffer = 0;
if (m_flips_buffer != 0)
{
glDeleteBuffers(1, &m_flips_buffer);
m_flips_buffer = 0;
}
}
// ------------------------------------------------------------------------
static GLuint getFlipsBuffer() { return m_flips_buffer; }