• Lesson 4 Nehe


    #include <gl/opengl.h>
    
    HGLRC hRC = NULL;
    HDC   hDC = NULL;
    HWND  hWnd = NULL;
    HINSTANCE hInstance = NULL;
    
    BOOL keys[256];
    BOOL active = TRUE;
    BOOL fullscreen = FALSE;
    GLfloat rtri = 0.0f;
    GLfloat rquad = 360.0f;
    
    LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
    
    GLvoid ReSizeGLScene(GLsizei width, GLsizei height){
        // 如果高为0 , 则设置其为1
        if (height == 0) height = 1;
        // 设置可以看到的视角
        glViewport(0,0,width, height);
    
        // 设置投影矩阵
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        // 设置平截头体
        gluPerspective(45.0f, (GLdouble)width/(GLdouble)height, 0.1f, 100.0f);
    
        // 设置模型矩阵
        glMatrixMode(GL_MODELVIEW);
        glLoadIdentity();
    }
    
    GLvoid InitGL(){
        glShadeModel(GL_SMOOTH);
        glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
        // 1.0f 为最大深度
        glClearDepth(1.0f);
        glEnable(GL_DEPTH_TEST);
        glDepthFunc(GL_LEQUAL);
        glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
    }
    
    BOOL DrawGLScene(){
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        glLoadIdentity();
        glTranslatef(-1.5f, 0.0f, -6.0f);
        glRotatef(rtri, 0.0f, 1.0f, 0.0f);
        glBegin(GL_TRIANGLES);
        glColor3f(1.0f, 0.0f, 0.0f);
        glVertex3f(0.0f, 1.0f, 0.0f);
    
        glColor3f(0.0f, 1.0f, 0.0f);
        glVertex3f(-1.0f, -1.0f, 0.0f);
    
        glColor3f(0.0f, 0.0f, 1.0f);
        glVertex3f(1.0f, -1.0f, 0.0f);
        glEnd();
    
    
        glLoadIdentity();                   // 重置坐标系
        glTranslatef(1.5f, 0.0f, -6.0f);
        glRotatef(rquad, 1.0f, 0.0f, 0.0f);
        glColor3f(0.5f, 0.5f, 1.0f);
        glBegin(GL_QUADS);
        glVertex3f(-1.0f, 1.0f, 0.0f);
        glVertex3f( 1.0f, 1.0f, 0.0f);
        glVertex3f( 1.0f,-1.0f, 0.0f);
        glVertex3f(-1.0f,-1.0f, 0.0f);
        glEnd();
    
        if (rtri>= 360.0f) rtri = 0.0f;
        rtri += 0.2f;
        if (rquad <= 0.0f) rquad = 360.0f;
        rquad -= 0.15f;
        return TRUE;
    }
    
    GLvoid KillGLWindow(){
        if (fullscreen){
            ChangeDisplaySettings(NULL, 0);
            ShowCursor(FALSE);
        }
    
        if (hRC){
            if (!wglMakeCurrent(NULL, NULL))
                MessageBox(NULL, "释放DC 或 RC 失败", "错误", MB_OK);
            if (!wglDeleteContext(hRC))
                MessageBox(NULL, "释放RC失败", "错误", MB_OK);
            hRC = NULL;
        }
    
        // ReleaseDC 失败返回0, 成功返回非0
        if (hDC && !ReleaseDC(hWnd, hDC)){
            MessageBox(NULL, "释放DC失败", "错误", MB_OK);
            hDC = NULL;
        }
    
        if (hWnd && !DestroyWindow(hWnd)){
            MessageBox(NULL, "销毁窗口失败", "错误", MB_OK);
            hWnd = NULL;
        }
    
        if (!UnregisterClass("opengl", hInstance)){
            MessageBox(NULL, "反注册失败", "错误", MB_OK);
            hInstance = NULL;
        }
    }
    
    BOOL CreateGLWindow(char* title, int width, int height, int bits, HINSTANCE hInstance, bool fullscreenflag){
        GLuint PixelFormat;
        WNDCLASS wc;
        DWORD dwStyle;
        DWORD dwExStyle;
    
    
        RECT WindowRect;
        WindowRect.left = (long)0;
        WindowRect.right= (long)width;
        WindowRect.top  =  (long)0;
        WindowRect.bottom = (long)height;
    
    
        fullscreen = fullscreenflag;
    
        wc.hInstance = hInstance;
        wc.cbClsExtra  = 0;
        wc.cbWndExtra  = 0;
        wc.style       = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
        wc.lpfnWndProc = WindowProc;
        wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
        wc.hCursor = LoadCursor(NULL, IDC_ARROW);
        wc.hbrBackground = NULL;
        wc.lpszClassName = "opengl";
        wc.lpszMenuName = NULL;
    
    
        // 注册窗口类
    
        if (!RegisterClass(&wc)){
            MessageBox(NULL, "窗口注册失败", "错误", MB_OK);
            return FALSE;
        }
    
        if (fullscreen){
            DEVMODE dmScreenSetting;
            memset(&dmScreenSetting, 0, sizeof(dmScreenSetting));
            dmScreenSetting.dmSize = sizeof(dmScreenSetting);
            dmScreenSetting.dmBitsPerPel = bits;
            dmScreenSetting.dmPelsHeight = height;
            dmScreenSetting.dmPelsWidth = width;
            dmScreenSetting.dmFields = DM_BITSPERPEL | DM_PELSHEIGHT | DM_PELSWIDTH;
    
            // 设置显示模式
            if  (ChangeDisplaySettings(&dmScreenSetting, CDS_FULLSCREEN)!= DISP_CHANGE_SUCCESSFUL){
                if (MessageBox(NULL, "当前显卡不支持全屏操做
    使用窗口模式?", "错误", MB_YESNO|MB_ICONEXCLAMATION) == IDYES){
                    fullscreen = FALSE;
                }
                else{
                    MessageBox(NULL, "程序将会被关闭", "错误", MB_OK|MB_ICONEXCLAMATION);
                    return FALSE;
                }
            }
        }
    
        if (fullscreen){
            dwExStyle = WS_EX_APPWINDOW;
            dwStyle = WS_POPUP;
            ShowCursor(FALSE);
        }
        else{
            dwExStyle = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
            dwStyle = WS_OVERLAPPEDWINDOW;
            //ShowCursor(TRUE);
        }
    
        dwStyle = dwStyle | WS_CLIPCHILDREN|WS_CLIPSIBLINGS;
        AdjustWindowRectEx(&WindowRect, dwStyle, FALSE, dwExStyle);
    
        if (!(hWnd = CreateWindowEx(dwExStyle, "opengl", title, dwStyle, 0,0, WindowRect.right-WindowRect.left, WindowRect.bottom-WindowRect.top, NULL, NULL, hInstance, NULL))){
            KillGLWindow();
            MessageBox(NULL, "不能创建一个窗口设备描述表", "错误", MB_OK);
            return FALSE;
        }
    
        static PIXELFORMATDESCRIPTOR pfd = {
            sizeof(PIXELFORMATDESCRIPTOR),
            1,
            PFD_DRAW_TO_WINDOW|
            PFD_SUPPORT_OPENGL|
            PFD_DOUBLEBUFFER,
            PFD_TYPE_RGBA,
            bits,
            0, 0, 0, 0, 0, 0,               // 忽略的色彩位
            0,                              // 无Alpha缓存
            0,                              // 忽略Shift Bit
            0,                              // 无累加缓存
            0, 0, 0, 0,                         // 忽略聚集位
            16,                             // 16位 Z-缓存 (深度缓存)
            0,                              // 无蒙板缓存
            0,                              // 无辅助缓存
            PFD_MAIN_PLANE,                         // 主绘图层
            0,                              // Reserved
            0, 0, 0                             // 忽略层遮罩
        };
        if (!(hDC = GetDC(hWnd))){
            KillGLWindow();
            MessageBox(NULL, "不能创建一个相匹配的像素模式", "错误", MB_OK|MB_ICONEXCLAMATION);
            return FALSE;
        }
    
        if (!(PixelFormat = ChoosePixelFormat(hDC, &pfd))){
            KillGLWindow();
            MessageBox(NULL, "不能设置像素格式", "错误", MB_OK|MB_ICONEXCLAMATION);
            return FALSE;
        }
    
        if (!SetPixelFormat(hDC, PixelFormat, &pfd)){
            KillGLWindow();
            MessageBox(NULL, "不能设置像素格式", "错误",MB_OK|MB_ICONEXCLAMATION);
            return FALSE;
        }
    
        if (!(hRC = wglCreateContext(hDC))){
            KillGLWindow();
            MessageBox(NULL, "不能创建当前的opengl渲染描述表", "错误", MB_OK|MB_ICONEXCLAMATION);
            return FALSE;
        }
    
        if (!wglMakeCurrent(hDC, hRC)){
            KillGLWindow();
            MessageBox(NULL, "不能激活当前的opengl渲染描述表", "错误", MB_OK);
            return FALSE;
        }
    
        ShowWindow(hWnd, SW_SHOW);
        // 激活窗口
        SetForegroundWindow(hWnd);
        // 接收键盘信息
        SetFocus(hWnd);
        ReSizeGLScene(width, height);
    
        InitGL();
        return TRUE;
    }
    
    LRESULT CALLBACK WindowProc( HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ){
        switch(uMsg){
        case WM_ACTIVATE:
            {
                if (!HIWORD(wParam))
                    active = TRUE;
                else
                    active = FALSE;
                return 0;
            }
        case WM_CLOSE:
            {
                PostQuitMessage(0);
                return 0;
            }
        case WM_KEYUP:
            {
                keys[wParam] = FALSE;
                return 0;
            }
        case WM_KEYDOWN:
            {
                keys[wParam] = TRUE;
                return 0;
            }
        case WM_SIZE:
            {
                ReSizeGLScene(LOWORD(lParam), HIWORD(lParam));
                return 0;
            }
        }
        return DefWindowProc(hwnd, uMsg, wParam, lParam);
    }
    
    int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow ){
        MSG msg;
        BOOL done = FALSE;
    
        if (MessageBox(NULL, "是否在全屏模式下运行?", "提示", MB_YESNO|MB_ICONEXCLAMATION) == IDYES)
            fullscreen = TRUE;
    
        if (!CreateGLWindow("Lesson 3", 640, 480, 16, hInstance, fullscreen))
            return 0;
    
        while (!done){
            if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)){
                if (msg.message == WM_QUIT)
                    done = TRUE;
                else{
                    TranslateMessage(&msg);
                    DispatchMessage(&msg);
                }
            }
            else
            {
                if (active)
                {
                    if (keys[VK_ESCAPE])
                        done = TRUE;
                    else{
                        DrawGLScene();
                        SwapBuffers(hDC);
                    }
                    if (keys[VK_F1]){
                        keys[VK_F1] = FALSE;
                        KillGLWindow();
                        fullscreen = !fullscreen;
                        if (!CreateGLWindow("Lesson 3", 640, 480, 16, hInstance, fullscreen))
                            return 0;
                    }
                }
    
            }
        }
        KillGLWindow();
        return (msg.wParam);
    }

    这里写图片描述

  • 相关阅读:
    【洛谷6620】[省选联考 2020 A 卷] 组合数问题(下降幂)
    【AtCoder】AtCoder Grand Contest 033 解题报告
    【AtCoder】AtCoder Grand Contest 034 解题报告
    【洛谷5445】[APIO2019] 路灯(树套树)
    【LOJ6059】「2017 山东一轮集训 Day1」Sum(倍增优化数位DP+NTT)
    【LOJ6159】「美团 CodeM 初赛 Round A」最长树链(树的直径)
    重新入门的Polya定理
    【洛谷6105】[Ynoi2010] y-fast trie(set)
    【BZOJ4480】 [JSOI2013] 快乐的jyy(回文自动机裸题)
    【LOJ6172】Samjia 和大树(树形DP+猜结论)
  • 原文地址:https://www.cnblogs.com/laohaozi/p/12538349.html
Copyright © 2020-2023  润新知