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

Windows编程

开发平台:

Visual C++

  1. /*==========================================================================
  2.  *
  3.  *  Copyright (C) 1995, 1996 Microsoft Corporation. All Rights Reserved.
  4.  *
  5.  *  File: tex3.c
  6.  *
  7.  ***************************************************************************/
  8. /*
  9.  * A texture mapping example using a spherical mapping, relative to a
  10.  * frame.
  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.     LPDIRECT3DRMMESHBUILDER builder = NULL;
  46.     LPDIRECT3DRMFRAME frame = NULL;
  47.     LPDIRECT3DRMFRAME axis = NULL;
  48.     LPDIRECT3DRMFRAME child = NULL;
  49.     LPDIRECT3DRMTEXTURE tex = NULL;
  50.     LPDIRECT3DRMWRAP wrap = NULL;
  51.     LPDIRECT3DRMLIGHT l1 = NULL;
  52.     LPDIRECT3DRMLIGHT l2 = 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), &l1)))
  59.   goto generic_error;
  60.     if (FAILED(scene->lpVtbl->AddLight(scene, l1)))
  61. goto generic_error;
  62.     if (FAILED(lpD3DRM->lpVtbl->CreateFrame(lpD3DRM, scene, &frame)))
  63. goto generic_error;
  64.     if (FAILED(frame->lpVtbl->SetOrientation(frame, scene, -D3DVAL(1), -D3DVAL(1),
  65.                                   D3DVAL(1), D3DVAL(0), D3DVAL(1), D3DVAL(0))))
  66.   goto generic_error;
  67.     if (FAILED(lpD3DRM->lpVtbl->CreateLightRGB(lpD3DRM, D3DRMLIGHT_DIRECTIONAL, D3DVAL(1),
  68.                                   D3DVAL(1), D3DVAL(1), &l2)))
  69.   goto generic_error;
  70.     if (FAILED(frame->lpVtbl->AddLight(frame, l2)))
  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,
  83.      D3DRMLOAD_FROMFILE, NULL, NULL);
  84.     if (rval != D3DRM_OK) {
  85.         Msg("Failed to load sphere3.x.n%s", D3DRMErrorToString(rval));
  86. goto ret_with_error;
  87.     }
  88.     if (FAILED(builder->lpVtbl->Scale(builder, D3DVAL(0.2), D3DVAL(0.2), D3DVAL(0.2))))
  89. goto generic_error;
  90.     if (FAILED(builder->lpVtbl->CreateMesh(builder, &mesh)))
  91. goto generic_error;
  92.     RELEASE(builder);
  93.     if (FAILED(frame->lpVtbl->AddVisual(frame, (LPDIRECT3DRMVISUAL) mesh)))
  94. goto generic_error;
  95.     RELEASE(mesh);
  96.     if (FAILED(lpD3DRM->lpVtbl->CreateWrap(lpD3DRM, D3DRMWRAP_SPHERE, frame,
  97.                               D3DVAL(0.0), D3DVAL(0.0), D3DVAL(0.0),
  98.                               D3DVAL(0.0), D3DVAL(1.0), D3DVAL(0.0),
  99.                               D3DVAL(0.0), D3DVAL(0.0), D3DVAL(1.0),
  100.                               D3DVAL(0.0), D3DVAL(0.0), D3DVAL(1.0),
  101.       D3DVAL(1.0), &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, "checker.ppm", &tex);
  115.     if (rval != D3DRM_OK) {
  116. Msg("Failed to load checker.ppm.n%s", D3DRMErrorToString(rval));
  117. goto ret_with_error;
  118.     }
  119.     if (FAILED(tex->lpVtbl->SetColors(tex, 2)))
  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.     if (FAILED(builder->lpVtbl->Load(builder, "tpot2.x", NULL,
  128.      D3DRMLOAD_FROMFILE, NULL, NULL)))
  129. goto generic_error;
  130.     if (FAILED(builder->lpVtbl->Scale(builder, D3DVAL(2), D3DVAL(2), D3DVAL(2))))
  131. goto generic_error;
  132.     if (FAILED(builder->lpVtbl->SetTexture(builder, tex)))
  133. goto generic_error;
  134.     if (FAILED(builder->lpVtbl->SetQuality(builder, D3DRMRENDER_GOURAUD)))
  135. goto generic_error;
  136.     if (FAILED(builder->lpVtbl->CreateMesh(builder, &mesh)))
  137. goto generic_error;
  138.     RELEASE(builder);
  139.     if (FAILED(child->lpVtbl->AddVisual(child, (LPDIRECT3DRMVISUAL) mesh)))
  140. goto generic_error;
  141.     if (FAILED(child->lpVtbl->AddMoveCallback(child, applyWrap, (void *) wrap)))
  142. goto generic_error;
  143.     if (FAILED(child->lpVtbl->AddDestroyCallback(child, cleanupWrap, wrap)))
  144. goto generic_error;
  145.     RELEASE(mesh);
  146.     RELEASE(frame);
  147.     RELEASE(axis);
  148.     RELEASE(child);
  149.     RELEASE(tex);
  150.     RELEASE(l1);
  151.     RELEASE(l2);
  152.     return TRUE;
  153. generic_error:
  154.     Msg("A failure occurred while building the scene.n");
  155. ret_with_error:
  156.     RELEASE(mesh);
  157.     RELEASE(builder);
  158.     RELEASE(frame);
  159.     RELEASE(axis);
  160.     RELEASE(child);
  161.     RELEASE(tex);
  162.     RELEASE(wrap);
  163.     RELEASE(l1);
  164.     RELEASE(l2);
  165.     return FALSE;
  166. }
  167. void
  168. OverrideDefaults(Defaults *defaults)
  169. {
  170.     defaults->bConstRenderQuality = TRUE;
  171.     lstrcpy(defaults->Name, "Texture Mapping III Direct3DRM Example");
  172. }