Index » Empathy Jukebox : Blob d59f27 / opengl / GLjukebox.pas
unit GLjukebox;

interface

uses
  gl,glu,glUtils,glUi,Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls,math,jpeg;

type
  TForm1 = class(TForm)
    Timer1: TTimer;
    procedure Timer1Timer(Sender: TObject);
    procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
    procedure Draw;
    procedure FormResize(Sender: TObject);
    procedure SetLbEnabled(var l : tglListbox);
    procedure FormPaint(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure FormCreate(Sender: TObject);


  private
    { Private declarations }

    ControlSet : TglControlSet;
    Artwork : array[0..3] of TglBitmap;
    Titles : array[0..3] of TglListbox;

    procedure createControls;

  public
    { Public declarations }
  end;

  type
  TColorRec = packed record
    b,g,r,a: Byte;
    end;


var
  Form1: TForm1;
  angle : single;
  //GL Vars
  light_diffuse : array[0..3] of GLFloat =  ( 1.5, 1.5, 1.5, 0.9 );
  light_position : array[0..3] of GLFloat = ( 1.5, 1.5, 1.5, 1 );
  DC:HDC;
  RC:HGLRC;




  rotation : integer = 0;
  //lastrotation : integer =0;
  velocity : integer;
  starttime : dword =0;
  endtime : dword =0;
  startpoint : integer;
  endpoint : integer;



implementation




procedure TForm1.createControls;
var
n : integer;
begin
ControlSet:=TglControlSet.Create(self);
ControlSet.RedrawProcedure:=draw;
//Front
Artwork[0]:=TglBitmap.Create(ControlSet);
with ArtWork[0] do begin
name:='Front Cover';
filename:='1.jpg';
Normal:=gl3D(0,0,1);
  with Texture do begin
    v1:=gl2D(0,0);
    v2:=gl2d(1,0);
    v3:=gl2d(1,1);
    v4:=gl2d(0,1);
  end;

  with Vertices do begin
    v1:=gl3d(-1.5, -0.75,  1.5);
    v2:=gl3d(0, -0.75,  1.5);
    v3:=gl3d(0, 0.75,  1.5);
    v4:=gl3d(-1.5, 0.75,  1.5);
  end;
end;

//Right
Artwork[1]:=TglBitmap.Create(ControlSet);
with ArtWork[1] do begin
name:='Right Cover';
filename:='2.jpg';
Normal:=gl3D(1,0,0);
  with Texture do begin
    v1:=gl2D(1,0);
    v2:=gl2d(1,1);
    v3:=gl2d(0,1);
    v4:=gl2d(0,0);
  end;

  with Vertices do begin
    v1:=gl3d(1.5, -0.75,  0);
    v2:=gl3d(1.5,  0.75,  0);
    v3:=gl3d(1.5,  0.75,  1.5);
    v4:=gl3d(1.5, -0.75,  1.5);
  end;
end;

//Back
Artwork[2]:=TglBitmap.Create(ControlSet);
with ArtWork[2] do begin
name:='Back Cover';
filename:='3.jpg';
Normal:=gl3D(0,0,-1);
  with Texture do begin
    v1:=gl2D(1,0);
    v2:=gl2d(1,1);
    v3:=gl2d(0,1);
    v4:=gl2d(0,0);
  end;

  with Vertices do begin
    v1:=gl3d(0, -0.75,  -1.5);
    v2:=gl3d(0,  0.75,  -1.5);
    v3:=gl3d(1.5,  0.75,  -1.5);
    v4:=gl3d(1.5, -0.75,  -1.5);
  end;
end;

//Left
Artwork[3]:=TglBitmap.Create(ControlSet);
with ArtWork[3] do begin
name:='Left Cover';
filename:='4.jpg';
Normal:=gl3D(-1,0,0);
  with Texture do begin
    v1:=gl2D(0,0);
    v2:=gl2d(1,0);
    v3:=gl2d(1,1);
    v4:=gl2d(0,1);
  end;

  with Vertices do begin
    v1:=gl3d(-1.5, -0.75,  -1.5);
    v2:=gl3d(-1.5, -0.75,  0);
    v3:=gl3d(-1.5,  0.75,  0);
    v4:=gl3d(-1.5,  0.75,  -1.5);
  end;
end;

Titles[0]:=tglListbox.Create(ControlSet);
with Titles[0] do begin
linespacing:=1.5;
name:='Front Listbox';
Normal:=gl3D(0,0,1);
  with Texture do begin
    v1:=gl2D(0,0);
    v2:=gl2d(1,0);
    v3:=gl2d(1,1);
    v4:=gl2d(0,1);
  end;

  with Vertices do begin
    v1:=gl3d(0, -0.75,  1.5);
    v2:=gl3d(1.5, -0.75,  1.5);
    v3:=gl3d(1.5,  0.75,  1.5);
    v4:=gl3d(0,  0.75,  1.5);
  end;
end;

Titles[1]:=tglListbox.Create(ControlSet);
with Titles[1] do begin
linespacing:=1.5;
name:='Right Listbox';
Normal:=gl3D(1,0,0);
  with Texture do begin
    v1:=gl2D(1,0);
    v2:=gl2d(1,1);
    v3:=gl2d(0,1);
    v4:=gl2d(0,0);
  end;

  with Vertices do begin
    v1:=gl3d(1.5, -0.75,  -1.5);
    v2:=gl3d(1.5,  0.75,  -1.5);
    v3:=gl3d(1.5,  0.75,  -0);
    v4:=gl3d(1.5, -0.75,  -0);
  end;
end;

Titles[2]:=tglListBox.Create(ControlSet);
with Titles[2] do begin
linespacing:=1.5;
name:='Back Listbox';
Normal:=gl3D(0,0,-1);
  with Texture do begin
    v1:=gl2D(1,0);
    v2:=gl2d(1,1);
    v3:=gl2d(0,1);
    v4:=gl2d(0,0);
  end;

  with Vertices do begin
    v1:=gl3d(-1.5, -0.75,  -1.5);
    v2:=gl3d(-1.5,  0.75,  -1.5);
    v3:=gl3d(0,  0.75,  -1.5);
    v4:=gl3d(0, -0.75,  -1.5);
  end;
end;

Titles[3]:=tglListBox.Create(ControlSet);
with Titles[3] do begin
linespacing:=1.5;
name:='Left Listbox';
Normal:=gl3D(-1,0,0);
  with Texture do begin
    v1:=gl2D(0,0);
    v2:=gl2d(1,0);
    v3:=gl2d(1,1);
    v4:=gl2d(0,1);
  end;

  with Vertices do begin
    v1:=gl3d(-1.5, -0.75,  0);
    v2:=gl3d(-1.5, -0.75,  1.5);
    v3:=gl3d(-1.5,  0.75,  1.5);
    v4:=gl3d(-1.5,  0.75,  0);
  end;
end;

 draw;
 titles[0].clear;
 titles[0].drawmode:=true;
 titles[0].realworldwidth:=round(getRealWorldPos(titles[0].Vertices.v3).x-getRealWorldPos(titles[0].Vertices.v1).x);
 titles[0].realworldheight:=round(getRealWorldPos(titles[0].Vertices.v1).y-getRealWorldPos(titles[0].Vertices.v3).y);
 titles[0].Resize;
 titles[1].clear;
 titles[1].drawmode:=true;
 titles[1].realworldwidth:=titles[0].realworldwidth;
 titles[1].realworldheight:=titles[0].realworldheight;
 titles[1].Resize;
 titles[2].clear;
 titles[2].drawmode:=true;
 titles[2].realworldwidth:=titles[0].realworldwidth;
 titles[2].realworldheight:=titles[0].realworldheight;
 titles[2].Resize;
 titles[3].clear;
 titles[3].drawmode:=true;
 titles[3].realworldwidth:=titles[0].realworldwidth;
 titles[3].realworldheight:=titles[0].realworldheight;
 titles[3].Resize;
 draw;

 setlbenabled(titles[0]);

 randomize;
 for n:=0  to round(random(160)) do titles[0].items.add('A test line'+inttostr(n));
 for n:=0  to round(random(160)) do titles[1].items.add('A test line'+inttostr(n));
 for n:=0  to round(random(160)) do titles[2].items.add('A test line'+inttostr(n));
 for n:=0  to round(random(160)) do titles[3].items.add('A test line'+inttostr(n));

end;

{$R *.DFM}
procedure setupPixelFormat(DC:HDC);
const
 pfd:TPIXELFORMATDESCRIPTOR = (
	nSize:sizeof(TPIXELFORMATDESCRIPTOR);	// size
	nVersion:1;								// version
	dwFlags:PFD_SUPPORT_OPENGL or PFD_DRAW_TO_WINDOW or
                PFD_DOUBLEBUFFER;	// support double-buffering
	iPixelType:PFD_TYPE_RGBA;		// color type
	cColorBits:24;					// prefered color depth
	cRedBits:0; cRedShift:0;		// color bits (ignored)
        cGreenBits:0;  cGreenShift:0;
        cBlueBits:0; cBlueShift:0;
        cAlphaBits:0;  cAlphaShift:0;   // no alpha buffer
        cAccumBits: 0;
        cAccumRedBits: 0;  		// no accumulation buffer,
        cAccumGreenBits: 0;     // accum bits (ignored)
        cAccumBlueBits: 0;
        cAccumAlphaBits: 0;
	cDepthBits:16;				// depth buffer
	cStencilBits:0;				// no stencil buffer
	cAuxBuffers:0;				// no auxiliary buffers
	iLayerType:PFD_MAIN_PLANE;  // main layer
    bReserved: 0;
    dwLayerMask: 0;
    dwVisibleMask: 0;
    dwDamageMask: 0;                    // no layer, visible, damage masks
    );
var pixelFormat:integer;
begin
  pixelFormat := ChoosePixelFormat(DC, @pfd);
  if (pixelFormat = 0) then
	exit;
  if (SetPixelFormat(DC, pixelFormat, @pfd) <> TRUE) then
	exit;
end;






procedure init();
begin
  glClearDepth(1.0);
  glDepthFunc(GL_LESS);
  glEnable(GL_DEPTH_TEST);
  glShadeModel(GL_SMOOTH);
  glMatrixMode(GL_PROJECTION);
  glEnable(GL_TEXTURE_2D);  //and very important, ENABLE TEXTURES!!! (new)
  glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST);
  glEnable(GL_LIGHT0);
  glEnable(GL_LIGHTING);

 glLoadIdentity;
 gluPerspective( 45.0, form1.width/form1.height,1, 10.0);
 glMatrixMode(GL_MODELVIEW);
 glLoadIdentity;

end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 DC:=GetDC(form1.Handle);        //Actually, you can use any windowed control here
{  if RC<>0 then begin
  wglMakeCurrent (0,0);
  wglDeleteContext(rc);
 end;
 f DC<>0 then DeleteDC(dc);}

// SetupPixelFormat(DC);
// RC:=wglCreateContext(DC); //makes OpenGL window out of DC
// wglMakeCurrent(DC, RC);   //makes OpenGL window active
// init;


end;

procedure TForm1.FormResize(Sender: TObject);
var
lastrotation : integer;
begin

 //if not assigned(ControlSet) then ControlSet.Free;

 SetupPixelFormat(DC);
 RC:=wglCreateContext(DC); //makes OpenGL window out of DC
 wglMakeCurrent(DC, RC);   //makes OpenGL window active
 init;

 if not assigned(ControlSet) then createControls;
 lastrotation:=currentrotation;
 currentrotation:=0;
 draw;
 titles[0].realworldwidth:=round(getRealWorldPos(titles[0].Vertices.v3).x-getRealWorldPos(titles[0].Vertices.v1).x);
 titles[0].realworldheight:=round(getRealWorldPos(titles[0].Vertices.v1).y-getRealWorldPos(titles[0].Vertices.v3).y);
 titles[1].realworldwidth:=titles[0].realworldwidth;
 titles[1].realworldheight:=titles[0].realworldheight;
 titles[2].realworldwidth:=titles[0].realworldwidth;
 titles[2].realworldheight:=titles[0].realworldheight;
 titles[3].realworldwidth:=titles[0].realworldwidth;
 titles[3].realworldheight:=titles[0].realworldheight;
 currentrotation:=lastrotation;
 ControlSet.processcontrols(EVENT_RESIZE);
 InvalidateRgn(self.handle,0,true);
end;


procedure TForm1.Draw;
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
  glloadidentity();

  glLightfv(GL_LIGHT0, GL_DIFFUSE, @light_diffuse);
  glLightfv(GL_LIGHT0, GL_POSITION, @light_position);


  glloadidentity();

  gluLookAt(0.0, 0.0, 4.75,  { eye is at (0,0,5)}
            0.0, 0.0, 0.0,      {center is at (0,0,0)}
            0.0, 1.0, 0);      {up is in positive Y direction}

  currentrotation:=currentrotation+rotation;
  if currentrotation>359 then currentrotation:=currentrotation-360;
  if currentrotation<0 then currentrotation:=360+currentrotation;
  glRotatef(currentrotation, 0.0, 1.0, 0.0);
//  caption:=inttostr(currentrotation)+' - '+inttostr(rotation)+' ';

  ControlSet.Draw;

  glPushMatrix;

  SwapBuffers(wglGetCurrentDC);
end;

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
  Y: Integer);
begin
if starttime=0 then if (controlset.processcontrols(EVENT_MOUSEMOVE)=RESULT_TRUE) then begin;rotation:=0;draw;exit;end;
endtime:=gettickcount;
endpoint:=x;
end;

procedure TForm1.FormMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
res : integer;
begin
if starttime=0 then begin
  res:=controlset.processcontrols(EVENT_MOUSEUP);
  if res=RESULT_TRUE then begin;rotation:=0;draw;exit;end;
  if res=RESULT_FALSE_BUT_NEEDS_REDRAW then begin
    rotation:=0;
    draw;
  end;

end;

starttime:=0;
//lastrotation:=rotation;
timer1.enabled:=false;
timer1timer(sender);
timer1.enabled:=true;
end;

procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
 if RC<>0 then begin
  wglMakeCurrent (0,0);
  wglDeleteContext(rc);
 end;
// if DC<>0 then DeleteDC(dc);
end;



procedure TForm1.FormMouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
if (controlset.processcontrols(EVENT_MOUSEDOWN)=RESULT_TRUE) then begin;rotation:=0;draw;exit;end;
starttime:=gettickcount;
startpoint:=x;
end;


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

procedure TForm1.SetLbEnabled(var l : tglListbox);
begin
Titles[0].enabled:=false;
Titles[1].enabled:=false;
Titles[2].enabled:=false;
Titles[3].enabled:=false;
l.enabled:=true;
end;


procedure TForm1.Timer1Timer(Sender: TObject);
var
distance : integer;
range : double;
n : integer;
begin
if starttime>0 then begin
  distance:=(endpoint - startpoint);
  //time:=endtime-starttime;
  range:=10 / width;

  if distance=0 then begin
    rotation:=0;
  //  lastrotation:=0;
    exit;
  end;


  rotation:=round(distance*range);//-lastrotation;
  //caption:=inttostr(distance)+' '+inttostr(rotation)+' '+inttostr(time)+' '+floattostr(range);

  if rotation=0 then exit;

//  lastrotation:=rotation;
  draw;
end;

if ((starttime=0) and (sender=form1)) then begin
  if (rotation>0) then begin
        if ((currentrotation>0) and (currentrotation<90)) then  begin for n:=currentrotation to 90-1 do     begin rotation:=1;draw;end;setlbenabled(Titles[3]);exit;end;
        if ((currentrotation>90) and (currentrotation<180)) then begin for n:=currentrotation to 180-1 do   begin rotation:=1;draw;end;setlbenabled(Titles[2]);exit;end;
        if ((currentrotation>180) and (currentrotation<270)) then begin for n:=currentrotation to 270-1 do  begin rotation:=1;draw;end;setlbenabled(Titles[1]);exit;end;
        if ((currentrotation>270) and (currentrotation<360)) then begin for n:=currentrotation to 360-1 do  begin rotation:=1;draw;end;setlbenabled(Titles[0]);exit;end;
      end;

  if (rotation<0) then begin
        if ((currentrotation>270) and (currentrotation<360)) then  begin for n:=currentrotation downto 270+1 do begin rotation:=-1; draw;end;setlbenabled(Titles[1]);exit;end;
        if ((currentrotation>180) and (currentrotation<270)) then begin for n:=currentrotation downto 180+1 do  begin rotation:=-1; draw;end;setlbenabled(Titles[2]);exit;end;
        if ((currentrotation>90) and (currentrotation<180)) then begin for n:=currentrotation downto 90+1 do    begin rotation:=-1; draw;end;setlbenabled(Titles[3]);exit;end;
        if ((currentrotation>0) and (currentrotation<90)) then begin for n:=currentrotation downto 0+1 do       begin rotation:=-1; draw;end;setlbenabled(Titles[0]);exit;end;
       end;
  rotation:=0;
  //lastrotation:=0;
end;

end;



end.