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 / hier1 / hier1.c next >
C/C++ Source or Header  |  1997-07-14  |  7KB  |  200 lines

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995, 1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File: hier1.c
  6.  *
  7.  ***************************************************************************/
  8.  
  9. /*
  10.  * RL Tutorial program 2 frame hierarchy
  11.  */
  12.  
  13. #include "rmdemo.h"
  14.  
  15. BOOL
  16. BuildScene(LPDIRECT3DRMDEVICE dev, LPDIRECT3DRMVIEWPORT view,
  17.        LPDIRECT3DRMFRAME scene, LPDIRECT3DRMFRAME camera)
  18. {
  19.     D3DRMRENDERQUALITY quality = D3DRMRENDER_FLAT;
  20.     LPDIRECT3DRMFRAME lights = NULL;
  21.     LPDIRECT3DRMFRAME axis = NULL;
  22.     LPDIRECT3DRMMESH torus_mesh = NULL;
  23.     LPDIRECT3DRMMESH sphere_mesh = NULL;
  24.     LPDIRECT3DRMFRAME torus = NULL;
  25.     LPDIRECT3DRMFRAME sphere = NULL;
  26.     LPDIRECT3DRMLIGHT lp = NULL;
  27.     LPDIRECT3DRMLIGHT la = NULL;
  28.     LPDIRECT3DRMMESHBUILDER builder = NULL;
  29.     HRESULT rval;
  30.  
  31.     view = view;        /* not used */
  32.  
  33.     /*
  34.      * This Demo shows a simple hierarchy of frames
  35.      */
  36.  
  37.     if (FAILED(dev->lpVtbl->SetQuality(dev, quality)))
  38.     goto generic_error;
  39.  
  40.     /*
  41.      * initialize the lights in the scene
  42.      */
  43.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, scene, &lights)))
  44.     goto generic_error;
  45.     if (FAILED(lights->lpVtbl->SetPosition(lights, scene, D3DVAL(5), D3DVAL(5), 
  46.                                 -D3DVAL(9))))
  47.                 goto generic_error;
  48.     if (FAILED(lpD3DRM->lpVtbl->CreateLightRGB(lpD3DRM, D3DRMLIGHT_POINT, D3DVAL(0.9), 
  49.                                   D3DVAL(0.8), D3DVAL(0.7), &lp)))
  50.                   goto generic_error;
  51.     if (FAILED(lights->lpVtbl->AddLight(lights, lp)))
  52.     goto generic_error;
  53.  
  54.     if (FAILED(lpD3DRM->lpVtbl->CreateLightRGB(lpD3DRM, D3DRMLIGHT_AMBIENT, D3DVAL(0.1), 
  55.                                   D3DVAL(0.1), D3DVAL(0.1), &la)))
  56.                   goto generic_error;
  57.     if (FAILED(scene->lpVtbl->AddLight(scene, la)))
  58.     goto generic_error;
  59.  
  60.  
  61.     /*
  62.      * load mesh files
  63.      */
  64.     if (FAILED(lpD3DRM->lpVtbl->CreateMeshBuilder(lpD3DRM, &builder)))
  65.     goto generic_error;
  66.     rval = builder->lpVtbl->Load(builder, "torus.x", NULL,
  67.                 D3DRMLOAD_FROMFILE, NULL, NULL);
  68.     if (rval != D3DRM_OK) {
  69.         Msg("Failed to load torus.x\n%s", D3DRMErrorToString(rval));
  70.     goto ret_with_error;
  71.     }
  72.     if (FAILED(builder->lpVtbl->CreateMesh(builder, &torus_mesh)))
  73.     goto generic_error;
  74.     RELEASE(builder);
  75.  
  76.     if (FAILED(lpD3DRM->lpVtbl->CreateMeshBuilder(lpD3DRM, &builder)))
  77.     goto generic_error;
  78.     rval = builder->lpVtbl->Load(builder, "sphere2.x", NULL,
  79.                     D3DRMLOAD_FROMFILE, NULL, NULL);
  80.     if (rval != D3DRM_OK) {
  81.         Msg("Failed to load sphere2.x\n%s", D3DRMErrorToString(rval));
  82.     goto ret_with_error;
  83.     }
  84.     if (FAILED(builder->lpVtbl->CreateMesh(builder, &sphere_mesh)))
  85.     goto generic_error;
  86.     RELEASE(builder);
  87.  
  88.     if (FAILED(torus_mesh->lpVtbl->SetGroupColorRGB(torus_mesh, -1, D3DVAL(0.0), 
  89.                                             D3DVAL(0.0), D3DVAL(1.0))))
  90.                         goto generic_error;
  91.  
  92.     /*
  93.      * create a torus frame within the scene create axis frame within
  94.      * frame of sphere create torus frame within frame of axis
  95.      */
  96.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, scene, &sphere)))
  97.     goto generic_error;
  98.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, sphere, &axis)))
  99.     goto generic_error;
  100.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, axis, &torus)))
  101.     goto generic_error;
  102.  
  103.     {
  104.     /*
  105.      * load a texture and wrap it onto the sphere
  106.      */
  107.     LPDIRECT3DRMTEXTURE tex;
  108.     LPDIRECT3DRMWRAP wrap;
  109.     D3DVALUE height;
  110.  
  111.     D3DRMBOX box;
  112.     D3DVALUE miny, maxy;
  113.     if (FAILED(sphere_mesh->lpVtbl->GetBox(sphere_mesh, &box)))
  114.         goto generic_error;
  115.     maxy = box.max.y;
  116.     miny = box.min.y;
  117.     height = maxy - miny;
  118.  
  119.     if (FAILED(lpD3DRM->lpVtbl->CreateWrap(lpD3DRM, D3DRMWRAP_CYLINDER, NULL, D3DVAL(0.0),
  120.                               D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0),
  121.                               D3DVAL(0.0), D3DVAL(1.0), D3DVAL(0.0),
  122.                               D3DVAL(1.0), D3DVAL(0.0), D3DVAL(0.0),
  123.                               D3DDivide(maxy, height), D3DVAL(1.0),
  124.                               D3DDivide(D3DVAL(1), height), &wrap)))
  125.                   goto generic_error;
  126.     if (FAILED(wrap->lpVtbl->Apply(wrap, (LPDIRECT3DRMOBJECT)sphere_mesh)))
  127.         goto generic_error;
  128.     RELEASE(wrap);
  129.  
  130.     rval = lpD3DRM->lpVtbl->LoadTexture(lpD3DRM, "tex2.ppm", &tex);
  131.     if (rval != D3DRM_OK) {
  132.             Msg("Failed to load tex2.ppm\n%s", D3DRMErrorToString(rval));
  133.         goto ret_with_error;
  134.     }
  135.     if (FAILED(tex->lpVtbl->SetColors(tex, 16)))
  136.         goto generic_error;
  137.     if (FAILED(tex->lpVtbl->SetShades(tex, 8)))
  138.         goto generic_error;
  139.     if (FAILED(sphere_mesh->lpVtbl->SetGroupTexture(sphere_mesh, -1, tex)))
  140.         goto generic_error;
  141.     RELEASE(tex);
  142.     }
  143.     /*
  144.      * add the loaded mesh into the frame
  145.      */
  146.     if (FAILED(torus->lpVtbl->AddVisual(torus, (LPDIRECT3DRMVISUAL) torus_mesh)))
  147.     goto generic_error;
  148.     if (FAILED(sphere->lpVtbl->AddVisual(sphere, (LPDIRECT3DRMVISUAL) sphere_mesh)))
  149.     goto generic_error;
  150.  
  151.     /*
  152.      * set up the frames position, orientation and rotation
  153.      */
  154.     if (FAILED(camera->lpVtbl->SetPosition(camera, scene, D3DVAL(0), D3DVAL(0), -D3DVAL(10))))
  155.     goto generic_error;
  156.     if (FAILED(sphere->lpVtbl->SetPosition(sphere, scene, D3DVAL(0), D3DVAL(0), D3DVAL(0))))
  157.     goto generic_error;
  158.     if (FAILED(axis->lpVtbl->SetPosition(axis, sphere, D3DVAL(2), D3DVAL(0), D3DVAL(0))))
  159.     goto generic_error;
  160.     if (FAILED(torus->lpVtbl->SetPosition(torus, axis, D3DVAL(1.0), D3DVAL(0), D3DVAL(0))))
  161.     goto generic_error;
  162.  
  163.     if (FAILED(sphere->lpVtbl->SetRotation(sphere, scene, D3DVAL(0), D3DVAL(1.0), D3DVAL(0.5),D3DVAL(0.05))))
  164.     goto generic_error;
  165.     if (FAILED(axis->lpVtbl->SetRotation(axis, sphere, D3DVAL(1), D3DVAL(0), D3DVAL(0), D3DVAL(0.01))))
  166.     goto generic_error;
  167.     if (FAILED(torus->lpVtbl->SetRotation(torus, axis, D3DVAL(0), D3DVAL(0), D3DVAL(1), -D3DVAL(0.05))))
  168.     goto generic_error;
  169.  
  170.     RELEASE(lights);
  171.     RELEASE(axis);
  172.     RELEASE(torus_mesh);
  173.     RELEASE(sphere_mesh);
  174.     RELEASE(torus);
  175.     RELEASE(sphere);
  176.     RELEASE(lp);
  177.     RELEASE(la);
  178.     return TRUE;
  179. generic_error:
  180.     Msg("A failure occurred while building the scene.\n");
  181. ret_with_error:
  182.     RELEASE(lights);
  183.     RELEASE(axis);
  184.     RELEASE(torus_mesh);
  185.     RELEASE(sphere_mesh);
  186.     RELEASE(torus);
  187.     RELEASE(sphere);
  188.     RELEASE(lp);
  189.     RELEASE(la);
  190.     RELEASE(builder);
  191.     return FALSE;
  192. }
  193.  
  194. void
  195. OverrideDefaults(Defaults* defaults)
  196. {
  197.     defaults->bConstRenderQuality = TRUE;
  198.     lstrcpy(defaults->Name, "Frame Hierarchy D3D RM Example");
  199. }
  200.