From: peter <my....@gm...> - 2009-09-11 10:35:35
|
Hi here is another fun example on using the Win32-GUI-OpenGLFrame-0.02 attached a file demostrate the new package capabilities, it is drawing a moving spider, a sea monster, a cube and a triangle, every one entity appears upon clicking it's own button, there is a slider to speed the movement of the spider. here is a picture showing the program: http://img34.imageshack.us/img34/3226/win32guiopengl.jpg you can download the program perl script from here: http://rapidshare.com/files/278504269/Win32-GUI-OpenGLFrame.pl you can install opengl for windows for active state perl 5.10 from here: http://www.bribes.org/perl/ppmdir.html and for installation: ppm install http://www.bribes.org/perl/ppm/OpenGL.ppd the opengl code in the example in not the optimal one , you can recode it in a thousand way, and it needs many corrections, but it is usefull and fun , i hope the math and science teachers to see that the design can have many 3D figures for the benefits of the students, also the biology teachers can design all sorts of moving insects , creatures ... . i hope if rob may can provide a ppm for the Win32-GUI-OpenGLFrame-0.02 , since it is until now not available on activestate or the other ppm providers sites ,and this is much more convenient to install for the most windows users. regards peter #!perl -w use strict; use warnings; use Win32::GUI qw(WS_CLIPCHILDREN); use Win32::GUI::OpenGLFrame qw(w32gSwapBuffers); use OpenGL qw(:all); my $spin = 0.0; my $xrot = 0; my $yrot = 0; my $rtri = 0.0; my $rtcube = 0.0; my $objectXRot = 0.0; my $objectYRot = 0.0; my $objectZRot = 0.0; my $flag = 3; our $rot=0; our $speed = 0.1; our $spinSpeed = 0.1; my $mw = Win32::GUI::Window->new( -title => "Win32-GUI-OpenGLFrame-0.02 Demonstration", -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, ); my $s1 = $mw->AddSlider ( -width => 300, -height => 40, -top => $mw->ScaleHeight()-30, -left => 10, -onScroll => \&slider, ); $s1->Pos(10); $mw->AddLabel( -text => "monster speed", -name => "Label_1", -left => 75, -top => $mw->ScaleHeight()-45, -width => 79, -height => 19, -foreground => 0, ); $mw->AddButton( -name => 'button1', -text => 'Triangle', -left => $mw->ScaleWidth()- 100, -top => $mw->ScaleHeight()-30, #-onClick => sub{-1}, ); $mw->AddButton( -name => 'button2', -text => 'Cube', -left => $mw->ScaleWidth()-140, -top => $mw->ScaleHeight()-30, ); $mw->AddButton( -name => 'button3', -text => 'Spider', -left => $mw->ScaleWidth()-300, -top => $mw->ScaleHeight()-30, ); $mw->AddButton( -name => 'button4', -text => 'sea monster', -left => $mw->ScaleWidth()-245, -top => $mw->ScaleHeight()-30, ); $mw->Show(); while(Win32::GUI::DoEvents() != -1) { $mw->oglwin->InvalidateRect(0); } #Win32::GUI::Dialog(); $mw->Hide(); exit(0); sub mainWinResize { my $win = shift; $win->oglwin->Resize($win->ScaleWidth(), $win->ScaleHeight()-50); $win->button1->Move($win->ScaleWidth()-100, $win->ScaleHeight()-30); $win->button2->Move($win->ScaleWidth()-160, $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 { glShadeModel(GL_SMOOTH); glClearColor(0, 0, 0, 0); glClearDepth(1); glEnable(GL_DEPTH_TEST); glDepthFunc(GL_LEQUAL); glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); glEnable(GL_DEPTH_TEST); #glEnable(GL_LIGHTING); glEnable(GL_LIGHT0); glMatrixMode(GL_PROJECTION); glMatrixMode(GL_MODELVIEW); return 1; } sub display { glPushMatrix(); #save current matrix if ($flag == 1){ glDisable(GL_LIGHTING );DrawTriangle();} #draw triangle elsif ($flag == 2){ glDisable(GL_LIGHTING );DrawCube();}; #draw cube if ($flag == 3) { glEnable(GL_LIGHTING); spider(); } #draw spider elsif ($flag == 4){ glEnable(GL_LIGHTING); Sea_Monster(); } glPopMatrix(); #restore matrix glFlush(); w32gSwapBuffers(); return 1; } sub DrawTriangle { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); glTranslatef(0.0, 0.0, -6.0); glRotatef($rtri, 0, 1, 0); my $size = 1; glScalef($size,$size,$size); glBegin(GL_TRIANGLES); glColor3f(1.0, 0.0, 0.0); glVertex3f(0.0, 1.0, 0.0); glColor3f(0.0, 1.0, 0.0); glVertex3f(-1.0, -1.0, 0.0); glColor3f(0.0, 0.0, 1.0); glVertex3f(1.0, -1.0, 0.0); glEnd(); $rtri += 2; } sub DrawCube { glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT); glLoadIdentity(); my $size = 1; glTranslatef(0.0, 0.0, -6.0); glRotatef($objectXRot, 1.0, 0.0, 0.0); glRotatef($objectYRot, 0.0, 1.0, 0.0); glRotatef($objectZRot, 0.0, 0.0, 1.0); glScalef($size,$size,$size); glBegin(GL_QUADS); # bottom face glColor3f(0.0,0.0,0.0); #black glVertex3f(-1.0,-1.0,-1.0); glColor3f(1.0,0.0,0.0); #red glVertex3f(1.0,-1.0,-1.0); glColor3f(1.0,0.0,1.0); #magenta glVertex3f(1.0,-1.0,1.0); glColor3f(0.0,0.0,1.0); #blue glVertex3f(-1.0,-1.0,1.0); # front face glColor3f(0.0,0.0,0.0); #black glVertex3f(-1.0,-1.0,-1.0); glColor3f(0.0,1.0,0.0); #green glVertex3f(-1.0,1.0,-1.0); glColor3f(1.0,1.0,0.0); #yellow glVertex3f(1.0,1.0,-1.0); glColor3f(1.0,0.0,0.0); #red glVertex3f(1.0,-1.0,-1.0); # right face glColor3f(0.0,0.0,0.0); #black glVertex3f(-1.0, -1.0, -1.0); glColor3f(0.0,0.0,1.0); #blue glVertex3f(-1.0,-1.0,1.0); glColor3f(0.0,1.0,1.0); #cyan glVertex3f(-1.0,1.0,1.0); glColor3f(0.0,1.0,0.0); #green glVertex3f(-1.0,1.0,-1.0); # left face glColor3f(1.0,1.0,1.0); #white glVertex3f(1.0,1.0,1.0); glColor3f(1.0,0.0,1.0); #magenta glVertex3f(1.0,-1.0,1.0); glColor3f(1.0,0.0,0.0); #red glVertex3f(1.0,-1.0,-1.0); glColor3f(1.0,1.0,0.0); #yellow glVertex3f(1.0,1.0,-1.0); # top face glColor3f(1.0,1.0,1.0); #white glVertex3f(1.0,1.0,1.0); glColor3f(1.0,1.0,0.0); #yelllow glVertex3f(1.0,1.0,-1.0); glColor3f(0.0,1.0,0.0); #green glVertex3f(-1.0,1.0,-1.0); glColor3f(0.0,1.0,1.0); #cyan glVertex3f(-1.0,1.0,1.0); # back face glColor3f(1.0,1.0,1.0); #white glVertex3f(1.0,1.0,1.0); glColor3f(0.0,1.0,1.0); #cyan glVertex3f(-1.0,1.0,1.0); glColor3f(0.0,0.0,1.0); #blue glVertex3f(-1.0,-1.0,1.0); glColor3f(1.0,0.0,1.0); #magenta glVertex3f(1.0,-1.0,1.0); glEnd(); $objectXRot += 0.5; $objectYRot += 0.5; $objectZRot += 0.5; } sub spider { my $i=0; my @light0_position = (2.0, -5.0, 4.0, 0.0); my @mat_specular = (1.0, 1.0, 1.0, 1.0); my @mat_shininess = (50.0); my @mat_amb_diff_color = (1.0, 0.5, 0.0, 0.5); my @mat_amb_diff_color2 = (1.0, 1.0, 0.0, 0.5); my @light_diffuse = (1.0, 1.0, 1.0, 1.0); my @light_ambient = (0.15, 0.15, 0.15, 0.15); my @light_specular = (1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLightfv_p(GL_LIGHT0, GL_POSITION, @light0_position); glLightfv_p(GL_LIGHT0, GL_DIFFUSE, @light_diffuse); glLightfv_p(GL_LIGHT0, GL_AMBIENT, @light_ambient); glLightfv_p(GL_LIGHT0, GL_SPECULAR, @light_specular); glLoadIdentity(); gluLookAt(0.0, 3.0, -10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); $spin += $spinSpeed; $spin = $spin - 360.0 if ($spin >360.0); glRotatef($spin, 0.0, 1.0, 0.0); $rot=$rot+$speed; # the frequency of the legs movement my $quad = OpenGL::gluNewQuadric(); glMaterialfv_p(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, @mat_amb_diff_color2); gluQuadricDrawStyle($quad, GLU_FILL); glPushMatrix(); glTranslatef(-0.5, 1.5, -4.0);#to rotate the spider around a circle glRotatef( $spin, 0.0, 1.0, 0.0 );#........................ glPushMatrix(); glTranslatef(0.0, 0.5, 0.0); gluSphere( $quad , 0.8 , 20 , 20 ); glPopMatrix(); glMaterialfv_p(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, @mat_amb_diff_color); #drawing 8 Legs, every Leg has 3 parts: for ($i = 0; $i<=359; $i=$i+45) { #drawing Leg's part 1: glPushMatrix(); glRotatef( $i, 0.0, 1.0, 0.0 ); glRotatef( 0+sin($rot+$i)*20, 0.0, 1.0, 0.0 ); glPushMatrix(); glRotatef(-90, 0.0, 1.0, 0.0); gluQuadricDrawStyle($quad, GLU_FILL); gluQuadricOrientation($quad, GLU_INSIDE); #Other draw steel choises below: #gluQuadricDrawStyle($quad, GLU_POINT); #gluQuadricDrawStyle($quad, GLU_SILHOUETTE); #gluQuadricDrawStyle($quad, GLU_FILL); #gluQuadricDrawStyle($quad, GLU_LINE); gluCylinder($quad, 0.2, 0.15, 2, 16, 10); glPopMatrix(); #drawing a smaller Leg's part 2: glTranslatef(-2.0, 0.0, 0.0); glRotatef(30, 0.0, 0.0, 1.0); glPushMatrix(); glRotatef(-90, 0.0, 1.0, 0.0); gluCylinder($quad, 0.15, 0.1, 1.5, 16, 10); glPopMatrix(); #drawing the smallest Leg's part 3: glTranslatef(-1.5, 0.0, 0.0); glRotatef(30, 0.0, 0.0, 1.0); glPushMatrix(); glRotatef(-90, 0.0, 1.0, 0.0); gluCylinder($quad, 0.1, 0.05, 1.0, 16, 10); glPopMatrix(); glPopMatrix(); } glPopMatrix(); } sub Sea_Monster { my $i=0; my @light0_position = (2.0, -5.0, 4.0, 0.0); my @mat_specular = (1.0, 1.0, 1.0, 1.0); my @mat_shininess = (50.0); my @mat_amb_diff_color = (1.0, 0.5, 0.0, 0.5); my @mat_amb_diff_color2 = (1.0, 1.0, 0.0, 0.5); my @light_diffuse = (1.0, 1.0, 1.0, 1.0); my @light_ambient = (0.15, 0.15, 0.15, 0.15); my @light_specular = (1.0, 1.0, 1.0, 1.0); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glLightfv_p(GL_LIGHT0, GL_POSITION, @light0_position); glLightfv_p(GL_LIGHT0, GL_DIFFUSE, @light_diffuse); glLightfv_p(GL_LIGHT0, GL_AMBIENT, @light_ambient); glLightfv_p(GL_LIGHT0, GL_SPECULAR, @light_specular); glLoadIdentity(); gluLookAt(0.0, 3.0, 10.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0); $spin += $spinSpeed; $spin = $spin - 360.0 if ($spin >360.0); glRotatef($spin, 0.0, 1.0, 0.0); $rot=$rot+$speed; # the frequency of the legs movement my $quad = OpenGL::gluNewQuadric(); glMaterialfv_p(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, @mat_amb_diff_color2); gluQuadricDrawStyle($quad, GLU_FILL); glPushMatrix(); glTranslatef(0.0, 0.6, 0.0); gluSphere( $quad , 0.8 , 20 , 20 ); glPopMatrix(); glMaterialfv_p(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, @mat_amb_diff_color); #drawing 8 Legs, every Leg has 3 parts: for ($i = 0; $i<=359; $i=$i+45) { #drawing Leg's part 1: glPushMatrix(); glRotatef( $i, 0.0, 1.0, 0.0 ); glPushMatrix(); glRotatef(-90, 0.0, 1.0, 0.0); gluQuadricDrawStyle($quad, GLU_FILL); gluQuadricOrientation($quad, GLU_INSIDE); #Other draw steel choises below: #gluQuadricDrawStyle($quad, GLU_POINT); #gluQuadricDrawStyle($quad, GLU_SILHOUETTE); #gluQuadricDrawStyle($quad, GLU_FILL); #gluQuadricDrawStyle($quad, GLU_LINE); gluCylinder($quad, 0.2, 0.15, 2, 16, 10); glPopMatrix(); glRotatef( 70+sin($rot+$i)*20, 1.0, 0.0, 0.0 ); #drawing a smaller Leg's part 2: glTranslatef(-2.0, 0.0, 0.0); glRotatef(30, 0.0, 0.0, 1.0); glPushMatrix(); glRotatef(-90, 0.0, 1.0, 0.0); gluCylinder($quad, 0.15, 0.1, 1.5, 16, 10); glPopMatrix(); #drawing the smallest Leg's part 3: glTranslatef(-1.5, 0.0, 0.0); glRotatef(30, 0.0, 0.0, 1.0); glPushMatrix(); glRotatef(-90, 0.0, 1.0, 0.0); gluCylinder($quad, 0.1, 0.05, 1.0, 16, 10); glPopMatrix(); glPopMatrix(); } #glRotatef(90, 1.0, 0.0, 0.0); #glutSwapBuffers(); } sub button1_Click { #to call the DrawTriangle() sub $flag = 1; } sub button2_Click { #to call the DrawCube() sub $flag = 2; } sub button3_Click { #to call the DrawMonster() sub $flag = 3; } sub button4_Click { #to call the DrawMonster() sub $flag = 4; } sub slider() { my $sliderval = $s1->GetPos(); # to determine the vibrational speed of the Legs: $speed = $sliderval /100; $spinSpeed = $speed; #rotational speed of the monster } |