Sophie

Sophie

distrib > Mandriva > 2010.0 > i586 > media > contrib-release > by-pkgid > ed1483a4d9709d82850bb84cf25535e7 > files > 84

sfml-1.5-1mdv2010.0.i586.rpm

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <title>SFML - Simple and Fast Multimedia Library</title>
        <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
        <link href="doxygen.css" rel="stylesheet" type="text/css" />
        <link href="tabs.css" rel="stylesheet" type="text/css" />
    </head>
    <body>
        <div id="logo">
            <img src="./logo.jpg" width="770" height="200" title="SFML home" alt="SFML logo" />
        </div>
<!-- Generated by Doxygen 1.5.8 -->
<div class="navigation" id="top">
  <div class="tabs">
    <ul>
      <li><a href="index.htm"><span>Main&nbsp;Page</span></a></li>
      <li><a href="namespaces.htm"><span>Namespaces</span></a></li>
      <li><a href="annotated.htm"><span>Classes</span></a></li>
      <li class="current"><a href="files.htm"><span>Files</span></a></li>
    </ul>
  </div>
  <div class="tabs">
    <ul>
      <li><a href="files.htm"><span>File&nbsp;List</span></a></li>
    </ul>
  </div>
<h1>PostFX.cpp</h1><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 
<a name="l00002"></a>00002 <span class="comment">//</span>
<a name="l00003"></a>00003 <span class="comment">// SFML - Simple and Fast Multimedia Library</span>
<a name="l00004"></a>00004 <span class="comment">// Copyright (C) 2007-2009 Laurent Gomila (laurent.gom@gmail.com)</span>
<a name="l00005"></a>00005 <span class="comment">//</span>
<a name="l00006"></a>00006 <span class="comment">// This software is provided 'as-is', without any express or implied warranty.</span>
<a name="l00007"></a>00007 <span class="comment">// In no event will the authors be held liable for any damages arising from the use of this software.</span>
<a name="l00008"></a>00008 <span class="comment">//</span>
<a name="l00009"></a>00009 <span class="comment">// Permission is granted to anyone to use this software for any purpose,</span>
<a name="l00010"></a>00010 <span class="comment">// including commercial applications, and to alter it and redistribute it freely,</span>
<a name="l00011"></a>00011 <span class="comment">// subject to the following restrictions:</span>
<a name="l00012"></a>00012 <span class="comment">//</span>
<a name="l00013"></a>00013 <span class="comment">// 1. The origin of this software must not be misrepresented;</span>
<a name="l00014"></a>00014 <span class="comment">//    you must not claim that you wrote the original software.</span>
<a name="l00015"></a>00015 <span class="comment">//    If you use this software in a product, an acknowledgment</span>
<a name="l00016"></a>00016 <span class="comment">//    in the product documentation would be appreciated but is not required.</span>
<a name="l00017"></a>00017 <span class="comment">//</span>
<a name="l00018"></a>00018 <span class="comment">// 2. Altered source versions must be plainly marked as such,</span>
<a name="l00019"></a>00019 <span class="comment">//    and must not be misrepresented as being the original software.</span>
<a name="l00020"></a>00020 <span class="comment">//</span>
<a name="l00021"></a>00021 <span class="comment">// 3. This notice may not be removed or altered from any source distribution.</span>
<a name="l00022"></a>00022 <span class="comment">//</span>
<a name="l00024"></a>00024 <span class="comment"></span>
<a name="l00025"></a>00025 
<a name="l00027"></a>00027 <span class="comment">// Headers</span>
<a name="l00029"></a>00029 <span class="comment"></span><span class="preprocessor">#include &lt;SFML/Graphics/PostFX.hpp&gt;</span>
<a name="l00030"></a>00030 <span class="preprocessor">#include &lt;SFML/Graphics/RenderWindow.hpp&gt;</span>
<a name="l00031"></a>00031 <span class="preprocessor">#include &lt;SFML/Graphics/GraphicsContext.hpp&gt;</span>
<a name="l00032"></a>00032 <span class="preprocessor">#include &lt;fstream&gt;</span>
<a name="l00033"></a>00033 <span class="preprocessor">#include &lt;iostream&gt;</span>
<a name="l00034"></a>00034 <span class="preprocessor">#include &lt;set&gt;</span>
<a name="l00035"></a>00035 <span class="preprocessor">#include &lt;sstream&gt;</span>
<a name="l00036"></a>00036 
<a name="l00037"></a>00037 
<a name="l00038"></a>00038 <span class="keyword">namespace </span>sf
<a name="l00039"></a>00039 {
<a name="l00043"></a><a class="code" href="classsf_1_1PostFX.htm#205bffbe841cf3804447e8d3e684f7eb">00043</a> <a class="code" href="classsf_1_1PostFX.htm#205bffbe841cf3804447e8d3e684f7eb" title="Default constructor.">PostFX::PostFX</a>() :
<a name="l00044"></a>00044 myShaderProgram(0)
<a name="l00045"></a>00045 {
<a name="l00046"></a>00046     <span class="comment">// No filtering on frame buffer</span>
<a name="l00047"></a>00047     myFrameBuffer.<a class="code" href="classsf_1_1Image.htm#d167422fd331cd069674391fb16e8452" title="Enable or disable image smooth filter.">SetSmooth</a>(<span class="keyword">false</span>);
<a name="l00048"></a>00048 }
<a name="l00049"></a>00049 
<a name="l00050"></a>00050 
<a name="l00054"></a><a class="code" href="classsf_1_1PostFX.htm#4ec47ed9e2539b589695854e2b259b47">00054</a> <a class="code" href="classsf_1_1PostFX.htm#205bffbe841cf3804447e8d3e684f7eb" title="Default constructor.">PostFX::PostFX</a>(<span class="keyword">const</span> <a class="code" href="classsf_1_1PostFX.htm" title="PostFX is used to apply a post effect to a window.">PostFX</a>&amp; Copy) :
<a name="l00055"></a>00055 <a class="code" href="classsf_1_1Drawable.htm" title="Abstract base class for every object that can be drawn into a render window.">Drawable</a>        (Copy),
<a name="l00056"></a>00056 myShaderProgram (0),
<a name="l00057"></a>00057 myTextures      (Copy.myTextures),
<a name="l00058"></a>00058 myFragmentShader(Copy.myFragmentShader),
<a name="l00059"></a>00059 myFrameBuffer   (Copy.myFrameBuffer)
<a name="l00060"></a>00060 {
<a name="l00061"></a>00061     <span class="comment">// No filtering on frame buffer</span>
<a name="l00062"></a>00062     myFrameBuffer.<a class="code" href="classsf_1_1Image.htm#d167422fd331cd069674391fb16e8452" title="Enable or disable image smooth filter.">SetSmooth</a>(<span class="keyword">false</span>);
<a name="l00063"></a>00063 
<a name="l00064"></a>00064     <span class="comment">// Create the shaders and the program</span>
<a name="l00065"></a>00065     <span class="keywordflow">if</span> (Copy.<a class="code" href="classsf_1_1PostFX.htm#b374a6e0d7f80fd340616cd79657cfee" title="OpenGL identifier for the program.">myShaderProgram</a>)
<a name="l00066"></a>00066         CreateProgram();
<a name="l00067"></a>00067 }
<a name="l00068"></a>00068 
<a name="l00069"></a>00069 
<a name="l00073"></a><a class="code" href="classsf_1_1PostFX.htm#b9349387988368fe8a2c66e4b3220d63">00073</a> <a class="code" href="classsf_1_1PostFX.htm#b9349387988368fe8a2c66e4b3220d63" title="Destructor.">PostFX::~PostFX</a>()
<a name="l00074"></a>00074 {
<a name="l00075"></a>00075     <span class="comment">// Destroy effect program</span>
<a name="l00076"></a>00076     <span class="keywordflow">if</span> (myShaderProgram)
<a name="l00077"></a>00077     {
<a name="l00078"></a>00078         <span class="comment">// Make sure we have a valid context</span>
<a name="l00079"></a>00079         priv::GraphicsContext Ctx;
<a name="l00080"></a>00080 
<a name="l00081"></a>00081         GLCheck(glDeleteObjectARB(myShaderProgram));
<a name="l00082"></a>00082     }
<a name="l00083"></a>00083 }
<a name="l00084"></a>00084 
<a name="l00085"></a>00085 
<a name="l00089"></a><a class="code" href="classsf_1_1PostFX.htm#03c9aa065a98750335780213aff2be92">00089</a> <span class="keywordtype">bool</span> <a class="code" href="classsf_1_1PostFX.htm#03c9aa065a98750335780213aff2be92" title="Load the effect from a file.">PostFX::LoadFromFile</a>(<span class="keyword">const</span> std::string&amp; Filename)
<a name="l00090"></a>00090 {
<a name="l00091"></a>00091     <span class="comment">// Open the file</span>
<a name="l00092"></a>00092     std::ifstream File(Filename.c_str());
<a name="l00093"></a>00093     <span class="keywordflow">if</span> (!File)
<a name="l00094"></a>00094     {
<a name="l00095"></a>00095         std::cerr &lt;&lt; <span class="stringliteral">"Failed to open effect file \""</span> &lt;&lt; Filename &lt;&lt; <span class="stringliteral">"\""</span> &lt;&lt; std::endl;
<a name="l00096"></a>00096         <span class="keywordflow">return</span> <span class="keyword">false</span>;
<a name="l00097"></a>00097     }
<a name="l00098"></a>00098 
<a name="l00099"></a>00099     <span class="comment">// Apply the preprocessing pass to the fragment shader code</span>
<a name="l00100"></a>00100     myFragmentShader = PreprocessEffect(File);
<a name="l00101"></a>00101 
<a name="l00102"></a>00102     <span class="comment">// Create the shaders and the program</span>
<a name="l00103"></a>00103     CreateProgram();
<a name="l00104"></a>00104 
<a name="l00105"></a>00105     <span class="keywordflow">return</span> myShaderProgram != 0;
<a name="l00106"></a>00106 }
<a name="l00107"></a>00107 
<a name="l00108"></a>00108 
<a name="l00112"></a><a class="code" href="classsf_1_1PostFX.htm#53b9df4741e52bd4978d642153e492b6">00112</a> <span class="keywordtype">bool</span> <a class="code" href="classsf_1_1PostFX.htm#53b9df4741e52bd4978d642153e492b6" title="Load the effect from a text in memory.">PostFX::LoadFromMemory</a>(<span class="keyword">const</span> std::string&amp; Effect)
<a name="l00113"></a>00113 {
<a name="l00114"></a>00114     <span class="comment">// Open a stream and copy the effect code</span>
<a name="l00115"></a>00115     std::istringstream Stream(Effect.c_str());
<a name="l00116"></a>00116 
<a name="l00117"></a>00117     <span class="comment">// Apply the preprocessing pass to the fragment shader code</span>
<a name="l00118"></a>00118     myFragmentShader = PreprocessEffect(Stream);
<a name="l00119"></a>00119 
<a name="l00120"></a>00120     <span class="comment">// Create the shaders and the program</span>
<a name="l00121"></a>00121     CreateProgram();
<a name="l00122"></a>00122 
<a name="l00123"></a>00123     <span class="keywordflow">return</span> myShaderProgram != 0;
<a name="l00124"></a>00124 }
<a name="l00125"></a>00125 
<a name="l00126"></a>00126 
<a name="l00130"></a><a class="code" href="classsf_1_1PostFX.htm#4d7e11f6ecf159d09460220ecc43e45a">00130</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1PostFX.htm#4d7e11f6ecf159d09460220ecc43e45a" title="Change a parameter of the effect (1 float).">PostFX::SetParameter</a>(<span class="keyword">const</span> std::string&amp; Name, <span class="keywordtype">float</span> X)
<a name="l00131"></a>00131 {
<a name="l00132"></a>00132     <span class="keywordflow">if</span> (myShaderProgram)
<a name="l00133"></a>00133     {
<a name="l00134"></a>00134         <span class="comment">// Enable program</span>
<a name="l00135"></a>00135         GLCheck(glUseProgramObjectARB(myShaderProgram));
<a name="l00136"></a>00136 
<a name="l00137"></a>00137         <span class="comment">// Get parameter location and assign it new values</span>
<a name="l00138"></a>00138         GLint Location = glGetUniformLocationARB(myShaderProgram, Name.c_str());
<a name="l00139"></a>00139         <span class="keywordflow">if</span> (Location != -1)
<a name="l00140"></a>00140             GLCheck(glUniform1fARB(Location, X));
<a name="l00141"></a>00141         <span class="keywordflow">else</span>
<a name="l00142"></a>00142             std::cerr &lt;&lt; <span class="stringliteral">"Parameter \""</span> &lt;&lt; Name &lt;&lt; <span class="stringliteral">"\" not found in post-effect"</span> &lt;&lt; std::endl;
<a name="l00143"></a>00143 
<a name="l00144"></a>00144         <span class="comment">// Disable program</span>
<a name="l00145"></a>00145         GLCheck(glUseProgramObjectARB(0));
<a name="l00146"></a>00146     }
<a name="l00147"></a>00147 }
<a name="l00148"></a>00148 
<a name="l00149"></a>00149 
<a name="l00153"></a><a class="code" href="classsf_1_1PostFX.htm#a92c8a3f203a64d816c6844bdfad2393">00153</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1PostFX.htm#4d7e11f6ecf159d09460220ecc43e45a" title="Change a parameter of the effect (1 float).">PostFX::SetParameter</a>(<span class="keyword">const</span> std::string&amp; Name, <span class="keywordtype">float</span> X, <span class="keywordtype">float</span> Y)
<a name="l00154"></a>00154 {
<a name="l00155"></a>00155     <span class="keywordflow">if</span> (myShaderProgram)
<a name="l00156"></a>00156     {
<a name="l00157"></a>00157         <span class="comment">// Enable program</span>
<a name="l00158"></a>00158         GLCheck(glUseProgramObjectARB(myShaderProgram));
<a name="l00159"></a>00159 
<a name="l00160"></a>00160         <span class="comment">// Get parameter location and assign it new values</span>
<a name="l00161"></a>00161         GLint Location = glGetUniformLocationARB(myShaderProgram, Name.c_str());
<a name="l00162"></a>00162         <span class="keywordflow">if</span> (Location != -1)
<a name="l00163"></a>00163             GLCheck(glUniform2fARB(Location, X, Y));
<a name="l00164"></a>00164         <span class="keywordflow">else</span>
<a name="l00165"></a>00165             std::cerr &lt;&lt; <span class="stringliteral">"Parameter \""</span> &lt;&lt; Name &lt;&lt; <span class="stringliteral">"\" not found in post-effect"</span> &lt;&lt; std::endl;
<a name="l00166"></a>00166 
<a name="l00167"></a>00167         <span class="comment">// Disable program</span>
<a name="l00168"></a>00168         GLCheck(glUseProgramObjectARB(0));
<a name="l00169"></a>00169     }
<a name="l00170"></a>00170 }
<a name="l00171"></a>00171 
<a name="l00172"></a>00172 
<a name="l00176"></a><a class="code" href="classsf_1_1PostFX.htm#031c257687c3115d6f524e7922e32d67">00176</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1PostFX.htm#4d7e11f6ecf159d09460220ecc43e45a" title="Change a parameter of the effect (1 float).">PostFX::SetParameter</a>(<span class="keyword">const</span> std::string&amp; Name, <span class="keywordtype">float</span> X, <span class="keywordtype">float</span> Y, <span class="keywordtype">float</span> Z)
<a name="l00177"></a>00177 {
<a name="l00178"></a>00178     <span class="keywordflow">if</span> (myShaderProgram)
<a name="l00179"></a>00179     {
<a name="l00180"></a>00180         <span class="comment">// Enable program</span>
<a name="l00181"></a>00181         GLCheck(glUseProgramObjectARB(myShaderProgram));
<a name="l00182"></a>00182 
<a name="l00183"></a>00183         <span class="comment">// Get parameter location and assign it new values</span>
<a name="l00184"></a>00184         GLint Location = glGetUniformLocationARB(myShaderProgram, Name.c_str());
<a name="l00185"></a>00185         <span class="keywordflow">if</span> (Location != -1)
<a name="l00186"></a>00186             GLCheck(glUniform3fARB(Location, X, Y, Z));
<a name="l00187"></a>00187         <span class="keywordflow">else</span>
<a name="l00188"></a>00188             std::cerr &lt;&lt; <span class="stringliteral">"Parameter \""</span> &lt;&lt; Name &lt;&lt; <span class="stringliteral">"\" not found in post-effect"</span> &lt;&lt; std::endl;
<a name="l00189"></a>00189 
<a name="l00190"></a>00190         <span class="comment">// Disable program</span>
<a name="l00191"></a>00191         GLCheck(glUseProgramObjectARB(0));
<a name="l00192"></a>00192     }
<a name="l00193"></a>00193 }
<a name="l00194"></a>00194 
<a name="l00195"></a>00195 
<a name="l00199"></a><a class="code" href="classsf_1_1PostFX.htm#3bc85737080c281148a247fccb8e06de">00199</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1PostFX.htm#4d7e11f6ecf159d09460220ecc43e45a" title="Change a parameter of the effect (1 float).">PostFX::SetParameter</a>(<span class="keyword">const</span> std::string&amp; Name, <span class="keywordtype">float</span> X, <span class="keywordtype">float</span> Y, <span class="keywordtype">float</span> Z, <span class="keywordtype">float</span> W)
<a name="l00200"></a>00200 {
<a name="l00201"></a>00201     <span class="keywordflow">if</span> (myShaderProgram)
<a name="l00202"></a>00202     {
<a name="l00203"></a>00203         <span class="comment">// Enable program</span>
<a name="l00204"></a>00204         GLCheck(glUseProgramObjectARB(myShaderProgram));
<a name="l00205"></a>00205 
<a name="l00206"></a>00206         <span class="comment">// Get parameter location and assign it new values</span>
<a name="l00207"></a>00207         GLint Location = glGetUniformLocationARB(myShaderProgram, Name.c_str());
<a name="l00208"></a>00208         <span class="keywordflow">if</span> (Location != -1)
<a name="l00209"></a>00209             GLCheck(glUniform4fARB(Location, X, Y, Z, W));
<a name="l00210"></a>00210         <span class="keywordflow">else</span>
<a name="l00211"></a>00211             std::cerr &lt;&lt; <span class="stringliteral">"Parameter \""</span> &lt;&lt; Name &lt;&lt; <span class="stringliteral">"\" not found in post-effect"</span> &lt;&lt; std::endl;
<a name="l00212"></a>00212 
<a name="l00213"></a>00213         <span class="comment">// Disable program</span>
<a name="l00214"></a>00214         GLCheck(glUseProgramObjectARB(0));
<a name="l00215"></a>00215     }
<a name="l00216"></a>00216 }
<a name="l00217"></a>00217 
<a name="l00218"></a>00218 
<a name="l00222"></a><a class="code" href="classsf_1_1PostFX.htm#4b9d05ec45df72151d0a5284cbf88fa1">00222</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1PostFX.htm#4b9d05ec45df72151d0a5284cbf88fa1" title="Set a texture parameter.">PostFX::SetTexture</a>(<span class="keyword">const</span> std::string&amp; Name, <a class="code" href="classsf_1_1Image.htm" title="Image is the low-level class for loading and manipulating images.">Image</a>* Texture)
<a name="l00223"></a>00223 {
<a name="l00224"></a>00224     <span class="comment">// Check that the current texture unit is available</span>
<a name="l00225"></a>00225     GLint MaxUnits;
<a name="l00226"></a>00226     GLCheck(glGetIntegerv(GL_MAX_TEXTURE_COORDS_ARB, &amp;MaxUnits));
<a name="l00227"></a>00227     <span class="keywordflow">if</span> (myTextures.size() &gt;= <span class="keyword">static_cast&lt;</span>std::size_t<span class="keyword">&gt;</span>(MaxUnits))
<a name="l00228"></a>00228     {
<a name="l00229"></a>00229         std::cerr &lt;&lt; <span class="stringliteral">"Impossible to use texture \""</span> &lt;&lt; Name &lt;&lt; <span class="stringliteral">"\" for post-effect : all available texture units are used"</span> &lt;&lt; std::endl;
<a name="l00230"></a>00230         <span class="keywordflow">return</span>;
<a name="l00231"></a>00231     }
<a name="l00232"></a>00232 
<a name="l00233"></a>00233     <span class="comment">// Make sure the given name is a valid variable in the effect</span>
<a name="l00234"></a>00234     <span class="keywordtype">int</span> Location = glGetUniformLocationARB(myShaderProgram, Name.c_str());
<a name="l00235"></a>00235     <span class="keywordflow">if</span> (Location == -1)
<a name="l00236"></a>00236     {
<a name="l00237"></a>00237         std::cerr &lt;&lt; <span class="stringliteral">"Texture \""</span> &lt;&lt; Name &lt;&lt; <span class="stringliteral">"\" not found in post-effect"</span> &lt;&lt; std::endl;
<a name="l00238"></a>00238         <span class="keywordflow">return</span>;
<a name="l00239"></a>00239     }
<a name="l00240"></a>00240 
<a name="l00241"></a>00241     <span class="comment">// Store the texture for later use</span>
<a name="l00242"></a>00242     myTextures[Name] = Texture ? Texture : &amp;myFrameBuffer;
<a name="l00243"></a>00243 }
<a name="l00244"></a>00244 
<a name="l00245"></a>00245 
<a name="l00249"></a><a class="code" href="classsf_1_1PostFX.htm#e4f7f8c7b1b28f80a41f3e9680248eba">00249</a> <a class="code" href="classsf_1_1PostFX.htm" title="PostFX is used to apply a post effect to a window.">PostFX</a>&amp; <a class="code" href="classsf_1_1PostFX.htm#e4f7f8c7b1b28f80a41f3e9680248eba" title="Assignment operator.">PostFX::operator =</a>(<span class="keyword">const</span> <a class="code" href="classsf_1_1PostFX.htm" title="PostFX is used to apply a post effect to a window.">PostFX</a>&amp; Other)
<a name="l00250"></a>00250 {
<a name="l00251"></a>00251     <a class="code" href="classsf_1_1PostFX.htm" title="PostFX is used to apply a post effect to a window.">PostFX</a> Temp(Other);
<a name="l00252"></a>00252 
<a name="l00253"></a>00253     std::swap(myShaderProgram,  Temp.<a class="code" href="classsf_1_1PostFX.htm#b374a6e0d7f80fd340616cd79657cfee" title="OpenGL identifier for the program.">myShaderProgram</a>);
<a name="l00254"></a>00254     std::swap(myTextures,       Temp.<a class="code" href="classsf_1_1PostFX.htm#3586a2f71d2b449404af64621570f428" title="Texture variables in the effect.">myTextures</a>);
<a name="l00255"></a>00255     std::swap(myFragmentShader, Temp.<a class="code" href="classsf_1_1PostFX.htm#953e57703bb2ef17c5167efda937e837" title="Fragment shader source code.">myFragmentShader</a>);
<a name="l00256"></a>00256     std::swap(myFrameBuffer,    Temp.<a class="code" href="classsf_1_1PostFX.htm#529a8974f7a0f0ae9e5232970861e690" title="Texture containing the current frame buffer.">myFrameBuffer</a>);
<a name="l00257"></a>00257 
<a name="l00258"></a>00258     <span class="keywordflow">return</span> *<span class="keyword">this</span>;
<a name="l00259"></a>00259 }
<a name="l00260"></a>00260 
<a name="l00261"></a>00261 
<a name="l00265"></a><a class="code" href="classsf_1_1PostFX.htm#617259ccc0f380c4818f92b7779c128e">00265</a> <span class="keywordtype">bool</span> <a class="code" href="classsf_1_1PostFX.htm#617259ccc0f380c4818f92b7779c128e" title="Tell whether or not the system supports post-effects.">PostFX::CanUsePostFX</a>()
<a name="l00266"></a>00266 {
<a name="l00267"></a>00267     <span class="comment">// Make sure we have a valid context</span>
<a name="l00268"></a>00268     priv::GraphicsContext Ctx;
<a name="l00269"></a>00269 
<a name="l00270"></a>00270     <span class="keywordflow">return</span> glewIsSupported(<span class="stringliteral">"GL_ARB_shading_language_100"</span>) != 0 &amp;&amp;
<a name="l00271"></a>00271            glewIsSupported(<span class="stringliteral">"GL_ARB_shader_objects"</span>)       != 0 &amp;&amp;
<a name="l00272"></a>00272            glewIsSupported(<span class="stringliteral">"GL_ARB_vertex_shader"</span>)        != 0 &amp;&amp;
<a name="l00273"></a>00273            glewIsSupported(<span class="stringliteral">"GL_ARB_fragment_shader"</span>)      != 0;
<a name="l00274"></a>00274 }
<a name="l00275"></a>00275 
<a name="l00276"></a>00276 
<a name="l00280"></a><a class="code" href="classsf_1_1PostFX.htm#c059bb07500cf27ffa1536512b8a24cc">00280</a> <span class="keywordtype">void</span> <a class="code" href="classsf_1_1PostFX.htm#c059bb07500cf27ffa1536512b8a24cc" title="/see Drawable::Render">PostFX::Render</a>(<a class="code" href="classsf_1_1RenderTarget.htm" title="Base class for all render targets (window, image, .">RenderTarget</a>&amp; Target)<span class="keyword"> const</span>
<a name="l00281"></a>00281 <span class="keyword"></span>{
<a name="l00282"></a>00282     <span class="comment">// Check that we have a valid program</span>
<a name="l00283"></a>00283     <span class="keywordflow">if</span> (!myShaderProgram)
<a name="l00284"></a>00284         <span class="keywordflow">return</span>;
<a name="l00285"></a>00285 
<a name="l00286"></a>00286     <span class="comment">// Copy the current framebuffer pixels to our frame buffer texture</span>
<a name="l00287"></a>00287     <span class="comment">// The ugly cast is temporary until PostFx are rewritten :)</span>
<a name="l00288"></a>00288     myFrameBuffer.<a class="code" href="classsf_1_1Image.htm#96597746f70ce1925ec82d2df8ae6974" title="Create the image from the current contents of the given window.">CopyScreen</a>((<a class="code" href="classsf_1_1RenderWindow.htm" title="Simple wrapper for sf::Window that allows easy 2D rendering.">RenderWindow</a>&amp;)Target);
<a name="l00289"></a>00289 
<a name="l00290"></a>00290     <span class="comment">// Enable program</span>
<a name="l00291"></a>00291     GLCheck(glUseProgramObjectARB(myShaderProgram));
<a name="l00292"></a>00292 
<a name="l00293"></a>00293     <span class="comment">// Bind textures</span>
<a name="l00294"></a>00294     TextureTable::const_iterator ItTex = myTextures.begin();
<a name="l00295"></a>00295     <span class="keywordflow">for</span> (std::size_t i = 0; i &lt; myTextures.size(); ++i)
<a name="l00296"></a>00296     {
<a name="l00297"></a>00297         <span class="keywordtype">int</span> Location = glGetUniformLocationARB(myShaderProgram, ItTex-&gt;first.c_str());
<a name="l00298"></a>00298         GLCheck(glUniform1iARB(Location, static_cast&lt;GLint&gt;(i)));
<a name="l00299"></a>00299         GLCheck(glActiveTextureARB(static_cast&lt;GLenum&gt;(GL_TEXTURE0_ARB + i)));
<a name="l00300"></a>00300         ItTex-&gt;second-&gt;Bind();
<a name="l00301"></a>00301         ItTex++;
<a name="l00302"></a>00302     }
<a name="l00303"></a>00303 
<a name="l00304"></a>00304     <span class="comment">// Compute the texture coordinates (in case the texture is larger than the screen, or flipped)</span>
<a name="l00305"></a>00305     <a class="code" href="classsf_1_1Rect.htm">IntRect</a> FrameBufferRect(0, 0, myFrameBuffer.<a class="code" href="classsf_1_1Image.htm#896f09a4eb769b5b866b6dde9c1a25e7" title="Return the width of the image.">GetWidth</a>(), myFrameBuffer.<a class="code" href="classsf_1_1Image.htm#f2131512b6290fd96fd10c539723fe89" title="Return the height of the image.">GetHeight</a>());
<a name="l00306"></a>00306     <a class="code" href="classsf_1_1Rect.htm">FloatRect</a> TexCoords = myFrameBuffer.<a class="code" href="classsf_1_1Image.htm#0477b06f784afcc1662f69ad5bce5c3b" title="Convert a subrect expressed in pixels, into float texture coordinates.">GetTexCoords</a>(FrameBufferRect);
<a name="l00307"></a>00307 
<a name="l00308"></a>00308     <span class="comment">// Render a fullscreen quad using the effect on our framebuffer</span>
<a name="l00309"></a>00309     <a class="code" href="classsf_1_1Rect.htm">FloatRect</a> Screen = Target.<a class="code" href="classsf_1_1RenderTarget.htm#d426b7ba51cc6970fb828a2bef020274" title="Get the current view.">GetView</a>().<a class="code" href="classsf_1_1View.htm#af368a69233ee426a5fbd63dde1cc4bd" title="Get the bounding rectangle of the view.">GetRect</a>();
<a name="l00310"></a>00310     glBegin(GL_QUADS);
<a name="l00311"></a>00311          glTexCoord2f(TexCoords.Left,  TexCoords.Top);    glVertex2f(Screen.<a class="code" href="classsf_1_1Rect.htm#9619c6b9741163c86eea6a0b84b2f9b3" title="Left coordinate of the rectangle.">Left</a>,  Screen.<a class="code" href="classsf_1_1Rect.htm#b234b311011892cfd8a3e831dec341a8" title="Bottom coordinate of the rectangle.">Bottom</a>);
<a name="l00312"></a>00312          glTexCoord2f(TexCoords.Right, TexCoords.Top);    glVertex2f(Screen.<a class="code" href="classsf_1_1Rect.htm#6ce87895496327187a20a46dc2479383" title="Right coordinate of the rectangle.">Right</a>, Screen.<a class="code" href="classsf_1_1Rect.htm#b234b311011892cfd8a3e831dec341a8" title="Bottom coordinate of the rectangle.">Bottom</a>);
<a name="l00313"></a>00313          glTexCoord2f(TexCoords.Right, TexCoords.Bottom); glVertex2f(Screen.<a class="code" href="classsf_1_1Rect.htm#6ce87895496327187a20a46dc2479383" title="Right coordinate of the rectangle.">Right</a>, Screen.<a class="code" href="classsf_1_1Rect.htm#4addcc0bf656fb5f2f80d406cf90c047" title="Top coordinate of the rectangle.">Top</a>);
<a name="l00314"></a>00314          glTexCoord2f(TexCoords.Left,  TexCoords.Bottom); glVertex2f(Screen.<a class="code" href="classsf_1_1Rect.htm#9619c6b9741163c86eea6a0b84b2f9b3" title="Left coordinate of the rectangle.">Left</a>,  Screen.<a class="code" href="classsf_1_1Rect.htm#4addcc0bf656fb5f2f80d406cf90c047" title="Top coordinate of the rectangle.">Top</a>);
<a name="l00315"></a>00315     glEnd();
<a name="l00316"></a>00316 
<a name="l00317"></a>00317     <span class="comment">// Disable program</span>
<a name="l00318"></a>00318     GLCheck(glUseProgramObjectARB(0));
<a name="l00319"></a>00319 
<a name="l00320"></a>00320     <span class="comment">// Disable texture units</span>
<a name="l00321"></a>00321     <span class="keywordflow">for</span> (std::size_t i = 0; i &lt; myTextures.size(); ++i)
<a name="l00322"></a>00322     {
<a name="l00323"></a>00323         GLCheck(glActiveTextureARB(static_cast&lt;GLenum&gt;(GL_TEXTURE0_ARB + i)));
<a name="l00324"></a>00324         GLCheck(glBindTexture(GL_TEXTURE_2D, 0));
<a name="l00325"></a>00325     }
<a name="l00326"></a>00326     GLCheck(glActiveTextureARB(GL_TEXTURE0_ARB));
<a name="l00327"></a>00327 }
<a name="l00328"></a>00328 
<a name="l00329"></a>00329 
<a name="l00334"></a>00334 std::string PostFX::PreprocessEffect(std::istream&amp; File)
<a name="l00335"></a>00335 {
<a name="l00336"></a>00336     <span class="comment">// Initialize output string</span>
<a name="l00337"></a>00337     std::set&lt;std::string&gt; myTextures;
<a name="l00338"></a>00338     std::string Out = <span class="stringliteral">""</span>;
<a name="l00339"></a>00339 
<a name="l00340"></a>00340     <span class="comment">// Variable declarations</span>
<a name="l00341"></a>00341     std::string Line;
<a name="l00342"></a>00342     <span class="keywordflow">while</span> (std::getline(File, Line) &amp;&amp; (Line.substr(0, 6) != <span class="stringliteral">"effect"</span>))
<a name="l00343"></a>00343     {
<a name="l00344"></a>00344         <span class="comment">// Remove the ending '\r', if any</span>
<a name="l00345"></a>00345         <span class="keywordflow">if</span> (!Line.empty() &amp;&amp; (Line[Line.size() - 1] == <span class="charliteral">'\r'</span>))
<a name="l00346"></a>00346             Line.erase(Line.size() - 1);
<a name="l00347"></a>00347 
<a name="l00348"></a>00348         <span class="comment">// Skip empty lines</span>
<a name="l00349"></a>00349         <span class="keywordflow">if</span> (Line == <span class="stringliteral">""</span>)
<a name="l00350"></a>00350             <span class="keywordflow">continue</span>;
<a name="l00351"></a>00351 
<a name="l00352"></a>00352         <span class="comment">// Extract variables type and name and convert them</span>
<a name="l00353"></a>00353         std::string Type, Name;
<a name="l00354"></a>00354         std::istringstream iss(Line);
<a name="l00355"></a>00355         <span class="keywordflow">if</span> (!(iss &gt;&gt; Type &gt;&gt; Name))
<a name="l00356"></a>00356         {
<a name="l00357"></a>00357             std::cerr &lt;&lt; <span class="stringliteral">"Post-effect error : invalid declaration (should be \"[type][name]\")"</span> &lt;&lt; std::endl
<a name="l00358"></a>00358                       &lt;&lt; <span class="stringliteral">"&gt; "</span> &lt;&lt; Line &lt;&lt; std::endl;
<a name="l00359"></a>00359             <span class="keywordflow">return</span> <span class="stringliteral">""</span>;
<a name="l00360"></a>00360         }
<a name="l00361"></a>00361 
<a name="l00362"></a>00362         <span class="keywordflow">if</span> (Type == <span class="stringliteral">"texture"</span>)
<a name="l00363"></a>00363         {
<a name="l00364"></a>00364             <span class="comment">// Textures need some checking and conversion</span>
<a name="l00365"></a>00365             <span class="keywordflow">if</span> (myTextures.find(Name) != myTextures.end())
<a name="l00366"></a>00366             {
<a name="l00367"></a>00367                 std::cerr &lt;&lt; <span class="stringliteral">"Post-effect error : texture \""</span> &lt;&lt; Name &lt;&lt; <span class="stringliteral">"\" already exists"</span> &lt;&lt; std::endl;
<a name="l00368"></a>00368                 <span class="keywordflow">return</span> <span class="stringliteral">""</span>;
<a name="l00369"></a>00369             }
<a name="l00370"></a>00370 
<a name="l00371"></a>00371             Out += <span class="stringliteral">"uniform sampler2D "</span> + Name + <span class="stringliteral">";\n"</span>;
<a name="l00372"></a>00372             myTextures.insert(Name);
<a name="l00373"></a>00373         }
<a name="l00374"></a>00374         <span class="keywordflow">else</span>
<a name="l00375"></a>00375         {
<a name="l00376"></a>00376             <span class="comment">// Other types are just copied to output with "uniform" prefix</span>
<a name="l00377"></a>00377             Out += <span class="stringliteral">"uniform "</span> + Type + <span class="stringliteral">" "</span> + Name + <span class="stringliteral">";\n"</span>;
<a name="l00378"></a>00378         }
<a name="l00379"></a>00379     }
<a name="l00380"></a>00380 
<a name="l00381"></a>00381     <span class="comment">// Effect code</span>
<a name="l00382"></a>00382     Out += <span class="stringliteral">"void main()\n"</span>;
<a name="l00383"></a>00383     <span class="keywordflow">while</span> (std::getline(File, Line))
<a name="l00384"></a>00384     {
<a name="l00385"></a>00385         <span class="comment">// Replace any texture lookup "T(" by "texture2D(T, "</span>
<a name="l00386"></a>00386         <span class="keywordflow">for</span> (std::set&lt;std::string&gt;::const_iterator i = myTextures.begin(); i != myTextures.end(); ++i)
<a name="l00387"></a>00387         {
<a name="l00388"></a>00388             std::string::size_type Pos = Line.find(*i);
<a name="l00389"></a>00389             <span class="keywordflow">if</span> (Pos != std::string::npos)
<a name="l00390"></a>00390                 Line.replace(Pos, i-&gt;size() + 1, <span class="stringliteral">"texture2D("</span> + *i + <span class="stringliteral">", "</span>);
<a name="l00391"></a>00391         }
<a name="l00392"></a>00392 
<a name="l00393"></a>00393         <span class="comment">// Replace "_in" by "gl_TexCoord[0].xy"</span>
<a name="l00394"></a>00394         <span class="keywordflow">for</span> (std::string::size_type Pos = Line.find(<span class="stringliteral">"_in"</span>); Pos != std::string::npos; Pos = Line.find(<span class="stringliteral">"_in"</span>))
<a name="l00395"></a>00395             Line.replace(Pos, 3, <span class="stringliteral">"gl_TexCoord[0].xy"</span>);
<a name="l00396"></a>00396 
<a name="l00397"></a>00397         <span class="comment">// Replace "_out" by "gl_FragColor"</span>
<a name="l00398"></a>00398         <span class="keywordflow">for</span> (std::string::size_type Pos = Line.find(<span class="stringliteral">"_out"</span>); Pos != std::string::npos; Pos = Line.find(<span class="stringliteral">"_out"</span>))
<a name="l00399"></a>00399             Line.replace(Pos, 4, <span class="stringliteral">"gl_FragColor"</span>);
<a name="l00400"></a>00400 
<a name="l00401"></a>00401         <span class="comment">// Write modified line to output string</span>
<a name="l00402"></a>00402         Out += Line + <span class="stringliteral">"\n"</span>;
<a name="l00403"></a>00403     }
<a name="l00404"></a>00404 
<a name="l00405"></a>00405     <span class="keywordflow">return</span> Out;
<a name="l00406"></a>00406 }
<a name="l00407"></a>00407 
<a name="l00408"></a>00408 
<a name="l00412"></a>00412 <span class="keywordtype">void</span> PostFX::CreateProgram()
<a name="l00413"></a>00413 {
<a name="l00414"></a>00414     <span class="comment">// Make sure we have a valid context</span>
<a name="l00415"></a>00415     priv::GraphicsContext Ctx;
<a name="l00416"></a>00416 
<a name="l00417"></a>00417     <span class="comment">// Check that we can use post-FX !</span>
<a name="l00418"></a>00418     <span class="keywordflow">if</span> (!<a class="code" href="classsf_1_1PostFX.htm#617259ccc0f380c4818f92b7779c128e" title="Tell whether or not the system supports post-effects.">CanUsePostFX</a>())
<a name="l00419"></a>00419     {
<a name="l00420"></a>00420         std::cerr &lt;&lt; <span class="stringliteral">"Failed to create a PostFX : your system doesn't support effects"</span> &lt;&lt; std::endl;
<a name="l00421"></a>00421         <span class="keywordflow">return</span>;
<a name="l00422"></a>00422     }
<a name="l00423"></a>00423 
<a name="l00424"></a>00424     <span class="comment">// Destroy effect program if it was already created</span>
<a name="l00425"></a>00425     <span class="keywordflow">if</span> (myShaderProgram)
<a name="l00426"></a>00426         GLCheck(glDeleteObjectARB(myShaderProgram));
<a name="l00427"></a>00427 
<a name="l00428"></a>00428     <span class="comment">// Define vertex shader source (we provide it directly as it doesn't have to change)</span>
<a name="l00429"></a>00429     <span class="keyword">static</span> <span class="keyword">const</span> std::string VertexShaderSrc =
<a name="l00430"></a>00430         <span class="stringliteral">"void main()"</span>
<a name="l00431"></a>00431         <span class="stringliteral">"{"</span>
<a name="l00432"></a>00432         <span class="stringliteral">"    gl_TexCoord[0] = gl_MultiTexCoord0;"</span>
<a name="l00433"></a>00433         <span class="stringliteral">"    gl_Position = ftransform();"</span>
<a name="l00434"></a>00434         <span class="stringliteral">"}"</span>;
<a name="l00435"></a>00435 
<a name="l00436"></a>00436     <span class="comment">// Create the program</span>
<a name="l00437"></a>00437     myShaderProgram = glCreateProgramObjectARB();
<a name="l00438"></a>00438 
<a name="l00439"></a>00439     <span class="comment">// Create the shaders</span>
<a name="l00440"></a>00440     GLhandleARB VertexShader   = glCreateShaderObjectARB(GL_VERTEX_SHADER_ARB);
<a name="l00441"></a>00441     GLhandleARB FragmentShader = glCreateShaderObjectARB(GL_FRAGMENT_SHADER_ARB);
<a name="l00442"></a>00442 
<a name="l00443"></a>00443     <span class="comment">// Compile them</span>
<a name="l00444"></a>00444     <span class="keyword">const</span> <span class="keywordtype">char</span>* VertexSrc   = VertexShaderSrc.c_str();
<a name="l00445"></a>00445     <span class="keyword">const</span> <span class="keywordtype">char</span>* FragmentSrc = myFragmentShader.c_str();
<a name="l00446"></a>00446     GLCheck(glShaderSourceARB(VertexShader,   1, &amp;VertexSrc,   NULL));
<a name="l00447"></a>00447     GLCheck(glShaderSourceARB(FragmentShader, 1, &amp;FragmentSrc, NULL));
<a name="l00448"></a>00448     GLCheck(glCompileShaderARB(VertexShader));
<a name="l00449"></a>00449     GLCheck(glCompileShaderARB(FragmentShader));
<a name="l00450"></a>00450 
<a name="l00451"></a>00451     <span class="comment">// Check the compile logs</span>
<a name="l00452"></a>00452     GLint Success;
<a name="l00453"></a>00453     GLCheck(glGetObjectParameterivARB(VertexShader, GL_OBJECT_COMPILE_STATUS_ARB, &amp;Success));
<a name="l00454"></a>00454     <span class="keywordflow">if</span> (Success == GL_FALSE)
<a name="l00455"></a>00455     {
<a name="l00456"></a>00456         <span class="keywordtype">char</span> CompileLog[1024];
<a name="l00457"></a>00457         GLCheck(glGetInfoLogARB(VertexShader, <span class="keyword">sizeof</span>(CompileLog), 0, CompileLog));
<a name="l00458"></a>00458         std::cerr &lt;&lt; <span class="stringliteral">"Failed to compile post-effect :"</span> &lt;&lt; std::endl
<a name="l00459"></a>00459                   &lt;&lt; CompileLog &lt;&lt; std::endl;
<a name="l00460"></a>00460         GLCheck(glDeleteObjectARB(VertexShader));
<a name="l00461"></a>00461         GLCheck(glDeleteObjectARB(FragmentShader));
<a name="l00462"></a>00462         GLCheck(glDeleteObjectARB(myShaderProgram));
<a name="l00463"></a>00463         myShaderProgram = 0;
<a name="l00464"></a>00464         <span class="keywordflow">return</span>;
<a name="l00465"></a>00465     }
<a name="l00466"></a>00466     GLCheck(glGetObjectParameterivARB(FragmentShader, GL_OBJECT_COMPILE_STATUS_ARB, &amp;Success));
<a name="l00467"></a>00467     <span class="keywordflow">if</span> (Success == GL_FALSE)
<a name="l00468"></a>00468     {
<a name="l00469"></a>00469         <span class="keywordtype">char</span> CompileLog[1024];
<a name="l00470"></a>00470         GLCheck(glGetInfoLogARB(FragmentShader, <span class="keyword">sizeof</span>(CompileLog), 0, CompileLog));
<a name="l00471"></a>00471         std::cerr &lt;&lt; <span class="stringliteral">"Failed to compile post-effect :"</span> &lt;&lt; std::endl
<a name="l00472"></a>00472                   &lt;&lt; CompileLog &lt;&lt; std::endl;
<a name="l00473"></a>00473         GLCheck(glDeleteObjectARB(VertexShader));
<a name="l00474"></a>00474         GLCheck(glDeleteObjectARB(FragmentShader));
<a name="l00475"></a>00475         GLCheck(glDeleteObjectARB(myShaderProgram));
<a name="l00476"></a>00476         myShaderProgram = 0;
<a name="l00477"></a>00477         <span class="keywordflow">return</span>;
<a name="l00478"></a>00478     }
<a name="l00479"></a>00479 
<a name="l00480"></a>00480     <span class="comment">// Attach the shaders to the program</span>
<a name="l00481"></a>00481     GLCheck(glAttachObjectARB(myShaderProgram, VertexShader));
<a name="l00482"></a>00482     GLCheck(glAttachObjectARB(myShaderProgram, FragmentShader));
<a name="l00483"></a>00483 
<a name="l00484"></a>00484     <span class="comment">// We can now delete the shaders</span>
<a name="l00485"></a>00485     GLCheck(glDeleteObjectARB(VertexShader));
<a name="l00486"></a>00486     GLCheck(glDeleteObjectARB(FragmentShader));
<a name="l00487"></a>00487 
<a name="l00488"></a>00488     <span class="comment">// Link the program</span>
<a name="l00489"></a>00489     GLCheck(glLinkProgramARB(myShaderProgram));
<a name="l00490"></a>00490 
<a name="l00491"></a>00491     <span class="comment">// Get link log</span>
<a name="l00492"></a>00492     GLCheck(glGetObjectParameterivARB(myShaderProgram, GL_OBJECT_LINK_STATUS_ARB, &amp;Success));
<a name="l00493"></a>00493     <span class="keywordflow">if</span> (Success == GL_FALSE)
<a name="l00494"></a>00494     {
<a name="l00495"></a>00495         <span class="comment">// Oops... link errors</span>
<a name="l00496"></a>00496         <span class="keywordtype">char</span> LinkLog[1024];
<a name="l00497"></a>00497         GLCheck(glGetInfoLogARB(myShaderProgram, <span class="keyword">sizeof</span>(LinkLog), 0, LinkLog));
<a name="l00498"></a>00498         std::cerr &lt;&lt; <span class="stringliteral">"Failed to link post-effect :"</span> &lt;&lt; std::endl
<a name="l00499"></a>00499                   &lt;&lt; LinkLog &lt;&lt; std::endl;
<a name="l00500"></a>00500         GLCheck(glDeleteObjectARB(myShaderProgram));
<a name="l00501"></a>00501         myShaderProgram = 0;
<a name="l00502"></a>00502         <span class="keywordflow">return</span>;
<a name="l00503"></a>00503     }
<a name="l00504"></a>00504 }
<a name="l00505"></a>00505 
<a name="l00506"></a>00506 } <span class="comment">// namespace sf</span>
</pre></div></div>

        <p id="footer">
            &nbsp;::&nbsp; Copyright &copy; 2007-2008 Laurent Gomila, all rights reserved &nbsp;::&nbsp;
            Documentation generated by <a href="http://www.doxygen.org/" title="doxygen website">doxygen 1.5.2</a> &nbsp;::&nbsp;
        </p>

    </body>
</html>