OpenGL 简单图形程序

1. Tool : codeblock 10.05

2.Code:

#include 
#include 
#include 
#define PI 3.1415926535898

GLubyte fly[] =
{
    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    0x03, 0x80, 0x01, 0xC0, 0x06, 0xC0, 0x03, 0x60,
    0x04, 0x60, 0x06, 0x20, 0x04, 0x30, 0x0C, 0x20,
    0x04, 0x18, 0x18, 0x20, 0x04, 0x0C, 0x30, 0x20,
    0x04, 0x06, 0x60, 0x20, 0x44, 0x03, 0xC0, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x44, 0x01, 0x80, 0x22, 0x44, 0x01, 0x80, 0x22,
    0x66, 0x01, 0x80, 0x66, 0x33, 0x01, 0x80, 0xCC,
    0x19, 0x81, 0x81, 0x98, 0x0C, 0xC1, 0x83, 0x30,
    0x07, 0xe1, 0x87, 0xe0, 0x03, 0x3f, 0xfc, 0xc0,
    0x03, 0x31, 0x8c, 0xc0, 0x03, 0x33, 0xcc, 0xc0,
    0x06, 0x64, 0x26, 0x60, 0x0c, 0xcc, 0x33, 0x30,
    0x18, 0xcc, 0x33, 0x18, 0x10, 0xc4, 0x23, 0x08,
    0x10, 0x63, 0xC6, 0x08, 0x10, 0x30, 0x0c, 0x08,
    0x10, 0x18, 0x18, 0x08, 0x10, 0x00, 0x00, 0x08
};
void draw_fly()
{
    //glBegin(GL_POINTS);
    glColor3f(0.0,1.0,0.0);
    glEnable (GL_POLYGON_STIPPLE);
    glPolygonStipple (fly);
    glRectf(0.4, 0.4, 0.4, 0.4);
    glDisable (GL_POLYGON_STIPPLE);
    glEnd();
}
void draw_point()
{
    glPointSize(8.0);   //设置点大小,不能再glBegin和glEnd之间
    glBegin(GL_POINTS);
    glColor3f(0.0,1.0,0.0);
    glVertex2f(-0.4, -0.4);
    glColor3f(1.0,0.0,1.0);
    glVertex2f(-0.4,-0.3);
    glColor3f(0.0,1.0,1.0);
    glVertex2f(-0.4,-0.2);
    glColor3f(1.0,1.0,0.0);
    glVertex2f(0-0.4,-0.1);
    glEnd();
}
void draw_line_loop()
{
    glBegin(GL_LINE_LOOP);  //mode为GL_LINE_LOOP
    glVertex2f(-0.3, -0.3);
    glVertex2f(-0.3,0.5);
    glVertex2f(0.0,0.4);
    glVertex2f(0.5,0.5);
    glVertex2f(0.5,-0.5);
    glEnd();
}

void draw_line_strip()
{

    glColor3f(1.0, 0.1, 0.1);// 绘制矩形
    glBegin(GL_LINE_STRIP);  //mode为GL_LINE_STRIP
    glVertex2f(-0.1, -0.1);
    glVertex2f(-0.2,0.22);
    glVertex2f(0.3,0.15);
    glVertex2f(0.2,-0.3);
    glEnd();
}
void draw_rec()
{

    glBegin(GL_LINE_LOOP);  //mode为GL_LINE_LOOP
    glVertex2f(-0.5, -0.5);
    glVertex2f(-0.5,0.5);
    glVertex2f(0.5,0.5);
    glVertex2f(0.5,-0.5);
    glEnd();
}
void draw_elipse()
{
    int i;
    GLfloat angle;
    GLint circle_points = 100;
    glBegin(GL_LINE_LOOP);
    for (i = 0; i < circle_points; i++)
    {
        angle = 2*PI*i/circle_points;
        glVertex2f(cos(angle), sin(angle));
    }
    glEnd();
}
void draw_line()
{

    glBegin(GL_LINES);
    glVertex3f(25.0f,160.0f,0.0f);
    glVertex3f(225.0f,160.0f,0.0f);
    glEnd();
}

void draw_rect()
{
    glColor4f(0.0, 0.0, 0.0, 0.5);// 绘制矩形
    glRectf(-0.2, -0.2, 0.2, 0.2);
    glEnd();

}
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);


int WINAPI WinMain(HINSTANCE hInstance,
                   HINSTANCE hPrevInstance,
                   LPSTR lpCmdLine,
                   int nCmdShow)
{
    WNDCLASSEX wcex;
    HWND hwnd;
    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL bQuit = FALSE;
    float theta = 0.5f;


    /* 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 test",
                          WS_OVERLAPPEDWINDOW,
                          CW_USEDEFAULT,
                          CW_USEDEFAULT,
                          600,
                          600,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hwnd, nCmdShow);

    /* enable OpenGL for the window */
    EnableOpenGL(hwnd, &hDC, &hRC);
    //glOrtho(-300, 300, 300, 300, 0, 500);

    /* 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=2;
            glLineWidth(curSizeLine);
            glPushMatrix();
            // glRotatef(theta, 1.0f, 1.0f, 1.0f);
            glColor3f(0.5f,0.5f,0.1f);

            draw_elipse();
            draw_rec();
            draw_line();
            //draw_rect();
            draw_fly();
            draw_line_loop();
            draw_line_strip();
            draw_point();
            //glPolygonStipple (fly);
            //   glPolygonStipple (halftone);
            glEnd();
            glPopMatrix();
            SwapBuffers(hDC);
            theta += 4.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 = 32;
    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);
}

3. Outputs

OpenGL 简单图形程序_第1张图片


你可能感兴趣的:(OpenGL 简单图形程序)