<?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>Schabby&#039;s Blog</title>
	<atom:link href="http://schabby.de/feed/" rel="self" type="application/rss+xml" />
	<link>http://schabby.de</link>
	<description>OpenGL, Java, Cassandra and other stuff that totally makes the world go round</description>
	<lastBuildDate>Mon, 16 Apr 2012 10:15:43 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.2</generator>
		<item>
		<title>For- und ForEach Schleifen in Java</title>
		<link>http://schabby.de/java-for/</link>
		<comments>http://schabby.de/java-for/#comments</comments>
		<pubDate>Thu, 19 Jan 2012 16:44:29 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=1037</guid>
		<description><![CDATA[Kurze Einführung in For-Schleifen in Java mit Beispielen. Die For-Schleife und For-Each Schleife sind essentielle Bestandteile der Programmiersprache Java.]]></description>
			<content:encoded><![CDATA[<p>In diesem post widme ich mich folgender Fragestellungen:</p>
<ul>
<li>Beispiele und Erklärung der For- und ForEach Schleife</li>
<li>Was ist mit For bzw. ForEach Loop gemeint?</li>
<li>Was sind Java Laufvariablen und wie benutze ich sie?</li>
</ul>
<p><span id="more-1037"></span>Es werden erst die klassische For-Schleifen besprochen, bevor wir uns den moderneren For-Each Schleifen widmen.</p>
<h2>Einführung und Definition</h2>
<p>Die For-Schleife dient zum wiederholten ausführen eines Code-Blocks, wobei in jeder Iteration ein Schritt-Operation (zB. Hochzählen einer Variable) ausgeführt wird. Etwas weniger allgemein formuliert kann man sagen, dass sie zum Hoch- oder Runterzählen von Variablen verwendet werden kann. Der englische Begriffe der Schleife ist Loop, daher sind beide Begriffe synonym zu verstehen.</p>
<p>Das folgende Beispiel zählt die variable i von 5 bis 10 hoch:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span> i<span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// die Laufvariable</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i <span style="color: #339933;">=</span> i <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ausgeführter Code der die Laufvariable i verwenden kann, zB.</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;i = &quot;</span><span style="color: #339933;">+</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Wir sehen, dass in dem eingeklammerten Teil vor dem <tt>for</tt> etwas kryptisches stehts, was offensichtlich zum Verwenden der For-Schleife nötig ist. Dieser Teil läßt sich in drei Teile zerlegen, die jeweils mit einem Semmikolon getrennt sind:<br/></p>
<p><tt>for(</tt> <b>Start</b> <tt>; </tt> <b>Ende</b> <tt>; </tt> <b>In-/Dekrementieren</b> <tt>)</tt><br/></p>
<p>Im obigen Beispiel ist besteht der erste Teil (Start) aus dem setzen des Startwertes <tt>i = 5</tt>, also dass die Schleife bei 5 anfangen soll zu zählen. Häufig wird an dieser Stelle die Deklaration der Variable  Der zweite Teil ist das Abbruchkriterium, nämlich <tt>i &lt;= 10</tt>, was so viel heißt wie, dass <tt>i</tt> solange hochgezählt wird, bis i kleiner-gleich 10 ist. Der dritte Teil besagt was nach jeder Iteration passieren soll. In diesem Fall wird <tt>i</tt> um 1 erhöht. Eine häufige Notation die man an dieser Stelle sieht ist <tt>i++</tt>, was einfach nur eine Kurzform von <tt>i = i + 1</tt> ist. Entsprechend ist übrigens <tt>i--</tt> nur die Kurzform von <tt>i = i - 1</tt>. Ein weiteres Beispiel einer For-Schleife sie wie folgt aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> i <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span> i <span style="color: #339933;">&gt;=</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> i<span style="color: #339933;">--</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ausgeführter Code der die Laufvariable i verwenden kann, zB.</span>
    <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;i = &quot;</span><span style="color: #339933;">+</span>i<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Diese Schleife zählt ganz offensichtlich runter von 10 auf 5. In diesem Beispiel deklarieren wir <tt>i</tt> &#8220;inline&#8221; gleich mit in der Start-Definition der For-Schleife. Das spart uns eine Zeile vorher und es ist klar, dass <tt>i</tt> nur im Scope des Schleifen-Blocks liegt. Die Definition der Abbruchbedingung ist entsprechend abgewandelt zum ersten Beispiel. Der letzte Teil ist die dekrementierung von <tt>i</tt>.</p>
<p>Generell kann man also den eingeklammerten Teil der Schleifendefinition wie folgt lesen:</p>
<p><tt>for( </tt><b>Starte bei <tt>i=10</tt> und führe</b><tt> ;  </tt><b>solange wie <tt>i&gt;=5</tt> gilt,</b><tt> ;  </tt><b>den Code <tt>i--</tt> aus</b><tt> )</tt><br/></p>
<p>Der Unterschied zu einer <tt>while</tt>-Schleife liegt also darin, dass im eingeklammerten Teil nicht nur die Abbruchbedingung festgelegt wird.</p>
<p>Schleifen können auch verschachtelt werden und so ist es nicht unüblich, dass man zum iterieren über Bilder oder Matrizen zwei For-Schleifen anwendet um mit zwei Laufvariablen einen 2-Dimensionalen Raum abzuwandern. Im folgenden Beispiel iterieren wir über das Google Logo und geben dabei Pixelweise die Farbe aus:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// lade Google Logo</span>
<span style="color: #003399;">BufferedImage</span> image <span style="color: #339933;">=</span> ImageIO.<span style="color: #006633;">read</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">URL</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;http://www.google.de/images/srpr/logo3w.png&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// x iteriert über die Breite des Bildes</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> image.<span style="color: #006633;">getWidth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// y iteriert über die Höhe des Bildes</span>
	<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> y <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> y <span style="color: #339933;">&lt;</span> image.<span style="color: #006633;">getHeight</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> y<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Gib Pixelfarben als Red, Green, Blue, alpha Komponenten auf der Konsole aus.</span>
		<span style="color: #000066; font-weight: bold;">int</span> red   <span style="color: #339933;">=</span> image.<span style="color: #006633;">getColorModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getRed</span><span style="color: #009900;">&#40;</span>image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> green <span style="color: #339933;">=</span> image.<span style="color: #006633;">getColorModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGreen</span><span style="color: #009900;">&#40;</span>image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> blue  <span style="color: #339933;">=</span> image.<span style="color: #006633;">getColorModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBlue</span><span style="color: #009900;">&#40;</span>image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #000066; font-weight: bold;">int</span> alpha <span style="color: #339933;">=</span> image.<span style="color: #006633;">getColorModel</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getBlue</span><span style="color: #009900;">&#40;</span>image.<span style="color: #006633;">getRGB</span><span style="color: #009900;">&#40;</span>x, y<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Position (&quot;</span><span style="color: #339933;">+</span>x<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, &quot;</span><span style="color: #339933;">+</span>y<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;): [&quot;</span><span style="color: #339933;">+</span>red<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">+</span>green<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">+</span>blue<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;,&quot;</span><span style="color: #339933;">+</span>alpha<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;]&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Natürlich können auch andere Werte als Zahlen als Laufvariabe und zur Iteration verwendet werden. So war es früher beispielsweise üblich, dass man Iterator-Objekte verwendete, die den aktuellen Status der Laufvariable wiederspiegelte und mit denen über unterschiedliche Datenstrukturen iteriert werden konnte. Im Prinzip speichern solche Iterator Objekte intern auch nur einen index ab, an welcher Stelle man sich aktuell in der Datenstruktur befindet. Ein Beispiel macht deutlich, was gemeint ist:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// dynamische Liste, so wie sie früher häufig verwendet wurde</span>
Vector<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> vector <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Vector<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// wir hängen ein paar Namen an die Liste an</span>
vector.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Peter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
vector.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Klaus&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
vector.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Frieda&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
vector.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Manfred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Vector.iterator() gibt ein Objekt vom Type Iterator zurück</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> Iterator<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> it <span style="color: #339933;">=</span> vector.<span style="color: #006633;">iterator</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// it.next() holt den nächsten Wert aus dem Vector ab</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die Ausgabe ist erwartungsgemäß:</p>
<pre>
Peter
Klaus
Frieda
Manfred
</pre>
<p>Das interessant an diesem Beispiel ist, dass der letzte Part des in Klammern eingeschlossenen Teils leer bleibt. Das heißt es gibt keinen Iterationsteil. Dieser wird nämlich im For-Block selbst ausgeführt (<tt>it.next()</tt>).</p>
<p>So weit, so gut. Wollen wir nun zum nächsten Level vordringen.</p>
<h2>ForEach Schleifen mit Beispielen</h2>
<p>For-Each Schleifen funktionieren ganz ähnlich wie die gewöhnlichen For-Schleifen und sind quasi eine Weiterentwicklung. Im Gegensatz zu den normalen, verwenden ForEach Schleifen aber keine nach außen sichtbare Laufvariable oder Iterator-Objekt, sondern gibt in jeder Iteration immer direkt den Wert zurück. Das heißt aber auch, dass immer über eine entsprechende Datenstruktur iteriert werden muss die das ermöglicht. Schauen wir uns ein Beispiel an:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// definition eine Datenstruktur, hier ein Array mit 9 Werten</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> array <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">8</span>, <span style="color: #cc66cc;">4</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">2</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">5</span>, <span style="color: #cc66cc;">9</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// ForEach Schleife</span>
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> k<span style="color: #339933;">:</span> array <span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;k = &quot;</span><span style="color: #339933;">+</span>k<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Die Ausgabe ist</p>
<pre>
k = 4
k = 8
k = 4
k = 2
k = 2
k = 1
k = 1
k = 5
k = 9
</pre>
<p>Man sieht also, dass <tt>k</tt> nicht die Laufvariable hält, sondern den direkten Wert. ForEarch iteriert also über alle Elemente und ruf den Block auf. Daher kann auch der Name abgeleitet werden: <i>For Each element, do this and that</i>. Intern wird dennoch ein Iterator-Objekt gehalten in dem der State der Iteration festgehalten wird. Da man als Entwickler aber quasi nie am Index der Laufvariable interessiert ist, sondern eigentlich nur an den Werten, macht ForEach jede Menge Sinn. Ein paar Beispiele für weitere Verwendungen von ForEach-Schleifen:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">Set<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> set <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> HashSet<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
set.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Peter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
set.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Frieda&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
set.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Manfred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #339933;">:</span> set<span style="color: #009900;">&#41;</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>In diesem Beispiel ist die Reihenfolge der Ausgabe nicht definiert, da es sich um eine Menge (Set) handelt und nicht um eine Liste oder andere order-preserving Datenstruktur. Das Gegenbeispiel dazu ist die Verwendung der ArrayList:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">List<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span> list <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> ArrayList<span style="color: #339933;">&lt;</span>String<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Peter&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Frieda&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
list.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Manfred&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> name<span style="color: #339933;">:</span> list<span style="color: #009900;">&#41;</span> <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Und das wars <img src='http://schabby.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' />  Ich denke Ihr habt nun eine sehr gute Vorstellung davon wie diese Schleifen zu verwenden sind. Wenn ich etwas vergessen habe oder einen Fehler eingebaut habt, laßt es mich bitte wissen. Wenn Euch der Post gefallen, dann laßt auch gern einen Kommentar da.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/java-for/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Arrays</title>
		<link>http://schabby.de/java-arrays/</link>
		<comments>http://schabby.de/java-arrays/#comments</comments>
		<pubDate>Sat, 19 Nov 2011 23:12:33 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=1077</guid>
		<description><![CDATA[Dieser Artikel behandelt die folgenden Themen Java Arrays mit Beispielen Arrays in Java in mehreren Dimensionen Arrays sind die niedrigsts (low-level) aber auch performanteste Datenstruktur in Java. Sie folgen sehr nah der Hardwarefunktionalität im Speicher des Rechners und sind somit eigentlich ein Überbleibsel aus der C/C++ Welt. Dennoch kann man Arrays in vielen Java-Anwendungen heutzutage [...]]]></description>
			<content:encoded><![CDATA[<p>Dieser Artikel behandelt die folgenden Themen</p>
<ul>
<li>Java Arrays mit Beispielen</li>
<li>Arrays in Java in mehreren Dimensionen</li>
</ul>
<p><span id="more-1077"></span><br />
Arrays sind die niedrigsts (low-level) aber auch performanteste Datenstruktur in Java. Sie folgen sehr nah der Hardwarefunktionalität im Speicher des Rechners und sind somit eigentlich ein Überbleibsel aus der C/C++ Welt. Dennoch kann man Arrays in vielen Java-Anwendungen heutzutage noch finden.</p>
<h2>Grundlagen</h2>
<p>Das klassische Array ist eine Folge von Zahlen fester Länge. Diese Folge von Zahlen liegt als Block im Speicher. Schauen wir uns die folgenden zwei Arrays an.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Definition eines Arrays das 24 Integerwerte halten kann. Zu beginn sind die alle 0.</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> array <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">24</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Definition eines Arrays das 8 Integerwerte halten kann</span>
<span style="color: #666666; font-style: italic;">// und dessen Werte vorinitialisiert werden</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// die eckigen Klammern können auch an die Variable geschrieben werden</span>
<span style="color: #000066; font-weight: bold;">int</span> someOtherArray<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Man definiert die Länge des Arrays, also wie viele Zahlen in das Array passen, wenn man das Array erstellt. Dabei wird der benötigte Speicher reserviert. Der Speicherblock kann nicht ohne weiteres wachsen, was wiederum die Länge des Array. Da die Zahlenarten in dem Array immer nur von einem Typ sind, ist zu beginn klar, wieviel Speicher reserviert werden muss. Für <tt>int</tt>-Arrays müssen zum Beispiel immer 4 bytes pro Wert vorgehalten werden. Das heißt ein <tt>int</tt>-Array das 24 Werte halten kann, benötigt 24*4=96 bytes. Streng genommen kommen da sogar nochmal 4 Bytes dazu, weil die Länges des Arrays im selben Speicherblock liegt (Backreferenzen für den Garbage Collector werden kurz mal ausgeblendet). </p>
<p>Die Zahl der Werte eines Arrays (oder eben die &#8220;Länge&#8221; eines Arrays), kann mit der Konstanten <tt>length</tt> abgefragt werden:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Definition eines Arrays das 8 Integerwerte halten kann</span>
<span style="color: #666666; font-style: italic;">// und dessen Werte vorinitialisiert werden</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test.<span style="color: #006633;">length</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe: 7</span></pre></div></div>

<p>Um auf die Werte des Arrays zuzugreifen, muss lediglich der Speicherindex angegeben werden:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Definition eines Arrays das 8 Integerwerte halten kann</span>
<span style="color: #666666; font-style: italic;">// und dessen Werte vorinitialisiert werden</span>
<span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe 32</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe -5</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Um Werte zuzuweisen, wird genauso verfahren</span>
test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">78</span><span style="color: #339933;">;</span>
test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">98</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe 78</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">4</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe 98</span></pre></div></div>

<p>Als wichtiger Sonderfall sein bemerkt, dass bei jedem Zugriff auf das Array geprüft wird, ob der Index noch innerhalb der Länge des Arrays liegt. Wenn der Index kleiner 0 oder größer als <tt>length</tt> ist, wird eine <tt>IndexOutOfBoundsException</tt> geworfen. Siehe zB:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> test <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// schmeisst IndexOutOfBoundsExcpetion!!!</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>test<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">5423</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<h2>Arrays aus verschiedenen Datentypen</h2>
<p>In den Grundlagen haben wir auf Arrays aus natürlichen Zahlen gearbeitet. Prinzipiell sind natürlich auch andere Datentypen möglich. Schauen wir uns als erstes Beispiele mit primitiven Datentypen an.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> intArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">int</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#123;</span><span style="color: #cc66cc;">33</span>,<span style="color: #cc66cc;">452</span>,<span style="color: #cc66cc;">6</span>,<span style="color: #cc66cc;">23</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">5</span>,<span style="color: #cc66cc;">32</span>,<span style="color: #cc66cc;">5</span>,<span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> floatArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> doubleArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #000066; font-weight: bold;">double</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">521</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Es können auch Arrays aus Objekten gebaut werden. Dabei sei zu beachten, dass beim erstellen des Arrays nicht die Objekte selbst erstellt werden, sondern nur die Pointer auf mögliche Objektinstanzen.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// erstelle 10 Pointer auf Object. Die Pointer sind</span>
<span style="color: #666666; font-style: italic;">// erstmal alle NullPointer.</span>
<span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> objectArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">10</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// wir instatitieren ein neues Objekt mit &quot;new&quot; und lassen den Pointer an </span>
<span style="color: #666666; font-style: italic;">// Index 0 und 3 auf das neue Objekt zeigen.</span>
objectArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
objectArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Object</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// es wird die toString() Methode des gerade instatitierten </span>
<span style="color: #666666; font-style: italic;">// Objektes ausgegeben</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>objectArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// NullPointerException, weil an Index 2 kein valider Pointer</span>
<span style="color: #666666; font-style: italic;">// auf ein Objekt zeigt.</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>objectArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Eine interessante Mischform nimmt dabei wieder der String ein, da er sich wie immer wie ein Objekt verhält, das aber nicht mit new instatiiert werden muss:</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> stringArray <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">42</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;hallo&quot;</span><span style="color: #339933;">;</span>
stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Juhuuu&quot;</span><span style="color: #339933;">;</span>
stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;Blablalb&quot;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe: hallo</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">2</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> <span style="color: #666666; font-style: italic;">// Ausgabe: Blablalb</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// NullPointerException!</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>stringArray<span style="color: #009900;">&#91;</span><span style="color: #cc66cc;">3</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>So viel zu den Basisics über Arrays. Wenn Euch was fehlt oder Ihr den Post gut fandet, laßt es mich gern in Form eines Kommentars wissen.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/java-arrays/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Display Lists in OpenGL</title>
		<link>http://schabby.de/display-lists/</link>
		<comments>http://schabby.de/display-lists/#comments</comments>
		<pubDate>Sun, 25 Sep 2011 16:18:39 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=1014</guid>
		<description><![CDATA[This is a short OpenGL tutorial covering Display Lists with a code example. This should get you started when you want to enhance the rendering performance in your application.]]></description>
			<content:encoded><![CDATA[<p>This post is a brief tutorial on how to use display lists in OpenGL. An working and self-contained example is given below using LWJGL.<br />
Display lists are an essential feature of OpenGL to improve the rendering performance of your application. You can think of them as kind of a macro for OpenGL commands that you name, record and call at some point in your game. More specifically, you assign an integer handle to a sequence of OpenGL commands and call the commands identified by that handle when you need them.<span id="more-1014"></span></p>
<p><center><iframe width="420" height="315" src="http://www.youtube.com/embed/svkD0ouFHQU" frameborder="0" allowfullscreen></iframe></center></p>
<p>The advantage is mainly rendering speed which is greatly improved as you don&#8217;t pump your geometry into the rendering pipeline through individual <tt>glVertex</tt>, <tt>glNormal</tt> etc. commands in every render pass. Instead, you render your potentially complex geometry into a display list once and call ist every time you want the geometry to be rendered. This becomes enormously beneficial in a Java environment where you want to use as few JNI calls as possible.</p>
<p>The disadvantage is that a display list is fixed once it is uploaded to video memory. That means you cannot modify the order or geometry data after you compiled your list. Even if you only need to change one single <tt>glTranslate</tt> parameter, you would need to delete the entire list and create a new one.</p>
<p>So far for dry theory, let&#8217;s look at the example. Please note that this example makes use of my <a href="http://schabby.de/lwjgl-example-base/" title="LWJGL Example Base">LWJGL example base class</a> which I recommend to copy first so that the example runs out of the box.</p>
<p>We start with setting up the example class as follows. The <tt>setUp</tt> method is responsible for recording the GL commands into the &#8220;macro&#8221;.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DisplayListExample <span style="color: #000000; font-weight: bold;">extends</span> LwjglExampleBase
<span style="color: #009900;">&#123;</span>
	<span style="color: #666666; font-style: italic;">// handle (id) of the display list to be generated</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> displayListHandle <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #666666; font-style: italic;">// angle in degrees required for rotating the cubes</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">float</span> rotationAngle <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Sets up LWJGL and OpenGL and renders the 
	 * display list.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// Set up LWJGL and OpenGL using the</span>
		<span style="color: #666666; font-style: italic;">// base class.</span>
		<span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">setUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Generate one (!) display list.</span>
		<span style="color: #666666; font-style: italic;">// The handle is used to identify the</span>
		<span style="color: #666666; font-style: italic;">// list later.</span>
		displayListHandle <span style="color: #339933;">=</span> glGenLists<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Start recording the new display list.</span>
		glNewList<span style="color: #009900;">&#40;</span>displayListHandle, GL_COMPILE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// Render a single cube</span>
		renderCube<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// End the recording of the current display list.</span>
		glEndList<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Setting up a display list is a simple as that. Note the parameter <tt>GL_COMPILE</tt>. It tells OpenGL to only compile the list and not to draw it at the same time. Alternatively,<br />
one could use GL_COMPILE_AND_EXECUTE which would render the result while recording.</p>
<p>Lets us have a look into the <tt>renderCube()</tt> which basically contains the OpenGL commands to render a simple cube (without normals because we dont have lighting).</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> renderCube<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span> size<span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">float</span> HALF <span style="color: #339933;">=</span> size<span style="color: #339933;">/</span><span style="color: #cc66cc;">2</span><span style="color: #339933;">;</span>
		glBegin<span style="color: #009900;">&#40;</span>GL_QUADS<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		glColor3f<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
		glColor3f<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
		glColor3f<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
		glColor3f<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
		glColor3f<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
&nbsp;
		glColor3f<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>  
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glVertex3f<span style="color: #009900;">&#40;</span>size<span style="color: #339933;">-</span>HALF, <span style="color: #339933;">-</span>HALF, size<span style="color: #339933;">-</span>HALF<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
		glEnd<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Now comes the interesting part of calling the display list. To illustrate its usefulness, I draw a grid of 5&#215;5 of the same cube and only change its position and rotation.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Render 5x5 cubes centered around the origin. Each
	 * cubes has a slightly different rotation to make this
	 * example look more interesting.
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> render<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// In each render pass, add one degree to rotation</span>
		<span style="color: #666666; font-style: italic;">// angle.</span>
		rotationAngle <span style="color: #339933;">+=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
		<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> x <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> x <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> x<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			<span style="color: #000000; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">int</span> z <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span> z <span style="color: #339933;">&lt;</span> <span style="color: #cc66cc;">5</span><span style="color: #339933;">;</span> z<span style="color: #339933;">++</span> <span style="color: #009900;">&#41;</span>
			<span style="color: #009900;">&#123;</span>
				<span style="color: #666666; font-style: italic;">// Save current MODELVIEW matrix to </span>
				<span style="color: #666666; font-style: italic;">// stack.</span>
				glPushMatrix<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// position next cube</span>
				glTranslatef<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">4</span>,<span style="color: #cc66cc;">0</span>, z<span style="color: #339933;">*</span><span style="color: #cc66cc;">2</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">4</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// rotate next cube</span>
				glRotatef<span style="color: #009900;">&#40;</span>x<span style="color: #339933;">*</span>z<span style="color: #339933;">*</span><span style="color: #cc66cc;">10</span><span style="color: #339933;">+</span>rotationAngle, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// Call the display list which renders</span>
				<span style="color: #666666; font-style: italic;">// the cube.</span>
				glCallList<span style="color: #009900;">&#40;</span>displayListHandle<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
				<span style="color: #666666; font-style: italic;">// Remove current MODELVIEW Matrix from</span>
				<span style="color: #666666; font-style: italic;">// stack.</span>
				glPopMatrix<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #009900;">&#125;</span>
		<span style="color: #009900;">&#125;</span>
	<span style="color: #009900;">&#125;</span></pre></div></div>

<p>And that&#8217;s it! Pretty easy, huh? To conclude the example I provide the <tt>main()</tt> method necessary to run the example. Also, please bear in mind that you need <a href="http://schabby.de/lwjgl-example-base/">LwjglExampleBase</a> to run the example.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;">	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> argv<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		DisplayListExample example <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> DisplayListExample<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		example.<span style="color: #006633;">setUp</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		example.<span style="color: #006633;">gameLoop</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Alright, we are done with the tutorial. If you liked the post, please leave a comment and let me know if you have questions or found a bug.</p>
<p>Cheers, Johannes</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/display-lists/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>OpenGL Picking in 3D</title>
		<link>http://schabby.de/picking-opengl-ray-tracing/</link>
		<comments>http://schabby.de/picking-opengl-ray-tracing/#comments</comments>
		<pubDate>Mon, 19 Sep 2011 22:25:54 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=964</guid>
		<description><![CDATA[There are two common ways to accomplish picking: Switching OpenGL to "selection mode" and tracing a line shot from your 2D view screen through the 3D scene. In this opengl picking tutorial I cover the latter and provide a code example.]]></description>
			<content:encoded><![CDATA[<p>This blog post explains a common and versatile approach to OpenGL picking called &#8220;ray picking&#8221;. An example in pseudo-code is given below.</p>
<p>Picking is the process of finding objects in you scene based on user input. Most commonly, you want to determine what object a user has clicked with his mouse. In this case the 2D mouse coordinates serve as a reference on the view port to identify the projected objected that has been clicked. A similar scenario is a first person shooter, where the gun serves as the picking pointer, however in world space coordinates. If you shoot your gun, the trajectory of the bullet is traced through your scene and collisions are detected.<span id="more-964"></span></p>
<div id="attachment_996" class="wp-caption aligncenter" style="width: 473px"><img src="http://schabby.de/wp-content/uploads/2011/09/screenshot-opengl-picking.png" alt="Screenshot OpenGL Picking" title="Screenshot OpenGL Picking" width="463" height="363" class="size-full wp-image-996" /><p class="wp-caption-text">A screenshot showing a hex map in an LWJGL application with a selected tile as an example for 3D picking</p></div>
<h3>OpenGL Selection Mode vs. Ray Picking</h3>
<p>There are two common ways of implementing picking. The first one makes use of a special OpenGL feature in which you render your scene in a so-called &#8220;selection mode&#8221;. In selection mode, OpenGL does not render to the frame buffer but gathers depths information of the objects with is later read at the point where your picking occurs. The second approach is called &#8220;Ray Picking&#8221; and is independent from OpenGL and simulates a line that is shot through the scene until it hits an object. I personally prefer the second approach for the following reasons:</p>
<ul>
<li>You don&#8217;t need an additional render pass. A render pass can take some considerable amount of time and special clipping/culling code is required to only render the small part of the scene which you want to perform the picking on. In addition, you render a less complex scene (omitting textures, lighting, normals, etc) in selection mode if possible which can add a high degree of complexity to your drawing routines (imagine breaking up display lists). Ray picking obviously requires some code to trace the ray through your scene, but this is usually less complex.</li>
<li>Ray picking can be done independently from the rest of the game code as a simultaneous and &#8220;read-only&#8221; operation on the scene graph. In some environments such as AWT/JOGL where the input devices are handled by a separate thread, your picking code can run as part of the handler thread and does not impact the performance of the rest of the game. This comes in handy especially in scenarios where you need to perform picking very often (eg. mouse hovering, target laser on the in-game gun).</li>
<li>In pretty much all Java environments, calling OpenGL takes a considerable amount of time due to JNI. It is therefore beneficial to minimize the number OpenGL calls. Rendering in selection mode would mean additional OpenGL calls that I rather substitute with ray picking.</li>
</ul>
<p>However, there are scenarios where ray picking is simply inferior or impractical. Consider picking in a scene with a lot of small details (eg. tree with little branches and leaves) or if you need to know the individual polygon/triangle that you picked. I will therefore cover both approaches, starting with ray picking in this post.</p>
<p><a href="http://schabby.de/wp-content/uploads/2011/09/ray-packing.png"><img class="alignright" title="Picking by Ray Tracing" src="http://schabby.de/wp-content/uploads/2011/09/ray-packing.png" alt="Picking by Ray Tracing" width="465" height="289" /></a></p>
<h3>Ray Picking</h3>
<p>Ray picking is the process of shooting a line (ie. &#8220;ray&#8221;) from the camera through the 2D viewscreen (where the 3D scene is projected on) into the scene until it hits an object. To do so, we need to know the camera and the point on the viewscreen (eg. mouse cursor position). The first point is trivial, however the second point is a bit more difficult to determine. Let&#8217;s say we have a 2D point on the viewscreen (x, y) and now want to map this point into world coordinates. One way would be to make use of the inverted viewing matrix, but inverting a matrix is usually an expensive undertaking. Instead, we compute the position of the plane of the viewscreen in world space and map the 2D point on this plane and from there into world space.</p>
<p>The algorithm in pseudo code follows below. Note that <tt>cameraLookAt</tt> is the 3D point where the camera looks at (as used on <tt>glLookat</tt>), <tt>cameraPosition</tt> is the current position of the camera in world space and <tt>cameraUp</tt> is the up vector of the camera.</p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">x = getMouseX() // scalar
y = getMouseY() // scalar

view = cameraLookAt - cameraPosition // 3D float vector
normalize view

h = crossProduct( view, cameraUp ) // 3D float vector
normalize h

v = crossProduct( h, view) // 3D float vector
normalize v</pre>
<p>ok, so far for <tt>h</tt> and <tt>v</tt>. Note that <tt>v</tt> is actually pointing in the same direction like the camera-up vector, but we compute it via the cross product to illustrate the method.</p>
<p>The vectors <tt>h</tt> and <tt>v</tt> are currently normalized to the length of 1. We now need to compute the lengths of <tt>h</tt> and <tt>v</tt> in the view port. In my example I assume that you set up your frustum by a field of view angle in degrees (<tt>fovy</tt>) as commonly the case with <tt>glFrustum</tt>. Let <tt>nearClippingDistance</tt> be the distance to the near clipping plane and <tt>width/height</tt> the ratio of the view port width divided by the viewport heigth.</tt></tt></p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">// convert fovy to radians
rad = fovy * PI / 180
vLength = tan( rad / 2 ) * nearClippingPlaneDistance
hLength = vLength * (width / height)

scale v by vLength
scale h by hLength</pre>
<p>The two scalars <tt>vLength</tt> and <tt>hLength</tt> in combinaton with <tt>v</tt> and <tt>h</tt> help spanning the view port plane extending from the center point. At this point it is worth mentioning that it may make sense to only compute <tt>h</tt> and <tt>v</tt> when you update the camera orientation because the normalization involves a square root which is a relatively expensive operation (actually <tt>tan</tt> may also be avoided).</p>
<p>Now it is time to map the 2D mouse coordinates onto the view port plane.</p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">// translate mouse coordinates so that the origin lies in the center
// of the view port
x -= width / 2
y -= height / 2

// scale mouse coordinates so that half the view port width and height
// becomes 1
y /= (height / 2)
x /= (width / 2)

// linear combination to compute intersection of picking ray with
// view port plane
pos = cameraPos + view + h*x + v*y

// compute direction of picking ray by subtracting intersection point
// with camera position
dir = pos - cameraPos</pre>
<p>That's pretty much it. We now got <tt>pos</tt> as the intersection point of the picking ray in the view port plane and the picking ray direction <tt>dir</tt>. This describes a line that can now be used to intersect with the individual objects of the scene.</p>
<pre style="border:1px gray solid;padding:5px;background-color:RGB(250, 250, 250);">// brute force
for all objects in the scene
  test for intersection and keep closest
end for</pre>
<p>So by intersecting the picking ray with all your visible objects in your scene while keeping track of the distance, you determine the object that is hit by your mouse cursor.<br />
Checking all objects in our scene may not be very efficient though. So in a first attempt I recommend to test only objects that lie (partly) within your viewing frustum. Under normal circumstances you got that code already in your scene graph as part of the software culling routine when pumping obects into the render pipeline.</p>
<p>I hope this helped. If you liked the post or if you got questions, ideas hints or found a problem, please leave a comment. I am trying to answer as quickly as possible. Also, if you want me to send you the source code of this example please let me know, I am happy to share the code.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/picking-opengl-ray-tracing/feed/</wfw:commentRss>
		<slash:comments>18</slash:comments>
		</item>
		<item>
		<title>LWJGL Example Base</title>
		<link>http://schabby.de/lwjgl-example-base/</link>
		<comments>http://schabby.de/lwjgl-example-base/#comments</comments>
		<pubDate>Thu, 25 Aug 2011 15:30:57 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=1006</guid>
		<description><![CDATA[LWJGL base class for most of the examples and tutorials in my blog.]]></description>
			<content:encoded><![CDATA[<p>This post contains the LWJGL base class that I use throughout the tutorials and examples. It basically sets up LWJGL and OpenGL which I otherwise would have to do over and over again in the individual examples. In addition, this makes the code in the tutorials and examples more readable by focusing on the important things.<br />
<span id="more-1006"></span><br />
So let&#8217;s dive into the code. I basically provide this class as a copy-paste template so that you have as little hassle as possible to set up the examples.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// import GL11 statically which makes the GL code</span>
<span style="color: #666666; font-style: italic;">// more readable</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">static</span> org.<span style="color: #006633;">lwjgl</span>.<span style="color: #006633;">opengl</span>.<span style="color: #006633;">GL11</span>.<span style="color: #339933;">*;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// some more LWJGL imports that I omitted for brevity.</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000000; font-weight: bold;">class</span> LwjglExampleBase 
<span style="color: #009900;">&#123;</span>
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** width of window */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> viewportWidth <span style="color: #339933;">=</span> <span style="color: #cc66cc;">800</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/** height of window */</span>
	<span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000066; font-weight: bold;">int</span> viewportHeight <span style="color: #339933;">=</span> <span style="color: #cc66cc;">600</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Rudimentary OpenGL setup with LWJGL. Use perspective
	 * projection with a field of view of 45 degrees. 
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setUp<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #666666; font-style: italic;">// set up LWJGL display</span>
		<span style="color: #000000; font-weight: bold;">try</span>
		<span style="color: #009900;">&#123;</span>
			Display.<span style="color: #006633;">setDisplayMode</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> DisplayMode<span style="color: #009900;">&#40;</span>viewportWidth, viewportHeight<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			Display.<span style="color: #006633;">create</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
		<span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>LWJGLException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
			e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			<span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		<span style="color: #666666; font-style: italic;">// init viewport and projection matrix</span>
		glViewport<span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, viewportWidth, viewportHeight<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glMatrixMode<span style="color: #009900;">&#40;</span>GL11.<span style="color: #006633;">GL_PROJECTION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glLoadIdentity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		GLU.<span style="color: #006633;">gluPerspective</span><span style="color: #009900;">&#40;</span> 45.0f, <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span>viewportWidth<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span>viewportHeight, 1.0f, 200.0f <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
		glMatrixMode<span style="color: #009900;">&#40;</span>GL11.<span style="color: #006633;">GL_MODELVIEW</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
		glEnable<span style="color: #009900;">&#40;</span>GL_DEPTH_TEST<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glShadeModel <span style="color: #009900;">&#40;</span>GL_SMOOTH<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		glClearColor <span style="color: #009900;">&#40;</span>0f, 0f, 0f, 0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #008000; font-style: italic; font-weight: bold;">/**
	 * Game loop is basically an infinite loop that runs
	 * until the window is closed. It calls the abstract
	 * render() method which is to be implemented by the
	 * specific example class. 
	 */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> gameLoop<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
	<span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>Display.<span style="color: #006633;">isCloseRequested</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
		<span style="color: #009900;">&#123;</span>
			glClear<span style="color: #009900;">&#40;</span>GL_COLOR_BUFFER_BIT <span style="color: #339933;">|</span> GL_DEPTH_BUFFER_BIT<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			glLoadIdentity<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			GLU.<span style="color: #006633;">gluLookAt</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">15</span>,<span style="color: #cc66cc;">0</span>, 
					<span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, 
					<span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
                        <span style="color: #666666; font-style: italic;">// call the abstract render method implemented by the</span>
                        <span style="color: #666666; font-style: italic;">// extending class (eg. example, tutorial, etc.)</span>
			render<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
&nbsp;
                        <span style="color: #666666; font-style: italic;">// sync to 60 frames per second</span>
			Display.<span style="color: #006633;">update</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
			Display.<span style="color: #006633;">sync</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">60</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
		<span style="color: #009900;">&#125;</span>
&nbsp;
		Display.<span style="color: #006633;">destroy</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #008000; font-style: italic; font-weight: bold;">/**
         * Abstract render method which is supposed to be
         * implemented by the extending tutorial or example
         * class.
         */</span>
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">abstract</span> <span style="color: #000066; font-weight: bold;">void</span> render<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getViewportWidth<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> viewportWidth<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setViewportWidth<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> viewportWidth<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">viewportWidth</span> <span style="color: #339933;">=</span> viewportWidth<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">int</span> getViewportHeight<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">return</span> viewportHeight<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> setViewportHeight<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">int</span> viewportHeight<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
		<span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">viewportHeight</span> <span style="color: #339933;">=</span> viewportHeight<span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s already it! Please make use of it as good as you can and leave a comment if you liked it or have questions.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/lwjgl-example-base/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lenovo ThinkPad Keyboard Removal</title>
		<link>http://schabby.de/lenovo-thinkpad-keyboard-removal/</link>
		<comments>http://schabby.de/lenovo-thinkpad-keyboard-removal/#comments</comments>
		<pubDate>Sat, 23 Jul 2011 14:48:09 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Dies Und Das]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=917</guid>
		<description><![CDATA[I am posting this little tutorial on how to remove the keyboard from a current Lenovo ThinkPad (in my case it&#8217;s a W510 Thinkpad) because the first time I tried this, I totally failed. Removing the screws on the back of the machine is the easiest part, but right after that I simply could not [...]]]></description>
			<content:encoded><![CDATA[<p>I am posting this little tutorial on how to remove the keyboard from a current Lenovo ThinkPad (in my case it&#8217;s a W510 Thinkpad) because the first time I tried this, I totally failed.<span id="more-917"></span> Removing the screws on the back of the machine is the easiest part, but right after that I simply could not get the keyboard off, even I closely followed Lenovos official guide which says your supposed to &#8220;push&#8221; the keyboard gently with two hands towards the screen. I searched the web for hours checking each tutorial and video I could find. Eventually, I found a side-note in some forum where a guy indirectly hinted to a different way. And here it comes (I took some pictures for illustration):</p>
<div style="height: 250px;"><strong>Step 1: Remove Screws</strong><br />
<a href="http://schabby.de/wp-content/uploads/2011/07/IMAG0330.jpg"><img class="alignright size-medium wp-image-919" title="Lenovo Thinkpad turned over" src="http://schabby.de/wp-content/uploads/2011/07/IMAG0330-300x179.jpg" alt="" width="300" height="179" /></a> Turn your Thinkpad so that it lays on the closed screen and look for the screws that have a little keyboard icon. Remove those screws. Note that you may have to remove the RAM plating first which covers an additional screw for the keyboard.</div>
<div style="height: 450px;"><strong>Step 2: Find Openings in Keyboard Border</strong><br />
<a href="http://schabby.de/wp-content/uploads/2011/07/IMAG0333.jpg"><img class="alignright size-medium wp-image-920" title="Thinpad Keyboard Right Gap" src="http://schabby.de/wp-content/uploads/2011/07/IMAG0333-300x179.jpg" alt="" width="300" height="179" /></a>Now turn over the ThinkPad again and open the screen. On the lower end of the keyboard you will notice tha the frame of the keyboard has little gaps. Find these gaps. There is one right below the arrow keys on the right hand side and one below the left alt-key. The pictures to the right shall help you understand what I mean <img src='http://schabby.de/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /><br />
<br />&nbsp;<br />&nbsp;<br />&nbsp;<br />
<a href="http://schabby.de/wp-content/uploads/2011/07/IMAG0334.jpg"><img class="alignright size-medium wp-image-921" title="Thinpad Keyboard Left Gap" src="http://schabby.de/wp-content/uploads/2011/07/IMAG0334-300x179.jpg" alt="" width="300" height="179" /></a></div>
<div style="height: 250px;"><strong>Step 3: Lever off Keyboard</strong><br />
<a href="http://schabby.de/wp-content/uploads/2011/07/IMAG0336.jpg"><img src="http://schabby.de/wp-content/uploads/2011/07/IMAG0336-300x179.jpg" alt="" title="Levering off keyboard" width="300" height="179" class="alignright size-medium wp-image-922" /></a>Now get a screwdriver and gently lever off the Keyboard by gently twisting the screwdriver on the opening. You will notice how easily the keyboars will come off. From there on you can handle the rest.
</div>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/lenovo-thinkpad-keyboard-removal/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>affiliate2go &#8211; iPhone App</title>
		<link>http://schabby.de/affiliate2go/</link>
		<comments>http://schabby.de/affiliate2go/#comments</comments>
		<pubDate>Thu, 31 Mar 2011 10:26:04 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Dies Und Das]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=903</guid>
		<description><![CDATA[Vor einigen Monaten entschlossen wir uns, eine iPhone App zu bauen mit der Publisher ihre Sales-Statistiken aus all ihren Partnernetzwerken beziehen können. Das ware die Geburtstunde für affiliate2go. Die Idee &#8220;Publisher Salesdaten aufs iPhone&#8221; ist nicht neu, neu ist aber, dass es nun eine einzelne App gibt, die mehrere Affiliate Netzwerke gleichzeitig anbindet. Bisher brauchte [...]]]></description>
			<content:encoded><![CDATA[<p>Vor einigen Monaten entschlossen wir uns, eine iPhone App zu bauen mit der Publisher ihre Sales-Statistiken aus all ihren Partnernetzwerken beziehen können. Das ware die Geburtstunde für <a href="http://www.ladenzeile.de/apps.html">affiliate2go</a>. <span id="more-903"></span>Die Idee &#8220;Publisher Salesdaten aufs iPhone&#8221; ist nicht neu, neu ist aber, dass es nun eine einzelne App gibt, die mehrere Affiliate Netzwerke gleichzeitig anbindet. Bisher brauchte man für jedes Affiliate Netzwerk jeweils eine App um an die eigenen Daten zu gelanden. Ausserdem benutzen andere Apps einen fremd-gehosteten Webservice um die Daten erst über einen Server und dann aufbereitet aufs iPhone in die App zu laden. Damit könnten die App-Entwickler die Daten ihrer Kunden sehr leicht mitlesen. affiliate2go geht dagegen direkt auf die API der Affiliate Netzwerke um so einen Mittelsmann auszuschliessen.</p>
<p><img style="float:left" src="http://cdn.ladenzeile.de/img/apps/splash_screen.png" alt="affiliate2go iPhone App" />Genau das macht affiliate2go zu einer technisch anspruchsvollen App: Sie muss die Eigenheiten der APIs der Affiliate Netwerke implementieren und dabei insbesondere die (leider nicht trivialen) Authorisationsmechaniken abbilden. Glücklicher Weise haben wir bei ladenzeile sehr viel Erfahrung mit dem Beziehen von Salesdaten aus Affiliate Netzwerken gewonnen, sodass wir das nötige Rüstzeug haben um eine solche App zu basteln.</p>
<p>Das aktuelle Release bindet zwei Große Affiliate Netzwerke an: Zanox und Affili.net. Weitere Netzwerke sind in der Planung. Bisher ist die App kostenlos. Wir wollen in der aktuellen Phase aber insbesondere Userfeedback sammeln, sodass wir uns sehr über alle möglichen Formen der Rückmeldung freuen.</p>
<p>Hier nochmal der Link zur App: <a href="http://www.ladenzeile.de/apps.html">affiliate2go von ladenzeile</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/affiliate2go/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Fly Me To The Moon</title>
		<link>http://schabby.de/fly-me-to-the-moon/</link>
		<comments>http://schabby.de/fly-me-to-the-moon/#comments</comments>
		<pubDate>Sat, 12 Mar 2011 17:50:00 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Dies Und Das]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=887</guid>
		<description><![CDATA[Es ist nun fast zehn Jahre her, dass ich ein Instrument länger als 15 Minuten in der Hand hielt. Damals spielte ich einer Punk-Rock Band und versuchte mich an einer Reihe von Instrumenten, bis man mich aus Qualifikationsgründen zum Sänger umschulte. Dann zog ich aus Göttingen nach Berlin und von Berlin nach China, was unserer [...]]]></description>
			<content:encoded><![CDATA[<p>Es ist nun fast zehn Jahre her, dass ich ein Instrument länger als 15 Minuten in der Hand hielt. Damals spielte ich einer Punk-Rock Band und versuchte mich an einer Reihe von Instrumenten, bis man mich aus Qualifikationsgründen zum Sänger umschulte. Dann zog ich aus Göttingen nach Berlin und von Berlin nach China, was unserer Band ein Ende setzte. Als ich aus China zurück kam und wieder nach Berlin zog, hatte ich kurzzeitig nochmal eine Band namens &#8220;Die Vier Eukalyptischen Reiter&#8221; in der wir in einer Drum-Piano-Bass Kombination einen eigenwilligen Jazz aufzuspielen wussten. <span id="more-887"></span>Wir mieteten einen Raum im <a href="http://www.orwohaus.de/">Orwo-Haus</a> und hätten Tag und Nacht 24/7 spielen können. Nichtsdestotrotz fanden wir aufgrund von Karriere, Freundin, S-Bahnstreik und fehlender Inspiration insgesamt nur drei Mal Gelegenheit für wenigen Minuten zu proben. Immerhin: die dabei entstanden Aufnahmen sind weltklasse, nicht jungenfre und nicht länger als 15 Minuten.</p>
<p>Vor diesem Hintergrund grenzt es eigentlich an ein Wunder, dass ich im fortgeschrittenen Alter von knapp 30 noch einmal ein Saiteninstrument in die Hand nahm. Aber gestern wars geschenen und ich musizierte wundebare Kammermusik für vier Personen. Als (mitleweile) Jazz und Swing-Fans widmeten wir uns dem alten Superhit &#8220;<a href="http://en.wikipedia.org/wiki/Fly_Me_to_the_Moon">Hit Fly Me To The Moon</a>&#8221; und spielten den Song so wie wir es vor zehn Jahren gelernt haben. Klar also, dass ich auch heute die Gesangslinie wieder singen musste.</p>
<p>Das Ergebnis kann hier heruntergeladen werden:</p>
<div style="font-size:20px;text-align:center"><a href='http://schabby.de/wp-content/uploads/2011/03/Fly-Me-To-The-Moon.mp3'>Fly Me To The Moon.mp3</a></div>
<p>Enjoy.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/fly-me-to-the-moon/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
<enclosure url="http://schabby.de/wp-content/uploads/2011/03/Fly-Me-To-The-Moon.mp3" length="1657207" type="audio/mpeg" />
		</item>
		<item>
		<title>Wunschzettel Ladenzeile Gewinnspiel</title>
		<link>http://schabby.de/wunschzettel-ladenzeile-gewinnspiel/</link>
		<comments>http://schabby.de/wunschzettel-ladenzeile-gewinnspiel/#comments</comments>
		<pubDate>Tue, 02 Nov 2010 14:37:56 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Dies Und Das]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=876</guid>
		<description><![CDATA[Natürlich muss ich hier an dieser Stelle auch unser Gewinnspiel promoten. Es werden 5&#215;500 Euro verlost plus ein Sonderpreis von 1000 Euro. Echt ärgerlich dass ich selbst nicht an meinem Gewinnspiel teilnehmen darf Das Wunschzettel Gewinnspiel: Wunschzettel posten und mit etwas Glück 500 EUR gewinnen. Hier mitmachen: Ladenzeile Wunschzettel Gewinnspiel Pingulina Hochbett mit Rutsche und [...]]]></description>
			<content:encoded><![CDATA[<p>Natürlich muss ich hier an dieser Stelle auch unser Gewinnspiel promoten. Es werden 5&#215;500 Euro verlost plus ein Sonderpreis von 1000 Euro. Echt ärgerlich dass ich selbst nicht an meinem Gewinnspiel teilnehmen darf <img src='http://schabby.de/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' />  <span id="more-876"></span></p>
<style>*{margin:0;border:none;padding:0;font-family:Arial;}#lz_wishlist{width:100%;height:100%;text-align:center}#lz_wishlist a{color:#1C4C7C;text-decoration:underline;}#lz_wishlist img{-moz-border-shadow:none;background:none;border:none;padding:0;}#lz_wishlist #lz_center{padding:0;text-align:left;margin:0 auto;background-color:white;font-family:Arial;width:398px;border:1px solid #1C4C7C;}#lz_wishlist .lz_item{padding:3px 0;border-bottom:1px solid #CCCCCC;}#lz_wishlist .lz_icon{float:left;width:40px;border:1px solid #CCCCCC;margin-right:5px;}#lz_wishlist .lz_itemContent{overflow:hidden;float:left;width:331px;}#lz_wishlist .lz_itemName{display:block;width:9999em;color:#1C4C7C;line-height:14px;font-size:12px;display:block;}#lz_wishlist .lz_itemCat{line-height:14px;font-size:10px;}#lz_wishlist .lz_itemPrice{line-height:14px;font-size:10px;}#lz_wishlist a#lz_footer{display:block;height:18px;}</style>
<div id="lz_wishlist">
<div id="lz_center"><img src="http://cdn.ladenzeile.de/img/wishlist/header.gif" height="100" width="398"/>
<div style="padding:10px;width:378px;">
<div style="font-size:11px;color:#333333;padding-bottom:10px;border-bottom:1px solid #CCCCCC">Das Wunschzettel Gewinnspiel: Wunschzettel posten und mit etwas Glück 500 EUR gewinnen. Hier mitmachen: <a target="_blank" href="http://www.ladenzeile.de/wunschzettel/" style="color:#1C4C7C">Ladenzeile Wunschzettel Gewinnspiel</a></div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/pingulina-hochbett-mit-rutsche-und-turm-i0002jtnf9.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://moebel.ladenzeile.de/pingulina-hochbett-mit-rutsche-und-turm-14117426.html">Pingulina Hochbett mit Rutsche und Turm</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://moebel.ladenzeile.de/baby-moebel-kindermoebel-kinderbetten-hochbetten/">Hochbetten</a></div>
<div class="lz_itemPrice">339,00 € * <span style="#999999">(Versand: 5,95 €)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/babybesteck-clown-wmf-i0002jj3co.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://moebel.ladenzeile.de/babybesteck-clown-wmf-2108223.html">Babybesteck CLOWN, WMF</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://moebel.ladenzeile.de/baby-moebel-kindermoebel-kinderbesteck/">Kinderbesteck</a></div>
<div class="lz_itemPrice">22,95 € * <span style="#999999">(Versand: 5,95 €)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/auerhahn-kinderbesteck-der-kleine-prinz-4tlg-auerhahn-i0002qgomg.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://moebel.ladenzeile.de/auerhahn-kinderbesteck-der-kleine-prinz-4tlg-auerhahn-16389844.html">Auerhahn Kinderbesteck DER KLEINE PRINZ &#8211; 4tlg., Auerhahn</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://moebel.ladenzeile.de/accessoires-besteck-besteckset/">Bestecksets</a></div>
<div class="lz_itemPrice">29,90 € * <span style="#999999">(Versand: 5,95 €)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/auerhahn-der-kleine-prinz-kinder-set-7-tlg-i0000yv04b.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://moebel.ladenzeile.de/auerhahn-der-kleine-prinz-kinder-set-7-tlg-9600932.html">Auerhahn Der kleine Prinz &#8211; Kinder-Set 7-tlg.</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://www.ladenzeile.de/auerhahn/">Auerhahn</a></div>
<div class="lz_itemPrice">42,90 € * <span style="#999999">(kostenloser Versand)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/steppjacke-exes-beere-i0001tgkcp.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://kindermode.ladenzeile.de/steppjacke-exes-beere-883142.html">Steppjacke, Exes, beere</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://kindermode.ladenzeile.de/maedchenmode-jacken-jacken/">Jacken für Mädchen</a></div>
<div class="lz_itemPrice">49,95 € * <span style="#999999">(Versand: 5,95 €)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/vorhaenge-fuer-hochbett-i0002ch8lr.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://moebel.ladenzeile.de/vorhaenge-fuer-hochbett-3211384.html">Vorhänge für Hochbett</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://moebel.ladenzeile.de/baby-moebel-kindermoebel-jugendzimmer/">Jugendzimmer</a></div>
<div class="lz_itemPrice">69,99 € * <span style="#999999">(Versand: 5,95 €)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/begehbares-anbauregal-silenta-made-in-germany-weiss-i0001uz0rp.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://moebel.ladenzeile.de/begehbares-anbauregal-silenta-made-in-germany-weiss-17282273.html">Begehbares Anbauregal, Silenta, Made in Germany, weiß</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://www.ladenzeile.de/silenta/">Silenta</a></div>
<div class="lz_itemPrice">349,00 € * <span style="#999999">(Versand: 5,95 €)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/silenta-hochbett-mit-treppe-made-in-germany-weiss-i0002leffr.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://moebel.ladenzeile.de/silenta-hochbett-mit-treppe-made-in-germany-weiss-21945931.html">Silenta, Hochbett mit Treppe, Made in Germany, weiß</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://moebel.ladenzeile.de/baby-moebel-kindermoebel-kinderbetten/">Kinderbetten</a></div>
<div class="lz_itemPrice">699,00 € * <span style="#999999">(Versand: 5,95 €)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/bsf-02214-210-0-kinderbesteck-4-teilig-eisbaer-i0001ncvxb.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://moebel.ladenzeile.de/bsf-02214-210-0-kinderbesteck-4-teilig-eisbaer-4161391.html">BSF 02214-210-0 Kinderbesteck 4-teilig Eisbär</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://www.ladenzeile.de/bsf/">BSF</a></div>
<div class="lz_itemPrice">20,00 € * <span style="#999999">(zuzüglich Versandkosten)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div class="lz_item">
<div class="lz_icon"><img src="http://cdn.ladenzeile.de/pyjama-dunkelgrau-gestreift-i0001tjjgp.jpg" height="40" width="40"/></div>
<div class="lz_itemContent"><a class="lz_itemName" target="_blank" href="http://kindermode.ladenzeile.de/pyjama-dunkelgrau-gestreift-597883.html">Pyjama, dunkelgrau/gestreift</a>
<div class="lz_itemCat">gefunden in <a target="_blank" style="color:#1C4C7C" href="http://kindermode.ladenzeile.de/maedchenmode-nachtwaesche-schlafanzug/">Mädchen-Schlafanzüge </a></div>
<div class="lz_itemPrice">16,99 € * <span style="#999999">(Versand: 5,95 €)</span></div>
</div>
<div style="clear:both"></div>
</div>
<div style="font-size:10px;margin-top:10px;color:#666666">* Preis kann sich seit der letzten Aktualisierung erhöht haben! <a target="_blank" rel="nofollow" style="color:#666666" href="http://www.ladenzeile.de/impressum.html#pricehint">Weitere Infos</a></div>
</div>
<p><a id="lz_footer" target="_blank" href="http://www.ladenzeile.de/"><img src="http://cdn.ladenzeile.de/img/wishlist/footer.gif" height="18" width="398"/></a></div>
</div>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/wunschzettel-ladenzeile-gewinnspiel/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>JOGL in Eclipse</title>
		<link>http://schabby.de/jogl-example-hello-world/</link>
		<comments>http://schabby.de/jogl-example-hello-world/#comments</comments>
		<pubDate>Sun, 11 Jul 2010 14:25:38 +0000</pubDate>
		<dc:creator>schabby</dc:creator>
				<category><![CDATA[Java]]></category>
		<category><![CDATA[OpenGL]]></category>

		<guid isPermaLink="false">http://schabby.de/?p=858</guid>
		<description><![CDATA[This post describes how to set up JOGL (Java OpenGL Binding) in Eclipse on Windows. It provides example code to get started with JOGL and Eclipse and a video walk-through covering the setup.]]></description>
			<content:encoded><![CDATA[<p>This getting started-tutorial is intended for JOGL users that are mere beginners. It helps to setup a recent JOGL installation in Eclipse on Windows. There is a video-walk-through further down the post, in case you dont feel like reading.<br />
<span id="more-858"></span><br />
First you need to download JOGL. This is actually more difficult than it sounds because the precompiled binary packages are a bit hidden in the new project site. Fortunately, I figured out <a href="http://schabby.de/jogl-download/">where you can download recent JOGL binaries</a>.</p>
<p>For this tutorial, I am using</p>
<ul>
<li><tt>jogl-b633-2012-01-23_20-37-13</tt>, and</li>
<li><tt>gluegen-b480-2012-01-23_16-49-04</tt>.</li>
</ul>
<p>This is how your project setup should look like:</p>
<p><a href="http://schabby.de/wp-content/uploads/2010/07/JOGLProjectSetup.png"><img src="http://schabby.de/wp-content/uploads/2010/07/JOGLProjectSetup.png" alt="JOGL Project Setup" title="JOGL Project Setup" width="310" height="346" class="aligncenter size-full wp-image-1033" /></a></p>
<p>So one you got the binaries, unpack the ZIP file somewhere. You will notice the <tt>jar</tt> folder with a number of JAR files. All you need is in that <tt>jar</tt> folder, because the DLL binaries are included in the JARs.</p>
<p>Create a new eclipse project (name doesnt matter) and add the following JARs on the classpath:</p>
<ul>
<li><tt>gluegen-rt-natives-windows-amd64.jar</tt></li>
<li><tt>gluegen-rt.jar</tt></li>
<li><tt>jogl-all-natives-windows-amd64.jar</tt></li>
<li><tt>gluegen-rt.jar</tt></li>
</ul>
<p>These files are the native part of the OpenGL binding and are linked via Java Native Interface (JNI). Since the DLLs are precompiled as part of the JAR files, you are almost ready to go.</p>
<p>Once you got that set up, create a package <tt>de.schabby.jogl.helloworld</tt> and copypaste the following two classes in the newly created package.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">de.schabby.jogl.helloworld</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.WindowAdapter</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">java.awt.event.WindowEvent</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLCapabilities</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLProfile</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.awt.GLCanvas</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.swing.JFrame</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> HelloWorld 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> main<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> args<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #666666; font-style: italic;">// setup OpenGL Version 2</span>
    	GLProfile profile <span style="color: #339933;">=</span> GLProfile.<span style="color: #006633;">get</span><span style="color: #009900;">&#40;</span>GLProfile.<span style="color: #006633;">GL2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	GLCapabilities capabilities <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLCapabilities<span style="color: #009900;">&#40;</span>profile<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    	<span style="color: #666666; font-style: italic;">// The canvas is the widget that's drawn in the JFrame</span>
    	GLCanvas glcanvas <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLCanvas<span style="color: #009900;">&#40;</span>capabilities<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	glcanvas.<span style="color: #006633;">addGLEventListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Renderer</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    	glcanvas.<span style="color: #006633;">setSize</span><span style="color: #009900;">&#40;</span> <span style="color: #cc66cc;">300</span>, <span style="color: #cc66cc;">300</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #003399;">JFrame</span> frame <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">JFrame</span><span style="color: #009900;">&#40;</span> <span style="color: #0000ff;">&quot;Hello World&quot;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        frame.<span style="color: #006633;">getContentPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span> glcanvas<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #666666; font-style: italic;">// shutdown the program on windows close event</span>
        frame.<span style="color: #006633;">addWindowListener</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">WindowAdapter</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> windowClosing<span style="color: #009900;">&#40;</span><span style="color: #003399;">WindowEvent</span> ev<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #003399;">System</span>.<span style="color: #006633;">exit</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        frame.<span style="color: #006633;">setSize</span><span style="color: #009900;">&#40;</span> frame.<span style="color: #006633;">getContentPane</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getPreferredSize</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        frame.<span style="color: #006633;">setVisible</span><span style="color: #009900;">&#40;</span> <span style="color: #000066; font-weight: bold;">true</span> <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>The <rr>Render.java</tt> class looks like follows. It implements <tt>GLEventListener</tt> which is the call-back implementaiton by JOGL to do all OpenGL rendering.</p>

<div class="wp_syntax"><div class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">package</span> <span style="color: #006699;">de.schabby.jogl.helloworld</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GL2</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLAutoDrawable</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.GLEventListener</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">import</span> <span style="color: #006699;">javax.media.opengl.glu.GLU</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">class</span> <span style="color: #003399;">Renderer</span> <span style="color: #000000; font-weight: bold;">implements</span> GLEventListener 
<span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> GLU glu <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> GLU<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> display<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">final</span> GL2 gl <span style="color: #339933;">=</span> gLDrawable.<span style="color: #006633;">getGL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGL2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glClear</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_COLOR_BUFFER_BIT</span> <span style="color: #339933;">|</span> GL2.<span style="color: #006633;">GL_DEPTH_BUFFER_BIT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glLoadIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glTranslatef</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.5f, 0.0f, <span style="color: #339933;">-</span>6.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glBegin</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_TRIANGLES</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>		
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>0.0f, 1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glEnd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
        gl.<span style="color: #006633;">glTranslatef</span><span style="color: #009900;">&#40;</span>3.0f, 0.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glBegin</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_QUADS</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>           	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.0f, 1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>1.0f, 1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glVertex3f</span><span style="color: #009900;">&#40;</span><span style="color: #339933;">-</span>1.0f, <span style="color: #339933;">-</span>1.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>	
        gl.<span style="color: #006633;">glEnd</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>				
        gl.<span style="color: #006633;">glFlush</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> displayChanged<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable, <span style="color: #000066; font-weight: bold;">boolean</span> modeChanged, <span style="color: #000066; font-weight: bold;">boolean</span> deviceChanged<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;displayChanged called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> init<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;init() called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        GL2 gl <span style="color: #339933;">=</span> gLDrawable.<span style="color: #006633;">getGL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGL2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glClearColor</span><span style="color: #009900;">&#40;</span>0.0f, 0.0f, 0.0f, 0.0f<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glShadeModel</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_FLAT</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> reshape<span style="color: #009900;">&#40;</span>GLAutoDrawable gLDrawable, <span style="color: #000066; font-weight: bold;">int</span> x, <span style="color: #000066; font-weight: bold;">int</span> y, <span style="color: #000066; font-weight: bold;">int</span> width, <span style="color: #000066; font-weight: bold;">int</span> height<span style="color: #009900;">&#41;</span> 
    <span style="color: #009900;">&#123;</span>
    	<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;reshape() called: x = &quot;</span><span style="color: #339933;">+</span>x<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, y = &quot;</span><span style="color: #339933;">+</span>y<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, width = &quot;</span><span style="color: #339933;">+</span>width<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;, height = &quot;</span><span style="color: #339933;">+</span>height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">final</span> GL2 gl <span style="color: #339933;">=</span> gLDrawable.<span style="color: #006633;">getGL</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">getGL2</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>height <span style="color: #339933;">&lt;=</span> <span style="color: #cc66cc;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">// avoid a divide by zero error!</span>
        <span style="color: #009900;">&#123;</span>
            height <span style="color: #339933;">=</span> <span style="color: #cc66cc;">1</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
&nbsp;
        <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">float</span> h <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> width <span style="color: #339933;">/</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">float</span><span style="color: #009900;">&#41;</span> height<span style="color: #339933;">;</span>
&nbsp;
        gl.<span style="color: #006633;">glViewport</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">0</span>, <span style="color: #cc66cc;">0</span>, width, height<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glMatrixMode</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_PROJECTION</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glLoadIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        glu.<span style="color: #006633;">gluPerspective</span><span style="color: #009900;">&#40;</span>45.0f, h, <span style="color: #cc66cc;">1.0</span>, <span style="color: #cc66cc;">20.0</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glMatrixMode</span><span style="color: #009900;">&#40;</span>GL2.<span style="color: #006633;">GL_MODELVIEW</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        gl.<span style="color: #006633;">glLoadIdentity</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
&nbsp;
	<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> dispose<span style="color: #009900;">&#40;</span>GLAutoDrawable arg0<span style="color: #009900;">&#41;</span> 
	<span style="color: #009900;">&#123;</span>
		<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;dispose() called&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
	<span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>By right-clicking on the <tt>main()</tt> method and chosing "run as application", Eclipse will execute the program. You should see something like this:</p>
<p><a href="http://schabby.de/wp-content/uploads/2010/07/JoglHelloWorld.png"><img src="http://schabby.de/wp-content/uploads/2010/07/JoglHelloWorld.png" alt="Jogle Hello World" title="Jogle Hello World" width="320" height="300" class="aligncenter size-full wp-image-1034" /></a></p>
<p>For the lazy, I provide a video-walkthough.<br />
<iframe width="620" height="415" src="http://www.youtube.com/embed/2EaaQrgvsL0" frameborder="0" allowfullscreen></iframe></p>
<p>Common problems and solutions:</p>
<ul>
<li>If you get <tt>Can't load AMD 64-bit .dll on a IA 32-bit platform</tt>, then you are using an 64bit DLL (eg. gluegen-rt.dee) with an JRE that is 32bit. In most cases you can just switch to a 64bit JRE if you are running a 64bit system. Make sure that Eclipse is using the right JRE when you execute the code.</li>
<li>If you get <tt>no gluegen-rt in java.library.path</tt>, then you may need to add <tt>gluegen-rt-natives-windows-amd64.jar</tt> to you classpath.</li>
<li>If you get <tt>no nativewindow_awt in java.library.path</tt>, then you may need to add <tt>jogl-all-natives-windows-amd64.jar</tt> to you classpath.</li>
</ul>
<p>That's it so far. Please let me know if you have questions or comments.</p>
]]></content:encoded>
			<wfw:commentRss>http://schabby.de/jogl-example-hello-world/feed/</wfw:commentRss>
		<slash:comments>27</slash:comments>
		</item>
	</channel>
</rss>

