unit u_kugel;

{$mode objfpc}{$H+}

// http://nehe.gamedev.net/

interface

uses
  Classes, SysUtils, FileUtil, LResources, Forms, Controls, Graphics, Dialogs,
  ExtCtrls, OpenGLContext,
  //dglOpenGL;

{ statt der dglOpenGL können auch die units GL, GLU eingebunden werden
  Bei Verwednung der dglOpenGL unit muss beim Form.Create eine saubere
  Initialisierung erfolgen }
   GL, GLU;

type

  { TForm1 }

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure FormPaint(Sender: TObject);
    procedure OnAppIdle(Sender: TObject; var Done: Boolean);
    procedure OpenGLControl1Resize(Sender: TObject);
  private
    OpenGLControl1: TOpenGLControl;
  public

  end;

var
  Form1: TForm1;
  rtri: GLFloat = 0.0;
  rquad: GLFloat = 0.0;

implementation

{ TForm1 }


procedure TForm1.FormCreate(Sender: TObject);

begin
  OpenGLControl1:=TOpenGLControl.Create(Self);
  with OpenGLControl1 do begin
    Name:='OpenGLControl1';
    Align:=alClient;
    Parent:=Self;
    OnPaint:=@FormPaint;
    OnResize:=@OpenGLControl1Resize;
    AutoResizeViewport:=true;
  end;

  { Beginn: Initialisierung von OpenGL bei Verwendung der dglOpenGL}
  //InitOpenGL;
  OpenGLControl1.MakeCurrent;
  //ReadExtensions;
  //ReadImplementationProperties;
  { End: Initialisierung von OpenGL bei Verwendung der dglOpenGL}

  { Beginn: Initialisierung von OpenGL gemäß Neon Helium}
  glShadeModel(GL_SMOOTH);             // Enables Shmooth Shading
  glClearColor(0.0, 0.0, 0.0, 0.5);    // Black Background
  glClearDepth(1.0);                   // Depth Buffer Setup
  glEnable(GL_DEPTH_TEST);             // Enables Despth Testing
  glDepthFunc(GL_LEQUAL);              // The Type of Depth Test to Do
  glHint(GL_PERSPECTIVE_CORRECTION_HINT,GL_NICEST); // Really Nice Perspective Calculations
  { End: Initialisierung von OpenGL gemäß Neon Helium}

  { Aufruf des IdleHandlers für das kontinuierliche Durchlaufen der .FormPaint }
  Application.AddOnIdleHandler(@OnAppIdle);

end;

procedure TForm1.FormDestroy(Sender: TObject);
begin

end;

procedure TForm1.OnAppIdle(Sender: TObject; var Done: Boolean);
begin
  Done:=false;
  //DebugLn(['TForm1.OnAppIdle ']);
  OpenGLControl1.Invalidate;
end;

procedure TForm1.FormPaint(Sender: TObject);

var
  quadric : PGLuquadric;

begin
    // Screen- und Tiefenbuffer bereinigen
  glClear( GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT );   // Clear the Screen and the Depth

  // Width  & Height existieren bereits in der Unit CONTROLS

  glViewport(0, 0, Width, Height);                        // Resetuje aktuální nastavení
  glMatrixMode(GL_PROJECTION);                            // Zvolí projekèní matici
  glLoadIdentity();                                       // Reset matice
  gluPerspective(45.0,Width/Height,0.1,100.0);            // Výpoèet perspektivy
  glMatrixMode(GL_MODELVIEW);                             // Zvolí matici Modelview
  glLoadIdentity;                                          // Reset the Current ModelView Matrix

  glEnable(gl_normalize);

	// Drehung
	glRotatef(45, 1, 0, 0);
	glRotatef(60, 0, 1, 0);


	//glPushMatrix();         // Startsystem wird gespeichert
	glLineWidth(1);
        glColor3b(1,0,0);
	quadric:=gluNewQuadric();
        gltranslated(30.0,0.0,0.0);
        gluQuadricOrientation(quadric,glu_outside);
	gluQuadricTexture(quadric,gl_false);
	gluQuadricDrawStyle(quadric, GLU_FILL);
        glShademodel(gl_flat);
        gluQuadricTexture(quadric, gl_false);
	gluSphere(quadric, 30, 40, 40);
	gluDeleteQuadric(quadric);

	//glPopMatrix();
        OpenGLControl1.SwapBuffers;
end;


procedure TForm1.OpenGLControl1Resize(Sender: TObject);
begin
  if OpenGLControl1.Height <= 0 then exit;
  // the viewport is automatically resized by the TOpenGLControl
  // you can disable it (OpenGLControl1.AutoResizeViewport:=false)
  // and do something yourself here
end;

initialization

  {$I u_kugel.lrs}

end.
