unit unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, ExtCtrls, LCLType, LclIntf,
OpenGLContext, GL, math;
type
{ TForm1 }
TForm1 = class(TForm)
OpenGLControl1: TOpenGLControl;
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure OpenGLControl1Paint(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
private
FRotation: GLfloat;
procedure InitGL;
end;
var
Form1: TForm1;
const
LightDiffuse: array[0..3] of Single = (1, 1, 1, 1);
implementation
procedure gluPerspective(fovy, aspect, zNear, zFar: GLdouble);
var
Width, Height: Double;
begin
Height := tan(fovy * pi / 360) * zNear;
Width := Height * aspect;
glFrustum(-Width, Width, -Height, Height, zNear, zFar);
end;
{$R *.lfm}
{ TForm1 }
procedure TForm1.OpenGLControl1Paint(Sender: TObject);
const
AlreadyInit: Boolean = False;
begin
if not(AlreadyInit) then
InitGL;
glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
glLoadIdentity;
glTranslatef(0, 0, -5);
glRotatef(FRotation, 0, 1, 0);
glBegin(GL_TRIANGLES);
glColor3f (1, 0, 0); glVertex3f( 0, 1, 0);
glColor3f (0, 1, 0); glVertex3f(-1, -1, 1);
glColor3f (0, 0, 1); glVertex3f( 1, -1, 1);
glColor3f (1, 0, 0); glVertex3f( 0, 1, 0);
glColor3f (0, 0, 1); glVertex3f( 1, -1, 1);
glColor3f (0, 1, 0); glVertex3f( 1, -1, -1);
glColor3f (1, 0, 0); glVertex3f( 0, 1, 0);
glColor3f (0, 1, 0); glVertex3f( 1, -1, -1);
glColor3f (0, 0, 1); glVertex3f(-1, -1, -1);
glColor3f (1, 0, 0); glVertex3f( 0, 1, 0);
glColor3f (0, 0, 1); glVertex3f(-1, -1, -1);
glColor3f (0, 1, 0); glVertex3f(-1, -1, 1);
glEnd;
OpenGLControl1.SwapBuffers;
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
OpenGLControl1.Anchors := [akTop, akLeft, akBottom, akRight];
OpenGLControl1.MultiSampling := 8;
FRotation := 0;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
OpenGLControl1.Invalidate;
if GetKeyState(VK_RSHIFT) < 0 then
FRotation := FRotation - 0.8
else
if GetKeyState(VK_LSHIFT) < 0 then
FRotation := FRotation + 0.8;
end;
procedure TForm1.InitGL;
begin
glShadeModel(GL_SMOOTH);
glClearColor(0, 0, 0, 0);
glClearDepth(1);
glEnable(GL_DEPTH_TEST);
glDepthFunc(GL_LEQUAL);
glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
glViewport(0, 0, OpenGLControl1.Width, OpenGLControl1.Height);
glMatrixMode(GL_PROJECTION);
glLoadIdentity;
gluPerspective(45, OpenGLControl1.Width/OpenGLControl1.Height, 0.1, 100);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity;
glEnable(GL_COLOR_MATERIAL);
glEnable(GL_LIGHTING);
glLightfv(GL_LIGHT1, GL_DIFFUSE, LightDiffuse);
glEnable(GL_LIGHT1);
end;
end.