D20070820SdlOpengl

  1. 1.http://www.tacoworks.jp/software/SDLdoc-jp/html/
  2. のをBaseとさせて頂き、Dで動かしてみました。
  3. 2.概ね、D対応のみ
  4.  
  5. /*
  6.  * SDL OpenGL チュートリアル.
  7.  * (c) Michael Vance, 2000
  8.  *
  9.  * LGPL の条件のもとで配布されています。
  10.  */
  11.  
  12. /* C
  13. #include <SDL/SDL.h>
  14. #include <GL/gl.h>
  15. #include <GL/glu.h>
  16.  
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. C */
  20. // D //////////////////////////////////////
  21. import SDL;
  22. import GL.gl;
  23. import GL.glu;
  24. import tango.stdc.stdio;
  25. //import tango.stdc.stdlib;
  26. //import tango.io.Console;
  27. //import tango.io.Stdout;
  28.  
  29. ////////////////////////////////////// D //
  30.  
  31. static GLboolean should_rotate = GL_TRUE;
  32.  
  33. static void quit_tutorial( int code )
  34. {
  35. /*
  36.   * SDL を終了してフルスクリーンモードを解放し、
  37.   * 以前のビデオ設定などを戻す。
  38.   */
  39. SDL_Quit( );
  40.  
  41. /* プログラムを終了する。*/
  42. exit( code );
  43. }
  44.  
  45. static void handle_key_down( SDL_keysym* keysym )
  46. {
  47.  
  48. /*
  49.   * 興味があるのは 'ESC' が押された時だけ。
  50.   *
  51.   * 練習:
  52.   * 矢印キーを処理し、表示位置・角度を変更するようにせよ。
  53.   */
  54. switch( keysym.sym ) {
  55. case SDLK_ESCAPE:
  56. quit_tutorial( 0 );
  57. break;
  58. case SDLK_SPACE:
  59. should_rotate = !should_rotate;
  60. break;
  61. default:
  62. break;
  63. }
  64.  
  65. }
  66.  
  67. static void process_events()
  68. {
  69. /* SDL イベントの置き場 */
  70. SDL_Event event;
  71.  
  72. /* すべてのイベントをキューからつかみ取る */
  73. while( SDL_PollEvent( &event ) ) {
  74.  
  75. switch( event.type ) {
  76. case SDL_KEYDOWN:
  77. /* キー押下を処理 */
  78. handle_key_down( &event.key.keysym );
  79. break;
  80. case SDL_QUIT:
  81. /* 終了要求 (Ctrl-c など) を処理 */
  82. quit_tutorial( 0 );
  83. break;
  84. //Add D :defaultが無いと不安で実行出来ないそうな
  85. default :
  86. break;
  87. }
  88.  
  89. }
  90.  
  91. }
  92.  
  93. static void draw_screen()
  94. {
  95. /* 回転角 */
  96. static float angle = 0.0f;
  97.  
  98. /*
  99.   * 練習:
  100.   * このひどいごみを頂点配列で置き換え、
  101.   * glDrawElements を呼び出せ。
  102.   *
  103.   * 練習:
  104.   * 上を終えた後、コンパイルされた頂点配列に変更せよ。
  105.   *
  106.   * 練習:
  107.   * 私の螺旋形状が正しいことを確認せよ。;)
  108.   */
  109. /*C static GLfloat v0[] = { -1.0f, -1.0f, 1.0f };
  110.   static GLfloat v1[] = { 1.0f, -1.0f, 1.0f };
  111.   static GLfloat v2[] = { 1.0f, 1.0f, 1.0f };
  112.   static GLfloat v3[] = { -1.0f, 1.0f, 1.0f };
  113.   static GLfloat v4[] = { -1.0f, -1.0f, -1.0f };
  114.   static GLfloat v5[] = { 1.0f, -1.0f, -1.0f };
  115.   static GLfloat v6[] = { 1.0f, 1.0f, -1.0f };
  116.   static GLfloat v7[] = { -1.0f, 1.0f, -1.0f };
  117.   static GLubyte red[] = { 255, 0, 0, 255 };
  118.   static GLubyte green[] = { 0, 255, 0, 255 };
  119.   static GLubyte blue[] = { 0, 0, 255, 255 };
  120.   static GLubyte white[] = { 255, 255, 255, 255 };
  121.   static GLubyte yellow[] = { 0, 255, 255, 255 };
  122.   static GLubyte black[] = { 0, 0, 0, 255 };
  123.   static GLubyte orange[] = { 255, 255, 0, 255 };
  124.   static GLubyte purple[] = { 255, 0, 255, 0 };
  125. C*/
  126. // D ////////////////////////////////////////
  127. static GLfloat[] v0 = [ -1.0, -1.0, 1.0 ];
  128. static GLfloat v1[] = [ 1.0f, -1.0f, 1.0f ];
  129. static GLfloat v2[] = [ 1.0f, 1.0f, 1.0f ];
  130. static GLfloat v3[] = [ -1.0f, 1.0f, 1.0f ];
  131. static GLfloat v4[] = [ -1.0f, -1.0f, -1.0f ];
  132. static GLfloat v5[] = [ 1.0f, -1.0f, -1.0f ];
  133. static GLfloat v6[] = [ 1.0f, 1.0f, -1.0f ];
  134. static GLfloat v7[] = [ -1.0f, 1.0f, -1.0f ];
  135.  
  136. static GLubyte red[] = [ 255, 0, 0, 255 ];
  137.  
  138.  
  139. static GLubyte green[] = [ 0, 255, 0, 255 ];
  140. static GLubyte blue[] = [ 0, 0, 255, 255 ];
  141. static GLubyte white[] = [ 255, 255, 255, 255 ];
  142. static GLubyte yellow[] = [ 0, 255, 255, 255 ];
  143. static GLubyte black[] = [ 0, 0, 0, 255 ];
  144. static GLubyte orange[] = [ 255, 255, 0, 255 ];
  145. static GLubyte purple[] = [ 255, 0, 255, 0 ];
  146. //////////////////////////////////////// D //
  147.  
  148. /* 色・デプスバッファを消去 */
  149. glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  150.  
  151. /* 射影行列は変更したくない */
  152. glMatrixMode( GL_MODELVIEW );
  153. glLoadIdentity( );
  154.  
  155. /* z 軸の方向に下げる */
  156. glTranslatef( 0.0, 0.0, -5.0 );
  157.  
  158. /* 回転 */
  159. glRotatef( angle, 0.0, 1.0, 0.0 );
  160.  
  161. if( should_rotate ) {
  162.  
  163. if( ++angle > 360.0f ) {
  164. angle = 0.0f;
  165. }
  166.  
  167. }
  168.  
  169. /* 三角形データをパイプラインに送る */
  170. glBegin( GL_TRIANGLES );
  171.  
  172. /* c
  173.   glColor4ubv(red);
  174.   glVertex3fv( v0 );
  175.   glColor4ubv( green );
  176.   glVertex3fv( v1 );
  177.   glColor4ubv( blue );
  178.   glVertex3fv( v2 );
  179.  
  180.   glColor4ubv( red );
  181.   glVertex3fv( v0 );
  182.   glColor4ubv( blue );
  183.   glVertex3fv( v2 );
  184.   glColor4ubv( white );
  185.   glVertex3fv( v3 );
  186.  
  187.   glColor4ubv( green );
  188.   glVertex3fv( v1 );
  189.   glColor4ubv( black );
  190.   glVertex3fv( v5 );
  191.   glColor4ubv( orange );
  192.   glVertex3fv( v6 );
  193.  
  194.   glColor4ubv( green );
  195.   glVertex3fv( v1 );
  196.   glColor4ubv( orange );
  197.   glVertex3fv( v6 );
  198.   glColor4ubv( blue );
  199.   glVertex3fv( v2 );
  200.  
  201.   glColor4ubv( black );
  202.   glVertex3fv( v5 );
  203.   glColor4ubv( yellow );
  204.   glVertex3fv( v4 );
  205.   glColor4ubv( purple );
  206.   glVertex3fv( v7 );
  207.  
  208.   glColor4ubv( black );
  209.   glVertex3fv( v5 );
  210.   glColor4ubv( purple );
  211.   glVertex3fv( v7 );
  212.   glColor4ubv( orange );
  213.   glVertex3fv( v6 );
  214.  
  215.   glColor4ubv( yellow );
  216.   glVertex3fv( v4 );
  217.   glColor4ubv( red );
  218.   glVertex3fv( v0 );
  219.   glColor4ubv( white );
  220.   glVertex3fv( v3 );
  221.  
  222.   glColor4ubv( yellow );
  223.   glVertex3fv( v4 );
  224.   glColor4ubv( white );
  225.   glVertex3fv( v3 );
  226.   glColor4ubv( purple );
  227.   glVertex3fv( v7 );
  228.  
  229.   glColor4ubv( white );
  230.   glVertex3fv( v3 );
  231.   glColor4ubv( blue );
  232.   glVertex3fv( v2 );
  233.   glColor4ubv( orange );
  234.   glVertex3fv( v6 );
  235.  
  236.   glColor4ubv( white );
  237.   glVertex3fv( v3 );
  238.   glColor4ubv( orange );
  239.   glVertex3fv( v6 );
  240.   glColor4ubv( purple );
  241.   glVertex3fv( v7 );
  242.  
  243.   glColor4ubv( green );
  244.   glVertex3fv( v1 );
  245.   glColor4ubv( red );
  246.   glVertex3fv( v0 );
  247.   glColor4ubv( yellow );
  248.   glVertex3fv( v4 );
  249.  
  250.   glColor4ubv( green );
  251.   glVertex3fv( v1 );
  252.   glColor4ubv( yellow );
  253.   glVertex3fv( v4 );
  254.   glColor4ubv( black );
  255.   glVertex3fv( v5 );
  256. C */
  257. // D //////////////////////
  258. glColor4ubv(red.ptr);
  259. glVertex3fv( v0.ptr );
  260. glColor4ubv( green.ptr );
  261. glVertex3fv( v1.ptr );
  262. glColor4ubv( blue.ptr );
  263. glVertex3fv( v2.ptr );
  264.  
  265. glColor4ubv( red.ptr );
  266. glVertex3fv( v0.ptr );
  267. glColor4ubv( blue.ptr );
  268. glVertex3fv( v2.ptr );
  269. glColor4ubv( white.ptr );
  270. glVertex3fv( v3.ptr );
  271.  
  272. glColor4ubv( green.ptr );
  273. glVertex3fv( v1.ptr );
  274. glColor4ubv( black.ptr );
  275. glVertex3fv( v5.ptr );
  276. glColor4ubv( orange.ptr );
  277. glVertex3fv( v6.ptr );
  278.  
  279. glColor4ubv( green.ptr );
  280. glVertex3fv( v1.ptr );
  281. glColor4ubv( orange.ptr );
  282. glVertex3fv( v6.ptr );
  283. glColor4ubv( blue.ptr );
  284. glVertex3fv( v2.ptr );
  285.  
  286. glColor4ubv( black.ptr );
  287. glVertex3fv( v5.ptr );
  288. glColor4ubv( yellow.ptr );
  289. glVertex3fv( v4.ptr );
  290. glColor4ubv( purple.ptr );
  291. glVertex3fv( v7.ptr );
  292.  
  293. glColor4ubv( black.ptr );
  294. glVertex3fv( v5.ptr );
  295. glColor4ubv( purple.ptr );
  296. glVertex3fv( v7.ptr );
  297. glColor4ubv( orange.ptr );
  298. glVertex3fv( v6.ptr );
  299.  
  300. glColor4ubv( yellow.ptr );
  301. glVertex3fv( v4.ptr );
  302. glColor4ubv( red.ptr );
  303. glVertex3fv( v0.ptr );
  304. glColor4ubv( white.ptr );
  305. glVertex3fv( v3.ptr );
  306.  
  307. glColor4ubv( yellow.ptr );
  308. glVertex3fv( v4.ptr );
  309. glColor4ubv( white.ptr );
  310. glVertex3fv( v3.ptr );
  311. glColor4ubv( purple.ptr );
  312. glVertex3fv( v7.ptr );
  313.  
  314. glColor4ubv( white.ptr );
  315. glVertex3fv( v3.ptr );
  316. glColor4ubv( blue.ptr );
  317. glVertex3fv( v2.ptr );
  318. glColor4ubv( orange.ptr );
  319. glVertex3fv( v6.ptr );
  320.  
  321. glColor4ubv( white.ptr );
  322. glVertex3fv( v3.ptr );
  323. glColor4ubv( orange.ptr );
  324. glVertex3fv( v6.ptr );
  325. glColor4ubv( purple.ptr );
  326. glVertex3fv( v7.ptr );
  327.  
  328. glColor4ubv( green.ptr );
  329. glVertex3fv( v1.ptr );
  330. glColor4ubv( red.ptr );
  331. glVertex3fv( v0.ptr );
  332. glColor4ubv( yellow.ptr );
  333. glVertex3fv( v4.ptr );
  334.  
  335. glColor4ubv( green.ptr );
  336. glVertex3fv( v1.ptr );
  337. glColor4ubv( yellow.ptr );
  338. glVertex3fv( v4.ptr );
  339. glColor4ubv( black.ptr );
  340. glVertex3fv( v5.ptr );
  341.  
  342. glEnd( );
  343.  
  344. /*
  345.   * 練習:
  346.   * 'Spc' で回転停止、'Esc' で終了することを
  347.   * ユーザーに教えるテキストを描画せよ。
  348.   * ベクターとテクスチャが貼られた四角形で行え。
  349.   */
  350.  
  351. /*
  352.   * バッファを交換する。これはバックバッファからの
  353.   * 次のフレームの描画と、
  354.   * フロントバッファであったものに起こる
  355.   * すべての描画操作の設定をドライバに通知する。
  356.   *
  357.   * ダブルバッファによって、
  358.   * 更新中の画面領域へアプリケーションが同時に描画することから起きる、
  359.   * 表示の乱れが防止される。
  360.   */
  361. SDL_GL_SwapBuffers( );
  362. }
  363.  
  364. static void setup_opengl( int width, int height )
  365. {
  366. float ratio = cast(float) width / cast(float) height;
  367.  
  368. /* シェーディングモデルは Gouraud (なめらか) */
  369. glShadeModel( GL_SMOOTH );
  370.  
  371. /* 裏面を取り除く */
  372. glCullFace( GL_BACK );
  373. glFrontFace( GL_CCW );
  374. glEnable( GL_CULL_FACE );
  375.  
  376. /* 消去時の色をセット */
  377. glClearColor( 0, 0, 0, 0 );
  378.  
  379. /* ビューポートを設定 */
  380. glViewport( 0, 0, width, height );
  381.  
  382. /*
  383.   * 射影行列を変更し、ビューボリュームにセット。
  384.   */
  385. glMatrixMode( GL_PROJECTION );
  386. glLoadIdentity( );
  387. /*
  388.   * 練習:
  389.   * これを glFrustum の呼び出しに置き換えよ。
  390.   */
  391. gluPerspective( 60.0, ratio, 1.0, 1024.0 );
  392. }
  393.  
  394. int main( char[][] arg )
  395. {
  396. /*
  397. FILE *fpLog
  398. fpLog = fopen("LogDebug.txt","w")
  399.  
  400. fwprintf(fpLog,"start")
  401. */
  402.  
  403. /*
  404. auto char[] inFile = ".sample.txt";
  405.   printf("targetFile=\"%.*s\"\n",inFile);
  406. */
  407.  
  408. /* 現在のビデオ設定についての情報 */
  409. //C const SDL_VideoInfo* info = NULL;
  410.  
  411. SDL_VideoInfo* info = null;
  412.  
  413. /* ウィンドウの寸法 */
  414. int width = 0;
  415. int height = 0;
  416. /* ウィンドウの色のピクセル深度 */
  417. int bpp = 0;
  418. /* SDL_SetVideoMode に渡すフラグ */
  419. int flags = 0;
  420.  
  421. /* まず、SDL ビデオサブシステムを初期化 */
  422. if( SDL_Init( SDL_INIT_VIDEO ) < 0 ) {
  423. /* 失敗したので終了 */
  424. fprintf( stderr, "ビデオの初期化に失敗しました: %s\n",
  425. SDL_GetError( ) );
  426. quit_tutorial( 1 );
  427. }
  428.  
  429.  
  430. /* いくつかのビデオ情報を取得しよう */
  431. //C info = SDL_GetVideoInfo( );
  432. info = SDL_GetVideoInfo();
  433.  
  434. if( !info ) {
  435. /* おそらくこれは絶対に起きないはず */
  436. fprintf( stderr, "ビデオの問い合わせに失敗しました: %s\n",
  437. SDL_GetError( ) );
  438. quit_tutorial( 1 );
  439. }
  440.  
  441. /*
  442.   * 横幅/高さを 640/480 にセット。
  443.   * (もちろん普通のアプリケーションにおいて
  444.   * ユーザーにこれを決定してもらうだろう)
  445.   * 画面から要求していたピクセル深度を取得。
  446.   * X11 では、VidMode は解像度を変えることができないため、
  447.   * これはおそらく過度安全だろう。
  448.   * Win32 では、ChangeDisplaySettings によって
  449.   * ピクセル深度を変えることができる。
  450.   */
  451. width = 640;
  452. height = 480;
  453. bpp = info.vfmt.BitsPerPixel;
  454.  
  455. /*
  456.   * ここで、OpenGL ウィンドウのために要求されたウィンドウ属性を設定したい。
  457.   * RGB 各チャンネルに *少なくとも* 5 ビット欲しい。
  458.   * また、少なくとも 16 ビットのデプスバッファも欲しい。
  459.   *
  460.   * 最後にする事はダブルバッファウィンドウの要求である。
  461.   * '1' でダルバッファが有効になり、
  462.   * '0' で無効になる。
  463.   *
  464.   * SDL_SetVideoMode へのフラグにおいて
  465.   * SDL_DOUBLEBUF を使わない事に注意。
  466.   * それは GL アトリビュートに影響せず、
  467.   * 標準の 2D blit 転送の設定だけに影響する。
  468.   */
  469. SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 );
  470. SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 );
  471. SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 );
  472. SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 );
  473. SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 );
  474.  
  475. /*
  476.   * SDL がフルスクリーンビデオモードにおいて
  477.   * OpenGL ウィンドウを提供してくれるよう要求したい。
  478.   *
  479.   * 練習:
  480.   * ウィンドウ状態で開始するオプションを作り、
  481.   * glViewPort でリサイズイベントを適切に処理せよ。
  482.   */
  483. //Practice flags = SDL_OPENGL | SDL_FULLSCREEN;
  484. //窓
  485. flags = SDL_OPENGL | SDL_SWSURFACE;
  486.  
  487.  
  488. /*
  489.   * Set the video mode
  490.   */
  491. //C if( SDL_SetVideoMode( width, height, bpp, flags ) == 0 ) {
  492. if( SDL_SetVideoMode( width, height, bpp, flags ) == null ) {
  493. /*
  494.   * これはさまざまな理由で起き得る。
  495.   * DISPLAY が設定されていない、
  496.   * 指定された解像度が利用可能でない、など。
  497.   */
  498. fprintf( stderr, "ビデオモードのセットに失敗しました: %s\n",
  499. SDL_GetError( ) );
  500. quit_tutorial( 1 );
  501. }
  502.  
  503.  
  504.  
  505. /*
  506.   * ここで、OpenGL の使用のために
  507.   * ダブルバッファのウィンドウを適切に設定したはず。
  508.   */
  509. setup_opengl( width, height );
  510.  
  511. /*
  512.   * さて、通常のアプリケーション処理 -- たくさんの再描画と
  513.   * イベントループを始めたい。
  514.   */
  515. while( 1 ) {
  516. /* やってくるイベントを処理 */
  517. process_events( );
  518. /* 画面を表示 */
  519. draw_screen( );
  520. }
  521.  
  522. /*
  523.   * 練習:
  524.   * SDL_GetTicks() を使ってタイミングを記録し、
  525.   * プログラム終了時に 1 秒間のフレームを表示せよ。
  526.   */
  527.  
  528. /* ここには届かない */
  529. return 0;
  530. }
最終更新:2007年08月23日 14:06
ツールボックス

下から選んでください:

新しいページを作成する
ヘルプ / FAQ もご覧ください。