perl-opengl-直线绘制
程序员文章站
2022-04-29 13:22:44
...
#!/usr/bin/perl -w use strict; use warnings; use OpenGL qw/ :all /; use OpenGL::Config; glutInit(); glutInitDisplayMode(GLUT_RGB|GLUT_SINGLE); glutInitWindowPosition(100,100); glutInitWindowSize(400,400); glutCreateWindow("my OpenGL program"); glClearColor(0,0,0,255); glClear(GL_COLOR_BUFFER_BIT); glMatrixMode(GL_PROJECTION); gluOrtho2D(-100,100,-100,100); glutDisplayFunc(\&mydis); glutMainLoop(); return 0; sub mydis() { my $x0=-100; my $y0=-85; my $xend=93; my $yend=26; my $dx=abs($xend-$x0); my $dy=abs($yend-$y0); my $p=2*$dy-$dx; my $twoDy=2*$dy; my $twoDyMinusDx=2*($dy-$dx); my $x; my $y; glPointSize(1); glColor3f(255,0,255); if ($x0>$xend) { $x=$xend; $y=$yend; $xend=$x0; } else { $x=$x0; $y=$y0; } glBegin(GL_POINTS); glVertex2f($x,$y); glEnd(); glFlush(); for (;$x<$xend;$x++) { if ($p<0) { $p+=$twoDy; } else { $y++; $p+=$twoDyMinusDx; } glBegin(GL_POINTS); glVertex2f($x,$y); glEnd(); } glFlush(); glColor3f(0,255,0); glBegin(GL_LINES); glVertex2f(-100,0); glVertex2f(100,0); glEnd(); glBegin(GL_LINES); glVertex2f(0,-100); glVertex2f(0,100); glEnd(); glFlush(); }
下一篇: perl-opengl键盘事件与色彩