Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Adding text to PDL's TriD 3d graphs

by zentara (Archbishop)
on Jan 10, 2007 at 19:57 UTC ( #594002=snippet: print w/ replies, xml ) Need Help??

Description: This is just a piece of eye candy, which is just a combination (and slight enhancement ) of examples which come with the PDL module. It is basically a little package which allows you to specify text and 3d position on a TriD graph.

The current OpenGL usage is still kind of tricky, but it's usable. The position of the text is specified as $x,$y,$z, where each can vary between 0 and 1, and represent the relative point in space where the left anchor of the text is attached. I'm sure there are many other things you can do with GL, but the docs are hard to find.

Anyways, you can just copy this package into your scripts, to give you text capability with changable text and position, or store it in a module.

To cycle thru the script, hit 'q' repeatedly.

#!/usr/bin/perl
use warnings;
use strict;
use PDL::LiteF;
use PDL::Graphics::TriD;
use PDL::Graphics::TriD::GL;
use PDL::Graphics::OpenGL;

# hit 'q' repeatedly to cycle thru the script

package TOBJ;
BEGIN{@TOBJ::ISA = qw/PDL::Graphics::TriD::Object/;}
use PDL::Graphics::OpenGLQ;
use PDL::Graphics::OpenGL;

my $self;

sub new {
 my ($pkg, $lb, $text, $pos) = @_;
# print "@_\n"; 
 
 $self = bless {
      "lb"   => $lb,
      "text"  => $text,
      "x" => $pos->[0],
      "y" => $pos->[1],
      "z" => $pos->[2],
   }, $pkg;
 
 return $self;
}

sub togl {
#        print "togl @_\n";
        glDisable(&GL_LIGHTING);
    glColor3f(1,0,1);
    glRasterPos3f($self->{'x'},$self->{'y'},$self->{'z'});
    PDL::Graphics::OpenGL::glpPrintString($self->{'lb'}, $self->{'text
+'});
    glEnable(&GL_LIGHTING);
}

sub ctext{
   print "ctext @_\n";
   $self->{'text'} = $_[1];
   
   if( $_[2] ){ 
   my $pos = $_[2]; 
   $self->{'x'} = $pos->[0],
   $self->{'y'} = $pos->[1],
   $self->{'z'} = $pos->[2],
   
    }

}

1;

#######################################################
package main;

# little piddle to add some color
my $pdlc = zeroes(3); #each elem has 3 elements
my $col = $pdlc->slice('1:1,:,:'); #green
#$col = $pdlc->slice('0:0,:,:'); #red
$col += 1;

#get GL window to pass to text object
my $win = PDL::Graphics::TriD::get_current_window();
my $vp = $win->current_viewport;
my $lb = $win->glpRasterFont("10x20",0,255);
# glShadeModel (&GL_FLAT);
glShadeModel (&GL_SMOOTH);


my $tobj = new TOBJ($lb,'points3d SURF2D', [0,0,0]);
$win->add_object($tobj);
hold3d();  #objects built up to this point will be held

my $nx = 20;
my $t =  (xvals zeroes $nx+1,$nx+1)/$nx;
my $u =  (yvals zeroes $nx+1,$nx+1)/$nx;
my $x = sin($u*15 + $t * 3)/2+0.5 + 5*($t-0.5)**2;
points3d(['SURF2D',$x],$pdlc);

$vp->setview([3,3,3]);
$tobj->ctext('line3d SURF2D',[0,0,0]);
line3d(['SURF2D',$x],$pdlc);


$vp->setview([3,-3,3]);
$tobj->ctext('mesh3d',[0,0,.75]);
mesh3d([$x]);

$vp->setview([3,-3,-3]);
$tobj->ctext('imag3d Lines=>0',[1,1,1]);
imag3d([$x],{Lines => 0});

$vp->setview([-3,-3,-3]);
$tobj->ctext('imag3d Lines => 0, Smooth => 1',[.75,.75,.75]);
imag3d([$x],{Lines => 0, Smooth => 1});

$vp->setview([0,0,3]);
$tobj->ctext('imag3d',[0,0,.5]);
imag3d_ns([$x]);


Comment on Adding text to PDL's TriD 3d graphs
Download Code
Re: Adding text to PDL's TriD 3d graphs
by bsb (Priest) on Mar 02, 2007 at 06:28 UTC
    Thanks for this example, I found it very useful.

    I thought I may be worth mentioning the extra element required to get 2-D position text, search for "Cargo-cult" below:

    { package Text; BEGIN{ @Text::ISA = qw/PDL::Graphics::TriD::Object/;} use PDL::Graphics::OpenGLQ; use PDL::Graphics::OpenGL; sub new { my ($class, $text, $x, $y) = @_; my $lb = PDL::Graphics::TriD::get_current_window ->glpRasterFont("8x13", +0,255); bless { lb => $lb, text => $text, posn => [$x,$y,0] }, $class; } sub togl { my $self = shift; # Cargo-cult: # http://glprogramming.com/red/chapter08.html glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0.0, 1.0, 0.0, 1.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); glDisable(&GL_LIGHTING); glColor3f(1,1,1); my @posn = @{ $self->{posn} }; glRasterPos3f($posn[0], $posn[1], $posn[2]); PDL::Graphics::OpenGL::glpPrintString($self->{lb}, $self->{tex +t}); glEnable(&GL_LIGHTING); } }

Back to Snippets Section

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: snippet [id://594002]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (8)
As of 2014-07-12 22:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    When choosing user names for websites, I prefer to use:








    Results (241 votes), past polls