Browse Source

Playing with lens flare

Panagiotis Christopoulos Charitos 12 years ago
parent
commit
ad132e99d0
4 changed files with 29 additions and 12 deletions
  1. 5 1
      shaders/PpsLfPass.glsl
  2. 7 3
      src/renderer/Lf.cpp
  3. 1 1
      testapp/Main.cpp
  4. 16 7
      tools/texture/ankitexture.py

+ 5 - 1
shaders/PpsLfPass.glsl

@@ -18,6 +18,7 @@ layout(std140) uniform flaresBlock
 layout(location = 0) in vec2 position;
 layout(location = 0) in vec2 position;
 
 
 out vec3 vTexCoords;
 out vec3 vTexCoords;
+out flat float vAlpha;
 
 
 void main()
 void main()
 {
 {
@@ -27,6 +28,8 @@ void main()
 
 
 	vec4 posScale = flare.posScale;
 	vec4 posScale = flare.posScale;
 	gl_Position = vec4(position * posScale.zw + posScale.xy , 0.0, 1.0);
 	gl_Position = vec4(position * posScale.zw + posScale.xy , 0.0, 1.0);
+
+	vAlpha = flare.alphaDepth.x;
 }
 }
 
 
 #pragma anki start fragmentShader
 #pragma anki start fragmentShader
@@ -34,10 +37,11 @@ void main()
 uniform sampler2DArray images;
 uniform sampler2DArray images;
 
 
 in vec3 vTexCoords;
 in vec3 vTexCoords;
+in flat float vAlpha;
 
 
 out vec3 fColor;
 out vec3 fColor;
 
 
 void main()
 void main()
 {
 {
-	fColor = texture(images, vTexCoords).rgb;
+	fColor = texture(images, vTexCoords).rgb * vAlpha;
 }
 }

+ 7 - 3
src/renderer/Lf.cpp

@@ -188,15 +188,19 @@ void Lf::run()
 			// Write the "flares"
 			// Write the "flares"
 			F32 factor = d / ((F32)depth - 1.0);
 			F32 factor = d / ((F32)depth - 1.0);
 
 
+			F32 flen = len * 2.0 * factor;
+
 			flares[flaresCount].pos = 
 			flares[flaresCount].pos = 
-				posNdc + dir * (len * len * 2.0 * factor);
+				posNdc + dir * flen;
 
 
 			flares[flaresCount].scale = 
 			flares[flaresCount].scale = 
-				Vec2(0.1 * 3.0, r->getAspectRatio() * 0.1) 
-				* (factor * len + 1.0);
+				Vec2(0.1, r->getAspectRatio() * 0.1) 
+				* (flen);
 
 
 			flares[flaresCount].depth = d;
 			flares[flaresCount].depth = d;
 
 
+			flares[flaresCount].alpha = flen / 2.0 + 0.1;
+
 			// Advance
 			// Advance
 			++flaresCount;
 			++flaresCount;
 			++groups[groupsCount - 1];
 			++groups[groupsCount - 1];

+ 1 - 1
testapp/Main.cpp

@@ -201,7 +201,7 @@ void init()
 		PointLight* point =
 		PointLight* point =
 			new PointLight(("vase_plight" + std::to_string(i)).c_str(),
 			new PointLight(("vase_plight" + std::to_string(i)).c_str(),
 			&scene, nullptr, Movable::MF_NONE, 
 			&scene, nullptr, Movable::MF_NONE, 
-			(i == 0) ? "data/textures/lens_flare/flares0.ankitex" : nullptr);
+			(i == 0) ? "data/textures/lens_flare/flares1.ankitex" : nullptr);
 		point->setRadius(2.0);
 		point->setRadius(2.0);
 		point->setLocalOrigin(lightPos);
 		point->setLocalOrigin(lightPos);
 		point->setDiffuseColor(Vec4(3.0, 0.2, 0.0, 0.0));
 		point->setDiffuseColor(Vec4(3.0, 0.2, 0.0, 0.0));

+ 16 - 7
tools/texture/ankitexture.py

@@ -175,9 +175,13 @@ def parse_commandline():
 
 
 	parser.add_option("-c", "--convert-path", dest = "convert_path", 
 	parser.add_option("-c", "--convert-path", dest = "convert_path", 
 			type = "string", default = "/usr/bin/convert", 
 			type = "string", default = "/usr/bin/convert", 
-			help = "The directory where convert tool is " \
+			help = "the directory where convert tool is " \
 			"located. Stupid etcpack cannot get it from PATH")
 			"located. Stupid etcpack cannot get it from PATH")
 
 
+	parser.add_option("--no-alpha", dest = "no_alpha", 
+			action = "store_true", default = False, 
+			help = "remove alpha channel")
+
 	(options, args) = parser.parse_args()
 	(options, args) = parser.parse_args()
 
 
 	if not options.inp or not options.out or not options.convert_path:
 	if not options.inp or not options.out or not options.convert_path:
@@ -195,7 +199,7 @@ def parse_commandline():
 		parser.error("Unrecognized type: " + options.type)
 		parser.error("Unrecognized type: " + options.type)
 
 
 	return (options.inp.split(":"), options.out, options.fast,
 	return (options.inp.split(":"), options.out, options.fast,
-			typ, options.normal, options.convert_path)
+			typ, options.normal, options.convert_path, options.no_alpha)
 
 
 def identify_image(in_file):
 def identify_image(in_file):
 	""" Return the size of the input image and the internal format """
 	""" Return the size of the input image and the internal format """
@@ -221,10 +225,10 @@ def identify_image(in_file):
 		raise Exception("Cannot extract size")
 		raise Exception("Cannot extract size")
 	
 	
 	# Identify the color space
 	# Identify the color space
-	if not re.search(r"red: 8-bit", stdout_str) \
+	"""if not re.search(r"red: 8-bit", stdout_str) \
 			or not re.search(r"green: 8-bit", stdout_str) \
 			or not re.search(r"green: 8-bit", stdout_str) \
 			or not re.search(r"blue: 8-bit", stdout_str):
 			or not re.search(r"blue: 8-bit", stdout_str):
-		raise Exception("Incorrect channel depths")
+		raise Exception("Incorrect channel depths")"""
 
 
 	if re.search(r"alpha: 8-bit", stdout_str):
 	if re.search(r"alpha: 8-bit", stdout_str):
 		color_format = CF_RGBA8
 		color_format = CF_RGBA8
@@ -446,7 +450,7 @@ def write_etc(out_file, fname, width, height, color_format):
 	data = in_file.read(data_size)
 	data = in_file.read(data_size)
 	out_file.write(data)
 	out_file.write(data)
 
 
-def convert(in_files, out, fast, typ, normal, tmp_dir, convert_path):
+def convert(in_files, out, fast, typ, normal, tmp_dir, convert_path, no_alpha):
 	""" This is the function that does all the work """
 	""" This is the function that does all the work """
 
 
 	# Invoke app named "identify" to get internal format and width and height
 	# Invoke app named "identify" to get internal format and width and height
@@ -466,6 +470,9 @@ def convert(in_files, out, fast, typ, normal, tmp_dir, convert_path):
 				or color_format != color_format_2:
 				or color_format != color_format_2:
 			raise Exception("Images are not same size and color space")
 			raise Exception("Images are not same size and color space")
 
 
+	if no_alpha:
+		color_format = CF_RGB8
+
 	# Create images
 	# Create images
 	for in_file in in_files:
 	for in_file in in_files:
 		mips_fnames = create_mipmaps(in_file, tmp_dir, width, height, \
 		mips_fnames = create_mipmaps(in_file, tmp_dir, width, height, \
@@ -545,7 +552,8 @@ def main():
 	""" The main """
 	""" The main """
 
 
 	# Parse cmd line args
 	# Parse cmd line args
-	(in_files, out, fast, typ, normal, convert_path) = parse_commandline();
+	(in_files, out, fast, typ, normal, convert_path, \
+			no_alpha) = parse_commandline();
 
 
 	if typ == TT_CUBE and len(in_files) != 6:
 	if typ == TT_CUBE and len(in_files) != 6:
 		raise Exception("Not enough images for cube generation")
 		raise Exception("Not enough images for cube generation")
@@ -564,7 +572,8 @@ def main():
 
 
 	# Do the work
 	# Do the work
 	try:
 	try:
-		convert(in_files, out, fast, typ, normal, tmp_dir, convert_path)
+		convert(in_files, out, fast, typ, normal, tmp_dir, \
+				convert_path, no_alpha)
 	finally:
 	finally:
 		shutil.rmtree(tmp_dir)
 		shutil.rmtree(tmp_dir)