| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 174 人关注过本帖, 1 人收藏
标题:运行不了,找了很久不知道错在什么地方,帮我改改,拜托了
只看楼主 加入收藏
樵头
Rank: 2
来 自:四川广安
等 级:论坛游民
帖 子:7
专家分:28
注 册:2010-9-22
收藏(1)
 问题点数:0 回复次数:3 
运行不了,找了很久不知道错在什么地方,帮我改改,拜托了
程序代码:
#include<Windows.h>
#include <d3d9.h>
#include <d3dx9.h>
#include <MMSystem.h>
#pragma comment (lib,"d3d9.lib")
#pragma comment(lib,"d3dx9.lib")
#pragma comment(lib,"winmm.lib")
LPDIRECT3D9                 g_D3D        = NULL;
LPDIRECT3DDEVICE9           g_d3dDevice  = NULL;
LPDIRECT3DVERTEXBUFFER9     g_pVB        = NULL ;

struct CUSTOMVERTEX
{
    D3DXVECTOR3 position;
    D3DXVECTOR3 normal;
};

#define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZ|D3DFVF_NORMAL)

////////////////////////////////////////////////////////////
HRESULT InitD3D(HWND hWnd)
{
    if (NULL==(g_D3D = Direct3DCreate9(D3D_SDK_VERSION)))
    {
        MessageBox(NULL,L"警告",L"g_D3D创建失败",MB_OK);
        return E_FAIL;
    }
    D3DPRESENT_PARAMETERS pddm;

    ZeroMemory( &pddm, sizeof( pddm ) );

    pddm.Windowed = TRUE ;
    pddm.SwapEffect =D3DSWAPEFFECT_DISCARD;
    pddm.BackBufferFormat = D3DFMT_UNKNOWN;
    pddm.EnableAutoDepthStencil= TRUE ;
    pddm.AutoDepthStencilFormat = D3DFMT_D16;
 
    if (FAILED(g_D3D->CreateDevice(D3DADAPTER_DEFAULT,
        D3DDEVTYPE_HAL,
        hWnd,
        D3DCREATE_SOFTWARE_VERTEXPROCESSING,
        &pddm,&g_d3dDevice)
        ))

    {
        MessageBox(NULL,L"g_d3dDevice创建失败",L"警告",MB_OK);
        return E_FAIL;
    }
    g_d3dDevice->SetRenderState(D3DRS_CULLMODE,D3DCULL_NONE );


    g_d3dDevice->SetRenderState(D3DRS_ZENABLE,TRUE);
 
    return S_OK ;

}
//////////////////////////////////////////
HRESULT InitGeometry()
{
    if (FAILED(g_d3dDevice->CreateVertexBuffer(50 * 2 * sizeof(CUSTOMVERTEX),0,
        D3DFVF_CUSTOMVERTEX,D3DPOOL_DEFAULT,&g_pVB,NULL)))
    {
        MessageBox(NULL,L"buffer创建失败",L"警告",MB_OK);
        return E_FAIL;
    }
    CUSTOMVERTEX* pVertices;
    if (FAILED(g_pVB->Lock(0,0,(void**)&pVertices,0)))
    {
        MessageBox(NULL,L"LOCK创建失败",L"警告",MB_OK);
        return E_FAIL;
    }
    for (DWORD i=0;i<50;i++)
    {
        FLOAT theta = (2*D3DX_PI*i)/(50-1);//第一个点和最后一个点是重合的所以是(50-1)个
        pVertices[2*i + 0].position = D3DXVECTOR3 (sinf(theta),-1.0f,cosf(theta));//sinf==float sin
        pVertices[2*i + 0].normal   = D3DXVECTOR3 (sinf(theta),0.0f,cosf(theta));
        pVertices[2*i + 1].position = D3DXVECTOR3 (sinf(theta),1.0f,cosf(theta));
        pVertices[2*i + 1].normal   = D3DXVECTOR3 (sinf(theta),0.0f,cosf(theta));

    }
    g_pVB->Unlock();
    return S_OK;
 
}
//////////////////////////////////////////////////////////
VOID cleanup()
{
    if (g_pVB != NULL)
    {
        g_pVB->Release();
    }
    if (g_d3dDevice != NULL)
    {
        g_d3dDevice->Release();
    }
    if (g_D3D != NULL)
    {
        g_D3D->Release();
    }
}
/////////////////////////////////////
VOID SetMatrices()
{
    D3DXMATRIXA16 matWord;
    D3DXMatrixIdentity(&matWord);
    D3DXMatrixRotationY(&matWord,timeGetTime()/1000.0f);
    g_d3dDevice->SetTransform(D3DTS_WORLD,&matWord);

    D3DXMATRIXA16 matView;
    D3DXVECTOR3 vEyePt(0.0f,3.0f,-5.0f);
    D3DXVECTOR3 vLookPt(0.0f,0.0f,0.0f);
    D3DXVECTOR3 vUpvec(0.0f,1.0f,0.0f);
    D3DXMatrixLookAtLH(&matView,&vEyePt,&vLookPt,&vUpvec);
    g_d3dDevice->SetTransform(D3DTS_VIEW,&matView);

    D3DXMATRIXA16 matPro;
    D3DXMatrixPerspectiveFovLH(&matPro,D3DX_PI/4.0,1.0f,1.0f,100.0f);
    g_d3dDevice->SetTransform(D3DTS_PROJECTION,&matPro);
}
/////////////////////////////////////
VOID SetupLights()
{
    D3DMATERIAL9 mTrl;
    ZeroMemory(&mTrl,sizeof(D3DMATERIAL9));////////??
    //ZeroMemory(&mTrl,sizeof(mTrl));
 
    mTrl.Diffuse.b = mTrl.Ambient.b = 1.0f;
    mTrl.Diffuse.g = mTrl.Ambient.g = 1.0f;
    mTrl.Ambient.r = mTrl.Ambient.r = 1.0f;
    mTrl.Diffuse.a = mTrl.Ambient.a = 1.0f;
    g_d3dDevice->SetMaterial(&mTrl);

    D3DXVECTOR3 vecDir;
    D3DLIGHT9    light;
    ZeroMemory(&light,sizeof(D3DLIGHT9));
    light.Type = D3DLIGHT_DIRECTIONAL;
    light.Diffuse.r = 1.0f;
    light.Diffuse.g = 1.0f;
    light.Diffuse.b= 1.0f;
    vecDir = D3DXVECTOR3 (cosf(timeGetTime()/350.0f),1.0f,sinf(timeGetTime()/350.0f));
    D3DXVec3Normalize((D3DXVECTOR3 *)&light.Direction,&vecDir);
 
    light.Range = 1000.0f;
    g_d3dDevice->SetLight(0,&light);
    g_d3dDevice->LightEnable(0,TRUE);
    g_d3dDevice->SetRenderState(D3DRS_LIGHTING,TRUE);

    g_d3dDevice->SetRenderState(D3DRS_AMBIENT,0x00202020 );

}
//////////////////////////////////////////////////////////////
VOID Render()
{
    g_d3dDevice->Clear(0,NULL,D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER,D3DCOLOR_XRGB(0,0,255),1.0f,0);
    if(SUCCEEDED(g_d3dDevice->BeginScene()))
    {
        SetupLights();
        SetMatrices();

        g_d3dDevice->SetStreamSource(0,g_pVB,0,sizeof(CUSTOMVERTEX));
        g_d3dDevice->SetFVF(D3DFVF_CUSTOMVERTEX);
        g_d3dDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP,0,2 * 50 - 2);
        g_d3dDevice->EndScene();
    }
        g_d3dDevice->Present(NULL,NULL,NULL,NULL);
}
/////////////////////////////////////////////
HRESULT WINAPI MsgPro(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam)
{
    switch (msg)
    {
    case WM_DESTROY :
        cleanup();
        PostQuitMessage(0);
        return 0;
    }
    return DefWindowProc(hWnd,msg,wParam,lParam);
}

INT WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nShowCmd)
{
    WNDCLASSEX wecx;
    wecx.cbSize = sizeof(WNDCLASSEX);
    wecx.lpfnWndProc = MsgPro;
    wecx.cbClsExtra = 0;
    wecx.cbWndExtra = 0;
    wecx.hbrBackground=NULL;
    wecx.hCursor =NULL;
    wecx.hIcon = NULL;
    wecx.hIconSm =NULL;
    wecx.hInstance = GetModuleHandle(NULL);
    wecx.lpszClassName =NULL;
    wecx.lpszMenuName = L"light";
    RegisterClassEx(&wecx);
    //{
    //    MessageBox(NULL,L"注册窗口失败",L"警告",MB_OK);
    //}
    HWND hWnd;
    hWnd = CreateWindow(L"light",L"灯光",WS_OVERLAPPEDWINDOW,0,0,
        GetSystemMetrics(SM_CXSCREEN),GetSystemMetrics(SM_CYSCREEN),
        NULL,NULL,wecx.hInstance,NULL);
    if (SUCCEEDED(InitD3D(hWnd)))
    {
        if (SUCCEEDED(InitGeometry()))
        {
            ShowWindow(hWnd,SW_SHOWDEFAULT);
            UpdateWindow(hWnd);
            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(L"light",wecx.hInstance);
    return 0;
}


[ 本帖最后由 樵头 于 2011-2-14 21:09 编辑 ]
2011-02-14 21:01
变幻小子
Rank: 6Rank: 6
来 自:广东陆丰
等 级:侠之大者
帖 子:188
专家分:473
注 册:2011-3-4
收藏
得分:0 
这个有待研究  学习

明天的梦
2011-03-22 21:30
liyanlong
Rank: 2
来 自:平南
等 级:论坛游民
帖 子:5
专家分:11
注 册:2011-4-13
收藏
得分:0 

这个问题你该到C语言的论坛去问一下,应该会得答案
2011-04-14 22:14
无语的小虾米
Rank: 1
等 级:新手上路
帖 子:10
专家分:4
注 册:2011-4-16
收藏
得分:0 
太长了,先标记一下,在学习!
2011-04-24 17:08
快速回复:运行不了,找了很久不知道错在什么地方,帮我改改,拜托了
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.017926 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved