TEX4.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: tex4.c
  6.  *
  7.  ***************************************************************************/
  8. /*
  9.  * A texture mapping example using a spherical mapping relative to the
  10.  * camera. The texture is also used as a background for the scene.
  11.  */
  12. #include "rmdemo.h"
  13. void CDECL 
  14. applyWrap(LPDIRECT3DRMFRAME frame, void* arg, D3DVALUE delta)
  15. {
  16.     LPDIRECT3DRMWRAP wrap = (LPDIRECT3DRMWRAP) arg;
  17.     LPDIRECT3DRMVISUALARRAY visuals;
  18.     LPDIRECT3DRMVISUAL v;
  19.     int count, i;
  20.     LPDIRECT3DRMMESH mesh;
  21.     frame->lpVtbl->GetVisuals(frame, &visuals);
  22.     count = visuals->lpVtbl->GetSize(visuals);
  23.     for (i = 0; i < count; i++) {
  24. visuals->lpVtbl->GetElement(visuals, i, &v);
  25. if (SUCCEEDED(v->lpVtbl->QueryInterface(v, &IID_IDirect3DRMMesh, &mesh))) {
  26.     wrap->lpVtbl->ApplyRelative(wrap, frame, (LPDIRECT3DRMOBJECT)mesh);
  27.     mesh->lpVtbl->Release(mesh);
  28. }
  29. v->lpVtbl->Release(v);
  30.     }
  31.     visuals->lpVtbl->Release(visuals);
  32. }
  33. void CDECL 
  34. cleanupWrap(LPDIRECT3DRMOBJECT obj, void *arg)
  35. {
  36.     LPDIRECT3DRMWRAP wrap = (LPDIRECT3DRMWRAP) arg;
  37.     obj;
  38.     wrap->lpVtbl->Release(wrap);
  39. }
  40. BOOL
  41. BuildScene(LPDIRECT3DRMDEVICE dev, LPDIRECT3DRMVIEWPORT view,
  42.    LPDIRECT3DRMFRAME scene, LPDIRECT3DRMFRAME camera)
  43. {
  44.     LPDIRECT3DRMMESH mesh = NULL;
  45.     LPDIRECT3DRMFRAME frame = NULL;
  46.     LPDIRECT3DRMFRAME axis = NULL;
  47.     LPDIRECT3DRMFRAME child = NULL;
  48.     LPDIRECT3DRMTEXTURE tex = NULL;
  49.     LPDIRECT3DRMWRAP wrap = NULL;
  50.     LPDIRECT3DRMLIGHT light1 = NULL;
  51.     LPDIRECT3DRMLIGHT light2 = NULL;
  52.     LPDIRECT3DRMMESHBUILDER builder = NULL;
  53.     HRESULT rval;
  54.     dev;
  55.     view;
  56.     camera;
  57.     if (FAILED(lpD3DRM->lpVtbl->CreateLightRGB(lpD3DRM, D3DRMLIGHT_AMBIENT, D3DVAL(0.2),
  58.                                   D3DVAL(0.2), D3DVAL(0.2), &light1)))
  59.   goto generic_error;
  60.     if (FAILED(lpD3DRM->lpVtbl->CreateLightRGB(lpD3DRM, D3DRMLIGHT_DIRECTIONAL, D3DVAL(1),
  61.                                   D3DVAL(1), D3DVAL(1), &light2)))
  62.   goto generic_error;
  63.     if (FAILED(scene->lpVtbl->AddLight(scene, light1)))
  64. goto generic_error;
  65.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, scene, &frame)))
  66. goto generic_error;
  67.     if (FAILED(frame->lpVtbl->SetOrientation(frame, scene, -D3DVAL(1), -D3DVAL(1),
  68.                                   D3DVAL(1), D3DVAL(0), D3DVAL(1), D3DVAL(0))))
  69.   goto generic_error;
  70.     if (FAILED(frame->lpVtbl->AddLight(frame, light2)))
  71. goto generic_error;
  72.     RELEASE(frame);
  73.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, scene, &frame)))
  74. goto generic_error;
  75.     if (FAILED(frame->lpVtbl->SetPosition(frame, scene, D3DVAL(0), D3DVAL(0), D3DVAL(15))))
  76. goto generic_error;
  77.     if (FAILED(frame->lpVtbl->SetRotation(frame, scene, D3DVAL(1.1), D3DVAL(0.3),
  78.                                D3DVAL(0.0), D3DVAL(0.04))))
  79.        goto generic_error;
  80.     if (FAILED(lpD3DRM->lpVtbl->CreateMeshBuilder(lpD3DRM, &builder)))
  81. goto generic_error;
  82.     rval = builder->lpVtbl->Load(builder, "sphere3.x", NULL, D3DRMLOAD_FROMFILE, NULL, NULL);
  83.     if (rval != D3DRM_OK) {
  84.         Msg("Failed to load sphere3.x.n");
  85. goto ret_with_error;
  86.     }
  87.     if (FAILED(builder->lpVtbl->Scale(builder, D3DVAL(0.2), D3DVAL(0.2), D3DVAL(0.2))))
  88. goto generic_error;
  89.     if (FAILED(builder->lpVtbl->CreateMesh(builder, &mesh)))
  90. goto generic_error;
  91.     RELEASE(builder);
  92.     if (FAILED(frame->lpVtbl->AddVisual(frame, (LPDIRECT3DRMVISUAL) mesh)))
  93. goto generic_error;
  94.     RELEASE(mesh);
  95.     if (FAILED(lpD3DRM->lpVtbl->CreateWrap(lpD3DRM, D3DRMWRAP_SPHERE, camera,
  96.       D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0),
  97.       D3DVAL(0.0), D3DVAL(1.0), D3DVAL(0.0),
  98.       D3DVAL(0.0), D3DVAL(0.0), -D3DVAL(1.0),
  99.       D3DVAL(0.0), D3DVAL(0.0),
  100.       D3DVAL(5.0), D3DVAL(5.0),
  101.       &wrap)))
  102.       goto generic_error;
  103.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, frame, &axis)))
  104. goto generic_error;
  105.     if (FAILED(axis->lpVtbl->SetRotation(axis, frame, D3DVAL(0), D3DVAL(1), D3DVAL(0), D3DVAL(0.04))))
  106. goto generic_error;
  107.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, axis, &child)))
  108. goto generic_error;
  109.     if (FAILED(child->lpVtbl->SetPosition(child, axis, D3DVAL(3), D3DVAL(0), D3DVAL(0))))
  110. goto generic_error;
  111.     if (FAILED(child->lpVtbl->SetRotation(child, axis, D3DVAL(1), D3DVAL(1), D3DVAL(0),
  112.                                D3DVAL(0.04))))
  113.        goto generic_error;
  114.     rval = lpD3DRM->lpVtbl->LoadTexture(lpD3DRM, "lake.ppm", &tex);
  115.     if (rval != D3DRM_OK) {
  116. Msg("Failed to load lake.ppm.n");
  117. goto ret_with_error;
  118.     }
  119.     if (FAILED(tex->lpVtbl->SetColors(tex, 32)))
  120. goto generic_error;
  121.     if (FAILED(tex->lpVtbl->SetDecalScale(tex, TRUE)))
  122. goto generic_error;
  123.     if (FAILED(tex->lpVtbl->SetDecalOrigin(tex, 128, 128)))
  124. goto generic_error;
  125.     if (FAILED(lpD3DRM->lpVtbl->CreateMeshBuilder(lpD3DRM, &builder)))
  126. goto generic_error;
  127.     rval = builder->lpVtbl->Load(builder, "tpot2.x", NULL, D3DRMLOAD_FROMFILE, NULL, NULL);
  128.     if (rval != D3DRM_OK) {
  129.         Msg("Failed to load tpot2.x.n");
  130. goto ret_with_error;
  131.     }
  132.     if (FAILED(builder->lpVtbl->Scale(builder, D3DVAL(2), D3DVAL(2), D3DVAL(2))))
  133. goto generic_error;
  134.     if (FAILED(builder->lpVtbl->SetTexture(builder, tex)))
  135. goto generic_error;
  136.     if (FAILED(builder->lpVtbl->SetQuality(builder, D3DRMRENDER_GOURAUD)))
  137. goto generic_error;
  138.     if (FAILED(builder->lpVtbl->CreateMesh(builder, &mesh)))
  139. goto generic_error;
  140.     RELEASE(builder);
  141.     if (FAILED(child->lpVtbl->AddVisual(child, (LPDIRECT3DRMVISUAL) mesh)))
  142. goto generic_error;
  143.     if (FAILED(child->lpVtbl->AddMoveCallback(child, applyWrap, (void *) wrap)))
  144. goto generic_error;
  145.     if (FAILED(child->lpVtbl->AddDestroyCallback(child, cleanupWrap, wrap)))
  146. goto generic_error;
  147.     if (FAILED(scene->lpVtbl->SetSceneBackgroundImage(scene, tex)))
  148. goto generic_error;
  149.     RELEASE(mesh);
  150.     RELEASE(frame);
  151.     RELEASE(axis);
  152.     RELEASE(child);
  153.     RELEASE(tex);
  154.     RELEASE(light1);
  155.     RELEASE(light2);
  156.     /* don't release the wrap */
  157.     return TRUE;
  158. generic_error:
  159.     Msg("A failure occurred while building the scene.n");
  160. ret_with_error:
  161.     RELEASE(mesh);
  162.     RELEASE(frame);
  163.     RELEASE(axis);
  164.     RELEASE(child);
  165.     RELEASE(tex);
  166.     RELEASE(wrap);
  167.     RELEASE(light1);
  168.     RELEASE(light2);
  169.     RELEASE(builder);
  170.     return FALSE;
  171. }
  172. void
  173. OverrideDefaults(Defaults* defaults)
  174. {
  175.     defaults->bConstRenderQuality = TRUE;
  176.     lstrcpy(defaults->Name, "Texture Mapping IV Direct3DRM Example");
  177. }