阶乘体系月亮太阳旋转原理

// 阶乘体系月亮太阳旋转原理.cpp : 定义应用程序的入口点。
//

#include "stdafx.h"
#include "阶乘体系月亮太阳旋转原理.h"
#include
#include
#include


LPDIRECT3D9              g_pD3D         = NULL ;
LPDIRECT3DDEVICE9        g_pd3dDevice   = NULL ;
LPDIRECT3DVERTEXBUFFER9  g_pVB          = NULL ;
LPDIRECT3DINDEXBUFFER9   g_pIB          = NULL ;

D3DXMATRIXA16            g_matTMParent  ;
D3DXMATRIXA16            g_matRParent   ;

D3DXMATRIXA16            g_matTMChild   ;
D3DXMATRIXA16            g_matRChild    ;

 

#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")

struct CUSTOMVERTEX
{
 FLOAT x,y,z;
 DWORD color;
};


#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ |D3DFVF_DIFFUSE)

 

struct MYINDEX
{
 WORD _0,_1,_2;
};

 

HRESULT InitD3D (HWND hWnd )
{
 //创建D3D对象
 if(NULL == ( g_pD3D =Direct3DCreate9 ( D3D_SDK_VERSION ) ))
  return E_FAIL;
 //创建设备的结构体
 D3DPRESENT_PARAMETERS d3dpp;
 ZeroMemory(&d3dpp ,sizeof(d3dpp));
 d3dpp.Windowed                 = TRUE;
 d3dpp.SwapEffect               = D3DSWAPEFFECT_DISCARD;
 d3dpp.BackBufferFormat         = D3DFMT_UNKNOWN;
 d3dpp.EnableAutoDepthStencil   = TRUE;
 d3dpp.AutoDepthStencilFormat   = D3DFMT_D16;

  if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
     return E_FAIL;

  //实现卷起功能
  g_pd3dDevice->SetRenderState(D3DRS_CULLMODE , D3DCULL_NONE);
 

  //起到z缓冲的功能
  g_pd3dDevice->SetRenderState(D3DRS_ZENABLE , TRUE );

  //顶点具有颜色,能起到光源的功能
  g_pd3dDevice->SetRenderState(D3DRS_LIGHTING ,FALSE);

  return S_OK;
}


HRESULT InitVB()
{
    /// 渲染正方体(cube)的八个顶点声明
    CUSTOMVERTEX vertices[] =
    {
  { -1,  1,  1 , 0xffff0000 },  /// v0
  {  1,  1,  1 , 0xff00ff00 },  /// v1
  {  1,  1, -1 , 0xff0000ff },  /// v2
  { -1,  1, -1 , 0xffffff00 },  /// v3

  { -1, -1,  1 , 0xff00ffff },  /// v4
  {  1, -1,  1 , 0xffff00ff },  /// v5
  {  1, -1, -1 , 0xff000000 },  /// v6
  { -1, -1, -1 , 0xffffffff },  /// v7
    };

    /// 创建顶点缓冲
    /// 分配储存八个用户顶点的存储器.
    /// 指定储存FVF的数据格式.
    if( FAILED( g_pd3dDevice->CreateVertexBuffer( 8*sizeof(CUSTOMVERTEX),
                                                  0, D3DFVF_CUSTOMVERTEX,
                                                  D3DPOOL_DEFAULT, &g_pVB, NULL ) ) )
    {
        return E_FAIL;
    }

    /// 将数据写入顶点缓冲.
    /// 调用顶点缓冲的Lock()函数,获取指针.
    VOID* pVertices;
    if( FAILED( g_pVB->Lock( 0, sizeof(vertices), (void**)&pVertices, 0 ) ) )
        return E_FAIL;
    memcpy( pVertices, vertices, sizeof(vertices) );
    g_pVB->Unlock();

    return S_OK;
}


/**-----------------------------------------------------------------------------
 * 创建索引缓冲,设置索引值.
 *------------------------------------------------------------------------------
 */
HRESULT InitIB()
{
    /// 渲染正方体(cube)的十二个顶点声明
    MYINDEX indices[] =
    {
  { 0, 1, 2 }, { 0, 2, 3 }, /// 上面
  { 4, 6, 5 }, { 4, 7, 6 }, /// 下面
  { 0, 3, 7 }, { 0, 7, 4 }, /// 左面
  { 1, 5, 6 }, { 1, 6, 2 }, /// 右面
  { 3, 2, 6 }, { 3, 6, 7 }, /// 前面
  { 0, 4, 5 }, { 0, 5, 1 } /// 后面
    };

    /// 创建索引缓冲
 /// D3DFMT_INDEX16的索引单位为16位.
 /// MYINDEX结构体中,使用WORD型进行声明,所以要使用D3DFMT_INDEX16.
    if( FAILED( g_pd3dDevice->CreateIndexBuffer( 12 * sizeof(MYINDEX), 0, D3DFMT_INDEX16, D3DPOOL_DEFAULT, &g_pIB, NULL ) ) )
    {
        return E_FAIL;
    }

    /// 将数据写入索引缓冲.
    /// 调用索引缓冲的Lock()函数,获取指针.
    VOID* pIndices;
    if( FAILED( g_pIB->Lock( 0, sizeof(indices), (void**)&pIndices, 0 ) ) )
        return E_FAIL;
    memcpy( pIndices, indices, sizeof(indices) );
    g_pIB->Unlock();

    return S_OK;
}


HRESULT InitGeometry()
{
 if( FAILED( InitVB() ) ) return E_FAIL;
 if( FAILED( InitIB() ) ) return E_FAIL;

 return S_OK;
}

 

void Animate()
{
 D3DXMatrixIdentity( &g_matTMParent);
  /// 父网格的Y轴旋转矩阵
    D3DXMatrixRotationY( &g_matRParent, GetTickCount()/500.0f );

 /// 子网格的Z轴旋转矩阵
    D3DXMatrixRotationZ( &g_matRChild, GetTickCount()/500.0f );
 /// 子网格离原点有一定的距离(3,3,3)
 D3DXMatrixTranslation( &g_matTMChild, 3, 3, 3 );
}


void SetupCamera()
{
 //创建一个世界矩阵
 D3DXMATRIXA16  matWorld;
 D3DXMatrixIdentity( &matWorld );
 g_pd3dDevice->SetTransform( D3DTS_WORLD , &matWorld );
 
 //创建视图矩阵
 D3DXVECTOR3 vEyePt   ( 0.0f , 10.0f , -10.0f );
 D3DXVECTOR3 vLookatPt( 0.0f , 0.0f , 0.0f  );
 D3DXVECTOR3 vUpVec   ( 0.0f , 1.0f , 0.0f  );
 //视图矩阵
 D3DXMATRIXA16  matView ;
 D3DXMatrixLookAtLH(&matView , &vEyePt , &vLookatPt , &vUpVec);
 g_pd3dDevice->SetTransform( D3DTS_VIEW , &matView );

 //创建投影矩阵
 D3DXMATRIXA16  matProj ;
 D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI/4,1.0f , 1.0f ,100.0f );    //近裁面和远裁面都是1.0
 g_pd3dDevice->SetTransform( D3DTS_PROJECTION , &matProj );

}

 


void CleanUp()
{
 if(g_pVB !=NULL)
  g_pVB->Release();
 if(g_pD3D!=NULL)
  g_pD3D->Release();
 if(g_pd3dDevice!=NULL)
  g_pd3dDevice->Release();
 if(g_pIB!=NULL)
  g_pIB->Release();
}


void DrawMesh(D3DXMATRIXA16 * pMat)
{
 g_pd3dDevice->SetTransform(D3DTS_WORLD ,pMat);
 g_pd3dDevice->SetStreamSource( 0 ,g_pVB , 0, sizeof(CUSTOMVERTEX));
    g_pd3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
 g_pd3dDevice->SetIndices(g_pIB);
 g_pd3dDevice->DrawIndexedPrimitive( D3DPT_TRIANGLELIST, 0, 0, 8, 0, 12 );  //8个顶点,12个三角形
}


VOID Render()
{
 D3DXMATRIXA16 matWorld;

    /// 后置缓冲和Z-缓冲初始化
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );

 /// 创建动画矩阵
 Animate();
    /// 开始渲染
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
  matWorld = g_matRParent * g_matTMParent;
  DrawMesh( &matWorld );  /// 绘制父正方体
   //
  matWorld = g_matRChild * g_matTMChild * matWorld;  //也就是说会继承父类的绕Y轴旋转
  DrawMesh( &matWorld );  /// 绘制子正方体

        /// 结束渲染
        g_pd3dDevice->EndScene();
    }

    /// 显示后置缓冲的画面!
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

 

 

LRESULT WINAPI MsgProc(HWND hWnd , UINT msg, WPARAM wParam ,LPARAM lParam)
{
 switch(msg)
 {
 case WM_DESTROY:
  CleanUp();
  PostQuitMessage(0);
  return 0;
 }

 return DefWindowProc(hWnd ,msg,wParam,lParam);
}

 

 

 

INT WINAPI WinMain( HINSTANCE hInst , HINSTANCE ,LPSTR ,INT )
{
 WNDCLASSEX wc = { sizeof(WNDCLASSEX) , CS_CLASSDC , MsgProc , 0L ,0L,
 GetModuleHandle(NULL), NULL , NULL , NULL , NULL,
 L"BasicFrame", NULL};
 
 RegisterClassEx(&wc);
 HWND hWnd = CreateWindow( L"BasicFrame", L"LightMappint",WS_OVERLAPPEDWINDOW,
  100,100,500,500 ,GetDesktopWindow() ,NULL ,wc.hInstance,NULL);

 

 

 MSG msg;
 ZeroMemory (&msg ,sizeof (msg));
 if( SUCCEEDED( InitD3D(hWnd ) ) )
  if(SUCCEEDED (InitGeometry()))
 {
   SetupCamera();
         ShowWindow(hWnd , SW_SHOWDEFAULT );
         UpdateWindow(hWnd);
     while(msg.message!=WM_QUIT)
   {
    if (PeekMessage (&msg,NULL,0U ,0U, PM_REMOVE))
    {
      TranslateMessage(&msg);
            DispatchMessage(&msg);
    }
    else
     Render();
   }
 UnregisterClass(L"D3D Tutorial",wc.hInstance);
 return 0;
 }
}

你可能感兴趣的:(Direct3D学习)