Delphi-PRAXiS
Seite 3 von 5     123 45      

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Sonstige Fragen zu Delphi (https://www.delphipraxis.net/19-sonstige-fragen-zu-delphi/)
-   -   Delphi Dreidimensionales Array darstellen. (https://www.delphipraxis.net/153661-dreidimensionales-array-darstellen.html)

Muellermilchtrinker 19. Aug 2010 19:04

AW: Dreidimensionales Array darstellen.
 
Zum ändern/erstellen der Muster hab ich ja die Ebenen, die ich dann durchschalten kann.
Ich möchte aber noch eine 3D Ansicht machen, damit man das Muster auch "real" sieht.

Jens01 20. Aug 2010 10:34

AW: Dreidimensionales Array darstellen.
 
Okay, bis dahin.
Meld Dich auch bei DGL an, die Leute sind richtig fit bei OpenGL.

Und sag, was ist Urlaub!?:shock:

Muellermilchtrinker 24. Aug 2010 11:32

AW: Dreidimensionales Array darstellen.
 
Zitat:

Zitat von Jens01 (Beitrag 1043679)
Und sag, was ist Urlaub!?:shock:

Weißt du das nicht??? Man geht da in ein anderes Land/Gebiet/Stadt und entspannt sich dort. :-D

Aber back to topic:

PROBLEM GELÖST
Ich hab das Problem gelöst (ohne gluOrtho2D :shock:). Mir wurde auf DGL geraten diese Zeile:
Delphi-Quellcode:
gluPerspective(45.0, ClientWidth/ClientHeight, NearClipping, FarClipping);
durch diese Zeile zu ersetzen:
Delphi-Quellcode:
gluPerspective(45.0, panel1.ClientWidth/panel1.ClientHeight, NearClipping, FarClipping);
Und es hat geklappt. Begründung: mit meinem vorherigen Code hab ich alles auf die Formulargröße gesetzt. MIt diesem Code mach ich das auf mein Panel und das ist ja so schön quadratisch. :-D Und jetzt ist mein Würfel ein Würfel. :stupid:

Weiter gehts:

Ich hab mir jetzt diese Prozedur geschrieben:
Delphi-Quellcode:
procedure TForm1.Cube(Color:Integer);
begin
    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0, 1.0,-1.0);         // Top Right Of The Quad (Top)
      glVertex3f(-1.0, 1.0,-1.0);         // Top Left Of The Quad (Top)
      glVertex3f(-1.0, 1.0, 1.0);         // Bottom Left Of The Quad (Top)
      glVertex3f( 1.0, 1.0, 1.0);         // Bottom Right Of The Quad (Top)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0,-1.0, 1.0);         // Top Right Of The Quad (Bottom)
      glVertex3f(-1.0,-1.0, 1.0);         // Top Left Of The Quad (Bottom)
      glVertex3f(-1.0,-1.0,-1.0);         // Bottom Left Of The Quad (Bottom)
      glVertex3f( 1.0,-1.0,-1.0);         // Bottom Right Of The Quad (Bottom)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0, 1.0, 1.0);         // Top Right Of The Quad (Front)
      glVertex3f(-1.0, 1.0, 1.0);         // Top Left Of The Quad (Front)
      glVertex3f(-1.0,-1.0, 1.0);         // Bottom Left Of The Quad (Front)
      glVertex3f( 1.0,-1.0, 1.0);         // Bottom Right Of The Quad (Front)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0,-1.0,-1.0);         // Bottom Left Of The Quad (Back)
      glVertex3f(-1.0,-1.0,-1.0);         // Bottom Right Of The Quad (Back)
      glVertex3f(-1.0, 1.0,-1.0);         // Top Right Of The Quad (Back)
      glVertex3f( 1.0, 1.0,-1.0);         // Top Left Of The Quad (Back)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f(-1.0, 1.0, 1.0);         // Top Right Of The Quad (Left)
      glVertex3f(-1.0, 1.0,-1.0);         // Top Left Of The Quad (Left)
      glVertex3f(-1.0,-1.0,-1.0);         // Bottom Left Of The Quad (Left)
      glVertex3f(-1.0,-1.0, 1.0);         // Bottom Right Of The Quad (Left)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0, 1.0,-1.0);         // Top Right Of The Quad (Right)
      glVertex3f( 1.0, 1.0, 1.0);         // Top Left Of The Quad (Right)
      glVertex3f( 1.0,-1.0, 1.0);         // Bottom Left Of The Quad (Right)
      glVertex3f( 1.0,-1.0,-1.0);         // Bottom Right Of The Quad (Right)
end;
Damit mache ich mein Cube und kann die Farbe beeinflussen. aber das ist ja nur ein Cube. Ich brauch aber mehrere (genau: 125). Wie schaffe ich die Verschiebung (je nach Koordinate)??? Zwischen den Cubes sollte auch ein bisschen Platz sein.

blackfin 24. Aug 2010 11:44

AW: Dreidimensionales Array darstellen.
 
glTranslate()

und

Tutorial Matrix 2

Muellermilchtrinker 24. Aug 2010 11:57

AW: Dreidimensionales Array darstellen.
 
Delphi-Quellcode:
    for x := 1 to 5 do
    begin
      for y := 1 to 5 do
      begin
        for z := 1 to 5 do
        begin
          glTranslatef(x/10,y/10,z/10);
          glBegin(GL_QUADS);
            Cube(0);
          glEnd;
        end;
      end;
    end;
Gibt leider nicht das Ergebnis. Ich glaub ich hab irgendwo in der Routine ein Problem.

blackfin 24. Aug 2010 12:00

AW: Dreidimensionales Array darstellen.
 
Kurz nur draufgesehen, mögliches Problem:

Du verschiebst in deiner Routine ja immer weiter, ohne dass du den Zeichenstift zurücksetzt :)
OpenGL ist eine State-Machine, das heisst, wenn du verschiebst, verschiebt er immer relativ!

Also: Ursprung vor jedem Cube setzen:
Delphi-Quellcode:
 for x := 1 to 5 do
    begin
      for y := 1 to 5 do
      begin
        for z := 1 to 5 do
        begin
          glMatrixMode(GL_MODELVIEW) ; // optional, falls ModelView-Matrix nicht aktiv
          glLoadIdentity() ; // <---- möööp :D
          glTranslatef(x/10,y/10,z/10);
          glBegin(GL_QUADS);
            Cube(0);
          glEnd;
        end;
      end;
    end;

Jens01 24. Aug 2010 12:18

AW: Dreidimensionales Array darstellen.
 
Ich persönlich würde hier eine bzw mehrere Schleifen bevorzugen.
ganz grob z.B.:
Delphi-Quellcode:
procedure WholeCube;
var
  i: Integer;
  i2: Integer;
  i3: Integer;
  x, y,z : Double;
begin
  Ebene := 5;
  Reihe := 5;
  Spalte := 5;

  z := 0;
  for i := 0 to Ebene - 1 do
  begin
    y := 0;
    for i2 := 0 to Reihe - 1 do
    begin
      x := 0;
      for i3 := 0 to Spalte - 1 do
      begin
        Cube(x, y, z);
        x := x + Abstand
      end;
      y := Y + Abstand;
    end;
    z := z + Abstand;
  end;
end;
Delphi-Quellcode:
procedure Cube(x,y,z: Double);
begin
  glVertex3f( 1.0 + x, 1.0 + Y,-1.0 + z);        // Top Right Of The Quad (Top)
  glVertex3f(-1.0 + x, 1.0 + Y,-1.0 + z);        // Top Left Of The Quad (Top)
  glVertex3f(-1.0 + x, 1.0 + Y, 1.0 + z);        // Bottom Left Of The Quad (Top)
  glVertex3f( 1.0 + x, 1.0 + Y, 1.0 + z);        // Bottom Right Of The Quad (Top)
  ...
end;
Also das ist jetzt eine ganz grobe Skizze (das funktioniert so 1:1 noch nicht). Für den Würfel selbst sollte man vielleicht auch eine Schleife nutzen.

Jetzt hast Du 2 Möglichkeiten!

blackfin 24. Aug 2010 12:27

AW: Dreidimensionales Array darstellen.
 
Wenn schon optimieren: Ich würde das Zeichnen eines einzelnen Cubes in eine Display-Liste speichern, die man nur noch aufrufen muss. (Stichwörter: GlNewList und GLEndList).
Das ist wesentlich schneller als dauernd glbegin / glvertex / glend.

Aber das ist erst dann angesagt, wenn die "normale" routine funzt :D

Muellermilchtrinker 24. Aug 2010 13:08

AW: Dreidimensionales Array darstellen.
 
Liste der Anhänge anzeigen (Anzahl: 1)
Also mein aktueller Code sieht so aus:
Delphi-Quellcode:
unit Unit1;

interface

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

Type
  TVector2f = Array [0..1] of Single;
  TVector3f = Array [0..2] of Single;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Timer1: TTimer;
    Timer2: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
    procedure Timer1Timer(Sender: TObject);
    procedure Timer2Timer(Sender: TObject);
  private
    { Private-Deklarationen }
    FrameCount : Cardinal; //FrameCounter
    procedure SetupGL;
    procedure Render;
    procedure ErrorHandler;
    procedure Cube(x,y,z:Double;Color:Integer);
  public
    { Public-Deklarationen }
    DC                               : HDC; //Handle auf Zeichenfläche
    RC                               : HGLRC;//Rendering Context
  end;

var
  Form1: TForm1;

const
  NearClipping = 1;
  FarClipping = 1000;

var
  RotateX : single;                // Rotation um die X-Achse
  RotateY : single;                // Rotation um die Y-Achse
  RotateZ : single;                // Rotation um die Z-Achse
  PosVect : TVector3f;        // Vector mit der Position des Objektes.

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  DC:= GetDC(Panel1.Handle);
  if not InitOpenGL then Application.Terminate;
  RC:= CreateRenderingContext( DC,
                               [opDoubleBuffered],
                               32,
                               24,
                               0,0,0,
                               0);
  ActivateRenderingContext(DC, RC);
  SetupGL;
end;

procedure TForm1.SetupGL;
begin
  glClearColor(0.0, 0.0, 0.0, 0.0); //Hintergrundfarbe:
  glEnable(GL_DEPTH_TEST);         //Tiefentest aktivieren
  glEnable(GL_CULL_FACE);          //Backface Culling aktivieren

  RotateX := 0;
  RotateY := 0;
  RotateZ := 0;
  PosVect[0] := 0;
  PosVect[1] := 0;
  PosVect[2] := -5;
end;

procedure TForm1.Cube(x,y,z:Double;Color:Integer);
begin
    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0+x, 1.0+y,-1.0+z);         // Top Right Of The Quad (Top)
      glVertex3f(-1.0+x, 1.0+y,-1.0+z);         // Top Left Of The Quad (Top)
      glVertex3f(-1.0+x, 1.0+y, 1.0+z);         // Bottom Left Of The Quad (Top)
      glVertex3f( 1.0+x, 1.0+y, 1.0+z);         // Bottom Right Of The Quad (Top)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0+x,-1.0+y, 1.0+z);         // Top Right Of The Quad (Bottom)
      glVertex3f(-1.0+x,-1.0+y, 1.0+z);         // Top Left Of The Quad (Bottom)
      glVertex3f(-1.0+x,-1.0+y,-1.0+z);         // Bottom Left Of The Quad (Bottom)
      glVertex3f( 1.0+x,-1.0+y,-1.0+z);         // Bottom Right Of The Quad (Bottom)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0+x, 1.0+y, 1.0+z);         // Top Right Of The Quad (Front)
      glVertex3f(-1.0+x, 1.0+y, 1.0+z);         // Top Left Of The Quad (Front)
      glVertex3f(-1.0+x,-1.0+y, 1.0+z);         // Bottom Left Of The Quad (Front)
      glVertex3f( 1.0+x,-1.0+y, 1.0+z);         // Bottom Right Of The Quad (Front)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0+x,-1.0+y,-1.0+z);         // Bottom Left Of The Quad (Back)
      glVertex3f(-1.0+x,-1.0+y,-1.0+z);         // Bottom Right Of The Quad (Back)
      glVertex3f(-1.0+x, 1.0+y,-1.0+z);         // Top Right Of The Quad (Back)
      glVertex3f( 1.0+x, 1.0+y,-1.0+z);         // Top Left Of The Quad (Back)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f(-1.0+x, 1.0+y, 1.0+z);         // Top Right Of The Quad (Left)
      glVertex3f(-1.0+x, 1.0+y,-1.0+z);         // Top Left Of The Quad (Left)
      glVertex3f(-1.0+x,-1.0+y,-1.0+z);         // Bottom Left Of The Quad (Left)
      glVertex3f(-1.0+x,-1.0+y, 1.0+z);         // Bottom Right Of The Quad (Left)

    if Color <> 0 then glColor4f(0.5, 0.5, 1.0, 0.8) // Set The Color
    else glColor4f(0.3, 0.3, 0.3, 0.4);
      glVertex3f( 1.0+x, 1.0+y,-1.0+z);         // Top Right Of The Quad (Right)
      glVertex3f( 1.0+x, 1.0+y, 1.0+z);         // Top Left Of The Quad (Right)
      glVertex3f( 1.0+x,-1.0+y, 1.0+z);         // Bottom Left Of The Quad (Right)
      glVertex3f( 1.0+x,-1.0+y,-1.0+z);         // Bottom Right Of The Quad (Right)
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  DeactivateRenderingContext;
  DestroyRenderingContext(RC);
  ReleaseDC(Handle, DC);
end;

procedure TForm1.Render;
Const voffset : TVector3f = (0.0, 0.0, 0.0);
      toffset : TVector2f = (0.0, 0.0);
      tscale : TVector2f = (1.0, 1.0);
var x,y,z:Integer;
begin
  glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);

  glMatrixMode(GL_PROJECTION);
  glLoadIdentity;
  gluPerspective(150.0, Panel1.ClientWidth/Panel1.ClientHeight, NearClipping, FarClipping);
  glMatrixMode(GL_MODELVIEW);
  glLoadIdentity;

  glTranslatef(0, 0, 0);

  // Das Objekt auf seine aktuelle Position setzen.
  glTranslatef( PosVect[0], PosVect[1], PosVect[2]);

  { Das Objekt noch drehen, damit denn wir haben bis jetzt ja nur den }
  { Vector gedreht.                                                  }
  glRotatef(RotateX, 1, 0, 0);
  glRotatef(RotateY, 0, 1, 0);
  glRotatef(RotateZ, 0, 0, 1);

  glBegin(GL_QUADS);
    for x := 0 to 4 do
      for y := 0 to 4 do
        for z := 0 to 4 do
          Cube(x*3,y*3,z*3,0);
  glEnd;

  SwapBuffers(DC);
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  inc(FrameCount);
  Render;
  If FrameCount = 20 then
  begin
    ErrorHandler;
    FrameCount := 0;
  end;
end;

procedure TForm1.Timer2Timer(Sender: TObject);
begin
  RotateY := RotateY + 0.5;
//  if RotateY <= 0 then RotateY := RotateY + 360;
//  RotateX := RotateX + 0.5;
//  if RotateX <= 0 then RotateX := RotateX + 360;
//  RotateZ := RotateY + 0.5;
end;

procedure TForm1.ErrorHandler;
begin
  Form1.Caption := string(gluErrorString(glGetError));
end;

end.
Rauskommt nun das: siehe Video in der Zip Datei: Anhang 31950 PS: Das ruckeln kommt von der Aufnahme. In Wirklichkeit ist es schön flüssig. Leider sieht es noch nicht so aus, wie in diesem Beispiel: http://www.delphipraxis.net/1041654-post6.html

Farben sind die gleichen. Hab von turboPASCAL schon den Code bekommen. Blick aber nicht ganz so durch und ich will es ja selber machen. Farben sind die gleichen wie in dem Programm von turboPASCAL

Jens01 24. Aug 2010 14:02

AW: Dreidimensionales Array darstellen.
 
von der Bewegung her, solltest Du Dich mit dem Kamera-Thema auseinandersetzen.
http://wiki.delphigl.com/index.php/Kamera

Der Autor hat ein Programmteil veröffentlicht, welches (bis auf Kleinigkeiten) gut läuft. Es läßt sich auch in Dein Programm einfacher einbinden, als man am Anfang vermuten würde. Ggf kannst Du den Autor auch einfach ansprechen.

Bei Deinem Problem mit den Farben mußt Du konkreter werden.


Alle Zeitangaben in WEZ +1. Es ist jetzt 21:09 Uhr.
Seite 3 von 5     123 45      

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz