| 1 | # |
| 2 | # "$Id: glimage.py 146 2005-11-22 12:32:49Z andreasheld $" |
| 3 | # |
| 4 | # Simple gl test program which generates an RGB image from |
| 5 | # scratch and attempts to display it within a GL pane. |
| 6 | # Test program for pyFLTK the Python bindings |
| 7 | # for the Fast Light Tool Kit (FLTK). |
| 8 | # Courtesy of: David McNab |
| 9 | # |
| 10 | # FLTK copyright 1998-1999 by Bill Spitzak and others. |
| 11 | # pyFLTK copyright 2003 by Andreas Held and others. |
| 12 | # |
| 13 | # This library is free software you can redistribute it and/or |
| 14 | # modify it under the terms of the GNU Library General Public |
| 15 | # License, version 2.0 as published by the Free Software Foundation. |
| 16 | # |
| 17 | # This library is distributed in the hope that it will be useful, |
| 18 | # but WITHOUT ANY WARRANTY without even the implied warranty of |
| 19 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 20 | # Library General Public License for more details. |
| 21 | # |
| 22 | # You should have received a copy of the GNU Library General Public |
| 23 | # License along with this library if not, write to the Free Software |
| 24 | # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
| 25 | # USA. |
| 26 | # |
| 27 | # Please report all bugs and problems to "[email protected]". |
| 28 | # |
| 29 | |
| 30 | |
| 31 | import sys |
| 32 | from fltk import * |
| 33 | |
| 34 | class MyWindow(Fl_Window): |
| 35 | """ |
| 36 | main window |
| 37 | """ |
| 38 | def __init__(self, w, h): |
| 39 | |
| 40 | # parent constructor |
| 41 | Fl_Window.__init__(self, w, h, sys.argv[0]) |
| 42 | |
| 43 | # add GL pane |
| 44 | gl = self.gl = GLPane(10, 10, w-20, h-20, "GL Window") |
| 45 | self.resizable(None) |
| 46 | |
| 47 | # gui created |
| 48 | self.end() |
| 49 | |
| 50 | # display this window and the GL pane |
| 51 | self.show(len(sys.argv), sys.argv) |
| 52 | gl.show() |
| 53 | |
| 54 | class GLPane(Fl_Gl_Window): |
| 55 | """ |
| 56 | GL window that hopefully will display an image |
| 57 | """ |
| 58 | def __init__(self, x, y, w, h, l): |
| 59 | |
| 60 | Fl_Gl_Window.__init__(self, x,y,w,h,l) |
| 61 | |
| 62 | # generate an RGB image string |
| 63 | print("Generating RGB image as a string") |
| 64 | |
| 65 | import numpy as np |
| 66 | rgb = np.zeros((h,w,3), dtype=np.uint8) |
| 67 | |
| 68 | # increase red from left to right |
| 69 | rgb[...,0] = np.linspace(0, 256, w, endpoint=False, dtype=np.uint8) |
| 70 | |
| 71 | # increase green from right to left |
| 72 | rgb[...,1] = 255 - rgb[...,0] |
| 73 | |
| 74 | # increase blue from top to bottom |
| 75 | rgb[...,2] = np.linspace(0, 256, h, endpoint=False, dtype=np.uint8)[..., np.newaxis] |
| 76 | |
| 77 | self.rgb = rgb.tobytes() |
| 78 | print("RGB image created") |
| 79 | |
| 80 | def draw(self): |
| 81 | glClear(0x00004000) #GL_COLOR_BUFFER_BIT) |
| 82 | glColor3f(0.5,0.6,0.7) |
| 83 | |
| 84 | # need to set x, y to -1 for drawing to lower left-hand corner |
| 85 | # 0,0 is i n the centre of the canvas |
| 86 | gl_draw_image(self.rgb, -1, -1, self.w(), self.h(), 3) |
| 87 | |
| 88 | |
| 89 | def main(): |
| 90 | w = MyWindow(400, 300) |
| 91 | Fl.run() |
| 92 | |
| 93 | if __name__ == "__main__": |
| 94 | main() |
| 95 | |
| 96 |