1#!/usr/local/bin/perl
2#
3# This is an example program from the legacy TriD/OpenGL/examples
4# directory moved here for reference.  It only uses OpenGL features
5# so can probably be deprecated in favor of the Perl OpenGL
6# examples eventually.
7#
8#     clip
9#
10# This program demonstrates arbitrary clipping planes.
11# Based on the "clip.c" program in the
12# OpenGL Programming Guide, Chapter 3, page 108, Listing 3-5.
13# However this program clips the front part of a rotating cube
14# with flat shaded faces instead of a wire frame sphere.
15#
16# the C synopsis of glClipPlane is
17#
18#   void glClipPlane(GLenum plane,const GLdouble *equation )
19#
20# For PERL the routine glpClipPlane was added, and the synopsis is:
21#
22#   void glpClipPlane(GLenum plane,GLdouble a,GLdouble b,GLdouble c,GLdouble d)
23#
24# and the 4 double vector (equasion) is packaged by the XSUB.
25# Or you can still use glClipPlane but then you have to pack() the structure
26#
27# note:  the statement f(@a) is equivalent to f($a[0],$a[1], ... $a[n])
28#        i.e. elements of a list are put on the call stack
29#
30
31use PDL::Graphics::OpenGL::Perl::OpenGL;
32use OpenGL qw(:all);
33
34sub tacky_cube {
35    local($s) = @_;
36    local(@x,@y,@z,@f);
37    local($i,$j,$k);
38    local(@r,@g,@b);
39    $s = $s/2.0;
40    @x=(-$s,-$s,-$s,-$s,$s,$s,$s,$s);
41    @y=(-$s,-$s,$s,$s,-$s,-$s,$s,$s);
42    @z=(-$s,$s,$s,-$s,-$s,$s,$s,-$s);
43    @f=(
44        0, 1, 2, 3,
45        3, 2, 6, 7,
46        7, 6, 5, 4,
47        4, 5, 1, 0,
48        5, 6, 2, 1,
49        7, 4, 0, 3,
50        );
51    @r=(1.0, 0,   0,   1.0, 1.0, 0);
52    @g=(0,   1.0, 0,   1.0, 0,   1.0);
53    @b=(0,   0,   1.0, 0,   1.0, 1.0);
54    for($i=0;$i<6;$i++){
55	glColor3f($r[$i],$g[$i],$b[$i]);
56        glBegin(GL_POLYGON);
57        for($j=0;$j<4;$j++){
58                $k=$f[$i*4+$j];
59                glVertex3d($x[$k],$y[$k],$z[$k]);
60        }
61        glEnd();
62    }
63
64}
65
66sub add_clip_plane {
67    # give the plane a slight tilt-away to prove we're not just
68    # clipping against the view volume
69    @eqn = (0.0, -0.3, -1.0, 1.2);
70    OpenGL::glpClipPlane(GL_CLIP_PLANE0, @eqn);
71    glEnable(GL_CLIP_PLANE0);
72}
73
74sub display{
75    glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
76    glPushMatrix();
77    glRotatef($spin, 0.0, 1.0, 0.0);
78    tacky_cube(3.0);
79    glPopMatrix();
80    glFlush();
81    glXSwapBuffers;
82}
83
84
85sub myReshape {
86    # glViewport(0, 0, w, h);
87    glMatrixMode(GL_PROJECTION);
88    glLoadIdentity();
89    gluPerspective(60.0, 1.0 , 1.0, 20.0);
90    glMatrixMode(GL_MODELVIEW);
91    glLoadIdentity ();
92}
93
94OpenGL::glpOpenWindow(width => 400, height => 400,
95	      attributes => [GLX_RGBA,GLX_DOUBLEBUFFER]);
96glClearColor(0,0,0,1);
97glShadeModel (GL_FLAT);
98myReshape();
99glDisable(GL_CULL_FACE);
100glEnable(GL_DEPTH_TEST);
101
102glLoadIdentity ();
103glTranslatef (0.0, 0.0, -5.0);
104add_clip_plane;
105
106# test glGetClipPlane()
107($a,$b,$c,$d)=OpenGL::glpGetClipPlane(GL_CLIP_PLANE0);
108print "Clipping plane (a,b,c,d) = ($a,$b,$c,$d)\n";
109
110$spin=0;
111while(1) {$spin += 1.0; display;}
112