D20070825SdlOpenglPractice

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

下から選んでください:

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