zoukankan      html  css  js  c++  java
  • vs2010 OpenGL 初体验

    使用OpenGL,没有加载一些glut等包,居然真的画出来了。

    首先创建工程,其步骤如下:

      1)创建一个Win32 Console Application。

      2)创建一个空的。转载了http://blog.sina.com.cn/s/blog_9579544301016wvp.html

    复制代码如下:

    #pragma comment(lib, "opengl32.lib")
    #pragma comment(lib, "glu32.lib")
    
    #include <windows.h>
    
    // C RunTime Header Files
    #include <stdlib.h>
    #include <malloc.h>
    #include <memory.h>
    #include <tchar.h>
    #include <gl/GL.h>
    #include <gl/GLU.h>
    
    HWND    hWnd;
    HDC     hDC;
    HGLRC   hRC;
    
    
    void Game_Init();
    void Game_Main();
    void Game_ShutDown(); 
    
    
     
    
    
    
    // Set up pixel format for graphics initialization
    void SetupPixelFormat()
    {
        PIXELFORMATDESCRIPTOR pfd, *ppfd;
        int pixelformat;
    
        ppfd = &pfd;
    
        ppfd->nSize = sizeof(PIXELFORMATDESCRIPTOR);
        ppfd->nVersion = 1;
        ppfd->dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
        ppfd->dwLayerMask = PFD_MAIN_PLANE;
        ppfd->iPixelType = PFD_TYPE_COLORINDEX;
        ppfd->cColorBits = 16;
        ppfd->cDepthBits = 16;
        ppfd->cAccumBits = 0;
        ppfd->cStencilBits = 0;
    
        pixelformat = ChoosePixelFormat(hDC, ppfd);
        SetPixelFormat(hDC, pixelformat, ppfd);
    }
    
    // Initialize OpenGL graphics
    void InitGraphics()
    {
        hDC = GetDC(hWnd);
    
        SetupPixelFormat();
    
        hRC = wglCreateContext(hDC);
        wglMakeCurrent(hDC, hRC);
    
        glClearColor(0, 0, 0, 0.5);
        glClearDepth(1.0);
        glEnable(GL_DEPTH_TEST);
    }
    
    // Resize graphics to fit window
    void ResizeGraphics()
    {
        // Get new window size
        RECT rect;
        int width, height;
        GLfloat aspect;
    
        GetClientRect(hWnd, &rect);
        width = rect.right;
        height = rect.bottom;
        aspect = (GLfloat)width / height;
    
        // Adjust graphics to window size
        glViewport(0, 0, width, height);
        glMatrixMode(GL_PROJECTION);
        glLoadIdentity();
        gluPerspective(45.0, aspect, 1.0, 100.0);
        glMatrixMode(GL_MODELVIEW);
    }
    
    // Draw frame
    void DrawGraphics()
    {
        glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
    
        // Set location in front of camera
        glLoadIdentity();
        glTranslated(0, 0, -10);
    
        // Draw a square
        glBegin(GL_QUADS);
        glColor3d(1, 0, 0);
        glVertex3d(-2, 2, 0);
        glVertex3d(2, 2, 0);
        glVertex3d(2, -2, 0);
        glVertex3d(-2, -2, 0);
        glEnd();
    
        // Show the new scene
        SwapBuffers(hDC);
    }
    
    // Handle window events and messages
    LONG WINAPI MainWndProc (HWND hWnd, UINT uMsg, WPARAM  wParam, LPARAM  lParam)
    {
        switch (uMsg)
        {
        case WM_SIZE:
            ResizeGraphics();
            break;
    
        case WM_CLOSE:
            DestroyWindow(hWnd);
            break;
     
        case WM_DESTROY:
            PostQuitMessage(0);
            break;
     
        // Default event handler
        default:
            return DefWindowProc (hWnd, uMsg, wParam, lParam);
            break;
        }
     
        return 1;
    }
    
    int WINAPI WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
        const LPCWSTR appname = TEXT("OpenGL Sample");
    
        WNDCLASS wndclass;
        MSG      msg;
     
        // Define the window class
        wndclass.style         = 0;
        wndclass.lpfnWndProc   = (WNDPROC)MainWndProc;
        wndclass.cbClsExtra    = 0;
        wndclass.cbWndExtra    = 0;
        wndclass.hInstance     = hInstance;
        wndclass.hIcon         = LoadIcon(hInstance, appname);
        wndclass.hCursor       = LoadCursor(NULL,IDC_ARROW);
        wndclass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
        wndclass.lpszMenuName  = appname;
        wndclass.lpszClassName = appname;
     
        // Register the window class
        if (!RegisterClass(&wndclass)) return FALSE;
     
        // Create the window
        hWnd = CreateWindow(
                appname,
                appname,
                WS_OVERLAPPEDWINDOW | WS_CLIPSIBLINGS | WS_CLIPCHILDREN,
                CW_USEDEFAULT,
                CW_USEDEFAULT,CW_USEDEFAULT
    ,CW_USEDEFAULT
    ,
                NULL,
                NULL,
                hInstance,
                NULL);
     
        if (!hWnd) return FALSE;
    
            // Initialize OpenGL
        InitGraphics();
    
     
    
        // Display the window
        ShowWindow(hWnd, nCmdShow);
        UpdateWindow(hWnd);
    
        // Event loop
        while (1)
        {
            if (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE) == TRUE)
            {
                if (!GetMessage(&msg, NULL, 0, 0)) return TRUE;
    
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
            DrawGraphics();
        }
    
        wglDeleteContext(hRC);
        ReleaseDC(hWnd, hDC);
    }  

    直接运行会报错error LNK2019: unresolved external symbol _main referenced in function ___tmainCRTStartup
    纠错方法:

      找到Project -> properties -> Linker -> System ->subSystem 选择Windows (/SUBSYSTEM:WINDOWS)。

      3)编译  -> 运行

      4)ok

     
  • 相关阅读:
    关于下拉框在页面加载时候选中值得问题
    《锋利的jQuery》要点归纳(二) jQuery中的DOM操作(上)
    《锋利的jQuery》要点归纳(三) jQuery中的事件和动画(下:动画篇)
    《锋利的jQuery》要点归纳(四)jQuery对表单、表格的操作及更多应用(上:表单应用)
    getElementById函数的一些发现
    《锋利的jQuery》要点归纳(三) jQuery中的事件和动画(上:事件篇)
    《锋利的jQuery》要点归纳(四)jQuery对表单、表格的操作及更多应用(中:表格应用)
    BENNY妙笔生花博皮博客园即将发布
    《锋利的jQuery》要点归纳(二) jQuery中的DOM操作(下)
    《锋利的jQuery》要点归纳(一) jQuery选择器
  • 原文地址:https://www.cnblogs.com/konkon/p/3830458.html
Copyright © 2011-2022 走看看