<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>
<chapter name="Tutorial 37: Geometry Under the Hood">
<setdocpatch name="37jGeometryUnderTheHood" patch="37jGeometryUnderTheHood.maxpat"/>

<previous name="jitterchapter36">3D Models</previous>
<next name="jitterchapter38">Basic Performance Setup</next>
<parent name="jitindex">Jitter Tutorials</parent>



<h1>Tutorial 37: Geometry Under the Hood</h1>


<p>This tutorial demonstrates the low-level support in Jitter to use matrices to store and manipulate geometry data and render them with the <o>jit.gl.mesh</o> object. Since the data is contained in an ordinary Jitter matrix, this opens up a world of possibilities where arbitrary matrix operators may be used to generate and/or process the geometry matrices. The tutorial will cover the <m>matrixoutput</m> attribute of the GL group, the organization of data in geometry matrices, an example of how geometry matrices can be processed using matrix operators, and introduce various drawing primitives supported by the <o>jit.gl.mesh</o> object.</p>

<bullet>Open the tutorial patch and click on the toggle object labeled <i>Start Rendering</i>.</bullet>

<h2>Matrix Output</h2>
<bullet>Click on the <o>toggle</o> box labeled <i>Turn matrixoutput on/off</i>.</bullet>
<p>Wait a minute&#x2014;the sphere that was just there a moment ago has disappeared. What's going on?</p>

<p>Some of the objects in the GL group support the <m>matrixoutput</m> attribute, and <o>jit.gl.gridshape</o> is one such object. This attribute determines whether or not the object's geometry is rendered directly in the object's associated drawing context or if the object sends a matrix containing geometry data out its left outlet. The geometry is not visible because it is being sent to a gate, which is closed.</p>

<bullet>Select the menu item <m>print</m> from the <o>umenu</o> object labeled "Matrix Destination"</bullet>
<p>In the max window you should see a series of messages like "print: jit_matrix u26300000007 quad_grid". This is similar to what you've seen in previous tutorials as the output of objects, which pass matrix data, with the exception, that there is an extra element. Rather than sending the message <m>jit_matrix [matrix-name]</m>, as you should be familiar with, the <o>jit.gl.gridshape</o> object sends message <m>jit_matrix [matrix-name] [drawing-primitive]</m>. In this case the drawing primitive is <m>quad_grid</m>, which means interpret the matrix as a grid of quadrilaterals. </p>

<illustration><img src="images/jitterchapter37a.png"/>The output of the <o>jit.gl.gridshape</o> object in the Max Console</illustration>
<div>
<techdetail>At the time this tutorial was written, the only objects that support the <m>matrixoutput</m> attribute are the <o>jit.gl.gridshape</o> , <o>jit.gl.nurbs</o>, and <o>jit.gl.plato</o> objects. However, by the time you read this, there may be other objects, which support this mode of operation. </techdetail>
</div>
<p>There is still no excitement in our window, but this is easily remedied.</p>

<bullet>Select the menu item <m>direct</m> from the <o>umenu</o> object labeled <i>Matrix Destination</i>.</bullet>
<p>The sphere is now visible again, because the message <m>jit_matrix [matrix-name] [drawing-primitive]</m> is being sent to the <o>jit.gl.mesh</o> object. In response to this message, the <o>jit.gl.mesh</o> object draws the supplied matrix using the specified drawing primitive. If no drawing primitive is specified, the <o>jit.gl.mesh</o> object's current drawing primitive will be used. Valid drawing primitives are: <m>points</m>, <m>lines</m>, <m>line_strip</m>, <m>line_loop</m>, <m>triangles</m>, <m>tri_strip</m>, <m>tri_fan</m>, <m>quads</m>, <m>quad_strip</m>, <m>polygon</m>, <m>tri_grid</m>, and <m>quad_grid</m>. </p>

<div>
<techdetail>The <o>jit.gl.mesh</o> object's current drawing primitive may be set using the <m>@draw_mode</m> attribute and can be set to any one of the valid drawing primitives mentioned above.</techdetail>
</div>
<p>In the same fashion that you can change the dimensions of video data, you can change the dimensions of the matrix output by sending the <o>jit.gl.gridshape</o> object the <m>dim</m> message. By default the dimensions of the matrix output by the <o>jit.gl.gridshape</o> object is 20 by 20.</p>

<bullet>Click on the <o>toggle</o> box labeled <i>Wireframe</i>.</bullet>
<bullet>Change the <o>number</o> box labeled <i>Matrix Dimensions</i>.</bullet>
<bullet>Try rotating the wireframe sphere with the mouse</bullet>
<illustration><img src="images/jitterchapter37b.png"/>&#x201C;The Death Star plans are not in the main computer.&#x201D;</illustration>
<div>
<techdetail>You may have noticed that in this patch the <o>jit.gl.handle</o> object is communicating with the <o>jit.gl.render</o> object rather than the <o>jit.gl.gridshape</o> object. This has the effect of rotating and positioning the entire scene. The <m>@inherit_transform 1</m> argument is necessary in order to do this correctly, otherwise the rotation would be compositied again with the scene's rotation, causing major confusion. </techdetail>
</div>

<h2>Geometry Matrix Details</h2>
<p>Video in Jitter is typically represented by 4-plane <m>char</m> data, but how is the geometry data being represented?</p>

<p>Each vertex in the geometry is typically represented as <m>float32</m> data with 3, 5, 8, 12, or 13 planes. Planes 0-2 specify the <i>x</i>, <i>y</i> and <i>z</i> position of the vertex. Planes 3 and 4 specify the texture co-ordinates <i>s</i> and <i>t</i>. Planes 5-7 specify the normal vector <i>nx</i>, <i>ny</i> and <i>nz</i> used to calculate the effects of lighting on the geometry. Planes 8-11 specify the <i>red</i>, <i>green</i>, <i>blue</i>, and <i>alpha</i> vertex color. Plane 12 specifies the edge flag <i>e</i>.</p>

<p>The output matrix of the <o>jit.gl.gridshape</o> object has 12 planes, but since we are not applying a texture to the geometry, and lighting is not enabled, the texture coordinates and normal vectors are ignored. </p>

<h2>Processing the Geometry Matrix</h2>
<p>Instead of simply rendering the geometry unaltered, as you've done so far, it is possible to process this geometry with matrix operators. </p>

<bullet>Select the menu item <m>xfade</m> from the <o>umenu</o> object labeled <i>Matrix Destination</i>.</bullet>
<p>Now the matrix is being sent through the <o>jit.xfade</o> object to cross fade the geometry matrix with a matrix of noise. Just as the <o>jit.xfade</o> object may be used to crossfade between video matrices, it may be used to crossfade between geometry matrices.</p>

<bullet>Click on the <o>button</o> object labeled "Generate Noise"</bullet>
<bullet>Gradually change the <o>number</o> box labeled "Crossfade Value" to <m>0.1</m>.</bullet>
<bullet>Turn wireframe rendering on and off by clicking the <o>toggle</o> box labeled <i>Wireframe</i>. Notice how the noise deforms both the geometry and the color of the shape.</bullet>
<p>This will subtly deform the sphere with noise, as well as introduce random colors. The texture coordinates and normal vectors are also being affected, but this is not visible since there is no texture being applied, and lighting is not enabled.</p>

<illustration><img src="images/jitterchapter37c.png"/>A slightly distorted sphere (wireframe and filled)</illustration>
<bullet>Click on the <o>button</o> object labeled "Generate Noise" several more times, or turn on the toggle box labeled "Rapid Noise" to generate a new matrix of noise for each time the geometry is drawn</bullet>
<bullet>Gradually change the <o>number</o> box labeled "Crossfade Value" to <m>1.0</m>. Again, turn wireframe rendering on and off to see how the noise is visualized.</bullet>
<p>Now the geometry being drawn is pure noise.</p>

<illustration><img src="images/jitterchapter37d.png"/>White noise expressed as a geometry (wireframe and filled)</illustration>
<bullet>Set the <o>number</o> box labeled "Crossfade Value" back to <m>0</m>.</bullet>

<h2>Drawing Primitives</h2>
<p>You will notice that despite the fact that <o>jit.gl.gridshape</o> is outputting the message <m>jit_matrix [matrix-name] quad_grid</m>, we are appending the output of jit.xfade with <m>quad_grid</m>. This is because most matrix operators will ignore the drawing primitive argument and simply output the message <m>jit_matrix [matrix-name]</m>. Hence we need to append the name of the drawing primitive to the message. This provides a good opportunity to experiment with various drawing primitives. </p>

<bullet>Try selecting the various menu items available in the <o>umenu</o> labeled <i>Drawing Primitive</i>.</bullet>
<illustration><img src="images/jitterchapter37e.png"/>Using different drawing primitives: <m>points</m> (left), <m>line_strip</m> (center), and <m>triangles</m> (right)</illustration>

<h2>Summary</h2>
<p>In addition to drawing directly to their associated drawing contexts, some objects in the GL group support the <m>matrixoutput</m> attribute. When enabled, the geometry matrix is sent out the object's left output with the message <m>jit_matrix [matrix-name] [drawing-primitive]</m>.</p>

<p>Geometry matrices are typically <m>float32</m> data with a plane count of 3, 5, 8, 12, or 13 planes, and may be processed using arbitrary matrix operators in a similar fashion to processing video matrices.</p>

<p>The <o>jit.gl.mesh</o> object supports various drawing primitives via the <m>@draw_mode</m> attribute to render these geometry matrices: <m>points</m>, <m>lines</m>, <m>line_strip</m>, <m>line_loop</m>, <m>triangles</m>, <m>tri_strip</m>, <m>tri_fan</m>, <m>quads</m>, <m>quad_strip</m>, <m>polygon</m>, <m>tri_grid</m>, and <m>quad_grid</m>.</p>

	<seealsolist>
		<seealso display="Video and Graphics Tutorial 7: Generating Geometry" module="Video and Graphics" name="jitterchapter00i_Generating Geometry" type="tutorial" />		
		<seealso display="GL Contexts" module="core" name="jitter_gl_contexts" type="vignette" />		
		<seealso name="jit.gl.gridshape">Generate simple geometric shapes as a connected grid</seealso>
		<seealso name="jit.gl.handle">Use mouse movement to control position/rotation</seealso>
		<seealso name="jit.gl.mesh">Render Matrix Data as a Shape</seealso>
		<seealso name="jit.gl.render">Render Open GL</seealso>
		<seealso name="jit.xfade">Crossfade between 2 matrices</seealso>
	</seealsolist>
	</chapter>
