61254

Creating spherical meshes with Direct x?

How do you go about creating a sphere with meshes in Direct-x? I'm using C++ and the program will be run on windows, only.

Everything is currently rendered through an IDiRECT3DDEVICE9 object.

Answer1:

You could use the D3DXCreateSphere function.

Answer2:

There are lots of ways to create a sphere.

One is to use polar coordinates to generate slices of the sphere.

struct Vertex { float x, y, z; float nx, ny, nz; };

Given that struct you'd generate the sphere as follows (I haven't tested this so I may have got it slightly wrong).

std::vector< Vertex > verts; int count = 0; while( count < numSlices ) { const float phi = M_PI / numSlices; int count2 = 0; while( count2 < numSegments ) { const float theta = M_2PI / numSegments const float xzRadius = fabsf( sphereRadius * cosf( phi ) ); Vertex v; v.x = xzRadius * cosf( theta ); v.y = sphereRadius * sinf( phi ); v.z = xzRadius * sinf( theta ); const float fRcpLen = 1.0f / sqrtf( (v.x * v.x) + (v.y * v.y) + (v.z * v.z) ); v.nx = v.x * fRcpLen; v.ny = v.y * fRcpLen; v.nz = v.z * fRcpLen; verts.push_back( v ); count2++; } count++; }

This is how D3DXCreateSphere does it i believe. Of course the code above does not form the faces but thats not a particularly complex bit of code if you set your mind to it :)

The other, and more interesting in my opinion, way is through surface subdivision.

If you start with a cube that has normals defined the same way as the above code you can recursively subdivide each side. Basically you find the center of the face. Generate a vector from the center to the new point. Normalise it. Push the vert out to the radius of the sphere as follows (Assuming v.n* is the normalised normal):

v.x = v.nx * sphereRadius; v.y = v.ny * sphereRadius; v.z = v.nz * sphereRadius;

You then repeat this process for the mid point of each edge of the face you are subdividing.

Now you can split each face into 4 new quadrilateral faces. You can then subdivide each of those quads into 4 new quads and so on until you get to the refinement level you require.

Personally I find this process provides a nicer vertex distribution on the sphere than the first method.

Recommend

  • Passing pointers to arrays to functions
  • Method drawVertices() didn't drawing anything on Android Canvas
  • Fill a triangle in 3D matplolib plot with a color gradient
  • Full 8 bit adder, illogical output
  • Classic ASP URL Rewriting
  • Efficient User-Agent Regex to find Safari in Python
  • C#: Import/Export Settings into/from a File
  • Invalid Date on validation Date of js
  • C: Incompatible pointer type initializing
  • Ionic 2 storage is not cleaning up on uninstall - Only for signed APK
  • Django: Count of Group Elements
  • AES padding and writing the ciphertext to a disk file
  • VS2008 Enable C++ Exception with SEH
  • Java applet as stand-alone Windows application?
  • Sending data from AppleScript to FileMaker records
  • MySQL WHERE-condition in procedure ignored
  • Where to put my custom functions in Wordpress?
  • How to show dropdown in excel using jrxml (jasper api)?
  • Importing jscolor library in angular 2
  • Release, debug version and Authorization Google?
  • Websockets service method fails during R startup
  • Alternatives to the OPTIONAL fallback SPARQL pattern?
  • How to get next/previous record number?
  • Apache 2.4 - remove | delete | uninstall
  • RestKit - RKRequestDelegate does not exist
  • WPF Applying a trigger on binding failure
  • Proper way to use connect-multiparty with express.js?
  • How get height of the a view with gone visibility and height defined as wrap_content in xml?
  • Trying to get generic when generic is not available
  • FormattedException instead of throw new Exception(string.Format(…)) in .NET
  • embed rChart in Markdown
  • How to get Windows thread pool to call class member function?
  • IndexOutOfRangeException on multidimensional array despite using GetLength check
  • Sorting a 2D array using the second column C++
  • costura.fody for a dll that references another dll
  • Observable and ngFor in Angular 2
  • How to Embed XSL into XML
  • UserPrincipal.Current returns apppool on IIS
  • Conditional In-Line CSS for IE and Others?
  • java string with new operator and a literal