Recent

Author Topic: Opengl using 100%cpu  (Read 4975 times)

acp693

  • Jr. Member
  • **
  • Posts: 62
Opengl using 100%cpu
« on: June 25, 2009, 01:31:34 pm »
Hi, I'm trying to modify the opengl demo found in the directory components to my own needs. I need to be able to switch the animations on and off. I placed an openglcontrol on a form with two buttons(On and Off).

The modified code works, the animation stops when the off button is pressed, however, the cpu continues at 100% until I close the application. Somehow I need to disable the OnAppIdle function, but I don't know how to do this?

Regards

Albert

Code: [Select]
  {Author: Mattias Gaertner

}
unit MainUnit;

{$mode objfpc}{$H+}

interface

uses
  Classes, SysUtils, LCLProc, LResources, Forms, Controls, Graphics, Dialogs,
  OpenGLContext, GL, GLU, StdCtrls;

type

  { TForm1 }

  TForm1 = class(TForm)
    OnBtn: TButton;
    OffBtn: TButton;
    OpenGLControl1: TOpenGLControl;
    procedure OnBtnClick(Sender: TObject);
    procedure OffBtnClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure OpenGLControl1Paint(Sender: TObject);
    procedure OpenGLControl1Resize(Sender: TObject);
    procedure OnAppIdle(Sender: TObject; var Done: Boolean);
  private
  public
    cube_rotationx: GLFloat;
    cube_rotationy: GLFloat;
    cube_rotationz: GLFloat;

  end;

var
  Form1: TForm1;
  OnOff: boolean;
implementation

{ TForm1 }

procedure TForm1.OnBtnClick(Sender: TObject);   // Start rotation
begin

  OpenGLControl1.OnPaint :=@OpenGLControl1Paint;
  OnOff:=true
end;

procedure TForm1.OffBtnClick(Sender: TObject);  // stop rotation
begin
  OnOff:=false;

end;

procedure TForm1.FormShow(Sender: TObject);
begin
  with OpenGLControl1 do begin
    OnResize:=@OpenGLControl1Resize;
    AutoResizeViewport:=true;
  end;

  Application.AddOnIdleHandler(@OnAppIdle);
end;

procedure TForm1.OpenGLControl1Paint(Sender: TObject);
var
  Speed: Double;
begin
  if Onoff then begin
  if Sender=nil then ;
 
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glEnable(GL_DEPTH_TEST);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, double(width) / height, 0.1, 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();

  glTranslatef(0.0, 0.0,-6.0);
  glRotatef(cube_rotationx, cube_rotationy, cube_rotationz, 0.0);

  glBegin(GL_QUADS);
          glColor3f(0.0,1.0,0.0);                              // Set The Color To Green
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Right Of The Quad (Top)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Left Of The Quad (Top)
          glVertex3f(-1.0, 1.0, 1.0);                  // Bottom Left Of The Quad (Top)
          glVertex3f( 1.0, 1.0, 1.0);                  // Bottom Right Of The Quad (Top)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,0.5,0.0);                              // Set The Color To Orange
          glVertex3f( 1.0,-1.0, 1.0);                  // Top Right Of The Quad (Bottom)
          glVertex3f(-1.0,-1.0, 1.0);                  // Top Left Of The Quad (Bottom)
          glVertex3f(-1.0,-1.0,-1.0);                  // Bottom Left Of The Quad (Bottom)
          glVertex3f( 1.0,-1.0,-1.0);                  // Bottom Right Of The Quad (Bottom)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,0.0,0.0);                              // Set The Color To Red
          glVertex3f( 1.0, 1.0, 1.0);                  // Top Right Of The Quad (Front)
          glVertex3f(-1.0, 1.0, 1.0);                  // Top Left Of The Quad (Front)
          glVertex3f(-1.0,-1.0, 1.0);                  // Bottom Left Of The Quad (Front)
          glVertex3f( 1.0,-1.0, 1.0);                  // Bottom Right Of The Quad (Front)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,1.0,0.0);                              // Set The Color To Yellow
          glVertex3f( 1.0,-1.0,-1.0);                  // Bottom Left Of The Quad (Back)
          glVertex3f(-1.0,-1.0,-1.0);                  // Bottom Right Of The Quad (Back)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Right Of The Quad (Back)
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Left Of The Quad (Back)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(0.0,0.0,1.0);                              // Set The Color To Blue
          glVertex3f(-1.0, 1.0, 1.0);                  // Top Right Of The Quad (Left)
          glVertex3f(-1.0, 1.0,-1.0);                  // Top Left Of The Quad (Left)
          glVertex3f(-1.0,-1.0,-1.0);                  // Bottom Left Of The Quad (Left)
          glVertex3f(-1.0,-1.0, 1.0);                  // Bottom Right Of The Quad (Left)
  glEnd();
  glBegin(GL_QUADS);
          glColor3f(1.0,0.0,1.0);                              // Set The Color To Violet
          glVertex3f( 1.0, 1.0,-1.0);                  // Top Right Of The Quad (Right)
          glVertex3f( 1.0, 1.0, 1.0);                  // Top Left Of The Quad (Right)
          glVertex3f( 1.0,-1.0, 1.0);                  // Bottom Left Of The Quad (Right)
          glVertex3f( 1.0,-1.0,-1.0);                  // Bottom Right Of The Quad (Right)
  glEnd();

  Speed := double(OpenGLControl1.FrameDiffTimeInMSecs)/10;

  cube_rotationx += 5.15 * Speed;
  cube_rotationy += 5.15 * Speed;
  cube_rotationz += 20.0 * Speed;

  OpenGLControl1.SwapBuffers;
  end;
end;

procedure TForm1.OpenGLControl1Resize(Sender: TObject);
begin
  if Sender=nil then ;
  if OpenGLControl1.Height <= 0 then exit;
end;

procedure TForm1.OnAppIdle(Sender: TObject; var Done: Boolean);
begin
  Done:=false;
  //DebugLn(['TForm1.OnAppIdle ']);
  OpenGLControl1.Invalidate;
end;

initialization
  {$I mainunit.lrs}

end.
                                                                                               

Vincent Snijders

  • Administrator
  • Hero Member
  • *
  • Posts: 2661
    • My Lazarus wiki user page
Re: Opengl using 100%cpu
« Reply #1 on: June 25, 2009, 02:03:05 pm »

acp693

  • Jr. Member
  • **
  • Posts: 62
Re: Opengl using 100%cpu
« Reply #2 on: June 25, 2009, 05:02:21 pm »
Thank you Vincent, That's it. Sorry for asking what must seem very obvious questions. But I'm very new to FPC and Lazarus and I'm working through all the elements I need for my application to work before starting the real programming.

Thanks again

Albert

PeterX

  • Sr. Member
  • ****
  • Posts: 341
Re: Opengl using 100%cpu
« Reply #3 on: March 29, 2021, 11:36:31 am »
Done:=false;  in OnAppIdle() eats up all CPU resources. 100%
usually using latest Lazarus release version with Windows 10

 

TinyPortal © 2005-2018