• 32. 细节映射


    通常会多次要在3D场景中显示大量的细节。例如,混凝土上的裂纹,或粗糙墙面的凸凹不平之处。也许并不总是想在纹理图像自身包含这种细节。通常,一些彩色图纹理虽然从远处看的时候它们很好,但靠近看时,也许会让观察者觉得有些模糊。使用一种名为细节映射的技术有助于解决这些问题,或至少得到改善。

           细节图是一个灰度图,保存小而精确的细节内容。当将其和颜色图一起施加到表面上时,最终的图像看上去原始图像好像有更多的细节。同样,使用细节图时,颜色图在从上靠近看时,看上去不模糊。使用细节图为表面增加细节,给图增加小的裂纹以及线条,这样可以提高表面的真实感。对墙和类似表面更是如此。

           细节映射的演示成名为DetailMapping,它位于本书配套光盘的CHAPTER6文件夹中。它将加载两个纹理图像,对它们实现多纹理映射,并将它们显示在一个正方形表面上。在合并两幅纹理图像时,通过一个有符号操作将细节添加到基图上。纹理图像中有符号值从-128~128,而无符号值从0~255。细节图中较亮的值将会增亮基图,而较黑的值则产生反面的效果。只有0不会对最终的图像产生任何效果,因为是正在添加图像而不是对图像作调制,就像在多纹理演示程序中的做法一样。

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

    #define WINDOW_CLASS "UGPDX"
    #define WINDOW_NAME "Detail Mapping"
    #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;

    // Matrices.
    D3DXMATRIX g_projection;
    D3DXMATRIX g_ViewMatrix;

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

    // Holds a texture image.
    LPDIRECT3DTEXTURE9 g_Texture = NULL;
    LPDIRECT3DTEXTURE9 g_Texture2
    = NULL;

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

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


    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()
    {
    // Fill in our structure to draw an object.
    // x, y, z, color, texture coords.
    stD3DVertex objData[] =
    {
    {
    -1.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f, 0.0f, 1.0f},
    {
    1.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 1.0f, 1.0f, 1.0f},
    {
    1.5f, -0.5f, 3.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f, 1.0f, 0.0f},

    {
    1.5f, -0.5f, 3.0f, D3DCOLOR_XRGB(255,255,255), 1.0f, 0.0f, 1.0f, 0.0f},
    {
    -1.5f, -0.5f, 3.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 0.0f, 0.0f, 0.0f},
    {
    -1.5f, -0.5f, 0.0f, D3DCOLOR_XRGB(255,255,255), 0.0f, 1.0f, 0.0f, 1.0f}
    };

    // Create the vertex buffer.
    if(FAILED(g_D3DDevice->CreateVertexBuffer(sizeof(objData), 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();


    // Load the texture image from file.
    if(D3DXCreateTextureFromFile(g_D3DDevice, "ground.bmp",
    &g_Texture) != D3D_OK) return false;

    if(D3DXCreateTextureFromFile(g_D3DDevice, "detail.tga",
    &g_Texture2) != D3D_OK) return false;


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


    // Set the projection matrix.
    D3DXMatrixPerspectiveFovLH(&g_projection, 45.0f,
    WINDOW_WIDTH
    /WINDOW_HEIGHT, 0.1f, 1000.0f);

    g_D3DDevice
    ->SetTransform(D3DTS_PROJECTION, &g_projection);


    // Define camera information.
    D3DXVECTOR3 cameraPos(0.0f, 0.0f, -1.0f);
    D3DXVECTOR3 lookAtPos(
    0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 upDir(
    0.0f, 1.0f, 0.0f);

    // Build view matrix.
    D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos,
    &lookAtPos, &upDir);

    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();

    // Apply the view (camera).
    g_D3DDevice->SetTransform(D3DTS_VIEW, &g_ViewMatrix);

    // Set the texture stages for the first texture unit (image).
    g_D3DDevice->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    g_D3DDevice
    ->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

    /*
    如果要模拟一块粗糙的石灰泥墙壁,可以通过细节映射(detail mapping)来实现。
    实现过程是:将基础贴图(也就是第一张纹理)的颜色未经修改便作为第二个纹理操作
    阶段中的第二个参数,然后通过D3DTOP_ADDSIGNED将灰色的细节纹理与基础贴图相加。
    这个操作本质上是做了一个加法,只是使用了有符号的颜色值来代替平时使用的无符号值。
    在对两张纹理的像素颜色进行D3DTOP_ADDSIGNED操作时,它将参数的每个成分相加后再减去
    偏移量0.5,从而使有效值域变为-0.5 ~ 0.5。对一些比较旧的显卡,当其不能支持
    D3DTOP_ADDSIGNED操作时,可以使用D3DTOP_MODULATE2X代替D3DTOP_ADDSIGNED操作进行模拟。
    */

    g_D3DDevice
    ->SetTextureStageState(0, D3DTSS_TEXCOORDINDEX, 0);
    // 颜色混合方式--使用当前颜色作为第一个texture stage的输出
    g_D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
    // 颜色混合的第一个参数的来源--纹理的颜色
    g_D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);

    // Set the texture stages for the second texture unit (image).
    g_D3DDevice->SetSamplerState(1, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
    g_D3DDevice
    ->SetSamplerState(1, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

    g_D3DDevice
    ->SetTextureStageState(1, D3DTSS_TEXCOORDINDEX, 1);
    g_D3DDevice
    ->SetTextureStageState(1, D3DTSS_COLOROP, D3DTOP_ADDSIGNED);
    g_D3DDevice
    ->SetTextureStageState(1, D3DTSS_COLORARG1, D3DTA_TEXTURE);
    g_D3DDevice
    ->SetTextureStageState(1, D3DTSS_COLORARG2, D3DTA_CURRENT);

    // Draw square.
    g_D3DDevice->SetTexture(0, g_Texture);
    g_D3DDevice
    ->SetTexture(1, g_Texture2);
    g_D3DDevice
    ->SetStreamSource(0, g_VertexBuffer, 0, sizeof(stD3DVertex));
    g_D3DDevice
    ->SetFVF(D3DFVF_VERTEX);
    g_D3DDevice
    ->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 2);

    // 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();
    g_D3DDevice
    = NULL;

    if(g_D3D != NULL) g_D3D->Release();
    g_D3D
    = NULL;

    if(g_VertexBuffer != NULL) g_VertexBuffer->Release();
    g_VertexBuffer
    = NULL;

    if(g_Texture != NULL) g_Texture->Release();
    g_Texture
    = NULL;

    if(g_Texture2 != NULL) g_Texture2->Release();
    g_Texture2
    = NULL;
    }

      

  • 相关阅读:
    Getting started with 3G | ip.access nano3G+OpenBSC+Osmocom-bb Part 1
    Metasploit的射频收发器功能 | Metasploit’s RF Transceiver Capabilities
    分析无线遥控器信号并制作Hack硬件进行攻击
    利用Hackrf One进行GPS定位欺骗制作超级跑马机
    使用OpenBTS基站测试物联网模块安全性
    在cmd中为命令设置别名以及启动cmd时自动执行bat
    Struts2、spring2、hibernate3在SSH中各起什么作用
    switch omega
    html转译字符 字符实体
    excel自定义数据验证
  • 原文地址:https://www.cnblogs.com/kex1n/p/2167462.html
Copyright © 2020-2023  润新知