In the previous tutorial, you saw how to position the camera and objects in the GL group to construct an OpenGL scene in Jitter. After understanding this tutorial, you’ll be able to hide selected polygons of an OpenGL object based on their spatial orientations, draw selected polygons in filled and wireframe modes, and add the results to the draw buffer using antialiasing and blending.
• Open the tutorial patch 33jPolyColorBlend
in the Jitter Tutorial folder Click on the toggle
box labeled “Start Rendering.”
You should see a gray sphere in the jit.pwindow
in the tutorial patch. It is drawn by a jit.gl.gridshape
object, connected to a jit.gl.handle
object that allows you to control its rotation. The jit.gl.handle
object's attribute is on, so once you rotate it the sphere will continue to rotate along the axis you set. If you like, give it a spin.
Just below the label “OpenGL Objects to Render” in the example patch is an object pak
. This object generates messages that set the polygon mode attribute of the jit.gl.gridshape
• Click both toggle
objects on above the pak
object. You should see the gray sphere in wireframe mode.
The sphere in wireframe mode.
Turning wireframe mode on allows you to see clearly that the jit.gl.gridshape
object approximates a sphere using polygons -- in this case, quadrilaterals. Every polygon drawn in OpenGL has a front side, defined as the side from which its vertices appear to wrap in a clockwise direction. Each polygon in a scene can, therefore, be categorized as either front-facing or back-facing, depending on whether its front side is pointed towards or away from the camera.
OpenGL can automatically hide either front-facing or back-facing polygons, which can serve to speed up drawing greatly or highlight certain aspects of data being visualized. In Jitter, you can control polygon visibility on an object-by-object basis using the
• Set the number
box above the prepend
object to .
The front-facing polygons of the sphere.
The , or . A setting of shows all polygons in the object. A setting of hides the back-facing polygons. A setting of hides the front-facing polygons. With the current setting of , the wireframe sphere appears solid, because the hidden lines -- polygon edges that would not be visible if the sphere were made of a solid material -- are not drawn. The rotation (did you give it a spin?) convincingly depicts that of a real-world solid object.
attribute of a GL group object can be set to
• Set the number box above the prepend
object to .
The back-facing polygons of the sphere.
Now the front-facing polygons are hidden. It’s as if you are looking through the sphere and seeing only the inside part that faces towards you. You can see from this picture that the perspective is somewhat strange, but watching the rotation in the patch makes it really obvious that the scene is not drawn “normally.”
In general, setting
will do a good job of removing the polygons that should be hidden for solid, convex objects such as the sphere. But for objects that aren’t solid, a combination of the front-facing and back-facing polygons may be in view.
• Set the number
box above the prepend
object to to show all the polygons. Set the left box to (off). Set the umenu
above the prepend
object to “opencylinder”.
Open cylinder, with solid front-facing polygons and wireframe back-facing polygons.
Using this new shape, we can see the distinction between front- and back-facing polygons. The message
, given two integers and , sets the front-facing polygons to wireframe mode if equals , and the back-facing ones to wireframe if equals .
The colors of objects in the GL group are specified using the message
, where R is the red component of the color, B is blue, G is green, and A is the alpha or opacity component. All values range from to .
• Set the number
box objects above the pak
object to the values This specifies pure red at 50% opacity.
You will see the color of the cylinder turn red, but the opacity is not visible. This is because blending, the mixing of pixels with those previously in the draw buffer, is turned off by default.
• Click the toggle
box above the message
box to turn blending on for the jit.gl.gridshape
object. You should see something like this:
The red cylinder with blending enabled.
ARGB vs. RGBA
If you’ve been using Jitter’s video-manipulation objects, you know that colors in those objects are stored in planes and specified in arguments n the order A, R, G, B. In the GL group of objects, the order is RGBA, with alpha last, as we’re seeing here. This may seem odd, so you are due bit of explanation. Jitter objects are tied as closely to native formats as possible in both the OpenGL and video domains, to allow the fastest possible processing. OpenGL stores colors of objects and vertices in RGBA format, and QuickTime stores its images in ARGB format. So the Jitter objects reflect this. If you want to combine OpenGL and video matrix processing in your patch, the pack
objects provide an easy way to convert between the two systems. You can also convert a matrix of char values from ARGB to RGBA by sending the matrix through a jit.matrix
object with the attribute set to (effectively shifting all the planes by one). Tutorial 6
shows more examples of using the attribute of the jit.matrix
Right now, each time the jit.gl.render
object receives the message, the draw buffer is filled with the dark gray that is the default erase color. You can set a different erase color using the RGBA number boxes above the renderer object.
• Set the number boxes above the jit.gl.render
object to the values 0, 0, 0.5 and 0.1.
The background changes to a dark blue (Red = 0, Green = 0, Blue = 0.5). If the cylinder is spinning, you will also see trails in the image. This is because the background is being erased with an opacity of 0.1. When the dark blue pixels are drawn on top of the existing pixels in the draw buffer, they are overlaid such that the result is one-tenth dark blue and nine-tenths whatever color was at each pixel previously. As a result, previous frames linger for some time before being fully erased. Note that while the blend_enable attribute must be set in order to see draw colors with partial opacity, it is not necessary for erasing with partial opacity.
The spinning cylinder, leaving trails.
When the blend function. The blend function is the basic operation in image compositing. It controls the application of new pixels, the source, over existing pixels, the destination. The function has two parts: a source blending factor and a destination blending factor. The source blending factor specifies how the source’s contribution to the finished image should be computed. The destination blending factor specifies the destination’s contribution.
attribute of an object in the GL group is on, each pixel is applied to the draw buffer using a
Jitter recognizes eleven possible modes for blending factors. Some can be applied only to the source, some only to the destination, and some to both. Each mode specifies a different set of multiplier values for red, green, blue and alpha. The message
The Blend Modes The source and destination blend factors are RGBA quadruplets which are multiplied componentwise by the RGBA values of the source and destination pixels, respectively. The corresponding components of the source and destination are added and then clamped to the range [ ] to produce the output pixel.
This table shows how the blend factors are calculated. The “Mode” column lists the number passed in the Jitter message s
refers to the red component of the source pixel.
The “OpenGL name” column lists the name of the mode. The “Relevant to” column lists whether the mode can apply to the source factor, the destination factor, or both. Finally, the “Blend Factor Equation” column specifies the actual formula that is used to calculate the pixel. The subscripts s and d refer to source and destination components, respectively. For example, R
Mode OpenGL name Relevant to Blend Factor Equation
0 GL_ZERO both (0, 0, 0, 0)
1 GL_ONE both (1, 1, 1, 1)
2 GL_DST_COLOR source (Rd
3 GL_SRC_COLOR destination (Rs
4 GL_ONE_MINUS_DST_COLOR source (1, 1, 1, 1)-(Rd
5 GL_ONE_MINUS_SRC_COLOR destination (1, 1, 1, 1)-(Rs
6 GL_SRC_ALPHA both (As
7 GL_ONE_MINUS_ SRC_ALPHA both (1, 1, 1, 1)- (As
8 GL_DST_ALPHA both (Ad
9 GL_ONE_MINUS_ DST_ALPHA both (1, 1, 1, 1)-(Ad
10 GL_SRC_ALPHA_SATURATE source (f, f, f, 1); f = min(As
The default source and destination blend modes for all objects in the GL group are
and , respectively. These correspond to the GL blend factors GL_SRC_ALPHA and GL_ONE_MINUS_SRC_ALPHA. This is a very commonly used blending operation, and you may never need another one. It allows you to crossfade intuitively between the source and destination by changing the source’s alpha value.
Other values are useful for simulating various real-world lighting situations, as well as special effects that have no physical counterpart.
• Set the RGBA number boxes above the jit.gl.render
object, which control the attribute, to the values and . This will set the background color to yellow and remove the trails, making the blend effect more visible.
• Set the left and right number boxes above the object pak
to and respectively. This specifies a source blend factor of GL_ZERO and a destination blend factor of GL_SRC_ALPHA.
The cylinder with a setting of .
Let’s examine how this
produces the image we see here. The source factor is GL_ZERO. This means that all the components of the source pixel are multiplied by zero -- the source pixel has no effect. You can verify this by trying different RGB values for the cylinder. They all produce the same colors.
The destination factor is GL_SRC_ALPHA. Looking in the table above, we can find the blend factor equation this corresponds to: (As
). Each component of the destination pixel is multiplied by the source’s alpha, in this case , before being added to the source pixel times the source factor, which in this case is . So, each time a pixel is drawn, it has its brightness reduced by one half.
When OpenGL draws polygons and lines, it approximates their ideal geometric shapes by filling in pixels on a raster grid. This process is prone to difficulties that parallel those in reconstructing ideal waveforms with digital audio. Inevitably, aliasing, spatial frequencies that are not included in the ideal image, will be introduced once it is reconstructed from discrete pixels. This aliasing is visible as what are commonly known as “jaggies,” especially in near-horizontal or near-vertical lines or edges.
OpenGL has some antialiasing techniques for reducing the jaggies. We’ve made these available in Jitter through attributes of the GL group of objects. Using these attributes, you can specify whether any given object in the GL group will use antialiasing when it is drawn.
• Turn on the toggle
box above the message
box to send the message jit.gl.gridshape
The antialiased lines have a smoother appearance, and also a fatter one. They may also draw more slowly, so if you’re concerned about drawing speed, you have to decide whether the improved appearance is worth the extra time.
The behavior of antialiasing in OpenGL is implementation-dependent. This means that makers of OpenGL hardware and drivers have some leeway in deciding what exactly happens when you request antialiasing. In the pictures above, for example, note that while the jaggies on the lines are reduced, the polygon edges in the lower left appear just the same. When you turn antialiasing on, Jitter requests that polygon edges be antialiased. But the particular OpenGL implementation that generated these pictures (an ATI Rage 128 accelerator with version 5.9.8 drivers) does not offer any help in this regard. Your implementation may differ.
We have defined front-facing and back-facing polygons, and seen how to draw them in both solid and wireframe modes (using the
and attributes). The renderer’s attribute and its use to draw trails have been introduced. We defined in detail what happens when a source pixel is applied to a draw buffer destination, taking opacity and blend modes into account. And finally, the handy if somewhat unpredictable antialiasing feature of OpenGL was introduced.
Generate simple geometric shapes as a connected grid
Use mouse movement to control position/rotation
Render Open GL
Make a multiplane matrix out of single plane matrices
Make multiple single plane matrices out of a multiplane matrix
Combine numbers and symbols into a list
Break a list into individual messages