回 帖 发 新 帖 刷新版面

主题:Direct3D编程问题 麻烦有哪位大侠帮我找下错误!

我刚接触DirectX 这里是一个旋转立方体的代码 编译和链接 都没问题 可是显示窗口一片漆黑~
找了半天 没发现错误 谁给看下

#include "stdafx.h"
#include "resource.h"
#include "d3dx9.h"
#include "mmsystem.h"
#define MAX_LOADSTRING 100
#pragma   comment(lib,"d3d9.lib")
#pragma   comment(lib,"d3dx9.lib")
#pragma comment(lib,"winmm.lib")
LPDIRECT3DDEVICE9 g_pDevice = NULL;
LPDIRECT3DVERTEXBUFFER9 g_pVertexBuffer;
HINSTANCE g_hInst;    
HWND g_hWnd;                        
TCHAR szTitle[MAX_LOADSTRING];                                
TCHAR szWindowClass[MAX_LOADSTRING];
typedef struct CUSTOMVERTEX

{
    float x,y,z;
    DWORD diffuse;
}CUSTOMVERTEX;

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_DIFFUSE)

HRESULT InitialiseD3D(HWND hWnd)
{
    if( NULL == (g_pD3D = Direct3DCreate9(D3D_SDK_VERSION)))
    return E_FAIL;
    D3DDISPLAYMODE d3ddm;
    if(FAILED(g_pD3D->GetAdapterDisplayMode(D3DADAPTER_DEFAULT,&d3ddm)))
        return E_FAIL;
    D3DPRESENT_PARAMETERS d3dpp;
    ZeroMemory(&d3dpp,sizeof(d3dpp));
    d3dpp.Windowed=TRUE;
    d3dpp.SwapEffect=D3DSWAPEFFECT_COPY;
    d3dpp.BackBufferFormat=d3ddm.Format;
    if( FAILED( g_pD3D->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd,
                                    D3DCREATE_SOFTWARE_VERTEXPROCESSING,
                                    &d3dpp, &g_pDevice ) ) )
    return E_FAIL;
    return S_OK;
}

HRESULT InitialiseVertexBuffer()
{
    VOID *pvertices;
    CUSTOMVERTEX Vertices[]=
    {
        {-5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(25,0,0)},
        {-5.0f,5.0f,5.0f,D3DCOLOR_XRGB(255,0,0)},
        {5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0)},
        {5.0f,5.0f,5.0f,D3DCOLOR_XRGB(255,0,0)},

        {-5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0)},
        {-5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0)},
        {5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0)},
        {5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0)},

        {5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(255,0,0)},
        {5.0f,5.0f,5.0f,D3DCOLOR_XRGB(255,0,0)},

        {-5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(255,0,0)},
        {-5.0f,5.0f,5.0f,D3DCOLOR_XRGB(255,0,0)},

        {-5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0)},
        {-5.0f,5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0)},

        {5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0)},
        {5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(255,0,0)},
        {-5.0f,-5.0f,-5.0f,D3DCOLOR_XRGB(255,0,0)},
        {-5.0f,-5.0f,5.0f,D3DCOLOR_XRGB(255,0,0)},
    };
    if(FAILED(g_pDevice->CreateVertexBuffer(18*sizeof(CUSTOMVERTEX),D3DUSAGE_WRITEONLY,D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVertexBuffer,NULL)))
        return E_FAIL;
    if(FAILED(g_pVertexBuffer->Lock(0,sizeof(Vertices),&pvertices,0)))
        return E_FAIL;
    memcpy(pvertices,Vertices,sizeof(Vertices));
    g_pVertexBuffer->Unlock();
    return S_OK;
}

void Clear()
{
    g_pVertexBuffer->Release();
    g_pDevice->Release();
    g_pD3D->Release();
}

VOID SetupMatrices()
{
    // Set up world matrix
    D3DXMATRIX matWorld,matWorldY,matWorldX,matWorldZ;
    D3DXMatrixRotationX(&matWorldX,timeGetTime()/400.0f);
    D3DXMatrixRotationY(&matWorldY,timeGetTime()/400.0f);
    D3DXMatrixRotationZ(&matWorldZ,timeGetTime()/400.0f);
    D3DXMatrixMultiply(&matWorld,&matWorldX,&matWorldY);
    D3DXMatrixMultiply(&matWorld,&matWorld,&matWorldZ);
    g_pDevice->SetTransform( D3DTS_WORLD, &matWorld );

    // Set up our view matrix. A view matrix can be defined given an eye point,
    // a point to lookat, and a direction for which way is up. Here, we set the
    // eye five units back along the z-axis and up three units, look at the 
    // origin, and define "up" to be in the y-direction.
    D3DXVECTOR3 vEyePt( 0.0f, 0.0f,-30.0f );
    D3DXVECTOR3 vLookatPt( 0.0f, 0.0f, 0.0f );
    D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
    D3DXMATRIX matView;
    D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
    g_pDevice->SetTransform(D3DTS_VIEW, &matView );

    // For the projection matrix, we set up a perspective transform (which
    // transforms geometry from 3D view space to 2D viewport space, with
    // a perspective divide making objects smaller in the distance). To build
    // a perpsective transform, we need the field of view (1/4 pi is common),
    // the aspect ratio, and the near and far clipping planes (which define at
    // what distances geometry should be no longer be rendered).
    D3DXMATRIX matProj;
    D3DXMatrixPerspectiveFovLH( &matProj,D3DX_PI/4,1.0f,1.0f,500.0f);
    g_pDevice->SetTransform(D3DTS_PROJECTION, &matProj );
}

void Render()

    if(g_pDevice==NULL)
        return;
    g_pDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),1.0F,0);
    g_pDevice->BeginScene();
    SetupMatrices();
    g_pDevice->SetStreamSource(0,g_pVertexBuffer,0,sizeof(CUSTOMVERTEX));
    g_pDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
    g_pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 0, 2);
    g_pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 4, 8);
    g_pDevice->DrawPrimitive( D3DPT_TRIANGLESTRIP, 14, 2);
    g_pDevice->EndScene();
    g_pDevice->Present(NULL,NULL,NULL,NULL);
}

ATOM                MyRegisterClass(HINSTANCE hInstance);
BOOL                InitInstance(HINSTANCE, int);
LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);

int APIENTRY WinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    MSG msg;

    

    // Initialize global strings
    LoadString(hInstance, IDS_TITLE, szTitle, MAX_LOADSTRING);
    LoadString(hInstance, IDS_WINDOW, szWindowClass, MAX_LOADSTRING);
    MyRegisterClass(hInstance);

    // Perform application initialization:
    if (!InitInstance (hInstance, nCmdShow)) 
    {
        return FALSE;
    }

    
    InitialiseD3D(g_hWnd);
        InitialiseVertexBuffer();
Render();
    // Main message loop:
    while (msg.message!=WM_QUIT) 
    {
        if (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }


    }

    return msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
    WNDCLASSEX wcex;

    wcex.cbSize = sizeof(WNDCLASSEX); 

    wcex.style            = CS_HREDRAW | CS_VREDRAW;
    wcex.lpfnWndProc    = (WNDPROC)WndProc;
    wcex.cbClsExtra        = 0;
    wcex.cbWndExtra        = 0;
    wcex.hInstance        = hInstance;
    wcex.hIcon            = LoadIcon(hInstance, (LPCTSTR)IDI_ICON);
    wcex.hCursor        = NULL;
    wcex.hbrBackground    = (HBRUSH)(COLOR_WINDOW+1);
    wcex.lpszMenuName    = NULL;
    wcex.lpszClassName    = szWindowClass;
    wcex.hIconSm        = LoadIcon(wcex.hInstance, (LPCTSTR)IDI_SMALL);

    return RegisterClassEx(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   g_hInst = hInstance; // Store instance handle in our global variable

   g_hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

   if (!g_hWnd)
   {
      return FALSE;
   }

 ShowWindow(g_hWnd, nCmdShow);
 UpdateWindow(g_hWnd);

   return TRUE;
}

回复列表 (共3个回复)

沙发

几种可能: 1,灯光没有关闭; 2,投影矩阵或者视图矩阵搞错; 3,DrawPrimitive失败;
一个个原因分析,检测下函数返回码,跟踪调试下程序

板凳

加了
    g_pDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_CCW);
    g_pDevice->SetRenderState(D3DRS_LIGHTING,FALSE);
还是黑屏
 调试显示 DrawPrimitive返回 S_OK,
投影矩阵试了几种方式 还是不好使 你能给点设置的建议吗 谢谢

3 楼

在你的Render()里加一个断点,跟踪调试一下

还有你这里:
while (msg.message!=WM_QUIT) 
    {
        if (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }


    }
}

应该是改成:
while (msg.message!=WM_QUIT) 
    {
        if (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE)) 
        {
            TranslateMessage(&msg);
            DispatchMessage(&msg);
        }
        else
        {
            Render();
        }

    }
}

我来回复

您尚未登录,请登录后再回复。点此登录或注册