zoukankan      html  css  js  c++  java
  • 8. 世界矩阵使物体移动

    #include<d3d9.h>
    #include
    <d3dx9.h>

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

    #define WINDOW_CLASS "UGPDX"
    #define WINDOW_NAME "World Matrix"
    #define WINDOW_WIDTH 640
    #define WINDOW_HEIGHT 480

    // Function Prototypes...
    bool InitializeD3D(HWND hWnd, bool fullscreen);
    bool InitializeObjects();
    void RenderScene();
    void Shutdown();


    // Direct3D object and device.
    LPDIRECT3D9 g_D3D = NULL;
    LPDIRECT3DDEVICE9 g_D3DDevice
    = NULL;

    D3DXMATRIX g_projection;
    D3DXMATRIX g_worldMatrix;
    D3DXMATRIX g_translation;
    D3DXMATRIX g_rotation;

    float g_angle = 0.0f;

    // Vertex buffer to hold the geometry.
    LPDIRECT3DVERTEXBUFFER9 g_VertexBuffer = NULL;

    // A structure for our custom vertex type
    struct stD3DVertex
    {
    float x, y, z;
    unsigned
    long color;
    };

    // Our custom FVF, which describes our custom vertex structure
    #define D3DFVF_VERTEX (D3DFVF_XYZ | D3DFVF_DIFFUSE)


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

    case WM_KEYUP:
    if(wParam == VK_ESCAPE) PostQuitMessage(0);
    break;
    }

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


    int WINAPI WinMain(HINSTANCE hInst, HINSTANCE prevhInst, LPSTR cmdLine, int show)
    {
    // Register the window class
    WNDCLASSEX wc = { sizeof(WNDCLASSEX), CS_CLASSDC, MsgProc, 0L, 0L,
    GetModuleHandle(NULL), NULL, NULL, NULL, NULL,
    WINDOW_CLASS, NULL };
    RegisterClassEx(
    &wc);

    // Create the application's window
    HWND hWnd = CreateWindow(WINDOW_CLASS, WINDOW_NAME, WS_OVERLAPPEDWINDOW,
    100, 100, WINDOW_WIDTH, WINDOW_HEIGHT, GetDesktopWindow(),
    NULL, wc.hInstance, NULL);

    // Initialize Direct3D
    if(InitializeD3D(hWnd, false))
    {
    // 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
    RenderScene();
    }
    }

    // Release any and all resources.
    Shutdown();

    // Unregister our window.
    UnregisterClass(WINDOW_CLASS, wc.hInstance);
    return 0;
    }


    bool InitializeD3D(HWND hWnd, bool fullscreen)
    {
    D3DDISPLAYMODE displayMode;

    // Create the D3D object.
    g_D3D = Direct3DCreate9(D3D_SDK_VERSION);
    if(g_D3D == NULL) return false;

    // Get the desktop display mode.
    if(FAILED(g_D3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &displayMode)))
    return false;

    // Set up the structure used to create the D3DDevice
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(
    &d3dpp, sizeof(d3dpp));

    if(fullscreen)
    {
    d3dpp.Windowed
    = FALSE;
    d3dpp.BackBufferWidth
    = WINDOW_WIDTH;
    d3dpp.BackBufferHeight
    = WINDOW_HEIGHT;
    }
    else
    d3dpp.Windowed
    = TRUE;
    d3dpp.SwapEffect
    = D3DSWAPEFFECT_DISCARD;
    d3dpp.BackBufferFormat
    = displayMode.Format;

    // Create the D3DDevice
    if(FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
    &d3dpp, &g_D3DDevice)))
    {
    return false;
    }

    // Initialize any objects we will be displaying.
    if(!InitializeObjects()) return false;

    return true;
    }


    bool InitializeObjects()
    {
    // Set the projection matrix.
    D3DXMatrixPerspectiveFovLH(&g_projection, D3DX_PI / 4,
    WINDOW_WIDTH
    /WINDOW_HEIGHT, 0.1f, 1000.0f);

    g_D3DDevice
    ->SetTransform(D3DTS_PROJECTION, &g_projection);

    // Set default rendering states.
    g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
    g_D3DDevice
    ->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);

    // Fill in our structure to draw an object.
    // x, y, z, color.
    stD3DVertex objData[] =
    {
    {
    -0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(255,255,0)},
    {
    0.3f, -0.3f, 1.0f, D3DCOLOR_XRGB(255,0,0)},
    {
    0.0f, 0.3f, 1.0f, D3DCOLOR_XRGB(0,0,255)}
    };

    // Create the vertex buffer.
    if(FAILED(g_D3DDevice->CreateVertexBuffer(3 * sizeof(stD3DVertex), 0,
    D3DFVF_VERTEX, D3DPOOL_DEFAULT,
    &g_VertexBuffer, NULL))) return false;

    // Fill the vertex buffer.
    void *ptr;
    if(FAILED(g_VertexBuffer->Lock(0, sizeof(objData), (void**)&ptr, 0))) return false;
    memcpy(ptr, objData,
    sizeof(objData));
    g_VertexBuffer
    ->Unlock();

    return true;
    }


    void RenderScene()
    {
    // Clear the backbuffer.
    g_D3DDevice->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_XRGB(0,0,0), 1.0f, 0);

    // Begin the scene. Start rendering.
    g_D3DDevice->BeginScene();

    // Build translation and rotation matrices.
    // 要使物体移动就需要每帧改变世界矩阵
    // 所以将世界矩阵放到这里来设置,透视投影矩阵每帧都保持不变所以在前面一次性设置好
    // 物体在世界坐标系中的初始位置是(0,0,0)即物体的初始世界坐标为(0,0,0),下面的平移矩阵将物体
    // 移动到(0,0,3.0)。
    // 平移旋转的实现:每一帧绘制的时候都将物体位置移到(0,0,3.0),然后开始绕Y轴旋转
    // 一定的角度,由于每一帧的角度大小是逐渐变大的,所以是每一帧都从一个相同的位置开始
    // 旋转,每帧旋转的度数逐渐变大,看上去旋转
    D3DXMatrixTranslation(&g_translation, 0.0f, 0.0f, 3.0f);
    D3DXMatrixRotationY(
    &g_rotation, g_angle);

    // Build the world matrix and update angle.
    g_worldMatrix = g_rotation * g_translation;
    g_angle
    += 0.01f;
    if(g_angle >= 360) g_angle = 0.0f;

    // Set the world matrix.
    g_D3DDevice->SetTransform(D3DTS_WORLD, &g_worldMatrix);

    // Draw the object.
    g_D3DDevice->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex));
    g_D3DDevice
    ->SetFVF(D3DFVF_VERTEX);
    g_D3DDevice
    ->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);

    // End the scene. Stop rendering.
    g_D3DDevice->EndScene();

    // Display the scene.
    g_D3DDevice->Present(NULL, NULL, NULL, NULL);
    }


    void Shutdown()
    {
    if(g_D3DDevice != NULL) g_D3DDevice->Release();
    if(g_D3D != NULL) g_D3D->Release();
    if(g_VertexBuffer != NULL) g_VertexBuffer->Release();

    g_D3DDevice
    = NULL;
    g_D3D
    = NULL;
    g_VertexBuffer
    = NULL;
    }
    /*
    在游戏编程中使物体可以在场景中移动,这很重要。下面将介绍改变当前世界矩阵或模型矩阵的方法。
    世界矩阵控制物体位置的参考点。开始时,起点位于(0,0)。通过指定要绘制的新几何图形集合的新起点,
    改动世界矩阵,就可以在3D空间中四处移动。例如,使用如D3DXMatrixTranslation()这样的函数就可以改变
    世界位置。D3DXMatrixTranslation()函数的参数包括创建不同于D3DXMATRIX类型的世界矩阵的矩阵、要移动世
    界矩阵的X、Y和Z的值。如果要将新的世界矩阵位置从(0,0,0)设置为(0,0,-5),那么只要使用程序清单1.23中
    的几行代码即可。
    D3DXMATRIX world;
    D3DXMatrixRotationY(&world, 30.0f);
    g_D3DDevice->SetTransform(D3DTS_WORLD, &g_world);
    */

      

  • 相关阅读:
    Civil 3D 二次开发 创建Civil 3D 对象—— 01 —— 创建几何空间点
    Civil 3D 二次开发 创建Civil 3D 对象—— 00 ——
    Civil 3D 二次开发 创建AutoCAD对象—— 01 —— 创建直线
    Civil 3D 二次开发 新建CLR项目出现错误C2143
    Civil 3D 二次开发 创建AutoCAD对象—— 00 ——
    了解AutoCAD对象层次结构 —— 6 ——块表记录
    datepicker97使用
    使用angular 外接 templateUrl,使用ng-include
    angularJs 遮罩
    网上找的有关css兼容问题
  • 原文地址:https://www.cnblogs.com/kex1n/p/2145900.html
Copyright © 2011-2022 走看看