Win32窗口中的OpenGL基元根据窗口大小而失真

时间:2016-02-13 21:54:04

标签: c++ c winapi opengl

我在C中制作了一个简单的OpenGL程序,绘制一个在框内旋转的三角形。三角形应该是等边的,但即使窗口宽度和高度相同,当三角形顶点沿窗口的宽度和长度移动时,它们会收缩并挤出。我可以说,因为我制作了一个静态等角框,三角形的顶点在它之外。这是它正在做的事情的简短剪辑:https://edwardseverinsen1717-gmail.tinytake.com/sf/NDg5NjQ0XzI2MDQzMDM在某些时刻讨论滞后。

这是我的代码:

#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>

LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);
void EnableOpenGL(HWND hwnd, HDC*, HGLRC*);
void DisableOpenGL(HWND, HDC, HGLRC);
void DrawTriangle(float theta);
void DrawBox(void);

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.0f;
    int i;

    /* 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,
                          500,
                          500,
                          NULL,
                          NULL,
                          hInstance,
                          NULL);

    ShowWindow(hwnd, nCmdShow);

    /* enable OpenGL for the window */
    EnableOpenGL(hwnd, &hDC, &hRC);

    /* 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(0.0f, 0.0f, 0.0f, 0.0f);
            glClear(GL_COLOR_BUFFER_BIT);

            DrawTriangle(theta);

            DrawBox();

            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);
}

void DrawTriangle(float theta)
{
    glPushMatrix();

    if(theta != 0)
    {
        glRotatef(theta, 0.0f, 0.0f, 1.0f);
    }
    if(theta == 1)
    {
        glRotatef(89, 0.0f, 0.0f, 1.0f);
    }

    glBegin(GL_TRIANGLES);

        glColor3f(1.0f, 0.0f, 0.0f);   glVertex2f(0.0f,   0.50f);
        glColor3f(1.0f, 0.0f, 0.0f);   glVertex2f(0.45f,  -0.50f);
        glColor3f(1.0f, 0.0f, 0.0f);   glVertex2f(-0.45f, -0.50f);

    glEnd();

    glPopMatrix();
}

void DrawBox(void)
{
    glPushMatrix();

    glBegin(GL_LINE_LOOP);

        glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.45f, 0.5f);      /*I decremented each x value by 0.05 to compensate for the bullshit with the window throwing off scale*/
        glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(0.45f, -0.5f);
        glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.45f, -0.5f);
        glColor3f(1.0f, 0.0f, 0.0f); glVertex2f(-0.45f, 0.5f);

    glEnd();

    glPopMatrix();
}

1 个答案:

答案 0 :(得分:4)

您使用的坐标不跨越等边三角形。等边三角形总是有三个边长相同的边。在您的示例中,底部的长度为0.9,但另外两个的长度为sqrt(0.45 ^ 2 + 1.0 ^ 2)= 1.097。即使假设您使用0.45而不是0.5,这仍然不是等边的。 (底部= 1.0,其他边= sqrt(0.5 ^ 2 + 1.0 ^ 2)= 1.12)

正如您已经注意到的,您必须补偿可渲染区域的宽高比(在将500x500传递到CreateWindowEx时不是二次方,因为此处包含标题栏和边框)。这通常通过定义具有客户区域的正确宽高比的投影矩阵来完成。请查看GetClientRect查询客户区大小,glOrtho指定投影矩阵。