同城约会| 杂志期刊| 小说| 两性论坛| 军事电影| 两性知识| 电脑知识| 汽车| 旅游| 收藏

Delphi版OpenGL样例代码导游

来源: 作者: 出处:综艺读书 2007-04-02 
关 键 词:设计  windows  os  ie  delphi  

由于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;

至此,一个opengl程序的框架就大致成型。但还有问题要解决。

第一,要防止windows擦除背景而影响速度。在form中加入成员函数

  private
    procedure WMEraseBkgnd(var Message: TWmEraseBkgnd); message WM_ERASEBKGND;

procedure TGLWindow.WMEraseBkgnd(var Message: TWmEraseBkgnd);
begin
  Message.Result := 1;
end;

第二,为了更保险些。再增加以下成员函数。

  protected
    procedure CreateParams(var Params: TCreateParams); override;

procedure TGLWindow.CreateParams(var Params: TCreateParams);
begin
  inherited;
  with Params do
  begin
    Style := Style or WS_CLIPCHILDREN or WS_CLIPSIBLINGS;
    WindowClass.Style := CS_VREDRAW or CS_HREDRAW or CS_OWNDC;
  end;
end;

好,现在就可以忘掉这些麻烦的东西了,写你的精彩3d显示吧:)

还得唠叨几句,在一个线程里不要创建多个rc,这样会严重影响性能。有些个人的opengl窗口控件演示有在一个form上放多个控件,其实并非好主义。应该用一个opengl窗口显示多个视图。另外,不要跨线程访问opengl函数。

还有windows自动安装显卡驱动时不会安装opengl的硬件加速,一定要自己安装显卡厂商的驱动!

另外,副赠全屏显示的函数:)

function FullScreen(win: TWinControl; width, height, bitdepth: integer): boolean;
var displaymode: DEVMODE;
begin
  FillChar(displaymode, sizeof(displaymode), 0);
  with displaymode do
  begin
    dmSize := sizeof(displaymode);
    dmPelsWidth := width;
    dmPelsHeight := height;
    dmBitsPerPel := bitdepth;
    dmFields := DM_BITSPERPEL or DM_PELSWIDTH or DM_PELSHEIGHT;
  end;
  if ChangeDisplaySettings(displaymode, CDS_FULLSCREEN) = DISP_CHANGE_SUCCESSFUL
  then begin
    ShowWindow(win.Handle, WS_MAXIMIZE);
    result := true;
  end
  else result := false;
end;

procedure RestoreDisplay(win: TWinControl);
begin
  ChangeDisplaySettings(PDEVMODE(0)^, 0);
  ShowWindow(win.Handle, SW_RESTORE);
end;

========================================

li Qingrui
http://wrighteagle.org/appdev

进入讨论组讨论。
收藏此文】【 】【打印】【关闭
较早的文章:Delphiwe编写规范

较新的文章:Delphi帮助里的虚拟键值表
相关图文阅读
频道图文推荐
综艺读书宗旨
相关专题
热点标签: 设计  windows  os  ie  delphi  
最新技术文档
站内各频道最新更新文档
站内最新制作专题
热门关键字导读
Photoshop教 程照片处理 照片制作 PS快捷键 抠图
计 算 机 故 障XP系统修复
艺 术 与 设 计设计 流媒体 设计欣赏 边框
计 算 机 安 全ARP
站内频道文章精选
百度推荐,商机无限
搜索您感兴趣的内容
Web 全站
综艺电脑频道编辑信箱  告诉我们您想看的专题或文章

Google

友情互链 | 收藏本站 | 联系我们 | 在线留言 | 京ICP备08008424号|