<?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>SlashCode &#187; OpenGL</title>
	<atom:link href="http://davideaversa.it/slashcode/category/guide/opengl-guide/feed/" rel="self" type="application/rss+xml" />
	<link>http://davideaversa.it/slashcode</link>
	<description></description>
	<lastBuildDate>Thu, 03 May 2012 14:28:53 +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>OpenGL4 Quick Reference Card</title>
		<link>http://davideaversa.it/slashcode/2010/05/opengl4-quick-reference-card/</link>
		<comments>http://davideaversa.it/slashcode/2010/05/opengl4-quick-reference-card/#comments</comments>
		<pubDate>Wed, 19 May 2010 09:53:37 +0000</pubDate>
		<dc:creator>THeK3nger</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[opengl]]></category>

		<guid isPermaLink="false">http://davideaversa.it/slashcode/?p=578</guid>
		<description><![CDATA[Mi sono ritrovato nella cartella Download del PC questo Pdf. Non mi ricordo dove l&#8217;ho preso, in ogni caso voglio rendervi partecipi. In questo PDF sono elencate in modo sintetico tutte (o quasi) le funzioni di OpenGL e la relativa sintassi. Uno strumento utilissimo per chi programma in OpenGL e, ovviamente, non ricorda la particolare <a href='http://davideaversa.it/slashcode/2010/05/opengl4-quick-reference-card/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl4-quick-reference-card%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_google1" style="width:90px;"><div class="g-plusone" data-size="medium" data-href="http://davideaversa.it/slashcode/2010/05/opengl4-quick-reference-card/" ></div></div><div class="really_simple_share_twitter" style="width:110px;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="OpenGL4 Quick Reference Card" data-url="http://davideaversa.it/slashcode/2010/05/opengl4-quick-reference-card/" 
						data-via=""  ></a></div></div>
		<div style="clear:both;"></div><p>Mi sono ritrovato nella cartella Download del PC questo Pdf. Non mi ricordo dove l&#8217;ho preso, in ogni caso voglio rendervi partecipi.</p>
<p>In questo PDF sono elencate in modo sintetico tutte (o quasi) le funzioni di OpenGL e la relativa sintassi. Uno strumento utilissimo per chi programma in OpenGL e, ovviamente, non ricorda la particolare sintassi di una particolare funzione!</p>
<p>Ecco qui di seguito il pratico download! <img src='http://davideaversa.it/slashcode/wp-includes/images/smilies/icon_biggrin.gif' alt=':D' class='wp-smiley' /> </p>
<p><a href="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/opengl4-quick-reference-card.pdf">OpenGL4 Quick Reference Card</a></p>
 <p><a href="http://davideaversa.it/slashcode/?flattrss_redirect&amp;id=578&amp;md5=af43485de91ce4208b708b9ba778bbac" title="Flattr" target="_blank"><img src="http://davideaversa.it/slashcode/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://davideaversa.it/slashcode/2010/05/opengl4-quick-reference-card/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=thek3nger&amp;popout=1&amp;url=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl4-quick-reference-card%2F&amp;language=it_IT&amp;category=text&amp;title=OpenGL4+Quick+Reference+Card&amp;description=Mi+sono+ritrovato+nella+cartella+Download+del+PC+questo+Pdf.+Non+mi+ricordo+dove+l%26%238217%3Bho+preso%2C+in+ogni+caso+voglio+rendervi+partecipi.+In+questo+PDF+sono+elencate+in+modo+sintetico...&amp;tags=opengl%2Cblog" type="text/html" />
	</item>
		<item>
		<title>OpenGL &#8211; Modellare con gli Array</title>
		<link>http://davideaversa.it/slashcode/2010/05/opengl-modellare-con-gli-array/</link>
		<comments>http://davideaversa.it/slashcode/2010/05/opengl-modellare-con-gli-array/#comments</comments>
		<pubDate>Tue, 18 May 2010 10:28:12 +0000</pubDate>
		<dc:creator>THeK3nger</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://davideaversa.it/slashcode/?p=574</guid>
		<description><![CDATA[<div><a href="http://davideaversa.it/slashcode/2010/05/opengl-modellare-con-gli-array/"><img title="OpenGL &#8211; Modellare con gli Array" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/ArcaneArcher-150x150.jpg" alt="OpenGL &#8211; Modellare con gli Array"  width="200" height="200" /></a></div><br/>Abbiamo visto come si può disegnare qualsivoglia figura inserendo i vari vertici della figura manualmente. Abbiamo visto anche che prima di inserire un vertice dobbiamo prepararlo impostando, ad esempio, il colore, il vettore normale (vedremo a cosa serve quando parleremo della luce), le texture e via dicendo. Questo può sembrarvi estremamente macchinoso&#8230; e lo è: <a href='http://davideaversa.it/slashcode/2010/05/opengl-modellare-con-gli-array/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl-modellare-con-gli-array%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_google1" style="width:90px;"><div class="g-plusone" data-size="medium" data-href="http://davideaversa.it/slashcode/2010/05/opengl-modellare-con-gli-array/" ></div></div><div class="really_simple_share_twitter" style="width:110px;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="OpenGL &#8211; Modellare con gli Array" data-url="http://davideaversa.it/slashcode/2010/05/opengl-modellare-con-gli-array/" 
						data-via=""  ></a></div></div>
		<div style="clear:both;"></div><p><img class="alignleft size-thumbnail wp-image-575" title="ArcaneArcher" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/ArcaneArcher-150x150.jpg" alt="" width="150" height="150" />Abbiamo visto come si può disegnare qualsivoglia figura inserendo i vari vertici della figura manualmente. Abbiamo visto anche che prima di inserire un vertice dobbiamo prepararlo impostando, ad esempio, il colore, il vettore normale (vedremo a cosa serve quando parleremo della luce), le texture e via dicendo. Questo può sembrarvi estremamente macchinoso&#8230; e lo è: dobbiamo usare circa 3-4 istruzioni per inserire ogni vertice! Un metodo decisamente inadatto per inserire oggetti formati da centinaia di vertici!</p>
<p>Anche volendo disegnare un semplice cubo (quindi con la miseria di 8 vertici) ci rendiamo conto che ogni vertice andrebbe specificato ben 3 volte (uno per ogni faccia a cui appartiene) con inutile spreco di prestazioni!</p>
<p>Ovviamente esiste la soluzione: usare gli array. Tramite gli array possiamo inserire con una manciata di comandi intere liste di vertici. Ma andiamo per passi.</p>
<p><strong>1 &#8211; ABILITARE GLI ARRAY</strong></p>
<p>Come gran parte degli stati di OpenGL gli array sono disabilitati di default. Esistono 8 diversi array da abilitare:</p>
<ul>
<li><code class="codecolorer text dawn"><span class="text">GL_VERTEX_ARRAY</span></code></li>
<li><code class="codecolorer text dawn"><span class="text">GL_COLOR_ARRAY</span></code></li>
<li><code class="codecolorer text dawn"><span class="text">GL_SECOND_COLOR_ARRAY</span></code></li>
<li><code class="codecolorer text dawn"><span class="text">GL_INDEX_ARRAY</span></code></li>
<li><code class="codecolorer text dawn"><span class="text">GL_NORMAL_ARRAY</span></code></li>
<li><code class="codecolorer text dawn"><span class="text">GL_FOG_COORDINATE_ARRAY</span></code></li>
<li><code class="codecolorer text dawn"><span class="text">GL_TEXTURE_COORDINATE_ARRAY</span></code></li>
<li><code class="codecolorer text dawn"><span class="text">GL_EDGE_FLAG_ARRAY</span></code></li>
</ul>
<p>Ovviamente non ci serviranno tutte adesso e possiamo limitarci, ad esempio, solamente a vertici e colori. Possiamo quindi abilitare gli array che ci interessano con <code class="codecolorer text dawn"><span class="text">glEnableClientState()</span></code> come abbiamo visto in precedenza.</p>
<p><strong>2 &#8211; INSERIRE DATI NEGLI ARRAY</strong></p>
<p>Una volta che gli array sono abilitati bisogna inserire dei dati al loro interno. Esiste una funzione per ogni tipo di array. Noi però per il momento vedremo solamente le funzioni che ci interessano:</p>
<ul>
<li><code class="codecolorer text dawn"><span class="text">glVertexPointer(size, type, stride, pointer)</span></code> è la funzione che permette di inserire un vettore all&#8217;interno dell&#8217;array dei vertici. <code class="codecolorer text dawn"><span class="text">size</span></code> è semplicemente il numero di coordinate per vertice (può valere solo 2,3 o 4). <code class="codecolorer text dawn"><span class="text">type</span></code> indica il tipo con cui sono specificati i vertici (GL_INT_, GL_FLOAT e così via). <code class="codecolorer text dawn"><span class="text">stride</span></code> è un valore che indica l&#8217;offeset fra vertici consecutivi all&#8217;interno del vettore. Questo parametro è solitamente 0 per array che contengono solamente vertici. <code class="codecolorer text dawn"><span class="text">pointer</span></code> è invece il puntatore al vettore che contiene i vertici da inserire nell&#8217;array.</li>
<li><code class="codecolorer text dawn"><span class="text">glColorPointer(size, type, stride, pointer)</span></code> è analoga a sopra ma serve per impostare il vettore dei colori.</li>
</ul>
<p><strong>3 &#8211; UTILIZZARE GLI ARRAY</strong></p>
<p>Esistono diverse funzioni per utilizzare i dati presenti negli array. La prima funzione base è <code class="codecolorer text dawn"><span class="text">glArrayElement(k)</span></code>. Questa funzione non fa altro che inserire il k-esimo elemento dell&#8217;array. Facciamo un esempio. Supponiamo di avere due array <em>vertex </em>e <em>color</em> correttamente inizializzati e inseriti negli array di OpenGL.</p>
<p>A questo punto i due codici sono equivalenti:</p>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">glBegin<span style="color: black;">&#40;</span>GL_TRIANGLES<span style="color: black;">&#41;</span><br />
glArrayElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span><br />
glArrayElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: black;">&#41;</span><br />
glArrayElement<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span><br />
glEnd<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">glBegin<span style="color: black;">&#40;</span>GL_TRIANGLES<span style="color: black;">&#41;</span><br />
glColor3f<span style="color: black;">&#40;</span>color<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>*<span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> color<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>*<span style="color: #ff4500;">3</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> color<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>*<span style="color: #ff4500;">3</span>+<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
glVertex2f<span style="color: black;">&#40;</span>color<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>*<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> color<span style="color: black;">&#91;</span><span style="color: #ff4500;">2</span>*<span style="color: #ff4500;">2</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
glColor3f<span style="color: black;">&#40;</span>color<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span>*<span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> color<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span>*<span style="color: #ff4500;">3</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> color<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span>*<span style="color: #ff4500;">3</span>+<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
glVertex2f<span style="color: black;">&#40;</span>color<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span>*<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> color<span style="color: black;">&#91;</span><span style="color: #ff4500;">3</span>*<span style="color: #ff4500;">2</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
glColor3f<span style="color: black;">&#40;</span>color<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span>*<span style="color: #ff4500;">3</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> color<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span>*<span style="color: #ff4500;">3</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> color<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span>*<span style="color: #ff4500;">3</span>+<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
glVertex2f<span style="color: black;">&#40;</span>color<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span>*<span style="color: #ff4500;">2</span><span style="color: black;">&#93;</span><span style="color: #66cc66;">,</span> color<span style="color: black;">&#91;</span><span style="color: #ff4500;">5</span>*<span style="color: #ff4500;">2</span> + <span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
glEnd<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>Come potete vedere c&#8217;è un bel risparmio. Notate anche che il vertice k-esimo viene colorato con il colore k-esimo.</p>
<p>La seconda funzione che vediamo ci permette di semplificare il tutto ancora di più. Tale funzione è <code class="codecolorer text dawn"><span class="text">glDrawElements(mode, count, type, indices)</span></code>. Questa funzione è del tutto equivalente a questo codice:</p>
<pre>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">glBegin<span style="color: black;">&#40;</span>mode<span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>count<span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; glArrayElement<span style="color: black;">&#40;</span>indices<span style="color: black;">&#91;</span>i<span style="color: black;">&#93;</span><span style="color: black;">&#41;</span><br />
glEnd<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
</pre>
<p>Il parametro <code class="codecolorer text dawn"><span class="text">indices</span></code>, come avrete notato, è un array di indici. Se indices contiene [0, 4, 6] verranno inseriti in sequenza il vertice 0, 4 e 6 contenuto nell&#8217;array di OpenGL. Notate anche che <strong>è un errore inserire glDrawElements() all&#8217;interno di un blocco glBegin/glEnd.</strong></p>
<p>Una terza funzione è <code class="codecolorer text dawn"><span class="text">glDrawArray(mode, first, count)</span></code>. Questa funzione è equivalente a:</p>
<pre>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">glBegin<span style="color: black;">&#40;</span>mode<span style="color: black;">&#41;</span><br />
<span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span>count<span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; glArrayElement<span style="color: black;">&#40;</span>first + i<span style="color: black;">&#41;</span><br />
glEnd<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
</pre>
<p>La differenza principale con glDrawElements riguarda la scelta degli indici: mentre glDrawElements permette di campionare gli indici in ordine sparso all&#8217;interno dell&#8217;array di OpenGL, glDrawArray permette solamente di disegnare un gruppo di vertici contiguo.</p>
<p>Per il momento basta così. Vi lascio con un piccolissimo esempio pratico:</p>
<pre>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: #dc143c;">GL</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: black;">GLU</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: black;">GLUT</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; glClearColor<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glShadeModel<span style="color: black;">&#40;</span>GL_SMOOTH<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #dc143c;">test</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; glutInit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> display<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; glClear<span style="color: black;">&#40;</span>GL_COLOR_BUFFER_BIT<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; glEnableClientState<span style="color: black;">&#40;</span>GL_VERTEX_ARRAY<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glEnableClientState<span style="color: black;">&#40;</span>GL_COLOR_ARRAY<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; vertici <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">100</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">325</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">175</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">175</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">325</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">250</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">325</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">325</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; colori <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.2</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0.2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0.8</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.2</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0.75</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.75</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.75</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0.35</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.35</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.35</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.5</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.5</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; glColorPointer<span style="color: black;">&#40;</span><span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> GL_FLOAT<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> colori<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glVertexPointer<span style="color: black;">&#40;</span><span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> GL_INT<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> vertici<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; indici <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">2</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">3</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">4</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">5</span><span style="color: black;">&#93;</span><br />
<br />
&nbsp; &nbsp; glDrawElements<span style="color: black;">&#40;</span>GL_TRIANGLES<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">6</span><span style="color: #66cc66;">,</span> GL_UNSIGNED_BYTE<span style="color: #66cc66;">,</span> indici<span style="color: black;">&#41;</span> <br />
<br />
&nbsp; &nbsp; glDisableClientState<span style="color: black;">&#40;</span>GL_COLOR_ARRAY<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glDisableClientState<span style="color: black;">&#40;</span>GL_VERTEX_ARRAY<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glFlush<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> reshape<span style="color: black;">&#40;</span>w<span style="color: #66cc66;">,</span> h<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; glViewport<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> w<span style="color: #66cc66;">,</span> h<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glMatrixMode<span style="color: black;">&#40;</span>GL_PROJECTION<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLoadIdentity<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; gluOrtho2D<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> w<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> h<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'__main__'</span>:<br />
&nbsp; &nbsp; glutInit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitDisplayMode<span style="color: black;">&#40;</span>GLUT_SINGLE | GLUT_RGB<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitWindowSize<span style="color: black;">&#40;</span><span style="color: #ff4500;">350</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">350</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitWindowPosition<span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutCreateWindow<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Array: Due Triangoli&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutDisplayFunc<span style="color: black;">&#40;</span>display<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutReshapeFunc<span style="color: black;">&#40;</span>reshape<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutMainLoop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
</pre>
<p>L&#8217;esempio è piuttosto semplice alla luce della spiegazione.</p>
<p>Con questo si conclude la prima parte del corso dedicata ai vertici. Nella prossima vedremo come manipolare la &#8220;telecamera&#8221; e gli oggetti. Analizzeremo quindi i 3 aspetti fondamentali della visualizzazione: composizione, inquadratura e proiezione.</p>
<p>Vi ricordo che per ulteriori chiarimenti sono sempre disponibile nei commenti, su <a href="http://www.linuxqualityhelp.it/">LQH</a> e se ci sono potete abusare di me all&#8217;indirizzo Jabber: <strong>thek3nger@jabber.linux.it</strong></p>
 <p><a href="http://davideaversa.it/slashcode/?flattrss_redirect&amp;id=574&amp;md5=dcf03d070c0ed3f9cb5a7743f7fe180b" title="Flattr" target="_blank"><img src="http://davideaversa.it/slashcode/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://davideaversa.it/slashcode/2010/05/opengl-modellare-con-gli-array/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=thek3nger&amp;popout=1&amp;url=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl-modellare-con-gli-array%2F&amp;language=it_IT&amp;category=text&amp;title=OpenGL+%26%238211%3B+Modellare+con+gli+Array&amp;description=Abbiamo+visto+come+si+pu%C3%B2+disegnare+qualsivoglia+figura+inserendo+i+vari+vertici+della+figura+manualmente.+Abbiamo+visto+anche+che+prima+di+inserire+un+vertice+dobbiamo+prepararlo+impostando%2C+ad+esempio%2C+il...&amp;tags=opengl%2Cpython%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Libro: Fondamenti di Grafica Tridimensionale</title>
		<link>http://davideaversa.it/slashcode/2010/05/libro-fondamenti-di-grafica-tridimensionale/</link>
		<comments>http://davideaversa.it/slashcode/2010/05/libro-fondamenti-di-grafica-tridimensionale/#comments</comments>
		<pubDate>Mon, 17 May 2010 12:05:07 +0000</pubDate>
		<dc:creator>THeK3nger</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[3d]]></category>
		<category><![CDATA[libri]]></category>

		<guid isPermaLink="false">http://davideaversa.it/slashcode/?p=572</guid>
		<description><![CDATA[<div><a href="http://davideaversa.it/slashcode/2010/05/libro-fondamenti-di-grafica-tridimensionale/"><img title="Libro: Fondamenti di Grafica Tridimensionale" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/fdgti.jpeg" alt="Libro: Fondamenti di Grafica Tridimensionale"  width="142" height="200" /></a></div><br/>Passato il week-end posso rimettermi al lavoro per scrivere l&#8217;ultima parte del primo capitolo del nostro corso introduttivo alle OpenGL. Nel frattempo però voglio suggerirvi un libro interessante e conciso per capire le meccaniche alla base di ogni sistema grafico tridimensionale. Il libro non si basa su nessuna libreria ma spiega in modo molto semplice <a href='http://davideaversa.it/slashcode/2010/05/libro-fondamenti-di-grafica-tridimensionale/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Flibro-fondamenti-di-grafica-tridimensionale%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_google1" style="width:90px;"><div class="g-plusone" data-size="medium" data-href="http://davideaversa.it/slashcode/2010/05/libro-fondamenti-di-grafica-tridimensionale/" ></div></div><div class="really_simple_share_twitter" style="width:110px;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Libro: Fondamenti di Grafica Tridimensionale" data-url="http://davideaversa.it/slashcode/2010/05/libro-fondamenti-di-grafica-tridimensionale/" 
						data-via=""  ></a></div></div>
		<div style="clear:both;"></div><p><img class="size-full wp-image-573 alignleft" title="fondamenti di grafica" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/fdgti.jpeg" alt="" width="140" height="196" />Passato il week-end posso rimettermi al lavoro per scrivere l&#8217;ultima parte del primo capitolo del nostro corso introduttivo alle OpenGL. Nel frattempo però voglio suggerirvi un libro interessante e conciso per capire le meccaniche alla base di ogni sistema grafico tridimensionale.</p>
<p>Il libro non si basa su nessuna libreria ma spiega in modo molto semplice tutti i passi con cui un sistema digitale costruisce, gestisce e disegna su schermo un modello tridimensionale analizzando le varie fasi: modellazione, clipping, proiezione, colore, luce ecc. Un libro molto utile per poter avere una visione di insieme del mondo della grafica 3D mentre si studia una libreria specifica.</p>
<p>Il libro è scritto a quattro mani da Riccardo Scateni, Paolo Cignoni, Claudio Montani e Roberto Scopigno e ha un prezzo di circa 25€.</p>
<p>Se vi interessa il mondo della computer grafica è un libro assolutamente da non perdere.</p>
 <p><a href="http://davideaversa.it/slashcode/?flattrss_redirect&amp;id=572&amp;md5=dbde66db36155bd8bbb87e4b1bd64651" title="Flattr" target="_blank"><img src="http://davideaversa.it/slashcode/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://davideaversa.it/slashcode/2010/05/libro-fondamenti-di-grafica-tridimensionale/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=thek3nger&amp;popout=1&amp;url=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Flibro-fondamenti-di-grafica-tridimensionale%2F&amp;language=it_IT&amp;category=text&amp;title=Libro%3A+Fondamenti+di+Grafica+Tridimensionale&amp;description=Passato+il+week-end+posso+rimettermi+al+lavoro+per+scrivere+l%26%238217%3Bultima+parte+del+primo+capitolo+del+nostro+corso+introduttivo+alle+OpenGL.+Nel+frattempo+per%C3%B2+voglio+suggerirvi+un+libro+interessante+e+conciso...&amp;tags=3d%2Clibri%2Cblog" type="text/html" />
	</item>
		<item>
		<title>OpenGL &#8211; Stipple</title>
		<link>http://davideaversa.it/slashcode/2010/05/opengl-stipple/</link>
		<comments>http://davideaversa.it/slashcode/2010/05/opengl-stipple/#comments</comments>
		<pubDate>Wed, 12 May 2010 16:00:01 +0000</pubDate>
		<dc:creator>THeK3nger</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[glut]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://davideaversa.it/slashcode/?p=566</guid>
		<description><![CDATA[<div><a href="http://davideaversa.it/slashcode/2010/05/opengl-stipple/"><img title="OpenGL &#8211; Stipple" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/halftone-150x150.jpg" alt="OpenGL &#8211; Stipple"  width="200" height="200" /></a></div><br/>Questa è la penultima lezione del primo grande capitolo di OpenGL: il disegno di poligoni. Oggi parleremo dei &#8220;pattern&#8221; e di come istruire il motore grafico affinché non utilizzi un colore uniforme per disegnare righe e campiture ma colori soltanto i pixel che decidiamo noi. L&#8217;esempio più banale consiste nel voler disegnare una riga tratteggiata <a href='http://davideaversa.it/slashcode/2010/05/opengl-stipple/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl-stipple%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_google1" style="width:90px;"><div class="g-plusone" data-size="medium" data-href="http://davideaversa.it/slashcode/2010/05/opengl-stipple/" ></div></div><div class="really_simple_share_twitter" style="width:110px;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="OpenGL &#8211; Stipple" data-url="http://davideaversa.it/slashcode/2010/05/opengl-stipple/" 
						data-via=""  ></a></div></div>
		<div style="clear:both;"></div><p><img class="alignleft size-thumbnail wp-image-567" title="halftone" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/halftone-150x150.jpg" alt="" width="150" height="150" /> Questa è la penultima lezione del primo grande capitolo di OpenGL: il disegno di poligoni. Oggi parleremo dei &#8220;pattern&#8221; e di come istruire il motore grafico affinché non utilizzi un colore uniforme per disegnare righe e campiture ma colori soltanto i pixel che decidiamo noi. L&#8217;esempio più banale consiste nel voler disegnare una riga tratteggiata o di voler realizzare campiture in half-tone come quella nell&#8217;immagine dell&#8217;articolo.</p>
<p>Prima di entrare nel dettaglio facciamo un attimo il punto sulla struttura di OpenGL. OpenGL è una <strong>macchina a stati</strong>. Gli stati attivi influenzano il comportamento di OpenGL in modo sostanziale. Per attivare uno stato si utilizza il comando:</p>
<p><code class="codecolorer text dawn"><span class="text">glEnable(state)</span></code></p>
<p>e si disabilita con</p>
<p><code class="codecolorer text dawn"><span class="text">glDisable(state)</span></code></p>
<p>Dove <code class="codecolorer text dawn"><span class="text">state</span></code> è l&#8217;identificativo di uno dei possibili stati di OpenGL. Non elencherò adesso tutti gli stati possibili di OpenGL perché sono tantissimi ma ve li citerò nel momento del bisogno. Un avvertimento: <strong>non tenete tutti gli stati abilitati se non ne avete bisogno</strong> perché questo danneggia pesantemente le prestazioni. Se non usate la nebbia (<code class="codecolorer text dawn"><span class="text">GL_FOG</span></code>) è inutile che la attivate rendendo più pesante il processo di rendering.</p>
<p><strong>STIPPLE NELLE LINEE</strong></p>
<p>Cominciamo con le linee. Abbiamo visto come tracciare una linea (ad esempio tramite la modalità <code class="codecolorer text dawn"><span class="text">GL_LINE_STRIP</span></code>) e come modificare il colore di tale linea (tramita <code class="codecolorer text dawn"><span class="text">glColor()</span></code> e le sue varianti). Vediamo ora come effettuare il tratteggio.</p>
<p>La prima cosa da fare è abilitare lo stato <code class="codecolorer text dawn"><span class="text">GL_LINE_STIPPLE</span></code> nella macchina tramite il comando <em>enable</em>. Dopodiché possiamo utilizzare il pratico comando:</p>
<p><code class="codecolorer text dawn"><span class="text">glLineStipple(coefficente, pattern)</span></code></p>
<p>Questo comando imposta semplicemente lo stipple di una linea usando il tratteggio indicato in <em>pattern</em>. Ma come definiamo questo pattern? Tramite un numero esadecimale a 16bit. Qui bisogna fare un po&#8217; di attenzione ma esiste un metodo infallibile per ricavare il numero corrispondente al pattern dei vostri sogni.</p>
<ol>
<li>Per prima cosa disegnate il pattern su un foglio quadrettato utilizzando solamente 16 quadretti.</li>
<li>Sotto questo schema disegnate un 1 in corrispondenza dei quadratini anneriti e uno 0 in corrispondenza dei quadratini vuoti.  Ad esempio se voglio fare un pattern tratteggiato con 4pixel disegnati e 4 no avrò: 1111000011110000.</li>
<li>Ribaltate il numero binario che avete trovato. Io ad esempio ottengo 0000111100001111.</li>
<li>Raggruppate le cifre a gruppi di 4 e convertite il numero in esadecimale. Io ad esempio ottengo 0F0F.</li>
</ol>
<p>Se vi domandate perché vada ribaltato vi basti sapere che dipende solamente dal modo con cui vengono letti i numeri nei calcolatori.<br />
Il parametro <em>coefficente</em> è un semplice moltiplicatore. Se lo impostate ad 1 allora avrete identicamente il pattern che avete progettato, se lo impostate a 2 questo pattern verrò raddoppiato (ad esempio, nel mio caso, invece di 4pixel si e 4 no avrò 8pixel si e 8 no), con 3 triplicato e così via.</p>
<p>Ho questo esempio pronto:</p>
<pre>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: black;">GLUT</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: black;">GLU</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: #dc143c;">GL</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> drawLine<span style="color: black;">&#40;</span>x1<span style="color: #66cc66;">,</span>y1<span style="color: #66cc66;">,</span>x2<span style="color: #66cc66;">,</span>y2<span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; glBegin<span style="color: black;">&#40;</span>GL_LINES<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glVertex2f<span style="color: black;">&#40;</span>x1<span style="color: #66cc66;">,</span>y1<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glVertex2f<span style="color: black;">&#40;</span>x2<span style="color: #66cc66;">,</span>y2<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glEnd<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; glClearColor<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glShadeModel<span style="color: black;">&#40;</span>GL_FLAT<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> display<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; glClear<span style="color: black;">&#40;</span>GL_COLOR_BUFFER_BIT<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glColor3f<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; glEnable<span style="color: black;">&#40;</span>GL_LINE_STIPPLE<span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#Prima Riga</span><br />
&nbsp; &nbsp; glLineStipple<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x0101</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; drawLine<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">125</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">150</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">125</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLineStipple<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x00FF</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; drawLine<span style="color: black;">&#40;</span><span style="color: #ff4500;">150</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">125</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">250</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">125</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLineStipple<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x1C47</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; drawLine<span style="color: black;">&#40;</span><span style="color: #ff4500;">250</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">125</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">350</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">125</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#Seconda Riga</span><br />
&nbsp; &nbsp; glLineWidth<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLineStipple<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x0101</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; drawLine<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">150</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLineStipple<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x00FF</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; drawLine<span style="color: black;">&#40;</span><span style="color: #ff4500;">150</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">250</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLineStipple<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x1C47</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; drawLine<span style="color: black;">&#40;</span><span style="color: #ff4500;">250</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">350</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLineWidth<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#Terza Riga</span><br />
&nbsp; &nbsp; glLineStipple<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x1C47</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glBegin<span style="color: black;">&#40;</span>GL_LINE_STRIP<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">7</span><span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; &nbsp; &nbsp; glVertex2f<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span> + <span style="color: black;">&#40;</span>i * <span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">75</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glEnd<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#Quarta Riga</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">for</span> i <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">range</span><span style="color: black;">&#40;</span><span style="color: #ff4500;">6</span><span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; &nbsp; &nbsp; drawLine<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span> + <span style="color: black;">&#40;</span>i * <span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">50</span> + <span style="color: black;">&#40;</span>i+<span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span> * <span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">50</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#Quinta Riga</span><br />
&nbsp; &nbsp; glLineStipple<span style="color: black;">&#40;</span><span style="color: #ff4500;">5</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x1C47</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; drawLine<span style="color: black;">&#40;</span><span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">350</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: black;">&#41;</span><br />
<br />
&nbsp; &nbsp; glDisable<span style="color: black;">&#40;</span>GL_LINE_STIPPLE<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glFlush<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> reshape<span style="color: black;">&#40;</span>w<span style="color: #66cc66;">,</span>h<span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; glViewport<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> w<span style="color: #66cc66;">,</span> h<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glMatrixMode<span style="color: black;">&#40;</span>GL_PROJECTION<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLoadIdentity<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; gluOrtho2D<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> w<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> h<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__<span style="color: #66cc66;">==</span><span style="color: #483d8b;">'__main__'</span> :<br />
&nbsp; &nbsp; glutInit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitDisplayMode<span style="color: black;">&#40;</span>GLUT_SINGLE | GLUT_RGB<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitWindowSize<span style="color: black;">&#40;</span><span style="color: #ff4500;">400</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">150</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitWindowPosition<span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutCreateWindow<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Righe&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutDisplayFunc<span style="color: black;">&#40;</span>display<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutReshapeFunc<span style="color: black;">&#40;</span>reshape<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutMainLoop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
</pre>
<p>Uso tutti comandi già visti (tranne <code class="codecolorer text dawn"><span class="text">glLineWidth()</span></code> ma credo che in questo caso il significato sia piuttosto intuitivo). La funzione <code class="codecolorer text dawn"><span class="text">drawLine()</span></code> è una funzione personale che serve a snellire il codice. Potete usarla oppure usare direttamente ogni volta lo schema <em>begin-vertex-end</em>.</p>
<p><strong>STIPPLE NEI POLIGONI</strong></p>
<p>Ora invece vediamo come possiamo fare qualcosa di analogo con le campiture dei poligoni. Il meccanismo è analogo a quello delle linee. Per prima cosa si abilità lo stato <code class="codecolorer text dawn"><span class="text">GL_POLYGON_STIPPLE</span></code>. Poi si imposta il pattern con:</p>
<p><code class="codecolorer text dawn"><span class="text">glPolygonStipple(pattern)</span></code></p>
<p>Questa volta però disegnare il pattern è più complesso. Bisogna infatti utilizzare un array di 128 numeri esadecimali a 8bit al fine di formare una bitmap di 32&#215;32 bit. Disegnare trame complesse è quindi molto più complesso del disegnare pattern per le righe. Proprio a causa di questa complessità e per il fatto che lo stipple di poligoni non è importante come lo stipple di linee, tralascerò per il momento il modo con cui vengono costruite queste bitmap. Ne farò forse un appendice.</p>
<p>Per il momento vi lascio questo codice di esempio:</p>
<pre>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: #dc143c;">GL</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: black;">GLUT</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: black;">GLU</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<br />
halftone <span style="color: #66cc66;">=</span> <span style="color: black;">&#91;</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span><br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0xAA</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0x55</span> <span style="color: black;">&#93;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; glClearColor<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glShadeModel<span style="color: black;">&#40;</span>GL_FLAT<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> reshape<span style="color: black;">&#40;</span>w<span style="color: #66cc66;">,</span>h<span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; glViewport<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> w<span style="color: #66cc66;">,</span> h<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glMatrixMode<span style="color: black;">&#40;</span>GL_PROJECTION<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLoadIdentity<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; gluOrtho2D<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> w<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> h<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> display<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; glClear<span style="color: black;">&#40;</span>GL_COLOR_BUFFER_BIT<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glColor3f<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#Disegna un quadrato bianco</span><br />
&nbsp; &nbsp; glRectf<span style="color: black;">&#40;</span><span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">125</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">125</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #808080; font-style: italic;">#Disegna un quadrato stipple in halftone</span><br />
&nbsp; &nbsp; glEnable<span style="color: black;">&#40;</span>GL_POLYGON_STIPPLE<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glPolygonStipple<span style="color: black;">&#40;</span>halftone<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glRectf<span style="color: black;">&#40;</span><span style="color: #ff4500;">125</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">225</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">125</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glDisable<span style="color: black;">&#40;</span>GL_POLYGON_STIPPLE<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glFlush<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'__main__'</span> :<br />
&nbsp; &nbsp; glutInit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitDisplayMode<span style="color: black;">&#40;</span>GLUT_SINGLE | GLUT_RGB<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitWindowSize<span style="color: black;">&#40;</span><span style="color: #ff4500;">250</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">150</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutCreateWindow<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;HalfTone&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutDisplayFunc<span style="color: black;">&#40;</span>display<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutReshapeFunc<span style="color: black;">&#40;</span>reshape<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutMainLoop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
</pre>
<p>Per questa volta è tutto. La prossima volta vedremo alcuni modi per evitare di inserire singolarmente tutti i vertici di una figura ed i rispettivi colori. Sostituendo quindi 20 istruzioni con una sola.</p>
 <p><a href="http://davideaversa.it/slashcode/?flattrss_redirect&amp;id=566&amp;md5=66f1c0aa3531cc6d68e19d9f63c08817" title="Flattr" target="_blank"><img src="http://davideaversa.it/slashcode/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://davideaversa.it/slashcode/2010/05/opengl-stipple/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=thek3nger&amp;popout=1&amp;url=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl-stipple%2F&amp;language=it_IT&amp;category=text&amp;title=OpenGL+%26%238211%3B+Stipple&amp;description=Questa+%C3%A8+la+penultima+lezione+del+primo+grande+capitolo+di+OpenGL%3A+il+disegno+di+poligoni.+Oggi+parleremo+dei+%26%238220%3Bpattern%26%238221%3B+e+di+come+istruire+il+motore+grafico+affinch%C3%A9+non+utilizzi+un...&amp;tags=glut%2Copengl%2Cpython%2Cblog" type="text/html" />
	</item>
		<item>
		<title>OpenGl &#8211; Approfondiamo le Modalità</title>
		<link>http://davideaversa.it/slashcode/2010/05/opengl-approfondiamo-le-modalita/</link>
		<comments>http://davideaversa.it/slashcode/2010/05/opengl-approfondiamo-le-modalita/#comments</comments>
		<pubDate>Tue, 11 May 2010 17:31:04 +0000</pubDate>
		<dc:creator>THeK3nger</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[glut]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://davideaversa.it/slashcode/?p=564</guid>
		<description><![CDATA[<div><a href="http://davideaversa.it/slashcode/2010/05/opengl-approfondiamo-le-modalita/"><img title="OpenGl &#8211; Approfondiamo le Modalità" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/The_Triangle-150x150.jpg" alt="OpenGl &#8211; Approfondiamo le Modalità"  width="200" height="200" /></a></div><br/>Nelle precedenti lezioni abbiamo visto come si riesca a disegnare qualunque poligono semplicemente inviando a OpenGL le coordinate dei suoi vertici. Per fare questo abbiamo anche visto che dobbiamo inserire il comando glVertex() all&#8217;interno di un blocco di istruzioni che comincia con: glBegin&#40;modes&#41; e termina con glEnd&#40;&#41; Questa volta vediamo, anche grazie ad un piccolo <a href='http://davideaversa.it/slashcode/2010/05/opengl-approfondiamo-le-modalita/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl-approfondiamo-le-modalita%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_google1" style="width:90px;"><div class="g-plusone" data-size="medium" data-href="http://davideaversa.it/slashcode/2010/05/opengl-approfondiamo-le-modalita/" ></div></div><div class="really_simple_share_twitter" style="width:110px;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="OpenGl &#8211; Approfondiamo le Modalità" data-url="http://davideaversa.it/slashcode/2010/05/opengl-approfondiamo-le-modalita/" 
						data-via=""  ></a></div></div>
		<div style="clear:both;"></div><p><img class="alignleft size-thumbnail wp-image-565" title="Triangoli" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/The_Triangle-150x150.jpg" alt="" width="150" height="150" />Nelle precedenti lezioni abbiamo visto come si riesca a disegnare qualunque poligono semplicemente inviando a OpenGL le coordinate dei suoi vertici. Per fare questo abbiamo anche visto che dobbiamo inserire il comando <em>glVertex()</em> all&#8217;interno di un blocco di istruzioni che comincia con:</p>
<p><code class="codecolorer python dawn"><span class="python">glBegin<span style="color: black;">&#40;</span>modes<span style="color: black;">&#41;</span></span></code></p>
<p>e termina con</p>
<p><code class="codecolorer python dawn"><span class="python">glEnd<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></span></code></p>
<p>Questa volta vediamo, anche grazie ad un piccolo programma di esempio come si comporta la renderizzazione al variare del parametro <em>modes</em>. Ogni modalità ha i suoi vantaggi e le sue modalità di utilizzo, ma andiamo a vedere queste modalità nel dettaglio:</p>
<ul>
<li><strong>GL_POINTS</strong>. Questa modalità interpreta i vertici come singoli punti.</li>
<li><strong>GL_LINES. </strong>Questa modalità interpreta i vertici come segmenti. In pratica collega due vertici con una linea a coppie di due. Ad esempio dati 4 vertici v1, v2, v3 e v4 OpenGL traccerà una linea da v1 a v2 e da v3 a v4.</li>
<li><strong>GL_LINE_STRIP.</strong> Questa modalità corrisponde ad una linea spezzata. In pratica OpenGL collegherà tutti i punti con una linea. v1 a v2, v2 a v3, v3 a v4 e così via.</li>
<li><strong>GL_LINE_LOOP. </strong>Come sopra ma, in più, unisce anche l&#8217;ultimo vertice al primo.</li>
<li><strong>GL_TRIANGLES.</strong> Questa modalità prende i vertici a gruppi di tre e forma dei triangoli.</li>
<li><strong>GL_TRIANGLE_STRIP</strong>. Questa è forse la modalità più utilizzata. In pratica forma dei triangoli utilizzando come vertici l&#8217;ultimo vertice inserito più due vertici precedenti. Facciamo un esempio. Prendiamo 6 vertici. Inseriamo i primi tre formando il triangolo v1-v2-v3. Quando inseriremo il quarto vertice OpenGL creerà il triangolo v2-v3-v4. Poi v3-v4-v5 e così via fino all&#8217;ultimo triangolo. La modalità precedente, invece avrebbe disegnato solamente due triangoli: v1-v2-v3 e v4-v5-v6.</li>
<li><strong>GL_TRIANGLE_FAN.</strong> Questa modalità è simile alla precedente ma ha una differenza fondamentale: il primo vertice inserito farà sempre parte dei triangoli. Così, supponendo di avere sempre i 6 vertici di prima, OpenGL disegnerà il triangolo v1-v2-v3, poi v1-v3-v4, poi v1-v4-v5 e così via.</li>
<li><strong>GL_QUADS.</strong> Questa modalità è simile alla modalità GL_TRIANGLES. Ogni 4 vertici passati OpenGL disegna un quadrato.</li>
<li><strong>GL_QUAD_STRIP. </strong>Questa modalità è analoga a GL_TRIANGLE_STRIP. Solamente con i quadrati al posto dei triangoli.</li>
<li><strong>GL_POLYGON.</strong> Questa modalità interpreta ogni vertice come vertice di un poligono.</li>
</ul>
<p>C&#8217;è però una cosa da tenere a mente: OpenGL non renderizza poligoni concavi. Gli unici poligoni che si possono renderizzare sono i poligoni convessi. Se volete disegnare poligoni non convessi dovete necessariamente suddividere il poligono da disegnare in un insieme di poligoni convessi (è sempre possibile). Solitamente vengono usati i triangoli poiché un triangolo è sempre convesso (questa proprietà non vale per i quadrilateri).</p>
<p>Un altra osservazione riguarda le facce. Un poligono bidimensionale in uno spazio tridimensionale ha due facce che chiameremo &#8220;davanti&#8221; e &#8220;dietro&#8221;. OpenGL decide quale faccia è &#8220;davanti&#8221; e quale è &#8220;dietro&#8221; semplicemente interpretando i vertici: viene considerata &#8220;davanti&#8221; la faccia dalla quale si vedono i vertici in senso anti-orario. E viceversa. L&#8217;ordine dei vertici è ovviamente l&#8217;ordine con i quali vengono inseriti: se inseriamo i vertici v1, v2 e v3 la faccia davanti sarà quella in cui posso vedere i vertici v1-v2-v3 ordinati in senso anti-orario.</p>
<p>Basta un piccolo sforzo di immaginazione, non è un concetto difficile. <img src='http://davideaversa.it/slashcode/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<p>Ora inseriamo un piccolo codice di esempio. In questo codice ad ogni click del mouse vengono disegnati sempre gli stessi punti (e sempre nello stesso ordine) ma <strong>con modalità diverse</strong>. Ad ogni click viene cambiata modalità. Una cosa istruttiva che potete fare è di disegnarvi i 9 punti che traccia il programma su un foglio di carta e di provare a prevedere cosa uscirà. Ovviamente alcune modalità con quei punti non si comportano molto bene (come GL_TRIANGLE_FAN e GL_POLYGON in quanto si verrebbero a formare poligoni concavi). Non ho però spostato i punti per far quadrare tutto, ho preferito lasciare i punti sempre fissi e far vedere come varia il risultato al variare delle modalità.</p>
<div class="codecolorer-container text dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="text codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap">modes = [GL_POINTS, GL_LINES, GL_LINE_STRIP,<br />
&nbsp; &nbsp; &nbsp; &nbsp; GL_LINE_LOOP, GL_TRIANGLES, GL_TRIANGLE_STRIP,<br />
&nbsp; &nbsp; &nbsp; &nbsp; GL_TRIANGLE_FAN, GL_QUADS, GL_QUAD_STRIP, GL_POLYGON]<br />
&nbsp; &nbsp; &nbsp; &nbsp; <br />
index = 0<br />
<br />
def init() :<br />
&nbsp; &nbsp; glClearColor(0,0,0,0)<br />
&nbsp; &nbsp; glMatrixMode(GL_PROJECTION)<br />
&nbsp; &nbsp; glLoadIdentity()<br />
&nbsp; &nbsp; glOrtho(0,1,0,1,-1,1)<br />
&nbsp; &nbsp; <br />
def mouse(button, state, x, y) :<br />
&nbsp; &nbsp; global index<br />
&nbsp; &nbsp; if button == GLUT_LEFT_BUTTON :<br />
&nbsp; &nbsp; &nbsp; &nbsp; if state == GLUT_DOWN :<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; index += 1<br />
&nbsp; &nbsp; if index &gt;= len(modes) :<br />
&nbsp; &nbsp; &nbsp; &nbsp; index -= len(modes)<br />
&nbsp; &nbsp; glutPostRedisplay()<br />
&nbsp; &nbsp; <br />
def display() :<br />
&nbsp; &nbsp; glClear(GL_COLOR_BUFFER_BIT)<br />
&nbsp; &nbsp; glColor3f(1,1,1)<br />
&nbsp; &nbsp; glBegin(modes[index])<br />
&nbsp; &nbsp; glVertex2f(0.25, 0.25)<br />
&nbsp; &nbsp; glVertex2f(0.10, 0.50)<br />
&nbsp; &nbsp; glVertex2f(0.25, 0.85)<br />
&nbsp; &nbsp; glVertex2f(0.50, 0.50)<br />
&nbsp; &nbsp; glVertex2f(0.65, 0.85)<br />
&nbsp; &nbsp; glVertex2f(0.90, 0.85)<br />
&nbsp; &nbsp; glVertex2f(0.75, 0.50)<br />
&nbsp; &nbsp; glVertex2f(0.60, 0.15)<br />
&nbsp; &nbsp; glVertex2f(0.90, 0.15)<br />
&nbsp; &nbsp; glEnd()<br />
&nbsp; &nbsp; glFlush()<br />
&nbsp; &nbsp; <br />
if __name__ == '__main__' :<br />
&nbsp; &nbsp; glutInit()<br />
&nbsp; &nbsp; glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB)<br />
&nbsp; &nbsp; glutInitWindowSize(250, 250)<br />
&nbsp; &nbsp; glutInitWindowPosition(100, 100)<br />
&nbsp; &nbsp; glutCreateWindow(&quot;Esempi Modalita'&quot;)<br />
&nbsp; &nbsp; init()<br />
&nbsp; &nbsp; glutDisplayFunc(display)<br />
&nbsp; &nbsp; glutMouseFunc(mouse)<br />
&nbsp; &nbsp; glutMainLoop()</div></div>
<p>Ho utilizzato solamente comandi già visti quindi non sto a rispiegarvi il codice. Il codice in questo caso non è nemmeno la cosa più importante. La cosa cruciale è guardare i vertici e vedere come vengono disegnati al variare della modalità.</p>
<p>Per questa volta è tutto. La prossima volta vedremo come modificare il disegno dei vertici ad esempio inserendo campiture nei poligoni o pattern nelle righe. Per fare, ad esempio, righe tratteggiate, spesse, fine, etc&#8230;</p>
 <p><a href="http://davideaversa.it/slashcode/?flattrss_redirect&amp;id=564&amp;md5=bcc9dbd50f55a4fad8cbbbea7e79d3b0" title="Flattr" target="_blank"><img src="http://davideaversa.it/slashcode/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://davideaversa.it/slashcode/2010/05/opengl-approfondiamo-le-modalita/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=thek3nger&amp;popout=1&amp;url=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl-approfondiamo-le-modalita%2F&amp;language=it_IT&amp;category=text&amp;title=OpenGl+%26%238211%3B+Approfondiamo+le+Modalit%C3%A0&amp;description=Nelle+precedenti+lezioni+abbiamo+visto+come+si+riesca+a+disegnare+qualunque+poligono+semplicemente+inviando+a+OpenGL+le+coordinate+dei+suoi+vertici.+Per+fare+questo+abbiamo+anche+visto+che+dobbiamo+inserire...&amp;tags=glut%2Copengl%2Cpython%2Cblog" type="text/html" />
	</item>
		<item>
		<title>OpenGL &#8211; Animazione</title>
		<link>http://davideaversa.it/slashcode/2010/05/opengl-animazione/</link>
		<comments>http://davideaversa.it/slashcode/2010/05/opengl-animazione/#comments</comments>
		<pubDate>Sun, 09 May 2010 17:03:11 +0000</pubDate>
		<dc:creator>THeK3nger</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[glut]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://davideaversa.it/slashcode/?p=558</guid>
		<description><![CDATA[<div><a href="http://davideaversa.it/slashcode/2010/05/opengl-animazione/"><img title="OpenGL &#8211; Animazione" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/3D_Vehicles-150x150.jpg" alt="OpenGL &#8211; Animazione"  width="200" height="200" /></a></div><br/>Prima di cominciare a spiegare il piccolo codice di esempio è importante fare qualche piccola premessa. La fluidità dell&#8217;animazione dipende dai frame per secondo (fps). I frame per secondo massimi dipendono dal refresh dello schermo. Gli schermi moderni vanno all&#8217;incirca a 60Hz, e quindi che in un secondo lo schermo proietta 60 immagini. Questo significa <a href='http://davideaversa.it/slashcode/2010/05/opengl-animazione/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl-animazione%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_google1" style="width:90px;"><div class="g-plusone" data-size="medium" data-href="http://davideaversa.it/slashcode/2010/05/opengl-animazione/" ></div></div><div class="really_simple_share_twitter" style="width:110px;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="OpenGL &#8211; Animazione" data-url="http://davideaversa.it/slashcode/2010/05/opengl-animazione/" 
						data-via=""  ></a></div></div>
		<div style="clear:both;"></div><p><img class="alignleft size-thumbnail wp-image-559" title="3D_Vehicles" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/3D_Vehicles-150x150.jpg" alt="" width="150" height="150" />Prima di cominciare a spiegare il piccolo codice di esempio è importante fare qualche piccola premessa. La fluidità dell&#8217;animazione dipende dai frame per secondo (fps). I frame per secondo massimi dipendono dal refresh dello schermo. Gli schermi moderni vanno all&#8217;incirca a 60Hz, e quindi che in un secondo lo schermo proietta 60 immagini. Questo significa che ogni secondo possiamo rappresentare al massimo 60 immagini differenti e, quindi, possiamo avere al massimo 60fps.</p>
<p>Andare a 60fps, inoltre, significa che in un sessantesimo di secondo il nostro pc deve calcolare tutti i vertici dell&#8217;immagine successiva, inviarli alla scheda video e renderizzarli. Nel caso in cui l&#8217;elaborazione necessita più di un sessantesimo di secondo dobbiamo inserirci nell&#8217;intervallo successivo, ovvero due sessantesimi di secondo. In questo caso la nostra animazione crolla a 30fps (60/2). Se nemmeno due sessantesimi di secondo sono sufficienti allora passiamo a tre sessantesimi facendo abbassare i nostri fps a 20 (60/3) e così via.</p>
<p>Data la presenza di questi intervalli fissi il tempo di calcolo che &#8220;avanza&#8221; alla funzione Display viene occupato dalla funzione <strong>Idle</strong>. Questa è la funzione che viene eseguita quando nessun&#8217;altra funzione deve essere eseguita.</p>
<p>Il secondo punto è la presenza del <strong>doppio buffer</strong>. Usando un solo buffer infatti c&#8217;è il rischio che i vertici da visualizzare vengano modificati ancor prima di essere completamente renderizzati sullo schermo. Questo rischia di creare un fastidioso &#8220;effetto-fantasma&#8221; causato dalla coesistenza sullo schermo di due frame incompleti. Per ovviare a questo tutte le schede video moderne implementano il doppio buffer: secondo questo sistema un buffer viene utilizzato per la visualizzazione mentre il secondo viene utilizzato dal programma per immagazzinare i vertici del prossimo frame. Quando arriva il momento della visualizzazione questi due buffer vengono scambiati. In questo modo è garantito che vengano visualizzati solo frame completi.</p>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: black;">GLUT</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: #dc143c;">GL</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<br />
spin <span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; glClearColor<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glShadeModel<span style="color: black;">&#40;</span>GL_FLAT<span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> display<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; glClear<span style="color: black;">&#40;</span>GL_COLOR_BUFFER_BIT<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glPushMatrix<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glRotatef<span style="color: black;">&#40;</span>spin<span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glColor3f<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glRectf<span style="color: black;">&#40;</span>-<span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> -<span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">25</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glPopMatrix<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutSwapBuffers<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> reshape<span style="color: black;">&#40;</span>w<span style="color: #66cc66;">,</span> h<span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; glViewport<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span> w<span style="color: #66cc66;">,</span> h<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glMatrixMode<span style="color: black;">&#40;</span>GL_PROJECTION<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLoadIdentity<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <br />
&nbsp; &nbsp; glOrtho<span style="color: black;">&#40;</span>-<span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> -<span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">50</span><span style="color: #66cc66;">,</span> -<span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glMatrixMode<span style="color: black;">&#40;</span>GL_MODELVIEW<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glLoadIdentity<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> spinDisplay<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">global</span> spin<br />
&nbsp; &nbsp; spin +<span style="color: #66cc66;">=</span> <span style="color: #ff4500;">0.5</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> spin <span style="color: #66cc66;">&gt;</span> <span style="color: #ff4500;">360</span> :<br />
&nbsp; &nbsp; &nbsp; &nbsp; spin <span style="color: #66cc66;">=</span> spin - <span style="color: #ff4500;">360</span><br />
&nbsp; &nbsp; glutPostRedisplay<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> mouse<span style="color: black;">&#40;</span>button<span style="color: #66cc66;">,</span> state<span style="color: #66cc66;">,</span> x<span style="color: #66cc66;">,</span> y<span style="color: black;">&#41;</span> :<br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> button <span style="color: #66cc66;">==</span> GLUT_LEFT_BUTTON :<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> state <span style="color: #66cc66;">==</span> GLUT_DOWN :<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glutIdleFunc<span style="color: black;">&#40;</span>spinDisplay<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">elif</span> button <span style="color: #66cc66;">==</span> GLUT_MIDDLE_BUTTON:<br />
&nbsp; &nbsp; &nbsp; &nbsp; <span style="color: #ff7700;font-weight:bold;">if</span> state <span style="color: #66cc66;">==</span> GLUT_DOWN :<br />
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; glutIdleFunc<span style="color: black;">&#40;</span><span style="color: #008000;">None</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'__main__'</span>:<br />
&nbsp; &nbsp; glutInit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitDisplayMode<span style="color: black;">&#40;</span>GLUT_DOUBLE | GLUT_RGB<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitWindowSize<span style="color: black;">&#40;</span><span style="color: #ff4500;">250</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">250</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutInitWindowPosition<span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutCreateWindow<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Quadrato Rotante&quot;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutMouseFunc<span style="color: black;">&#40;</span>mouse<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutDisplayFunc<span style="color: black;">&#40;</span>display<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutReshapeFunc<span style="color: black;">&#40;</span>reshape<span style="color: black;">&#41;</span><br />
&nbsp; &nbsp; glutMainLoop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>Questo programma fa due cose in più rispetto al precedente: fa ruotare il quadrato e permette di attivare/disattivare la rotazione con la pressione di due tasti del mouse (il centrale stoppa mentre il sinistro avvia).</p>
<p>Molte funzioni sono le stesse dell&#8217;ultima volta quindi non mi ripeterò. Vediamo le novità.</p>
<p>Innanzitutto potete notare che utilizzo GLUT_DOUBLE in DisplayMode. Questo serve ad attivare il doppio buffer.</p>
<p>Poi c&#8217;è la funzione <em>glutMouseFunc()</em> che serve ad impostare la funzione che servirà a gestire l&#8217;input del mouse. In questo caso ho usato una fantasiosa funzione <em>mouse</em>. Tale funzione prende quattro parametri:</p>
<ul>
<li><strong>button</strong>: indica quale bottone è stato premuto.</li>
<li><strong>state</strong>: indica lo stato (premuto, alzato, click, etc&#8230;) vedremo più avanti queste cose in dettaglio.</li>
<li><strong>x</strong> e <strong>y</strong>: sono, ovviamente, la posizione del puntatore.</li>
</ul>
<p>Nella funzione mouse faccio una sola cosa: verifico quale tasto è stato premuto e a seconda del tasto modifico la funzione di Idle. Perché <strong>è la funzione IDLE che modifica i frame</strong>. La funzione display li mostra, la funzione IDLE li modifica (modificando i parametri utilizzati da display). Questo è il punto fondamentale.</p>
<p>Come potete vedere sposto la funzione di idle con il comando <em>glutIdleFunc</em>. La imposto a <em>None </em>(o NULL in C) per fermare il quadrato e alla funzione <em>spinDisplay</em> per ruotarlo. Quest&#8217;ultima funzione non fa altro che incrementare una variabile <em>spin</em> che rappresenta i gradi di rotazione.</p>
<p>La funzione display è del tutto simile a quella dell&#8217;esempio precedente. Viene aggiunta però la funzione <em>glRotate </em>la quale non fa altro che ruotare di <em>spin</em> gradi tutti i vertici che verranno inseriti fra <em>glPushMatrix </em>e <em>glPopMatrix</em>.</p>
<p>Un altra cosa accessoria è la funzione <em>glutReshapeFunc</em> che imposta la funzione da chiamare nel momento in cui la finestra viene ridimensionata. In questo caso la funzione evita solamente che vengano alterate le proporzioni.</p>
<p><strong>EDIT:</strong> È disponibile la versione in C del codice di questa puntata. Lo trovate <a href="http://stylecode.altervista.org/opengl2.c">questo indirizzo</a></p>
 <p><a href="http://davideaversa.it/slashcode/?flattrss_redirect&amp;id=558&amp;md5=e6c74c2495f67a6a9893455f02112734" title="Flattr" target="_blank"><img src="http://davideaversa.it/slashcode/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://davideaversa.it/slashcode/2010/05/opengl-animazione/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=thek3nger&amp;popout=1&amp;url=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fopengl-animazione%2F&amp;language=it_IT&amp;category=text&amp;title=OpenGL+%26%238211%3B+Animazione&amp;description=Prima+di+cominciare+a+spiegare+il+piccolo+codice+di+esempio+%C3%A8+importante+fare+qualche+piccola+premessa.+La+fluidit%C3%A0+dell%26%238217%3Banimazione+dipende+dai+frame+per+secondo+%28fps%29.+I+frame+per+secondo+massimi...&amp;tags=glut%2Copengl%2Cpython%2Cblog" type="text/html" />
	</item>
		<item>
		<title>Qualche cenno di GLUT/OpenGL</title>
		<link>http://davideaversa.it/slashcode/2010/05/qualche-cenno-di-glutopengl/</link>
		<comments>http://davideaversa.it/slashcode/2010/05/qualche-cenno-di-glutopengl/#comments</comments>
		<pubDate>Sat, 01 May 2010 15:53:10 +0000</pubDate>
		<dc:creator>THeK3nger</dc:creator>
				<category><![CDATA[OpenGL]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[glut]]></category>
		<category><![CDATA[opengl]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://davideaversa.it/slashcode/?p=551</guid>
		<description><![CDATA[<div><a href="http://davideaversa.it/slashcode/2010/05/qualche-cenno-di-glutopengl/"><img title="Qualche cenno di GLUT/OpenGL" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/qareeb-150x150.png" alt="Qualche cenno di GLUT/OpenGL"  width="200" height="200" /></a></div><br/>Stavo ripassando gran parte delle API delle OpenGL. Stavo facendo piccoli schemi e riassunti ed allora ho pensato: perché non condividere un po&#8217; di questo lavoro? Così ci trascrivo qualche piccolo cenno di OpenGL senza nessuna intenzione di portare alla luce chissà quale serie di tutorial. Se poi esce qualcosa di appetibile meglio così. Innanzitutto <a href='http://davideaversa.it/slashcode/2010/05/qualche-cenno-di-glutopengl/' class='excerpt-more'>[...]</a>]]></description>
			<content:encoded><![CDATA[<div style="height:33px;" class="really_simple_share robots-nocontent snap_nopreview"><div class="really_simple_share_facebook_like" style="width:100px;"><iframe src="http://www.facebook.com/plugins/like.php?href=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fqualche-cenno-di-glutopengl%2F&amp;layout=button_count&amp;show_faces=false&amp;width=100&amp;action=like&amp;colorscheme=light&amp;send=false&amp;height=27" 
						scrolling="no" frameborder="0" style="border:none; overflow:hidden; width:100px; height:27px;" allowTransparency="true"></iframe></div><div class="really_simple_share_google1" style="width:90px;"><div class="g-plusone" data-size="medium" data-href="http://davideaversa.it/slashcode/2010/05/qualche-cenno-di-glutopengl/" ></div></div><div class="really_simple_share_twitter" style="width:110px;"><a href="http://twitter.com/share" class="twitter-share-button" data-count="horizontal" 
						data-text="Qualche cenno di GLUT/OpenGL" data-url="http://davideaversa.it/slashcode/2010/05/qualche-cenno-di-glutopengl/" 
						data-via=""  ></a></div></div>
		<div style="clear:both;"></div><p><img class="alignleft size-thumbnail wp-image-552" title="Montone" src="http://davideaversa.it/slashcode/wp-content/uploads/2010/05/qareeb-150x150.png" alt="" width="150" height="150" />Stavo ripassando gran parte delle API delle OpenGL. Stavo facendo piccoli schemi e riassunti ed allora ho pensato: perché non condividere un po&#8217; di questo lavoro? Così ci trascrivo qualche piccolo cenno di OpenGL senza nessuna intenzione di portare alla luce chissà quale serie di tutorial. Se poi esce qualcosa di appetibile meglio così.</p>
<p>Innanzitutto devo fare due appunti: il primo è che, per mia semplicità, userò il Python come linguaggio per il codice, tuttavia le API sono identiche sia in pyOpenGL che nell&#8217;OpenGL standard quindi anche se programmate in C potete facilmente convertire il tutto nel linguaggio a voi più familiare. Secondo, le libreria OpenGL prese da sole sono un po&#8217; complicate, infatti di solito ci si appoggia alle librerie collegate <strong>GLUT</strong>. Queste librerie si incaricano di fare tutto il lavoro &#8220;esterno&#8221; alla visualizzazione grafica come gestione degli input e integrazione nel sistema a finestre. Usare GLUT è proprio l&#8217;approccio che userò anche io.</p>
<p>Iniziamo mostrando un piccolo programma che disegna un quadrato bianco su uno sfondo nero.</p>
<div class="codecolorer-container python dawn" style="overflow:auto;white-space:nowrap;border:1px solid #9F9F9F;width:435px;height:300px;"><div class="python codecolorer" style="padding:5px;font:normal 12px/1.4em Monaco, Lucida Console, monospace;white-space:nowrap"><span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: black;">GLUT</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<span style="color: #ff7700;font-weight:bold;">from</span> OpenGL.<span style="color: #dc143c;">GL</span> <span style="color: #ff7700;font-weight:bold;">import</span> *<br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
glClearColor<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
glMatrixMode<span style="color: black;">&#40;</span>GL_PROJECTION<span style="color: black;">&#41;</span><br />
glLoadIdentity<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
glOrtho<span style="color: black;">&#40;</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">0</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span>-<span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">def</span> display<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>:<br />
glClear<span style="color: black;">&#40;</span>GL_COLOR_BUFFER_BIT<span style="color: black;">&#41;</span><br />
glColor3f<span style="color: black;">&#40;</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: #66cc66;">,</span><span style="color: #ff4500;">1</span><span style="color: black;">&#41;</span><br />
glBegin<span style="color: black;">&#40;</span>GL_POLYGON<span style="color: black;">&#41;</span><br />
glVertex3f<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
glVertex3f<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.75</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
glVertex3f<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.75</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.75</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
glVertex3f<span style="color: black;">&#40;</span><span style="color: #ff4500;">0.25</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0.75</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">0</span><span style="color: black;">&#41;</span><br />
glEnd<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
glFlush<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
<br />
<span style="color: #ff7700;font-weight:bold;">if</span> __name__ <span style="color: #66cc66;">==</span> <span style="color: #483d8b;">'__main__'</span> :<br />
glutInit<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
glutInitDisplayMode<span style="color: black;">&#40;</span>GLUT_SINGLE | GLUT_RGB<span style="color: black;">&#41;</span><br />
glutInitWindowSize<span style="color: black;">&#40;</span><span style="color: #ff4500;">250</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">250</span><span style="color: black;">&#41;</span><br />
glutInitWindowPosition<span style="color: black;">&#40;</span><span style="color: #ff4500;">100</span><span style="color: #66cc66;">,</span> <span style="color: #ff4500;">100</span><span style="color: black;">&#41;</span><br />
glutCreateWindow<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;Hello World&quot;</span><span style="color: black;">&#41;</span><br />
init<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span><br />
glutDisplayFunc<span style="color: black;">&#40;</span>display<span style="color: black;">&#41;</span><br />
glutMainLoop<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></div></div>
<p>Partiamo quindi a spiegare il codice cominciando da l main. La prima funzione che incontriamo è <strong>glutInit()</strong>. Questa funzione si incarica di inizializzare GLUT. La versione canonica di questa funzione prende per argomento gli argomenti del main (<em>argc </em>e <em>argv </em>per chi conosce il C).</p>
<p>La seconda funzione è <strong>glutInitDisplayMode()</strong>. Questa funzione prende per parametri delle opzioni di visualizzazione. Nel nostro caso <em>GLUT_SINGLE </em>e <em>GLUT_RGB</em> i quali impostano la visualizzazione a singolo buffer (capiremo la differenza con il doppio buffer la prossima volta) e la visualizzazione RGB (ovvero con i colori tradizionalmente usati nei pc).</p>
<p>Gli altri tre comandi invece creano la finestra impostandone dimensione, posizione e titolo.</p>
<p>A questo punto entriamo in <strong>init()</strong>. In questa funzione vengono preimpostate alcune variabili di OpenGL. Non entrerò nel dettaglio ora, vi basti sapere che <strong>glClearColor()</strong> è la funzione per il colore di &#8220;sfondo&#8221; ovvero per quello usato durante la cancellazione. Questa funzione prende 4 parametri (rosso, verde, blu e trasparenza).</p>
<p>Un altra funzione importantissima è <strong>glutDisplayFunc()</strong> questa è la funzione che indica al motore grafico quale funzione chiamare nel momento in cui c&#8217;è bisogno di disegnare sullo schermo. In questo esempio passiamo semplicemente la funzione <strong>display</strong> la quale non fa altro che disegnare un quadrato bianco.</p>
<p>Come? Semplice. Per disegnare qualcosa bisogna passare al sistema le coordinate dei vertici secondo un algoritmo del tipo:</p>
<ul>
<li>Si imposta il colore.</li>
<li>Si avverte il sistema che stiamo inserendo i vertici con <strong>glBegin().</strong></li>
<li>Si passano le coordinate dei vertici con <strong>glVertex3f()</strong></li>
<li>Si avverte il sistema che abbiamo terminato con <strong>glEnd()</strong></li>
</ul>
<p>Il parametro di glBegin è molto importante ma capiremo meglio il suo significato successivamente.</p>
<p>La funzione <strong>glFlush()</strong> è inutile per molte applicazioni. Ma voi mettetela. Così sarete sicuri che il vostro codice funzioni, ad esempio, in applicazioni di rete.</p>
<p>Alla fine, per chiudere in bellezza si lancia <strong>glutMainLoop() </strong>per avviare il programma.</p>
<p>Questo è solo un piccolo esempio. Disegnare un quadrato immobile non è ne bello ne utile. Ma questo esempio, nella sua semplicità, racchiude l&#8217;essenza di ogni programma OpenGL. La prossima volta vedremo come far ruotare questo dannato quadrato.</p>
 <p><a href="http://davideaversa.it/slashcode/?flattrss_redirect&amp;id=551&amp;md5=b26b6dc9a66f3852ec64e4222b450b0a" title="Flattr" target="_blank"><img src="http://davideaversa.it/slashcode/wp-content/plugins/flattr/img/flattr-badge-large.png" alt="flattr this!"/></a></p>]]></content:encoded>
			<wfw:commentRss>http://davideaversa.it/slashcode/2010/05/qualche-cenno-di-glutopengl/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		<atom:link rel="payment" href="https://flattr.com/submit/auto?user_id=thek3nger&amp;popout=1&amp;url=http%3A%2F%2Fdavideaversa.it%2Fslashcode%2F2010%2F05%2Fqualche-cenno-di-glutopengl%2F&amp;language=it_IT&amp;category=text&amp;title=Qualche+cenno+di+GLUT%2FOpenGL&amp;description=Stavo+ripassando+gran+parte+delle+API+delle+OpenGL.+Stavo+facendo+piccoli+schemi+e+riassunti+ed+allora+ho+pensato%3A+perch%C3%A9+non+condividere+un+po%26%238217%3B+di+questo+lavoro%3F+Cos%C3%AC+ci+trascrivo+qualche...&amp;tags=glut%2Copengl%2Cpython%2Cblog" type="text/html" />
	</item>
	</channel>
</rss>

