OpenGL制作简易地图(一)

来源:互联网 发布:c语言的函数和数学函数 编辑:程序博客网 时间:2024/04/29 17:07

1.随机生成一张地图,黑方块为禁区,白方块为活动区

2.随机在活动区找两个块,红方块为起点,蓝方块为终点。

3.生成该地图。

4.上CODE.

//alex.shoal@gmail.com//latest update:2012-06-19//sourse code location:..\CB_CODE\GL_TREE#include <windows.h>#include <time.h>#include <gl/gl.h>#define N 50#define WIDTH 800#define UNIT 2.0f/Nint MAP[N][N];LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);void DisableOpenGL(HWND, HDC, HGLRC);void draw_rectfill(){    glColor4f(0.0, 0.0, 0.0, 0.5);// 绘制矩形    glRectf(-0.2, -0.2, 0.2, 0.2);    glEnd();}void init_field(){    for (int i=0; i<N; i++)        for (int j=0; j<N; j++)            MAP[i][j]=0;}void set_block(){    srand(time(NULL));    for(int i=0; i<800; i++)    {        int x=rand()%N;        int y=rand()%N;        MAP[x][y]=1;    }}void set_positon(){    int num=0;    srand(time(NULL));    while (num<2)    {        int i=rand()%N;        int j=rand()%N;        if(MAP[i][j]==0)        {            MAP[i][j]=2+num++; // MAP[][]==2,MEANS THIS IS A START POSITION.        }    }}void draw_PickPOS(){    glColor4f(0.0, 0.0, 0.0, 0.5);// 绘制矩形    for(int i=0; i<N; i++)        for (int j=0; j<N; j++)        {            if(MAP[i][j]==2)            {   glColor4f(1.0, 0.0, 0.0, 0.5);// 绘制矩形                GLfloat x1=i*UNIT-1.0f;                GLfloat y1=j*UNIT-1.0f;                GLfloat x2=x1+UNIT;                GLfloat y2=y1+UNIT;                glRectf(x1, y1, x2, y2);            }            else if(MAP[i][j]==3)            {                glColor4f(0.0, 0.0, 1.0, 0.5);// 绘制矩形}                GLfloat x1=i*UNIT-1.0f;                GLfloat y1=j*UNIT-1.0f;                GLfloat x2=x1+UNIT;                GLfloat y2=y1+UNIT;                glRectf(x1, y1, x2, y2);            }        }         glEnd();}void draw_grid(){    glColor3f(0.0, 0.0, 0.0);// 绘制矩形    glBegin(GL_LINES);  //mode为GL_LINE_LOOP    for (int i=0; i<N; i++)    {        GLfloat x=i*UNIT-1.0f;        glVertex2f(x, -1.0f);        glVertex2f(x,1.0f);    }    for (int j=0; j<N; j++)    {        GLfloat y= j*UNIT-1.0f;        glVertex2f(-1.0f, y);        glVertex2f(1.0f,y);    }    glEnd();}void draw_block(){    glColor4f(0.0, 0.0, 0.0, 0.5);// 绘制矩形    for(int i=0; i<N; i++)        for (int j=0; j<N; j++)        {            if(MAP[i][j]==1)            {                GLfloat x1=i*UNIT-1.0f;                GLfloat y1=j*UNIT-1.0f;                GLfloat x2=x1+UNIT;                GLfloat y2=y1+UNIT;                glRectf(x1, y1, x2, y2);            }        }    glEnd();}int WINAPI WinMain(HINSTANCE hInstance,                   HINSTANCE hPrevInstance,                   LPSTR lpCmdLine,                   int nCmdShow){    WNDCLASSEX wcex;    HWND hwnd;    HDC hDC;    HGLRC hRC;    MSG msg;    BOOL bQuit = FALSE;    /* register window class */    wcex.cbSize = sizeof(WNDCLASSEX);    wcex.style = CS_OWNDC;    wcex.lpfnWndProc = WindowProc;    wcex.cbClsExtra = 0;    wcex.cbWndExtra = 0;    wcex.hInstance = hInstance;    wcex.hIcon = LoadIcon(NULL, IDI_APPLICATION);    wcex.hCursor = LoadCursor(NULL, IDC_ARROW);    wcex.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);    wcex.lpszMenuName = NULL;    wcex.lpszClassName = "GLSample";    wcex.hIconSm = LoadIcon(NULL, IDI_APPLICATION);;    if (!RegisterClassEx(&wcex))        return 0;    /* create main window */    hwnd = CreateWindowEx(0,                          "GLSample",                          "OpenGL Sample",                          WS_OVERLAPPEDWINDOW,                          CW_USEDEFAULT,                          CW_USEDEFAULT,                          WIDTH,                          WIDTH,                          NULL,                          NULL,                          hInstance,                          NULL);    ShowWindow(hwnd, nCmdShow);    /* enable OpenGL for the window */    EnableOpenGL(hwnd, &hDC, &hRC);    set_block();    set_positon();    /* program main loop */    while (!bQuit)    {        /* check for messages */        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))        {            /* handle or dispatch messages */            if (msg.message == WM_QUIT)            {                bQuit = TRUE;            }            else            {                TranslateMessage(&msg);                DispatchMessage(&msg);            }        }        else        {            /* OpenGL animation code goes here */            glClearColor(1.0f, 1.0f, 1.0f, 1.0f);            glClear(GL_COLOR_BUFFER_BIT);            GLfloat curSizeLine=1;            glLineWidth(curSizeLine);            glPushMatrix();            //glRotatef(theta, 0.0f, 0.0f, 1.0f);            draw_grid();            draw_block();            draw_PickPOS();            glPopMatrix();            SwapBuffers(hDC);            // theta += 1.0f;            Sleep (1);        }    }    /* shutdown OpenGL */    DisableOpenGL(hwnd, hDC, hRC);    /* destroy the window explicitly */    DestroyWindow(hwnd);    return msg.wParam;}LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam){    switch (uMsg)    {    case WM_CLOSE:        PostQuitMessage(0);        break;    case WM_DESTROY:        return 0;    case WM_KEYDOWN:    {        switch (wParam)        {        case VK_ESCAPE:            PostQuitMessage(0);            break;        }    }    break;    default:        return DefWindowProc(hwnd, uMsg, wParam, lParam);    }    return 0;}void EnableOpenGL(HWND hwnd, HDC* hDC, HGLRC* hRC){    PIXELFORMATDESCRIPTOR pfd;    int iFormat;    /* get the device context (DC) */    *hDC = GetDC(hwnd);    /* set the pixel format for the DC */    ZeroMemory(&pfd, sizeof(pfd));    pfd.nSize = sizeof(pfd);    pfd.nVersion = 1;    pfd.dwFlags = PFD_DRAW_TO_WINDOW |                  PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;    pfd.iPixelType = PFD_TYPE_RGBA;    pfd.cColorBits = 24;    pfd.cDepthBits = 16;    pfd.iLayerType = PFD_MAIN_PLANE;    iFormat = ChoosePixelFormat(*hDC, &pfd);    SetPixelFormat(*hDC, iFormat, &pfd);    /* create and enable the render context (RC) */    *hRC = wglCreateContext(*hDC);    wglMakeCurrent(*hDC, *hRC);}void DisableOpenGL (HWND hwnd, HDC hDC, HGLRC hRC){    wglMakeCurrent(NULL, NULL);    wglDeleteContext(hRC);    ReleaseDC(hwnd, hDC);}


5.结果

结果1.

结果2

6.寻找从红方块至蓝方块的最佳路径(先到这吧,有空再说)