0

当我尝试加载大小大于 128*128 的 heighmap.raw 时,出现堆栈溢出错误

谁能帮我吗?

#include<Windows.h>
#include<WindowsX.h>
#include<d3d9.h>
#include<d3dx9.h>
#define CUSTOMFVF (D3DFVF_XYZ|D3DFVF_DIFFUSE)
#define CENTERX 400
#define CENTERY 300
#define RADIUS 100
#include<fstream> 
#define WIDTH  64
#define HEIGHT 64


float eyeX;
float eyeY;
float eyeZ;

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

IDirect3D9 *pDirect3D = NULL;
IDirect3DDevice9 *pDevice = NULL;
IDirect3DVertexBuffer9 *pV_Buffer = NULL;
IDirect3DIndexBuffer9 *pV_Index = NULL;
D3DPRESENT_PARAMETERS pp = {0};
D3DVIEWPORT9 vp={0};
void InitDirectx(HWND hWnd);
bool render();
void Init_graphics(void);
void cleanD3D(void);

struct OURCUSTOMVERTEX
{
    float x,y,z;
     DWORD color;
};


D3DXMATRIX matView;
D3DXMATRIX matProjection;
D3DXMATRIX matWorld;

LRESULT CALLBACK WindowProc(HWND hWnd,UINT msg,WPARAM wParam,LPARAM lParam);

int WINAPI WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR cmdline,int nCmdShow)
{
    WNDCLASSEX wcex = {0};

    wcex.cbSize = sizeof(WNDCLASSEX);
    wcex.style = CS_HREDRAW | CS_VREDRAW;
    wcex.hInstance = hInstance;
    wcex.lpszClassName = TEXT("DirectX Window");
    wcex.lpfnWndProc = WindowProc;

    ATOM result = RegisterClassEx (&wcex);

    if(result == 0)
    {
        MessageBox(NULL, TEXT("Register class failed"), TEXT("Error"), MB_OK);
    }
    HWND hWnd = NULL;

        hWnd = CreateWindowEx(0,wcex.lpszClassName,TEXT("DirectX Window"),WS_OVERLAPPEDWINDOW,CW_USEDEFAULT,CW_USEDEFAULT,800, 600,NULL,NULL,hInstance,NULL);

    if(hWnd == NULL)
    {
        MessageBox(NULL,TEXT("Window Creation Failed"),TEXT("ERROR"),MB_OK);
    }
    ShowWindow(hWnd,nCmdShow);

    InitDirectx(hWnd);

    MSG msg = {0};

    while(true)
    {
        PeekMessage(&msg,NULL,0,0,PM_REMOVE);
        if(msg.message == WM_QUIT)
            break;
        DispatchMessage(&msg);
        render();
    }
}
LRESULT CALLBACK WindowProc(HWND hwnd,UINT message,WPARAM wparam,LPARAM lparam)
{
    switch(message)
    {
    case WM_KEYDOWN:
        switch(wparam)
        {
        case VK_UP:
            eyeZ = eyeZ - .05;
            break;
        case VK_DOWN:
            eyeZ = eyeZ  + .05;
            break;
        case VK_LEFT:
            eyeX = eyeX - .05;
            break;
        case VK_RIGHT:
            eyeX = eyeX + .05;
            break;
        }
        break;
    case WM_DESTROY:
        {
            PostQuitMessage(0);
            break;
        }
    default : 
        return DefWindowProc(hwnd,message,wparam,lparam);
    }
    return 0;
}

void InitDirectx(HWND hWnd)
{
    pDirect3D = Direct3DCreate9(D3D_SDK_VERSION);

    pp.BackBufferFormat=D3DFMT_A8R8G8B8;
    pp.BackBufferWidth = 800;
    pp.BackBufferHeight = 600;
    pp.hDeviceWindow=hWnd;
    pp.SwapEffect=D3DSWAPEFFECT_DISCARD;
    pp.Windowed =true;

    pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,D3DDEVTYPE_HAL,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&pp,&pDevice);


    pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
    Init_graphics();
}
bool render()
{
    pDevice->Clear(0,NULL,D3DCLEAR_TARGET,D3DCOLOR_XRGB(0,0,0),0.0,NULL);

    pDevice->BeginScene();
    {

    /** create the view matrix **/
    D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(eyeX, eyeY, eyeZ), &D3DXVECTOR3(0, 0, 1), &D3DXVECTOR3(0, 1, 0));

    pDevice->SetFVF(CUSTOMFVF);

    pDevice->SetTransform(D3DTS_WORLD, &matWorld);
    pDevice->SetTransform(D3DTS_VIEW, &matView);
    pDevice->SetTransform(D3DTS_PROJECTION, &matProjection);

    pDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
    pDevice->SetStreamSource(0,pV_Buffer,0,sizeof(OURCUSTOMVERTEX));
   // pDevice->DrawPrimitive(D3DPT_TRIANGLESTRIP, 2);
    pDevice->SetIndices(pV_Index);
    pDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0, 0, WIDTH*HEIGHT, 0, (WIDTH-1)*(HEIGHT-1)*2);
    }
    pDevice->EndScene();

    pDevice->Present(NULL,NULL,0,NULL);

    return true;


}

void CleanD3D(void)
{
    pV_Buffer->Release();

    pDevice->Release();

    pDirect3D->Release();
}

void Init_graphics(void)
{
    OURCUSTOMVERTEX Vertices[WIDTH*HEIGHT];
     float flt_HeightData[WIDTH][HEIGHT];

     flt_HeightData[0][0]=0;
     flt_HeightData[1][0]=0;
     flt_HeightData[2][0]=0;
     flt_HeightData[3][0]=0;

     flt_HeightData[0][1]=1;
     flt_HeightData[1][1]=0;
     flt_HeightData[2][1]=2;
     flt_HeightData[3][1]=2;

     flt_HeightData[0][2]=2;
     flt_HeightData[1][2]=2;
     flt_HeightData[2][2]=4;
     flt_HeightData[3][2]=2;
  std::ifstream f_DataFile;

     f_DataFile.open("heightdata.raw", std::ios::binary);

     if (f_DataFile.is_open())
     {

    for (int x=0;x< WIDTH;x++)    {

        for (int y=0; y< HEIGHT;y++)        {
            Vertices[y*WIDTH + x].x = -x;
            Vertices[y*WIDTH + x].y = y;
            Vertices[y*WIDTH + x].z =  f_DataFile.get()/50;
            Vertices[y*WIDTH + x].color = 0xffffffff;
        }
    }

     }
     f_DataFile.close();
        pDevice->CreateVertexBuffer(WIDTH*HEIGHT*sizeof(OURCUSTOMVERTEX), 
        0,
    CUSTOMFVF,
    D3DPOOL_DEFAULT,
    &pV_Buffer,
    NULL);

    VOID *pVoid = NULL;



    pV_Buffer->Lock(0, (WIDTH-1)*(HEIGHT-1)*6*sizeof(OURCUSTOMVERTEX),(void**)&pVoid,0);
    memcpy(pVoid,Vertices,WIDTH*HEIGHT*sizeof(OURCUSTOMVERTEX));
    pV_Buffer->Unlock();




     short s_Indices[(WIDTH-1)*(HEIGHT-1)*6];


    for (int x=0;x< WIDTH-1;x++)    {

        for (int y=0; y< HEIGHT-1;y++)        {
            s_Indices[(x+y*(WIDTH-1))*6+2] = x+y*WIDTH;
            s_Indices[(x+y*(WIDTH-1))*6+1] = (x+1)+y*WIDTH;
            s_Indices[(x+y*(WIDTH-1))*6] = (x+1)+(y+1)*WIDTH;

            s_Indices[(x+y*(WIDTH-1))*6+3] = (x+1)+(y+1)*WIDTH;
            s_Indices[(x+y*(WIDTH-1))*6+4] = x+y*WIDTH;
            s_Indices[(x+y*(WIDTH-1))*6+5] = x+(y+1)*WIDTH;

        }
    }
    pDevice->CreateIndexBuffer((WIDTH-1)*(HEIGHT-1)*6*sizeof(short),    
          0,
        D3DFMT_INDEX16,
        D3DPOOL_MANAGED,
        &pV_Index,
        NULL);

        VOID *pVoid2= NULL;

    pV_Index->Lock(0, (WIDTH-1)*(HEIGHT-1)*6*sizeof(short),(void**)&pVoid2,0);
    memcpy(pVoid2, s_Indices,(WIDTH-1)*(HEIGHT-1)*6*sizeof(short));
    pV_Index->Unlock();

     pDevice->SetRenderState(D3DRS_CULLMODE, D3DCULL_NONE);
     pDevice->SetRenderState(D3DRS_LIGHTING,false);
     pDevice->SetRenderState(D3DRS_FILLMODE,D3DFILL_WIREFRAME);










    /** Reset the world matrix to identity **/
    D3DXMatrixIdentity(&matWorld);


    /** create the projection matrix **/
    D3DXMatrixPerspectiveFovLH(&matProjection, 60, 1.6666F, 1, 1000);

    vp.X= 0;
    vp.Y = 0;
    vp.Width = WIDTH;
    vp.Height = HEIGHT;

    //pDevice->SetViewport(&vp);


    eyeX = 0;
    eyeY = 0;
    eyeZ = +5;

}
4

3 回答 3

4

增加堆栈的大小,或者最好在堆上分配数组。在 C++ 中执行此操作的最简单方法是使用std::vector.

于 2013-04-07T07:22:19.833 回答
2

这些

OURCUSTOMVERTEX Vertices[WIDTH*HEIGHT];
float flt_HeightData[WIDTH][HEIGHT];

太大而不能放在堆栈上。您可以使用new动态分配它们。

OURCUSTOMVERTEX* Vertices = new OURCUSTOMVERTEX[WIDTH*HEIGHT];
float* flt_HeightData = new float[WIDTH*HEIGHT];

这意味着您需要了解动态堆分配。特别是你会遇到二维flt_HeightData数组的问题。

更好,更简单的方法是使用std::vector

std::vector<OURCUSTOMVERTEX> Vertices(WIDTH*HEIGHT);
std::vector<float> flt_HeightData(WIDTH*HEIGHT);

但又会flt_HeightData引起问题。

我想你有一些阅读要做,有很多东西要学。

于 2013-04-07T07:23:15.167 回答
1

我想这是罪魁祸首:

OURCUSTOMVERTEX Vertices[WIDTH*HEIGHT];

在堆上分配它,例如:

OURCUSTOMVERTEX* Vertices = new OURCUSTOMVERTEX[WIDTH*HEIGHT];
于 2013-04-07T07:23:46.163 回答