骤雨__ 2021-09-06 03:53 采纳率: 0%
浏览 26

d3d9运行结果是黑色背景,什么也没有

请帮我看看,我的代码是哪里错了?
我用的是vs2019,运行后是除了黑色的背景,什么也没得。

img


/*
   Demo Name:  Template
      Author:  Allen Sherrod
     Chapter:  Ch 1
*/
#include<d3d9.h>
#include<d3dx9.h>
#pragma comment(lib, "d3d9.lib")
#pragma comment(lib, "d3dx9.lib")
#define WINDOW_CLASS    "UGPDX"
#define WINDOW_NAME     "Template"
#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;
D3DXMATRIX g_WorldMatrix;
// Mesh objects
LPD3DXMESH g_teapot{};
LPD3DXMESH g_cube{};
LPD3DXMESH g_sphere{};
LPD3DXMESH g_torus{};
LRESULT WINAPI MsgProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp)
{
    switch (msg)
    {
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
        break;
    case WM_KEYUP:
        if (wp == VK_ESCAPE) PostQuitMessage(0);
        break;
    }
    return DefWindowProc(hWnd, msg, wp, lp);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE ph, LPSTR cmd, int s)
{
    // 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_HARDWARE_VERTEXPROCESSING | D3DCREATE_PUREDEVICE,
        &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, 45.0f,
        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);
    // 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);
    // End the scene.  Stop rendering.
    g_D3DDevice->EndScene();
    // Display the scene.
    g_D3DDevice->Present(NULL, NULL, NULL, NULL);
}
*/
bool InitializeObjects()
{
    // 设置程序的默认渲染状态
    g_D3DDevice->SetRenderState(D3DRS_LIGHTING, FALSE);

    // 创建茶壶
    // ( Direct3D 设备对象、
    // 函数返回时要填充的网面的指针、
    // 用三角索引数组设置的 LPD3DXBUFFER 指针 )
    if (FAILED(D3DXCreateTeapot(g_D3DDevice, &g_teapot, NULL)))
        return false;

    // (Direct3D 设备对象、盒子宽度、高度、深度、存储盒子网格的指针、存储三角形索引的指针)
    if (FAILED(D3DXCreateBox(g_D3DDevice, 2, 2, 2, &g_cube, NULL)))
        return false;

    if (FAILED(D3DXCreateSphere(g_D3DDevice, 1.5, 25, 25, &g_sphere, NULL))) 
        return false;

    if (FAILED(D3DXCreateTorus(g_D3DDevice, 0.5f, 1.2f, 25, 25, &g_torus, NULL)))
        return false;

    D3DXVECTOR3 cameraPos(0.0f, 0.0f, -8.0f);
    D3DXVECTOR3 lookAtPos(0.0f, 0.0f, 0.0f);
    D3DXVECTOR3 upDir(0.0f, 1.0f, 0.0f);

    D3DXMatrixLookAtLH(&g_ViewMatrix, &cameraPos, &lookAtPos, &upDir);

    return true;
}

void RenderScene()
{
    // Clear the bakcbuffer.
    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);

        // Draw teapot.
        D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, -2.0f, 0.0f);   
            // 将矩阵对象及标示对象的x、y、z值发送给该函数
        g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);
            // 将矩阵应用到 Direct3D中
        g_teapot->DrawSubset(0);
            // 在指定的位置上绘制该对象。

        // Draw Cube.
        D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, -2.0f, 0.0f);
        g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);
        g_cube->DrawSubset(0);

        // Draw Sphere.
        D3DXMatrixTranslation(&g_WorldMatrix, 2.0f, 2.0f, 0.0f);
        g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);
        g_sphere->DrawSubset(0);

        // Draw Torus.
        D3DXMatrixTranslation(&g_WorldMatrix, -2.0f, 2.0f, 0.0f);
        g_D3DDevice->SetTransform(D3DTS_WORLD, &g_WorldMatrix);
        g_torus->DrawSubset(0);

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

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

void Shutdown()
{
    // Release all resources.
    if (g_D3DDevice != NULL) g_D3DDevice->Release();
    if (g_D3D != NULL) g_D3D->Release();
    g_D3DDevice = NULL;
    g_D3D = NULL;

    if (g_teapot) { g_teapot->Release(); g_teapot = NULL; }
    if (g_cube) { g_cube->Release(); g_cube = NULL; }
    if (g_sphere) { g_sphere->Release(); g_sphere = NULL; }
    if (g_torus) { g_torus->Release(); g_torus = NULL; }
}
  • 写回答

1条回答 默认 最新

  • 关注

    你没有设置光源和 camera,原先注释掉的那一段

    评论

报告相同问题?

问题事件

  • 创建了问题 9月6日

悬赏问题

  • ¥15 stata安慰剂检验作图但是真实值不出现在图上
  • ¥15 c程序不知道为什么得不到结果
  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题