| 1 | # Copyright (C) 1998 Tuomas J. Lukka. | ||
| 2 | # All rights reserved, except redistribution | ||
| 3 | # with PDL under the PDL License permitted. | ||
| 4 | |||
| 5 | package PDL::Demos::TriD2; | ||
| 6 | |||
| 7 | use PDL; | ||
| 8 | use PDL::Graphics::TriD; | ||
| 9 | use PDL::Graphics::TriD::Image; | ||
| 10 | |||
| 11 | PDL::Demos::Routines->import(); | ||
| 12 | sub comment($); | ||
| 13 | sub act($); | ||
| 14 | sub actnw($); | ||
| 15 | sub output; | ||
| 16 | |||
| 17 | sub run { | ||
| 18 | |||
| 19 | comment q| | ||
| 20 | Welcome to a short tour of the more esoteric capabilities of | ||
| 21 | PDL::Graphics::TriD. | ||
| 22 | |||
| 23 | As in '3d', press 'q' in the graphics window for the next | ||
| 24 | screen. Rotate the image by pressing mouse button one and | ||
| 25 | dragging in the graphics window. | ||
| 26 | |||
| 27 | Note that the script must start with | ||
| 28 | |||
| 29 | use PDL; | ||
| 30 | use PDL::Graphics::TriD; | ||
| 31 | use PDL::Graphics::TriD::Image; | ||
| 32 | |||
| 33 | to work. | ||
| 34 | |; | ||
| 35 | |||
| 36 | actnw q| | ||
| 37 | # Number of subdivisions for lines / surfaces. | ||
| 38 | $size = 25; | ||
| 39 | |||
| 40 | # You remember this from the first 3d demo, right? | ||
| 41 | $r = (xvals zeroes $size+1,$size+1) / $size; | ||
| 42 | $g = (yvals zeroes $size+1,$size+1) / $size; | ||
| 43 | $b = ((sin($r*6.3) * sin($g*6.3)) ** 3)/2 + 0.5; # Bumps | ||
| 44 | imagrgb [$r,$g,$b]; # Draw an image | ||
| 45 | # [press 'q' in the graphics window when done] | ||
| 46 | |; | ||
| 47 | |||
| 48 | actnw q| | ||
| 49 | # How about this? | ||
| 50 | imagrgb3d([$r,$g,$b]); # Draw an image on the lower plane | ||
| 51 | # [press 'q' in the graphics window when done] | ||
| 52 | |; | ||
| 53 | |||
| 54 | actnw q| | ||
| 55 | # Let's add the real image on top of this... | ||
| 56 | hold3d(); | ||
| 57 | imag3d([$r,$g,$b+0.1], [$r,$g,$b]); | ||
| 58 | # For the next demo, please rotate this so that much | ||
| 59 | # of the image is visible. | ||
| 60 | # Don't make your window too big or you might run out of memory | ||
| 61 | # at the next step. | ||
| 62 | # [press 'q' in the graphics window when done] | ||
| 63 | |; | ||
| 64 | |||
| 65 | actnw q| | ||
| 66 | # Warning: your mileage will vary based on which | ||
| 67 | # OpenGL implementation you are using :( | ||
| 68 | # Let's grab this picture... | ||
| 69 | $pic = grabpic3d(); | ||
| 70 | |||
| 71 | # Lighten it up a bit so you see the background, | ||
| 72 | # black on black is confusing | ||
| 73 | $l = 0.3; | ||
| 74 | $pic = ($pic + $l) / (1 + $l); | ||
| 75 | |||
| 76 | # And plot it in the picture ;) ;) | ||
| 77 | hold3d(); # You remember, we leave the previous one in... | ||
| 78 | $o0 = imagrgb3d($pic, {Points => [[0,0,0],[0,1,0],[0,1,1],[0,0,1]]}); | ||
| 79 | |||
| 80 | # Because we have the data in $pic, we could just as easily | ||
| 81 | # save it in a jpeg using the PDL::Io::Pic module - or read | ||
| 82 | # it from one. | ||
| 83 | # [press 'q' in the graphics window when done] | ||
| 84 | |; | ||
| 85 | |||
| 86 | actnw q| | ||
| 87 | # That was fun - let's do that again! | ||
| 88 | $pic1 = grabpic3d(); | ||
| 89 | |||
| 90 | # Lighten it up | ||
| 91 | $pic1 = ($pic1 + $l) / (1 + $l); | ||
| 92 | |||
| 93 | # And plot it in the picture ;) ;) | ||
| 94 | hold3d(); # You remember, we leave the previous one in... | ||
| 95 | $o1 = imagrgb3d($pic1, {Points => [[0,0,0],[1,0,0],[1,0,1],[0,0,1]]}); | ||
| 96 | # [press 'q' in the graphics window when done] | ||
| 97 | |; | ||
| 98 | |||
| 99 | actnw q| | ||
| 100 | # Now, let's update them in real time! | ||
| 101 | nokeeptwiddling3d(); # Don't wait for user while drawing | ||
| 102 | while(1) { | ||
| 103 | $p = grabpic3d(); | ||
| 104 | $p = ($p + $l) / (1 + $l); | ||
| 105 | $pic .= $p; $pic1 .= $p; | ||
| 106 | $o0->data_changed(); $o1->data_changed(); | ||
| 107 | last if twiddle3d(); # exit from loop when 'q' pressed | ||
| 108 | } | ||
| 109 | # [press 'q' in the graphics window when done] | ||
| 110 | |; | ||
| 111 | |||
| 112 | actnw q| | ||
| 113 | # Finally, leave 3d in a sane state | ||
| 114 | keeptwiddling3d(); # Don't wait for user while drawing | ||
| 115 | release3d(); | ||
| 116 | # [press 'q' in the graphics window when done] | ||
| 117 | |; | ||
| 118 | |||
| 119 | } | ||
| 120 | |||
| 121 | 1; |