分享

欧拉角旋转函数

 polaris_c2000 2016-05-24
//-----------------------------------------------------------------------------
// File: Lights.cpp
// Copyright (c) Microsoft Corporation & Waitingfy.com. All rights reserved.
//-----------------------------------------------------------------------------
#include
#include
#include
#include
#include
#include
//-----------------------------------------------------------------------------
// Global variables
//-----------------------------------------------------------------------------
LPDIRECT3D9             g_pD3D       = NULL;// Used to create the D3DDevice
LPDIRECT3DDEVICE9       g_pd3dDevice = NULL;// Our rendering device
LPDIRECT3DVERTEXBUFFER9 g_pVB        = NULL;// Buffer to hold vertices
ID3DXMesh* Objects = 0;//茶壶
  
floatg_fSpinX = 0.0f;
floatg_fSpinY = 0.0f;
  
// A structure for our custom vertex type. We added a normal, and omitted the
// color (which is provided by the material)
structCUSTOMVERTEX
{
    D3DXVECTOR3 position;// The 3D position for the vertex
    D3DXVECTOR3 normal;  // The surface normal for the vertex
};
  
// Our custom FVF, which describes our custom vertex structure
#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)
  
//-----------------------------------------------------------------------------
// Name: InitD3D()
// Desc: Initializes Direct3D
//-----------------------------------------------------------------------------
HRESULTInitD3D( HWNDhWnd )
{
    // Create the D3D object.
    if( NULL == ( g_pD3D = Direct3DCreate9( D3D_SDK_VERSION ) ) )
        returnE_FAIL;
  
    // Set up the structure used to create the D3DDevice. Since we are now
    // using more complex geometry, we will create a device with a zbuffer.
    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;
  
    // Create the D3DDevice
    if( FAILED( g_pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                      D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                      &d3dpp, &g_pd3dDevice ) ) )
    {
        returnE_FAIL;
    }
  
    // Turn off culling
    g_pd3dDevice->SetRenderState( D3DRS_CULLMODE, D3DCULL_NONE );
  
    // Turn on the zbuffer
    g_pd3dDevice->SetRenderState( D3DRS_ZENABLE, TRUE );
  
    returnS_OK;
}
  
//-----------------------------------------------------------------------------
// Name: Cleanup()
// Desc: Releases all previously initialized objects
//-----------------------------------------------------------------------------
VOIDCleanup()
{
    if( g_pVB != NULL )
        g_pVB->Release();
  
    if( g_pd3dDevice != NULL )
        g_pd3dDevice->Release();
  
    if( g_pD3D != NULL )
        g_pD3D->Release();
    if(Objects != NULL){
       Objects->Release();
    }
  
}
  
//-----------------------------------------------------------------------------
// Name: setupRotate()
// Desc: 绕3个角度旋转
//-----------------------------------------------------------------------------
  
VOIDsetupRotate(D3DXMATRIXA16 *returnMatrix, floatyaw, float pitch, float roll)
{
  
    floatsx = sin(pitch);float sy = sin(yaw); float sz = sin(roll);
    floatcx = cos(pitch);float cy = cos(yaw); float cz = cos(roll);
    returnMatrix->_11 = cz * cy - sx * sy * sz;
    returnMatrix->_12 = cy * sz + sx * sy * cz;
    returnMatrix->_13 = -sy * cx;
    returnMatrix->_14 = 0.0f;
    returnMatrix->_21 = -cx * sz;
    returnMatrix->_22 = cx * cz;
    returnMatrix->_23 = sx;
    returnMatrix->_24 = 0.0f;
    returnMatrix->_31 = sy * cz + sx * cy * sz;
    returnMatrix->_32 = sy * sz - sx * cy * cz;
    returnMatrix->_33 = cx * cy;
    returnMatrix->_34 = 0.0f;
    returnMatrix->_41 = 0.0f;
    returnMatrix->_42 = 0.0f;
    returnMatrix->_43 = 0.0f;
    returnMatrix->_44 = 1.0f;
}
  
//-----------------------------------------------------------------------------
// Name: SetupMatrices()
// Desc: Sets up the world, view, and projection transform matrices.
//-----------------------------------------------------------------------------
VOIDSetupMatrices()
{
    // 旋转
    D3DXMATRIXA16 matWorld;
    D3DXMatrixIdentity( &matWorld );
    //根据x轴和y轴移动距离旋转
    setupRotate(&matWorld,D3DXToRadian(g_fSpinX),D3DXToRadian(g_fSpinY),0.0f);
    g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  
    // 摄像机的位置
    D3DXVECTOR3 vEyePt( 0.0f, 3.0f,-5.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 );
    g_pd3dDevice->SetTransform( D3DTS_PROJECTION, &matProj );
}
  
//-----------------------------------------------------------------------------
// Name: SetupLights()
// Desc: Sets up the Lights and materials for the scene.
//-----------------------------------------------------------------------------
VOIDSetupLights()
{
    // 初始化一个材料
  
    D3DMATERIAL9 mtrl;
    ZeroMemory( &mtrl,sizeof(D3DMATERIAL9) );
    mtrl.Diffuse = mtrl.Ambient = D3DXCOLOR(1.0f,1.0f,0.0f,1.0f);//材质有漫射和环境光的设置,都为黄色
    g_pd3dDevice->SetMaterial( &mtrl );
  
    // 初始化一个白色的方向光
    ///*
    D3DXVECTOR3 vecDir;
    D3DLIGHT9 light;
    ZeroMemory( &light,sizeof(D3DLIGHT9) );
    light.Type       = D3DLIGHT_DIRECTIONAL;//方向光
    light.Diffuse = D3DXCOLOR(1.0f,1.0f,1.0f,1.0f);//设置光源的漫射光颜色为白色
    light.Direction = D3DXVECTOR3(0.0f,0.0f,1.0f);//光的传播方向平行z轴
    light.Range       = 1000.0f;
    g_pd3dDevice->SetLight( 0, &light );
    g_pd3dDevice->LightEnable( 0, TRUE );
    g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );
  
    //*/
    // Finally, turn on some ambient light.
    g_pd3dDevice->SetRenderState( D3DRS_AMBIENT, 0x00202020 );
}
  
//-----------------------------------------------------------------------------
// Name: Render()
// Desc: Draws the scene
//-----------------------------------------------------------------------------
VOIDRender()
{
    // 背景为蓝色
    g_pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,
                         D3DCOLOR_XRGB(0,0,255), 1.0f, 0 );
    // Begin the scene
    if( SUCCEEDED( g_pd3dDevice->BeginScene() ) )
    {
        // 茶壶的材料颜色也定义在这个函数中
        SetupLights();
  
        // Setup the world, view, and projection matrices
        SetupMatrices();
  
        // Render the vertex buffer contents
        g_pd3dDevice->SetStreamSource( 0, g_pVB, 0,sizeof(CUSTOMVERTEX) );
        g_pd3dDevice->SetFVF( D3DFVF_CUSTOMVERTEX );
  
        //创建一个茶壶
  
        //Objects = 0;
        D3DXMATRIX  Worlds;
        D3DXCreateTeapot(g_pd3dDevice, &Objects, 0);
        Objects->DrawSubset(0);
        Objects->Release();
        Objects = 0;
        // End the scene
        g_pd3dDevice->EndScene();
    }
  
    // Present the backbuffer contents to the display
    g_pd3dDevice->Present( NULL, NULL, NULL, NULL );
    //Objects->Release();
}
  
//-----------------------------------------------------------------------------
// Name: MsgProc()
// Desc: The window's message handler
//-----------------------------------------------------------------------------
LRESULTWINAPI MsgProc( HWNDhWnd, UINT msg, WPARAM wParam,LPARAM lParam )
{
    staticPOINT ptLastMousePosit;
    staticPOINT ptCurrentMousePosit;
    staticbool bMousing;
    switch( msg )
    {
        caseWM_DESTROY:
            Cleanup();
            PostQuitMessage( 0 );
            return0;
        caseWM_LBUTTONDOWN: // 当鼠标左键按下时,记录当前的位置(屏幕坐标),并设置按下标志
            {
                ptLastMousePosit.x = ptCurrentMousePosit.x = LOWORD (lParam);
                ptLastMousePosit.y = ptCurrentMousePosit.y = HIWORD (lParam);
                bMousing =true;
            }
            break;
        caseWM_LBUTTONUP:
            {
                bMousing =false;
            }
            break;
        caseWM_MOUSEMOVE: // 鼠标移动时,保存X/Y轴的移动距离
            {
                ptCurrentMousePosit.x = LOWORD (lParam);
                ptCurrentMousePosit.y = HIWORD (lParam);
                if( bMousing )
                {
                    g_fSpinX -= (ptCurrentMousePosit.x - ptLastMousePosit.x);
                    g_fSpinY -= (ptCurrentMousePosit.y - ptLastMousePosit.y);
                }
                ptLastMousePosit.x = ptCurrentMousePosit.x;
                ptLastMousePosit.y = ptCurrentMousePosit.y;
            }
         break;
    }
    returnDefWindowProc( hWnd, msg, wParam, lParam );
}
//-----------------------------------------------------------------------------
// Name: WinMain()
// Desc: The application's entry point
//-----------------------------------------------------------------------------
INTWINAPI WinMain( HINSTANCEhInst, HINSTANCE,LPSTR, INT )
{
    // Register the window class
    WNDCLASSEX wc = {sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
                      GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
                      'D3D Tutorial', NULL };
    RegisterClassEx( &wc );
    // Create the application's window
    HWNDhWnd = CreateWindow( 'D3D Tutorial','D3D Tutorial 04: Lights',
                              WS_OVERLAPPEDWINDOW, 100, 100, 300, 300,
                              GetDesktopWindow(), NULL, wc.hInstance, NULL );
    // Initialize Direct3D
    if( SUCCEEDED( InitD3D( hWnd ) ) )
    {
            // Show the window
            ShowWindow( hWnd, SW_SHOWDEFAULT );
            UpdateWindow( hWnd );
  
            // Enter the message loop
            MSG msg;
            ZeroMemory( &msg,sizeof(msg) );
            while( msg.message!=WM_QUIT )
            {
                if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
                {
                    TranslateMessage( &msg );
                    DispatchMessage( &msg );
                }
                else
                    Render();             
            }
    }
    UnregisterClass('D3D Tutorial', wc.hInstance );
    return0;
}

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约

    类似文章 更多