bobo has asked for the wisdom of the Perl Monks concerning the following question:

I'm just starting to play with opengl so i used my friend's program written in C and "translated" it to perl (i read here that it works)... And for some reason it doesn't work... it makes a window, but no lines are drawn (the program is supposed to draw 2 lines for every 2 mouseclicks (start and end point for one line))... where could be the problem? P.S. friend said his program (in C) works when he runs it here is the code...
#!/usr/bin/perl -w use strict; use OpenGL qw/ :all /; #use Math::Trig; my (@Lx, @Ly, $Ix); #my $window; my ($sub_width,$sub_height) = (800,600); #************************************************* # Glavni program. #************************************************* glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize($sub_width,$sub_height); glutInitWindowPosition(200,400); glutInit(); #$window = glutCreateWindow("Glut OpenGL Linija"); glutCreateWindow("Glut OpenGL Linija"); glutReshapeFunc(\&myReshape); glutDisplayFunc(\&myDisplay); glutMouseFunc(\&myMouse); glutKeyboardFunc(\&myKeyboard); print "Lijevom tipkom misa zadaj tocke - algoritam Bresenham-a\n"; print "Tipke r, g, b, k mijenjaju boju.\n"; glutMainLoop(); #******************************************* # Osvjezavanje prikaza. (nakon preklapanja prozora) #******************************************* sub myDisplay{ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glFlush(); } #******************************************* # Promjena velicine prozora. #******************************************* sub myReshape{ my ($width, $height) = @_; $sub_width = $width; $sub_height = $height; $Ix = 0; glViewport(0, 0, $sub_width, $sub_height); glMatrixMode(GL_PROJECTION); glLoadIdentity(); gluOrtho2D(0, $sub_width, 0, $sub_height); glClearColor( 1.0, 1.0, 1.0, 0.0 ); glClear( GL_COLOR_BUFFER_BIT ); glPointSize(1.0); glColor3f(0.0, 0.0, 0.0); } #********************************************** # Crtaj moju liniju. #********************************************** sub crtajLinijuKut0{ my ($pocetniX, $pocetniY, $zavrsniX, $zavrsniY) = @_; print "Usao u KUT 0 !\n"; my $x = $pocetniX<$zavrsniX ? $pocetniX : $zavrsniX; my $y = $pocetniY; my $deltaX = $zavrsniX - $pocetniX; glBegin(GL_POINTS); for (0..abs($deltaX)){ glVertex2i($x, $y); $x++; } glEnd(); } sub crtajLinijuKut90{ my ($pocetniX, $pocetniY, $zavrsniX, $zavrsniY) = @_; print "Usao u KUT 90 !\n"; my $x = $pocetniX; my $y = $pocetniY<$zavrsniY ? $pocetniY : $zavrsniY; my $deltaY = $zavrsniY - $pocetniY; glBegin(GL_POINTS); for (0..abs($deltaY)){ glVertex2i($x, $y); $y++; } glEnd(); } sub crtajLinijuKutDo45{ my ($pocetniX, $pocetniY, $zavrsniX, $zavrsniY) = @_; print "Usao u KUT DO 45 !\n"; my $x = $pocetniX; my $y = $pocetniY; my $incX = 1; my $incY = 1; my $deltaX = $zavrsniX - $pocetniX; my $deltaY = $zavrsniY - $pocetniY; my $nagibPravca = abs($deltaY/$deltaX); if (($deltaX < 0)){ $incX = -1; } if ($deltaY < 0){ $incY = -1; $nagibPravca = -$nagibPravca; } my $pogreska = 0; glBegin(GL_POINTS); for (0..abs($deltaX)){ glVertex2i($x, $y); if ( abs($pogreska + $nagibPravca) < 0.5 ) { $pogreska += $nagibPravca; }else{ $y += $incY; $pogreska += $nagibPravca - $incY; } $x += $incX; } glEnd(); } sub crtajLinijuKutDo90{ my ($pocetniX, $pocetniY, $zavrsniX, $zavrsniY) = @_; print "Usao u KUT DO 90 !\n"; my $x = $pocetniX; my $y = $pocetniY; my $incX = 1; my $incY = 1; my $deltaY = $zavrsniX - $pocetniX; my $deltaX = $zavrsniY - $pocetniY; my $nagibPravca = $deltaY/$deltaX; if ($deltaX < 0){ $incY = -1; $nagibPravca = -$nagibPravca; } if ($deltaY < 0){ $incX = -1; } my $pogreska = 0; glBegin(GL_POINTS); for (0..abs($deltaX)){ glVertex2i($x, $y); if (abs($pogreska + $nagibPravca) < 0.5){ $pogreska += $nagibPravca; }else{ $x += $incX; $pogreska += $nagibPravca - $incX; } $y += $incY; } glEnd(); } sub myLine{ my ($pocetniX, $pocetniY, $zavrsniX, $zavrsniY) = @_; my $deltaX = abs($zavrsniX - $pocetniX); my $deltaY = abs($zavrsniY - $pocetniY); my $nagibPravca=abs($deltaY/$deltaX); #***************************** # NACRTAJ OBICNU LINIJU #***************************** glColor3f(1., 0, 0); glBegin(GL_LINES); { glVertex2i($pocetniX, $pocetniY + 25); glVertex2i($zavrsniX, $zavrsniY + 25); } glEnd(); glColor3f(0, 0, 0); #***************************** # BRESENHAMOV POSTUPAK #**************************** if ($deltaX == 0){ &crtajLinijuKut0($pocetniX, $pocetniY, $zavrsniX, $zavrsniY); }elsif ($deltaY == 0){ &crtajLinijuKut90($pocetniX, $pocetniY, $zavrsniX, $zavrsniY); }elsif ($nagibPravca < 1){ &crtajLinijuKutDo45($pocetniX, $pocetniY, $zavrsniX, $zavrsniY +); }elsif ($nagibPravca > 1){ &crtajLinijuKutDo90($pocetniX, $pocetniY, $zavrsniX, $zavrsniY +); } } #********************************************* # Mis. #********************************************* sub myMouse{ my ($button, $state, $x, $y) = @_; # Lijeva tipka - crta pocetnu tocku ili liniju. if ($button == GLUT_LEFT_BUTTON and $state == GLUT_DOWN){ # Pamti krajnju tocke linije. $Lx[$Ix] = $x; $Ly[$Ix] = $sub_height - $y; $Ix = ($Ix+1)%2; printf("Koordinate tocke %d: %d %d \n", $Ix+1, $x, $y); # Crta prvu tocku ili liniju do druge tocke. if ($Ix == 0) {&myLine($Lx[0], $Ly[0], $Lx[1], $Ly[1]);} #else glVertex2i(x, sub_height-y); glFlush(); } # Desna tipka - brise canvas. elsif ($button == GLUT_RIGHT_BUTTON and $state == GLUT_DOWN) { &myReshape($sub_width, $sub_height); } } #**************************************************** # Tastatura tipke - r, g, b, k - mijenjaju boju. #**************************************************** sub myKeyboard{ my ($theKey, $mouseX, $mouseY) = @_; $theKey eq 'r' and glColor3f(1,0,0); $theKey eq 'g' and glColor3f(0,1,0); $theKey eq 'b' and glColor3f(0,0,1); $theKey eq 'k' and glColor3f(0,0,0); print "tipka $theKey\n"; glRecti($sub_width-15, $sub_height-15, $sub_width, $sub_height); glFlush(); }

Replies are listed 'Best First'.
Re: opengl problem
by bobo (Novice) on Mar 25, 2010 at 23:42 UTC
    after few hours of searching, found out it was problem with ubuntu and it's visual appearance... :(
      if you want to use perl opengl on windows, then you may find this new link usefull:
      http://www.perlmonks.com/?node_id=832081
      you need activestate perl and the win32gui module and the OpenGLFrame-0.02 from cpan, before that you need to install opengl easily for the activestate perl by the command:
      ppm install http://www.bribes.org/perl/ppm/OpenGL.ppd
      from the http://www.bribes.org/perl/ppmdir.html