二级Delphi版OpenGL样例代码导游
来源:优易学  2011-11-2 15:49:25   【优易学:中国教育考试门户网】   资料下载   IT书店

  由于Delphi自带OpenGL.pas是1.0版的,而现在实际使用的至少是1.1版,Windows纯软件模拟方式也是1.1版的,所以要自己导入一些必要的函数。也可用一些开源的免费单元,如Mike Lischke的OpenGL12.pas。当然,自己写可以设计得更简洁,而且不必在过于超前完备的庞大代码中找错误。
  首先引入必要的单元Windows, Messages, OpenGL
  要增加一些必要的扩展。
  const
  // GL_EXT_bgra
  GL_BGR_EXT = $80E0;
  GL_BGRA_EXT = $80E1;
  // polygon offset
  GL_POLYGON_OFFSET_UNITS = $2A00;
  GL_POLYGON_OFFSET_POINT = $2A01;
  GL_POLYGON_OFFSET_LINE = $2A02;
  GL_POLYGON_OFFSET_FILL = $8037;
  GL_POLYGON_OFFSET_FACTOR = $8038;
  procedure glBindTexture(target: GLEnum; texture: GLuint); stdcall; external opengl32;
  procedure glDeleteTextures(n: GLsizei; textures: PGLuint); stdcall; external opengl32;
  procedure glGenTextures(n: GLsizei; textures: PGLuint); stdcall; external opengl32;
  function glIsTexture(texture: GLuint): GLboolean; stdcall; external opengl32;
  procedure glPolygonOffset(factor, units: GLfloat); stdcall; external opengl32;
  // 此声明用于纠正OpenGL.pas的一个bug
  function gluBuild2DMipmaps(target: GLEnum; components, width, height: GLint; format, atype: GLEnum; Data: Pointer): GLint; stdcall; external opengl32;
  现在接口已经基本升级到1.1版。如果还需要其他扩展,可类似增加。
  接下来,要创建OpenGL的绘图上下文RC,为此需要GDI窗口的设备上下文DC。TForm.Handle属性或其他TWinControl的Handle属性都是DC。可使用如下函数由DC创建RC,返回值为RC的句柄。之后即可使用OpenGL绘图。一般可在Form的OnCreate事件内使用。此函数的选项含义分别为深度缓冲区,模版缓冲区,积累缓冲区,生成Alpha通道的值。
  type
  TRCOptions = set of (roDepth, roStencil, roAccum, roAlpha);
  function CreateRC(dc: HDC; opt: TRCOptions): HGLRC;
  var
  PFDescriptor: TPixelFormatDescriptor;
  PixelFormat: Integer;
  begin
  FillChar(PFDescriptor, SizeOf(PFDescriptor), 0);
  with PFDescriptor do
  begin
  nSize := SizeOf(PFDescriptor);
  nVersion := 1;
  dwFlags := PFD_SUPPORT_OPENGL or PFD_DRAW_TO_WINDOW or PFD_DOUBLEBUFFER;
  iPixelType := PFD_TYPE_RGBA;
  cColorBits := GetDeviceCaps(DC, BITSPIXEL) * GetDeviceCaps(DC, PLANES);
  if roDepth in opt then cDepthBits := 24;
  if roStencil in opt then cStencilBits := 8;
  if roAccum in opt then cAccumBits := 64;
  iLayerType := PFD_MAIN_PLANE;
  end;
  PixelFormat := ChoosePixelFormat(DC, @PFDescriptor);
  Assert(PixelFormat <> 0);
  Assert(SetPixelFormat(DC, PixelFormat, @PFDescriptor));
  Result := wglCreateContext(DC);
  Assert(Result <> 0);
  wglMakeCurrent(dc, Result);
  end;
  在Form的OnPaint事件里绘图。记住,绘图完成后要用SwapBuffers(dc: HDC)交换绘图缓冲和显示缓冲,这样图象才会显示出来。还要记得在Form的OnResize事件里调用 glViewport(0, 0, ClientWidth, ClientHeight); 好让RC和DC同步。
  在Form的OnDestroy事件里销毁RC。
  procedure DestroyRC(rc: HGLRC);
  begin
  if rc = 0 then Exit;
  wglMakeCurrent(0, 0);
  wglDeleteContext(rc);
  end;

[1] [2] 下一页

责任编辑:小草

文章搜索:
 相关文章
热点资讯
资讯快报
热门课程培训