【问题标题】:Delphi OpenGL traiangle does not rotateDelphi OpenGL三角形不旋转
【发布时间】:2014-08-26 21:57:11
【问题描述】:

我正在尝试对我通过旋转制作的三角形进行动画处理,但无法弄清楚如何做到这一点。 除了在 FormPaint 函数之前使用 glRotateF 之外,还有其他东西。

unit tri;    
interface
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, OpenGL;

const
  GL_NO_ERROR = 0;

type
  HGLRC = THandle;
  GLenum = Cardinal;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    GLContext : HGLRC;
    glDC: HDC;
    errorCode: GLenum;
    openGLReady: Boolean;
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  pfd: TPixelFormatDescriptor;
  FormatIndex: Integer;
begin
  FillChar(pfd,SizeOf(pfd),0);
  with pfd do
  begin
    nSize := SizeOf(pfd);
    nVersion := 1; {The current version of the desccriptor is 1}
    dwFlags := PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL;
    iPixelType := PFD_TYPE_RGBA;
    cColorBits := 24; {support 24-bit color}
    cDepthBits := 32; {depth of z-axis}
    iLayerType := PFD_MAIN_PLANE;
  end;
  glDC := getDC(handle);
  FormatIndex := ChoosePixelFormat(glDC,@pfd);
  SetPixelFormat(glDC,FormatIndex,@pfd);
  GLContext := wglCreateContext(glDC);
  wglMakeCurrent(glDC,GLContext);
  OpenGLReady := true;
end;


procedure TForm1.FormPaint(Sender: TObject);
begin
  if not openGLReady then
    exit;
  {background}
  glClearColor(0.1,0.0,0.1,0.0);
  glClear(GL_COLOR_BUFFER_BIT);
  glLoadIdentity; // Reset The View
  glTranslatef(0.0, 0, 0.0);
  glRotateF (360, 0.0, 0.0, 1.0);
  glBegin( GL_POLYGON ); // start drawing a polygon
    glColor3f( 1.0, 0.0, 0.0);
    glVertex3f( 0.0, 0.5, 0.0 ); // Top
    glColor3f(0.0, 1.0, 0.0);
    glVertex3f( 0.5, -0.5, 0.0 ); // Bottom Right
    glColor3f(0.0, 0.0, 1.0);
    glVertex3f( -0.5, -0.5, 0.0 ); // Bottom Left
  glEnd;
  glFlush;
  {error checking}
  errorCode:=glGetError;
   if errorCode<>GL_NO_ERROR then
      raise Exception.Create('Error in Paint'#13+gluErrorString(errorCode));
  SwapBuffers(wglGetCurrentDC);
  glFlush();
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  wglMakeCurrent(Canvas.Handle,0);
  wglDeleteContext(GLContext);
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  if not openGLReady then
  exit;
  glViewPort(0,0,ClientWidth,ClientHeight);
  glOrtho(-1.0,1.0,-1.0,1.0,-1.0,1.0);
  errorCode := glGetError;
  if errorCode<>GL_NO_ERROR then
  raise Exception.Create('FormResize:'+gluErrorString(errorCode));
end;

procedure GLInit;
begin
  // set viewing projection
  glMatrixMode(GL_PROJECTION);
  glFrustum(-0.1, 0.1, -0.1, 0.1, 0.3, 15.0);
  // position viewer
  glMatrixMode(GL_MODELVIEW);
  glEnable(GL_DEPTH_TEST);
end;

end.

由于我使用的是 delphi 7 的 VCL 组件,我需要什么?以及如何设置它以使我的三角形在 z 轴上旋转?

【问题讨论】:

  • 您问题的标题与内容无关。请修改。
  • 如果你打算进行 OpenGL 编程,你真的很想使用 a more modern style. glBeginglEnd 已被弃用,在某些实现中甚至根本不存在,包括OpenGLES,当今几乎所有移动设备的图形系统。
  • 我正在使用 jon jacobs 的“delphi 开发人员 opengl 指南”,不幸的是,它是唯一可用的关于 opengl for delphi 的书。

标签: delphi opengl delphi-7


【解决方案1】:

替换

glRotateF (360, 0.0, 0.0, 1.0);

通过

glRotateF (alpha, 0.0, 0.0, 1.0);

其中alpha: GLfloat; 是您在FormCreate 中设置为0 的表单类的私有成员。现在,只需将TTimer 添加到您的表单中,将其Interval 设置为30 并在其OnTimer 事件中添加以下代码:

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  alpha := alpha + 0.1;
  Invalidate;
end;

(要去除闪烁,可以添加

procedure TForm1.WMEraseBkgnd(var Message: TWMEraseBkgnd);
begin
  Message.Result := 0;
end;

也到你的班级。)

【讨论】:

  • WMEraseBkbnd 不能消除闪烁,但其他一切正常。
  • @azimuthgust:您确实将其声明为受保护的消息过程,对吗? procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-04-02
  • 2014-11-06
  • 2012-11-12
相关资源
最近更新 更多