File: OpenGLQ.pd

package info (click to toggle)
libpdl-graphics-trid-perl 2.102-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 480 kB
  • sloc: perl: 5,082; ansic: 683; makefile: 6
file content (280 lines) | stat: -rw-r--r-- 7,996 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use strict;
use warnings;
use PDL::Types qw(types ppdefs_all);

{ no warnings 'once'; # pass info back to Makefile.PL
$PDL::Core::Dev::EXTRAS{$::PDLMOD}{OBJECT} .= join '', map " $::PDLBASE-$_\$(OBJ_EXT)", qw(font sphere);
}

my $F = ['F'];

pp_addpm({At=>'Top'},<<'EOD');
=head1 NAME

PDL::Graphics::OpenGLQ - quick routines to plot lots of stuff from ndarrays.

=head1 SYNOPSIS

only for internal use - see source

=head1 DESCRIPTION

only for internal use - see source

=head1 AUTHOR

Copyright (C) 1997,1998 Tuomas J. Lukka.  
All rights reserved. There is no warranty. You are allowed
to redistribute this software / documentation under certain
conditions. For details, see the file COPYING in the PDL 
distribution. If this file is separated from the PDL distribution, 
the copyright notice should be included in the file.

=cut

EOD

pp_addhdr('
#ifdef __APPLE__
#include <OpenGL/gl.h>
#else
#include <GL/gl.h>
#endif
');

my @internal = (Doc => 'internal', NoPthread => 1);

pp_def(
  'gl_spheres',
  GenericTypes => $F,
  Pars => 'coords(tri=3,n);',
  OtherPars => 'double radius; int slices; int stacks;',
  CHeader => "char *pdl_3d_solidSphere(double radius, GLint slices, GLint stacks);\n",
  Code => '
    float oldcoord0 = 0.0, oldcoord1 = 0.0, oldcoord2 = 0.0;
    glPushMatrix();
    loop(n) %{
      glTranslatef(
	$coords(tri=>0) - oldcoord0,
	$coords(tri=>1) - oldcoord1,
	$coords(tri=>2) - oldcoord2
      );
      char *err = pdl_3d_solidSphere($COMP(radius), $COMP(slices), $COMP(stacks));
      if (err) $CROAK("%s", err);
      oldcoord0 = $coords(tri=>0), oldcoord1 = $coords(tri=>1), oldcoord2 = $coords(tri=>2);
    %}
    glPopMatrix();
  ',
  @internal
);

sub TRI { my $par = join '', @_; join ',', map "$par(tri => $_)", 0..2 }
sub make_tri { shift()."(".TRI(@_).");\n" }
sub COLOR { make_tri("glColor3f",'$colors',@_) }
sub VERTEX { make_tri("glVertex3f",'$coords',@_) }
sub NORMAL { make_tri("glNormal3f",'$norm',@_) }
sub RPOS { make_tri("glRasterPos3f",'$coords',@_) }
sub ADCOLOR { "
  {
    GLfloat ad[] = { ".TRI('$colors'.$_[0]).",1.0 };
    glMaterialfv(GL_FRONT_AND_BACK,GL_DIFFUSE, ad);
  }
" }

sub make_func {
  my ($name) = @_;
  for (['_col', ' colors(tri,n);', COLOR().VERTEX()], ['_nc', '', VERTEX()]) {
    pp_def(lc($name).$_->[0],
      GenericTypes => $F,
      Pars => 'coords(tri=3,n);'.$_->[1],
      Code => '
        glBegin('.uc($name).');
        loop(n) %{'.$_->[2].'%}
        glEnd();
      ',
      @internal
    );
  }
}

make_func($_) for qw(gl_line_strip gl_lines gl_points);

pp_def(
  'gl_texts',
  GenericTypes => $F,
  Pars => 'coords(tri,x); ',
  OtherPars => 'SV *arr',
  CHeader => "void pdl_3d_bitmapString( const unsigned char *string );\n",
  Code => '
    SV *sv = $COMP(arr);
    if (!(SvROK(sv) && SvTYPE(SvRV(sv))==SVt_PVAV))
      $CROAK("gl_texts requires an array ref");
    AV *arr = (AV *)SvRV(sv);
    loop(x) %{
      SV *elem = *(av_fetch(arr, x, 0));
      if (!elem) continue;
      char *str = SvPV_nolen(elem);
      '.RPOS().'
      pdl_3d_bitmapString((const unsigned char *)str);
    %}
  ',
  @internal
);

for my $m (
{Suf => '_mat',
 Func => \&ADCOLOR},
{Suf => '',
 Func => \&COLOR},
) {
for(
{Name => 'gl_triangles',
 NormalCode => ''},
{Name => 'gl_triangles_n',
 NormalInit => '
	tmp1[0] = $coordsb(tri => 0) - $coordsa(tri => 0);
	tmp1[1] = $coordsb(tri => 1) - $coordsa(tri => 1);
	tmp1[2] = $coordsb(tri => 2) - $coordsa(tri => 2);
	tmp2[0] = $coordsc(tri => 0) - $coordsa(tri => 0);
	tmp2[1] = $coordsc(tri => 1) - $coordsa(tri => 1);
	tmp2[2] = $coordsc(tri => 2) - $coordsa(tri => 2);
        tmp3[0] = tmp1[1]*tmp2[2] - tmp2[1]*tmp1[2];
        tmp3[1] = -(tmp1[0]*tmp2[2] - tmp2[0]*tmp1[2]);
        tmp3[2] = tmp1[0]*tmp2[1] - tmp2[0]*tmp1[1];
        magn = sqrt(tmp3[0]*tmp3[0] + tmp3[1]*tmp3[1] + tmp3[2]*tmp3[2]);
        tmp3[0] = tmp3[0] / magn;
        tmp3[1] = tmp3[1] / magn;
        tmp3[2] = tmp3[2] / magn;',
 NormalCode => '
	glNormal3f(tmp3[0], tmp3[1], tmp3[2]);
 '
},
{Name => 'gl_triangles_wn',
 NormalArgs => 'norma(tri); normb(tri); normc(tri);',
 (map {("NormalCode".($_ eq 'A'?'':$_),NORMAL(lc $_))} ('A'..'C')),
}) {
# This may be suboptimal but should still be fast enough..
# We only do triangles with this.
pp_def(
	$_->{Name}.$m->{Suf},
	GenericTypes => $F,
	Pars => 'coordsa(tri=3); coordsb(tri); coordsc(tri);'.
		 ($_->{NormalArgs}//'').
		'colorsa(tri); colorsb(tri); colorsc(tri);',
	Code => '
		float tmp1[3], tmp2[3], tmp3[3], magn;
		glBegin(GL_TRIANGLES);
		broadcastloop %{'.
			($_->{NormalInit}//"\n").
			($_->{NormalCode}//'').
			&{$m->{Func}}("a").VERTEX("a").
			($_->{NormalCodeB}//'').
			&{$m->{Func}}("b").VERTEX("b").
			($_->{NormalCodeC}//'').
			$m->{Func}->("c").VERTEX("c").'
		%}
		glEnd();
		',
		@internal
);
}
}

pp_def('gl_arrows',
	Pars => 'coords(tri=3,n); indx indsa(); indx indsb();',
	OtherPars => 'float headlen; float width;',
	Code => '
		float hl = $COMP(headlen);
		float w = $COMP(width);
		float tmp2[3] = { 0.000001, -0.0001, 1 };
		broadcastloop %{
			PDL_Indx a = $indsa(), b = $indsb();
			float tmp1[3]; 
			float norm[3];
			float norm2[3];
			float normlen,origlen,norm2len;
			tmp1[0] = $coords(tri => 0, n => a) -
				  $coords(tri => 0, n => b);
			tmp1[1] = $coords(tri => 1, n => a) -
				  $coords(tri => 1, n => b);
			tmp1[2] = $coords(tri => 2, n => a) -
				  $coords(tri => 2, n => b);
			float partback[3];
			partback[0] = $coords(tri => 0, n => b) + hl*tmp1[0];
			partback[1] = $coords(tri => 1, n => b) + hl*tmp1[1];
			partback[2] = $coords(tri => 2, n => b) + hl*tmp1[2];
			norm[0] = tmp1[1]*tmp2[2] - tmp2[1]*tmp1[2];
			norm[1] = -(tmp1[0]*tmp2[2] - tmp2[0]*tmp1[2]);
			norm[2] = tmp1[0]*tmp2[1] - tmp2[0]*tmp1[1];
			norm2[0] = tmp1[1]*norm[2] - norm[1]*tmp1[2];
			norm2[1] = -(tmp1[0]*norm[2] - norm[0]*tmp1[2]);
			norm2[2] = tmp1[0]*norm[1] - norm[0]*tmp1[1];
			normlen = sqrt(norm[0] * norm[0] +
				norm[1] * norm[1] + norm[2] * norm[2]);
			norm2len = sqrt(norm2[0] * norm2[0] +
				norm2[1] * norm2[1] + norm2[2] * norm2[2]);
			origlen = sqrt(tmp1[0] * tmp1[0] +
				tmp1[1] * tmp1[1] + tmp1[2] * tmp1[2]);
			norm[0] *= w/normlen;
			norm[1] *= w/normlen;
			norm[2] *= w/normlen;
			norm2[0] *= w/norm2len;
			norm2[1] *= w/norm2len;
			norm2[2] *= w/norm2len;
			tmp1[0] /= origlen;
			tmp1[1] /= origlen;
			tmp1[2] /= origlen;
			glBegin(GL_LINES);
			glVertex3d( $coords(tri => 0, n => a) ,
				    $coords(tri => 1, n => a) ,
				    $coords(tri => 2, n => a) );
			glVertex3d( $coords(tri => 0, n => b) ,
				    $coords(tri => 1, n => b) ,
				    $coords(tri => 2, n => b) );
			glEnd();
			if(w!=0) {
			glBegin(GL_TRIANGLES);
			glVertex3d( $coords(tri => 0, n => b) ,
				    $coords(tri => 1, n => b) ,
				    $coords(tri => 2, n => b) );
			glVertex3d( partback[0] + norm[0],
				    partback[1] + norm[1],
				    partback[2] + norm[2]);
			glVertex3d( partback[0] + norm2[0],
				    partback[1] + norm2[1],
				    partback[2] + norm2[2]);
			glVertex3d( $coords(tri => 0, n => b) ,
				    $coords(tri => 1, n => b) ,
				    $coords(tri => 2, n => b) );
			glVertex3d( partback[0] - norm[0],
				    partback[1] - norm[1],
				    partback[2] - norm[2]);
			glVertex3d( partback[0] - norm2[0],
				    partback[1] - norm2[1],
				    partback[2] - norm2[2]);
			glVertex3d( $coords(tri => 0, n => b) ,
				    $coords(tri => 1, n => b) ,
				    $coords(tri => 2, n => b) );
			glVertex3d( partback[0] + norm2[0],
				    partback[1] + norm2[1],
				    partback[2] + norm2[2]);
			glVertex3d( partback[0] - norm[0],
				    partback[1] - norm[1],
				    partback[2] - norm[2]);
			glVertex3d( $coords(tri => 0, n => b) ,
				    $coords(tri => 1, n => b) ,
				    $coords(tri => 2, n => b) );
			glVertex3d( partback[0] - norm2[0],
				    partback[1] - norm2[1],
				    partback[2] - norm2[2]);
			glVertex3d( partback[0] + norm[0],
				    partback[1] + norm[1],
				    partback[2] + norm[2]);
			glEnd();
			}
		%}
	',
	@internal
);

pp_done();