<?xml version="1.0"?>
<rss version="2.0">
	<channel>
		<title>&quot;TV Mode&quot; Filter Algorithm?</title>
		<link>http://www.allegro.cc/forums/view/434397</link>
		<description>Allegro.cc Forum Thread</description>
		<webMaster>matthew@allegro.cc (Matthew Leverton)</webMaster>
		<lastBuildDate>Sun, 05 Dec 2004 11:29:11 +0000</lastBuildDate>
	</channel>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>So, I&#39;ve been working on an OpenGL game engine in C, and I&#39;d like to implement some display filters (currently scanlines and &quot;tv mode,&quot; as seen in many emulators).</p><p>Scanlines are easy, however, the whole &quot;tv mode&quot; filter I&#39;m puzzled about.  I&#39;ve seen it done in numerous emulators (snes9x, Gens, Kega Fusion, etc.), and it produces a VERY nice TV-esque effect, especially when combined with scanlines.  It seems like some sort of interpolation, but I can&#39;t even think of how to do it.</p><p>Snes9x <u>is</u> open-source.  I&#39;ve even found the code for the tv mode filter, but it&#39;s undocumented, and I can&#39;t make any sense out of it.  There are numerous emulators that use SDL (on some page called SDLEMU or something), and they all appear to use the same graphics code, because they all have similar filters.  Unfortunately, these are all closed-source (lame).</p><p>So, do any of you know how this tv filter is achieved, or have any idea how the function in snes9x&#39;s code works?  I&#39;d really love to have this filter, as I&#39;m aiming to write an old-school rpg engine, and having a tv filter would make it even more old-school <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>I&#39;m just looking for a general algorithm, like:</p><p>Scanlines X%: Scale the screen by a factor of two.  Darken every other horizontal line by X%.</p><p>The snes9x source file in question is attached... oddly enough, it uses Allegro (though hardly it seems).</p><p>The entire source can be found here: <a href="http://www.zophar.net/Files/snes9x-1.42-src.tar.gz">http://www.zophar.net/Files/snes9x-1.42-src.tar.gz</a></p><p>Thanks!
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DmAndrew12)</author>
		<pubDate>Sun, 28 Nov 2004 12:13:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>FYI, if you need to take another look at an emulator, <a href="http://sourceforge.net/projects/zsnes/">ZSNES</a> is also open-source.</p><p>Not only that, I imagine you could compile it for Linux considering it is written in SDL, IIRC. <img src="http://www.allegro.cc/forums/smileys/grin.gif" alt=";D" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Carrus85)</author>
		<pubDate>Sun, 28 Nov 2004 12:21:39 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Dm: I&#39;ve not looked at the code, but I would suggest somekind of blur filter would help the effect. <img src="http://www.allegro.cc/forums/smileys/smiley.gif" alt=":)" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Richard Phipps)</author>
		<pubDate>Sun, 28 Nov 2004 17:31:16 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Unfortunately, ZSNES&#39;s drawing code is in x86 assembly!  I only know MIPS assembly at the moment <img src="http://www.allegro.cc/forums/smileys/cry.gif" alt=":&#39;(" />
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DmAndrew12)</author>
		<pubDate>Sun, 28 Nov 2004 22:41:42 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Scanlines and TV Mode are nearly identical. TV Mode just uses a lighter colored pixel to draw the lines. This mode is mainly used to reduce the bleeding effect that red has on a television. This could be achieved using Allegro by creating a buffer the size of your screen, using hline to place the lines where they are needed. You could then call it like:</p><div class="source-code snippet"><div class="inner"><pre><a href="http://www.allegro.cc/manual/draw_trans_sprite" target="_blank"><span class="a">draw_trans_sprite</span></a><span class="k2">(</span>buffer, <a href="http://www.allegro.cc/manual/screen" target="_blank"><span class="a">screen</span></a>, <span class="n">0</span>,<span class="n">0</span><span class="k2">)</span><span class="k2">;</span>
</pre></div></div><p>

Making sure you setup the set_trans_blender function before set_gfx_mode.</p><p>This will overlay the partially transparent bitmap of hlines on top of the screen essentially darkening it to eliminate bleeding.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Jeremy McCleese)</author>
		<pubDate>Mon, 29 Nov 2004 02:42:52 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I&#39;ve looked at screenshots, and there seems to be more to it than that :-(</p><p>I&#39;ve attached some examples of various tv mode effects by different emulators.  Sorry for the large filesize, but the pixels and colors are crucial.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DmAndrew12)</author>
		<pubDate>Mon, 29 Nov 2004 09:40:27 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It&#39;s probably some kind of interlacing effect. Like, for every frame, it copies either the even or odd rows to the screen, discarding the rest for that frame.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Kitty Cat)</author>
		<pubDate>Mon, 29 Nov 2004 09:48:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Not really what you need but I made a kind of a TV mode in my Paranoid game (link in my sig). Press &#39;m&#39; in the game to change the mode from the default green scanlines to TV (it&#39;s black and white but that&#39;s only because all the gfx are like that). As I said, it&#39;s not really what you need because it achieves the effect by expanding every pixel into a 3x3 square (3 vertical lines, 1 red, 1 green, 1 blue) so the game&#39;s resolution is actually 3 times smaller then the gfx_mode. But it looks kind of cool...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Mon, 29 Nov 2004 12:39:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Looks to me as if they&#39;re doing a 2x resize with interpolated pixels in between (sorta anti-aliasing). The Kega seems to do it in both dimensions, while the SNes9x only does it horizontally.<br />Basically, the algorithm would then work as follows:<br />- fill every pixel at even x values with the source color at (x/2, y/2).<br />- fill every pixel at uneven x values with (src(x/2, y/2) + src(x/2+1,y/2)) / 2
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Tobias Dammers)</author>
		<pubDate>Mon, 29 Nov 2004 22:18:59 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Here&#39;s my code if you want to check it out. This is the unoptimized easy to understand version (code comments explain what you can do to optimize it). There are actually 6 different implementations of what is basically the same algorithm, choose whatever looks best if you want...</p><p>Btw, despite the .cpp extension of the source code file, the actual stretch filter function is written in plain C...
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (miran)</author>
		<pubDate>Tue, 30 Nov 2004 00:43:58 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>So, I&#39;ve been working on an OpenGL game engine in C</p></div></div><p>
This kind of full-screen effect will be inefficient if you try to implement it with OpenGL the way that you would implement it with Allegro. You might be better off trying to find a solution that involves overlaying a translucent texture to the screen--say, one that looks interlaced--after the scene is rendered. You can also enable multisampling, which will give you full screen antialiasing if you need that to achieve the desired effect.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goodbytes)</author>
		<pubDate>Tue, 30 Nov 2004 02:22:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>I was thinking of rendering the scene to a surface, and then doing the effects to that surface, then blitting that surface to the screen.  First, I have to figure out how to render to a texture. <img src="http://www.allegro.cc/forums/smileys/wink.gif" alt=";)" /></p><p>As far as the scanlines go, I&#39;m thinking of rendering the scene to a surface, then setting the alpha of every other line to the desired %.  Then I&#39;ll blit onto a black background.</p><p>I could just overlay a semi-transparent lined textured, but that won&#39;t give me an accurate effect <img src="http://www.allegro.cc/forums/smileys/undecided.gif" alt=":-/" /></p><p>Any other ideas?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DmAndrew12)</author>
		<pubDate>Tue, 30 Nov 2004 03:02:08 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>then setting the alpha of every other line to the desired %. Then I&#39;ll blit onto a black background.</p></div></div><p>
Trust me, you can do the exact same thing by overlaying the screen with a texture. Use an 8 bit grayscale texture of, say, 32x32 size with texture repeating enabled and appropriate texcoords so that the texels correspond 1:1 with pixels, with white areas as 100% alpha, and black as 0%. Render the scene normally, then overlay the texture with blend mode (GL_ZERO, GL_SRC_COLOR). It will multiply all the scene pixels with the corresponding  white level, just like setting every other line to a certain alpha will, except you don&#39;t need to use your alpha channel and it will be much more efficient. Why wouldn&#39;t this give you an accurate effect?
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goodbytes)</author>
		<pubDate>Tue, 30 Nov 2004 11:11:31 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Quote:</div><div class="quote"><p>Trust me, you can do the exact same thing by overlaying the screen with a texture. Use an 8 bit grayscale texture of, say, 32x32 size with texture repeating enabled and appropriate texcoords so that the texels correspond 1:1 with pixels, with white areas as 100% alpha, and black as 0%. Render the scene normally, then overlay the texture with blend mode (GL_ZERO, GL_SRC_COLOR). It will multiply all the scene pixels with the corresponding white level, just like setting every other line to a certain alpha will, except you don&#39;t need to use your alpha channel and it will be much more efficient. Why wouldn&#39;t this give you an accurate effect?
</p></div></div><p>
Why a texture? Just draw untextured horizontal lines with the right alpha.  You&#39;ll use a lot less bandwidth and negligeably more vertex processing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Bob)</author>
		<pubDate>Tue, 30 Nov 2004 13:38:20 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>It seems to me they used something like 2xSai and then overlayed scanlines ontop of that.  Tell you the truth, not very impressive.  I was hoping for something that would do the cool <span class="cuss"><span>ass</span></span> TV effects from Silent Hill 3.  Like moving scanlines, flickering images, that sort of thing.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Mandrake Root Produc)</author>
		<pubDate>Tue, 30 Nov 2004 19:22:28 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><div class="quote_container"><div class="title">Bob said:</div><div class="quote"><p>Why a texture? Just draw untextured horizontal lines with the right alpha. You&#39;ll use a lot less bandwidth and negligeably more vertex processing.</p></div></div><p>

Right... If you&#39;re just doing plain horizontal lines, which you are, listen to Bob. I&#39;m a little stuck in texture mode because I use a texture overlay in a project I&#39;m working on to simulate an oil painting. For more complicated effects like that, I would suggest a texture overlay.
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (Goodbytes)</author>
		<pubDate>Tue, 30 Nov 2004 23:06:37 +0000</pubDate>
	</item>
	<item>
		<description><![CDATA[<div class="mockup v2"><p>Thanks guys
</p></div>]]>
		</description>
		<author>no-reply@allegro.cc (DmAndrew12)</author>
		<pubDate>Sun, 05 Dec 2004 11:29:11 +0000</pubDate>
	</item>
</rss>
