Browse Source

Fixing particles & adding some packing functions

Panagiotis Christopoulos Charitos 10 years ago
parent
commit
27efe0c6ac
3 changed files with 53 additions and 4 deletions
  1. 44 1
      shaders/Pack.glsl
  2. 8 2
      src/scene/ParticleEmitter.cpp
  3. 1 1
      testapp/Main.cpp

+ 44 - 1
shaders/Pack.glsl

@@ -3,6 +3,9 @@
 // Code licensed under the BSD License.
 // http://www.anki3d.org/LICENSE
 
+#ifndef ANKI_SHADERS_PACK_GLSL
+#define ANKI_SHADERS_PACK_GLSL
+
 /// Pack 3D normal to 2D vector
 /// See the clean code in comments in revision < r467
 vec2 packNormal(in vec3 normal)
@@ -45,6 +48,44 @@ vec4 unpackUnorm4x8(in highp uint u)
 }
 #endif
 
+// Convert from RGB to YCbCr.
+// The RGB should be in [0, 1] and the output YCbCr will be in [0, 1] as well.
+vec3 rgbToYCbCr(in vec3 rgb)
+{
+    float y = dot(rgb, vec3(0.299, 0.587, 0.114));
+	float cb = 0.5 + dot(rgb, vec3(-0.168736, -0.331264, 0.5));
+	float cr = 0.5 + dot(rgb, vec3(0.5, -0.418688, -0.081312));
+    return vec3(y, cb, cr);
+}
+
+// Convert the output of rgbToYCbCr back to RGB.
+vec3 yCbCrToRgb(in vec3 ycbcr)
+{
+	float cb = ycbcr.y - 0.5;
+	float cr = ycbcr.z - 0.5;
+	float y = ycbcr.x;
+	float r = 1.402 * cr;
+	float g = -0.344 * cb - 0.714 * cr;
+	float b = 1.772 * cb;
+	return vec3(r, g, b) + y;
+}
+
+// Pack a vec2 to a single float.
+// comp should be in [0, 1] and the output will be in [0, 1].
+float packUnorm2ToUnorm1(in vec2 comp)
+{
+	return dot(round(comp * 15.0), vec2(1.0 / (255.0 / 16.0), 1.0 / 255.0));
+}
+
+// Unpack a single float to vec2. Does the oposite of packUnorm2ToUnorm1.
+vec2 unpackUnorm1ToUnorm2(in float c)
+{
+	float temp = c * (255.0 / 16.0);
+	float a = floor(temp);
+	float b = fract(temp);
+	return vec2(a, b) * vec2(1.0 / 15.0, 16.0 / 15.0);
+}
+
 // Populate the G buffer
 void writeGBuffer(
 	in vec3 diffColor, in vec3 normal, in float specColor, in float specPower,
@@ -59,7 +100,7 @@ void writeGBuffer(
 void readGBuffer(
 	in sampler2D fai0, in sampler2D fai1,
 	in vec2 texCoord,
-	out vec3 diffColor, out vec3 normal, out float specColor, 
+	out vec3 diffColor, out vec3 normal, out float specColor,
 	out float specPower)
 {
 	vec4 comp = textureRt(fai0, texCoord);
@@ -91,3 +132,5 @@ void readNormalSpecularColorFromGBuffer(
 	normal = normalize(comp.xyz * 2.0 - 1.0);
 	specColor = comp.a;
 }
+
+#endif

+ 8 - 2
src/scene/ParticleEmitter.cpp

@@ -297,6 +297,10 @@ Error ParticleEmitter::create(
 	comp = getSceneAllocator().newInstance<MoveComponent>(this);
 	addComponent(comp, true);
 
+	// Move component feedback
+	comp = getSceneAllocator().newInstance<MoveFeedbackComponent>(this);
+	addComponent(comp, true);
+
 	// Spatial component
 	comp = getSceneAllocator().newInstance<SpatialComponent>(this, &m_obb);
 	addComponent(comp, true);
@@ -394,6 +398,10 @@ void ParticleEmitter::onMoveComponentUpdate(MoveComponent& move)
 {
 	m_identityRotation =
 		move.getWorldTransform().getRotation() == Mat3x4::getIdentity();
+
+	SpatialComponent& sp = getComponent<SpatialComponent>();
+	sp.setSpatialOrigin(move.getWorldTransform().getOrigin());
+	sp.markForUpdate();
 }
 
 //==============================================================================
@@ -707,8 +715,6 @@ Error ParticleEmitter::doInstancingCalcs()
 //==============================================================================
 void ParticleEmitter::getRenderWorldTransform(U index, Transform& trf)
 {
-	ANKI_ASSERT(m_transforms.getSize() > 0);
-
 	if(index == 0)
 	{
 		// Don't transform the particle positions. They are already in world

+ 1 - 1
testapp/Main.cpp

@@ -41,7 +41,7 @@ App* app;
 ModelNode* horse;
 PerspectiveCamera* cam;
 
-#define NO_PLAYER 0
+#define NO_PLAYER 1
 
 
 //==============================================================================