How to use OpenGL through the mingw compiler in Windows

Source: Internet
Author: User

In Windows, Microsoft's Visual C compiler supports the c99 standard very poorly. In addition, the GNU-Based C compiler (GCC, llvm clang, etc) c99 can be directly used with the GNU extension language feature (known as the gnu99 standard). After llvm clang3.0, gnu11 can be well supported. Therefore, it is most appropriate to select a GNU standard compiler for C language-based software development.

Mingw is an integration of the GCC compiler on the Windows platform. It contains most windwos APIs and can be used directly. In addition, there are also d3d8 and d3d9 libraries.

Download the mingw installer to this address: http://sourceforge.net/projects/mingw/files/

Then click"Download mingw-get-inst-xxxx.exe"This hyperlink can be downloaded.

When installing mingw, you can select the components to be installed. Of course, even a full selection won't take up much space.

After mingw is installed, add the bin directory under the mingw directory to the environment variable path. Then, we can use the eclipse for CPP ide to edit and debug the code. After eclipse is enabled, the IDE will search for the GCC compiler by itself, which is very convenient.

Before using eclipse and mingw to write OpenGL code, add necessary libraries. Go to project> Properties> C/C ++ build> Settings> mingw C linker. Then, add the opengl32, glu32, Glaux, and GDI32 libraries to libraries (-l) respectively. Then we can test it through the following code:

/* ============================================================================ Name        : openglTest.c Author      : Zenny Chen Version     : Copyright   : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ */#include <windows.h>        /* must include this before GL/gl.h */#include <GL/gl.h>          /* OpenGL header file */#include <GL/glu.h>         /* OpenGL utilities header file */static void display(){    glClear(GL_COLOR_BUFFER_BIT);    glDrawArrays(GL_TRIANGLES, 0, 3);    glFlush();}static void InitGLContext(GLsizei width, GLsizei height){    glClearColor(0.4f, 0.4f, 0.4f, 1.0f);    glViewport(0, 0, width, height);    glEnableClientState(GL_COLOR_ARRAY);    glEnableClientState(GL_VERTEX_ARRAY);    static const GLfloat vertices[] = {        0.0f, 1.0f,        -1.0f, -1.0f,        1.0f, -1.0f    };    glVertexPointer(2, GL_FLOAT, 0, vertices);    static const GLfloat colors[] = {        1.0f, 0.0f, 0.0f, 1.0f,        0.0f, 1.0f, 0.0f, 1.0f,        0.0f, 0.0f, 1.0f, 1.0f    };    glColorPointer(4, GL_FLOAT, 0, colors);    glEnable(GL_CULL_FACE);    glFrontFace(GL_CCW);    glCullFace(GL_BACK);}static LONG WINAPI WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam){    static PAINTSTRUCT ps;    switch(uMsg)    {    case WM_PAINT:        display();        BeginPaint(hWnd, &ps);        EndPaint(hWnd, &ps);        return 0;    case WM_SIZE:        InitGLContext(LOWORD(lParam), HIWORD(lParam));        PostMessage(hWnd, WM_PAINT, 0, 0);        return 0;    case WM_CHAR:        switch (wParam)        {        case 27:            /* ESC key */            PostQuitMessage(0);            break;        }        return 0;    case WM_CLOSE:        PostQuitMessage(0);        return 0;    }    return DefWindowProc(hWnd, uMsg, wParam, lParam);}static HWND CreateOpenGLWindow(char* title, int x, int y, int width, int height,           BYTE type, DWORD flags){    int         pf;    HDC         hDC;    HWND        hWnd;    WNDCLASS    wc;    PIXELFORMATDESCRIPTOR pfd;    static HINSTANCE hInstance = 0;    /* only register the window class once - use hInstance as a flag. */    if (!hInstance)    {        hInstance = GetModuleHandle(NULL);        wc.style         = CS_OWNDC;        wc.lpfnWndProc   = (WNDPROC)WindowProc;        wc.cbClsExtra    = 0;        wc.cbWndExtra    = 0;        wc.hInstance     = hInstance;        wc.hIcon         = LoadIcon(NULL, IDI_WINLOGO);        wc.hCursor       = LoadCursor(NULL, IDC_ARROW);        wc.hbrBackground = NULL;        wc.lpszMenuName  = NULL;        wc.lpszClassName = "OpenGL";        if (!RegisterClass(&wc))        {            MessageBox(NULL, "RegisterClass() failed:  "                   "Cannot register window class.", "Error", MB_OK);            return NULL;        }    }    hWnd = CreateWindow("OpenGL", title, WS_OVERLAPPEDWINDOW |            WS_CLIPSIBLINGS | WS_CLIPCHILDREN,            x, y, width, height, NULL, NULL, hInstance, NULL);    if (hWnd == NULL)    {        MessageBox(NULL, "CreateWindow() failed:  Cannot create a window.",               "Error", MB_OK);        return NULL;    }    hDC = GetDC(hWnd);    /* there is no guarantee that the contents of the stack that become       the pfd are zeroed, therefore _make sure_ to clear these bits. */    memset(&pfd, 0, sizeof(pfd));    pfd.nSize        = sizeof(pfd);    pfd.nVersion     = 1;    pfd.dwFlags      = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | flags;    pfd.iPixelType   = type;    pfd.cColorBits   = 32;    pf = ChoosePixelFormat(hDC, &pfd);    if (pf == 0)    {        MessageBox(NULL, "ChoosePixelFormat() failed:  "               "Cannot find a suitable pixel format.", "Error", MB_OK);        return 0;    }    if (SetPixelFormat(hDC, pf, &pfd) == FALSE)    {        MessageBox(NULL, "SetPixelFormat() failed:  "               "Cannot set format specified.", "Error", MB_OK);        return 0;    }    DescribePixelFormat(hDC, pf, sizeof(PIXELFORMATDESCRIPTOR), &pfd);    ReleaseDC(hWnd, hDC);    return hWnd;}int APIENTRY WinMain(HINSTANCE hCurrentInst, HINSTANCE hPreviousInst,    LPSTR lpszCmdLine, int nCmdShow){    HDC hDC;                /* device context */    HGLRC hRC;              /* opengl context */    HWND  hWnd;             /* window */    MSG   msg;              /* message */    hWnd = CreateOpenGLWindow("minimal", 0, 0, 256, 256, PFD_TYPE_RGBA, 0);    if (hWnd == NULL)    exit(1);    hDC = GetDC(hWnd);    hRC = wglCreateContext(hDC);    wglMakeCurrent(hDC, hRC);    ShowWindow(hWnd, nCmdShow);    while(GetMessage(&msg, hWnd, 0, 0))    {        TranslateMessage(&msg);        DispatchMessage(&msg);    }    wglMakeCurrent(NULL, NULL);    ReleaseDC(hWnd, hDC);    wglDeleteContext(hRC);    DestroyWindow(hWnd);    return msg.wParam;}

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.