home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / sdk / graphics / directx / globe / globe.c next >
C/C++ Source or Header  |  1997-07-14  |  8KB  |  226 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995, 1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File: globe.c
  6.  *
  7.  ***************************************************************************/
  8.  
  9. #include "rmdemo.h"
  10.  
  11. D3DRMRENDERQUALITY quality = D3DRMRENDER_GOURAUD;
  12.  
  13. LPDIRECT3DRMANIMATION anim;
  14. int motion_points = 17;
  15.  
  16. D3DVECTOR motion[17] =
  17. {{-D3DVAL(14), D3DVAL(4), D3DVAL(45)},
  18. {D3DVAL(9), -D3DVAL(3), D3DVAL(36)},
  19. {D3DVAL(5), D3DVAL(0), D3DVAL(5)},
  20. {-D3DVAL(5), D3DVAL(8), D3DVAL(25)},
  21. {D3DVAL(0), D3DVAL(3), D3DVAL(20)},
  22. {-D3DVAL(3), -D3DVAL(4), D3DVAL(10)},
  23. {D3DVAL(8), D3DVAL(10), D3DVAL(15)},
  24. {D3DVAL(16), D3DVAL(0), D3DVAL(30)},
  25. {D3DVAL(10), -D3DVAL(4), D3DVAL(42)},
  26. {-D3DVAL(15), D3DVAL(0), D3DVAL(37)},
  27. {-D3DVAL(5), -D3DVAL(7), D3DVAL(15)},
  28. {D3DVAL(5), D3DVAL(5), D3DVAL(20)},
  29. {D3DVAL(5), D3DVAL(10), D3DVAL(30)},
  30. {D3DVAL(13), D3DVAL(8), D3DVAL(50)},
  31. {D3DVAL(0), D3DVAL(8), D3DVAL(25)},
  32. {D3DVAL(0), D3DVAL(0), D3DVAL(20)},
  33. {-D3DVAL(14), D3DVAL(4), D3DVAL(45)}
  34. };
  35.  
  36. static void CDECL cleanupPath(LPDIRECT3DRMOBJECT obj, void* arg)
  37. {
  38.     anim->lpVtbl->Release(anim);
  39. }
  40.  
  41. static void CDECL moveCamera(LPDIRECT3DRMFRAME frame, void *arg, D3DVALUE delta)
  42. {
  43.     static D3DVALUE t = D3DVAL(0.0);
  44.     LPDIRECT3DRMFRAME world_frame = (LPDIRECT3DRMFRAME) arg;
  45.     LPDIRECT3DRMFRAME scene;
  46.  
  47.     frame->lpVtbl->GetScene(frame, &scene);
  48.  
  49.     t += D3DVAL(0.08);
  50.     anim->lpVtbl->SetFrame(anim, frame);
  51.     anim->lpVtbl->SetTime(anim, t);
  52.  
  53.     frame->lpVtbl->LookAt(frame, world_frame, scene, D3DRMCONSTRAIN_Z);
  54.  
  55.     scene->lpVtbl->Release(scene);
  56. }
  57.  
  58. /*
  59.  * world_scene
  60.  */
  61. BOOL
  62. run_world_scene(LPDIRECT3DRMFRAME scene, LPDIRECT3DRMFRAME camera)
  63. {
  64.     LPDIRECT3DRMFRAME light1 = NULL;
  65.     LPDIRECT3DRMFRAME world_frame = NULL;
  66.     LPDIRECT3DRMLIGHT l1 = NULL;
  67.     LPDIRECT3DRMLIGHT l2 = NULL;
  68.     LPDIRECT3DRMTEXTURE tex = NULL;
  69.     LPDIRECT3DRMWRAP wrap = NULL;
  70.     LPDIRECT3DRMMESHBUILDER sphere3_builder = NULL;
  71.     LPDIRECT3DRMMATERIAL mat = NULL;
  72.     HRESULT rval;
  73.     D3DRMBOX box;
  74.     D3DVALUE miny, maxy, height;
  75.  
  76.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, scene, &light1)))
  77.     goto generic_error;
  78.     if (FAILED(light1->lpVtbl->SetPosition(light1, scene, D3DVAL(2), D3DVAL(0.0), D3DVAL(22))))
  79.     goto generic_error;
  80.     if (FAILED(lpD3DRM->lpVtbl->CreateLightRGB(lpD3DRM, D3DRMLIGHT_DIRECTIONAL, D3DVAL(0.9),
  81.                                   D3DVAL(0.9), D3DVAL(0.9), &l1)))
  82.                   goto generic_error;
  83.     if (FAILED(light1->lpVtbl->AddLight(light1, l1)))
  84.     goto generic_error;
  85.     if (FAILED(lpD3DRM->lpVtbl->CreateLightRGB(lpD3DRM, D3DRMLIGHT_AMBIENT, D3DVAL(0.1),
  86.                                   D3DVAL(0.1), D3DVAL(0.1), &l2)))
  87.                   goto generic_error;
  88.     if (FAILED(scene->lpVtbl->AddLight(scene, l2)))
  89.     goto generic_error;
  90.     if (FAILED(camera->lpVtbl->SetPosition(camera, scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0))))
  91.     goto generic_error;
  92.     if (FAILED(camera->lpVtbl->SetOrientation(camera, scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(1), D3DVAL(0.0),
  93.                         D3DVAL(1), D3DVAL(0.0))))
  94.                         goto generic_error;
  95.  
  96.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, scene, &world_frame)))
  97.     goto generic_error;
  98.     if (FAILED(world_frame->lpVtbl->SetPosition(world_frame, scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(30))))
  99.     goto generic_error;
  100.     if (FAILED(world_frame->lpVtbl->SetOrientation(world_frame, scene, D3DVAL(0.0), D3DVAL(0.0), D3DVAL(1),
  101.                                         D3DVAL(0.0), D3DVAL(1), D3DVAL(0.0))))
  102.                     goto generic_error;
  103.     if (FAILED(world_frame->lpVtbl->SetRotation(world_frame, scene, D3DVAL(0.03), 
  104.                                      D3DVAL(0.1), D3DVAL(0.0), D3DVAL(0.1))))
  105.                      goto generic_error;
  106.  
  107.     if (FAILED(lpD3DRM->lpVtbl->CreateMeshBuilder(lpD3DRM, &sphere3_builder)))
  108.     goto generic_error;
  109.     rval = sphere3_builder->lpVtbl->Load(sphere3_builder, "sphere3.x",
  110.                         NULL, D3DRMLOAD_FROMFILE, NULL, NULL);
  111.     if (rval != D3DRM_OK) {
  112.     Msg("Failed to load sphere3.x.\n%s", D3DRMErrorToString(rval));
  113.     goto ret_with_error;
  114.     }
  115.     if (FAILED(lpD3DRM->lpVtbl->CreateMaterial(lpD3DRM, D3DVAL(20.0), &mat)))
  116.     goto generic_error;
  117.     if (FAILED(sphere3_builder->lpVtbl->SetMaterial(sphere3_builder, mat)))
  118.     goto generic_error;
  119.     mat->lpVtbl->Release(mat); mat = NULL;
  120.     if (FAILED(sphere3_builder->lpVtbl->Scale(sphere3_builder,
  121.                        D3DVAL(2), D3DVAL(2), D3DVAL(2))))
  122.                    goto generic_error;
  123.  
  124.     if (FAILED(sphere3_builder->lpVtbl->SetColorRGB(sphere3_builder, D3DVAL(1), D3DVAL(1), D3DVAL(1))))
  125.     goto generic_error;
  126.     if (FAILED(sphere3_builder->lpVtbl->GetBox(sphere3_builder, &box)))
  127.     goto generic_error;
  128.     maxy = box.max.y;
  129.     miny = box.min.y;
  130.     height = maxy - miny;
  131.  
  132.     if (FAILED(lpD3DRM->lpVtbl->CreateWrap
  133.     (lpD3DRM, D3DRMWRAP_CYLINDER, NULL,
  134.      D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0),
  135.      D3DVAL(0.0), D3DVAL(1.0), D3DVAL(0.0),
  136.      D3DVAL(0.0), D3DVAL(0.0), D3DVAL(1.0),
  137.      D3DVAL(0.0), D3DDivide(miny, height),
  138.      D3DVAL(1.0), D3DDivide(-D3DVAL(1.0), height),
  139.      &wrap
  140.     )))
  141.     goto generic_error;
  142.     if (FAILED(wrap->lpVtbl->Apply(wrap, (LPDIRECT3DRMOBJECT) sphere3_builder)))
  143.     goto generic_error;
  144.  
  145.     rval = lpD3DRM->lpVtbl->LoadTexture(lpD3DRM, "tex1.ppm", &tex);
  146.     if (rval != D3DRM_OK) {
  147.     Msg("Failed to load tex1.ppm\n%s", D3DRMErrorToString(rval));
  148.     goto ret_with_error;
  149.     }
  150.     if (FAILED(tex->lpVtbl->SetShades(tex, 32)))
  151.     goto generic_error;
  152.     if (FAILED(sphere3_builder->lpVtbl->SetTexture(sphere3_builder, tex)))
  153.     goto generic_error;
  154.  
  155.     if (FAILED(world_frame->lpVtbl->AddVisual(world_frame, (LPDIRECT3DRMVISUAL) sphere3_builder)))
  156.     goto generic_error;
  157.  
  158.     if (FAILED(camera->lpVtbl->AddMoveCallback(camera, moveCamera, 
  159.                     (void *) world_frame)))
  160.                     goto generic_error;
  161.     if (FAILED(camera->lpVtbl->AddDestroyCallback(camera, cleanupPath, NULL)))
  162.     goto generic_error;
  163.  
  164.     RELEASE(light1);
  165.     RELEASE(world_frame);
  166.     RELEASE(sphere3_builder);
  167.     RELEASE(l1);
  168.     RELEASE(l2);
  169.     RELEASE(tex);
  170.     RELEASE(wrap);
  171.     return TRUE;
  172. generic_error:
  173.     Msg("An error occurred while building the scene.\n");
  174. ret_with_error:
  175.     RELEASE(light1);
  176.     RELEASE(world_frame);
  177.     RELEASE(l1);
  178.     RELEASE(l2);
  179.     RELEASE(tex);
  180.     RELEASE(wrap);
  181.     RELEASE(sphere3_builder);
  182.     RELEASE(mat);
  183.     return FALSE;
  184. }
  185.  
  186. BOOL
  187. BuildScene(LPDIRECT3DRMDEVICE dev, LPDIRECT3DRMVIEWPORT view,
  188.        LPDIRECT3DRMFRAME scene, LPDIRECT3DRMFRAME camera)
  189. {
  190.     int i;
  191.  
  192.     view = view;        /* not used */
  193.  
  194.     /*
  195.      * This example flys a camera around a globe. The path taken is
  196.      * defined by a spline curve.
  197.      */
  198.  
  199.  
  200.     if (FAILED(dev->lpVtbl->SetQuality(dev, quality)))
  201.     goto generic_error;
  202.  
  203.     if (FAILED(lpD3DRM->lpVtbl->CreateAnimation(lpD3DRM, &anim)))
  204.     goto generic_error;
  205.     if (FAILED(anim->lpVtbl->SetOptions(anim, D3DRMANIMATION_CLOSED | D3DRMANIMATION_LINEARPOSITION | D3DRMANIMATION_POSITION)))
  206.     goto generic_error;
  207.     for (i = 0; i < motion_points; i++) {
  208.         if (FAILED(anim->lpVtbl->AddPositionKey(anim, D3DVAL(i), motion[i].x, motion[i].y,
  209.                              motion[i].z/2)))
  210.                  goto generic_error;
  211.     }
  212.     if (!run_world_scene(scene, camera))
  213.     goto ret_with_error;
  214.     return TRUE;
  215. generic_error:
  216.     Msg("A failure occurred while building the scene.\n");
  217. ret_with_error:
  218.     return FALSE;
  219. }
  220.  
  221. void
  222. OverrideDefaults(Defaults* defaults)
  223. {
  224.     lstrcpy(defaults->Name, "Globe Direct3DRM Example");
  225. }
  226.