uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Menus, ComCtrls, StdCtrls, AppEvnts, ExtCtrls,
OpenGL;
type
TFKyte =
class(TForm)
MainMenu1: TMainMenu;
StatusBar: TStatusBar;
Datei1: TMenuItem;
Bearbeiten1: TMenuItem;
procedure FormResize(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure IdleHandler(Sender: TObject;
var Done: Boolean);
procedure FormMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);
procedure FormMouseWheelUp(Sender: TObject; Shift: TShiftState;
MousePos: TPoint;
var Handled: Boolean);
procedure FormMouseWheelDown(Sender: TObject; Shift: TShiftState;
MousePos: TPoint;
var Handled: Boolean);
procedure FormKeyPress(Sender: TObject;
var Key: Char);
private
StartTime, TimeCount, FrameCount : Cardinal;
//FrameCounter
Frames, DrawTime : Cardinal;
//& Timebased Movement
myDC : HDC;
myRC : HGLRC;
myPalette : HPALETTE;
procedure SetupPixelFormat;
procedure ErrorHandler;
public
{ Public-Deklarationen }
end;
var
FKyte: TFKyte;
scale : single = 1;
//Für die Skalierung
rotation : integer = 0;
//Zahl in °, um die gedreht wird
XMove, YMove, ZMove: Single;
//Größe der Bewegung
Verschieben, Drehen, Tiefe : Boolean;
XAnfangVerschiebung, YAnfangVerschiebung, ZAnfangVerschiebung,
Klick : Integer;
TVector2f :
Array [0..1]
of Single;
TVector3f :
Array [0..2]
of Single;
TVector4i :
Array [0..3]
of Integer;
implementation
{$R *.dfm}
Function VectorAdd(
const vector: TVector3f; Value: Single): TVector3f;
Begin
Result[0] := vector[0] + Value;
Result[1] := vector[1] + Value;
Result[2] := vector[2] + Value;
End;
Function VectorMult(
const vector: TVector3f; Value: Single): TVector3f;
Begin
Result[0] := vector[0] * Value;
Result[1] := vector[1] * Value;
Result[2] := vector[2] * Value;
End;
Function ToVector2f(x, y: Single): TVector2f;
Begin
Result[0] := x;
Result[1] := y;
End;
Function ToVector3f(x, y, z: Single): TVector3f;
Begin
Result[0] := x;
Result[1] := y;
Result[2] := z;
End;
Function ToVector4i(x, y, z, w: Integer): TVector4i;
Begin
Result[0] := x;
Result[1] := y;
Result[2] := z;
Result[3] := w;
End;
Procedure DrawSphereObject(radius: Single; n: word;
typ: byte; inverted: boolean;
voffset: TVector3f; toffset, tscale: TVector2f);
Var alpha : TVector2f;
// i*beta around x axis
SinAlpha: TVector2f;
// sin(alpha)
CosAlpha: TVector2f;
// cos(alpha)
beta : Single;
// DegToRad(360° / n)
delta : Single;
// j*beta around z axis
SinDelta: Single;
// sin(delta)
CosDelta: Single;
// cos(delta)
h, q : word;
// half, quarter
v : TVector3f;
// vector for normal and vertex
border : TVector4i;
// for-loop borders
i, j : Integer;
k : Byte;
Begin
radius := abs(radius);
If (n < 4)
Or (Odd(n))
Or (radius = 0)
Then Exit;
h := n
div 2;
q := n
div 4;
If (Odd(h))
Then inc(q);
Case typ
of
0: border := ToVector4i( -h, h-1, -q, q);
// sphere
1: border := ToVector4i( -h, h-1, -q, 0);
// hemisphere x-
2: border := ToVector4i( -h, h-1, 0, q);
// hemisphere x+
3: border := ToVector4i(-h-q, -h+q-1, -q, q);
// hemisphere y-
4: border := ToVector4i( -q, q-1, -q, q);
// hemisphere y+
5: border := ToVector4i( -h, -1, -q, q);
// hemisphere z-
6: border := ToVector4i( 0, h-1, -q, q);
// hemisphere z+
else Exit;
End;
beta := 2*PI / n;
For i:=border[0]
to border[1]
do Begin
alpha := ToVector2f(i*beta, (i+1)*beta);
SinAlpha := ToVector2f(sin(alpha[0]), sin(alpha[1]));
CosAlpha := ToVector2f(cos(alpha[0]), cos(alpha[1]));
glBegin(GL_TRIANGLE_STRIP);
For j:=border[2]
to border[3]
do Begin
delta := j*beta;
SinDelta := sin(delta);
CosDelta := cos(delta);
If (inverted)
Then
For k:=1
downto 0
do Begin
v := ToVector3f(-SinDelta, -CosAlpha[k]*CosDelta, -SinAlpha[k]*CosDelta);
glNormal3fv(@v);
v := VectorAdd(VectorMult(v, -radius), voffset);
glTexCoord2f((j/n + 0.25)*tscale[0] + toffset[0], ((i+k)/n)*tscale[1] + toffset[1]);
glvertex3fv(@v);
End;
If (
not inverted)
Then
For k:=0
to 1
do Begin
v := ToVector3f(SinDelta, CosAlpha[k]*CosDelta, SinAlpha[k]*CosDelta);
glNormal3fv(@v);
v := VectorAdd(VectorMult(v, radius), voffset);
glTexCoord2f((j/n + 0.25)*tscale[0] + toffset[0], ((i+k)/n)*tscale[1] + toffset[1]);
glvertex3fv(@v);
End
End;
glEnd;
End;
End;