irrlicht/examples/10.Shaders/tutorial.html

211 lines
33 KiB
HTML
Raw Normal View History

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=9"/>
<meta name="generator" content="Doxygen 1.8.13"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Tutorial 10: Shaders</title>
<html xmlns="http://www.w3.org/1999/xhtml">
<!-- Wanted to avoid copying .css to each folder, so copied default .css from doxyen in here, kicked out most stuff we don't need for examples and modified some a little bit.
Target was having a single html in each example folder which is created from the main.cpp files and needs no files besides some images below media folder.
Feel free to improve :)
-->
<style>
body, table, div, p, dl {
font: 400 14px/22px;
}
body {
background-color: #F0F0F0;
color: black;
margin-left: 5%;
margin-right: 5%;
}
p.reference, p.definition {
font: 400 14px/22px;
}
.title {
font: 400 14px/28px;
font-size: 150%;
font-weight: bold;
margin: 10px 2px;
}
h1, h2, h3, h4, h5, h6 {
-webkit-transition: text-shadow 0.5s linear;
-moz-transition: text-shadow 0.5s linear;
-ms-transition: text-shadow 0.5s linear;
-o-transition: text-shadow 0.5s linear;
transition: text-shadow 0.5s linear;
margin-right: 15px;
}
caption {
font-weight: bold;
}
h3.version {
font-size: 90%;
text-align: center;
}
a {
color: #3D578C;
font-weight: normal;
text-decoration: none;
}
.contents a:visited {
color: #4665A2;
}
a:hover {
text-decoration: underline;
}
a.el {
font-weight: bold;
}
a.code, a.code:visited, a.line, a.line:visited {
color: #4665A2;
}
a.codeRef, a.codeRef:visited, a.lineRef, a.lineRef:visited {
color: #4665A2;
}
pre.fragment {
border: 1px solid #C4CFE5;
background-color: #FBFCFD;
padding: 4px 6px;
margin: 4px 8px 4px 2px;
overflow: auto;
word-wrap: break-word;
font-size: 9pt;
line-height: 125%;
font-family: monospace, fixed;
font-size: 105%;
}
div.fragment {
padding: 0px;
margin: 4px 8px 4px 2px;
background-color: #FBFCFD;
border: 1px solid #C4CFE5;
}
div.line {
font-family: monospace, fixed;
font-size: 13px;
min-height: 13px;
line-height: 1.0;
text-wrap: unrestricted;
white-space: -moz-pre-wrap; /* Moz */
white-space: -pre-wrap; /* Opera 4-6 */
white-space: -o-pre-wrap; /* Opera 7 */
white-space: pre-wrap; /* CSS3 */
word-wrap: break-word; /* IE 5.5+ */
text-indent: -53px;
padding-left: 53px;
padding-bottom: 0px;
margin: 0px;
-webkit-transition-property: background-color, box-shadow;
-webkit-transition-duration: 0.5s;
-moz-transition-property: background-color, box-shadow;
-moz-transition-duration: 0.5s;
-ms-transition-property: background-color, box-shadow;
-ms-transition-duration: 0.5s;
-o-transition-property: background-color, box-shadow;
-o-transition-duration: 0.5s;
transition-property: background-color, box-shadow;
transition-duration: 0.5s;
}
div.contents {
margin-top: 10px;
margin-left: 12px;
margin-right: 8px;
}
div.center {
text-align: center;
margin-top: 0px;
margin-bottom: 0px;
padding: 0px;
}
div.center img {
border: 0px;
}
span.keyword {
color: #008000
}
span.keywordtype {
color: #604020
}
span.keywordflow {
color: #e08000
}
span.comment {
color: #800000
}
span.preprocessor {
color: #806020
}
span.stringliteral {
color: #002080
}
span.charliteral {
color: #008080
}
blockquote {
background-color: #F7F8FB;
border-left: 2px solid #9CAFD4;
margin: 0 24px 0 4px;
padding: 0 12px 0 16px;
}
hr {
height: 0px;
border: none;
border-top: 1px solid #4A6AAA;
}
address {
font-style: normal;
color: #2A3D61;
}
div.header {
background-image:url('nav_h.png');
background-repeat:repeat-x;
background-color: #F9FAFC;
margin: 0px;
border-bottom: 1px solid #C4CFE5;
}
div.headertitle {
padding: 5px 5px 5px 10px;
}
.image {
text-align: center;
}
.caption {
font-weight: bold;
}
div.zoom {
border: 1px solid #90A5CE;
}
tr.heading h2 {
margin-top: 12px;
margin-bottom: 4px;
}
</style>
</head>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<!--END TITLEAREA-->
<!-- end header part -->
<!-- Generated by Doxygen 1.8.13 -->
</div><!-- top -->
<div class="header">
<div class="headertitle">
<div class="title">Tutorial 10: Shaders </div> </div>
</div><!--header-->
<div class="contents">
<div class="textblock"><div class="image">
<img src="../../media/010shot.jpg" alt="010shot.jpg"/>
</div>
<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>
<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>
<p>At first, we need to include all headers and do the stuff we always do, like in nearly all other tutorials: </p><div class="fragment"><div class="line"><span class="preprocessor">#include &lt;irrlicht.h&gt;</span></div><div class="line"><span class="preprocessor">#include &lt;iostream&gt;</span></div><div class="line"><span class="preprocessor">#include &quot;driverChoice.h&quot;</span></div><div class="line"></div><div class="line"><span class="keyword">using namespace </span>irr;</div><div class="line"></div><div class="line"><span class="preprocessor">#ifdef _MSC_VER</span></div><div class="line"><span class="preprocessor">#pragma comment(lib, &quot;Irrlicht.lib&quot;)</span></div><div class="line"><span class="preprocessor">#endif</span></div></div><!-- fragment --><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><div class="fragment"><div class="line">IrrlichtDevice* device = 0;</div><div class="line"><span class="keywordtype">bool</span> UseHighLevelShaders = <span class="keyword">false</span>;</div><div class="line"><span class="keywordtype">bool</span> UseCgShaders = <span class="keyword">false</span>;</div><div class="line"></div><div class="line"><span class="keyword">class </span>MyShaderCallBack : <span class="keyword">public</span> video::IShaderConstantSetCallBack</div><div class="line">{</div><div class="line"><span class="keyword">public</span>:</div><div class="line"></div><div class="line"> <span class="keyword">virtual</span> <span class="keywordtype">void</span> OnSetConstants(video::IMaterialRendererServices* services,</div><div class="line"> s32 userData)</div><div class="line"> {</div><div class="line"> video::IVideoDriver* driver = services-&gt;getVideoDriver();</div><div class="line"></div><div class="line"> <span class="comment">// set inverted world matrix</span></div><div class="line"> <span class="comment">// if we are using highlevel shaders (the user can select this when</span></div><div class="line"> <span class="comment">// starting the program), we must set the constants by name.</span></div><div class="line"></div><div class="line"> core::matrix4 invWorld = driver-&gt;getTransform(video::ETS_WORLD);</div><div class="line"> invWorld.makeInverse();</div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (UseHighLevelShaders)</div><div class="line"> services-&gt;setVertexShaderConstant(<span class="stringliteral">&quot;mInvWorld&quot;</span>, invWorld.pointer(), 16);</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> services-&gt;setVertexShaderConstant(invWorld.pointer(), 0, 4);</div><div class="line"></div><div class="line"> <span class="comment">// set clip matrix</span></div><div class="line"></div><div class="line"> core::matrix4 worldViewProj;</div><div class="line"> worldViewProj = driver-&gt;getTransform(video::ETS_PROJECTION);</div><div class="line"> worldVie
<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>
<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><div class="fragment"><div class="line"><span class="comment">// create materials</span></div><div class="line"></div><div class="line">video::IGPUProgrammingServices* gpu = driver-&gt;getGPUProgrammingServices();</div><div class="line">s32 newMaterialType1 = 0;</div><div class="line">s32 newMaterialType2 = 0;</div><div class="line"></div><div class="line"><span class="keywordflow">if</span> (gpu)</div><div class="line">{</div><div class="line"> MyShaderCallBack* mc = <span class="keyword">new</span> MyShaderCallBack();</div><div class="line"></div><div class="line"> <span class="comment">// create the shaders depending on if the user wanted high level</span></div><div class="line"> <span class="comment">// or low level shaders:</span></div><div class="line"></div><div class="line"> <span class="keywordflow">if</span> (UseHighLevelShaders)</div><div class="line"> {</div><div class="line"> <span class="comment">// Choose the desired shader type. Default is the native</span></div><div class="line"> <span class="comment">// shader type for the driver, for Cg pass the special</span></div><div class="line"> <span class="comment">// enum value EGSL_CG</span></div><div class="line"> <span class="keyword">const</span> video::E_GPU_SHADING_LANGUAGE shadingLanguage =</div><div class="line"> UseCgShaders ? video::EGSL_CG:video::EGSL_DEFAULT;</div><div class="line"></div><div class="line"> <span class="comment">// create material from high level shaders (hlsl, glsl or cg)</span></div><div class="line"></div><div class="line"> newMaterialType1 = gpu-&gt;addHighLevelShaderMaterialFromFiles(</div><div class="line"> vsFileName, <span class="stringliteral">&quot;vertexMain&quot;</span>, video::EVST_VS_1_1,</div><div class="line"> psFileName, <span class="stringliteral">&quot;pixelMain&quot;</span>, video::EPST_PS_1_1,</div><div class="line"> mc, video::EMT_SOLID, 0, shadingLanguage);</div><div class="line"></div><div class="line"> newMaterialType2 = gpu-&gt;addHighLevelShaderMaterialFromFiles(</div><div class="line"> vsFileName, <span class="stringliteral">&quot;vertexMain&quot;</span>, video::EVST_VS_1_1,</div><div class="line"> psFileName, <span class="stringliteral">&quot;pixelMain&quot;</span>, video::EPST_PS_1_1,</div><div class="line"> mc, video::EMT_TRANSPARENT_ADD_COLOR, 0 , shadingLanguage);</div><div class="line"> }</div><div class="line"> <span class="keywordflow">else</span></div><div class="line"> {</div><div class="line"> <span class="comment">// create material from low level shaders (asm or arb_asm)</span></div><div class="line"></div><div class="line"> newMaterialType1 = gpu-&gt;addShaderMaterialFromFiles(vsFileName,</div><div class="line"> psFileName, mc, video::EMT_SOLID);</div><div class="line"></div><div class="line"> newMaterialType2 = gpu-&gt;addShaderMaterialFromFiles(vsFileName,</div><div class="line"> psFileName, mc, video::EMT_TRANSPARENT_ADD_COLOR);</div><div class="line"> }</div><div class="line"></div><div class="line"> mc-&gt;drop();</div><div class="line">}</div></div><!-- fragment --><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><div class="fragment"><div class="line"><span class="comment">// create test scene node 1, with the new created material type 1</span></div><div class="line"></div><div class="line">scene::ISceneNode* node = smgr-&gt;addCubeSceneNode(50);</div><div class="line">node-&gt;setPosition(core::vector3df(0,0,0));</div><div class="line">node-&gt;setMaterialTexture(0, driver-&gt;getTexture(<span class="stringliteral">&quot;../../media/wall.bmp&quot;</span>));</div><div class="
</div></div><!-- contents -->
<!-- HTML footer for doxygen 1.8.13-->
<!-- start footer part -->
<p>&nbsp;</p>
</body>
</html>