DirectX-将四面体细分为球体

时间:2015-02-09 20:14:19

标签: geometry directx

我刚开始学习DirectX。我找到了一个用OpenGL编写的例子,其中四面体被细分为一个球体,我试图用DirectX重写这个例子。该算法在OpenGL应用程序中运行良好,但我有一个问题是使用OpenGL绘制它。我的应用程序绘制了四面体,如图所示:

Screenshot http://media902.dropshots.com/photos/1251021/20150209/143114.jpg

当我尝试将四面体细分为球体时,我得到了以下图片:

Screenshot http://media901.dropshots.com/photos/1251021/20150209/143125.jpg

负责细分四面体的代码如下:

struct point
{
    D3DXVECTOR3 Pos;
};

#define M 16*64*3

point data[10000];

float x= 0,  y= 0, z = 0, edge = 1;
//tetrahedron
point v[4]= { 
    { D3DXVECTOR3(0.0, 0.0, 1.0)}, 
    { D3DXVECTOR3(0.0, 0.942809, -0.333333)}, 
    { D3DXVECTOR3( -0.816497, -0.471405, -0.333333 )}, 
    { D3DXVECTOR3( 0.816497, -0.471405, -0.333333)}, 
};

int k = 0;

point unit(point p)
{
   point c;
   float d=0.0;
   for(int i=0; i<3; i++)
   {
      d+=p.Pos[i]*p.Pos[i];
   }
   d=sqrt(d);
   if(d > 0.0) 
   {
      for(int i=0; i<3; i++) 
      {
          c.Pos[i] = p.Pos[i]/d;
      }
   }
   return c;
}

void triangle( point  a, point b, point c)
{
    data[k]= a;
    k++;
    data[k] = b;
    k++;
    data[k] = c;
    k++;
}

point plus(point a, point b)
{
    point c;
    for(int i=0; i<3; i++)
    {
        c.Pos[i] = a.Pos[i] + b.Pos[i];
    }
    return c;
} 

void divide_triangle(point a, point b, point c, int n)
{
     point v1, v2, v3;
     if(n>0)
     {
         v1 = unit(plus(a,b));
         v2 = unit(plus(a,c));
         v3 = unit(plus(b,c));   
         divide_triangle(a ,v1, v2, n-1);
         divide_triangle(c ,v2, v3, n-1);
         divide_triangle(b ,v3, v1, n-1);
         divide_triangle(v1 ,v3, v2, n-1);
      }
      else triangle(a, b, c);
}

void tetrahedron(int n)
{
   divide_triangle(v[0], v[1], v[2], n);
   divide_triangle(v[3], v[2], v[1], n );
   divide_triangle(v[0], v[3], v[1], n );
   divide_triangle(v[0], v[2], v[3], n );
}

函数tetrahedron()在函数中被调用:

HRESULT InitDevice()
{
    (...)
    tetrahedron(4);
    bd.Usage = D3D10_USAGE_DEFAULT;
    bd.ByteWidth = sizeof( point ) * k;
    bd.BindFlags = D3D10_BIND_INDEX_BUFFER; 
    bd.CPUAccessFlags = 0;
    bd.MiscFlags = 0;
    InitData.pSysMem = data;
    hr = g_pd3dDevice->CreateBuffer( &bd, &InitData,    &g_pTetrahedronVertexBuffer );
    if( FAILED( hr ) )
        return hr;

    g_Camera.setPerspectiveProjectionLH(45.0, width / ( FLOAT )height, 0.1f, 250.0f);

    return S_OK;
}

并在函数中调用函数InitDevice():

int WINAPI wWinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPWSTR     lpCmdLine, int nCmdShow )
{
    UNREFERENCED_PARAMETER( hPrevInstance );
    UNREFERENCED_PARAMETER( lpCmdLine );

    if( FAILED( InitWindow( hInstance, nCmdShow ) ) )
        return 0;

    RegisterInputDevices(g_hWnd);

    if( FAILED( InitDevice() ) )
    {
        CleanupDevice();
        return 0;
    }

    MSG msg = {0};
    gTimeStart = GetTickCount(); 

    while( 1 )
    {
        while( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
        {
            TranslateMessage( &msg ); 
            DispatchMessage( &msg );
            if (WM_QUIT == msg.message) { break;} 
        }
        if (WM_QUIT == msg.message) 
        {
            break; 
        }

        Render(); 
    }

    CleanupDevice(); 
    return ( int )msg.wParam;
}    

在函数Render()中,我得到了代码的最后一部分,它引用了绘制球体:

UINT stride, offset = 0;
(...)
stride = sizeof( point );
offset = 0;
g_pd3dDevice->IASetVertexBuffers( 0, 1, &g_pTetrahedronVertexBuffer, &stride, &offset ); //vertex buffer
g_pd3dDevice->IASetPrimitiveTopology( D3D10_PRIMITIVE_TOPOLOGY_TRIANGLELIST    ); 
g_pTexturedColorTechnique->GetPassByIndex( 0 )->Apply( 0 ); 
g_pd3dDevice->DrawIndexed( k, 0, 0 );     

你能帮助我并告诉我为什么我会得到这么奇怪的物体而不是球体?怎么了?

0 个答案:

没有答案