ruby-glfw 0.9

Sign up to get free protection for your applications and to get access to all the features.
Files changed (206) hide show
  1. data/README +1 -0
  2. data/README.API +73 -0
  3. data/Rakefile +120 -0
  4. data/examples/boing.rb +519 -0
  5. data/examples/gears.rb +327 -0
  6. data/examples/keytest.rb +117 -0
  7. data/examples/listmodes.rb +20 -0
  8. data/examples/mipmaps.rb +104 -0
  9. data/examples/mipmaps.tga +0 -0
  10. data/examples/particles.rb +837 -0
  11. data/examples/pong3d.rb +741 -0
  12. data/examples/pong3d_field.tga +0 -0
  13. data/examples/pong3d_instr.tga +0 -0
  14. data/examples/pong3d_menu.tga +0 -0
  15. data/examples/pong3d_title.tga +0 -0
  16. data/examples/pong3d_winner1.tga +0 -0
  17. data/examples/pong3d_winner2.tga +0 -0
  18. data/examples/splitview.rb +432 -0
  19. data/examples/triangle.rb +89 -0
  20. data/examples/wave.rb +294 -0
  21. data/ext/glfw/glfw.c +1094 -0
  22. data/ext/glfw/mkrf_conf.rb +70 -0
  23. data/glfw-src/Makefile +220 -0
  24. data/glfw-src/compile.ami +61 -0
  25. data/glfw-src/compile.bat +217 -0
  26. data/glfw-src/compile.sh +607 -0
  27. data/glfw-src/docs/Makefile +57 -0
  28. data/glfw-src/docs/Reference.pdf +0 -0
  29. data/glfw-src/docs/UsersGuide.pdf +0 -0
  30. data/glfw-src/docs/cleanup.bat +22 -0
  31. data/glfw-src/docs/glfwdoc.sty +80 -0
  32. data/glfw-src/docs/glfwrm.tex +3034 -0
  33. data/glfw-src/docs/glfwug.tex +2024 -0
  34. data/glfw-src/docs/readme.txt +80 -0
  35. data/glfw-src/examples/Makefile.amigaos.gcc +70 -0
  36. data/glfw-src/examples/Makefile.amigaos.vbcc +70 -0
  37. data/glfw-src/examples/Makefile.dos.djgpp +71 -0
  38. data/glfw-src/examples/Makefile.macosx.gcc +96 -0
  39. data/glfw-src/examples/Makefile.win32.bcc +75 -0
  40. data/glfw-src/examples/Makefile.win32.cross-mgw +79 -0
  41. data/glfw-src/examples/Makefile.win32.cygwin +79 -0
  42. data/glfw-src/examples/Makefile.win32.lcc +74 -0
  43. data/glfw-src/examples/Makefile.win32.mgw +75 -0
  44. data/glfw-src/examples/Makefile.win32.msvc +74 -0
  45. data/glfw-src/examples/Makefile.win32.ow +74 -0
  46. data/glfw-src/examples/Makefile.win32.pellesc +74 -0
  47. data/glfw-src/examples/Makefile.x11.in +54 -0
  48. data/glfw-src/examples/boing.c +606 -0
  49. data/glfw-src/examples/bundle.sh +46 -0
  50. data/glfw-src/examples/gears.c +382 -0
  51. data/glfw-src/examples/keytest.c +264 -0
  52. data/glfw-src/examples/listmodes.c +48 -0
  53. data/glfw-src/examples/mipmaps.c +126 -0
  54. data/glfw-src/examples/mipmaps.tga +0 -0
  55. data/glfw-src/examples/mtbench.c +301 -0
  56. data/glfw-src/examples/mthello.c +48 -0
  57. data/glfw-src/examples/particles.c +1148 -0
  58. data/glfw-src/examples/pong3d.c +839 -0
  59. data/glfw-src/examples/pong3d_field.tga +0 -0
  60. data/glfw-src/examples/pong3d_instr.tga +0 -0
  61. data/glfw-src/examples/pong3d_menu.tga +0 -0
  62. data/glfw-src/examples/pong3d_title.tga +0 -0
  63. data/glfw-src/examples/pong3d_winner1.tga +0 -0
  64. data/glfw-src/examples/pong3d_winner2.tga +0 -0
  65. data/glfw-src/examples/splitview.c +506 -0
  66. data/glfw-src/examples/triangle.c +108 -0
  67. data/glfw-src/examples/wave.c +397 -0
  68. data/glfw-src/images/opengl.gif +0 -0
  69. data/glfw-src/images/osicert.gif +0 -0
  70. data/glfw-src/include/GL/glfw.h +486 -0
  71. data/glfw-src/lib/amigaos/Makefile.amigaos.gcc +128 -0
  72. data/glfw-src/lib/amigaos/Makefile.amigaos.vbcc +128 -0
  73. data/glfw-src/lib/amigaos/SDI_compiler.h +94 -0
  74. data/glfw-src/lib/amigaos/amigaos_enable.c +51 -0
  75. data/glfw-src/lib/amigaos/amigaos_fullscreen.c +319 -0
  76. data/glfw-src/lib/amigaos/amigaos_glext.c +61 -0
  77. data/glfw-src/lib/amigaos/amigaos_init.c +284 -0
  78. data/glfw-src/lib/amigaos/amigaos_joystick.c +359 -0
  79. data/glfw-src/lib/amigaos/amigaos_thread.c +494 -0
  80. data/glfw-src/lib/amigaos/amigaos_time.c +206 -0
  81. data/glfw-src/lib/amigaos/amigaos_window.c +830 -0
  82. data/glfw-src/lib/amigaos/platform.h +337 -0
  83. data/glfw-src/lib/dos/Makefile.dos.djgpp +146 -0
  84. data/glfw-src/lib/dos/dos_enable.c +51 -0
  85. data/glfw-src/lib/dos/dos_events.c +173 -0
  86. data/glfw-src/lib/dos/dos_fullscreen.c +101 -0
  87. data/glfw-src/lib/dos/dos_glext.c +59 -0
  88. data/glfw-src/lib/dos/dos_init.c +105 -0
  89. data/glfw-src/lib/dos/dos_irq.s +246 -0
  90. data/glfw-src/lib/dos/dos_joystick.c +94 -0
  91. data/glfw-src/lib/dos/dos_keyboard.c +694 -0
  92. data/glfw-src/lib/dos/dos_mouse.c +337 -0
  93. data/glfw-src/lib/dos/dos_thread.c +267 -0
  94. data/glfw-src/lib/dos/dos_time.c +309 -0
  95. data/glfw-src/lib/dos/dos_window.c +563 -0
  96. data/glfw-src/lib/dos/platform.h +341 -0
  97. data/glfw-src/lib/enable.c +295 -0
  98. data/glfw-src/lib/fullscreen.c +95 -0
  99. data/glfw-src/lib/glext.c +201 -0
  100. data/glfw-src/lib/image.c +629 -0
  101. data/glfw-src/lib/init.c +108 -0
  102. data/glfw-src/lib/input.c +280 -0
  103. data/glfw-src/lib/internal.h +210 -0
  104. data/glfw-src/lib/joystick.c +101 -0
  105. data/glfw-src/lib/macosx/Makefile.macosx.gcc +172 -0
  106. data/glfw-src/lib/macosx/Makefile.macosx.gcc.universal +166 -0
  107. data/glfw-src/lib/macosx/libglfw.pc.in +11 -0
  108. data/glfw-src/lib/macosx/macosx_enable.c +42 -0
  109. data/glfw-src/lib/macosx/macosx_fullscreen.c +126 -0
  110. data/glfw-src/lib/macosx/macosx_glext.c +52 -0
  111. data/glfw-src/lib/macosx/macosx_init.c +194 -0
  112. data/glfw-src/lib/macosx/macosx_joystick.c +50 -0
  113. data/glfw-src/lib/macosx/macosx_thread.c +414 -0
  114. data/glfw-src/lib/macosx/macosx_time.c +112 -0
  115. data/glfw-src/lib/macosx/macosx_window.c +1279 -0
  116. data/glfw-src/lib/macosx/platform.h +349 -0
  117. data/glfw-src/lib/stream.c +194 -0
  118. data/glfw-src/lib/tga.c +405 -0
  119. data/glfw-src/lib/thread.c +340 -0
  120. data/glfw-src/lib/time.c +83 -0
  121. data/glfw-src/lib/win32/Makefile.win32.bcc +265 -0
  122. data/glfw-src/lib/win32/Makefile.win32.cross-mgw +274 -0
  123. data/glfw-src/lib/win32/Makefile.win32.cygwin +279 -0
  124. data/glfw-src/lib/win32/Makefile.win32.lcc +246 -0
  125. data/glfw-src/lib/win32/Makefile.win32.mgw +243 -0
  126. data/glfw-src/lib/win32/Makefile.win32.msvc +242 -0
  127. data/glfw-src/lib/win32/Makefile.win32.ow +242 -0
  128. data/glfw-src/lib/win32/Makefile.win32.pellesc +242 -0
  129. data/glfw-src/lib/win32/glfwdll.def +67 -0
  130. data/glfw-src/lib/win32/glfwdll_mgw1.def +67 -0
  131. data/glfw-src/lib/win32/glfwdll_mgw2.def +67 -0
  132. data/glfw-src/lib/win32/glfwdll_pellesc.def +65 -0
  133. data/glfw-src/lib/win32/libglfw.pc.in +11 -0
  134. data/glfw-src/lib/win32/platform.h +474 -0
  135. data/glfw-src/lib/win32/win32_dllmain.c +60 -0
  136. data/glfw-src/lib/win32/win32_enable.c +155 -0
  137. data/glfw-src/lib/win32/win32_fullscreen.c +317 -0
  138. data/glfw-src/lib/win32/win32_glext.c +85 -0
  139. data/glfw-src/lib/win32/win32_init.c +356 -0
  140. data/glfw-src/lib/win32/win32_joystick.c +234 -0
  141. data/glfw-src/lib/win32/win32_thread.c +511 -0
  142. data/glfw-src/lib/win32/win32_time.c +146 -0
  143. data/glfw-src/lib/win32/win32_window.c +1714 -0
  144. data/glfw-src/lib/window.c +727 -0
  145. data/glfw-src/lib/x11/Makefile.x11.in +243 -0
  146. data/glfw-src/lib/x11/platform.h +415 -0
  147. data/glfw-src/lib/x11/x11_enable.c +51 -0
  148. data/glfw-src/lib/x11/x11_fullscreen.c +524 -0
  149. data/glfw-src/lib/x11/x11_glext.c +69 -0
  150. data/glfw-src/lib/x11/x11_init.c +275 -0
  151. data/glfw-src/lib/x11/x11_joystick.c +371 -0
  152. data/glfw-src/lib/x11/x11_keysym2unicode.c +902 -0
  153. data/glfw-src/lib/x11/x11_thread.c +507 -0
  154. data/glfw-src/lib/x11/x11_time.c +154 -0
  155. data/glfw-src/lib/x11/x11_window.c +1746 -0
  156. data/glfw-src/license.txt +21 -0
  157. data/glfw-src/readme.html +927 -0
  158. data/glfw-src/support/d/examples/Makefile +59 -0
  159. data/glfw-src/support/d/examples/boing.d +610 -0
  160. data/glfw-src/support/d/examples/gears.d +379 -0
  161. data/glfw-src/support/d/examples/keytest.d +272 -0
  162. data/glfw-src/support/d/examples/listmodes.d +48 -0
  163. data/glfw-src/support/d/examples/mipmaps.d +126 -0
  164. data/glfw-src/support/d/examples/mtbench.d +304 -0
  165. data/glfw-src/support/d/examples/mthello.d +54 -0
  166. data/glfw-src/support/d/examples/particles.d +1150 -0
  167. data/glfw-src/support/d/examples/pong3d.d +840 -0
  168. data/glfw-src/support/d/examples/splitview.d +486 -0
  169. data/glfw-src/support/d/examples/triangle.d +108 -0
  170. data/glfw-src/support/d/examples/wave.d +400 -0
  171. data/glfw-src/support/d/imports/gl.d +4539 -0
  172. data/glfw-src/support/d/imports/glfw.d +349 -0
  173. data/glfw-src/support/d/imports/glu.d +328 -0
  174. data/glfw-src/support/d/lib/glfwdll.def +64 -0
  175. data/glfw-src/support/d/lib/glu32.def +56 -0
  176. data/glfw-src/support/d/lib/makefile +12 -0
  177. data/glfw-src/support/d/lib/opengl32.def +372 -0
  178. data/glfw-src/support/d/readme.html +83 -0
  179. data/glfw-src/support/delphi/examples/Triangle.dpr +105 -0
  180. data/glfw-src/support/delphi/lib/glfw.pas +437 -0
  181. data/glfw-src/support/delphi/readme.html +97 -0
  182. data/glfw-src/support/lua/examples/gears.lua +383 -0
  183. data/glfw-src/support/lua/examples/test1.lua +68 -0
  184. data/glfw-src/support/lua/readme.html +128 -0
  185. data/glfw-src/support/lua/src/luaglfw.c +1179 -0
  186. data/glfw-src/support/lua/src/luaglfw.h +48 -0
  187. data/glfw-src/support/lua/src/runlua.c +82 -0
  188. data/glfw-src/support/masm/examples/fpc.mac +47 -0
  189. data/glfw-src/support/masm/examples/makeit.bat +66 -0
  190. data/glfw-src/support/masm/examples/triangle.asm +232 -0
  191. data/glfw-src/support/masm/include/glfw.inc +326 -0
  192. data/glfw-src/support/masm/include/glu32.inc +55 -0
  193. data/glfw-src/support/masm/include/opengl32.inc +372 -0
  194. data/glfw-src/support/masm/lib/glfwdll.lib +0 -0
  195. data/glfw-src/support/masm/readme.html +170 -0
  196. data/glfw-src/support/msvc80/GLFW.sln +26 -0
  197. data/glfw-src/support/msvc80/GLFW.vcproj +257 -0
  198. data/glfw-src/support/msvc80/GLFWDLL.vcproj +287 -0
  199. data/glfw-src/support/visualbasic/bindings/glfw.bas +320 -0
  200. data/glfw-src/support/visualbasic/bindings/glu32.bas +284 -0
  201. data/glfw-src/support/visualbasic/bindings/opengl32.bas +999 -0
  202. data/glfw-src/support/visualbasic/examples/Triangle.bas +101 -0
  203. data/glfw-src/support/visualbasic/readme.html +164 -0
  204. data/website/index.html +84 -0
  205. data/website/style.css +110 -0
  206. metadata +301 -0
@@ -0,0 +1,108 @@
1
+ //========================================================================
2
+ // This is a small test application for GLFW.
3
+ // The program opens a window (640x480), and renders a spinning colored
4
+ // triangle (it is controlled with both the GLFW timer and the mouse). It
5
+ // also calculates the rendering speed (FPS), which is displayed in the
6
+ // window title bar.
7
+ //========================================================================
8
+
9
+ import std.string;
10
+ import glfw;
11
+
12
+
13
+ //========================================================================
14
+ // main()
15
+ //========================================================================
16
+
17
+ int main()
18
+ {
19
+ int width, height, running, frames, x, y;
20
+ double t, t0, fps;
21
+ char[] titlestr;
22
+
23
+ // Initialise GLFW
24
+ glfwInit();
25
+
26
+ // Open OpenGL window
27
+ if( !glfwOpenWindow( 640, 480, 0,0,0,0, 0,0, GLFW_WINDOW ) )
28
+ {
29
+ glfwTerminate();
30
+ return 0;
31
+ }
32
+
33
+ // Enable sticky keys
34
+ glfwEnable( GLFW_STICKY_KEYS );
35
+
36
+ // Disable vertical sync (on cards that support it)
37
+ glfwSwapInterval( 0 );
38
+
39
+ // Main loop
40
+ running = GL_TRUE;
41
+ frames = 0;
42
+ t0 = glfwGetTime();
43
+ while( running )
44
+ {
45
+ // Get time and mouse position
46
+ t = glfwGetTime();
47
+ glfwGetMousePos( &x, &y );
48
+
49
+ // Calculate and display FPS (frames per second)
50
+ if( (t-t0) > 1.0 || frames == 0 )
51
+ {
52
+ fps = cast(double)frames / (t-t0);
53
+ titlestr = "Spinning Triangle (" ~ toString(fps) ~ " FPS)\0";
54
+ glfwSetWindowTitle( titlestr );
55
+ t0 = t;
56
+ frames = 0;
57
+ }
58
+ frames ++;
59
+
60
+ // Get window size (may be different than the requested size)
61
+ glfwGetWindowSize( &width, &height );
62
+ height = height > 0 ? height : 1;
63
+
64
+ // Set viewport
65
+ glViewport( 0, 0, width, height );
66
+
67
+ // Clear color buffer
68
+ glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
69
+ glClear( GL_COLOR_BUFFER_BIT );
70
+
71
+ // Select and setup the projection matrix
72
+ glMatrixMode( GL_PROJECTION );
73
+ glLoadIdentity();
74
+ gluPerspective( 65.0f, cast(GLfloat)width/cast(GLfloat)height, 1.0f,
75
+ 100.0f );
76
+
77
+ // Select and setup the modelview matrix
78
+ glMatrixMode( GL_MODELVIEW );
79
+ glLoadIdentity();
80
+ gluLookAt( 0.0f, 1.0f, 0.0f, // Eye-position
81
+ 0.0f, 20.0f, 0.0f, // View-point
82
+ 0.0f, 0.0f, 1.0f ); // Up-vector
83
+
84
+ // Draw a rotating colorful triangle
85
+ glTranslatef( 0.0f, 14.0f, 0.0f );
86
+ glRotatef( 0.3*cast(GLfloat)x + cast(GLfloat)t*100.0f, 0.0f, 0.0f, 1.0f );
87
+ glBegin( GL_TRIANGLES );
88
+ glColor3f( 1.0f, 0.0f, 0.0f );
89
+ glVertex3f( -5.0f, 0.0f, -4.0f );
90
+ glColor3f( 0.0f, 1.0f, 0.0f );
91
+ glVertex3f( 5.0f, 0.0f, -4.0f );
92
+ glColor3f( 0.0f, 0.0f, 1.0f );
93
+ glVertex3f( 0.0f, 0.0f, 6.0f );
94
+ glEnd();
95
+
96
+ // Swap buffers
97
+ glfwSwapBuffers();
98
+
99
+ // Check if the ESC key was pressed or the window was closed
100
+ running = !glfwGetKey( GLFW_KEY_ESC ) &&
101
+ glfwGetWindowParam( GLFW_OPENED );
102
+ }
103
+
104
+ // Close OpenGL window and terminate GLFW
105
+ glfwTerminate();
106
+
107
+ return 0;
108
+ }
@@ -0,0 +1,400 @@
1
+ /*****************************************************************************
2
+ * Wave Simulation in OpenGL
3
+ * (C) 2002 Jakob Thomsen
4
+ * http://home.in.tum.de/~thomsen
5
+ * Modified for GLFW by Sylvain Hellegouarch - sh@programmationworld.com
6
+ * Modified for variable frame rate by Marcus Geelnard
7
+ * 2003-Jan-31: Minor cleanups and speedups / MG
8
+ *****************************************************************************/
9
+
10
+ import glfw;
11
+ import std.math;
12
+ import std.c.stdlib;
13
+ import std.c.stdio;
14
+
15
+ /* Maximum delta T to allow for differential calculations */
16
+ const float MAX_DELTA_T = 0.01;
17
+
18
+ /* Animation speed (10.0 looks good) */
19
+ const float ANIMATION_SPEED = 10.0;
20
+
21
+
22
+ GLfloat alpha = 210.0f, beta = -70.0f;
23
+ GLfloat zoom = 2.0f;
24
+
25
+ int running = 1;
26
+
27
+ struct Vertex
28
+ {
29
+ GLfloat x,y,z;
30
+ GLfloat r,g,b;
31
+ }
32
+
33
+ const int GRIDW = 50;
34
+ const int GRIDH = 50;
35
+ const int VERTEXNUM = (GRIDW*GRIDH);
36
+
37
+ const int QUADW = (GRIDW-1);
38
+ const int QUADH = (GRIDH-1);
39
+ const int QUADNUM = (QUADW*QUADH);
40
+
41
+ GLuint quad[4*QUADNUM];
42
+ Vertex vertex[VERTEXNUM];
43
+
44
+ /* The grid will look like this:
45
+ *
46
+ * 3 4 5
47
+ * *---*---*
48
+ * | | |
49
+ * | 0 | 1 |
50
+ * | | |
51
+ * *---*---*
52
+ * 0 1 2
53
+ */
54
+
55
+ void initVertices( )
56
+ {
57
+ int x,y,p;
58
+
59
+ /* place the vertices in a grid */
60
+ for(y=0;y<GRIDH;y++)
61
+ for(x=0;x<GRIDW;x++)
62
+ {
63
+ p = y*GRIDW + x;
64
+
65
+ //vertex[p].x = (-GRIDW/2)+x+sin(2.0*M_PI*(double)y/(double)GRIDH);
66
+ //vertex[p].y = (-GRIDH/2)+y+cos(2.0*M_PI*(double)x/(double)GRIDW);
67
+ vertex[p].x = cast(GLfloat)(x-GRIDW/2)/cast(GLfloat)(GRIDW/2);
68
+ vertex[p].y = cast(GLfloat)(y-GRIDH/2)/cast(GLfloat)(GRIDH/2);
69
+ vertex[p].z = 0;//sin(d*M_PI);
70
+ //vertex[p].r = (GLfloat)x/(GLfloat)GRIDW;
71
+ //vertex[p].g = (GLfloat)y/(GLfloat)GRIDH;
72
+ //vertex[p].b = 1.0-((GLfloat)x/(GLfloat)GRIDW+(GLfloat)y/(GLfloat)GRIDH)/2.0;
73
+ if((x%4<2)^(y%4<2))
74
+ {
75
+ vertex[p].r = 0.0;
76
+ }
77
+ else
78
+ {
79
+ vertex[p].r=1.0;
80
+ }
81
+
82
+ vertex[p].g = cast(GLfloat)y/cast(GLfloat)GRIDH;
83
+ vertex[p].b = 1.0-(cast(GLfloat)x/cast(GLfloat)GRIDW+cast(GLfloat)y/cast(GLfloat)GRIDH)/2.0;
84
+ }
85
+
86
+ for(y=0;y<QUADH;y++)
87
+ for(x=0;x<QUADW;x++)
88
+ {
89
+ p = 4*(y*QUADW + x);
90
+
91
+ /* first quad */
92
+ quad[p+0] = y *GRIDW+x; /* some point */
93
+ quad[p+1] = y *GRIDW+x+1; /* neighbor at the right side */
94
+ quad[p+2] = (y+1)*GRIDW+x+1; /* upper right neighbor */
95
+ quad[p+3] = (y+1)*GRIDW+x; /* upper neighbor */
96
+ }
97
+ }
98
+
99
+ double dt;
100
+ double p[GRIDW][GRIDH];
101
+ double vx[GRIDW][GRIDH];
102
+ double vy[GRIDW][GRIDH];
103
+ double ax[GRIDW][GRIDH];
104
+ double ay[GRIDW][GRIDH];
105
+
106
+
107
+
108
+ void initSurface( )
109
+ {
110
+ int x, y;
111
+ double dx, dy, d;
112
+
113
+ for(y = 0; y<GRIDH; y++)
114
+ {
115
+ for(x = 0; x<GRIDW; x++)
116
+ {
117
+ dx = cast(double)(x-GRIDW/2);
118
+ dy = cast(double)(y-GRIDH/2);
119
+ d = sqrt( dx*dx + dy*dy );
120
+ if(d < 0.1 * cast(double)(GRIDW/2))
121
+ {
122
+ d = d * 10.0;
123
+ p[x][y] = -cos(d * (PI / cast(double)(GRIDW * 4))) * 100.0;
124
+ }
125
+ else
126
+ {
127
+ p[x][y] = 0.0;
128
+ }
129
+ vx[x][y] = 0.0;
130
+ vy[x][y] = 0.0;
131
+ }
132
+ }
133
+ }
134
+
135
+
136
+ /* Draw view */
137
+ void draw_screen( )
138
+ {
139
+ /* Clear the color and depth buffers. */
140
+ glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
141
+
142
+ /* We don't want to modify the projection matrix. */
143
+ glMatrixMode(GL_MODELVIEW);
144
+ glLoadIdentity();
145
+
146
+ /* Move back. */
147
+ glTranslatef(0.0, 0.0, -zoom);
148
+ /* Rotate the view */
149
+ glRotatef(beta, 1.0, 0.0, 0.0);
150
+ glRotatef(alpha, 0.0, 0.0, 1.0);
151
+
152
+ //glDrawArrays(GL_POINTS,0,VERTEXNUM); /* Points only */
153
+ glDrawElements(GL_QUADS, 4*QUADNUM, GL_UNSIGNED_INT, quad);
154
+ //glDrawElements(GL_LINES, QUADNUM, GL_UNSIGNED_INT, quad);
155
+
156
+ glfwSwapBuffers();
157
+ }
158
+
159
+
160
+ /* Initialize OpenGL */
161
+ void setup_opengl( )
162
+ {
163
+ /* Our shading model--Gouraud (smooth). */
164
+ glShadeModel(GL_SMOOTH);
165
+
166
+ /* Culling. */
167
+ //glCullFace(GL_BACK);
168
+ //glFrontFace(GL_CCW);
169
+ //glEnable(GL_CULL_FACE);
170
+
171
+ /* Switch on the z-buffer. */
172
+ glEnable(GL_DEPTH_TEST);
173
+
174
+ glEnableClientState(GL_VERTEX_ARRAY);
175
+ glEnableClientState(GL_COLOR_ARRAY);
176
+ glVertexPointer(3/*3 components per vertex (x,y,z)*/, GL_FLOAT, Vertex.sizeof, vertex);
177
+ glColorPointer(3/*3 components per vertex (r,g,b)*/, GL_FLOAT, Vertex.sizeof, &vertex[0].r); //Pointer to the first color
178
+ glPointSize(2.0);
179
+
180
+ /* Background color is black. */
181
+ glClearColor(0, 0, 0, 0);
182
+ }
183
+
184
+
185
+ /* Modify the height of each vertex according to the pressure. */
186
+ void adjustGrid( )
187
+ {
188
+ int pos;
189
+ int x, y;
190
+
191
+ for(y = 0; y<GRIDH; y++)
192
+ {
193
+ for(x = 0; x<GRIDW; x++)
194
+ {
195
+ pos = y*GRIDW + x;
196
+ vertex[pos].z = cast(float) (p[x][y]*(1.0/50.0));
197
+ }
198
+ }
199
+ }
200
+
201
+
202
+ /* Calculate wave propagation */
203
+ void calc( )
204
+ {
205
+ int x, y, x2, y2;
206
+ double time_step = dt * ANIMATION_SPEED;
207
+
208
+ /* compute accelerations */
209
+ for(x = 0; x < GRIDW; x++)
210
+ {
211
+ x2 = (x + 1) % GRIDW;
212
+ for(y = 0; y < GRIDH; y++)
213
+ {
214
+ ax[x][y] = p[x][y] - p[x2][y];
215
+ }
216
+ }
217
+
218
+ for(y = 0; y < GRIDH;y++)
219
+ {
220
+ y2 = (y + 1) % GRIDH;
221
+ for(x = 0; x < GRIDW; x++)
222
+ {
223
+ ay[x][y] = p[x][y] - p[x][y2];
224
+ }
225
+ }
226
+
227
+ /* compute speeds */
228
+ for(x = 0; x < GRIDW; x++)
229
+ {
230
+ for(y = 0; y < GRIDH; y++)
231
+ {
232
+ vx[x][y] = vx[x][y] + ax[x][y] * time_step;
233
+ vy[x][y] = vy[x][y] + ay[x][y] * time_step;
234
+ }
235
+ }
236
+
237
+ /* compute pressure */
238
+ for(x = 1; x < GRIDW; x++)
239
+ {
240
+ x2 = x - 1;
241
+ for(y = 1; y < GRIDH; y++)
242
+ {
243
+ y2 = y - 1;
244
+ p[x][y] = p[x][y] + (vx[x2][y] - vx[x][y] + vy[x][y2] - vy[x][y]) * time_step;
245
+ }
246
+ }
247
+ }
248
+
249
+
250
+ /* Program entry point */
251
+ int main(char[][] args)
252
+ {
253
+ /* Dimensions of our window. */
254
+ int width, height;
255
+ /* Style of our window. */
256
+ int mode;
257
+ /* Frame time */
258
+ double t, t_old, dt_total;
259
+
260
+ /* Initialize GLFW */
261
+ if(glfwInit() == GL_FALSE)
262
+ {
263
+ fprintf(stderr, "GLFW initialization failed\n");
264
+ exit(-1);
265
+ }
266
+
267
+ /* Desired window properties */
268
+ width = 640;
269
+ height = 480;
270
+ mode = GLFW_WINDOW;
271
+
272
+ /* Open window */
273
+ if( glfwOpenWindow(width,height,0,0,0,0,16,0,mode) == GL_FALSE )
274
+ {
275
+ fprintf(stderr, "Could not open window\n");
276
+ glfwTerminate();
277
+ exit(-1);
278
+ }
279
+
280
+ /* Set title */
281
+ glfwSetWindowTitle( "Wave Simulation" );
282
+
283
+ /* Keyboard handler */
284
+ glfwSetKeyCallback( &handle_key_down );
285
+ glfwEnable( GLFW_KEY_REPEAT );
286
+
287
+ /* Window resize handler */
288
+ glfwSetWindowSizeCallback( &handle_resize );
289
+
290
+ /* Initialize OpenGL */
291
+ setup_opengl();
292
+
293
+ /* Initialize simulation */
294
+ initVertices();
295
+ initSurface();
296
+ adjustGrid();
297
+
298
+ /* Initialize timer */
299
+ t_old = glfwGetTime() - 0.01;
300
+
301
+ /* Main loop */
302
+ while(running)
303
+ {
304
+ /* Timing */
305
+ t = glfwGetTime();
306
+ dt_total = t - t_old;
307
+ t_old = t;
308
+
309
+ /* Safety - iterate if dt_total is too large */
310
+ while( dt_total > 0.0f )
311
+ {
312
+ /* Select iteration time step */
313
+ dt = dt_total > MAX_DELTA_T ? MAX_DELTA_T : dt_total;
314
+ dt_total -= dt;
315
+
316
+ /* Calculate wave propagation */
317
+ calc();
318
+ }
319
+
320
+ /* Compute height of each vertex */
321
+ adjustGrid();
322
+
323
+ /* Draw wave grid to OpenGL display */
324
+ draw_screen();
325
+
326
+ /* Still running? */
327
+ running = running && glfwGetWindowParam( GLFW_OPENED );
328
+ }
329
+
330
+ glfwTerminate();
331
+
332
+ return 0;
333
+ }
334
+
335
+ version(Windows) {
336
+ extern(Windows):
337
+ } else {
338
+ extern(C):
339
+ }
340
+ /* Handle key strokes */
341
+ void handle_key_down(int key, int action)
342
+ {
343
+ if( action != GLFW_PRESS )
344
+ {
345
+ return;
346
+ }
347
+
348
+ switch(key) {
349
+ case GLFW_KEY_ESC:
350
+ running = 0;
351
+ break;
352
+ case GLFW_KEY_SPACE:
353
+ initSurface();
354
+ break;
355
+ case GLFW_KEY_LEFT:
356
+ alpha+=5;
357
+ break;
358
+ case GLFW_KEY_RIGHT:
359
+ alpha-=5;
360
+ break;
361
+ case GLFW_KEY_UP:
362
+ beta-=5;
363
+ break;
364
+ case GLFW_KEY_DOWN:
365
+ beta+=5;
366
+ break;
367
+ case GLFW_KEY_PAGEUP:
368
+ if(zoom>1) zoom-=1;
369
+ break;
370
+ case GLFW_KEY_PAGEDOWN:
371
+ zoom+=1;
372
+ break;
373
+ default:
374
+ break;
375
+ }
376
+ }
377
+
378
+
379
+ /* Callback function for window resize events */
380
+ void handle_resize( int width, int height )
381
+ {
382
+ float ratio = 1.0f;
383
+
384
+ if( height > 0 )
385
+ {
386
+ ratio = cast(float) width / cast(float) height;
387
+ }
388
+
389
+ /* Setup viewport (Place where the stuff will appear in the main window). */
390
+ glViewport(0, 0, width, height);
391
+
392
+ /*
393
+ * Change to the projection matrix and set
394
+ * our viewing volume.
395
+ */
396
+ glMatrixMode(GL_PROJECTION);
397
+ glLoadIdentity();
398
+ gluPerspective(60.0, ratio, 1.0, 1024.0);
399
+ }
400
+