<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>BlackHC's Adventures in the Dev World &#187; OpenGL</title>
	<atom:link href="http://blog.blackhc.net/tag/opengl/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.blackhc.net</link>
	<description>Just another weblog</description>
	<lastBuildDate>Wed, 16 Nov 2011 23:12:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>My Bachelor Thesis</title>
		<link>http://blog.blackhc.net/2009/10/my-bachelor-thesis/</link>
		<comments>http://blog.blackhc.net/2009/10/my-bachelor-thesis/#comments</comments>
		<pubDate>Sat, 17 Oct 2009 13:43:19 +0000</pubDate>
		<dc:creator>BlackHC</dc:creator>
				<category><![CDATA[Personal Rantings]]></category>
		<category><![CDATA[University]]></category>
		<category><![CDATA[Bachelor Thesis]]></category>
		<category><![CDATA[Equalizer]]></category>
		<category><![CDATA[Inkscape]]></category>
		<category><![CDATA[LaTeX]]></category>
		<category><![CDATA[Omondo]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[TikZ]]></category>

		<guid isPermaLink="false">http://blog.blackhc.net/?p=661</guid>
		<description><![CDATA[<a href="http://blog.blackhc.net/2009/10/my-bachelor-thesis/" title="My Bachelor Thesis"></a>For the last two months I have been working on my bachelor thesis at the Chair of Computer Graphics and Visualization. It is about "Multi-Tile Terrain Rendering with OGL/Equalizer"&#180;. The chair has a very nice Direct3D 10 terrain rendering engine &#8230;<p class="read-more"><a href="http://blog.blackhc.net/2009/10/my-bachelor-thesis/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://blog.blackhc.net/2009/10/my-bachelor-thesis/" title="My Bachelor Thesis"></a><div id="attachment_664" class="wp-caption alignright" style="width: 310px"><a href="http://blog.blackhc.net/wp-content/uploads/2009/10/frontshot.png"><img class="size-medium wp-image-664 " title="Screenshot of Utah" src="http://blog.blackhc.net/wp-content/uploads/2009/10/frontshot-300x238.png" alt="Screenshot of Utah" width="300" height="238" /></a><p class="wp-caption-text">Screenshot of Utah</p></div>
<p>For the last two months I have been working on my bachelor thesis at the Chair of Computer Graphics and Visualization. It is about "Multi-Tile Terrain Rendering with OGL/Equalizer"<a class="annotation" title="the German title is &quot;Multi-Tile Terrain Rendering mit OGL/Equalizer&quot; :)" href="javascript:;"><strong>&#180;</strong></a>.<br />
The chair has a very nice Direct3D 10 terrain rendering engine and they want to run it at the newly founded KAUST (King Abdullah University of Science and Technology<a class="annotation" title="women are actually allowed to drive on the campus:" href="javascript:;"><strong>&#180;</strong></a>) in a massive CAVE environment. A CAVE is a room whose walls are actually screens.<br />
The CAVE at KAUST even supports stereoscopic rendering. Thus in total 12 views can be rendered to.</p>
<p>My job was to port said terrain engine from Direct3D to OpenGL and afterwards to the Equalizer framework, which is an open-source framework for parallelizing OpenGL applications.</p>
<p>You can find/download an online version of my bachelor thesis <a title="Multi-Tile Terrain Rendering with OGL/Equalizer" href="http://blog.blackhc.net/wp-content/uploads/2009/10/BachelorThesisOnline.pdf" target="_blank">here</a>. I'll upload the LaTeX at a later date and update this post.</p>
<p>I've spent the last months writing about all this, so I don't feel like talking about the thesis itself anymore. Instead the remainder of this post will contain a post-mortem of it.<br />
<span id="more-661"></span></p>
<h3>Post-Mortem</h3>
<h4>Preparations</h4>
<p>I think I did a good job at preparing myself for the thesis: I'm a LaTex noob, so I got lots<a class="annotation" title="4 to be exact" href="javascript:;"><strong>&#180;</strong></a> of LaTeX books; I'm a (paper-)writing noob, so I got lots<a class="annotation" title="3 in this case" href="javascript:;"><strong>&#180;</strong></a> of books about writing papers and writing good English.</p>
<p>The books have helped me a lot and it is always a good decision to get as many books as possible, because the more sources you have, the better.</p>
<p>I got all the LaTeX books from our university library:</p>
<ul>
<li><a href="http://www.amazon.de/gp/product/389842510X/ref=sib_rdr_dp" target="_blank">[Einstieg in] LaTeX</a><br />
A big book (696 pages) but not particularly helpful. The reviewer on Amazon is pretty much right: it contains lots of information., but always stops short of providing the really useful details/information.</li>
<li><a href="http://www.amazon.de/LATEX-GE-PACKT-Karsten-G%C3%BCnther/dp/3826615883/ref=sr_1_6?ie=UTF8&amp;s=books&amp;qid=1255775138&amp;sr=8-6" target="_blank">LaTeX - Ge-packt</a><br />
A small but thick pocket book (608 pages). I totally forgot I had it and it stood on my bookshelf until I found it yesterday. I've used it though while writing another paper last year, and it's pretty neat. It's a nifty reference, that's what it is.</li>
<li><a href="http://www.amazon.de/LaTeX-Hacks-Techniken-professionellen-Textsatz/dp/3897214776/ref=pd_sim_b_5" target="_blank">LaTeX Hacks: Tipps und Techniken für professionellen Textsatz</a><br />
This book is very good. It contains 100 "LaTeX hacks" (on 416 pages). These are small, closed sections describing the solution to a specific problem you might encounter while using LaTeX.<br />
I've used the book a lot of times to look up solutions to my problems and it references many useful LaTeX packages, too---so it's a good start for searches.</li>
<li><a href="http://www.amazon.de/Wissenschaftliche-Arbeiten-schreiben-LaTeX-CD-ROM/dp/3826658922/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1255777607&amp;sr=1-1" target="_blank">Wissenschaftliche Arbeiten schreiben mit LATEX</a><br />
<strong>Get this book</strong> if you want to write your thesis with LaTeX. It's incredibly useful and covers most of what you'll need. It takes you on a journey through all features of LaTeX that you will use and describes the most common use cases. It even has a section describing how to fix common LaTeX errors like overflowed hboxes and others.</li>
</ul>
<p>I've also bought some  books about writing papers and the English language in general:</p>
<ul>
<li><a href="http://www.amazon.com/Manual-Writers-Research-Dissertations-Seventh/dp/0226823377/ref=sr_1_1?ie=UTF8&amp;s=books&amp;qid=1255778175&amp;sr=1-1" target="_blank">A Manual for Writers of Research Papers, Theses, and Dissertations, Seventh Edition: Chicago Style for Students and Researchers</a><br />
This book covers everything you need to know about the style and format of a thesis. It has a huge chapter about how to add citations and what format the bibligraphy should use, etc.<br />
In retrospect reading it was probably overkill, because a bachelor thesis apparently doesn't have very strict requirements (well, I'll see about that when I get my feedback..).<br />
It, however, contains a few nice chapters about editing your text and improving its readability, and the punctuation and spelling chapters are nice, too.</li>
<li><a href="http://www.amazon.co.uk/Oxford-Guide-Effective-Writing-Speaking/dp/0192806130/ref=pd_sim_b_1" target="_blank">Oxford Guide to Effective Writing and Speaking</a><br />
I've only read this book in parts but it's not bad. It contains a useful introduction to English grammar, if you want to refresh that knowledge.</li>
<li><a href="http://www.amazon.co.uk/Oxford-Guide-Plain-English-Martin/dp/0199233454/ref=sr_1_11?ie=UTF8&amp;s=books&amp;qid=1255779088&amp;sr=8-11" target="_blank">Oxford Guide to Plain English</a><br />
This book is simply awesome. Plain English refers to writing English in a simple and easily understood way. This book describes what makes a text 'plain' and easy to read and how to transform your texts to improve readability. It contains lots and lots of examples of complicated texts an shows how easy it is to improve them with few changes.<br />
The book itself is a very nice read and in parts even funny. The author clearly knows what he is teaching and it helped me a lot while writing my bachelor thesis.</li>
</ul>
<h4>Writing the Thesis</h4>
<p>I had two months for the implementation of the project and for writing the thesis. The plan was to implement everything in August and write everything in September and be done at the beginning of October (before my birthday).</p>
<p>Instead last week I ended up being in crunch mode working 15 hours on it every day <img src='http://blog.blackhc.net/wp-includes/images/smilies/icon_neutral.gif' alt=':-|' class='wp-smiley' /> </p>
<p>The implementation went mostly as planned. I found a few driver bugs though, bought <a href="http://www.gremedy.com/" target="_blank">gDEBugger</a> for debugging shader issues and found some bugs in it, too. I think I reported 5-7 bugs in gDEBugger's support forum in a matter of days and still have to write/extend a few reports.</p>
<p>I wrote my thesis in LaTeX and used the very good <a href="http://www.xm1math.net/texmaker/" target="_blank">Texmaker</a> as editor. It supports multiple file projects and allows you to select a main file (usually latex is called with the file that is currently being edited).</p>
<p><a href="http://www.jedit.org/" target="_blank">jEdit</a> also supports LaTeX editing through a plugin: <a href="http://plugins.jedit.org/plugins/?LaTeXTools" target="_blank">LaTeXTools</a>. It isn't working as well as Texmaker though, but it has generally more text editing options and supports directory searches and regexp which is useful for a multi-file project.</p>
<p>I used subversion for version management, which proved useful at times, even though TortoiseSVN's difference viewer doesn't support line wrapping, which sucks a bit because I always write long blocks of text in Texmaker and it made diff'ing difficult.</p>
<p>For creating vector graphics, a very cool LaTeX package exists. It's called <a href="http://sourceforge.net/projects/pgf/" target="_blank">TikZ</a> ("Tikz ist kein Zeichenprogramm"), and it allows you to create illustrations the LaTeX way: by coding them.<br />
See <a href="http://www.texample.net/tikz/examples/" target="_blank">http://www.texample.net/tikz/examples/</a> for some neat looking examples.</p>
<p>I really wanted to use it to create my illustrations, but due to time constraints I couldn't read the whole manual (it's huge!) and I think even then it would have been too difficult.<br />
You see, it suffers from the same problem TeX and LaTeX suffer from in general. It's easy to write something that looks okay, but as soon as you want to tweak something to look exactly the way you imagine it and it is not a standard option, you end up reading weird package code and wishing you had a Phd in TeX editing.</p>
<p>TikZ has exactly the same problem. I've understood the tutorials and the manual well enough, but I wanted to create class diagrams with it. I got lost quickly and the 500 page manual didn't offer a straight-forward way either.</p>
<p>In the end I installed the latest version of <a href="http://www.inkscape.org/" target="_blank">Inkscape</a> and created all my illustrations with it. Well, actually I always tried to create them with TikZ first, got frustrated after losing an hour or two, and spent another hour creating them in Inkscape.</p>
<p>While Inkscape makes it harder to change an illustration later on and there is no way to layout diagrams automatically, you can still become quite fast with it after learning a few hotkeys.</p>
<p>I wanted to use Eclipse and <a href="http://www.uml2.org/download_studio_eclipse_3.4.html" target="_blank">Omondo's UML plugin</a> to create some diagrams of a Java subproject and it failed horribly..</p>
<blockquote><p>Having to wait 20 minutes, while it hogs your computer at 100% CPU usage and 400 MB memory usage, to create a UML model of a 5 classes project is quite unacceptable!</p></blockquote>
<p>Starting Eclipse later crashed it every time, too, by the way. I had to uninstall the damned plugin for Eclipse to start at all again.</p>
<p>In the end I recreated the classes in C++ and used Visual Studio's class diagram viewer to display the classes. You can then print the diagram to PDF and edit it some more in Inkscape, which supports importing single-page PDFs.</p>
<p>I hope I'll find time to write about game development related topics again in the next weeks. There are a few things I want to work on, but sadly university starts on Monday again and it will probably use up most of my time.</p>
<p>Cheers,<br />
 Andreas</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.blackhc.net/2009/10/my-bachelor-thesis/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>From Vertex Positions to Packed Element Arrays</title>
		<link>http://blog.blackhc.net/2009/07/from-vertex-positions-to-packed-element-arrays/</link>
		<comments>http://blog.blackhc.net/2009/07/from-vertex-positions-to-packed-element-arrays/#comments</comments>
		<pubDate>Sat, 18 Jul 2009 17:42:16 +0000</pubDate>
		<dc:creator>BlackHC</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[University]]></category>
		<category><![CDATA[DirectX]]></category>
		<category><![CDATA[no vertex buffer]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://blog.blackhc.net/?p=573</guid>
		<description><![CDATA[<a href="http://blog.blackhc.net/2009/07/from-vertex-positions-to-packed-element-arrays/" title="From Vertex Positions to Packed Element Arrays"></a>At my new workplace at university I'm currently porting an advanced terrain rendering engine from DirectX to OpenGL. One of the performance optimizations the engine uses is that it draws the terrain tiles right from the index buffer without using &#8230;<p class="read-more"><a href="http://blog.blackhc.net/2009/07/from-vertex-positions-to-packed-element-arrays/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://blog.blackhc.net/2009/07/from-vertex-positions-to-packed-element-arrays/" title="From Vertex Positions to Packed Element Arrays"></a><p>At my new workplace at university I'm currently porting an advanced terrain rendering engine from DirectX to OpenGL.<br />
One of the performance optimizations the engine uses is that it draws the terrain tiles right from the index buffer without using a vertex buffer at all - that is it packs the vertex position into the 32-bit index and unpacks it in the vertex shader.</p>
<p>Why is this faster than rendering using a vertex buffer and no index buffer?</p>
<p>When using an index buffer the graphics card can make use of a cache of already transformed (vertex-shaded) vertices and when an index is reused, it can use the cached result instead of running the vertex shader again. Of course, this only works if there exists a certain temporal locality, but that is given.<br />
If no index buffer is used, the vertex cache won't be used, because the implicit index is different for each vertex.</p>
<p>Since I need to port the engine from DirectX to OpenGL, I did some research to see if it is possible to do the same in OpenGL.<br />
It's not really possible but you can achieve something quite similar in OpenGL 3.0.</p>
<h3>Using the Input-Assembler Stage without Buffers (OpenGL)</h3>
<p>This is meant as OpenGL analogon for <a href="http://msdn.microsoft.com/en-us/library/bb232912(VS.85).aspx" target="_blank">Using the Input-Assembler Stage without Buffers (Direct3D 10)</a>.</p>
<p>I think the title is self-explanatory but for greater clarity let me rephrase it:<br />
The aim is to render something without using vertex or index buffers, that is (in OpenGL speak) using neither vertex data nor an elements array to render something.<br />
Instead the automatically supplied <strong>gl_VertexID</strong> attribute  (<strong>vertexId</strong> in DirectX) is used to determine the vertex the shader is currently processing.</p>
<p>The example in MSDN simply draws a triangle using <strong>vertexId</strong>:</p>
<pre class="brush: cpp; title: ; notranslate">
VSOut VSmain(VSIn input)
{
VSOut output;

if (input.vertexId == 0)
output.pos = float4(0.0, 0.5, 0.5, 1.0);
else if (input.vertexId == 2)
output.pos = float4(0.5, -0.5, 0.5, 1.0);
else if (input.vertexId == 1)
output.pos = float4(-0.5, -0.5, 0.5, 1.0);

output.color = clamp(output.pos, 0, 1);

return output;
}
</pre>
<p>If you want to do the same thing in OpenGL, you have at least two problems:</p>
<ul>
<li>OpenGL uses <strong>glVertex*</strong> as end marker for the specification of a single vertex and the array rendering commands (<strong>glDrawArrays</strong>, <strong>glDrawElements</strong>, etc.) all require an enabled vertex array.</li>
<li><strong>gl_</strong><strong>VertexID </strong>is only supplied if:
<ul>
<li>the vertex comes from a vertex array command that specifies a complete primitive (e.g. DrawArrays, DrawElements)</li>
<li>all enabled vertex arrays have non-zero buffer object bindings, and</li>
<li>the vertex does not come from a display list, even if the display list was compiled using DrawArrays / DrawElements with data sourced from buffer objects.</li>
</ul>
<p>(from <a href="http://www.opengl.org/registry/specs/EXT/gpu_shader4.txt" target="_blank">GL_EXT_gpu_shader_4</a>)</li>
</ul>
<p>There is no way around these requirements, but what you can do is to create dummy vertex buffer with one element, bind it as vertex array and simply draw as many vertices as you want. If you don't access gl_Vertex there is no way that uninitialized data can affect the shader and although behavior is generally undefined in OpenGL, if you render beyond the vertex buffer size, it has worked so far that I've test this on.</p>
<p style="text-align: center;">
<div id="attachment_583" class="wp-caption aligncenter" style="width: 690px"><a href="http://blog.blackhc.net/wp-content/uploads/2009/07/circle.JPG"><img class="size-full wp-image-583" title="circle" src="http://blog.blackhc.net/wp-content/uploads/2009/07/circle.JPG" alt="Drawing a circle without accessing gl_Vertex" width="680" height="228" /></a><p class="wp-caption-text">Drawing a circle without accessing gl_Vertex</p></div>
<p>You can download the source code <a href="http://blog.blackhc.net/wp-content/uploads/2009/07/CircleVertexID.zip" target="_blank">here</a>.</p>
<h3>Packing Vertex Positions into the Elements Array</h3>
<p>The next step is to start packing and unpacking data in the <strong>gl_VertexID</strong>. For this an integer type and bit operations (shifting and masking at least) are required in the vertex shader, so it requires GLSL 1.30 at least.</p>
<p>The code is quite short from my proof of concept project, so I'm pasting the shader here:</p>
<pre class="brush: cpp; title: ; notranslate">#version 130
#extension GL_EXT_gpu_shader4 : enable

out vec4 color;

vec3 unpackVertex(int index) {
return vec3( index &amp; 0xFF, (index &gt;&gt; 8 ) &amp; 0xFF, (index &gt;&gt; 16) &amp; 0xFF ) * (2 / 255.0) - vec3(1.0);
}

void main()
{
vec3 unpackedData = unpackVertex( gl_VertexID );
gl_Position = vec4( unpackedData, 1.0 );
color = vec4( (unpackedData + 1.0) / 2.0, 1.0 );
}</pre>
<p>In main.cpp the equivalent can be found for setting up the elements array:</p>
<pre class="brush: cpp; title: ; notranslate">
#define packFloat(v)	(int(((v) + 1.0) / 2 * 255) &amp; 255)
#define packVertex(x,y,z) (packFloat( x ) + (packFloat( y ) &lt;&lt; 8 ) + (packFloat( z ) &lt;&lt; 16))

void display(void) {
[...]

unsigned indices[] = {
packVertex( 0.0, 0.0, -1.0 ), packVertex( 1.0, 0.0, -1.0 ), packVertex( 1.0, 1.0, -1.0 ),
packVertex( 0.0, 0.0, -1.0 ), packVertex( -1.0, 0.0, -1.0 ), packVertex( -1.0, -1.0, -1.0 )
};
glDrawElements( GL_TRIANGLES, sizeof( indices ) / sizeof( *indices ), GL_UNSIGNED_INT, indices );

[...]
}
</pre>
<p>For the code to actually make sense I should initialize an index buffer/elements array buffer in OpenGL and upload the indices into it but this was just for testing.</p>
<div id="attachment_592" class="wp-caption aligncenter" style="width: 691px"><a href="http://blog.blackhc.net/wp-content/uploads/2009/07/positionPacking.JPG"><img class="size-full wp-image-592" title="positionPacking" src="http://blog.blackhc.net/wp-content/uploads/2009/07/positionPacking.JPG" alt="Drawing two triangles by packing their position into the index buffer" width="681" height="231" /></a><p class="wp-caption-text">Drawing two triangles by packing their position into the index buffer</p></div>
<p>You can download the source code <a href="http://blog.blackhc.net/wp-content/uploads/2009/07/IndexPacking.zip">here</a>.</p>
<p>Note:<br />
It has been brought to my attention (thanks Yagero), that on NVIDIA cards the driver currently reduces the element array data size from int to short, which causes the third byte (ie z values in this packing scheme) to always be 0.</p>
<p>A fix is to use an element array buffer (index buffer) which prevents the driver from messing with the data size.<br />
You can download the adapted source code <a href="http://blog.blackhc.net/wp-content/uploads/2009/07/IndexPackingWBuffer.zip" target="_blank">here</a>. To show that it works, I have swapped the x and z component in the packing scheme.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.blackhc.net/2009/07/from-vertex-positions-to-packed-element-arrays/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Weird Things Happen</title>
		<link>http://blog.blackhc.net/2008/09/weird-things-happen/</link>
		<comments>http://blog.blackhc.net/2008/09/weird-things-happen/#comments</comments>
		<pubDate>Mon, 15 Sep 2008 19:15:17 +0000</pubDate>
		<dc:creator>BlackHC</dc:creator>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Personal Rantings]]></category>
		<category><![CDATA[FBO]]></category>
		<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Transfer Functions]]></category>

		<guid isPermaLink="false">http://blackhc.wordpress.com/?p=204</guid>
		<description><![CDATA[<a href="http://blog.blackhc.net/2008/09/weird-things-happen/" title="Weird Things Happen"></a>Today I was at work for the first time since August and nobody seems to have logged into my shared workstation since then since my account name was still in the login field. The first weird thing that happened was &#8230;<p class="read-more"><a href="http://blog.blackhc.net/2008/09/weird-things-happen/">Read more &#187;</a></p>]]></description>
			<content:encoded><![CDATA[<a href="http://blog.blackhc.net/2008/09/weird-things-happen/" title="Weird Things Happen"></a><p>Today I was at work for the first time since August and nobody seems to have logged into my shared workstation since then since my account name was still in the login field.<br />
The first weird thing that happened was that suddenly the application I've been working on didn't run correctly anymore.<br />
First let me tell you a bit about the app:<br />
It renders a volume texture with some custom raytracing shaders in one window and allows editing of the <strong>transfer function</strong> - this is a function/texture that maps a scalar value in the volume texture to a color in another window. The <em>transfer function</em> is made up of primitives/shapes that you can colorize and stretch or deform the way you want to allow for easy editing. Look at <a title="A Transfer Function Editor" href="http://software.sci.utah.edu/doc/User/Tutorials/BioImage/Figures/editTFWindow.jpg" target="_blank">this picture</a> to see what a transfer function (editor) could look like - this isn't ours though.<br />
The <strong>TF</strong> editor uses an <strong>FBO </strong>to render the <em>transfer function</em> into the <em>transfer texture</em>.</p>
<p>While the rest of the app was running mostly fine (that is AntTweakBar rendered fine and the background of the transfer function editor which is a histogram of the volume texture, too), the actual transfer function rendering was corrupted.<br />
On my workstation you could move the primitive around and while you did so it would either totally disappear, invert its color or show small light green or purple blocks that stayed stationary inside the window as you moved the primitive around - the whole look liked corrupted memory that stayed corrupted and was only visible when the primitive rendered over it.</p>
<p>It's a pity I didn't take any screenshots but I didn't think of that back then.</p>
<p>I was totally at a loss with this behavior, so I rebooted which didn't fix it. Somehow I was hoping that the gfx card broke in the last month and it wasn't my code which caused this totally inexplicable behavior.<br />
To determine this I logged into another workstation in our lab, checked out my code and run the program. To my horror it had similar problems... now quite the same but still strangely colored sprinkles all over the editor window when the primitive was rendered there. Both workstations are using graphics cards from <strong>Nvidia </strong>(<strong>8800 GTX</strong> if I recall correctly).</p>
<p>I tried to determine the revision which broke rendering with a binary search like trial &amp; error through our revision history at work and found out that it broke when I changed the way the primitives were rendered to the texture.</p>
<p>It used to simply iterate through all primitives and render them once to the <em>FBO </em>and it was done.</p>
<p>The paper I'm implementing at the moment required  a special solution for overlapping primitives though, so I switched to a more complicated 2.5 pass rendering.</p>
<p>If $latex c_i $ is the color value of the <em>i</em>th primitive and $latex \alpha _ i $ its alpha value, then the resulting color and alpha are calculated as follows: $latex \left&lt; c, \alpha \right&gt; = \left&lt; \frac{\displaystyle\sum_i^n{c_i \alpha_i}}{\displaystyle\sum_{i}^n{\alpha_i}},\max_{i=1..n}{\alpha_i} \right&gt;$.</p>
<p>For this I do three passes and use two floating point textures:</p>
<ol>
<li> Render the primitives into a temp texture with
<pre>glBlendFuncSeparate( GL_SRC_ALPHA, GL_ONE, GL_ONE, GL_ONE );</pre>
<p>to get $latex \left&lt; {\displaystyle\sum_i^n{c_i \alpha_i}},{\displaystyle\sum_{i}^n{\alpha_i}} \right&gt; $.</li>
<li>Next I set the <em>FBO </em>to the real transfer function texture and use the temp texture as source and divide the color values by their alpha using a custom shader.</li>
<li>Finally I use
<pre>glBlendEquation( GL_MAX );</pre>
<p>and only masks all except the alpha channel and render all primitives again into the transfer function texture. This gets the correct term into the alpha channel of the texture.</li>
</ol>
<p>This usually works and although it's a bit cumbersome it yields the correct result. Only now I identified it to be the source of all my trouble.</p>
<p>Here's the relevant part in our source code:</p>
<p>    _checkGLErrors();<br />
    m_fbo.AttachTexture( GL_COLOR_ATTACHMENT0_EXT, m_tempTexture.glTarget, m_tempTexture.glTexID );<br />
    assert( m_fbo.IsValid() );<br />
    m_fbo.Bind();<br />
    _checkGLErrors();</p>
<p>    glClearColor( 0.0, 0.0, 0.0, 0.0 );<br />
    glClear( GL_COLOR_BUFFER_BIT );</p>
<p>    // C = srcC * srcA + dstC = C * A + C' * A' + ...<br />
    // A = srcA + dstA = A + A' + A'' + ...<br />
    glBlendFuncSeparate( GL_SRC_ALPHA, GL_ONE, GL_ONE, GL_ONE );<br />
    glBlendEquationSeparate( GL_FUNC_ADD, GL_FUNC_ADD );</p>
<p>    for( Primitive::PList::const_iterator i = m_primitives.begin() ; i != m_primitives.end() ; i++ ) {<br />
        (*i)->bake();<br />
    }</p>
<p>    m_fbo.Unattach( GL_COLOR_ATTACHMENT0_EXT );<br />
    m_fbo.Disable();<br />
    _checkGLErrors();</p>
<p>    // divide color by alpha<br />
    glMatrixMode( GL_PROJECTION );<br />
    glLoadIdentity();</p>
<p>    glBlendFunc( GL_ONE, GL_ZERO );</p>
<p>    procInf.imageOp( m_tempTexture, m_transferFunction, m_program );</p>
<p>    // render again this time to figure out the max alpha value</p>
<p>    glMatrixMode( GL_PROJECTION );<br />
    glLoadIdentity();<br />
    glOrtho( 0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );</p>
<p>    m_fbo.AttachTexture( GL_COLOR_ATTACHMENT0_EXT, m_transferFunction.glTarget, m_transferFunction.glTexID );<br />
    assert( m_fbo.IsValid() );<br />
    m_fbo.Bind();<br />
    _checkGLErrors();</p>
<p>    glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE );<br />
    glBlendEquation( GL_MAX );</p>
<p>    for( Primitive::PList::const_iterator i = m_primitives.begin() ; i != m_primitives.end() ; i++ ) {<br />
        (*i)->bake();<br />
    }</p>
<p>Now I was ready to hunt the bug but the problem the revision was written more than a few days before I left in August and it was working back then. I refused to believe that it was broken and I went to my boss who had been using his rig in the last days and indeed it worked. I couldn't really believe my eyes. Two workstations appeared to be broken for some reason (<em>driver issues or even the gfx cards? - who would know..</em>).</p>
<p>I went back and somehow managed to change the appearance of the bug on one of the workstations - all the framebuffer corruptions went away and were replaced with what seemed like an unchanging alpha channel of one of the FBO textures with alpha being 0 on the initial position of the primitive and 1.0 everywhere else, so it masked it out at its initial position.</p>
<p>For some reason I thought that maybe using BlendFuncSeparate and the normal BlendFunc function together maybe left OGL's state machine in a mess for some reason so I added calls to disable and enable blending to code to render the call to glBlendFunc useless (see the code above and/or below).</p>
<p>    _checkGLErrors();<br />
    m_fbo.AttachTexture( GL_COLOR_ATTACHMENT0_EXT, m_tempTexture.glTarget, m_tempTexture.glTexID );<br />
    assert( m_fbo.IsValid() );<br />
    m_fbo.Bind();<br />
    _checkGLErrors();</p>
<p>    glClearColor( 0.0, 0.0, 0.0, 0.0 );<br />
    glClear( GL_COLOR_BUFFER_BIT );</p>
<p>    // C = srcC * srcA + dstC = C * A + C' * A' + ...<br />
    // A = srcA + dstA = A + A' + A'' + ...<br />
    glBlendFuncSeparate( GL_SRC_ALPHA, GL_ONE, GL_ONE, GL_ONE );<br />
    glBlendEquationSeparate( GL_FUNC_ADD, GL_FUNC_ADD );</p>
<p>    for( Primitive::PList::const_iterator i = m_primitives.begin() ; i != m_primitives.end() ; i++ ) {<br />
        (*i)->bake();<br />
    }</p>
<p>    m_fbo.Unattach( GL_COLOR_ATTACHMENT0_EXT );<br />
    m_fbo.Disable();<br />
    _checkGLErrors();</p>
<p>    // divide color by alpha<br />
    glMatrixMode( GL_PROJECTION );<br />
    glLoadIdentity();</p>
<p>    ---> glDisable(GL_BLEND); <---<br />
    glBlendFunc( GL_ONE, GL_ZERO );</p>
<p>    procInf.imageOp( m_tempTexture, m_transferFunction, m_program );</p>
<p>    // render again this time to figure out the max alpha value</p>
<p>    glMatrixMode( GL_PROJECTION );<br />
    glLoadIdentity();<br />
    glOrtho( 0.0, 1.0, 0.0, 1.0, -1.0, 1.0 );</p>
<p>    m_fbo.AttachTexture( GL_COLOR_ATTACHMENT0_EXT, m_transferFunction.glTarget, m_transferFunction.glTexID );<br />
    assert( m_fbo.IsValid() );<br />
    m_fbo.Bind();<br />
    _checkGLErrors();</p>
<p>    ---> glEnable( GL_BLEND ); <---<br />
    glColorMask( GL_FALSE, GL_FALSE, GL_FALSE, GL_TRUE );<br />
    glBlendEquation( GL_MAX );</p>
<p>    for( Primitive::PList::const_iterator i = m_primitives.begin() ; i != m_primitives.end() ; i++ ) {<br />
        (*i)->bake();<br />
    }</p>
<p>    m_fbo.Disable();</p>
<p>When I ran the program, it decided to work normally and render everything the way it should. I was stunned once again.<br />
Now the good thing with causality is that everything is supposed to be repeatable, so I removed the statements again and was expecting to see the bug appear again.. - but guess what it did <strong>not</strong> and suddenly the machine was working again as supposed.</p>
<p>I tried the same fix with the other workstation in our lab and it fixed it before I had time to take some screenshots of the persisting framebuffer corruption.</p>
<p>I'm still at a loss here and I guess I won't be able to come up with an explanation of my own for this, so if anyone has an idea, feel free to tell me <img src='http://blog.blackhc.net/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Cheers,<br />
Andreas</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.blackhc.net/2008/09/weird-things-happen/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

