Author Topic: Basic OpenGL  (Read 2197 times)

JRS

  • Guest
Basic OpenGL
« on: November 27, 2010, 11:33:10 PM »
I've been following Charles's OxygenBasic for Windows [JIT] Basic compiler and his recent direction with embracing OpenGL. When I moved my desktop to Linux I thought I would have wait till Charles got around to a Linux version of the compiler. On a whim, I tried O2h under Linux using Wine and all the examples and IDE worked fine.

Peter (Bacon and Gtk-Server author) created a OpenGL demo in ScriptBasic (before the birth of BaCon) with his Gtk-Server interface. (GtkGlExt & GLUT libraries required) I first tried it under Windows in 2008 and thought I would give a spin under Linux on Ubuntu.

Code: Text
  1. #!/usr/bin/scriba
  2. REM
  3. REM Demo with gtkglext - May - August 2008, (c) PvE.
  4. REM
  5. REM Run with the free Scriptbasic interpreter from http://www.scriptbasic.com
  6. REM
  7. REM ---------------------------------------------------------------------
  8. REM Some constants
  9. REM ---------------------------------------------------------------------
  10.  
  11. REM Get GTK definitions
  12. IMPORT gtk.bas
  13.  
  14. ESCAPE = 65307
  15.  
  16. REM These hex values are retrieved from the GL header files
  17. GL_DEPTH_BUFFER_BIT=0x100
  18. GL_COLOR_BUFFER_BIT=0x4000
  19.  
  20. GL_LIGHT0=0x4000
  21. GL_MODELVIEW=0x1700
  22. GL_POSITION=0x1203
  23. GL_AMBIENT=0x1200
  24. GL_DIFFUSE=0x1201
  25. GL_SPECULAR=0x1202
  26. GL_SMOOTH=0x1D01
  27. GL_DEPTH_TEST=0x0B71
  28. GL_LIGHTING=0x0B50
  29. GL_FRONT=0x0404
  30. GL_AMBIENT_AND_DIFFUSE=0x1602
  31.  
  32. REM These are from the GtkGlExt enumerations
  33. GDK_GL_RGBA_TYPE=0
  34. GDK_GL_MODE_RGB=0
  35. GDK_GL_MODE_DOUBLE=2
  36. GDK_GL_MODE_DEPTH=16
  37.  
  38. REM ---------------------------------------------------------------------
  39. REM Draw bitmapped text on the screen, character by character
  40. REM ---------------------------------------------------------------------
  41.  
  42. FUNCTION bitmap_text(txt$)
  43.  
  44. FOR idx = 1 TO LEN(txt$)
  45.     glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ASC(MID(txt$, idx, 1)))
  46. NEXT idx
  47.  
  48. END FUNCTION
  49.  
  50. REM ---------------------------------------------------------------------
  51. REM Draw stroked text on the screen, character by character
  52. REM ---------------------------------------------------------------------
  53.  
  54. FUNCTION stroke_text(txt$)
  55.  
  56. FOR idx = 1 TO LEN(txt$)
  57.     glutStrokeCharacter(GLUT_STROKE_ROMAN, ASC(MID(txt$, idx, 1)))
  58. NEXT idx
  59.  
  60. END FUNCTION
  61.  
  62. REM ---------------------------------------------------------------------
  63. REM Draw and expose the picture
  64. REM
  65. REM Note: colors in OpenGL are mostly defined as an array of floats.
  66. REM Such an array cannot be passed to the GTK-server, therefore we pass
  67. REM a binary structure which is Base64 encoded. To create a Base64
  68. REM encoded float array structure, I used the following:
  69. REM
  70. REM newlisp -e '(base64-enc (pack "fff" 1 1 0))'
  71. REM
  72. REM In this example the float array {1.0 1.0 0.0} is shown in Base64
  73. REM and can be passed to the GTK-server. Of course the datatype for the
  74. REM argument in the GL-function in the configfile should be set to
  75. REM BASE64 also.
  76. REM
  77. REM NewLisp can be obtained for free from http://www.newlisp.org
  78. REM---------------------------------------------------------------------
  79.  
  80. FUNCTION expose
  81.  
  82. REM Setup the drawing area
  83. GLCONTEXT = gtk_widget_get_gl_context(DA)
  84. GLDRAWABLE = gtk_widget_get_gl_window(DA)
  85. gdk_gl_drawable_gl_begin(GLDRAWABLE, GLCONTEXT)
  86.  
  87. REM Define clearing color
  88. glClearColor(0.5, 1, 1, 0)
  89. REM Clear screen
  90. FLAG = GL_COLOR_BUFFER_BIT OR GL_DEPTH_BUFFER_BIT
  91. glClear(FLAG)
  92. REM Enable shading, depth and lighting
  93. glShadeModel(GL_SMOOTH)
  94. glEnable(GL_DEPTH_TEST)
  95. glEnable(GL_LIGHTING)
  96. glEnable(GL_LIGHT0)
  97. REM Setup lighting
  98. glLightfv(GL_LIGHT0, GL_POSITION, "AAAAQAAAAEAAAADBAAAAAA==")
  99. glLightfv(GL_LIGHT0, GL_DIFFUSE, "AACAPwAAgD8AAIA/AACAPw==")
  100. glLightfv(GL_LIGHT0, GL_AMBIENT, "mpkZPpqZGT6amRk+")
  101. glLightfv(GL_LIGHT0, GL_SPECULAR, "AACAPwAAgD8AAIA/AACAPw==")
  102. REM Setup reflected color of object
  103. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, "zczMPTMzMz/NzMw9AAAAPw==")
  104. REM Make sure we see the model
  105. glMatrixMode(GL_MODELVIEW)
  106. REM Save current matrix
  107. glPushMatrix()
  108. REM Rotate
  109. glRotatef(ROTX, 0, 1, 0)
  110. glRotatef(ROTY, 1, 0, 0)
  111. REM Dump rotated image
  112. glutSolidTeapot(SIZE)
  113. REM Undo the last rotation
  114. glLoadIdentity()
  115. REM Setup reflected color of font
  116. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, "AACAP83MzD4AAIA/AAAAAA==")
  117. REM Determine position of bitmapped text
  118. glRasterPos2f(0, -0.8)
  119. REM Draw some bitmapped text
  120. bitmap_text("OpenGL demo with Scriptbasic")
  121. REM Setup reflected color of font
  122. glMaterialfv(GL_FRONT, GL_AMBIENT_AND_DIFFUSE, "AAAAAAAAAAAAAIA/AAAAAA==")
  123. REM Determine position of STROKED text -> drawed so translate
  124. glTranslatef(-0.9, 0.8, 0.0)
  125. REM Setup scaling -> stroked characters are large, make smaller
  126. glScalef(0.0005, 0.0006, 0)
  127. REM Draw some stroked text
  128. stroke_text("Using GTK-server with GtkGlExt!")
  129. REM Now put back the matrix
  130. glPopMatrix()
  131.  
  132. REM Now swap buffers and draw
  133. gdk_gl_drawable_swap_buffers(GLDRAWABLE)
  134. gdk_gl_drawable_gl_end(GLDRAWABLE)
  135.  
  136. END FUNCTION
  137.  
  138. REM ---------------------------------------------------------------------
  139. REM Main program
  140. REM ---------------------------------------------------------------------
  141. REM gtk_server_cfg("-log=/tmp/log.txt")
  142.  
  143. REM Check availability of GtkGlExt and GLUT
  144. AVAIL = gtk_server_require("libgtkglext-x11-1.0.so")
  145. IF AVAIL <> "ok" THEN
  146.     PRINT "Install the GtkGlExt libraries from gtkglext.sourceforge.net first, and run this demo again.\n"
  147.     gtk_server_exit()
  148.     END
  149. END IF
  150. AVAIL = gtk_server_require("libglut.so.3")
  151. IF AVAIL <> "ok" THEN
  152.     PRINT "Install the GLUT libraries from freeglut.sourceforge.net first, and run this demo again.\n"
  153.     gtk_server_exit()
  154.     END
  155. END IF
  156.  
  157. REM Initialize libs
  158. gtk_init("NULL", "NULL")
  159. gtk_gl_init("NULL", "NULL")
  160. glutInit(1, "' '")
  161.  
  162. REM Window
  163. win = gtk_window_new(0)
  164. gtk_window_set_default_size(win, 640, 480)
  165. gtk_window_set_title(win, "This is a teapot demo with ScriptBasic")
  166. gtk_window_set_position(win, 1)
  167. REM Signal every 100 msecs
  168. gtk_server_connect(win, "show", "idle")
  169. gtk_server_timeout(75, win, "show")
  170. gtk_server_connect(win, "key-press-event", "key-press-event")
  171. REM Drawing area
  172. DA = gtk_drawing_area_new()
  173. gtk_container_add(win, DA)
  174.  
  175. FLAG = GDK_GL_MODE_RGB OR GDK_GL_MODE_DOUBLE OR GDK_GL_MODE_DEPTH
  176. GLCONFIG = gdk_gl_config_new_by_mode(FLAG)
  177. gtk_widget_set_gl_capability(DA, GLCONFIG, "NULL", 1, GDK_GL_RGBA_TYPE)
  178. gtk_server_connect(DA, "expose-event", "expose")
  179. gtk_widget_show_all(win)
  180.  
  181. REM Initialize variables
  182. EVENT=0
  183. ROTX=0
  184. ROTY=330
  185. REM size of teapot
  186. SIZE=0.5
  187.  
  188. GLUT_BITMAP_HELVETICA_18 = glutBitmapHelvetica18()
  189. GLUT_STROKE_ROMAN = glutStrokeRoman()
  190.  
  191. REM Mainloop
  192. REPEAT
  193.  
  194.     EVENT = gtk_server_callback("wait")
  195.  
  196.     REM Rotate
  197.     ROTX = ROTX + 3
  198.     ROTY = ROTY + 2
  199.     IF ROTX > 359 THEN
  200.         ROTX=0
  201.     END IF
  202.     IF ROTX < 0 THEN
  203.         ROTX=360
  204.     END IF
  205.     IF ROTY > 359 THEN
  206.         ROTY=0
  207.     END IF
  208.     IF ROTY < 0 THEN
  209.         ROTY=360
  210.     END IF
  211.  
  212.     REM Check events
  213.     IF EVENT = "idle" THEN
  214.         expose()
  215.     END IF
  216.  
  217.     IF EVENT = "expose" THEN
  218.         expose()
  219.     END IF
  220.  
  221.     IF EVENT = "key-press-event" THEN
  222.         keyb = gtk_server_key()
  223.     END IF
  224.  
  225. UNTIL EVENT = win OR keyb = ESCAPE
  226.  
  227. REM Exit GTK
  228. gtk_server_exit()
  229.  

« Last Edit: November 28, 2010, 04:38:37 PM by JRS »