use strict; use warnings; use Win32::GUI qw(WS_CLIPCHILDREN); use Win32::GUI::OpenGLFrame qw(w32gSwapBuffers); use OpenGL qw(:all); my $flag = 0; my $axrng = 10.0; my $x; my $y; my $r; my $w; my $h; our $glist=[]; #to store all vertexes in a list to easy on the cpu my $mw = Win32::GUI::Window->new( -title => "OpenGL Demonstration MathArt", -pos => [0,0], -size => [640,480], -pushstyle => WS_CLIPCHILDREN, # stop flickering on resize -onResize => \&mainWinResize, ); my $glw = $mw->AddOpenGLFrame( -name => 'oglwin', -width => $mw->ScaleWidth()-240, -height => $mw->ScaleHeight() - 80, -pos => [100,0], -display => \&display, -init => \&Init, -reshape => \&reshape, -doubleBuffer => 0, -visible => 1, ); $mw->AddButton( -name => 'button1', -text => 'Draw', -left => $mw->ScaleWidth()-100, -top => $mw->ScaleHeight()-30, ); $mw->AddButton( -name => 'button3', -text => 'Exit', -left => $mw->ScaleWidth()-220, -top => $mw->ScaleHeight()-30, -onClick => sub{undef($mw);exit(0)}, ); $mw->Show(); while(Win32::GUI::DoEvents() != -1) { $mw->oglwin->InvalidateRect(0); } sub mainWinResize { my $win = shift; $win->oglwin->Resize($win->ScaleWidth()-240, $win->ScaleHeight()-80); $win->button1->Move($win->ScaleWidth()-100, $win->ScaleHeight()-30); $win->button3->Move($win->ScaleWidth()-220, $win->ScaleHeight()-30); $flag = 1; glClearColor(1.0,1.0,1.0,1.0); glClear(GL_COLOR_BUFFER_BIT); plotmathart(); return 0; } sub reshape { #my ($w, $h) = @_; my $width=$mw->ScaleWidth()-240; my $height=$mw->ScaleHeight()-80; glViewport(0, 0, $width, $height); glMatrixMode (GL_PROJECTION); glLoadIdentity (); # define the projection #gluOrtho2D(left, right, bottom, top) gluOrtho2D(-$axrng, $axrng, -$axrng*$height/$width, $axrng*$height/ +$width); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return 0; } sub Init { glClearColor(0.0, 0.0, 0.0, 0.0); glClear(GL_COLOR_BUFFER_BIT); glFlush(); w32gSwapBuffers(); } sub display { if ($flag == 1) #ie you have clicked the Draw button # and the scene stored in the glist, so it just calling that glist { plotmathart(); glFlush(); w32gSwapBuffers();return 1; } return 1; } sub plotmathart { glPushMatrix(); glCallList($glist); glPopMatrix(); } sub plot { glPushMatrix(); #save current matrix glClearColor(1.0, 1.0, 1.0, 1.0); # make the glist $glist = glGenLists(1); glNewList($glist, GL_COMPILE_AND_EXECUTE); glLoadIdentity(); for($x=-$axrng; $x <= $axrng; $x += 0.03) { for($y=-$axrng; $y <= $axrng; $y += 0.03) { $r = cos($x) + sin($y); glColor3f(cos($y*$r), cos($x*$y*$r), sin($r*$x)); glBegin(GL_POINTS); glVertex2f($x, $y); glEnd; } } glEndList(); glPopMatrix(); #restore matrix #glFlush(); #w32gSwapBuffers(); return 1; } sub button1_Click { $flag = 1;plot(); }
use strict; use warnings; use Win32::GUI qw(WS_CLIPCHILDREN); use Win32::GUI::OpenGLFrame qw(w32gSwapBuffers); use OpenGL qw(:all); my $flag = 0; my $width = 500 ; my $height = 500; my $axrng = 10.0; my $x; my $y; my $r; my $w; my $h; my $glist; #to store all vertexes in a list to easy on the cpu my $mw = Win32::GUI::Window->new( -title => "OpenGL Demonstration MathArt", -pos => [0,0], -size => [640,480], -pushstyle => WS_CLIPCHILDREN, # stop flickering on resize -onResize => \&mainWinResize, ); my $glw = $mw->AddOpenGLFrame( -name => 'oglwin', -width => $mw->ScaleWidth(), -height => $mw->ScaleHeight() - 50, -display => \&display, -init => \&Init, -reshape => \&reshape, -doubleBuffer => 1, ); $mw->AddButton( -name => 'button1', -text => 'Draw', -left => $mw->ScaleWidth()-100, -top => $mw->ScaleHeight()-30, ); $mw->AddButton( -name => 'button3', -text => 'Exit', -left => $mw->ScaleWidth()-220, -top => $mw->ScaleHeight()-30, -onClick => sub{undef($mw);exit(0)}, ); $mw->Show(); while(Win32::GUI::DoEvents() != -1) { $mw->oglwin->InvalidateRect(0); } sub mainWinResize { my $win = shift; $win->oglwin->Resize($win->ScaleWidth(), $win->ScaleHeight()-50); $win->button1->Move($win->ScaleWidth()-100, $win->ScaleHeight()-30); $win->button3->Move($win->ScaleWidth()-220, $win->ScaleHeight()-30); return 0; } sub reshape { my ($w, $h) = @_; glViewport(0, 0, $w, $h); glMatrixMode (GL_PROJECTION); glLoadIdentity (); # define the projection gluPerspective(45.0, $h ? $w/$h : 0, 1.0, 20.0); glMatrixMode(GL_MODELVIEW); glLoadIdentity(); return 0; } sub Init { glClearColor(1.0, 1.0, 1.0, 1.0); } sub display { if ($flag == 1) #ie you have click Draw button { plotmathart(); } glFlush(); w32gSwapBuffers(); return 1; } sub plotmathart { glPushMatrix(); glCallList($glist); glPopMatrix(); } sub plot { glPushMatrix(); #save current matrix glClearColor(1.0, 1.0, 1.0, 1.0); # make the glist $glist = glGenLists(1); glNewList($glist, GL_COMPILE); glLoadIdentity(); glTranslatef(0.0, 0.0, -20.0); for($x=-$axrng; $x <= $axrng; $x += 0.03) { for($y=-$axrng; $y <= $axrng; $y += 0.03) { $r = cos($x) + sin($y); glColor3f(cos($y*$r), cos($x*$y*$r), sin($r*$x)); glBegin(GL_POINTS); glVertex2f($x, $y); glEnd; } } glEndList(); glPopMatrix(); #restore matrix return 1; } sub button1_Click { $flag = 1;plot(); }
In reply to 2D draw using opengl by orange
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |