OpenGL...lation(3pm)
โจ NAME โจ
OpenGL::Tessellation โ the whole ~discussion~ of tessellation in POGL ๐ (that's Perl OpenGL, bestie, not the C one). This is a User Contributed Perl Documentation page, manual section 3pm, and she is long, so buckle up.
๐ธ SYNOPSIS ๐ธ
# somewhere in your drawing routine or drawlist compilation
my $tess = gluNewTess();
gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_END, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_COMBINE, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_ERROR, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_EDGE_FLAG, 'DEFAULT');
gluTessBeginPolygon($tess);
gluTessBeginContour($tess);
gluTessVertex_p($tess, 0, 200, 0);
gluTessVertex_p($tess, 150, -200, 0);
gluTessVertex_p($tess, 0, -100, 0);
gluTessVertex_p($tess, -150, -200, 0);
gluTessEndContour($tess);
gluTessEndPolygon($tess);
gluDeleteTess($tess);
๐ฆ DESCRIPTION ๐ฆ
okay so. here's the tea. โ
OpenGL rendering hardware typically does not have support for drawing concave polygons or drawing polygons with windows. not it. so OpenGL provides glu extensions that let you translate concave polygon vertices into triangles that can be rendered quickly on GL hardware. slay.
The OpenGL red book chapter 11 has the full discussion of Tessellators and the OpenGL functions (http://glprogramming.com/red/chapter11.html, or honestly just use your favorite search engine and search for "opengl gluNewTess"). fr fr, read that chapter before you read the rest of this document. it's giving prerequisite. ๐
As much as possible, the POGL implementation of the tessellation functions tries to remain faithful to the OpenGL specification. Where it doesn't match exactly, POGL follows the spirit of the specification โจ but offloads what it can to c based implementations. bestie is delegating, as she should ๐
Tessellation functions are safe to call during drawlist creation. It is advisable to use drawlists, or to store the generated polygon data into OpenGL::Array objects, because these methods offer faster redraws. ๐ performance queen behavior.
๐ FUNCTIONS ๐
๐ gluNewTess
my $tess = gluNewTess();
Returns a reference that can be passed to the remaining tesselation functions. ๐ซถ
Note: this isn't the c-reference returned by the normal gluNewTess() c function โ it is a struct which contains that reference as well as other members allowing callbacks to interface cleanly with the perl code. This means that if you have loaded other c-libraries that use standard opengl tessellation, you will not be able to use this perl reference directly. heads up, she's special and she's not compatible with the c girlies. ๐
The POGL implementation of gluNewTess() allows for two additional parameters to be passed:
- a boolean value indicating that default c callbacks and perl callbacks should be passed rgba color data ๐จ
- a boolean value indicating that xyz normal data should be passed ๐
Eventually one additional flag indicating that texture data should be passed will be added as well. (coming soon to a tessellator near you)
my $tess = gluNewTess();
# gluTessVertex_p should be passed only x,y,z vertex data
# as in gluTessVertex_p($tess, $x, $y, $z);
my $tess = gluNewTess('do_colors');
# gluTessVertex_p should be passed x,y,z AND r,g,b,a vertex data
# as in gluTessVertex_p($tess, $x, $y, $z, $r, $g, $b, $a);
my $tess = gluNewTess('do_colors', 'do_normals');
# gluTessVertex_p should be passed x,y,z AND r,g,b,a AND nx,ny,nz vertex data
# as in gluTessVertex_p($tess, $x, $y, $z, $r, $g, $b, $a, $nx, $ny, $nz);
my $tess = gluNewTess(undef, 'do_normals');
# gluTessVertex_p should be passed x,y,z AND nx,ny,nz vertex data (no colors)
# as in gluTessVertex_p($tess, $x, $y, $z, $nx, $ny, $nz);
Any true value can be passed in place of 'do_colors' and 'do_normals' โ though using 'do_colors' and 'do_normals' acts as documentation. ๐ self-documenting code? we love to see it.
Behavior in these modes will be discussed further for functions to which they apply. ๐
๐ gluDeleteTess
gluDeleteTess($tess);
This deletes the tessellation structure and frees up any remaining associated memory. ๐งนโจ clean up after yourself, bestie, memory leaks are not the vibe.
๐ gluTessCallback
gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_BEGIN, \&glBegin);
gluTessCallback($tess, GLU_TESS_BEGIN, sub { my $enum = shift; glBegin($enum) });
gluTessCallback($tess, GLU_TESS_BEGIN); # unsets current handler
Registers handlers for each of the tessellation callback types. Takes a tessellation reference generated by gluNewTess, a type, and a coderef or the word 'DEFAULT'. ๐
- If the word
'DEFAULT'is passed, a default c-level callback will be installed (which will be discussed for each callback). ๐ - If no 3rd argument is given, then any handler currently set will be removed. ๐ซ
Valid callback types are:
GLU_TESS_BEGIN
GLU_TESS_END
GLU_TESS_VERTEX
GLU_TESS_COMBINE
GLU_TESS_ERROR
GLU_TESS_EDGE_FLAG
GLU_TESS_BEGIN_DATA
GLU_TESS_END_DATA
GLU_TESS_VERTEX_DATA
GLU_TESS_COMBINE_DATA
GLU_TESS_ERROR_DATA
GLU_TESS_EDGE_FLAG_DATA
These types and their passed parameters will be discussed in the CALLBACKS section. ๐
The types ending with "_DATA" are similar to their non-_DATA counterpart, but when called are passed the option $polygon_data that can be set during gluTessBeginPolygon. ๐
๐ gluTessBeginPolygon
gluTessBeginPolygon($tess);
gluTessBeginPolygon($tess, $polygon_data);
Begins the tessellation transaction. It must eventually be ended with a gluTessEndPolygon before the tessellator will normally begin work. no leaving her on read, close the transaction. ๐ซ
An optional second argument can be passed which can be any perl scalar or reference. If a callback is registered using a type ending in _DATA, this perl scalar or reference will be passed as an additional argument to that callback. ๐
gluTessCallback($tess, GLU_TESS_END_DATA, sub {
my $polygon_data = shift;
glEnd();
print "glEnd: (".($polygon_data->[2] eq 8 ? "YES" : "NO").")\n";
});
gluTessBeginPoly($tess, [6,7,8]); # arrayref will be passed to _DATA callbacks
A sample Object Oriented tesselation sample listed at the end of this document makes use of this "opaque" polygon data. ๐ถ๏ธ
๐ gluTessEndPolygon
gluTessEndPolygon($tess);
Finishes the tessellation transaction, which normally will immediately fire the necessary callbacks generated by the tessellation process. ๐ฅ Once finished, it cleans up any accumulated temporary vertice data. ๐งผ
๐ gluTessBeginContour
gluTessBeginContour($tess);
Starts a new contour of the tessellation of the current polygon. Please read the OpenGL documentation, and red book chapter on tessellation for more help on when to use different contours. ๐ Should eventually be followed by a gluTessEndContour call. ๐
(At a high level, tessellated polygons may have windows and multiple separate portions. Each inner and outer border of these portions should be represented by a different contour.) ๐ช๐ช
๐ gluTessVertex_p
gluTessVertex_p($tess, $x, $y, $z);
gluTessVertex_p($tess, $x, $y, $z, $vertex_data);
Adds a vertex to the current contour of the current polygon being tessellated. ๐
If the vertex callback type is set to GLU_TESS_VERTEX, the optional $vertex_data argument will be passed to the vertex callback, and to the combine callback (if GLU_TESS_VERTEX_DATA is used, then the $polygon_data passed to gluTessBeginPolygon will be passed instead). This optional opaque vertex data can be any perl scalar or reference and can be used to pass useful information along during the tessellation process. ๐
IMPORTANT: If the'do_colors'or'do_normals'parameters were passed togluNewTess, then those additional properties MUST be passed as additional arguments. no skipping, she will know. ๐ค
# my $tess = gluNewTess('do_colors');
gluTessVertex_p($tess, $x, $y, $z, $r, $g, $b, $a);
gluTessVertex_p($tess, $x, $y, $z, $r, $g, $b, $a, $vertex_data);
# my $tess = gluNewTess('do_colors', 'do_normals');
gluTessVertex_p($tess, $x, $y, $z, $r, $g, $b, $a, $nx, $ny, $nz);
gluTessVertex_p($tess, $x, $y, $z, $r, $g, $b, $a, $nx, $ny, $nz, $vertex_data);
# my $tess = gluNewTess(undef, 'do_normals');
gluTessVertex_p($tess, $x, $y, $z, $nx, $ny, $nz);
gluTessVertex_p($tess, $x, $y, $z, $nx, $ny, $nz, $vertex_data);
๐ CALLBACKS ๐
All of the callbacks support a 'DEFAULT' handler that can be installed by passing the word 'DEFAULT' in place of the callback code reference. The DEFAULT c implementations are there to avoid needing to round trip out to perl. ๐๏ธ๐จ The defaults employed are described for each of the callback types.
With the exception of the COMBINE callback, return values from callbacks are discarded. ๐๏ธ she's not listening to what you return, bestie, she just wants you to do the thing.
โจ GLU_TESS_BEGIN
gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_BEGIN, \&glBegin);
gluTessCallback($tess, GLU_TESS_BEGIN, sub {
my $enum = shift;
glBegin($enum);
});
The 'DEFAULT' option installs a c-handler that calls the glBegin c function directly without round-tripping out to perl. โก
If $polygon_data was set during gluTessBeginPolygon, it is discarded. ๐ฎ
โจ GLU_TESS_BEGIN_DATA
Similar to GLU_TESS_BEGIN but will be passed optional $polygon_data set in gluTessBeginPolygon if any. The 'DEFAULT' handler will ignore this data. ๐
gluTessCallback($tess, GLU_TESS_BEGIN_DATA, sub {
my ($enum, $polygon_data) = @_;
glBegin($enum);
print "glBegin - and I received polygon_data\n" if $polygon_data;
});
โจ GLU_TESS_END
gluTessCallback($tess, GLU_TESS_END, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_END, \&glEnd);
gluTessCallback($tess, GLU_TESS_END, sub { glEnd() });
The 'DEFAULT' option installs a c-handler that calls the glEnd c function directly without round-tripping out to perl. โก
If $polygon_data was set during gluTessBeginPolygon, it is discarded. ๐ฎ
โจ GLU_TESS_END_DATA
Similar to GLU_TESS_END but will be passed optional $polygon_data set in gluTessBeginPolygon if any. The 'DEFAULT' handler will ignore this data. ๐
gluTessCallback($tess, GLU_TESS_END_DATA, sub {
my ($polygon_data) = @_;
glEnd();
print "glEnd - and I received polygon_data\n" if $polygon_data;
});
okay that's part 1 of 3 bestie, keep reading, we're not done yet ๐ง๐
โจ "GLU_TESS_VERTEX" โจ
okay so the GLU_TESS_VERTEX callback handler is lowkey a shapeshifter โ it acts a lil different depending on how you called gluNewTess. and bestie, those optional behaviors are a gift, because they let you do sane default processing of colors and normals without needing to roundtrip all the way out to perl ๐ซถ
my $tess = gluNewTess();
gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
# the following will break if vertex_data is passed to gluTessVertex_p
gluTessCallback($tess, GLU_TESS_VERTEX, \&glVertex3f);
gluTessCallback($tess, GLU_TESS_VERTEX, sub {
my ($x, $y, $z) = @_;
glVertex3f($x, $y, $z);
});
# you can also pass vertex_data to gluTessVertex_p
gluTessCallback($tess, GLU_TESS_VERTEX, sub {
my ($x, $y, $z, $vertex_data) = @_;
glVertex3f($x, $y, $z);
print "glVertex - and I received vertex_data\n" if $vertex_data;
});
the 'DEFAULT' option installs a c-handler that calls the glVertex c function directly, no round-tripping out to perl at all. iconic. โจ but heads up: the DEFAULT handler discards any polygon_data or vertex_data. rip to those.
IF $vertex_data was set during gluTessVertex_p it will be passed as the final argument. noted ๐
if gluNewTess was passed 'do_colors' then the GLU_TESS_VERTEX callback will also be passed the rgba information. and the 'DEFAULT' option will pass the color information to glColor4f before calling glVertex3f. look at her go:
my $tess = gluNewTess('do_colors');
gluTessCallback($tess, GLU_TESS_VERTEX, sub {
my ($x, $y, $z, $r, $g, $b, $a, $vertex_data) = @_;
glColor4f($r, $g, $b, $a);
glVertex3f($x, $y, $z);
});
if gluNewTess was passed 'do_normals' then the GLU_TESS_VERTEX callback will also be passed the normal x,y,z information. and the 'DEFAULT' option will pass the normal information to glNormal3f before calling glVertex3f. two for one, we love to see it ๐
my $tess = gluNewTess('do_colors', 'do_normals');
gluTessCallback($tess, GLU_TESS_VERTEX, sub {
my ($x, $y, $z, $r, $g, $b, $a, $nx, $ny, $nz, $vertex_data) = @_;
glColor4f($r, $g, $b, $a);
glNormalf($nx, $ny, $nz);
glVertex3f($x, $y, $z);
});
# OR
my $tess = gluNewTess(undef, 'do_normals');
gluTessCallback($tess, GLU_TESS_VERTEX, sub {
my ($x, $y, $z, $nx, $ny, $nz, $vertex_data) = @_;
glNormalf($nx, $ny, $nz);
glVertex3f($x, $y, $z);
});
in all cases, any optional vertex_data will be passed as the final argument. periodt. ๐
โจ "GLU_TESS_VERTEX_DATA" โจ
basically GLU_TESS_VERTEX's sibling, but this one gets handed the optional $polygon_data that was set in gluTessBeginPolygon (if any) instead of the optional $vertex_data passed to gluTessVertex_p. the 'DEFAULT' handler will ignore this data entirely, no thoughts head empty ๐ธ
gluTessCallback($tess, GLU_TESS_VERTEX_DATA, sub {
my ($x, $y, $z, $vertex_data) = @_;
glVertex3f($x, $y, $z);
print "glVertex - and I received vertex_data\n" if $vertex_data;
});
โจ "GLU_TESS_COMBINE" โจ
buckle up bestie, this one has lore ๐ฆ
gluTessCallback($tess, GLU_TESS_COMBINE, 'DEFAULT');
# works with gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
# OR
# the following callback is valid for gluNewTess() (no do_colors or do_normals)
# using gluTessVertex_p($tess, $x, $y, $z);
my $tess = gluNewTess();
gluTessCallback($tess, GLU_TESS_COMBINE, sub {
my ($x, $y, $z, # new vertex location
$v0, $v1, $v2, $v3, # border vertex arrayrefs
$w0, $w1, $w2, $w3, # border vertex weights
$polygon_data) = @_; # optional data passed to gluTessBeginPolygon
return ($x, $y, $z);
});
# works with gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
# OR
# the following callback is valid for gluNewTess() when vertex data is passed
# using gluTessVertex_p($tess, $x, $y, $z, [$r, $g, $b, $a]);
# The DEFAULT callback cannot automatically proceess this type of data
# but passing data to a custom handler this way could handle any arbitrary data passed to it
my $tess = gluNewTess();
use constant _r => 0;
use constant _g => 1;
use constant _b => 2;
use constant _a => 3;
gluTessCallback($tess, GLU_TESS_COMBINE, sub {
my ($x, $y, $z, # new vertex location
$v0, $v1, $v2, $v3, # border vertex arrayrefs
$w0, $w1, $w2, $w3, # border vertex weights
$polygon_data) = @_; # optional data passed to gluTessBeginPolygon
# $v0 will contain [$x, $y, $z, [$r, $g, $b, $a]]
my @rgba = map {$_->[3]} $v0, $v1, $v2, $v3;
# generate a point with color weighted from the surrounding vertices
# then return that color information in the same way we received it (an rgba arrayref)
return (
$x, $y, $z,
[$w0*$rgba[0]->[_r] + $w1*$rgba[1]->[_r] + $w2*$rgba[2]->[_r] + $w3*$rgba[3]->[_r],
$w0*$rgba[0]->[_g] + $w1*$rgba[1]->[_g] + $w2*$rgba[2]->[_g] + $w3*$rgba[3]->[_g],
$w0*$rgba[0]->[_b] + $w1*$rgba[1]->[_b] + $w2*$rgba[2]->[_b] + $w3*$rgba[3]->[_b],
$w0*$rgba[0]->[_a] + $w1*$rgba[1]->[_a] + $w2*$rgba[2]->[_a] + $w3*$rgba[3]->[_a]],
);
});
# works with gluTessCallback($tess, GLU_TESS_VERTEX, sub {
# my ($x, $y, $z, $rgba) = @_;
# glColor4f(@$rgba);
# glVertex3f($x, $y, $z);
# });
# OR
# the following callback is valid for gluNewTess('do_colors')
# using gluTessVertex_p($tess, $x, $y, $z, $r, $g, $b, $a);
# the DEFAULT callback COULD automatically proceess this type of data as well if additional vertex data is not passed
my $tess = gluNewTess('do_colors');
use constant _r => 3;
use constant _g => 4;
use constant _b => 5;
use constant _a => 6;
gluTessCallback($tess, GLU_TESS_COMBINE, sub {
my ($x, $y, $z, # new vertex location
$v0, $v1, $v2, $v3, # border vertex arrayrefs
$w0, $w1, $w2, $w3, # border vertex weights
$polygon_data) = @_; # optional data passed to gluTessBeginPolygon
# $v0 will contain [$x, $y, $z, $r, $g, $b, $a]
return ( # generate a point with color weighted from the surrounding vertices
$x, $y, $z,
$w0*$v0->[_r] + $w1*$v1->[_r] + $w2*$v2->[_r] + $w3*$v3->[_r],
$w0*$v0->[_g] + $w1*$v1->[_g] + $w2*$v2->[_g] + $w3*$v3->[_g],
$w0*$v0->[_b] + $w1*$v1->[_b] + $w2*$v2->[_b] + $w3*$v3->[_b],
$w0*$v0->[_a] + $w1*$v1->[_a] + $w2*$v2->[_a] + $w3*$v3->[_a],
($v0->[7] || $v1->[7] || $v2->[7] || $v3->[7]), # if we received vertex data - return some for the new vertex
);
});
# works with gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
# OR
# works with gluTessCallback($tess, GLU_TESS_VERTEX, sub {
# my ($x, $y, $z, $r, $g, $b, $a, $vertex_data) = @_;
# glColor4f($r, $g, $b, $a);
# glVertex3f($x, $y, $z);
# });
so when does this even fire? the combine callback gets called if the tessellator decides a new vertex is needed โ which, bestie, happens with self intersecting polygons. and when it does, the COMBINE callback is how you interpolate appropriate values for normals, and colors, or honestly for any desired information you want. main character energy ๐ฅ
the combine callback will be passed the following:
$x, $y, $zโ the x y and z coordinates of the new vertex being created.$v0, $v1, $v2, $v3โ arrayrefs of vertex information for the vertices bordering this new vertex (the ones that caused the new vertex to be created).
by default if gluNewTess() is called, these arrayrefs will be passed:
my ($x, $y, $z, $vertex_data) = @$v0;
# received from gluTessVertex_p($tess, $x, $y, $z, $vertex_data);
if gluNewTess('do_colors') is called, the following will be passed:
my ($x, $y, $z, $r, $g, $b, $a, $vertex_data) = @$v0;
# received from gluTessVertex_p($tess, $x, $y, $z, $r, $g, $b, $a, $vertex_data);
if gluNewTess('do_colors', 'do_normals') is called, the following will be passed:
my ($x, $y, $z, $r, $g, $b, $a, $nx, $ny, $nz, $vertex_data) = @$v0;
# received from gluTessVertex_p($tess, $x, $y, $z, $r, $g, $b, $a, $nx, $ny, $nz, $vertex_data);
if gluNewTess(undef, 'do_normals') is called, the following will be passed:
my ($x, $y, $z, $nx, $ny, $nz, $vertex_data) = @$v0;
# received from gluTessVertex_p($tess, $x, $y, $z, $nx, $ny, $nz, $vertex_data);
in all cases, the data returned by the COMBINE callback should be in the same format that each of the vertices are in when passed into the COMBINE callback. match the vibe you were given, ok? ๐
$w0, $w1, $w2, $w3โ weights of the participating vertices (weight$w0corresponds to vertex$v0).- optional
$polygon_dataโ any optional data passed togluTessBeginPolygon. normally this would only be passed toGLU_TESS_COMBINE_DATA, butGLU_TESS_COMBINE_DATAandGLU_TESS_COMBINEshare the same code implementation. iykyk ๐
โจ "GLU_TESS_COMBINE_DATA" โจ
identical in function to the GLU_TESS_COMBINE handler. literally the same. they use the same callback implementation, so no surprises here, no cap ๐
โจ "GLU_TESS_ERROR" โจ
gluTessCallback($tess, GLU_TESS_ERROR, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_ERROR, \&glEdgeFlag);
gluTessCallback($tess, GLU_TESS_ERROR, sub {
my $errno = shift;
my $err = gluErrorString($errno);
warn "Received a glu tess error ($errno - $err)\n";
});
the 'DEFAULT' option installs a c-handler that warns with the appropriate gluErrorString. so it'll tell you what went wrong, slay ๐
and if $polygon_data was set during gluTessBeginPolygon, it is discarded. gone. into the void. ๐
โจ "GLU_TESS_ERROR_DATA" โจ
similar to GLU_TESS_ERROR but it will be passed the optional $polygon_data set in gluTessBeginPolygon if any. the 'DEFAULT' handler will ignore this data though โ she's not nosy like that ๐
okay bestie so we were mid-flow with that GLU_TESS_ERROR_DATA callback example, and hereโs the rest of it โ itโs giving error-handling realness:
gluTessCallback($tess, GLU_TESS_ERROR_DATA, sub {
my ($errno, $polygon_data) = @_;
my $err = gluErrorString($errno);
warn "Received a glu tess error ($errno - $err)\n";
warn "And I received polygon_data\n" if $polygon_data;
});
๐
GLU_TESS_EDGE_FLAG ๐
so for GLU_TESS_EDGE_FLAG, youโve got options, and theyโre all lowkey iconic. You can pass 'DEFAULT', or a code ref to \&glEdgeFlag, or a sub. Like:
gluTessCallback($tess, GLU_TESS_EDGE_FLAG, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_EDGE_FLAG, \&glEdgeFlag);
gluTessCallback($tess, GLU_TESS_EDGE_FLAG, sub {
my ($flag) = @_;
glEdgeFlag($flag);
});
The 'DEFAULT' option installs a c-handler that calls the glEdgeFlag c function directly without round-tripping out to perl. (no cap, itโs speedy.)
If $polygon_data was set during gluTessBeginPolygon, it is discarded. (rip, but okay.)
๐ฆ GLU_TESS_EDGE_FLAG_DATA ๐ฆ
Similar to GLU_TESS_EDGE_FLAG but will be passed $polygon_data set in gluTessBeginPolygon if any. The 'DEFAULT' handler will ignore this data. So if you need that polygon data, use this one, bestie.
gluTessCallback($tess, GLU_TESS_EDGE_FLAG_DATA, sub {
my ($flag, $polygon_data) = @_;
glEdgeFlag($flag);
print "glEdgeFlag - and I received polygon_data\n" if $polygon_data;
});
๐ธ Example: Basic Arrowhead ๐ธ
okay so this example is literally the blueprint. It sets up a window, does the tessellation thing, and draws an arrowhead. Hereโs the code, no edits:
use OpenGL qw(:all);
glutInit();
glutInitWindowSize(501, 501);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Tessellation");
glMatrixMode(GL_PROJECTION());
glLoadIdentity();
glOrtho(-250,250,-250,250,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
my $view_triangles = 1; # set to zero to show polygon
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) if $view_triangles;
glutDisplayFunc(sub {
glColor3f(1,1,1);
my $tess = gluNewTess();
gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_END, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_COMBINE, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_ERROR, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_EDGE_FLAG, 'DEFAULT') if ! $view_triangles;
gluTessBeginPolygon($tess);
gluTessBeginContour($tess);
gluTessVertex_p($tess, 0, 200, 0);
gluTessVertex_p($tess, 150, -200, 0);
gluTessVertex_p($tess, 0, -100, 0);
gluTessVertex_p($tess, -150, -200, 0);
gluTessEndContour($tess);
gluTessEndPolygon($tess);
gluDeleteTess($tess);
glutSwapBuffers();
});
glutMainLoop();
๐ Example: Multiple contours ๐
multiple contours? we love to see it. This one uses an array of arrays to define two shapes, and tessellates them both. Slay.
use OpenGL qw(:all);
glutInit();
glutInitWindowSize(501, 501);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Tessellation");
glMatrixMode(GL_PROJECTION());
glLoadIdentity();
glOrtho(-250,250,-250,250,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
my $view_triangles = 1; # set to zero to show polygon
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) if $view_triangles;
glutDisplayFunc(sub {
glColor3f(1,1,1);
my $v = [[[125,0,0], [150,150,0], [0,125,0], [-150,150,0],
[-125,0,0], [-150,-150,0], [0,-125,0], [150,-150,0], [125,0,0]],
[[75,0,0], [100,100,0], [0,75,0], [-100,100,0],
[-75,0,0], [-100,-100,0], [0,-75,0], [100,-100,0], [75,0,0]]
];
my $tess = gluNewTess();
gluTessCallback($tess, GLU_TESS_BEGIN, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_END, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_VERTEX, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_COMBINE, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_ERROR, 'DEFAULT');
gluTessCallback($tess, GLU_TESS_EDGE_FLAG, 'DEFAULT') if ! $view_triangles;
gluTessBeginPolygon($tess);
foreach (@$v) {
gluTessBeginContour($tess);
foreach (@$_) {
gluTessVertex_p($tess, @$_);
}
gluTessEndContour($tess);
}
gluTessEndPolygon($tess);
gluDeleteTess($tess);
glutSwapBuffers();
});
glutMainLoop();
๐ Example: Sample OO Tessellation interface using polygon_data ๐
and now for the main character: an object-oriented tessellation interface using polygon_data. This is highkey iconic. It defines a package OpenGL::Tess and does all the callback magic. Hereโs the whole thing:
use OpenGL qw(:all);
glutInit();
glutInitWindowSize(501, 501);
glutInitDisplayMode(GLUT_RGB | GLUT_DOUBLE);
glutCreateWindow("Tessellation");
glMatrixMode(GL_PROJECTION());
glLoadIdentity();
glOrtho(-250,250,-250,250,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
my $view_triangles = 0;
glPolygonMode(GL_FRONT_AND_BACK, GL_LINE) if $view_triangles;
glutDisplayFunc(sub {
glColor3f(1,1,1);
my $v = [[[125,0,0], [150,150,0, 0,1,0], [0,125,0], [-150,150,0, 1,0,0],
[-125,0,0], [-150,-150,0, 0,0,1], [0,-125,0], [150,-150,0, 1,1,0], [125,0,0]],
[[75,0,0], [100,100,0], [0,75,0], [-100,100,0],
[-75,0,0], [-100,-100,0], [0,-75,0], [100,-100,0], [75,0,0]]
];
OpenGL::Tess->new(do_colors => 1, no_edge_flag => $view_triangles)->draw_contours(@$v);
glutSwapBuffers();
});
glutMainLoop();
###----------------------------------------------------------------###
package OpenGL::Tess;
# Sample object oriented Tessellator
# OpenGL::Tess->new(do_colors => 1, no_edge_flag => $view_triangles)->draw_contours(@$v);
use strict;
sub new {
my $class = shift;
my $self = bless {@_}, $class;
my $tess = $self->{'_tess'} = OpenGL::gluNewTess($self->do_colors);
for my $cb (qw(begin end vertex combine error edge_flag)) {
my $enum = OpenGL->can("GLU_TESS_\U${cb}_DATA") || die "Couldn't find callback for $cb";
my $name = "_$cb";
OpenGL::gluTessCallback($tess, $enum->(), sub { $_[-1]->$name(@_) });
}
return $self;
}
sub DESTROY {
my $tess = shift->{'_tess'};
OpenGL::gluDeleteTess($tess) if $tess;
}
sub tess {
my $self = shift;
return $self->{'_tess'} || die "Missing tess";
}
sub do_colors { shift->{'do_colors'} }
sub begin_polygon {
my $self = shift;
my $tess = $self->tess;
# self will be passed as last arg ([-1]) to all callbacks as opaque polygon data
return OpenGL::gluTessBeginPolygon($tess, $self);
}
sub end_polygon { OpenGL::gluTessEndPolygon( shift->tess) }
sub begin_contour { OpenGL::gluTessBeginContour(shift->tess) }
sub end_contour { OpenGL::gluTessEndContour( shift->tess) }
sub draw_contours {
my $self = shift;
$self->begin_polygon;
foreach my $c (@_) {
$self->begin_contour;
$self->add_vertex(@$_) for @$c;
$self->end_contour;
}
$self->end_polygon;
}
sub add_vertex {
my $self = shift;
die 'Usage $self->add_vertex($x,$y,$z)' if @_ < 3;
if ($self->do_colors) {
push @_, 1 for @_ .. 6;
OpenGL::gluTessVertex_p($self->tess, @_[0..6]);
} else {
OpenGL::gluTessVertex_p($self->tess, @_[0..3]);
}
}
sub _begin {
my ($self, $enum) = @_;
OpenGL::glBegin($enum);
}
sub _end { OpenGL::glEnd() }
sub _vertex {
my ($self, $x, $y, $z, $r, $g, $b, $a) = @_;
OpenGL::glColor4f($r, $g, $b, $a) if $self->do_colors;
OpenGL::glVertex3f($x, $y, $z);
}
sub _edge_flag {
my ($self, $flag) = @_;
return if $self->{'no_edge_flag'};
OpenGL::glEdgeFlag($flag);
}
sub _error {
my ($self, $errno) = @_;
warn __PACKAGE__ ." error: ".OpenGL::gluErrorString($errno);
}
sub _combine {
my ($self, $x, $y, $z, $v0, $v1, $v2, $v3, $w0, $w1, $w2, $w3) = @_;
return ($x, $y, $z) if !$self->do_colors;
return ($x, $y, $z,
$w0*$v0->[3] + $w1*$v1->[3] + $w2*$v2->[3] + $w3*$v3->[3],
$w0*$v0->[4] + $w1*$v1->[4] + $w2*$v2->[4] + $w3*$v3->[4],
$w0*$v0->[5] + $w1*$v1->[5] + $w2*$v2->[5] + $w3*$v3->[5],
$w0*$v0->[6] + $w1*$v1->[6] + $w2*$v2->[6] + $w3*$v3->[6]);
}
1;
๐ AUTHOR ๐
Paul Seamons - paul AT seamons dot com - 2011
and thatโs the whole page, bestie! This masterpiece is from perl v5.40.1, dated 2025-03-21, and itโs the OpenGL...lation(3pm) manpage. Stay tessellated ๐ โจ
๐ฆ where this page comes from
- debian trixie: libopengl-perl 0.7004+dfsg-1 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ubuntu resolute: libopengl-perl 0.7004+dfsg-1 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms