OSDN Git Service

3c1816434ebcaf0b0caba638e6fe67af8d629fd7
[ring-lang-081/ring.git] / docs / build / html / _sources / usingopengl2.txt
1 .. index:: 
2         single: RingOpenGL と RingAllegro の用法 (3D グラフィックス); はじめに
3
4 ====================================================
5 RingOpenGL と RingAllegro の用法 (3D グラフィックス)
6 ====================================================
7
8
9 RingOpenGL と RingAllegro の用法を学びます。
10
11
12 .. index:: 
13         pair: RingOpenGL と RingAllegro の用法 (3D グラフィックス); 3D 立方体とテクスチャ
14
15 3D 立方体とテクスチャ
16 =====================
17
18 ソースコード:
19
20 .. code-block:: ring
21
22         # ライブラリの読み込み
23                 load "gamelib.ring"             # RingAllegro ライブラリ
24                 load "opengl21lib.ring"         # RingOpenGL  ライブラリ
25
26         #==============================================================
27         # macOS への対応
28                 al_run_main()   
29                 func al_game_start      # al_run_main() から呼び出されます。
30                         main()          # main 関数本体を呼び出します。
31         #==============================================================
32
33         func main
34
35                 new GraphicsApp {
36                         start()
37                 }
38
39
40         class GraphicsApp from GraphicsAppBase
41
42                 TITLE = "Ring Cube"
43
44                 bitmap texture
45
46                 xrot = 0.0
47                 yrot = 0.0
48                 zrot = 0.0
49
50                 func loadresources
51
52                         bitmap = al_load_bitmap("ring.bmp")
53                         texture = al_get_opengl_texture(bitmap)
54
55                 func destroyResources
56
57                         al_destroy_bitmap(bitmap)
58
59                 func drawScene
60
61                         w = 800 h = 600
62                         ratio =  w / h
63
64                         glViewport(0, 0, w, h)
65                         glMatrixMode(GL_PROJECTION)
66                         glLoadIdentity()
67
68                         gluPerspective(45,ratio,1,100)
69                         glMatrixMode(GL_MODELVIEW)
70                         glLoadIdentity()
71
72                         glEnable(GL_TEXTURE_2D)                                                 
73                         glShadeModel(GL_SMOOTH)                                                 
74                         glClearColor(0.0, 0.0, 0.0, 0.5)
75                         glClearDepth(1.0)                       
76                         glEnable(GL_DEPTH_TEST) 
77                         glEnable(GL_CULL_FACE)
78                         glDepthFunc(GL_LEQUAL)
79                         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
80
81                         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
82                         glLoadIdentity()                                                                        
83                         glTranslatef(0.0,0.0,-5.0)
84
85                         glRotatef(xrot,1.0,0.0,0.0)
86                         glRotatef(yrot,0.0,1.0,0.0)
87                         glRotatef(zrot,0.0,0.0,1.0)
88
89                         glBindTexture(GL_TEXTURE_2D, texture)
90
91                         glBegin(GL_QUADS)
92                                 // 前面
93                                 glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0,  1.0)
94                                 glTexCoord2f(1.0, 0.0) glVertex3f( 1.0, -1.0,  1.0)
95                                 glTexCoord2f(1.0, 1.0) glVertex3f( 1.0,  1.0,  1.0)
96                                 glTexCoord2f(0.0, 1.0) glVertex3f(-1.0,  1.0,  1.0)
97                                 // 背面
98                                 glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0, -1.0)
99                                 glTexCoord2f(1.0, 1.0) glVertex3f(-1.0,  1.0, -1.0)
100                                 glTexCoord2f(0.0, 1.0) glVertex3f( 1.0,  1.0, -1.0)
101                                 glTexCoord2f(0.0, 0.0) glVertex3f( 1.0, -1.0, -1.0)
102                                 // 上面
103                                 glTexCoord2f(0.0, 1.0) glVertex3f(-1.0,  1.0, -1.0)
104                                 glTexCoord2f(0.0, 0.0) glVertex3f(-1.0,  1.0,  1.0)
105                                 glTexCoord2f(1.0, 0.0) glVertex3f( 1.0,  1.0,  1.0)
106                                 glTexCoord2f(1.0, 1.0) glVertex3f( 1.0,  1.0, -1.0)
107                                 // 底面
108                                 glTexCoord2f(1.0, 1.0) glVertex3f(-1.0, -1.0, -1.0)
109                                 glTexCoord2f(0.0, 1.0) glVertex3f( 1.0, -1.0, -1.0)
110                                 glTexCoord2f(0.0, 0.0) glVertex3f( 1.0, -1.0,  1.0)
111                                 glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0,  1.0)
112                                 // 右面
113                                 glTexCoord2f(1.0, 0.0) glVertex3f( 1.0, -1.0, -1.0)
114                                 glTexCoord2f(1.0, 1.0) glVertex3f( 1.0,  1.0, -1.0)
115                                 glTexCoord2f(0.0, 1.0) glVertex3f( 1.0,  1.0,  1.0)
116                                 glTexCoord2f(0.0, 0.0) glVertex3f( 1.0, -1.0,  1.0)
117                                 // 左面
118                                 glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0, -1.0)
119                                 glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0,  1.0)
120                                 glTexCoord2f(1.0, 1.0) glVertex3f(-1.0,  1.0,  1.0)
121                                 glTexCoord2f(0.0, 1.0) glVertex3f(-1.0,  1.0, -1.0)
122                         glEnd()
123
124                         xrot += 0.3
125                         yrot += 0.2
126                         zrot += 0.4
127
128
129         class GraphicsAppBase
130
131                 display event_queue ev timeout 
132                 timer  redraw   = true
133
134                 FPS             = 60 
135
136                 SCREEN_W        = 800
137                 SCREEN_H        = 600
138
139                 KEY_UP          = 1
140                 KEY_DOWN        = 2
141                 KEY_LEFT        = 3
142                 KEY_RIGHT       = 4
143
144                 Key = [false,false,false,false]
145
146                 TITLE = "Graphics Application"
147
148                 func start
149
150                         SetUp()
151                         loadResources()
152                         eventsLoop()
153                         destroy()
154
155                 func setup
156
157                         al_init()
158                         al_init_image_addon()
159                         al_set_new_display_flags(ALLEGRO_OPENGL) 
160                         display = al_create_display(SCREEN_W,SCREEN_H)
161                         al_set_Window_title(display,TITLE)
162                         al_clear_to_color(al_map_rgb(0,0,0))
163                         event_queue = al_create_event_queue()
164                         al_register_event_source(event_queue, 
165                                 al_get_display_event_source(display))
166                         ev = al_new_allegro_event()
167                         timeout = al_new_allegro_timeout()
168                         al_init_timeout(timeout, 0.06)
169                         timer = al_create_timer(1.0 / FPS)
170                         al_register_event_source(event_queue, 
171                                 al_get_timer_event_source(timer))
172                         al_start_timer(timer)
173                         al_install_mouse()
174                         al_register_event_source(event_queue, 
175                                 al_get_mouse_event_source())
176                         al_install_keyboard()
177                         al_register_event_source(event_queue, 
178                                 al_get_keyboard_event_source())
179
180                 func eventsLoop
181
182                         while true
183                                 al_init_timeout(timeout, 0.06)
184                                 al_wait_for_event_until(event_queue, ev, timeout)
185                                 switch al_get_allegro_event_type(ev)
186                                 on ALLEGRO_EVENT_DISPLAY_CLOSE
187                                         exit
188                                 on ALLEGRO_EVENT_TIMER
189                                         redraw = true
190                                 on ALLEGRO_EVENT_MOUSE_AXES
191                                         mouse_x = al_get_allegro_event_mouse_x(ev)
192                                         mouse_y = al_get_allegro_event_mouse_y(ev)
193                                 on ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY
194                                         mouse_x = al_get_allegro_event_mouse_x(ev)
195                                         mouse_y = al_get_allegro_event_mouse_y(ev)
196                                 on ALLEGRO_EVENT_MOUSE_BUTTON_UP
197                                         exit
198                                 on ALLEGRO_EVENT_KEY_DOWN
199                                         switch al_get_allegro_event_keyboard_keycode(ev)
200                                                 on ALLEGRO_KEY_UP
201                                                         key[KEY_UP] = true
202                                                 on ALLEGRO_KEY_DOWN
203                                                         key[KEY_DOWN] = true
204                                                 on ALLEGRO_KEY_LEFT
205                                                         key[KEY_LEFT] = true
206                                                 on ALLEGRO_KEY_RIGHT
207                                                         key[KEY_RIGHT] = true
208                                         off
209                                 on ALLEGRO_EVENT_KEY_UP
210                                         switch al_get_allegro_event_keyboard_keycode(ev)
211                                                 on ALLEGRO_KEY_UP
212                                                         key[KEY_UP] = false
213                                                 on ALLEGRO_KEY_DOWN
214                                                         key[KEY_DOWN] = false
215                                                 on ALLEGRO_KEY_LEFT
216                                                         key[KEY_LEFT] = false
217                                                 on ALLEGRO_KEY_RIGHT
218                                                         key[KEY_RIGHT] = false
219                                                 on ALLEGRO_KEY_ESCAPE
220                                                         exit
221                                         off
222                                 off
223                                 if redraw and al_is_event_queue_empty(event_queue)
224                                         redraw = false
225                                         drawScene()
226                                         al_flip_display()
227                                 ok
228                                 callgc()
229                         end
230
231                 func destroy
232
233                         destroyResources()
234                         al_destroy_timer(timer)
235                         al_destroy_allegro_event(ev)
236                         al_destroy_allegro_timeout(timeout)
237                         al_destroy_event_queue(event_queue)
238                         al_destroy_display(display)
239                         al_exit()
240
241                 func loadresources
242
243                 func drawScene
244
245                 func destroyResources
246
247 スクリーンショット:
248
249 .. image:: ringcube3d.png
250         :alt: Ring Cube
251
252 .. index:: 
253         pair: RingOpenGL と RingAllegro の用法 (3D グラフィックス); 複数の立方体
254
255 複数の立方体
256 ============
257
258 スクリーンショット:
259
260 .. code-block:: ring
261
262         # ライブラリの読み込み
263                 load "gamelib.ring"             # RingAllegro ライブラリ
264                 load "opengl21lib.ring"         # RingOpenGL ライブラリ
265
266         #==============================================================
267         # macOS への対応
268                 al_run_main()   
269                 func al_game_start      # al_run_main() により呼び出されます。
270                         main()          # main 関数本体を呼び出します。
271         #==============================================================
272
273
274         func main
275
276                 new GraphicsApp {
277                         start()
278                 }
279
280
281         class GraphicsApp from GraphicsAppBase
282
283                 TITLE = "Many Cubes"
284
285                 bitmap bitmap2 bitmap3
286                 texture texture2 texture3
287
288                 fps = 120
289                 xrot = 0.0
290                 yrot = 0.0
291                 zrot = 0.0
292
293                 nPerspective = 100
294
295                 func loadresources
296
297                         bitmap = al_load_bitmap("sky1.jpg")
298                         texture = al_get_opengl_texture(bitmap)
299
300                         bitmap2 = al_load_bitmap("sky2.jpg")
301                         texture2 = al_get_opengl_texture(bitmap2)
302
303                         bitmap3 = al_load_bitmap("sky3.jpg")
304                         texture3 = al_get_opengl_texture(bitmap3)
305
306                 func destroyResources
307
308                         al_destroy_bitmap(bitmap)
309                         al_destroy_bitmap(bitmap2)
310                         al_destroy_bitmap(bitmap3)
311
312                 func drawScene
313
314                         prepare()
315                         cubes()
316                         rotate()
317
318                 func Prepare
319                         w = 800 h = 600
320                         ratio =  w / h
321                         glViewport(0, 0, w, h)
322                         glMatrixMode(GL_PROJECTION)
323                         glLoadIdentity()
324                         gluPerspective(-nPerspective,ratio,1,nPerspective)
325                         glMatrixMode(GL_MODELVIEW)
326                         glLoadIdentity()
327                         glEnable(GL_TEXTURE_2D)                                                 
328                         glShadeModel(GL_SMOOTH)         
329                         glClearColor(0.0, 0.0, 0.0, 0.5)
330                         glClearDepth(1.0)                       
331                         glEnable(GL_DEPTH_TEST) 
332                         glEnable(GL_CULL_FACE)
333                         glDepthFunc(GL_LEQUAL)
334                         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
335                         glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT)
336
337                 func Cubes
338                         cube(5,-3.4,-5,:sky1)
339                         cube(0,-3,-5,:sky1)
340                         cube(-5,-3,-5,:sky1)
341                         cube(5,0.5,-5,:sky2)
342                         cube(0,0.5,-5,:sky2)
343                         cube(-5,0.5,-5,:sky2)
344                         cube(5,4,-5,:sky3)
345                         cube(0,4,-5,:sky3)
346                         cube(-5,4,-5,:sky3)
347
348                 func Rotate 
349                         xrot += 0.3 * 5
350                         yrot += 0.2 * 5
351                         zrot += 0.4 * 5
352                         nPerspective += 0.5
353
354
355                 func cube(x,y,z,nTexture)
356                         glLoadIdentity()                                                                        
357                         glTranslatef(x,y,z)
358                         glRotatef(xrot,1.0,0.0,0.0)
359                         glRotatef(yrot,0.0,1.0,0.0)
360                         glRotatef(zrot,0.0,0.0,1.0)
361                         drawcube(nTexture)
362
363                 func drawcube(cTexture) 
364
365                         switch cTexture
366                                 on :sky1
367                                         glBindTexture(GL_TEXTURE_2D, texture)
368                                 on :sky2
369                                         glBindTexture(GL_TEXTURE_2D, texture2)
370                                 on :sky3
371                                         glBindTexture(GL_TEXTURE_2D, texture3)
372                         off
373
374
375                         glBegin(GL_QUADS)
376                                 // 前面
377                                 glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0,  1.0)
378                                 glTexCoord2f(1.0, 0.0) glVertex3f( 1.0, -1.0,  1.0)
379                                 glTexCoord2f(1.0, 1.0) glVertex3f( 1.0,  1.0,  1.0)
380                                 glTexCoord2f(0.0, 1.0) glVertex3f(-1.0,  1.0,  1.0)
381                                 // 背面
382                                 glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0, -1.0)
383                                 glTexCoord2f(1.0, 1.0) glVertex3f(-1.0,  1.0, -1.0)
384                                 glTexCoord2f(0.0, 1.0) glVertex3f( 1.0,  1.0, -1.0)
385                                 glTexCoord2f(0.0, 0.0) glVertex3f( 1.0, -1.0, -1.0)
386                                 // 上面
387                                 glTexCoord2f(0.0, 1.0) glVertex3f(-1.0,  1.0, -1.0)
388                                 glTexCoord2f(0.0, 0.0) glVertex3f(-1.0,  1.0,  1.0)
389                                 glTexCoord2f(1.0, 0.0) glVertex3f( 1.0,  1.0,  1.0)
390                                 glTexCoord2f(1.0, 1.0) glVertex3f( 1.0,  1.0, -1.0)
391                                 // 底面
392                                 glTexCoord2f(1.0, 1.0) glVertex3f(-1.0, -1.0, -1.0)
393                                 glTexCoord2f(0.0, 1.0) glVertex3f( 1.0, -1.0, -1.0)
394                                 glTexCoord2f(0.0, 0.0) glVertex3f( 1.0, -1.0,  1.0)
395                                 glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0,  1.0)
396                          
397                                 // 右面
398                                 glTexCoord2f(1.0, 0.0) glVertex3f( 1.0, -1.0, -1.0)
399                                 glTexCoord2f(1.0, 1.0) glVertex3f( 1.0,  1.0, -1.0)
400                                 glTexCoord2f(0.0, 1.0) glVertex3f( 1.0,  1.0,  1.0)
401                                 glTexCoord2f(0.0, 0.0) glVertex3f( 1.0, -1.0,  1.0)
402                  
403                                 // 左面
404                                 glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0, -1.0)
405                                 glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0,  1.0)
406                                 glTexCoord2f(1.0, 1.0) glVertex3f(-1.0,  1.0,  1.0)
407                                 glTexCoord2f(0.0, 1.0) glVertex3f(-1.0,  1.0, -1.0)
408                         glEnd()
409
410
411         class GraphicsAppBase
412
413                 display event_queue ev timeout 
414                 timer  redraw   = true
415
416                 FPS             = 60 
417
418                 SCREEN_W        = 800
419                 SCREEN_H        = 600
420
421                 KEY_UP          = 1
422                 KEY_DOWN        = 2
423                 KEY_LEFT        = 3
424                 KEY_RIGHT       = 4
425
426                 Key = [false,false,false,false]
427
428                 TITLE = "Graphics Application"
429
430                 func start
431
432                         SetUp()
433                         loadResources()
434                         eventsLoop()
435                         destroy()
436
437                 func setup
438
439                         al_init()
440                         al_init_image_addon()
441                         al_set_new_display_flags(ALLEGRO_OPENGL) 
442                         display = al_create_display(SCREEN_W,SCREEN_H)
443                         al_set_Window_title(display,TITLE)
444                         al_clear_to_color(al_map_rgb(0,0,0))
445                         event_queue = al_create_event_queue()
446                         al_register_event_source(event_queue, 
447                                 al_get_display_event_source(display))
448                         ev = al_new_allegro_event()
449                         timeout = al_new_allegro_timeout()
450                         al_init_timeout(timeout, 0.06)
451                         timer = al_create_timer(1.0 / FPS)
452                         al_register_event_source(event_queue, 
453                                 al_get_timer_event_source(timer))
454                         al_start_timer(timer)
455                         al_install_mouse()
456                         al_register_event_source(event_queue, 
457                                 al_get_mouse_event_source())
458                         al_install_keyboard()
459                         al_register_event_source(event_queue, 
460                                 al_get_keyboard_event_source())
461
462                 func eventsLoop
463
464                         while true
465                                 al_init_timeout(timeout, 0.06)
466                                 al_wait_for_event_until(event_queue, ev, timeout)
467                                 switch al_get_allegro_event_type(ev)
468                                 on ALLEGRO_EVENT_DISPLAY_CLOSE
469                                         exit
470                                 on ALLEGRO_EVENT_TIMER
471                                         redraw = true
472                                 on ALLEGRO_EVENT_MOUSE_AXES
473                                         mouse_x = al_get_allegro_event_mouse_x(ev)
474                                         mouse_y = al_get_allegro_event_mouse_y(ev)
475                                 on ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY
476                                         mouse_x = al_get_allegro_event_mouse_x(ev)
477                                         mouse_y = al_get_allegro_event_mouse_y(ev)
478                                 on ALLEGRO_EVENT_MOUSE_BUTTON_UP
479                                         exit
480                                 on ALLEGRO_EVENT_KEY_DOWN
481                                         switch al_get_allegro_event_keyboard_keycode(ev)
482                                                 on ALLEGRO_KEY_UP
483                                                         key[KEY_UP] = true
484                                                 on ALLEGRO_KEY_DOWN
485                                                         key[KEY_DOWN] = true
486                                                 on ALLEGRO_KEY_LEFT
487                                                         key[KEY_LEFT] = true
488                                                 on ALLEGRO_KEY_RIGHT
489                                                         key[KEY_RIGHT] = true
490                                         off
491                                 on ALLEGRO_EVENT_KEY_UP
492                                         switch al_get_allegro_event_keyboard_keycode(ev)
493                                                 on ALLEGRO_KEY_UP
494                                                         key[KEY_UP] = false
495                                                 on ALLEGRO_KEY_DOWN
496                                                         key[KEY_DOWN] = false
497                                                 on ALLEGRO_KEY_LEFT
498                                                         key[KEY_LEFT] = false
499                                                 on ALLEGRO_KEY_RIGHT
500                                                         key[KEY_RIGHT] = false
501                                                 on ALLEGRO_KEY_ESCAPE
502                                                         exit
503                                         off
504                                 off
505                                 if redraw and al_is_event_queue_empty(event_queue)
506                                         redraw = false
507                                         drawScene()
508                                         al_flip_display()
509                                 ok
510                                 callgc()
511                         end
512
513                 func destroy
514
515                         destroyResources()
516                         al_destroy_timer(timer)
517                         al_destroy_allegro_event(ev)
518                         al_destroy_allegro_timeout(timeout)
519                         al_destroy_event_queue(event_queue)
520                         al_destroy_display(display)
521                         al_exit()
522
523                 func loadresources
524
525                 func drawScene
526
527                 func destroyResources
528
529
530 スクリーンショット:
531
532 .. image:: manycubes.png
533         :alt: 複数の立方体
534
535 .. index:: 
536         pair: RingOpenGL と RingAllegro の用法 (3D グラフィックス); TicTacToe 3D ゲーム
537
538 TicTacToe 3D ゲーム
539 ===================
540
541
542 ソースコード:
543
544 .. code-block:: ring
545
546
547         # ライブラリの読み込み
548                 load "gamelib.ring"             # RingAllegro ライブラリ
549                 load "opengl21lib.ring"         # RingOpenGL ライブラリ
550
551         #==============================================================
552         # macOS への対応
553                 al_run_main()   
554                 func al_game_start      # al_run_main() により呼び出されます。
555                         main()          # main 関数本体を呼び出します。
556         #==============================================================
557
558         func main
559                 new TicTacToe3D {
560                         start()
561                 }
562
563         class TicTacToe3D from GameLogic
564
565                 FPS = 60
566                 TITLE = "TicTacToe 3D"
567
568                 oBackground = new GameBackground
569                 oGameSound = new GameSound
570                 oGameCube = new GameCube
571                 oGameOver = new GameOver
572                 oGameInterface = new GameInterface 
573
574                 func loadresources
575                         oGameOver.loadresources()
576                         oGameSound.loadresources()
577                         oBackGround.loadresources()
578                         oGameCube.loadresources()
579
580                 func destroyResources
581                         oGameOver.destroyResources()
582                         oGameSound.destroyResources()
583                         oBackGround.destroyResources()
584                         oGameCube.destroyResources()
585
586                 func drawScene
587                         oBackground.update()
588                         oGameInterface.update(self)
589
590                 func MouseClickEvent
591                         oGameInterface.MouseClickEvent(self)
592
593         class GameInterface 
594
595                 func Update oGame
596                         prepare()
597                         cubes(oGame)
598
599                 func Prepare 
600                         w = 1024 h = 768
601                         ratio =  w / h
602                         glViewport(0, 0, w, h)
603                         glMatrixMode(GL_PROJECTION)
604                         glLoadIdentity()
605                         gluPerspective(-120,ratio,1,120)
606                         glMatrixMode(GL_MODELVIEW)
607                         glLoadIdentity()
608                         glEnable(GL_TEXTURE_2D)                                                 
609                         glShadeModel(GL_SMOOTH)         
610                         glClearColor(0.0, 0.0, 0.0, 0.5)
611                         glClearDepth(1.0)                       
612                         glEnable(GL_DEPTH_TEST) 
613                         glEnable(GL_CULL_FACE)
614                         glDepthFunc(GL_LEQUAL)
615                         glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST)
616
617                 func Cubes oGame
618                         oGame.oGameCube {
619                                 aGameMap = oGame.aGameMap 
620                                 cube( 5  , -3 , -5  , aGameMap[1][1] )
621                                 cube( 0  , -3 , -5  , aGameMap[1][2] )
622                                 cube( -5 , -3 , -5  , aGameMap[1][3] )
623                                 cube( 5  , 1  , -5  , aGameMap[2][1] )
624                                 cube( 0  , 1  , -5  , aGameMap[2][2] )
625                                 cube( -5 , 1  , -5  , aGameMap[2][3] )
626                                 cube( 5  , 5  , -5  , aGameMap[3][1] )
627                                 cube( 0  , 5  , -5  , aGameMap[3][2] )
628                                 cube( -5 , 5  , -5  , aGameMap[3][3] )
629                                 rotate()
630                         }
631
632                 func MouseClickEvent oGame
633                         oGame {
634                                 aBtn = Point2Button(Mouse_X,Mouse_Y)
635                                 nRow = aBtn[1]
636                                 nCol = aBtn[2]
637                                 if nRow != 0 and nCol != 0      
638                                         if aGameMap[nRow][nCol] = :n
639                                                 aGameMap[nRow][nCol] = cActivePlayer
640                                                 ChangeActivePlayer()
641                                                 CheckGameOver()
642                                         ok                      
643                                 ok
644                         }
645
646         Class GameLogic from GraphicsAppBase
647
648                 aGameMap = [
649                         [ :n , :n , :n ] ,
650                         [ :n , :n , :n ] ,
651                         [ :n , :n , :n ]
652                 ]
653
654                 aGameButtons = [                        # x1,y1,x2,y2
655                         [176,88,375,261],               # [1,1]
656                         [423,88,591,261],               # [1,2]
657                         [645,88,876,261],               # [1,3]
658                         [176,282,375,428],              # [2,1]
659                         [423,282,591,428],              # [2,2]
660                         [645,282,876,428],              # [2,3]
661                         [176,454,375,678],              # [3,1]
662                         [423,454,591,678],              # [3,2]
663                         [645,454,876,678]               # [3,3]
664                 ]
665
666                 cActivePlayer = :x
667
668                 func point2button x,y
669                         nRow = 0
670                         nCol = 0
671                         for t = 1 to len(aGameButtons) 
672                                 rect = aGameButtons[t]
673                                 if x >= rect[1] and x <= rect[3] and
674                                    y >= rect[2] and y <= rect[4] 
675                                                 switch t
676                                                         on 1  nRow = 1  nCol = 1
677                                                         on 2  nRow = 1  nCol = 2
678                                                         on 3  nRow = 1  nCol = 3
679                                                         on 4  nRow = 2  nCol = 1
680                                                         on 5  nRow = 2  nCol = 2
681                                                         on 6  nRow = 2  nCol = 3
682                                                         on 7  nRow = 3  nCol = 1
683                                                         on 8  nRow = 3  nCol = 2
684                                                         on 9  nRow = 3  nCol = 3
685                                                 off
686                                                 exit 
687                                 ok
688                         next 
689                         return [nRow,nCol]
690
691                 func ChangeActivePlayer()
692                         if cActivePlayer = :x
693                                 cActivePlayer = :o
694                         else 
695                                 cActivePlayer = :x
696                         ok
697
698                 func CheckGameOver
699                         aList = [
700                                 aGameMap[1][1],
701                                 aGameMap[1][2],
702                                 aGameMap[1][3],
703                                 aGameMap[2][1],
704                                 aGameMap[2][2],
705                                 aGameMap[2][3],
706                                 aGameMap[3][1],
707                                 aGameMap[3][2],
708                                 aGameMap[3][3]
709                         ]
710                         for item in aList 
711                                 switch item 
712                                         on :x   item = 1
713                                         on :o   item = 2
714                                         on :n   item = 0
715                                 off
716                         next
717                         nStatus = CheckWinner(aList)
718                         if nStatus 
719                                 oGameOver {
720                                         Switch nStatus
721                                                 on 1 Player1Win(this)  
722                                                 on 2 Player2Win(this)  
723                                                 on 3 NoOneWin(this)   
724                                         off
725                                 }
726                                 refreshGame()
727                         ok
728
729                 func refreshGame
730                         aGameMap = [
731                                 [ :n , :n , :n ] ,
732                                 [ :n , :n , :n ] ,
733                                 [ :n , :n , :n ]
734                         ]
735                         cActivePlayer = :x
736
737                 func CheckWinner lst
738                         // 垂直の確認
739                         for v=1 to 9 step 3
740                                 if lst[v]!=0 and lst[v+1]!=0 and lst[v+2]!=0
741                                         if lst[v]=lst[v+1] and lst[v+1]=lst[v+2]
742                                                 return lst[v]
743                                         ok
744                                 ok
745                         next
746                         // 水平
747                         for h=1 to 3
748                                 if lst[h]!=0 and lst[h+3]!=0 and lst[h+6]!=0
749                                         if lst[h]=lst[h+3] and lst[h+3]=lst[h+6]
750                                                 return lst[h]
751                                         ok
752                                 ok
753                         next
754                         // 十字型
755                         if lst[1]!=0 and lst[5]!=0 and lst[9]!=0
756                                 if lst[1]=lst[5] and lst[5]=lst[9] return lst[1] ok
757                         ok
758                         if lst[3]!=0 and lst[5]!=0 and lst[7]!=0
759                                 if lst[3]=lst[5] and lst[5]=lst[7] return lst[3] ok
760                         ok
761                         // 枕木型
762                         tie=true
763                         for i=1 to 9
764                                 if lst[i]=0 tie=false exit ok
765                         next
766                         if tie=true return 3 ok return 0
767
768
769
770         class GameOver
771
772                 font bitmap
773
774                 func loadresources
775                         font = al_load_ttf_font("font/pirulen.ttf",54,0 )
776                         bitmap = al_load_bitmap("image/ballon.png")
777
778                 func destroyResources
779                         al_destroy_bitmap(bitmap)
780                         al_destroy_font(font)
781
782                 func Player1Win oGame
783                         showMsg(oGame,80,430,"Good job X you won!")
784                         
785                 func Player2Win oGame
786                         showMsg(oGame,80,430,"Good job O you won!")
787                 
788                 func NoOneWin oGame
789                         showMsg(oGame,150,430,"Oh no it's a tie!")
790                         
791                 func ShowMsg oGame,x,y,cMsg
792                         oGame {
793                                 drawScene()
794                                 al_flip_display()
795                                 al_rest(0.3)
796                                 newdisplay = al_create_display(SCREEN_W,SCREEN_H)
797                                 al_set_window_title(newdisplay,TITLE)
798                                 al_clear_to_color(al_map_rgb(255,255,255))
799                                 al_draw_bitmap(this.bitmap,200,50,1)
800                                 al_draw_text(this.font,
801                                          al_map_rgb(0,0,255), x,y,
802                                          ALLEGRO_ALIGN_LEFT,cMsg)
803                                 al_flip_display()
804                                 al_rest(2)
805                                 al_destroy_display(newdisplay)
806                                 al_set_target_backbuffer(display)
807                         }
808
809         class GameCube
810
811                 bitmap bitmap2 bitmap3 
812                 textureX textureO textureN
813
814                 xrot = 0.0
815                 yrot = 0.0
816                 zrot = 0.0
817
818                 func loadresources
819                         bitmap = al_load_bitmap("image/o.png")
820                         textureO = al_get_opengl_texture(bitmap)
821                         bitmap2 = al_load_bitmap("image/x.png")
822                         textureX = al_get_opengl_texture(bitmap2)
823                         bitmap3 = al_load_bitmap("image/empty.png")
824                         textureN = al_get_opengl_texture(bitmap3)
825
826                 func destroyResources
827                         al_destroy_bitmap(bitmap)
828                         al_destroy_bitmap(bitmap2)
829                         al_destroy_bitmap(bitmap3)
830
831                 func cube(x,y,z,nTexture)
832                         glLoadIdentity()                                                                        
833                         glTranslatef(x,y,z)
834                         glRotatef(xrot,1.0,0.0,0.0)
835                         glRotatef(yrot,0.0,1.0,0.0)
836                         glRotatef(zrot,0.0,0.0,1.0)
837                         setCubeTexture(nTexture)
838                         drawCube()
839
840                 func setCubeTexture cTexture
841                         switch cTexture
842                                 on :x
843                                         glBindTexture(GL_TEXTURE_2D, textureX)
844                                 on :o
845                                         glBindTexture(GL_TEXTURE_2D, textureO)
846                                 on :n
847                                         glBindTexture(GL_TEXTURE_2D, textureN)
848                         off
849
850                 func Rotate 
851                         xrot += 0.3 * 5
852                         yrot += 0.2 * 5
853                         zrot += 0.4 * 5
854
855                 func drawcube
856                         glBegin(GL_QUADS)
857                                 // 前面
858                                 glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0,  1.0)
859                                 glTexCoord2f(1.0, 0.0) glVertex3f( 1.0, -1.0,  1.0)
860                                 glTexCoord2f(1.0, 1.0) glVertex3f( 1.0,  1.0,  1.0)
861                                 glTexCoord2f(0.0, 1.0) glVertex3f(-1.0,  1.0,  1.0)
862                                 // 背面
863                                 glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0, -1.0)
864                                 glTexCoord2f(1.0, 1.0) glVertex3f(-1.0,  1.0, -1.0)
865                                 glTexCoord2f(0.0, 1.0) glVertex3f( 1.0,  1.0, -1.0)
866                                 glTexCoord2f(0.0, 0.0) glVertex3f( 1.0, -1.0, -1.0)
867                                 // 上面
868                                 glTexCoord2f(0.0, 1.0) glVertex3f(-1.0,  1.0, -1.0)
869                                 glTexCoord2f(0.0, 0.0) glVertex3f(-1.0,  1.0,  1.0)
870                                 glTexCoord2f(1.0, 0.0) glVertex3f( 1.0,  1.0,  1.0)
871                                 glTexCoord2f(1.0, 1.0) glVertex3f( 1.0,  1.0, -1.0)
872                                 // 底面
873                                 glTexCoord2f(1.0, 1.0) glVertex3f(-1.0, -1.0, -1.0)
874                                 glTexCoord2f(0.0, 1.0) glVertex3f( 1.0, -1.0, -1.0)
875                                 glTexCoord2f(0.0, 0.0) glVertex3f( 1.0, -1.0,  1.0)
876                                 glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0,  1.0)
877                          
878                                 // 右面
879                                 glTexCoord2f(1.0, 0.0) glVertex3f( 1.0, -1.0, -1.0)
880                                 glTexCoord2f(1.0, 1.0) glVertex3f( 1.0,  1.0, -1.0)
881                                 glTexCoord2f(0.0, 1.0) glVertex3f( 1.0,  1.0,  1.0)
882                                 glTexCoord2f(0.0, 0.0) glVertex3f( 1.0, -1.0,  1.0)
883                  
884                                 // 左面
885                                 glTexCoord2f(0.0, 0.0) glVertex3f(-1.0, -1.0, -1.0)
886                                 glTexCoord2f(1.0, 0.0) glVertex3f(-1.0, -1.0,  1.0)
887                                 glTexCoord2f(1.0, 1.0) glVertex3f(-1.0,  1.0,  1.0)
888                                 glTexCoord2f(0.0, 1.0) glVertex3f(-1.0,  1.0, -1.0)
889                         glEnd()
890
891
892         class GameBackground 
893
894                 nBackX = 0
895                 nBackY = 0
896                 nBackDiffx = -1
897                 nBackDiffy = -1
898                 nBackMotion = 1
899                 aBackMotionList = [
900                         [ -1, -1 ] ,            # 右下
901                         [ 0 , 1  ] ,            # 上
902                         [ -1, -1 ] ,            # 右下
903                         [ 0 , 1  ] ,            # 上
904                         [ 1 , -1 ] ,            # 左下
905                         [ 0 , 1  ] ,            # 上
906                         [ 1 , -1 ] ,            # 左下
907                         [ 0 , 1  ]                      # 上
908                 ]
909
910                 bitmap
911
912                 func Update 
913                         draw()
914                         motion()
915
916                 func draw
917                         al_draw_bitmap(bitmap,nBackX,nBackY,1)
918
919                 func motion
920                         nBackX += nBackDiffx
921                         nBackY += nBackDiffy
922                         if (nBackY = -350) or (nBackY = 0)
923                                 nBackMotion++
924                                 if nBackMotion > len(aBackMotionList)
925                                         nBackMotion = 1
926                                 ok
927                                 nBackDiffx  = aBackMotionList[nBackMotion][1]
928                                 nBackDiffy  = aBackMotionList[nBackMotion][2]
929                         ok
930
931                 func loadResources
932                         bitmap = al_load_bitmap("image/back.jpg")
933
934                 func destroyResources
935                         al_destroy_bitmap(bitmap)
936
937
938         class GameSound
939
940                 sample sampleid
941
942                 func loadresources
943                         sample = al_load_sample( "sound/music1.wav" )
944                         sampleid = al_new_allegro_sample_id()
945                         al_play_sample(sample, 1.0, 0.0,1.0,ALLEGRO_PLAYMODE_LOOP,sampleid)
946
947                 func destroyResources
948                         al_destroy_allegro_sample_id(sampleid)
949                         al_destroy_sample(sample)
950
951
952         class GraphicsAppBase
953
954                 display event_queue ev timeout 
955                 timer  
956                 redraw                  = true
957                 FPS                     = 60 
958                 SCREEN_W                = 1024
959                 SCREEN_H                = 700
960                 KEY_UP                  = 1
961                 KEY_DOWN                = 2
962                 KEY_LEFT                = 3
963                 KEY_RIGHT               = 4
964                 Key                     = [false,false,false,false]
965                 Mouse_X                 = 0
966                 Mouse_Y                 = 0
967                 TITLE                   = "Graphics Application"
968                 PRINT_MOUSE_XY  = False
969
970                 func start
971                         SetUp()
972                         loadResources()
973                         eventsLoop()
974                         destroy()
975
976                 func setup
977                         al_init()
978                         al_init_font_addon()
979                         al_init_ttf_addon()
980                         al_init_image_addon()
981                         al_install_audio()
982                         al_init_acodec_addon()
983                         al_reserve_samples(1)
984                         al_set_new_display_flags(ALLEGRO_OPENGL) 
985                         display = al_create_display(SCREEN_W,SCREEN_H)
986                         al_set_window_title(display,TITLE)
987                         al_clear_to_color(al_map_rgb(0,0,0))
988                         event_queue = al_create_event_queue()
989                         al_register_event_source(event_queue, 
990                                 al_get_display_event_source(display))
991                         ev = al_new_allegro_event()
992                         timeout = al_new_allegro_timeout()
993                         al_init_timeout(timeout, 0.06)
994                         timer = al_create_timer(1.0 / FPS)
995                         al_register_event_source(event_queue, 
996                                 al_get_timer_event_source(timer))
997                         al_start_timer(timer)
998                         al_install_mouse()
999                         al_register_event_source(event_queue, 
1000                                 al_get_mouse_event_source())
1001                         al_install_keyboard()
1002                         al_register_event_source(event_queue, 
1003                                 al_get_keyboard_event_source())
1004
1005                 func eventsLoop
1006                         while true
1007                                 al_init_timeout(timeout, 0.06)
1008                                 al_wait_for_event_until(event_queue, ev, timeout)
1009                                 switch al_get_allegro_event_type(ev)
1010                                 on ALLEGRO_EVENT_DISPLAY_CLOSE
1011                                         CloseEvent()
1012                                 on ALLEGRO_EVENT_TIMER
1013                                         redraw = true
1014                                 on ALLEGRO_EVENT_MOUSE_AXES
1015                                         mouse_x = al_get_allegro_event_mouse_x(ev)
1016                                         mouse_y = al_get_allegro_event_mouse_y(ev)
1017                                         if PRINT_MOUSE_XY
1018                                                 see "x = " + mouse_x + nl
1019                                                 see "y = " + mouse_y + nl
1020                                         ok
1021                                 on ALLEGRO_EVENT_MOUSE_ENTER_DISPLAY
1022                                         mouse_x = al_get_allegro_event_mouse_x(ev)
1023                                         mouse_y = al_get_allegro_event_mouse_y(ev)
1024                                 on ALLEGRO_EVENT_MOUSE_BUTTON_UP
1025                                         MouseClickEvent()
1026                                 on ALLEGRO_EVENT_KEY_DOWN
1027                                         switch al_get_allegro_event_keyboard_keycode(ev)
1028                                                 on ALLEGRO_KEY_UP
1029                                                         key[KEY_UP] = true
1030                                                 on ALLEGRO_KEY_DOWN
1031                                                         key[KEY_DOWN] = true
1032                                                 on ALLEGRO_KEY_LEFT
1033                                                         key[KEY_LEFT] = true
1034                                                 on ALLEGRO_KEY_RIGHT
1035                                                         key[KEY_RIGHT] = true
1036                                         off
1037                                 on ALLEGRO_EVENT_KEY_UP
1038                                         switch al_get_allegro_event_keyboard_keycode(ev)
1039                                                 on ALLEGRO_KEY_UP
1040                                                         key[KEY_UP] = false
1041                                                 on ALLEGRO_KEY_DOWN
1042                                                         key[KEY_DOWN] = false
1043                                                 on ALLEGRO_KEY_LEFT
1044                                                         key[KEY_LEFT] = false
1045                                                 on ALLEGRO_KEY_RIGHT
1046                                                         key[KEY_RIGHT] = false
1047                                                 on ALLEGRO_KEY_ESCAPE
1048                                                         exit
1049                                         off
1050                                 off
1051                                 if redraw and al_is_event_queue_empty(event_queue)
1052                                         redraw = false
1053                                         drawScene()
1054                                         al_flip_display()
1055                                 ok
1056                                 callgc()
1057                         end
1058
1059                 func destroy
1060                         destroyResources()
1061                         al_destroy_timer(timer)
1062                         al_destroy_allegro_event(ev)
1063                         al_destroy_allegro_timeout(timeout)
1064                         al_destroy_event_queue(event_queue)
1065                         al_destroy_display(display)
1066                         al_exit()
1067
1068                 func loadresources
1069
1070                 func drawScene
1071
1072                 func destroyResources
1073
1074                 func MouseClickEvent
1075                         exit                    # イベントループから脱出
1076
1077                 func CloseEvent
1078                         exit                    # イベントループから脱出
1079
1080
1081 スクリーンショット:
1082
1083 .. image:: tictactoe3d.png
1084         :width: 450pt
1085         :height: 350pt
1086         :alt: TicTacToe 3D ゲーム
1087
1088
1089 .. index:: 
1090         pair: RingOpenGL と RingAllegro の用法 (3D グラフィックス); その他の 3D サンプル
1091
1092 その他の 3D サンプル
1093 ====================
1094
1095 サンプルは ring/samples/3D フォルダにあります。
1096
1097 このスクリーンショットはトップダウン - 多層立方体のサンプルです。
1098
1099 .. image:: more3dsamples.jpg
1100         :alt: 3D サンプル
1101
1102 このスクリーンショットはカメラのサンプルです。
1103
1104 .. image:: more3dsamples2.jpg
1105         :alt: カメラ
1106
1107 このスクリーンショットはカメラと背景のサンプルです。
1108
1109 開発者 : アズディン・レマル
1110
1111 .. image:: cameraandbackground.png
1112         :alt: カメラと背景のサンプル
1113
1114