【OpenGL】不能再简单的OpenGL Demo
可以说是最简单的OpenGL的Demo了。并没有使用OpenGL EW。只是完全用OpenGL替代了WM_PAINT用GDI显示Hello World而已。
从这个方面来说,D3D的初始化太麻烦了。
//=============================================================================
//作者:0xAA55
//网址:http://www.0xaa55.com/
//请保留原作者信息,否则视为侵权。
//-----------------------------------------------------------------------------
#include<tchar.h>
#include<Windows.h>
#include<gl/GL.h>
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp);//消息处理函数
HDC hDC;//HDC上下文
HGLRC hRC;//OpenGL上下文
//=============================================================================
//函数:InitOpenGL
//描述:初始化OpenGL
//-----------------------------------------------------------------------------
void InitOpenGL(HDC hDC)
{
PIXELFORMATDESCRIPTOR PFD={0};
int nPixelFormat;
PFD.nSize =sizeof(PFD);
PFD.nVersion =1;
PFD.dwFlags =PFD_DRAW_TO_WINDOW|PFD_SUPPORT_OPENGL|PFD_DOUBLEBUFFER;
PFD.iPixelType =PFD_TYPE_RGBA;
//后台缓冲区格式
PFD.cDepthBits =32;
PFD.cAlphaBits =8; PFD.cAlphaShift =24;
PFD.cBlueBits =8; PFD.cBlueShift =16;
PFD.cGreenBits =8; PFD.cGreenShift =8;
PFD.cRedBits =8; PFD.cRedShift =0;
PFD.iLayerType =PFD_MAIN_PLANE;
nPixelFormat=ChoosePixelFormat(hDC,&PFD);
SetPixelFormat(hDC,nPixelFormat,&PFD);//设置格式使其兼容OpenGL
hRC=wglCreateContext(hDC);//得到OpenGL上下文
if(!hRC)
goto Cleanup;
Cleanup:
//MessageBox(NULL,TEXT("初始化OpenGL没成功。"),TEXT("Fuck!"),MB_ICONERROR);
return;
}
//=============================================================================
//函数:_tWinMain
//描述:整个程序的入口点
//-----------------------------------------------------------------------------
int APIENTRY _tWinMain
(
HINSTANCE hInst,
HINSTANCE hPrevInst,
LPTSTR szCmdLine,
int nShowCmd
)
{
WNDCLASSEX WndClass;
ATOM ClassAtom=0;
HWND hWnd;
MSG msg;
//注册窗口类
memset(&WndClass,0,sizeof(WndClass));
WndClass.cbSize=sizeof(WndClass);
WndClass.lpfnWndProc=WndProc;
WndClass.hInstance=hInst;
WndClass.hIcon=LoadIcon(hInst,MAKEINTRESOURCE(IDI_APPLICATION));
WndClass.hCursor=LoadCursor(hInst,MAKEINTRESOURCE(IDC_ARROW));
WndClass.hbrBackground=(HBRUSH)(COLOR_BTNFACE+1);
WndClass.lpszClassName=TEXT("NormalWindow");
WndClass.hIconSm=LoadIcon(hInst,MAKEINTRESOURCE(IDI_APPLICATION));
ClassAtom=RegisterClassEx(&WndClass);
if(!ClassAtom)
goto Cleanup;
//创建窗口
hWnd=CreateWindowEx(0,(LPCTSTR)ClassAtom,TEXT("主窗口"),
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,CW_USEDEFAULT,
NULL,NULL,hInst,NULL);
if(!hWnd)
goto Cleanup;
//显示窗口
ShowWindow(hWnd,nShowCmd);
UpdateWindow(hWnd);
hDC=GetDC(hWnd);//取得窗口的GDI绘图句柄
InitOpenGL(hDC);//初始化OpenGL
//消息循环
while(GetMessage(&msg,NULL,0,0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
ReleaseDC(hWnd,hDC);
UnregisterClass((LPCTSTR)ClassAtom,hInst);
return 0;
Cleanup:
if(ClassAtom)
UnregisterClass((LPCTSTR)ClassAtom,hInst);
MessageBox(NULL,TEXT("启动程序失败。"),TEXT("无法启动程序"),MB_ICONEXCLAMATION);
return 1;
}
//=============================================================================
//函数:WndProc
//描述:窗口消息处理程序
//-----------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd,UINT uMsg,WPARAM wp,LPARAM lp)
{
RECT rc;
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_PAINT:
wglMakeCurrent(hDC,hRC);//使OpenGL获取焦点
GetClientRect(hWnd,&rc);//取得客户区域的大小
glViewport(rc.left,rc.top,rc.right-rc.left,rc.bottom-rc.top);//设置观察区域大小
glClear(GL_COLOR_BUFFER_BIT);//擦除画板
glBegin(GL_TRIANGLES);//画三角形表
glColor4d(1.0,0.0,0.0,1.0);//红色
glVertex2d( 0.0, 0.5);//三角形的顶角
glColor4d(0.0,1.0,0.0,1.0);//绿色
glVertex2d(-0.5,-0.5);//三角形的左角
glColor4d(0.0,0.0,1.0,1.0);//蓝色
glVertex2d( 0.5,-0.5);//三角形的右角
glEnd();//提交三角形表
glFlush();//让显卡渲染
SwapBuffers(hDC);//把画板内容刷到屏幕上
wglMakeCurrent(NULL,NULL);//释放焦点
break;
default:
return DefWindowProc(hWnd,uMsg,wp,lp);
}
return 0;
}BIN:
SRC:
页:
[1]