今天準備回到windows上繼續學習OpenGL, 結果不知道是因爲在虛擬機的原因還是因爲其他的,一個基本的OpenGL框架竟然卡,而且關閉按鈕不接收消息,先把代碼貼上來,回去在windows上實驗一下:
#include <windows.h>
#include <gl/gl.h>
#include <gl/glu.h>
#pragma comment(lib, "opengl32.lib")
#pragma comment(lib, "glu32.lib")
#define GLTEST_CLASS "GLTest.Window"
#define WND_W 800
#define WND_H 600
#define FPS 30
static HINSTANCE G_hinst = NULL;
static HWND G_hwnd = NULL;
static HDC G_hdc = NULL;
static HGLRC G_hrc = NULL;
static PIXELFORMATDESCRIPTOR S_pfd = {
sizeof(PIXELFORMATDESCRIPTOR),
1,
PFD_DRAW_TO_WINDOW |
PFD_SUPPORT_OPENGL |
PFD_DOUBLEBUFFER,
PFD_TYPE_RGBA,
0,
0, 0, 0, 0, 0, 0,
0,
0,
0,
0, 0, 0, 0,
16,
0,
0,
PFD_MAIN_PLANE,
0,
0, 0, 0
};
void InitGL();
void ReshapeGL(int w, int h);
void DisplayGL();
static LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
switch ( msg ) {
case WM_PAINT:
DisplayGL();
break;
case WM_SIZE:
ReshapeGL(LOWORD(lparam), HIWORD(lparam));
break;
case WM_CLOSE:
PostQuitMessage(0);
break;
default:
return DefWindowProc(hwnd, msg, wparam, lparam);
}
return 0;
}
int CreateGLWindow(const char *title, int w, int h)
{
WNDCLASS wc;
DWORD wnd_style_ex, wnd_style;
RECT wnd_rect;
int pf;
G_hinst = GetModuleHandle(NULL);
wnd_rect.left = 0;
wnd_rect.top = 0;
wnd_rect.right = w;
wnd_rect.bottom = h;
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = (WNDPROC)WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = G_hinst;
wc.hIcon = LoadIcon(NULL, IDI_WINLOGO);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = NULL;
wc.lpszMenuName = NULL;
wc.lpszClassName = GLTEST_CLASS;
if ( !RegisterClass(&wc) )
return 0;
wnd_style_ex = WS_EX_APPWINDOW | WS_EX_WINDOWEDGE;
wnd_style = WS_OVERLAPPEDWINDOW;
AdjustWindowRectEx(&wnd_rect, wnd_style, 0, wnd_style_ex);
G_hwnd = CreateWindowEx(wnd_style_ex,
GLTEST_CLASS,
title,
wnd_style,
0, 0,
wnd_rect.right-wnd_rect.left, wnd_rect.bottom-wnd_rect.top,
NULL, NULL, G_hinst, NULL);
G_hdc = GetDC(G_hwnd);
S_pfd.cColorBits = GetDeviceCaps(NULL, BITSPIXEL);
if ( !(pf = ChoosePixelFormat(G_hdc, &S_pfd)) )
return 0;
if ( !SetPixelFormat(G_hdc, pf, &S_pfd) )
return 0;
G_hrc = wglCreateContext(G_hdc);
if ( !G_hrc )
return 0;
if ( !wglMakeCurrent(G_hdc, G_hrc) )
return 0;
ShowWindow(G_hwnd,SW_SHOW);
ReshapeGL(w, h);
InitGL();
return 1;
}
void ReleaseGLWindow()
{
wglMakeCurrent(NULL, NULL);
wglDeleteContext(G_hrc);
ReleaseDC(G_hwnd, G_hdc);
DestroyWindow(G_hwnd);
UnregisterClass(GLTEST_CLASS, G_hinst);
}
void InitGL()
{
glEnable(GL_TEXTURE_2D);
glShadeModel(GL_SMOOTH);
glClearColor(0.0f, 0.0f, 0.0f, 0.0f);
glClearDepth(1.0f);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
}
void ReshapeGL(int w, int h)
{
glViewport(0, 0, w, h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
gluOrtho2D(0.0f, (GLfloat)w, 0.0f, (GLfloat)h);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void DisplayGL()
{
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SwapBuffers(G_hdc);
}
int WINAPI
WinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPSTR lpCmdLine,
int nCmdShow)
{
MSG msg;
DWORD intval = 1000 / FPS;
CreateGLWindow("GLTest", WND_W, WND_H);
for ( ;; ) {
if ( PeekMessage(&msg, G_hwnd, 0, 0, PM_REMOVE) ) {
if ( WM_QUIT == msg.message )
break;
else {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
DisplayGL();
Sleep(intval);
}
ReleaseGLWindow();
return 0;
}