GLOBE.C
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:7k
源码类别:

Windows编程

开发平台:

Visual C++

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