<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 34: Using Textures">
<setdocpatch name="34jUsingTextures" patch="34jUsingTextures.maxpat"/>

<previous name="jitterchapter33">Polygon Modes, Colors and Blending</previous>
<next name="jitterchapter35">Lighting and Fog</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 34: Using Textures</h1>
<p>This tutorial shows you how to create and apply textures to 3D geometry data generated by the GL group. It will cover the creation of a named texture, assigning a named texture to a GL object, the use of colors in conjunction with textures, the conversion of image/video data to a texture, and various ways to wrap the geometry with a texture.</p>


<bullet>Open the tutorial patch and click on the <o>toggle</o> object labeled <i>Start Rendering</i>.</bullet>
<p>You will see a white parallelogram, but it is actually a tetrahedron being drawn by the <o>jit.gl.plato</o> object. The <o>jit.gl.plato</o> object is capable of rendering several platonic solids including tetrahedrons, hexahedrons (also known as cubes), octahedrons, dodecahedrons, and icosahedrons. Since lighting is not turned on and there is no texture being applied to the tetrahedron, it is difficult to tell that it is actually a 3D shape.</p>


<bullet>Use the mouse to rotate the tetrahedron with the <o>jit.gl.handle</o> object, as covered in <link type="tutorial" module="jit" name="jitterchapter32">Tutorial 32</link>.</bullet>
<p>This should illustrate that it is actually a 3D shape, but by applying a texture to the <o>jit.gl.plato</o> object, this will become even more apparent.</p>

<illustration><img src="images/jitterchapter34a.png"/>Rotating the platonic solid.</illustration>
<h2>What is a Texture?</h2>
<p>A texture is essentially an image that is overlaid upon geometry. Just like other images in Jitter, textures have an alpha, red, green, and blue component. In order to make use use the alpha component, blending must be enabled. Blending is covered in detail in <link type="tutorial" module="jit" name="jitterchapter33">Tutorial 33</link>. </p>

<p>In Jitter, a texture has a name and belongs to the <o>jit.gl.texture</o> object. Other objects that are attached to the drawing context associated with a given <o>jit.gl.render</o> object may make use of any of the named textures attached to that <o>jit.gl.render</o> object. Jitter permits arbitrarily sized textures. There is a minimum size of 1 by 1 and a maximum size dependent upon the OpenGL implementation. </p>

<h2>Creating a Texture</h2>
<bullet>The patch contains a <o>jit.gl.texture</o> object named <m>grid</m>. Click the <o>message</o> box labeled <m>dim 64 64</m> in the section of the patch labeled <i>Textures</i>. This message is being sent to the <o>jit.gl.texture</o> object.</bullet>
<p>The <m>dim</m> attribute takes two arguments that specify the width and height of the texture. This creates a <m>64</m> by <m>64</m> texture named <m>grid</m>, and fills it with the default pattern (a white and grey checkerboard). You will not see any of the results yet, because the texture has not yet been applied to the geometry.</p>

<bullet>Apply the texture to the tetrahedron by clicking on the <o>message</o> box labeled <m>texture grid</m> in the section of the patch labeled <i>Platonic Solid</i>. This sets the <o>jit.gl.plato</o> object's <m>texture</m> attribute, and when drawing, it will use the texture named <m>grid</m>. You should now see a checkered tetrahedron.</bullet>
<illustration><img src="images/jitterchapter34b.png"/>Tetrahedron with a checkerboard texture applied to it.</illustration>
<p>The <o>jit.gl.plato</o> object uses a "gift-wrapping" strategy to apply the texture to the tetrahedron. The image below illustrates exactly how the different platonic solids are wrapped.</p>


<illustration><img src="images/jitterchapter34c.png"/>            How texture maps are applied to different platonic solids.</illustration>

<h2>Textures and Color</h2>
<p>When applying a texture to geometry, OpenGL also takes into account color and lighting information, so the current color and lighting values will be multiplied with the texture image when drawn. If the color is white and lighting is turned off, the texture colors will be unaltered.</p>

<bullet>In the section of the patch labeled <i>Platonic Solid</i>, set the color of the tetrahedron to red by setting the <o>number</o> box labeled <i>red</i> to <m>1</m>, the <o>number</o> box labeled <i>green</i> to <m>0</m>, and the <o>number</o> box labeled <i>blue</i> to <m>0</m>.</bullet>

<illustration><img src="images/jitterchapter34d.png"/>Manipulating the color of the rendered object.</illustration>

<bullet>Set the color of the tetrahedron back to white (<m>1. 1. 1.)</m> for our next section.</bullet>

<h2>Converting an Image or Video to a Texture</h2>
<p>While it is illustrative, typically you will want to make use of textures other than the default grey and white checkerboard. This can be accomplished by loading an image or a movie into the <o>jit.movie</o> or <o>jit.matrix</o> objects and sending the output to the <o>jit.gl.texture</o> object.</p>

<bullet>In the section of the patch labeled <i>Textures</i> click the <o>message</o> box containing <m>read colorbars.png, bang</m> to load the <i>colorbars.png</i> image into the <o>jit.movie</o> object, and send it on its way to the attached texture object named <m>picture</m>.</bullet>
<p>You still won't see any of the results yet, because the <o>jit.gl.plato</o> object is still using the texture named <m>grid</m>.</p>

<bullet>Click the message box containing <m>texture picture</m> in the section of the patch labeled <i>Platonic Solid</i>.</bullet>
<p>Now you should see the colorbars image wrapped around the tetrahedron.</p>

<illustration><img src="images/jitterchapter34e.png"/>Using an image as a texture.</illustration>

<p>In many instances you will only need to use still images as textures, but Jitter also supports the use of moving video as textures by repeatedly copying the output of the <o>jit.movie</o> object into the named texture.</p>

<bullet>Click the <o>message</o> box containing <m>read dishes.mov</m> to load <i>dishes.mov</i> into the <o>jit.movie</o> object.</bullet>
<bullet>Click on the <o>toggle</o> object connected to the <o>metro</o> object to start copying the video to the texture named <m>picture</m>.</bullet>
<illustration><img src="images/jitterchapter34f.png"/>Texture-mapping using a movie.</illustration>

<h2>Interpolation and Texture size</h2>
<p>By default, texture interpolation is turned on, so screen pixels which are between texture pixels will use an interpolated value of its neighbors within the texture image. This has the effect of blurring or smoothing out the texture. To apply textures without interpolation, the interpolation may be turned off using the <o>jit.gl.texture</o> object's <m>filter</m> attribute and setting the value to <i>none</i>.</p>

<bullet>Click the <o>message</o> box containing <m>filter none</m> to turn interpolation off and click the <o>message</o> box containing <m>filter linear</m> to turn interpolation on for the texture named <m>picture</m>.</bullet>
<p>By default the texture will adapt its width and height to that of the incoming matrix. The texture size can be changed, by sending the message <m>adapt 0, dim[width] [height]</m> where <m>[width]</m> and <m>[height]</m> specify the new dimensions. </p>

<bullet>Set the <o>number</o> box labeled <i>Resize texture</i> to <m>16</m>. This will disable <m>adapt</m> on the <o>jit.gl.texture</o> object and set its <m>dim</m> attribute, resizing the texture <m>picture</m> to be a <m>16</m> by <m>16</m> image.</bullet>
<illustration><img src="images/jitterchapter34g.png"/>Using an uninterpolated texture</illustration>

<h2>Mapping Modes</h2>
<bullet>Open the <m>texture_mapping</m> subpatch.</bullet>
<p>So far we have only addressed the implicit texture mapping that the <o>jit.gl.plato</o> object provides. OpenGL also provides a few other <i>explicit</i> texture mappings for applying textures to geometry data. These are the <i>object linear</i>, <i>eye linear</i>, and <i>sphere map</i> mapping modes. </p>

<p>The <i>object linear</i> mode applies the texture in a fixed manner relative to the object's coordinate system. As the object is rotated and positioned in the 3D scene, the texture mapping remains the same. In contrast, the <i>eye linear</i> mode applies the texture in a fixed manner relative to the eye's coordinate system. As the object is rotated and positioned in the 3D scene, the application of the texture to the object will change. Lastly, the <i>sphere map</i> mapping mode will produce the effect commonly called "environment mapping"; the object is rendered as though it is reflecting the surrounding environment, and assumes that the texture contains a sphere mapped image of the surrounding environment. As the object is rotated and positioned in the 3D scene, the application of the texture to the object will change.</p>

<p>These explicit mapping modes may be used by setting the GL group <m>tex_map</m> attribute. A <m>tex_map</m> value of <m>0</m> is the default and will use the GL object's implicit texture coordinates. A <m>tex_map</m> value of <m>1</m> will use OpenGL's <i>object linear</i> mode. A <m>tex_map</m> value of <m>2</m> will use OpenGL's <i>sphere map</i> mode. A <m>tex_map</m> value of <m>3</m> will use OpenGL's <i>eye linear</i> mode.</p>

<bullet>Try changing the <o>number</o> box connected to the message box containing <m>tex_map $1</m>. Position and rotate the tetrahedron with your mouse, and see how the various modes affect the texture mapping.</bullet>

<illustration><img src="images/jitterchapter34h.png"/>Using different implicit mapping modes: object linear (left), sphere map (right), eye linear (lower).</illustration>

<p>OpenGL's <i>object linear</i> and <i>eye linear</i> mapping modes have additional parameters that affect the way in which they apply the texture. These are set with the GL group <m>tex_plane_s</m> and <m>tex_plane_t</m> attributes. These attributes are each vectors in 4 dimensional homogenous coordinates. The scalar product of <m>tex_plane_s</m> and a given point in 4 dimensional homogenous coordinates determines the horizontal position of the texture image to apply. The scalar product of <m>tex_plane_t</m> and a given point in 4 dimensional homogenous coordinates determines the vertical position of the texture image to apply. By default, <m>tex_plane_s</m> is equal to <m>(1. 0. 0. 0.)</m> and <m>tex_plane_t</m> is equal to <m>(0. 1. 0. 0.).</m> </p>

<p>A more detailed description of how the <m>tex_plane_s</m> and <m>tex_plane_t</m> attributes affect the texture mapping are out of the scope of this tutorial, but that doesn't mean you can't play with it anyway to generate interesting effects. For the curious, please consult the OpenGL <i>Red Book</i> or <i>Blue Book</i>.</p>
<p>A more user friendly way of manipulating the <m>tex_plane_s</m> and <m>tex_plane_t</m> attributes is contained in the green box via the subpatch named <m>tex_coord_magic</m>. This subpatch handles the math necessary to perform rotation, zoom, and anchor offset operations on the texture.</p>

<bullet>Experiment with the <o>number</o> box objects under the <i>rotate, zoom, anchor</i> labels (<m>tex_map</m> should be set to <m>1</m> or <m>3</m> in order to have any effect).</bullet>
<illustration><img src="images/jitterchapter34i.png"/>Experimenting with additional parameters.</illustration>

<h2>Summary</h2>
<p>We have established how to create textures and various ways to apply them to the geometry created by the GL group. The <o>jit.gl.texture</o> object may be used to create, size, and copy image or video data to named textures. The GL group <m>texture</m> attribute specifies which named texture to use, and the GL group <m>tex_map</m> attribute selects either the implicit texture mapping mode or one of the three explicit OpenGL texture mapping modes: <i>object linear</i>, <i>eye linear</i>, or <i>sphere map</i>.</p>

	<seealsolist>
		<seealso display="GL Texture Output" module="core" name="jitter_gl_texture_output" type="vignette">
		GL Texture Output
		</seealso>
		<seealso display="GL Contexts" module="core" name="jitter_gl_contexts" type="vignette">
		GL Contexts
		</seealso>
		<seealso name="jit.gl.handle">Use mouse movement to control position/rotation</seealso>
		<seealso name="jit.gl.plato">Generate platonic solids</seealso>
		<seealso name="jit.gl.render">Render Open GL</seealso>
		<seealso name="jit.gl.texture">Create OpenGL textures</seealso>
		<seealso name="jit.matrix">The Jitter Matrix!</seealso>
		<seealso name="jit.movie">Play or edit a movie</seealso>
	</seealsolist>
	</chapter>
