程式語言 - Wine - C/C++ - OpenGL 1.0 - Draw Disk



參考資訊:
https://www.winehq.org/
https://github.com/gamedev-net/nehe-opengl
https://wiki.winehq.org/Winelib_User%27s_Guide

main.c

#include <math.h>
#include <stdbool.h>
#include <windows.h>
#include <GL/gl.h>
#include <GL/glu.h>
    
#define W 320
#define H 240
    
static WNDPROC defWndProc = NULL;
   
static LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    switch (uMsg) {
    case WM_CLOSE:
        DestroyWindow(hWnd);
        return 0;
    case WM_DESTROY:
        PostQuitMessage(0);
        return 0;
    }
    return CallWindowProc(defWndProc, hWnd, uMsg, wParam, lParam);
}
     
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
    GLuint tex = 0;
    HDC hDC = NULL;
    HGLRC hRC = NULL;
    HWND hWnd = NULL;
    GLuint fmt = 0;
    PIXELFORMATDESCRIPTOR pfd = { 0 };
    GLdouble nearPlane = 0.1;
    GLdouble farPlane = 100.0;
    GLdouble top = nearPlane * tan(45.0 * 3.14159 / 360.0);
    GLdouble right = top * ((GLfloat)W / (GLfloat)H);
   
    pfd.nSize = sizeof(PIXELFORMATDESCRIPTOR);
    pfd.nVersion = 1;
    pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
    pfd.iPixelType = PFD_TYPE_RGBA;
    pfd.cColorBits = 16;
    pfd.cDepthBits = 16;
    pfd.iLayerType = PFD_MAIN_PLANE;
    
    hWnd = CreateWindow(WC_DIALOG, "main", WS_OVERLAPPEDWINDOW | WS_VISIBLE, 0, 0, W, H, NULL, NULL, NULL, NULL);
    defWndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (long int)WndProc);
        
    hDC = GetDC(hWnd);
    fmt = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, fmt, &pfd);
    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);
  
    BITMAP bm = { 0 };
    BITMAPINFO bmi = { 0 };
    HBITMAP bmp = (HBITMAP)LoadImageA(NULL, "main.bmp", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
    GetObject(bmp, sizeof(BITMAP), &bm);
  
    bmi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
    bmi.bmiHeader.biWidth = bm.bmWidth;
    bmi.bmiHeader.biHeight = -bm.bmHeight;
    bmi.bmiHeader.biPlanes = 1;
    bmi.bmiHeader.biBitCount = 24;
    bmi.bmiHeader.biCompression = BI_RGB;
  
    int size = bm.bmWidth * bm.bmHeight * 3;
    BYTE *pixels = HeapAlloc(GetProcessHeap(), 0, size);
    HDC hdc = GetDC(NULL);
    GetDIBits(hdc, bmp, 0, bm.bmHeight, pixels, &bmi, DIB_RGB_COLORS);
    ReleaseDC(NULL, hdc);
  
    glGenTextures(1, &tex);
    glBindTexture(GL_TEXTURE_2D, tex);
    glEnable(GL_TEXTURE_2D);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, bm.bmWidth, bm.bmHeight, 0, GL_BGR, GL_UNSIGNED_BYTE, pixels);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
   
    HeapFree(GetProcessHeap(), 0, pixels);
    DeleteObject(bmp);
 
    RECT rc = { 0 };
    GetClientRect(hWnd, &rc);
    glViewport(0, 0, rc.right - rc.left, rc.bottom - rc.top);
 
    glMatrixMode(GL_PROJECTION);
    glFrustum(-right, right, -top, top, nearPlane, farPlane);
    
    glMatrixMode(GL_MODELVIEW);
    glLoadIdentity();
 
    typedef GLUquadric *(WINAPI *PFN_gluNewQuadric)(void);
    typedef void (WINAPI *PFN_gluDeleteQuadric)(GLUquadric *);
    typedef void (WINAPI *PFN_gluQuadricDrawStyle)(GLUquadric *, GLenum);
    typedef void (WINAPI *PFN_gluQuadricNormals)(GLUquadric *, GLenum);
    typedef void (WINAPI *PFN_gluQuadricTexture)(GLUquadric *, GLboolean);
    typedef void (WINAPI *PFN_gluSphere)(GLUquadric *, GLdouble, GLint, GLint);
    typedef void (WINAPI *PFN_gluCylinder)(GLUquadric *, GLdouble, GLdouble, GLdouble, GLint, GLint);
    typedef void (WINAPI *PFN_gluDisk)(GLUquadric *, GLdouble, GLdouble, GLint, GLint);
 
    HMODULE hGLU = NULL;
    PFN_gluNewQuadric pgluNewQuadric = NULL;
    PFN_gluDeleteQuadric pgluDeleteQuadric = NULL;
    PFN_gluQuadricDrawStyle pgluQuadricDrawStyle = NULL;
    PFN_gluQuadricNormals pgluQuadricNormals = NULL;
    PFN_gluQuadricTexture pgluQuadricTexture = NULL;
    PFN_gluSphere pgluSphere = NULL;
    PFN_gluCylinder pgluCylinder = NULL;
    PFN_gluDisk pgluDisk = NULL;
 
    hGLU = LoadLibraryA("glu32.dll"); 
    pgluNewQuadric = (PFN_gluNewQuadric)GetProcAddress(hGLU, "gluNewQuadric");
    pgluDeleteQuadric = (PFN_gluDeleteQuadric)GetProcAddress(hGLU, "gluDeleteQuadric");
    pgluQuadricDrawStyle = (PFN_gluQuadricDrawStyle)GetProcAddress(hGLU, "gluQuadricDrawStyle");
    pgluQuadricNormals = (PFN_gluQuadricNormals)GetProcAddress(hGLU, "gluQuadricNormals");
    pgluQuadricTexture = (PFN_gluQuadricTexture)GetProcAddress(hGLU, "gluQuadricTexture");
    pgluSphere = (PFN_gluSphere)GetProcAddress(hGLU, "gluSphere");
    pgluCylinder = (PFN_gluCylinder)GetProcAddress(hGLU, "gluCylinder");
    pgluDisk = (PFN_gluDisk)GetProcAddress(hGLU, "gluDisk");
  
    MSG msg = { 0 };
    while (GetMessage(&msg, NULL, 0, 0)) {
        DispatchMessage(&msg);
  
        GLUquadricObj *q = pgluNewQuadric();
        pgluQuadricNormals(q, GLU_SMOOTH);
        pgluQuadricTexture(q, GL_TRUE);
        glLoadIdentity();
        glTranslatef(0.0f, 0.0f, -5.0f);
        glRotatef(0, 0.0f, 1.0f, 0.0f);
        glBindTexture(GL_TEXTURE_2D, tex);
        pgluDisk(q, 0.5f, 1.5f, 32, 32);
        pgluDeleteQuadric(q);
  
        SwapBuffers(hDC);
    }
    FreeLibrary(hGLU);
    glDeleteTextures(1, &tex);
    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);

    return 0;
}

編譯、執行

$ winegcc main.c -o main.exe -lGL -lGLU -lgdi32 -lopengl32
$ wine main.exe