ODE講座20:好きな姿勢に設定しよう! 19万アクセス達成
10 月 27
ODE texture ODE texture テクスチャを切り替えるサンプルプログラム チップさんから「プログラムに関する質問なのですが、テクスチャをたくさん使うにはどうすればいいのでしょうか?LOOPの中でfn.path_to_texturesのパスを変えてもテクスチャが変わらなくて悩んでいます。というご質問を頂きました今回のODE講座で回答します。 simloopの中でpath_to_texturesのパスを変えてもテクスチャは変わりません。 dsStartGraphics()を使う必要があります。ただし、drawstuff.hの95行目のdsSimulationLoop()の下に以下の一行を追加して、ode-0.9のフォルダでmake,make installし直してください。ode-0.8だとエラーが出るようです。 DS_API void dsStartGraphics(int window_width, int window_height, struct dsFunctions *fn); なお、シミュレーション中の複数の物体に違ったテクスチャを割り当てるためには、dsStartGraphics()を各物体を描画する前に、各物体毎に呼び出さなければいけず、描画速度が遅くなります。drawstuffのソースコードに手を加えるか、他の3Dグラフィクスライブラリを使用することをお勧めします。 そもそもdrawstuffはテストプログラムの表示用ライブラリなので凝ったことはできません。その代わりにソースも短くコードも複雑ではないのでOpenGLの勉強にもピッタリです。 以下に't'キーと'u'キーを押すとテクスチャが切り替わるサンプルプログラムを紹介しますので参考にしてください。ここからダウンロード可能です。
CODE:
  1. // texture.cpp: テクスチャの変更 by Kosei Demura (2007-10-25)
  2. #include
  3. #include
  4.  
  5. #ifdef dDOUBLE
  6. #define dsDrawBox      dsDrawBoxD
  7. #define dsDrawSphere   dsDrawSphereD
  8. #define dsDrawCylinder dsDrawCylinderD
  9. #define dsDrawCapsule  dsDrawCapsuleD
  10. #endif
  11.  
  12. static dWorldID world;
  13. static dSpaceID space;
  14. static dGeomID  capsule;
  15. static int texture_flag = 0;
  16.  
  17. dsFunctions fn;
  18.  
  19. static void simLoop(int pause)
  20. {
  21. const dReal *pos;
  22. const dReal *R;
  23. dReal r, l;
  24.  
  25. pos   = dGeomGetPosition(capsule);
  26. R     = dGeomGetRotation(capsule);
  27.  
  28. if (texture_flag == 0) dsSetTexture(DS_NONE);
  29. else                   dsSetTexture(DS_WOOD);
  30.  
  31. fn.path_to_textures = "./textures";
  32. dsSetColor(1.2, 1.2, 1.2);
  33. dGeomCapsuleGetParams(capsule, &r, &l);
  34. dsDrawCapsule(pos, R, l, r);
  35. }
  36.  
  37. void command(int cmd)
  38. {
  39. switch (cmd) {
  40. case ‘t’:
  41. fn.path_to_textures = "./textures";
  42. dsStartGraphics(640,480,&fn);
  43. texture_flag = 1;
  44. break;
  45. case ‘u’:
  46. fn.path_to_textures = "../../drawstuff/textures";
  47. dsStartGraphics(640,480,&fn);
  48. texture_flag = 0;
  49. break;
  50. }
  51. }
  52.  
  53. void start()
  54. {
  55. static float xyz[3] = {   3.0, 0.0, 1.0};
  56. static float hpr[3] = {-180.0, 0.0, 0.0};
  57. dsSetViewpoint(xyz, hpr);
  58. dsSetSphereQuality(3);
  59. }
  60.  
  61. void  setDrawStuff()
  62. {
  63. fn.version = DS_VERSION;
  64. fn.start   = &start;
  65. fn.step    = &simLoop;
  66. fn.command = &command;
  67. fn.stop    = NULL;
  68. fn.path_to_textures = "../../drawstuff/textures";
  69. }
  70.  
  71. // カプセルジオメトリの生成
  72. void makeCapsule()
  73. {
  74. dReal r = 0.1, l = 1.0;
  75.  
  76. capsule  =  dCreateCapsule(space, r, l);    // 直方体ジオメトリの生成
  77. dGeomSetPosition(capsule,   0, 0, 1);       // 位置の設定
  78. }
  79.  
  80. int main(int argc, char *argv[])
  81. {
  82. setDrawStuff();
  83. world = dWorldCreate();
  84. space = dHashSpaceCreate(0);
  85. makeCapsule();
  86. dsSimulationLoop(argc,argv,640,480,&fn);
  87. dWorldDestroy(world);
  88. return 0;
  89. }

投稿:07年10月27日

Leave a Reply

カウンタ (since 2008-1-11)
Copyright © 1998-2008    Kosei Demura