<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Video and Graphics 7: Generating Geometry">

<previous name="jitterchapter00h_Jitter Matrix 2">Jitter Matrix Part 2</previous>
<next name="jitterchapter00j_Audio into a matrix">Audio into a matrix</next>
<parent name="jitindex">Jitter Tutorials</parent>

<openfile name="Generating Geometry" patch="jitter_core_-_7_-_Generating_Geometry.maxpat"/>

<h1>Video and Graphics Tutorial 7: Generating Geometry</h1>



<h2>Intro</h2>



<p>In this section we’ll use the Jitter Matrix to create 3D geometry instead of video.
We can define XYZ points in virtual space in a way similar to how we define RGB color values in cells of a matrix, and then use that to draw 3D shapes. While the <i>char</i> data type (numbers in the range 0-255) works well for color, we'll need much greater precision for 3D coordinates - so we'll use the <i>float32</i> (32-bit floating point) data types for our matrices.
</p>

<h2>Setup</h2>

<p>Begin with the usual named <o>jit.world</o> and <o>toggle</o> object pair.</p>



<h2>Mesh generation - simple, using <o>Jit.gl.mesh</o>, <o>jit.noise</o> and the float32 matrix</h2>


<p>Click on the <o>toggle</o> to turn on your <o>jit.world</o> and add a <b>jit.gl.mesh @lighting_enable 1</b> object. You'll notice that adding the <o>jit.gl.mesh</o> object doesn’t automatically display anything by itself - this is because it’s waiting for a matrix to tell it what to draw. The <o>jit.gl.mesh</o> object takes in any floating point matrix with 3 or more planes (we'll discuss those extra planes in later tutorials) and interprets them as OpenGL primitive shapes.</p>

<illustration><img src="images/jitter_core - 7a.png"/></illustration>

<p>Now add a <b>jit.noise 3 float32 20 20</b> object. This gives us 3 planes for x, y and z and a 20 x 20 grid of 400 points. Add a <o>button</o> above to trigger the noise. Connect the <o>jit.noise</o> to the left inlet of the <o>jit.gl.mesh</o> object and then lock the patch. Turn on your <o>jit.world</o> and click the on the <o>button</o> - you should see a chaotic ball of shards filling the upper right corner of your render window.</p>

<p>You’ll notice it’s only rendering shapes in the upper right-hand area of the window. That's because the <o>jit.noise</o> outputs values ranging from 0. - 1. In OpenGL, the center is always a “0. 0. 0.” Positive x goes right, negative x goes left, while positive y goes up, negative down, and positive z goes forward (or away) and negative goes back. To fill out the screen a little better, add a <b>jit.map @map 0. 1. -1. 1.</b> object between <o>jit.noise</o> and <o>jit.gl.mesh</o>. The <o>jit.map</o> objects works similarly to scale on Jitter Matrix values.</p>

<p>Add a scale <o>attrui</o> to your <o>jit.gl.mesh</o> object so you can adjust the size and fit it on screen. Finally, connect the middle outlet of <o>jit.world</o>  to your <o>jit.noise</o> object to animate it.</p>

<illustration><img src="images/jitter_core - 7b.png"/></illustration>


<h2>Matrixoutput</h2>


<p>Now that we’ve created chaos, let’s use an object from earlier to generate a more structured output. Create a <o>jit.gl.gridshape</o> object and add a message (or <o>attrui</o>) to control its <i>matrixoutput</i> attribute and another for dim &lt;x,y&gt;, then add a <o>jit.pwindow</o> below and connect to it from the gridshape object. It will not appear in your render window until you set the <i>matrixoutput</i> to 1 - add a <o>toggle</o> and turn it on. </p>

<illustration><img src="images/jitter_core - 7c.png"/></illustration>

<p>You can now see the x, y, z data displayed in the pwindow as RGB values. As with our <o>jit.noise</o> object, this matrix data is <i>float32</i> and describes the geometry of the shape selected. </p>

<illustration><img src="images/jitter_core - 7d.png"/></illustration>

<p>Add another <o>jit.gl.mesh</o> object (with lighting) and connect the <o>jit.gl.gridshape</o> object’s output into the left inlet to see it. Again - you may want to adjust <i>scale</i> and <i>position</i> so you can see both objects on the screen.   </p>

<h2>Drawermodes</h2>


<p>Using the geometry data from <o>jit.gl.gridshape</o> with <o>jit.gl.mesh</o> provides us with some extra options - add an <o>attrui</o> to your mesh object and select the <i>draw_mode</i> attribute. These modes determine how the points in your geometry are connected to each other to create your form. The default is <i>tri_grid</i>, which interprets the matrix as a surface that is filled in with triangles. Try some of the other options to see what’s available. Increasing the <i>dim</i> attribute of the <o>jit.gl.gridshape</o> will allow you to increase or decrease the number of points and thus the resulting image. You can also adjust the <i>point_size</i> with drawmode points and <i>line_width</i> with drawmode <i>lines</i>, <i>line_strip</i> or <i>line_loop</i> to further modify the look.</p>

<illustration><img src="images/jitter_core - 7e.png"/></illustration>

<h2>Manipulating the geometry</h2>


<p>The geometry data we get from <o>jit.gl.gridshape</o> is really just an array of floating point values, so we can manipulate it any way we'd like before we send it to <o>jit.gl.mesh</o> for rendering. The <o>jit.gl.gridshape</o> object outputs a 12-plane matrix. You’ll learn more about this in later tutorials, but for now we only need the first three planes of the matrix, which store x, y and z values. Make a <b>jit.unpack 1 @jump 3</b> object and connect the output of <o>jit.gl.gridshape</o> to it.  Now add a <o>jit.xfade</o> object with a <o>message</o> box containing <m>xfade $1</m> message and a <o>flonum</o> (or <o>attrui</o>) connected. Patch the left outlet of <o>jit.noise</o> into the left inlet and the outlet of <o>jit.unpack</o> into the right. Disconnect the <o>jit.gl.gridshape</o> from the <o>jit.gl.mesh</o>, replacing it with the output of jit.xfade. Try adjusting the xfade value from 0. - 1. to see how the two matrices modify each other. A 0. will be the pure noise, and a 1. will be the gridshape, but anything else will be a combination of the two.</p>

<p>To smooth out the transformations caused by <o>jit.noise</o>, add a <b>jit.slide @slide_up 10 @slide_down 10</b> object and add controls for the <i>slide_up</i> and <i>slide_down</i> attributes. Adjust the values to see how it changes the form.</p>

<illustration><img src="images/jitter_core - 7f.png"/></illustration>

<div>
	<techdetail>

		<h2>Explore Further</h2>

		<p>Most of the <o>jit.gl.mesh</o> object draw_mode options are actually standard OpenGL primitives, designed to make it lightweight for an array of values to be sent and interpreted in a specific way instead of making a separate call for each triangle. Many of the modes are interpreted across the horizontal dimension of the incoming matrix. For example, in polygon mode, each row represents one complete polygon shape. To fully understand how each draw_mode is interpreted, we recommend consulting the OpenGL Red Book or similar references</p>

		<p>The <o>jit.gen</o> object is another great way to generate geometry data. Take a look at <openfilelink filename="jitter_core_-_7_-Generating_Geometry_2_Gen.maxpat">this patch</openfilelink>, which does something very similar to what we created above, but all inside of <o>jit.gen</o>. </p>

	</techdetail>
</div>





</chapter>
