Opengl the window first experience ------ By YDD tin pot

  Sophomore to start thinking about playing games, because the school's curriculum is really boring to think of something meaningful. After all, you have to learn this line of programming do something, so he found online about graphics programming, incidental learned Opengl (as well as Windows on Direct3D but because of cross-platform Opengl, etc. into the pit). Although it has not anything to write games out, after all, a picture with a bottom of the interface to make it a fun game play is still very large workload. Then because of the recent gettin 'blog think of this learning experience, so these days to look at a bit of code I've written before, to write down my understanding and want to help others also addressed the future of their own.

  I am the first to write OpenGL on Windows, but most call you to go online next glut library to create Windows window, because it is simple and convenient not have to learn the Windows API. These tutorials will not need to give you introduce these things, but I personally think we should use the Windows API to create a window; because it provides many Windows API, API through which we can achieve a lot of features, and create a window of the API is very based; you can understand better learn the API of Windows, and using the API can be more flexible to customize your window and you want to achieve functional Gan glut library does not provide. For Linux due to the limited knowledge I do not know how to use Opengl we can look at Baidu own.

// Win32Project.cpp: Defines the entry point of the application.
//
 
#include " the stdafx.h " 

// the Windows message loop processing various information 
LRESULT CALLBACK the WndProc (the HWND the hWnd, UINT Message, 
    WPARAM the wParam, LPARAM the lParam) 
{ 

    Switch (Message) 
    { 
    Case the WM_CREATE:
         return  0 ;
     Case the WM_CLOSE: 
        the PostQuitMessage ( 0 );
         return  0 ; 

    Case the WM_DESTROY:
         return  0 ; 

    Case the WM_KEYDOWN: // keyboard press message 
        return  0;

    default:
        return DefWindowProc(hWnd, message, wParam, lParam);
    }
}


//Windows程序入口函数
int WINAPI WinMain(HINSTANCE hInstance,
    HINSTANCE hPrevInstance,
    LPSTR lpCmdLine,
    int iCmdShow)
{

    HDC hDC;
    HGLRC hRC;
    MSG msg;
    BOOL bQuit = FALSE;

    WNDCLASS wc;

    wc.style = CS_OWNDC;
    wc.lpfnWndProc = WndProc;
    wc.cbClsExtra = 0;
    wc.cbWndExtra = 0; 
    Wc.hInstance = hInstance; 
    wc.hIcon = the LoadIcon (NULL, IDI_APPLICATION); 
    wc.hCursor = the LoadCursor (NULL, IDC_ARROW); 
    wc.hbrBackground = (HBRUSH) the GetStockObject (BLACK_BRUSH); 
    wc.lpszMenuName = NULL; 
    wc.lpszClassName the TEXT = ( " GLSample " ); 
    the RegisterClass ( & WC); 

    // before the middle of the two following four digits are a function of the screen x, y followed aspect 
    the HWND CreateWindowA the hWnd = ( " GLSample " , " the OpenGL the Sample ", WS_CAPTION | WS_POPUPWINDOW | WS_VISIBLE, 0, 0, 400, 600, NULL, NULL, hInstance, NULL);
    if (hWnd == NULL) return -1;

    hDC = GetDC(hWnd);

    PIXELFORMATDESCRIPTOR pfd;

    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;

    int iFormat = ChoosePixelFormat(hDC, &pfd);
    SetPixelFormat(hDC, iFormat, &pfd);

    hRC = wglCreateContext(hDC);
    wglMakeCurrent(hDC, hRC);

    while (!bQuit)
    {

        if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
        {

            if (msg.message == WM_QUIT)
            {
                bQuit = TRUE;
            }
            else
            {
                TranslateMessage(&msg);
                DispatchMessage(&msg);
            }
        }
        else
        {

            glClearColor(1.0f, 1.0f, 0.0f, 1.0f);
            glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
/*            
               在这里添加Opengl代码
*/               
            SwapBuffers(hDC);

            Sleep(10);
        }
    }

    wglMakeCurrent(NULL, NULL);
    wglDeleteContext(hRC);
    ReleaseDC(hWnd, hDC);
    DestroyWindow(hWnd);


    return msg.wParam;
}

 

  This code above you use Visual Studio or Visual C ++ Win32 project to create a use, because bloggers I use Visual Studio 2015 so the following pictures are truncated in VS2015, VS and VC wizard interface is different, but about the meaning of the options can still be found, because bloggers are lazy so I just made a key step in a plan.

  Open VS or VC → New → Project

  Pop creation wizard, found under Win32 Visual C ++, there are a Win32 and Win32 console project, select Win32 project like writing a project name click OK.

  After a wizard will pop up again, not directly point to determine the next step, over-attention to the following options.

   If you are a beginner, you should create an empty project wizard interface do not check this option, he generated code can help you better analyze and entry

Next VS will help you create a lot of good files that you may not know what effect.

As shown below:

But the key documents on four stdafx.h, stdafx.cpp, you take the project name and project name plus .h .cpp (PS: Here I have a name for the Win32Project3).

And then briefly explain the role of four files

stdafx.h: You put VS comes with header files such as <iostream>, <Windows.h> are included in this document. After generating a .pch file the first time you compiled when compiling the compiler will use this file to speed up your compilation speed

stdafx.cpp: stdafx.h header file that contains the specific file Why should I not quite clear Gangster know can message to me

Win32Project.h: headers can write your own definition of the structure, classes and variables

Win32Project.cpp:这次我们的代码就写在这里WinMain主要的文件

现在创建好了一个工程你点调试→开始调试运行就能弹出一个Windows窗口

接下来双击打开Win32Project.cpp删除原来的代码把文章开头的代码粘贴过来

开始调试发现报错因为没有引入头文件

在stdafx.h文件中加入 #include <gl/GL.h>当然你也可以直接加在Win32Project.cpp文件里

再次调试运行 一般情况下都会出现如下结果

这是因为没有引入OpenGL的Lib库文件可以在stdafx.h 或 Win32Project.cpp加入#pragma comment(lib, "opengl32.lib")来引用OpenGL库文件

最后调试运行

如果你的窗口变黄了就代表你成功了

下篇Blog我打算写OpenGL的glew库的使用,个人觉得glew对OpenGL来说是十分重要的

最后就是博主是第一次写博文,写的不好多多包涵,有任何问题可以给我留言。

Guess you like

Origin www.cnblogs.com/yddsblog/p/11084835.html