xShandow 发表于 2004-11-19 11:10

给出我目前所使用的OpenGL测试框架代码(2D)

<P>目前只讨论2D,因为3D的基础应该是2D,掌握好了2D,才能更好的掌握3D,以下是我所用的代码.所有的绘制部分,我为了简单起见,全部放在一个叫Draw的函数里进行.

这是文件下载地址.
</P>
<P>
#include &lt;windows.h&gt; //一定要放在gl/gl.h之前,
#include &lt;gl/gl.h&gt;
#include &lt;math.h&gt;</P>
<P>#pragma comment(lib,"OpenGL32.lib")</P>
<P>HGLRC hRC;
HDC   hDC;</P>
<P>//回调函数
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM ,LPARAM);</P>
<P>//设置视见区大小
void SceneResizeViewport(GLsizei w,GLsizei h)
{
if(h==0)
  h=1;
//设置视见区
glViewport(0,0,w,h);</P>
<P>//重置投影矩阵
glMatrixMode(GL_PROJECTION);
glLoadIdentity();</P>
<P>//设置修剪范围
glOrtho(0,w,h,0,0,1);
//重置模形矩阵
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}</P>
<P>//把绘制信息全放到一个函数里进行
//#define GL_PI 3.1415f
void Draw()
{
glPushMatrix();</P>
<P>  glBegin(GL_TRIANGLES);
  glVertex2f(25.0f,0.0f);
  glVertex2f(0.0f,25.0f);
  glVertex2f(50.0f,25.0f);
    glEnd();</P>
<P>
glPopMatrix();
}</P>
<P>//在这里进行渲染
void SceneShow()
{
//清除缓冲区及深度缓冲区
glClear(GL_COLOR_BUFFER_BIT );
//重置所有的矩阵
glLoadIdentity();
//以下绘制开始
Draw();</P>
<P>//交换缓冲区,显示
SwapBuffers(hDC);
}</P>
<P>//初始化OpenGL
void InitOpenGL(HWND hWnd)
{
PIXELFORMATDESCRIPTOR pfd;
int iFormat;</P>
<P>//从窗口句柄取得设备句柄
hDC=GetDC(hWnd);</P>
<P>//以下设置像素格式
ZeroMemory(&amp;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 = 16;
pfd.cDepthBits = 16;
pfd.iLayerType = PFD_MAIN_PLANE;</P>
<P>//设置像素格式
iFormat=ChoosePixelFormat(hDC,&amp;pfd);
SetPixelFormat(hDC,iFormat,&amp;pfd);
//创建渲染场景
hRC=wglCreateContext(hDC);
//设置为当前渲染场景
wglMakeCurrent(hDC,hRC);</P>
<P>glShadeModel(GL_SMOOTH);    //允许平滑着色
glClearColor(0.0f,0.0f,0.0f,1.0f);  //设置清屏颜色</P>
<P>//下面的代码没有确切的意思.是为了程序在运行初始能够显示而设立的
RECT rect;
int sw,sh;</P>
<P>GetClientRect(hWnd,&amp;rect);
sw=rect.right-rect.left;
sh=rect.bottom-rect.top;</P>
<P>if(sw &gt; 0 &amp;&amp; sh &gt;0 )
{
  SceneResizeViewport(sw,sh);
}

}</P>
<P>//释放OpenGL和窗体对象
void ReleaseOpenGL(HWND hWnd)
{
wglMakeCurrent(NULL,NULL);
wglDeleteContext(hRC);
ReleaseDC(hWnd,hDC);
}</P>
<P>//更改分辩率
bool ChangeResolution(int w,int h,int bitdepth,int fre)
{
DEVMODE devMode;
int modeSwitch;
int closeMode=0;</P>
<P>//枚举
EnumDisplaySettings(NULL,closeMode,&amp;devMode);</P>
<P>//设置为当前所请求的
devMode.dmBitsPerPel=bitdepth;
devMode.dmPelsHeight=h;
devMode.dmPelsWidth =w;
devMode.dmDisplayFrequency=fre;</P>
<P>devMode.dmFields=DM_BITSPERPEL | DM_PELSWIDTH |DM_PELSHEIGHT|DM_DISPLAYFREQUENCY;</P>
<P>//更改
modeSwitch=ChangeDisplaySettings(&amp;devMode,CDS_FULLSCREEN);</P>
<P>if(modeSwitch==DISP_CHANGE_SUCCESSFUL)
{
  return true;
}
else
{
  return false;
}
}</P>
<P>LRESULT CALLBACK WndProc(HWND hWnd,UINT message,WPARAM wParam,LPARAM lParam)
{
RECT rect;
int sw,sh;</P>
<P>switch(message)
{
case WM_SIZE:
  GetClientRect(hWnd,&amp;rect);
  sw=rect.right-rect.left;
  sh=rect.bottom-rect.top;</P>
<P>  if(sw &gt; 0 &amp;&amp; sh &gt;0 )
  {
   SceneResizeViewport(sw,sh);
  }
  return 0;</P>
<P>case WM_CLOSE:
  PostQuitMessage(0);
  return 0;
case WM_KEYDOWN:
  switch(wParam)
  {
  case VK_ESCAPE:
   PostMessage(hWnd,WM_CLOSE,0,0);
   break;
  }
  return 0;
}</P>
<P>return DefWindowProc(hWnd,message,wParam,lParam);
}</P>
<P>//创建一个窗口供OpenGL使用.
//窗口标题,程序实例句柄,窗口宽,高,颜色位,是否全屏
HWND CreateOpenGL(char*title,HINSTANCE hInst,int sw,int sh,int bit,bool fullscreen)
{
HWND hWnd;
WNDCLASS wc;</P>
<P>wc.style   = CS_OWNDC;
wc.lpfnWndProc  = WndProc;
wc.cbClsExtra  = 0;
wc.cbWndExtra  = 0;
wc.hInstance  = hInst;
wc.hIcon   = LoadIcon(NULL,IDI_APPLICATION);
wc.hCursor   = LoadCursor(NULL,IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
wc.lpszMenuName  = NULL;
wc.lpszClassName = "OpenGL框架程序";

//注册窗口类
RegisterClass(&amp;wc);</P>
<P>//全屏模式开
if(fullscreen)
{
  //创建窗口
  hWnd=CreateWindow("OpenGL框架程序",title,WS_POPUP | WS_CLIPSIBLINGS | WS_VISIBLE,
   0,0,sw,sh,NULL,NULL,hInst,NULL);
}
else
{
  //让窗口保持在屏幕的正中间,计算左起,和上起 坐标
  int t,l;
  l=(GetSystemMetrics(SM_CXSCREEN)-sw)/2;
  t=(GetSystemMetrics(SM_CYSCREEN)-sh)/2;</P>
<P>  hWnd=CreateWindow("OpenGL框架程序",title,WS_VISIBLE|WS_THICKFRAME|WS_SYSMENU|WS_MINIMIZEBOX,
   l,t,sw,sh,NULL,NULL,hInst,NULL);
}</P>
<P>//显示窗口
ShowWindow(hWnd,SW_SHOW);
//更新
UpdateWindow(hWnd);</P>
<P>if(fullscreen)
{
  //更改分辩率
  ChangeResolution(sw,sh,bit,60);
  //隐藏鼠标
  ShowCursor(false);
}</P>
<P>//返回窗口句柄
return hWnd;
}</P>
<P>//入口函数
int WINAPI WinMain(HINSTANCE hInst,HINSTANCE,LPSTR,int)
{
MSG msg;
HWND hWnd;
bool fullscreen=false;</P>
<P>//创建窗口
hWnd=CreateOpenGL("OpenGL 2D",hInst,640,480,16,fullscreen);</P>
<P>//初始化OpenGL设置
InitOpenGL(hWnd);</P>
<P>//进入循环
while(1)
{
  if(PeekMessage(&amp;msg,NULL,0,0,PM_REMOVE))
  {
   if(msg.message==WM_QUIT)
   {
    break;
   }
   else
   {
    TranslateMessage(&amp;msg);
    DispatchMessage(&amp;msg);
   }
  }
  else
  {
    SceneShow();
  }
}</P>
<P>ReleaseOpenGL(hWnd);
DestroyWindow(hWnd);</P>
<P>return 0;
}


</P>

[此贴子已经被作者于2004-11-19 16:15:39编辑过]
页: [1]
查看完整版本: 给出我目前所使用的OpenGL测试框架代码(2D)