<?xml version='1.0' encoding='UTF-8'?>

<?xml-stylesheet href="./_c74_tut.xsl" type="text/xsl"?>

<chapter name="Video and Graphics 4: Adding 3D Objects">

<previous name="jitterchapter00e_Control Jitter with Messages">Control Jitter with Messages</previous>
<next name="jitterchapter00g_Jitter Matrix 1">Jitter Matrix Part 1</next>
<parent name="jitindex">Jitter Tutorials</parent>

<openfile name="Live Capture" patch="jitter_core_-_4_-_adding_3D_objects.maxpat"/>

<h1>Video and Graphics Tutorial 4: Adding 3D Objects</h1>



<h2>Intro</h2>



<p>The capabilities of Jitter go far beyond simple playback and manipulation of video - it is also a platform for 3D graphics and has a includes a full OpenGL engine (OpenGL is a cross-platform system for drawing and manipulating hardware-accelerated graphics). It’s important to note that OpenGL makes use of the GPU hardware on your computer, which is highly optimized for graphics. In this lesson, we will learn the basics of working with 3D shapes and models and learn how to adjust their surface materials.
</p>

<h2>Setup</h2>

<p>Start by adding a named <o>jit.world</o> object to your patch (we'll name ours <m>jit.world 3D</m>) with a <o>toggle</o> connected to it. When you click on the toggle to turn it on, any OpenGL objects we add to our patch will get drawn by <o>jit.world.</o></p>

<illustration><img src="images/jitter_core - 4a.png"/></illustration>

<div>
	<techdetail>

		<p>Context Naming: It’s not absolutely necessary to use named window contexts for OpenGL drawing (just as it's not absolutely necessary to name a <o>jit.world</o> when working with video). As you move into more advanced OpenGL usage, being able to specify and keep track of which context an object draws into becomes more important - getting into the habit of doing this now will help you later.
		</p>


	</techdetail>
</div>



<h2>Geometry using <o>jit.gl.gridshape</o></h2>

<p>There are many ways to create and display 3D geometry in Jitter. We’re going to start with a <o>jit.gl.gridshape</o> object. As soon as you add a <o>jit.gl.gridshape</o> object to your patch, you should see a flat grey circle displayed in your window. In order to adjust the size (scale) of the object so it fits on screen, let’s create an <o>attrui</o> for the <i>scale</i> attribute and adjust the scale of the x, y and z dimensions. Try setting them all to about .5</p>

<div>
	<techdetail>

		<p>Tip: connecting a <o>flonum</o> object to the inlet of the <o>attrui</o> will let us adjust all three values at once.
		</p>

	</techdetail>
</div>

<illustration><img src="images/jitter_core - 4b.png"/></illustration>

<p>In its default state, our shape looks very grey and not very 3-dimensional. We’ll start by adding some color to the object. Create an <o>attrui</o> for the <i>color</i> attribute of <o>jit.gl.gridshape</o>. This will let you set the color using the color picker built into the <o>attrui</o>. While this is very handy for quickly choosing a color, it’s also important to get familiar with the way that color values in OpenGL objects work. To do this, create a <o>pak</o> object with 4 float inputs (<b>pak 0. 0. 0. 1.</b>), add 4 <o>flonum</o> objects, and connect their outlets to the <o>pak</o> object inlets. Connect the <o>pak</o> object's outlet to the <i>color</i> <o>attrui</o> and try adjusting the float values between 0 and 1.  OpenGL colors are specified as four values: Red, Green, Blue, and Alpha (opacity).  You’ll notice that the Alpha value has no apparent effect (this is because the <i>blend_enable</i> attribute is not turned on by default). To add lighting, try adding <o>attrui</o> objects the <i>lighting_enable</i> and <i>smooth_shading</i> attributes and toggle those on. To further customize the look of our 3D object, add a couple of additional <o>attrui</o> objects to the <o>jit.gl.gridshape</o> object. </p>

<p>First, add an <o>attrui</o> for the <i>position</i> attribute. In OpenGL, we define space using Cartesian coordinates x, y and z for width, height, and depth. By the way - the center of our virtual world is (0. 0. 0.). Try moving your object object around the screen.</p>

<p>Next, add an <o>attrui</o> for the <i>shape</i> attribute and explore the basic geometric figures. </p>

<p>Finally, let’s add an <o>attrui</o> for one for the <i>rotatexyz</i> attribute that will let us rotate our object. We can set the degree of rotation on each axis using values between between 0. and 360 degrees.</p>

<p>A typical OpenGL scene will likely contain many objects, each with their own parameters. Try copying and pasting the <o>jit.gl.gridshape</o> and the associated <o>attrui</o> objects you've created, and then change settings for each of them individually to make them unique. </p>

<illustration><img src="images/jitter_core - 4c.png"/></illustration>

<h2>Add a 3D Model file</h2>

<p>While you can do a lot with simple geometry, Jitter is also capable of handling complex 3D models created using third-party software. Create a <o>jit.gl.model</o> object, create a <o>message</o> box that contains the message <m>read duck.dae</m> and connect the outlet of the <o>message</o> box to the inlet of the <o>jit.gl.model</o> object.</p>

<illustration><img src="images/jitter_core - 4d.png"/></illustration>

<p>When you click on the message, it loads the object and shows it in our window, including any texture that was made with the model. You can use the same attributes you used with the <o>jit.gl.gridshape</o> to scale, rotate and position the duck within your scene.</p>


<illustration><img src="images/jitter_core - 4e.png"/></illustration>

<h2>Using the mouse to manipulate objects in a scene</h2>


<p>It's often helpful to have a way to quickly move objects around in OpenGL scenes. To do this, create a <o>message</o> box that contains the message <m>auto_handle $1</m> and connect its outlet to the inlet of the <o>jit.world</o> object. Add a <o>toggle</o> running into the left inlet of your <o>message</o> box, then lock the patch and turn click on the toggle to turn it on. </p>


<illustration><img src="images/jitter_core - 4f.png"/></illustration>


<p>Enabling the <i>auto_handle</i> attribute lets you adjust the position and orientation of an object using your mouse. Bring your OpenGL window to bring it to the front. Now, clicking on an object and dragging will now allow you to adjust the rotation; holding the Command key (Control on windows) and clicking/dragging adjusts the x/y position; holding Alt/Option and dragging up and down adjusts the z position.</p>


<illustration><img src="images/jitter_core - 4g.png"/></illustration>




<div>
	<techdetail>

		<h2>Explore Further</h2>

		<p>As we learned in previous chapters, all Jitter attributes can be controlled with messages.  Try hooking things like the scale and position of your 3D objects to other data sources - number data from MIDI controllers or the <o>random</o> object, audio amplitude (using <o>peakamp~</o>), etc... For more fine-tuned and high quality control of lighting and color, look at connecting a <o>jit.gl.material</o> object to your OpenGL objects and a <o>jit.gl.light</o> object to your scene. There are a number of other Jitter GL objects to explore, as different sorts of geometry.</p>


	</techdetail>
</div>





</chapter>
