english languagejezyk polski
back to gamecaskforum index
It is currently 6 Sep 2010, o 01:11




Post new topic Reply to topic  [ 1 post ] 
Author Message
 Post subject: OpenGL: Basic methods of drawing
PostPosted: 14 Feb 2010, o 13:01 
User avatar

Joined: 29 Dec 2009, o 11:44
Posts: 20
Especially for the needs of this tutorial I have prepared a very simple framework which consists of the following modules:
* GameDevice (contains processes essential to initialize OpenGL)
* GameTimer (clock defining time of displaying successive frames of animation)
* GameEngine (object linking all modules of framework)
Connecting OpenGL to your application is not difficult today – it is enough to look at the following code:

Code:
unit Unit1;

interface

uses Classes, Controls, Forms;

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private declarations }
    procedure RENDER(Sender: TObject);
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

uses OpenGL, GamecaskEngine;

var Engine: TGamecaskEngine;

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Engine:= TGamecaskEngine.Create();
  Engine.Control:= TCustomControl(Form1);
  Engine.GameInitialize();
  Engine.GameDevice.Align:= alClient;
  Engine.GameDevice.Size:= Point(200, 200);
  Engine.GameDevice.OnRender:= RENDER;
  Engine.GameTimer.Interval:= 100;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  Engine.GameFinalize();
  Engine.Free();
end;

procedure TForm1.RENDER(Sender: TObject);
begin
  //
end;

end.

As the title of tutorial suggests we deal with essentials of OpenGL and not writing frameworks, that is why let’s concentrate only on RENDER procedure. The RENDER procedure is a place where we will draw subsequent frames of animation with refreshing every X milisecond. X value is a number of miliseconds after which another "painting" of the animation frame takes place, and we attribute it to a variable Interval of a clock:

Code:
Engine.GameTimer.Interval:= 100;
// 1000 miliseconds equals 1 second so in a second
// RENDER procedure will be evoked 10 times.

Below there is an attachment with the full source code of the application.

Attachment:
File comment: Source code.
example0.zip [7.42 KiB]
Downloaded 57 times

Now we can deal with basic methods of drawing in OpenGL.

Code:
procedure TForm1.RENDER(Sender: TObject);
begin

  glColor3f(1, 0, 0);   // color RGB: red, green, blue
  glBegin(GL_POINTS);
    glVertex3f(0, 0, 0);   // x, y, z
  glEnd;

  glColor3f(0, 1, 0);
  glBegin(GL_LINES);
    glVertex3f(-80, 10, 0);
    glVertex3f( 80, 10, 0);
  glEnd;

  glColor3f(0, 0, 1);
  glBegin(GL_TRIANGLES);
    glVertex3f( 0, 40, 0);
    glVertex3f(40, 60, 0);
    glVertex3f(20, 20, 0);
  glEnd;

  glColor3f(1, 1, 0);
  glBegin(GL_QUADS);
    glVertex3f(-40, -40, 0);
    glVertex3f(-60, -40, 0);
    glVertex3f(-60, -60, 0);
    glVertex3f(-40, -60, 0);
  glEnd;

  glColor3f(0, 1, 1);
  glBegin(GL_POLYGON);
    glVertex3f(40, -40, 0);
    glVertex3f(60, -40, 0);
    glVertex3f(60, -60, 0);
    glVertex3f(40, -70, 0);
    glVertex3f(20, -30, 0);
    glVertex3f(40, -10, 0);
  glEnd;

end;

Result of the above code can be visible in the following picture.

Image

Below there is a source code of a program which describes the way of creating animation with the possibility of connecting OpenGL window to a component of TPanel type.

Attachment:
File comment: Source code.
example1.zip [7.82 KiB]
Downloaded 55 times


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 1 post ] 


Who is online

Users browsing this forum: No registered users and 1 guest


You cannot post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  
cron

Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group.
© 2009 - 2010, gamecask

phpBB SEO