Forum > OpenGL
Trying to use GLScene
AussieUser:
Can someone explain why this isn't working. I just get a black screen, I want to see a sphere. Any help appreciated.
unit Unit1;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, GLScene, GLViewer,
GLVectorGeometry, GLVectorTypes, GLObjects;
type
{ TForm1 }
TForm1 = class(TForm)
private
Scene: TGLScene;
Viewer: TGLSceneViewer;
Camera: TGLCamera;
Sphere: TGLSphere;
Light: TGLLightSource;
public
constructor Create(AOwner: TComponent); override;
end;
var
Form1: TForm1;
implementation
{$R *.lfm}
{ TForm1 }
constructor TForm1.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
// Create the scene
Scene := TGLScene.Create(Self);
// Create the camera and set its position
Camera := TGLCamera.Create(Scene);
Camera.Position.SetPoint(0, 0, 10); // Set the camera 10 units away from the scene
// Camera.PointTo(TVector(0,0,0),TVector(0,1,0));
// Add a light source to the scene
Light := TGLLightSource.Create(Scene);
Light.Position.SetPoint(5, 5, 5); // Place the light source at (5, 5, 5)
// Create a sphere and place it at the center of the scene
Sphere := TGLSphere.Create(Scene);
Sphere.Radius := 10.0; // Set the radius of the sphere to 1 unit
Sphere.Position.SetPoint(0, 0, 0); // Set the position of the sphere
// Set up the GLSceneViewer
Viewer := TGLSceneViewer.Create(Scene);
Viewer.Parent := Form1; // Attach the viewer to the form
Viewer.Align := alClient; // Set it to fill the form
Viewer.Camera := Camera; // Assign the camera to the viewer
// Start rendering the scene
Viewer.Invalidate;
end;
end.
TRon:
Please use codetags
The first thing I noticed is that sceneviewer's owner should be the form. The owner of these components that you create should be the form.
And since you seem to be doing everything manually. Also take care of setting the parents.
AussieUser:
Thanks. I'm very new to GLScene. I would appreciate a short program that does things properly to save me mucking about too much.
TRon:
You are aware that the package comes with a bunch of examples (i counted over 30) ?
You seem to be creating these components at runtime but what you should be doing is use the visual designer.
edit: just load one of the examples and you can see for yourself what that entails.
AussieUser:
All the examples I saw were based on Delphi, even those in the Lazarus folder. If you could point me to a Lazarus one that would be good.
Navigation
[0] Message Index
[#] Next page
[*] Previous page