home *** CD-ROM | disk | FTP | other *** search
/ VRML Tools for 3D Cyberspace / VRML_Tools_For_3D_Cyberspace.iso / amber / demos / piano / demo.cpp next >
C/C++ Source or Header  |  1996-07-01  |  2KB  |  147 lines

  1. #define DEMO_OBJS
  2.  
  3. #include <windows.h>
  4. #include "amber.hpp"
  5. #include "demo.hpp"
  6. #include "intxmsg.hpp"
  7.  
  8. #define X 0
  9. #define Y 1
  10. #define Z 2
  11.  
  12. int channel=0, lastChannel=0, lastNote=0, patch=0;
  13. void keyBounce(void *obj)
  14. {
  15.     geometryClass *geo = (geometryClass *)obj;
  16.     quatClass q;
  17.     V3 euler;
  18.  
  19.     geo->getOrientation(q);
  20.     q.getEuler(euler);
  21.  
  22.     if (euler[0] <= 0.0) return;
  23.  
  24.     if (euler[0] <= 0.018) {
  25.         q.init();
  26.         geo->setOrientation(q);
  27.     }
  28.     else {
  29.         geo->rotate(0, -0.018);
  30.     }
  31.  
  32. }
  33. void init(channelClass *ch)
  34. {
  35.     int i;
  36.     V3 ext, piv, pos;
  37.     geometryClass *g;
  38.  
  39.     keys = new geometryClass("piano.wrl");
  40.     keys->setCollisionState(TRUE,TRUE,TRUE);
  41.     audio = new audioClass();
  42.     audio->midiOpenFX();
  43.  
  44.     for (i=3; i<28; i++) {
  45.         g = amber->getGeometry(i);
  46.         g->getExtents(ext);
  47.         g->getPosition(pos);
  48.         piv[0] = pos[0];
  49.         piv[1] = pos[1];
  50.         piv[2] = pos[2] - ext[2]/2.0;
  51.         g->setPivot(piv);
  52.         g->addAction(keyBounce);
  53.  
  54.     }
  55.  
  56.     keys->getHierarchyBounds(pos, ext);
  57.     pos[2] += 12;
  58.     pos[1] += 5;
  59.     ch->rotate(0, 0.34);
  60.     ch->setPosition(pos);
  61. }
  62.  
  63. void cleanup()
  64. {
  65.     delete audio;
  66. }
  67.  
  68. void processChar(unsigned int nChar)
  69. {
  70.     intxMsgClass *msg;
  71.     int id, note;
  72.     quatClass q;
  73.     
  74.     switch(nChar) {
  75.         case 'M':
  76.             patch++;
  77.             if (patch>127) patch=0;
  78.             audio->midiChangePatch(0, patch);
  79.             break;
  80.         case '0':
  81.             audio->midiChangePatch(0, 0);
  82.             //channel=0;
  83.             break;
  84.         case '1':
  85.             //channel=1;
  86.             audio->midiChangePatch(0, 9);
  87.             break;
  88.         case '2':
  89.             channel=2;
  90.             break;
  91.         case '3':
  92.             channel=3;
  93.             break;
  94.         case '4':
  95.             channel=4;
  96.             break;
  97.         case '5':
  98.             channel=5;
  99.             break;
  100.         case '6':
  101.             channel=6;
  102.             break;
  103.         case '7':
  104.             channel=7;
  105.             break;
  106.         case '8':
  107.             channel=8;
  108.             break;
  109.         case '9':
  110.             channel=9;
  111.             break;
  112.         case 'A':
  113.             channel=10;
  114.             break;
  115.         case 'B':
  116.             channel=11;
  117.             break;
  118.         case 'C':
  119.             channel=12;
  120.             break;
  121.         case 'D':
  122.             channel=13;
  123.             break;
  124.         case 'E':
  125.             channel=14;
  126.             break;
  127.         case 'F':
  128.             channel=15;
  129.             break;
  130.         case ' ':
  131.             msg = ch->pick(mouse->pos);
  132.             if (msg) {
  133.                 id = msg->iGeo->getId();
  134.                 note = 50 + id;
  135.                 audio->midiNoteOff(lastChannel, lastNote, 127);
  136.                 audio->midiNoteOn(channel, note, 127);
  137.                 lastChannel=channel;
  138.                 lastNote=note;
  139.                 q.rotate(0, 0.09);
  140.                 msg->iGeo->setOrientation(q);
  141.                 delete msg;
  142.                 break;
  143.             }
  144.     }
  145. }
  146.  
  147.