#!/usr/bin/env sim Copyright (C) 2009 by Dipl.-Ing. Michael Niederle This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License, version 2. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. For details of the GNU General Public License see http://www.gnu.org/copyleft/gpl.html or write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. writeln "Rotating Cube" declare $$window_width $$window_height $$angle 0 $ambient_colour float_array(0.1 0.1 0.1 1) $light_0_colour float_array(0.5 0.5 0.5 1) $light_0_position float_array(4 10 0 1) $init_scene: glEnable GL'DEPTH_TEST glEnable GL'COLOR_MATERIAL glClearColor 0.2 0.2 0.2 1 # dark gray background glEnable GL'LIGHTING glEnable GL'LIGHT0 glLightfv GL'LIGHT0 GL'DIFFUSE light_0_colour glLightfv GL'LIGHT0 GL'POSITION light_0_position glEnable GL'NORMALIZE $draw_scene: glViewport 0 0 window_width window_height glMatrixMode GL'PROJECTION # switch to setting the camera perspective glLoadIdentity # reset the camera gluPerspective 45 # the camera angle window_width/window_height # the width-to-height ratio 1 # the near z clipping coordinate 200 # the far z clipping coordinate glClear GL'COLOR_BUFFER_BIT | GL'DEPTH_BUFFER_BIT glLightModelfv GL'LIGHT_MODEL_AMBIENT ambient_colour glMatrixMode GL'MODELVIEW # switch to the drawing perspective glLoadIdentity glTranslatef 0 0 -3 glRotatef angle 0 0 1 glRotatef angle 0 1 0 glRotatef angle 1 0 0 glColor3f 1 0 1 draw_cuboid -0.5 -0.5 -0.5 1 1 1 !X11_display XOpenDisplay(NULL) if X11_display->address() == 0: raise "Connection to X-Server failed!" !X11_screen XDefaultScreen() $root XDefaultRootWindow() $attributes int_array(GLX'RGBA GLX'DEPTH_SIZE 24 GLX'DOUBLEBUFFER X'None) $$vi glXChooseVisual(X11_screen attributes) if vi->address() == 0: raise "Connection to X-Server failed!" $colormap root->create_colormap(vi'visual X'AllocNone) $$set_window_attributes allocate_memory('XSetWindowAttributes) !set_window_attributes'colormap colormap $wanted_events X'ExposureMask | X'KeyPressMask | X'ButtonPressMask !set_window_attributes'event_mask wanted_events $win root->create 0 0 600 600 0 vi'depth X'InputOutput vi'visual X'CWColormap | X'CWEventMask set_window_attributes win->store_name "GLX Demo" win->map $glx_context glXCreateContext(vi NULL GL'TRUE) glXMakeCurrent win glx_context init_scene $event allocate_memory('XEvent) $window_attributes allocate_memory('XWindowAttributes) forever: if event->check_mask(wanted_events): case event'type X'Expose: if event'xexpose'count == 0: win->get_attributes window_attributes !window_width window_attributes'width !window_height window_attributes'height draw_scene glXSwapBuffers win X'KeyPress | X'ButtonPress: glXMakeCurrent X'None NULL glXDestroyContext glx_context win->destroy XCloseDisplay terminate : $t1 current_time() ifdef window_width: # ready to draw? if angle=>inc() >= 360: angle=>dec 360 draw_scene glXSwapBuffers win $t2 current_time() $dt t2-t1 # display a maximum of 20 frames per second if dt < 0.05: sleep 0.05-dt