Recent

Author Topic: Still image (single frame) or how to stop opengl?  (Read 5599 times)

Peiman

  • New Member
  • *
  • Posts: 41
Still image (single frame) or how to stop opengl?
« on: July 13, 2011, 10:57:17 pm »
Hi,

Opengl refreshes the scene automatically by a certain FPS. I wonder if it is possible to render just one frame. Is there any function to stop its working? I even used destroy destructor. though rendering stopped but the cpu usage remained high until I stopped the main application. 

Leledumbo

  • Hero Member
  • *****
  • Posts: 8834
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Still image (single frame) or how to stop opengl?
« Reply #1 on: July 14, 2011, 06:44:15 am »
Quote
Opengl refreshes the scene automatically by a certain FPS
Are you sure? Coz in my app, the main loop does it. And without, the scene doesn't get refreshed at all. Would you mind showing some code?

Peiman

  • New Member
  • *
  • Posts: 41
Re: Still image (single frame) or how to stop opengl?
« Reply #2 on: July 14, 2011, 08:01:31 am »
This is the code,
TT and C are depth-sorted arrays of triangles and their respective colors.
I used the bool variable Q to assure only once the paint is done and on the second time it destroys the openglcontrol1. After calling the destroy method, it does not render any more, but the cpu core #4 keeps its 40% performance.

urender, geotypes and geofunc are my units for geometrical calculations

Code: [Select]
uses
  Classes, Windows,SysUtils, LCLProc, LResources, Forms, Controls, Graphics, Dialogs,
  OpenGLContext, GL, GLU, urender, geotypes, geofunc;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure OpenGLControl1Paint(Sender: TObject);
  private
  public
    OpenGLControl1: TOpenGLControl;
  end;

var
  Form1: TForm1;
  Q : boolean = true;
implementation

{$R *.lfm}



{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin

  OpenGLControl1:=TOpenGLControl.Create(form1);
  with OpenGLControl1 do begin
    Name:='OpenGLControl1';
    Align:=alClient;
    Parent:=Self;
    OnPaint:=@OpenGLControl1Paint;
    AutoResizeViewport:=true;
  end;
end;

procedure TForm1.OpenGLControl1Paint(Sender: TObject);
var
  i,j,N : integer;
  r : single;
begin
if Q then
  begin
  glClearColor(1.0, 1.0, 1.0, 1.0);
  glClear(GL_COLOR_BUFFER_BIT);
  glMatrixMode(GL_PROJECTION);
  glLoadIdentity();
  gluPerspective(45.0, double(width) / height, 0.1, 100.0);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity();
  gluLookAt(5,5,5,0,0,0,0,1,0);
  glEnable(GL_BLEND);
  glBlendFunc (GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);


// opengl geometry
  n := 10;
  for i := 0 to n-1 do
    begin
    glBegin(GL_TRIANGLES);
    glColor4f(C[i].x,C[i].y,C[i].z,0.15);
      for j := 0 to 2 do
        glVertex3f( TT[i,j].x, TT[i,j].y, TT[i,j].z); 
    glEnd();
    end;

  OpenGLControl1.SwapBuffers;
  Q := not(Q);
  end
else
  OpenGLControl1.destroy;
end;

end.

User137

  • Hero Member
  • *****
  • Posts: 1791
    • Nxpascal home
Re: Still image (single frame) or how to stop opengl?
« Reply #3 on: July 15, 2011, 02:52:32 am »
You shouldn't need to call .Destroy at all. TForm frees all its child components automatically when program ends, such as TButton, TPanel... and TOpenGLControl, one of which you assigned to form1.

Anyway, it also looks weird code for 2 reasons:
1) You are calling OpenGLControl1.destroy instead of OpenGLControl1.free (or rather better FreeAndNil(OpenGLControl1) ). You should never call .Destroy directly... I do not know the difference exactly but that is basic.

2) Also destroying an object from within the object itself. Imagine this kind of internal code flow:
Code: [Select]
procedure TOpenGLControl.HandleEvents
begin
  ...
  case Event of
    event_Paint:
      DoPaint(self); // <-- Here would be called OpenGLControl1Paint()
        // from which you are destroying this object, and propably this whole code block that calls it
    ...
  end;
  ...
  DoSomethingElse; // Now if the object is destroyed, everything happening here
    // would be undefined and prone to Access violation
end;
« Last Edit: July 15, 2011, 03:02:03 am by User137 »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8834
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Still image (single frame) or how to stop opengl?
« Reply #4 on: July 15, 2011, 06:36:11 am »
Instead of having Q, you can simple assign nil to OnPaint of OpenGLControl1. I have no idea which code takes 40% cpu, I'll try debugging your code tonight.

 

TinyPortal © 2005-2018