Advertisement
Guest User

Untitled

a guest
Aug 8th, 2012
705
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.84 KB | None | 0 0
  1. // aaaarrgghhh!!! meshes are horrible!!!
  2. struct spheredef
  3. {
  4. LPDIRECT3DVERTEXBUFFER9 vb;
  5. LPDIRECT3DINDEXBUFFER9 ib;
  6.  
  7. int numverts;
  8. int numindexes;
  9. };
  10.  
  11. spheredef sd_Jupiter;
  12. spheredef sd_Sattelite;
  13.  
  14.  
  15. HRESULT D3D_BuildSphere (spheredef *sd, int slices, int stacks, float radius)
  16. {
  17. HRESULT hr = S_OK;
  18.  
  19. // prevent overflowing the index buffer
  20. while (slices * stacks * 6 > 65534)
  21. {
  22. slices--;
  23. stacks--;
  24. }
  25.  
  26. float drho = D3DX_PI / (float) stacks;
  27. float dtheta = (D3DX_PI / (float) slices) * 2.0f;
  28.  
  29. float ds = 1.0f / (float) slices;
  30. float dt = 1.0f / (float) stacks;
  31.  
  32. float t = 1.0f;
  33. float s = 0.0f;
  34.  
  35. hr = d3d_Device->CreateVertexBuffer
  36. (
  37. sizeof (float) * 8 * stacks * (slices + 1) * 2,
  38. 0,
  39. 0,
  40. D3DPOOL_MANAGED,
  41. &sd->vb,
  42. NULL
  43. );
  44.  
  45. if (FAILED (hr)) return hr;
  46.  
  47. hr = d3d_Device->CreateIndexBuffer
  48. (
  49. sizeof (unsigned short) * slices * stacks * 6,
  50. 0,
  51. D3DFMT_INDEX16,
  52. D3DPOOL_MANAGED,
  53. &sd->ib,
  54. NULL
  55. );
  56.  
  57. if (FAILED (hr)) return hr;
  58.  
  59. float *sv = NULL;
  60. unsigned short *ndx = NULL;
  61.  
  62. sd->vb->Lock (0, 0, (void **) &sv, 0);
  63. sd->ib->Lock (0, 0, (void **) &ndx, 0);
  64.  
  65. sd->numindexes = 0;
  66. sd->numverts = 0;
  67.  
  68. int stripverts = (slices + 1) * 2;
  69.  
  70. for (int i = 0; i < stacks; i++, t -= dt, sd->numverts += stripverts)
  71. {
  72. float rho = (float) i * drho;
  73. float srho = (float) (sin (rho));
  74. float crho = (float) (cos (rho));
  75. float srhodrho = (float) (sin (rho + drho));
  76. float crhodrho = (float) (cos (rho + drho));
  77.  
  78. s = 0.0f;
  79.  
  80. // each of these is a triangle strip
  81. for (int j = 0; j <= slices; j++, s += ds, sv += 16)
  82. {
  83. float theta = (j == slices) ? 0.0f : j * dtheta;
  84. float stheta = (float) (-sin (theta));
  85. float ctheta = (float) (cos (theta));
  86.  
  87. sv[0] = stheta * srho * radius;
  88. sv[1] = ctheta * srho * radius;
  89. sv[2] = crho * radius;
  90.  
  91. sv[3] = stheta * srho;
  92. sv[4] = ctheta * srho;
  93. sv[5] = crho;
  94.  
  95. VectorNormalize (&sv[3]);
  96.  
  97. sv[6] = s;
  98. sv[7] = t;
  99.  
  100. sv[8] = stheta * srhodrho * radius;
  101. sv[9] = ctheta * srhodrho * radius;
  102. sv[10] = crhodrho * radius;
  103.  
  104. sv[11] = stheta * srhodrho;
  105. sv[12] = ctheta * srhodrho;
  106. sv[13] = crhodrho;
  107.  
  108. VectorNormalize (&sv[3]);
  109.  
  110. sv[14] = s;
  111. sv[15] = (t - dt);
  112. }
  113.  
  114. // and unwind the strip into it's component indexes
  115. for (int j = 2; j < stripverts; j++, ndx += 3, sd->numindexes += 3)
  116. {
  117. ndx[0] = sd->numverts + j - 2;
  118.  
  119. if (j & 1)
  120. {
  121. ndx[1] = sd->numverts + j;
  122. ndx[2] = sd->numverts + j - 1;
  123. }
  124. else
  125. {
  126. ndx[1] = sd->numverts + j - 1;
  127. ndx[2] = sd->numverts + j;
  128. }
  129. }
  130. }
  131.  
  132. sd->vb->Unlock ();
  133. sd->ib->Unlock ();
  134.  
  135. return S_OK;
  136. }
  137.  
  138.  
  139. void D3D_DestroySphere (spheredef *sd)
  140. {
  141. if (sd->ib)
  142. {
  143. sd->ib->Release ();
  144. sd->ib = NULL;
  145. }
  146.  
  147. if (sd->vb)
  148. {
  149. sd->vb->Release ();
  150. sd->vb = NULL;
  151. }
  152. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement