| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 2345 人关注过本帖
标题:Delphi中OpenGL绘图,改变窗体大小出错
只看楼主 加入收藏
ldj905649880
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-12-8
收藏
 问题点数:0 回复次数:1 
Delphi中OpenGL绘图,改变窗体大小出错
代码如下,每次改变窗体大小的时候,绘出的图形就乱了,请大神指教怎么回事,
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OPENGL;

type
  TfrmMain = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    { Private declarations }
    hrc:HGLRC;
    w,h:glFloat;
    Sphere:GLUquadricObj;
  public
    { Public declarations }
    procedure MyDraw;
  end;

var
  frmMain: TfrmMain;

implementation

{$R *.dfm}

procedure TfrmMain.FormCreate(Sender: TObject);
var
  pfd:TPixelFormatDescriptor;   //设置描述表
  PixelFormat:Integer;
begin
  ControlStyle:=ControlStyle+[csOpaque];
  FillChar(pfd,sizeof(pfd),0);                //
  with pfd do
  begin
    nSize:=sizeof(TPixelFormatDescriptor);
    nVersion:=1;
    dwFlags:=PFD_DRAW_TO_WINDOW or PFD_SUPPORT_GDI or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
    iPixelType:=PFD_TYPE_RGBA;
    cColorBits:=24;
    cDepthBits:=32;
    iLayerType:=PFD_MAIN_PLANE;
  end;
  PixelFormat:=ChoosePixelFormat(Canvas.Handle,@pfd);
  SetPixelFormat(Canvas.Handle,PixelFormat,@pfd);
  hrc:=wglCreateContext(Canvas.Handle);
  w:=ClientWidth;
  h:=ClientHeight;

end;

procedure TfrmMain.FormDestroy(Sender: TObject);
begin
    wglMakeCurrent(Canvas.Handle,HRC);
    wglDeleteContext(hRc);
    ReleaseDC(Handle,Canvas.Handle);
end;

procedure TfrmMain.FormPaint(Sender: TObject);
begin
wglMakeCurrent(Canvas.Handle,hrc);
  MyDraw;
end;

procedure TfrmMain.FormResize(Sender: TObject);
begin

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity;
  glFrustum(-1.0,1.0,-1.0,1.0,3.0,7.0);
  glViewport(0,0,ClientWidth,ClientHeight);
    glMatrixMode(GL_PROJECTION);
    glLoadIdentity();
  MyDraw;
end;

procedure TfrmMain.MyDraw;
begin
  glClearColor(0.8,1,1,1);
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glLineWidth(3.0);

  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  glRotatef(45,0,0,1);
    glColor3f(1.0,0.0,0.0); //画红色的x轴
    glBegin(GL_LINES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(1.0, 0.0, 0.0);
    glEnd();
    glColor3f(0.0,1.0,0.0); //画绿色的y轴
    glBegin(GL_LINES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, 1.0, 0.0);
    glEnd();
    glColor3f(0.0,0.0,1.0); //画蓝色的z轴
    glBegin(GL_LINES);
        glVertex3f(0.0, 0.0, 0.0);
        glVertex3f(0.0, 0.0, 1.0);
    glEnd();

  glFlush;
  SwapBuffers(Canvas.handle);

end;

end.
搜索更多相关主题的帖子: procedure Windows private 
2013-12-08 22:19
ldj905649880
Rank: 1
等 级:新手上路
帖 子:2
专家分:0
注 册:2013-12-8
收藏
得分:0 
肿么没有大神解答下
2013-12-13 09:56
快速回复:Delphi中OpenGL绘图,改变窗体大小出错
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016685 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved