#pragma once#include#include#include
classGLContext
{public:
GLContext();~GLContext();voidSetup(HWND,HDC);voidSetupPixelFormat(HDC);private:
HWND hWnd;
HDC hDC;
HGLRC hRC;intformat;
};
GLContext::GLContext()
{this->hWnd = 0;this->hDC = 0;this->hRC = 0;this->format = 0;
}
GLContext::~GLContext()
{
}voidGLContext::SetupPixelFormat(HDC hDC) {intpixelFormat;
PIXELFORMATDESCRIPTOR pfd={sizeof(PIXELFORMATDESCRIPTOR), //size
1, //version
PFD_SUPPORT_OPENGL | //OpenGL window
PFD_DRAW_TO_WINDOW | //render to window
PFD_DOUBLEBUFFER, //support double-buffering
PFD_TYPE_RGBA, //color type
32, //prefered color depth
0, 0, 0, 0, 0, 0, //color bits (ignored)
0, //no alpha buffer
0, //alpha bits (ignored)
0, //no accumulation buffer
0, 0, 0, 0, //accum bits (ignored)
16, //depth buffer
0, //no stencil buffer
0, //no auxiliary buffers
PFD_MAIN_PLANE, //main layer
0, //reserved
0, 0, 0, //no layer, visible, damage masks
};
pixelFormat= ChoosePixelFormat(hDC, &pfd);
SetPixelFormat(hDC, pixelFormat,&pfd);
}voidGLContext::Setup(HWND hwnd, HDC hdc) {this->hWnd =hwnd;this->hDC =hdc;
SetupPixelFormat(hDC);
hRC=wglCreateContext(hDC);
wglMakeCurrent(hDC, hRC);//initialize glew
glewExperimental =GL_TRUE;
glewInit();if(AllocConsole())
{
freopen("CONOUT$", "w+t", stdout);
freopen("CONOUT$", "w+t", stderr);const GLubyte* Devise = glGetString(GL_RENDERER); //返回一个渲染器标识符,通常是个硬件平台
const GLubyte* str =glGetString(GL_VERSION);
printf("OpenGL实现的版本号:%s\n", str);
printf("硬件平台:%s\n", Devise);
}
}
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);classGLWindow
{public:
GLWindow();~GLWindow();void Setup(HINSTANCE, HINSTANCE, LPSTR, int);//LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam);
void RegisterDisplayFunc(void (*display)()) {this->Display =display;
}voidRun();private:void(*Display)();private:bool exiting = false;long windowWidth = 800;long windowHeight = 600;long windowBits = 64;bool fullscreen = false;
WNDCLASSEX windowClass;//window class
HWND hwnd; //window handle
HDC hDC;
MSG msg;//message
DWORD dwExStyle; //Window Extended Style
DWORD dwStyle; //Window Style
RECT windowRect;
GLContext glContext;
};
GLWindow::GLWindow() {}
GLWindow::~GLWindow() {}void GLWindow::Setup(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, intnShowCmd)
{
windowRect.left= (long)0; //Set Left Value To 0
windowRect.right = (long)windowWidth; //Set Right Value To Requested Width
windowRect.top = (long)0; //Set Top Value To 0
windowRect.bottom = (long)windowHeight; //Set Bottom Value To Requested Height//fill out the window class structure
windowClass.cbSize = sizeof(WNDCLASSEX);
windowClass.style= CS_HREDRAW |CS_VREDRAW;
windowClass.lpfnWndProc= MainWindowProc; //当窗体触发任何一个事件时,便会调用该函数
windowClass.cbClsExtra = 0;
windowClass.cbWndExtra= 0;
windowClass.hInstance=hInstance;
windowClass.hIcon= LoadIcon(NULL, IDI_APPLICATION); //default icon
windowClass.hCursor = LoadCursor(NULL, IDC_ARROW); //default arrow
windowClass.hbrBackground = NULL; //don't need background
windowClass.lpszMenuName = NULL; //no menu
windowClass.lpszClassName = L"Windows API";
windowClass.hIconSm= LoadIcon(NULL, IDI_WINLOGO); //windows logo small icon//register the windows class
if (!RegisterClassEx(&windowClass)) {
puts("Register Class Failed");
}
dwExStyle= WS_EX_APPWINDOW | WS_EX_WINDOWEDGE; //Window Extended Style
dwStyle = WS_OVERLAPPEDWINDOW; //Windows Style
AdjustWindowRectEx(&windowRect, dwStyle, FALSE, dwExStyle); //Adjust Window To True Requested Size//class registered, so now create our window
hwnd = CreateWindowEx(NULL, //extended style
L"Windows API", //class name
L"OpenGL", //app name
dwStyle | WS_CLIPCHILDREN |WS_CLIPSIBLINGS,0, 0, //x,y coordinate
windowRect.right -windowRect.left,
windowRect.bottom- windowRect.top, //width, height
NULL, //handle to parent
NULL, //handle to menu
hInstance, //application instance
NULL); //no extra params
ShowWindow(hwnd, SW_SHOW);//display the window
UpdateWindow(hwnd); //update the window
hDC =GetDC(hwnd);
glContext.Setup(hwnd,hDC);
}
LRESULT CALLBACK MainWindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{intheight, width;//dispatch messages
switch(uMsg)
{case WM_CREATE: //window creation
break;case WM_DESTROY: //window destroy
caseWM_QUIT:
CloseWindow(hWnd);break;case WM_CLOSE: //windows is closing//deselect rendering context and delete it//wglMakeCurrent(hDC, NULL);//wglDeleteContext(hRC);//send WM_QUIT to message queue
PostQuitMessage(0);break;caseWM_SIZE:
height= HIWORD(lParam); //retrieve width and height
width =LOWORD(lParam);break;case WM_ACTIVATEAPP: //activate app
break;case WM_PAINT: //paint
PAINTSTRUCT ps;
BeginPaint(hWnd,&ps);
EndPaint(hWnd,&ps);break;case WM_LBUTTONDOWN: //left mouse button
break;case WM_RBUTTONDOWN: //right mouse button
break;case WM_MOUSEMOVE: //mouse movement
break;case WM_LBUTTONUP: //left button release
break;case WM_RBUTTONUP: //right button release
break;caseWM_KEYUP:break;caseWM_KEYDOWN:intfwKeys;
LPARAM keyData;
fwKeys= (int)wParam; //virtual-key code
keyData = lParam; //key data
switch(fwKeys)
{caseVK_ESCAPE:
PostQuitMessage(0);break;default:break;
}break;default:break;
}returnDefWindowProc(hWnd, uMsg, wParam, lParam);
}voidGLWindow::Run() {while (true)
{
(*Display)();
SwapBuffers(hDC);while (PeekMessage(&msg, NULL, 0, 0, PM_NOREMOVE))
{if (!GetMessage(&msg, NULL, 0, 0))
{
exiting= true;break;
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
}
本文介绍了一个OpenGL上下文的设置过程,包括像素格式的选择、渲染上下文的创建及初始化等关键步骤。通过具体的C++代码示例,展示了如何为OpenGL应用程序准备必要的渲染环境。

917

被折叠的 条评论
为什么被折叠?



