Меню сайта


Статистика



Главная » Файлы » Исходники

Исходник для примера в теме про Delphi+OpenGL
04.12.2009, 23:54
unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,dglOpenGL, ExtCtrls,math;

type
TForm1 = class(TForm)
Timer1: TTimer;
procedure FormCreate(Sender: TObject);
procedure Timer1Timer(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure FormPaint(Sender: TObject);

private
{ Private declarations }
public
procedure Draw;
end;

var
Form1: TForm1;
angle: integer = 0;
Scale: single = 1.0;
Delta: single = 0.01;
hrc: HGLRC;

implementation

{$R *.dfm}

procedure SetDCPixelFormat(hdc : HDC);
var
pfd : TPixelFormatDescriptor;
nPixelFormat : Integer;
begin
FillChar (pfd, SizeOf (pfd), 0);
pfd.dwFlags:=PFD_DRAW_TO_WINDOW or PFD_SUPPORT_OPENGL or PFD_DOUBLEBUFFER;
pfd.iPixelType:=PFD_TYPE_RGBA;
pfd.cColorBits:=32;
nPixelFormat:=ChoosePixelFormat(hdc, @pfd);
SetPixelFormat(hdc, nPixelFormat, @pfd);
hrc:= wglCreateContext(hdc);
wglMakeCurrent(hdc, hrc);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
SetDCPixelFormat(Canvas.Handle);
end;

procedure TForm1.Draw;
var i: integer;
a: single;
begin

glViewport(0,0,ClientWidth,ClientHeight);
glClear(GL_COLOR_BUFFER_BIT);

glMatrixMode(GL_MODELVIEW);
glLoadIdentity;

glRotatef(DegToRad(angle)*2,0,0,1);
glBegin(GL_QUADS);
glColor3f(Abs(sin(Angle/10)),0,0);
glVertex2f(5,5);
glVertex2f(5,-5);
glColor3f(Abs(cos(Scale)),0,sin(Angle/10));
glVertex2f(-5,-5);
glVertex2f(-5,5);
glEnd;

glScalef(2,2,2);
for i:=0 to 2 do begin
glRotatef(-15-sin(DegToRad(angle))*6,0,0,1);
glBegin(GL_TRIANGLES);
glColor3f(sin(DegToRad(angle+i*10)),0.0,0.0);
glVertex2f(cos(DegToRad(angle)),sin(DegToRad(angle)));
glColor3f(Abs(cos(DegToRad(angle-i*2))),sin(DegToRad(angle+i*2)),0.0);
glVertex2f(0,0);
glColor3f(0.0,0.0,1.0);
glVertex2f(cos(DegToRad(angle+60)),sin(DegToRad(angle+60)));

glColor3f(1.0,0.0,0.0);
glVertex2f(-cos(DegToRad(angle)),-sin(DegToRad(angle)));
glColor3f(0.0,1.0,0.0);
glVertex2f(0,0);
glColor3f(0.0,0.0,1.0);
glVertex2f(-cos(DegToRad(angle+60)),-sin(DegToRad(angle+60)));

glEnd;
end;

glScalef(10,10,10);
for i:=0 to 4 do begin
glRotatef(45+sin(DegToRad(angle))*10,0,0,1);
glBegin(GL_TRIANGLES);
glColor3f(sin(angle/10+i*100),sin(DegToRad(angle+i*10)),Abs(sin(Scale/10)));
glVertex2f(cos(DegToRad(angle/2)),sin(DegToRad(angle/2)));
glColor3f(sin(DegToRad(angle+i*10)),sin(angle/10+i*100),Abs(cos(Scale/10)));
glVertex2f(cos(DegToRad(angle/2+15)),sin(DegToRad(angle/2+15)));
glColor3f(Scale+i*10,sin(DegToRad(angle)),sin(angle/10)+i*10);
glVertex2f(0,0);
glColor3f(Abs(sin(DegToRad(angle/10))),0.6,sin(DegToRad(angle)));
glVertex2f(-cos(DegToRad(angle/2)),-sin(DegToRad(angle/2)));
glColor3f(Abs(sin(DegToRad(angle+i*5))),sin(DegToRad(angle/10+i*100)),Abs(sin(Scale+i*10)));
glVertex2f(-cos(DegToRad(angle/2+15)),-sin(DegToRad(angle/2+15)));
glColor3f(Abs(cos(DegToRad(angle+i*10)/10)),sin(DegToRad(angle+i*5)),1);
glVertex2f(0,0);
glEnd;
end;

glColor3f(1,1,1);
glEnable(GL_BLEND);
glBlendFunc(GL_DST_COLOR,GL_ONE);
glLoadIdentity;

glRotatef(2*angle,0,0,1);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0,0);
a:=0;
while a <= 2*pi do begin
a:=a+2*pi/360;
glColor3f(cos(a*2),1,sin(a*2));
glVertex2f(cos(a)/2,sin(a)/2);
end;
glEnd;

glBlendFunc(GL_DST_COLOR,GL_ONE_MINUS_DST_COLOR);
glRotatef(-4*angle,0,0,1);
glBegin(GL_TRIANGLE_FAN);
glVertex2f(0,0);
a:=0;
while a <= 2*pi do begin
a:=a+2*pi/360;
glColor3f(0.6,sin(a*2)*0.6,cos(a*2)*0.6);
glVertex2f(cos(a),sin(a));
end;
glEnd;

glDisable(GL_BLEND);
SwapBuffers(Canvas.Handle);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
inc(angle);
if angle = 360 then angle:=0;
Scale:=Scale + delta;
if Scale >= 2 then delta:=-0.0085;
if Scale <=0.5 then delta:=0.0085;
Form1.Paint;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
wglDeleteContext(hrc);
wglMakeCurrent(0,0);
end;

procedure TForm1.FormPaint(Sender: TObject);
begin
Form1.Draw;
end;

end.

Категория: Исходники | Добавил: Molch
Просмотров: 877 | Загрузок: 2 | Рейтинг: 5.0/1
Всего комментариев: 0
Добавлять комментарии могут только зарегистрированные пользователи.
[ Регистрация | Вход ]

Скачать