aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/others/irrlicht-1.8.1/doc/html/example010.html
diff options
context:
space:
mode:
Diffstat (limited to 'src/others/irrlicht-1.8.1/doc/html/example010.html')
-rw-r--r--src/others/irrlicht-1.8.1/doc/html/example010.html455
1 files changed, 455 insertions, 0 deletions
diff --git a/src/others/irrlicht-1.8.1/doc/html/example010.html b/src/others/irrlicht-1.8.1/doc/html/example010.html
new file mode 100644
index 0000000..c74a26c
--- /dev/null
+++ b/src/others/irrlicht-1.8.1/doc/html/example010.html
@@ -0,0 +1,455 @@
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
2<html xmlns="http://www.w3.org/1999/xhtml">
3<head>
4<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
5<title>Irrlicht 3D Engine: Tutorial 10: Shaders</title>
6
7<link href="tabs.css" rel="stylesheet" type="text/css"/>
8<link href="doxygen.css" rel="stylesheet" type="text/css" />
9<link href="navtree.css" rel="stylesheet" type="text/css"/>
10<script type="text/javascript" src="jquery.js"></script>
11<script type="text/javascript" src="resize.js"></script>
12<script type="text/javascript" src="navtree.js"></script>
13<script type="text/javascript">
14 $(document).ready(initResizable);
15</script>
16<link href="search/search.css" rel="stylesheet" type="text/css"/>
17<script type="text/javascript" src="search/search.js"></script>
18<script type="text/javascript">
19 $(document).ready(function() { searchBox.OnSelectItem(0); });
20</script>
21
22</head>
23<body>
24<div id="top"><!-- do not remove this div! -->
25
26
27<div id="titlearea">
28<table cellspacing="0" cellpadding="0">
29 <tbody>
30 <tr style="height: 56px;">
31
32 <td id="projectlogo"><img alt="Logo" src="irrlichtlogo.png"/></td>
33
34
35 <td style="padding-left: 0.5em;">
36 <div id="projectname">Irrlicht 3D Engine
37
38 </div>
39
40 </td>
41
42
43
44
45 <td> <div id="MSearchBox" class="MSearchBoxInactive">
46 <span class="left">
47 <img id="MSearchSelect" src="search/mag_sel.png"
48 onmouseover="return searchBox.OnSearchSelectShow()"
49 onmouseout="return searchBox.OnSearchSelectHide()"
50 alt=""/>
51 <input type="text" id="MSearchField" value="Search" accesskey="S"
52 onfocus="searchBox.OnSearchFieldFocus(true)"
53 onblur="searchBox.OnSearchFieldFocus(false)"
54 onkeyup="searchBox.OnSearchFieldChange(event)"/>
55 </span><span class="right">
56 <a id="MSearchClose" href="javascript:searchBox.CloseResultsWindow()"><img id="MSearchCloseImg" border="0" src="search/close.png" alt=""/></a>
57 </span>
58 </div>
59</td>
60
61
62 </tr>
63 </tbody>
64</table>
65</div>
66
67<!-- Generated by Doxygen 1.7.5.1 -->
68<script type="text/javascript">
69var searchBox = new SearchBox("searchBox", "search",false,'Search');
70</script>
71<script type="text/javascript" src="dynsections.js"></script>
72</div>
73<div id="side-nav" class="ui-resizable side-nav-resizable">
74 <div id="nav-tree">
75 <div id="nav-tree-contents">
76 </div>
77 </div>
78 <div id="splitbar" style="-moz-user-select:none;"
79 class="ui-resizable-handle">
80 </div>
81</div>
82<script type="text/javascript">
83 initNavTree('example010.html','');
84</script>
85<div id="doc-content">
86<div class="header">
87 <div class="headertitle">
88<div class="title">Tutorial 10: Shaders </div> </div>
89</div>
90<div class="contents">
91<div class="textblock"><div class="image">
92<img src="010shot.jpg" alt="010shot.jpg"/>
93</div>
94 <p>This tutorial shows how to use shaders for D3D8, D3D9, OpenGL, and Cg with the engine and how to create new material types with them. It also shows how to disable the generation of mipmaps at texture loading, and how to use text scene nodes.</p>
95<p>This tutorial does not explain how shaders work. I would recommend to read the D3D, OpenGL, or Cg documentation, to search a tutorial, or to read a book about this.</p>
96<p>At first, we need to include all headers and do the stuff we always do, like in nearly all other tutorials: </p>
97<div class="fragment"><pre class="fragment"><span class="preprocessor">#include &lt;<a class="code" href="irrlicht_8h.html" title="Main header file of the irrlicht, the only file needed to include.">irrlicht.h</a>&gt;</span>
98<span class="preprocessor">#include &lt;iostream&gt;</span>
99<span class="preprocessor">#include &quot;<a class="code" href="driver_choice_8h.html">driverChoice.h</a>&quot;</span>
100
101<span class="keyword">using namespace </span>irr;
102
103<span class="preprocessor">#ifdef _MSC_VER</span>
104<span class="preprocessor"></span><span class="preprocessor">#pragma comment(lib, &quot;Irrlicht.lib&quot;)</span>
105<span class="preprocessor">#endif</span>
106</pre></div><p>Because we want to use some interesting shaders in this tutorials, we need to set some data for them to make them able to compute nice colors. In this example, we'll use a simple vertex shader which will calculate the color of the vertex based on the position of the camera. For this, the shader needs the following data: The inverted world matrix for transforming the normal, the clip matrix for transforming the position, the camera position and the world position of the object for the calculation of the angle of light, and the color of the light. To be able to tell the shader all this data every frame, we have to derive a class from the IShaderConstantSetCallBack interface and override its only method, namely OnSetConstants(). This method will be called every time the material is set. The method setVertexShaderConstant() of the IMaterialRendererServices interface is used to set the data the shader needs. If the user chose to use a High Level shader language like HLSL instead of Assembler in this example, you have to set the variable name as parameter instead of the register index. </p>
107<div class="fragment"><pre class="fragment">IrrlichtDevice* device = 0;
108<span class="keywordtype">bool</span> UseHighLevelShaders = <span class="keyword">false</span>;
109<span class="keywordtype">bool</span> UseCgShaders = <span class="keyword">false</span>;
110
111<span class="keyword">class </span>MyShaderCallBack : <span class="keyword">public</span> video::IShaderConstantSetCallBack
112{
113<span class="keyword">public</span>:
114
115 <span class="keyword">virtual</span> <span class="keywordtype">void</span> OnSetConstants(video::IMaterialRendererServices* services,
116 <a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> userData)
117 {
118 video::IVideoDriver* driver = services-&gt;getVideoDriver();
119
120 <span class="comment">// set inverted world matrix</span>
121 <span class="comment">// if we are using highlevel shaders (the user can select this when</span>
122 <span class="comment">// starting the program), we must set the constants by name.</span>
123
124 <a class="code" href="namespaceirr_1_1core.html#a73fa92e638c5ca97efd72da307cc9b65" title="Typedef for f32 matrix.">core::matrix4</a> invWorld = driver-&gt;getTransform(<a class="code" href="namespaceirr_1_1video.html#a15b57657a320243be03ae6f66fcff43da843cf42adb3fa9caf61c9e228cf14e85" title="World transformation.">video::ETS_WORLD</a>);
125 invWorld.makeInverse();
126
127 <span class="keywordflow">if</span> (UseHighLevelShaders)
128 services-&gt;setVertexShaderConstant(<span class="stringliteral">&quot;mInvWorld&quot;</span>, invWorld.pointer(), 16);
129 <span class="keywordflow">else</span>
130 services-&gt;setVertexShaderConstant(invWorld.pointer(), 0, 4);
131
132 <span class="comment">// set clip matrix</span>
133
134 <a class="code" href="namespaceirr_1_1core.html#a73fa92e638c5ca97efd72da307cc9b65" title="Typedef for f32 matrix.">core::matrix4</a> worldViewProj;
135 worldViewProj = driver-&gt;getTransform(<a class="code" href="namespaceirr_1_1video.html#a15b57657a320243be03ae6f66fcff43dae7ec186418508c67a7562af012d7b63f" title="Projection transformation.">video::ETS_PROJECTION</a>);
136 worldViewProj *= driver-&gt;getTransform(<a class="code" href="namespaceirr_1_1video.html#a15b57657a320243be03ae6f66fcff43da152f4262d5874186e0288934c7d31e14" title="View transformation.">video::ETS_VIEW</a>);
137 worldViewProj *= driver-&gt;getTransform(<a class="code" href="namespaceirr_1_1video.html#a15b57657a320243be03ae6f66fcff43da843cf42adb3fa9caf61c9e228cf14e85" title="World transformation.">video::ETS_WORLD</a>);
138
139 <span class="keywordflow">if</span> (UseHighLevelShaders)
140 services-&gt;setVertexShaderConstant(<span class="stringliteral">&quot;mWorldViewProj&quot;</span>, worldViewProj.pointer(), 16);
141 <span class="keywordflow">else</span>
142 services-&gt;setVertexShaderConstant(worldViewProj.pointer(), 4, 4);
143
144 <span class="comment">// set camera position</span>
145
146 <a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a> pos = device-&gt;getSceneManager()-&gt;
147 getActiveCamera()-&gt;getAbsolutePosition();
148
149 <span class="keywordflow">if</span> (UseHighLevelShaders)
150 services-&gt;setVertexShaderConstant(<span class="stringliteral">&quot;mLightPos&quot;</span>, reinterpret_cast&lt;f32*&gt;(&amp;pos), 3);
151 <span class="keywordflow">else</span>
152 services-&gt;setVertexShaderConstant(reinterpret_cast&lt;f32*&gt;(&amp;pos), 8, 1);
153
154 <span class="comment">// set light color</span>
155
156 video::SColorf col(0.0f,1.0f,1.0f,0.0f);
157
158 <span class="keywordflow">if</span> (UseHighLevelShaders)
159 services-&gt;setVertexShaderConstant(<span class="stringliteral">&quot;mLightColor&quot;</span>,
160 reinterpret_cast&lt;f32*&gt;(&amp;col), 4);
161 <span class="keywordflow">else</span>
162 services-&gt;setVertexShaderConstant(reinterpret_cast&lt;f32*&gt;(&amp;col), 9, 1);
163
164 <span class="comment">// set transposed world matrix</span>
165
166 <a class="code" href="namespaceirr_1_1core.html#a73fa92e638c5ca97efd72da307cc9b65" title="Typedef for f32 matrix.">core::matrix4</a> world = driver-&gt;getTransform(<a class="code" href="namespaceirr_1_1video.html#a15b57657a320243be03ae6f66fcff43da843cf42adb3fa9caf61c9e228cf14e85" title="World transformation.">video::ETS_WORLD</a>);
167 world = world.getTransposed();
168
169 <span class="keywordflow">if</span> (UseHighLevelShaders)
170 {
171 services-&gt;setVertexShaderConstant(<span class="stringliteral">&quot;mTransWorld&quot;</span>, world.pointer(), 16);
172
173 <span class="comment">// set texture, for textures you can use both an int and a float setPixelShaderConstant interfaces (You need it only for an OpenGL driver).</span>
174 <a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> TextureLayerID = 0;
175 <span class="keywordflow">if</span> (UseHighLevelShaders)
176 services-&gt;setPixelShaderConstant(<span class="stringliteral">&quot;myTexture&quot;</span>, &amp;TextureLayerID, 1);
177 }
178 <span class="keywordflow">else</span>
179 services-&gt;setVertexShaderConstant(world.pointer(), 10, 4);
180 }
181};
182</pre></div><p>The next few lines start up the engine just like in most other tutorials before. But in addition, we ask the user if he wants to use high level shaders in this example, if he selected a driver which is capable of doing so. </p>
183<div class="fragment"><pre class="fragment"><span class="keywordtype">int</span> main()
184{
185 <span class="comment">// ask user for driver</span>
186 <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0" title="An enum for all types of drivers the Irrlicht Engine supports.">video::E_DRIVER_TYPE</a> driverType=driverChoiceConsole();
187 <span class="keywordflow">if</span> (driverType==<a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0ae685cada50f8c100403134d932d0414c" title="No driver, just for counting the elements.">video::EDT_COUNT</a>)
188 <span class="keywordflow">return</span> 1;
189
190 <span class="comment">// ask the user if we should use high level shaders for this example</span>
191 <span class="keywordflow">if</span> (driverType == <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0a4691ca314f9018f508dcf2c57dcaacec" title="Direct3D 9 device, only available on Win32 platforms.">video::EDT_DIRECT3D9</a> ||
192 driverType == <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0a2715182a79f1cb8e2826fd68a8150a53" title="OpenGL device, available on most platforms.">video::EDT_OPENGL</a>)
193 {
194 <span class="keywordtype">char</span> i;
195 printf(<span class="stringliteral">&quot;Please press &#39;y&#39; if you want to use high level shaders.\n&quot;</span>);
196 std::cin &gt;&gt; i;
197 <span class="keywordflow">if</span> (i == <span class="charliteral">&#39;y&#39;</span>)
198 {
199 UseHighLevelShaders = <span class="keyword">true</span>;
200 printf(<span class="stringliteral">&quot;Please press &#39;y&#39; if you want to use Cg shaders.\n&quot;</span>);
201 std::cin &gt;&gt; i;
202 <span class="keywordflow">if</span> (i == <span class="charliteral">&#39;y&#39;</span>)
203 UseCgShaders = <span class="keyword">true</span>;
204 }
205 }
206
207 <span class="comment">// create device</span>
208 device = <a class="code" href="namespaceirr.html#abaf4d8719cc26b0d30813abf85e47c76" title="Creates an Irrlicht device. The Irrlicht device is the root object for using the engine.">createDevice</a>(driverType, core::dimension2d&lt;u32&gt;(640, 480));
209
210 <span class="keywordflow">if</span> (device == 0)
211 <span class="keywordflow">return</span> 1; <span class="comment">// could not create selected driver.</span>
212
213 video::IVideoDriver* driver = device-&gt;getVideoDriver();
214 scene::ISceneManager* smgr = device-&gt;getSceneManager();
215 gui::IGUIEnvironment* gui = device-&gt;getGUIEnvironment();
216
217 <span class="comment">// Make sure we don&#39;t try Cg without support for it</span>
218 <span class="keywordflow">if</span> (UseCgShaders &amp;&amp; !driver-&gt;queryFeature(<a class="code" href="namespaceirr_1_1video.html#a57b1721e42a79c5dcf8e830e3621e08fa92cc732f2742ef93d0d389a72bc1c236" title="Support for NVidia&#39;s CG shader language.">video::EVDF_CG</a>))
219 {
220 printf(<span class="stringliteral">&quot;Warning: No Cg support, disabling.\n&quot;</span>);
221 UseCgShaders=<span class="keyword">false</span>;
222 }
223</pre></div><p>Now for the more interesting parts. If we are using Direct3D, we want to load vertex and pixel shader programs, if we have OpenGL, we want to use ARB fragment and vertex programs. I wrote the corresponding programs down into the files d3d8.ps, d3d8.vs, d3d9.ps, d3d9.vs, opengl.ps and opengl.vs. We only need the right filenames now. This is done in the following switch. Note, that it is not necessary to write the shaders into text files, like in this example. You can even write the shaders directly as strings into the cpp source file, and use later addShaderMaterial() instead of addShaderMaterialFromFiles(). </p>
224<div class="fragment"><pre class="fragment"> <a class="code" href="namespaceirr_1_1io.html#ab1bdc45edb3f94d8319c02bc0f840ee1" title="Type used for all file system related strings.">io::path</a> vsFileName; <span class="comment">// filename for the vertex shader</span>
225 <a class="code" href="namespaceirr_1_1io.html#ab1bdc45edb3f94d8319c02bc0f840ee1" title="Type used for all file system related strings.">io::path</a> psFileName; <span class="comment">// filename for the pixel shader</span>
226
227 <span class="keywordflow">switch</span>(driverType)
228 {
229 <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0a8cc3807f6f28404f3424ad7e31b3142f" title="Direct3D8 device, only available on Win32 platforms.">video::EDT_DIRECT3D8</a>:
230 psFileName = <span class="stringliteral">&quot;../../media/d3d8.psh&quot;</span>;
231 vsFileName = <span class="stringliteral">&quot;../../media/d3d8.vsh&quot;</span>;
232 <span class="keywordflow">break</span>;
233 <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0a4691ca314f9018f508dcf2c57dcaacec" title="Direct3D 9 device, only available on Win32 platforms.">video::EDT_DIRECT3D9</a>:
234 <span class="keywordflow">if</span> (UseHighLevelShaders)
235 {
236 <span class="comment">// Cg can also handle this syntax</span>
237 psFileName = <span class="stringliteral">&quot;../../media/d3d9.hlsl&quot;</span>;
238 vsFileName = psFileName; <span class="comment">// both shaders are in the same file</span>
239 }
240 <span class="keywordflow">else</span>
241 {
242 psFileName = <span class="stringliteral">&quot;../../media/d3d9.psh&quot;</span>;
243 vsFileName = <span class="stringliteral">&quot;../../media/d3d9.vsh&quot;</span>;
244 }
245 <span class="keywordflow">break</span>;
246
247 <span class="keywordflow">case</span> <a class="code" href="namespaceirr_1_1video.html#ae35a6de6d436c76107ad157fe42356d0a2715182a79f1cb8e2826fd68a8150a53" title="OpenGL device, available on most platforms.">video::EDT_OPENGL</a>:
248 <span class="keywordflow">if</span> (UseHighLevelShaders)
249 {
250 <span class="keywordflow">if</span> (!UseCgShaders)
251 {
252 psFileName = <span class="stringliteral">&quot;../../media/opengl.frag&quot;</span>;
253 vsFileName = <span class="stringliteral">&quot;../../media/opengl.vert&quot;</span>;
254 }
255 <span class="keywordflow">else</span>
256 {
257 <span class="comment">// Use HLSL syntax for Cg</span>
258 psFileName = <span class="stringliteral">&quot;../../media/d3d9.hlsl&quot;</span>;
259 vsFileName = psFileName; <span class="comment">// both shaders are in the same file</span>
260 }
261 }
262 <span class="keywordflow">else</span>
263 {
264 psFileName = <span class="stringliteral">&quot;../../media/opengl.psh&quot;</span>;
265 vsFileName = <span class="stringliteral">&quot;../../media/opengl.vsh&quot;</span>;
266 }
267 <span class="keywordflow">break</span>;
268 }
269</pre></div><p>In addition, we check if the hardware and the selected renderer is capable of executing the shaders we want. If not, we simply set the filename string to 0. This is not necessary, but useful in this example: For example, if the hardware is able to execute vertex shaders but not pixel shaders, we create a new material which only uses the vertex shader, and no pixel shader. Otherwise, if we would tell the engine to create this material and the engine sees that the hardware wouldn't be able to fulfill the request completely, it would not create any new material at all. So in this example you would see at least the vertex shader in action, without the pixel shader. </p>
270<div class="fragment"><pre class="fragment"> <span class="keywordflow">if</span> (!driver-&gt;queryFeature(<a class="code" href="namespaceirr_1_1video.html#a57b1721e42a79c5dcf8e830e3621e08fae3c30045e54cd02efdb3e67eff12664f" title="Is Pixel Shader 1.1 supported?">video::EVDF_PIXEL_SHADER_1_1</a>) &amp;&amp;
271 !driver-&gt;queryFeature(<a class="code" href="namespaceirr_1_1video.html#a57b1721e42a79c5dcf8e830e3621e08fa85cee74794874723bd275226ad0ded76" title="Are ARB fragment programs v1.0 supported?">video::EVDF_ARB_FRAGMENT_PROGRAM_1</a>))
272 {
273 device-&gt;getLogger()-&gt;log(<span class="stringliteral">&quot;WARNING: Pixel shaders disabled &quot;</span>\
274 <span class="stringliteral">&quot;because of missing driver/hardware support.&quot;</span>);
275 psFileName = <span class="stringliteral">&quot;&quot;</span>;
276 }
277
278 <span class="keywordflow">if</span> (!driver-&gt;queryFeature(<a class="code" href="namespaceirr_1_1video.html#a57b1721e42a79c5dcf8e830e3621e08fae85fe645c5839d5b015047abf5fff3e6" title="Is Vertex Shader 1.1 supported?">video::EVDF_VERTEX_SHADER_1_1</a>) &amp;&amp;
279 !driver-&gt;queryFeature(<a class="code" href="namespaceirr_1_1video.html#a57b1721e42a79c5dcf8e830e3621e08fa1eca6110e4fd3ee59e1aee60cf20e88b" title="Are ARB vertex programs v1.0 supported?">video::EVDF_ARB_VERTEX_PROGRAM_1</a>))
280 {
281 device-&gt;getLogger()-&gt;log(<span class="stringliteral">&quot;WARNING: Vertex shaders disabled &quot;</span>\
282 <span class="stringliteral">&quot;because of missing driver/hardware support.&quot;</span>);
283 vsFileName = <span class="stringliteral">&quot;&quot;</span>;
284 }
285</pre></div><p>Now lets create the new materials. As you maybe know from previous examples, a material type in the Irrlicht engine is set by simply changing the MaterialType value in the SMaterial struct. And this value is just a simple 32 bit value, like video::EMT_SOLID. So we only need the engine to create a new value for us which we can set there. To do this, we get a pointer to the IGPUProgrammingServices and call addShaderMaterialFromFiles(), which returns such a new 32 bit value. That's all.</p>
286<p>The parameters to this method are the following: First, the names of the files containing the code of the vertex and the pixel shader. If you would use addShaderMaterial() instead, you would not need file names, then you could write the code of the shader directly as string. The following parameter is a pointer to the IShaderConstantSetCallBack class we wrote at the beginning of this tutorial. If you don't want to set constants, set this to 0. The last parameter tells the engine which material it should use as base material.</p>
287<p>To demonstrate this, we create two materials with a different base material, one with EMT_SOLID and one with EMT_TRANSPARENT_ADD_COLOR. </p>
288<div class="fragment"><pre class="fragment"> <span class="comment">// create materials</span>
289
290 video::IGPUProgrammingServices* gpu = driver-&gt;getGPUProgrammingServices();
291 <a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> newMaterialType1 = 0;
292 <a class="code" href="namespaceirr.html#ac66849b7a6ed16e30ebede579f9b47c6" title="32 bit signed variable.">s32</a> newMaterialType2 = 0;
293
294 <span class="keywordflow">if</span> (gpu)
295 {
296 MyShaderCallBack* mc = <span class="keyword">new</span> MyShaderCallBack();
297
298 <span class="comment">// create the shaders depending on if the user wanted high level</span>
299 <span class="comment">// or low level shaders:</span>
300
301 <span class="keywordflow">if</span> (UseHighLevelShaders)
302 {
303 <span class="comment">// Choose the desired shader type. Default is the native</span>
304 <span class="comment">// shader type for the driver, for Cg pass the special</span>
305 <span class="comment">// enum value EGSL_CG</span>
306 <span class="keyword">const</span> <a class="code" href="namespaceirr_1_1video.html#a913671e32f20f13e51336bfbe20a82a3" title="Enumeration for different types of shading languages.">video::E_GPU_SHADING_LANGUAGE</a> shadingLanguage =
307 UseCgShaders ? <a class="code" href="namespaceirr_1_1video.html#a913671e32f20f13e51336bfbe20a82a3a703622fd615408677044b9ec67f5ea42" title="Cg shading language.*/.">video::EGSL_CG</a>:<a class="code" href="namespaceirr_1_1video.html#a913671e32f20f13e51336bfbe20a82a3ac65c039e1c80a430a816c450a5f30d4b" title="The default language, so HLSL for Direct3D and GLSL for OpenGL.">video::EGSL_DEFAULT</a>;
308
309 <span class="comment">// create material from high level shaders (hlsl, glsl or cg)</span>
310
311 newMaterialType1 = gpu-&gt;addHighLevelShaderMaterialFromFiles(
312 vsFileName, <span class="stringliteral">&quot;vertexMain&quot;</span>, <a class="code" href="namespaceirr_1_1video.html#a9decae50d4dc2455e7b009f5c71b24f9a60cc4ef72d14e7192dc721bde0f07461">video::EVST_VS_1_1</a>,
313 psFileName, <span class="stringliteral">&quot;pixelMain&quot;</span>, <a class="code" href="namespaceirr_1_1video.html#a07fb77e9aec681402ad376f7ef9b724ca600133dcb93a6cbdddaed1e09cc8a2cc">video::EPST_PS_1_1</a>,
314 mc, <a class="code" href="namespaceirr_1_1video.html#ac8e9b6c66f7cebabd1a6d30cbc5430f1a9bc471b9c18c9e2d20496004d2a2e803" title="Standard solid material.">video::EMT_SOLID</a>, 0, shadingLanguage);
315
316 newMaterialType2 = gpu-&gt;addHighLevelShaderMaterialFromFiles(
317 vsFileName, <span class="stringliteral">&quot;vertexMain&quot;</span>, <a class="code" href="namespaceirr_1_1video.html#a9decae50d4dc2455e7b009f5c71b24f9a60cc4ef72d14e7192dc721bde0f07461">video::EVST_VS_1_1</a>,
318 psFileName, <span class="stringliteral">&quot;pixelMain&quot;</span>, <a class="code" href="namespaceirr_1_1video.html#a07fb77e9aec681402ad376f7ef9b724ca600133dcb93a6cbdddaed1e09cc8a2cc">video::EPST_PS_1_1</a>,
319 mc, <a class="code" href="namespaceirr_1_1video.html#ac8e9b6c66f7cebabd1a6d30cbc5430f1a1b5a814c4466aca2943ff056003a50d1" title="A transparent material.">video::EMT_TRANSPARENT_ADD_COLOR</a>, 0 , shadingLanguage);
320 }
321 <span class="keywordflow">else</span>
322 {
323 <span class="comment">// create material from low level shaders (asm or arb_asm)</span>
324
325 newMaterialType1 = gpu-&gt;addShaderMaterialFromFiles(vsFileName,
326 psFileName, mc, <a class="code" href="namespaceirr_1_1video.html#ac8e9b6c66f7cebabd1a6d30cbc5430f1a9bc471b9c18c9e2d20496004d2a2e803" title="Standard solid material.">video::EMT_SOLID</a>);
327
328 newMaterialType2 = gpu-&gt;addShaderMaterialFromFiles(vsFileName,
329 psFileName, mc, <a class="code" href="namespaceirr_1_1video.html#ac8e9b6c66f7cebabd1a6d30cbc5430f1a1b5a814c4466aca2943ff056003a50d1" title="A transparent material.">video::EMT_TRANSPARENT_ADD_COLOR</a>);
330 }
331
332 mc-&gt;drop();
333 }
334</pre></div><p>Now it's time for testing the materials. We create a test cube and set the material we created. In addition, we add a text scene node to the cube and a rotation animator to make it look more interesting and important. </p>
335<div class="fragment"><pre class="fragment"> <span class="comment">// create test scene node 1, with the new created material type 1</span>
336
337 scene::ISceneNode* node = smgr-&gt;addCubeSceneNode(50);
338 node-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0,0));
339 node-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/wall.bmp&quot;</span>));
340 node-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3acea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
341 node-&gt;setMaterialType((<a class="code" href="namespaceirr_1_1video.html#ac8e9b6c66f7cebabd1a6d30cbc5430f1" title="Abstracted and easy to use fixed function/programmable pipeline material modes.">video::E_MATERIAL_TYPE</a>)newMaterialType1);
342
343 smgr-&gt;addTextSceneNode(gui-&gt;getBuiltInFont(),
344 L<span class="stringliteral">&quot;PS &amp; VS &amp; EMT_SOLID&quot;</span>,
345 video::SColor(255,255,255,255), node);
346
347 scene::ISceneNodeAnimator* anim = smgr-&gt;createRotationAnimator(
348 <a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0.3f,0));
349 node-&gt;addAnimator(anim);
350 anim-&gt;drop();
351</pre></div><p>Same for the second cube, but with the second material we created. </p>
352<div class="fragment"><pre class="fragment"> <span class="comment">// create test scene node 2, with the new created material type 2</span>
353
354 node = smgr-&gt;addCubeSceneNode(50);
355 node-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,-10,50));
356 node-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/wall.bmp&quot;</span>));
357 node-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3acea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
358 node-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3ab9cbb5be402278cf0276da84acd3da14" title="Flag for blend operation.">video::EMF_BLEND_OPERATION</a>, <span class="keyword">true</span>);
359 node-&gt;setMaterialType((<a class="code" href="namespaceirr_1_1video.html#ac8e9b6c66f7cebabd1a6d30cbc5430f1" title="Abstracted and easy to use fixed function/programmable pipeline material modes.">video::E_MATERIAL_TYPE</a>)newMaterialType2);
360
361 smgr-&gt;addTextSceneNode(gui-&gt;getBuiltInFont(),
362 L<span class="stringliteral">&quot;PS &amp; VS &amp; EMT_TRANSPARENT&quot;</span>,
363 video::SColor(255,255,255,255), node);
364
365 anim = smgr-&gt;createRotationAnimator(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0.3f,0));
366 node-&gt;addAnimator(anim);
367 anim-&gt;drop();
368</pre></div><p>Then we add a third cube without a shader on it, to be able to compare the cubes. </p>
369<div class="fragment"><pre class="fragment"> <span class="comment">// add a scene node with no shader</span>
370
371 node = smgr-&gt;addCubeSceneNode(50);
372 node-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,50,25));
373 node-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/wall.bmp&quot;</span>));
374 node-&gt;setMaterialFlag(<a class="code" href="namespaceirr_1_1video.html#a8a3bc00ae8137535b9fbc5f40add70d3acea597a2692b8415486a464a7f954d34" title="Will this material be lighted? Default: true.">video::EMF_LIGHTING</a>, <span class="keyword">false</span>);
375 smgr-&gt;addTextSceneNode(gui-&gt;getBuiltInFont(), L<span class="stringliteral">&quot;NO SHADER&quot;</span>,
376 video::SColor(255,255,255,255), node);
377</pre></div><p>And last, we add a skybox and a user controlled camera to the scene. For the skybox textures, we disable mipmap generation, because we don't need mipmaps on it. </p>
378<div class="fragment"><pre class="fragment"> <span class="comment">// add a nice skybox</span>
379
380 driver-&gt;setTextureCreationFlag(<a class="code" href="namespaceirr_1_1video.html#acaf6f7414534f7d62bff18c5bf11876fa288b302e9d4faaba80c7796c7bc1682c">video::ETCF_CREATE_MIP_MAPS</a>, <span class="keyword">false</span>);
381
382 smgr-&gt;addSkyBoxSceneNode(
383 driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/irrlicht2_up.jpg&quot;</span>),
384 driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/irrlicht2_dn.jpg&quot;</span>),
385 driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/irrlicht2_lf.jpg&quot;</span>),
386 driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/irrlicht2_rt.jpg&quot;</span>),
387 driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/irrlicht2_ft.jpg&quot;</span>),
388 driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/irrlicht2_bk.jpg&quot;</span>));
389
390 driver-&gt;setTextureCreationFlag(<a class="code" href="namespaceirr_1_1video.html#acaf6f7414534f7d62bff18c5bf11876fa288b302e9d4faaba80c7796c7bc1682c">video::ETCF_CREATE_MIP_MAPS</a>, <span class="keyword">true</span>);
391
392 <span class="comment">// add a camera and disable the mouse cursor</span>
393
394 scene::ICameraSceneNode* cam = smgr-&gt;addCameraSceneNodeFPS();
395 cam-&gt;setPosition(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(-100,50,100));
396 cam-&gt;setTarget(<a class="code" href="namespaceirr_1_1core.html#a06f169d08b5c429f5575acb7edbad811" title="Typedef for a f32 3d vector.">core::vector3df</a>(0,0,0));
397 device-&gt;getCursorControl()-&gt;setVisible(<span class="keyword">false</span>);
398</pre></div><p>Now draw everything. That's all. </p>
399<div class="fragment"><pre class="fragment"> <span class="keywordtype">int</span> lastFPS = -1;
400
401 <span class="keywordflow">while</span>(device-&gt;run())
402 <span class="keywordflow">if</span> (device-&gt;isWindowActive())
403 {
404 driver-&gt;beginScene(<span class="keyword">true</span>, <span class="keyword">true</span>, video::SColor(255,0,0,0));
405 smgr-&gt;drawAll();
406 driver-&gt;endScene();
407
408 <span class="keywordtype">int</span> fps = driver-&gt;getFPS();
409
410 <span class="keywordflow">if</span> (lastFPS != fps)
411 {
412 <a class="code" href="namespaceirr_1_1core.html#aef83fafbb1b36fcce44c07c9be23a7f2" title="Typedef for wide character strings.">core::stringw</a> str = L<span class="stringliteral">&quot;Irrlicht Engine - Vertex and pixel shader example [&quot;</span>;
413 str += driver-&gt;getName();
414 str += <span class="stringliteral">&quot;] FPS:&quot;</span>;
415 str += fps;
416
417 device-&gt;setWindowCaption(str.c_str());
418 lastFPS = fps;
419 }
420 }
421
422 device-&gt;drop();
423
424 <span class="keywordflow">return</span> 0;
425}
426</pre></div><p>Compile and run this, and I hope you have fun with your new little shader writing tool :). </p>
427</div></div>
428</div>
429 <div id="nav-path" class="navpath">
430 <ul>
431<!-- window showing the filter options -->
432<div id="MSearchSelectWindow"
433 onmouseover="return searchBox.OnSearchSelectShow()"
434 onmouseout="return searchBox.OnSearchSelectHide()"
435 onkeydown="return searchBox.OnSearchSelectKey(event)">
436<a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(0)"><span class="SelectionMark">&#160;</span>All</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(1)"><span class="SelectionMark">&#160;</span>Classes</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(2)"><span class="SelectionMark">&#160;</span>Namespaces</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(3)"><span class="SelectionMark">&#160;</span>Files</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(4)"><span class="SelectionMark">&#160;</span>Functions</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(5)"><span class="SelectionMark">&#160;</span>Variables</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(6)"><span class="SelectionMark">&#160;</span>Typedefs</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(7)"><span class="SelectionMark">&#160;</span>Enumerations</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(8)"><span class="SelectionMark">&#160;</span>Enumerator</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(9)"><span class="SelectionMark">&#160;</span>Friends</a><a class="SelectItem" href="javascript:void(0)" onclick="searchBox.OnSelectItem(10)"><span class="SelectionMark">&#160;</span>Defines</a></div>
437
438<!-- iframe showing the search results (closed by default) -->
439<div id="MSearchResultsWindow">
440<iframe src="javascript:void(0)" frameborder="0"
441 name="MSearchResults" id="MSearchResults">
442</iframe>
443</div>
444
445
446 <li class="footer">
447<a href="http://irrlicht.sourceforge.net" target="_blank">Irrlicht
448Engine</a> Documentation &copy; 2003-2012 by Nikolaus Gebhardt. Generated on Sun Nov 17 2013 20:18:41 for Irrlicht 3D Engine by
449<a href="http://www.doxygen.org/index.html" target="_blank">Doxygen</a> 1.7.5.1 </li>
450 </ul>
451 </div>
452
453
454</body>
455</html>