Creating your first LCL program
Using LCL is typically the easiest way to access OpenGL with Lazarus. Since GLUT is deprecated, using the LCL is generally a good idea for a new OpenGL Lazarus project. Most of the code samples described below for GLUT are easy to translate into the LCL code, though you will have to find equivalents for the functions with the 'glut' prefix, for example instead of "glutSwapBuffers" we will use the LCL's "SwapBuffers" property to display our rendering. The one great feature that GLUT provides that is hard to do with the LCL is showing text on the screen (see GLUT's "Bitmap Fonts" section below). However, since this is your first LCL program, we will keep it simple by not showing any text.
Lazarus comes with a example OpenGL program, you can find it in the folder Lazarus/Examples/openglcontrol. That example demonstrates many powerful features for creating an animated OpenGL image. However, it is also a relatively complicated program. Below is a minimal Lazarus project that mimics some of the features described in the GLUT samples described below. To create this, launch Lazarus and choose Project/NewProject to create a new application. Choose the Project/ProjectInspector menu item, click the 'Add..' button, go to the 'New Requirement' and and the "LazOpenGLContext" package. Next, paste the code below into your 'unit1.pas'. Then, click on your form and in the events tab of the object inspector link the "OnCreate" event to the function "FormCreate". You should now be able to run your new application by choosing the Run/Run menu item.
The code creates a new OpenGL panel that fills the form. OpenGL draws a simple triangle in the form. Note that when you run the application you can resize the form and the triangle rescales proportionally to fill the form.
创建你的第一个 lcl 程序
使用lcl 最简单的创建opengl 的方法。 因为glut 过时了。 在lazarus 工程中使用lcl 是一个好主意。 很多glut代码例子 可以很容易的翻译成lcl 代码。 你可以发现函数有 glut前缀, 例如代替glutswapbuffers 函数 lcl 中使用swapbuffers 函数显示我们的涂抹。 glut最伟大的性能是他提供了显示text 在屏幕上。而lcl 显示text在屏幕上有点困难. 因为这是你的第一个lcl 程序。 我们将保持她简单一点,不显示任何text.
lazarus 一个简单的opengl 程序。 你可以在如下目录找到它 Lazarus/Examples/openglcontrol. 这个例子 说明了很多强大的性能 例如 创建动画。这是一个相对复杂的程序。 下面是一个最小的lazarus 工程 。模拟了一些glut例子的 性能。
创建lazarus +opengl 程序 了 步骤如下: 打开 Lazarus 创建一个新工程 Project/NewProject to create a new application .
选择下面的菜单 Project/ProjectInspector 然后单击 add按钮,然后单击new requirement 然后选择LazOpenGLContext 包。
Next, paste the code below into your 'unit1.pas'. Then, click on your form and in the events tab of the object inspector link the "OnCreate" event to the function "FormCreate". You should now be able to run your new application by choosing the Run/Run menu item.
The code creates a new OpenGL panel that fills the form. OpenGL draws a simple triangle in the form. Note that when you run the application you can resize the form and the triangle rescales proportionally to fill the form.
--------------------------------------------------------
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, OpenGLContext, gl;
type
{ TForm1 }
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure GLboxPaint(Sender: TObject);
private
{ private declarations }
public
{ public declarations }
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
var
GLBox:TOpenGLControl;
{ TForm1 }
procedure TForm1.GLboxPaint(Sender: TObject);
begin
glClearColor(0.27, 0.53, 0.71, 1.0); //Set blue background
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity;
glBegin(GL_TRIANGLES);
glColor3f(1, 0, 0);
glVertex3f( 0.0, 1.0, 0.0);
glColor3f(0, 1, 0);
glVertex3f(-1.0,-1.0, 0.0);
glColor3f(0, 0, 1);
glVertex3f( 1.0,-1.0, 0.0);
glEnd;
GLbox.SwapBuffers;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
GLbox:= TOpenGLControl.Create(Form1);
GLbox.AutoResizeViewport:= true;
GLBox.Parent := self;
GLBox.MultiSampling:= 4;
GLBox.Align := alClient;
GLBox.OnPaint := @GLboxPaint; //for "mode delphi" this would be "GLBox.OnPaint := GLboxPaint"
GLBox.invalidate;
end;
end.