📄 cgscene.pas
字号:
unit CgScene;
{ CgLib: Scene global stuff.
Version 1.00
(c) 1998-2000 Tom Nuydens. Use at your own risk. See cglib.txt for details. }
interface
uses
Windows, GL, GLu, CgTypes, CgObject, CgLight, CgMaterials, CgTexture, CgUtils,
CgGeometry, DArrays;
type
TCGFog = class(TObject)
private
FMode: GLenum;
FDensity: Single;
FStart: Single;
FEnd: Single;
FColor: TCGColorF;
procedure SetMode(m: GLenum);
procedure SetDensity(d: Single);
procedure SetStart(s: Single);
procedure SetEnd(e: Single);
procedure SetColor(c: TCGColorF);
public
constructor Create;
procedure Enable;
procedure Disable;
property Mode: GLenum read FMode write SetMode;
property Density: Single read FDensity write SetDensity;
property FogStart: Single read FStart write SetStart;
property FogEnd: Single read FEnd write SetEnd;
property Color: TCGColorF read FColor write SetColor;
end;
TCGLightModel = class(TObject)
private
FAmbient: TCGColorF;
FLocalViewer: Boolean;
FTwoSided: Boolean;
procedure SetAmbient(c: TCGColorF);
procedure SetLocalViewer(l: Boolean);
procedure SetTwoSided(t: Boolean);
public
property Ambient: TCGColorF read FAmbient write SetAmbient;
property LocalViewer: Boolean read FLocalViewer write SetLocalViewer;
property TwoSided: Boolean read FTwoSided write SetTwoSided;
end;
TCGCamera = record
Pos, Target, Up: TCGVector;
end;
TCGViewingVolume = record
FOV, Aspect, zNear, zFar: Single;
end;
TCGObjectArray = class(TDArray)
public
constructor Create; override;
function GetObject(i: Integer): TCGObject;
procedure SetObject(i: Integer; o: TCGObject);
function CopyObject(i: Integer): TCGObject;
property Items[i: Integer]: TCGObject read GetObject write SetObject; default;
end;
TCGScene = class(TObject)
public
Name: String[32];
Objects: TCGObjectArray;
Lights: array [0..7] of TCGLight;
Materials: TCGMaterialLib;
Textures: TCGTextureLib;
Fog: TCGFog;
LightModel: TCGLightModel;
Camera: TCGCamera;
ViewingVolume: TCGViewingVolume;
constructor Create;
destructor Destroy; override;
function NewObject: Integer;
procedure Render;
procedure SaveToFile(filename: String);
procedure LoadFromFile(filename: String);
end;
// File support types:
// Scene file header
TCGSceneHeader = record
Version: TCGVersion; // Major/minor version.
Name: String[32]; // Name of this scene.
Filler: array [0..92] of Byte;
end;
// Scene file is chunk based -> chunk header = chunk ID.
TCGSChunkHeader = Integer;
// Chunk for fog settings
TCGSFogChunk = record
Mode: GLenum;
Density: Single;
FogStart: Single;
FogEnd: Single;
Color: TCGColorF;
Enabled: Boolean;
end;
// Chunk for lightmodel settings
TCGSLightModelChunk = record
Ambient: TCGColorF;
LocalViewer: Boolean;
TwoSided: Boolean;
end;
// Chunk for a single light's data.
TCGSLightChunk = record
Index: GLenum;
Ambient: TCGColorF;
Diffuse: TCGColorF;
Specular: TCGColorF;
Position: TCGVector;
SpotDirection: TCGVector;
SpotExponent: Single;
SpotCutoff: Single;
ConstAtt: Single;
LinearAtt: Single;
QuadraticAtt: Single;
Infinite: Boolean;
Enabled: Boolean;
end;
procedure cgSetCamera(cam: TCGCamera);
procedure cgSetPerspective(vol: TCGViewingVolume);
implementation
uses
Classes, SysUtils;
{******************************************************************************}
{ PROCEDURES AND FUNCTIONS }
{******************************************************************************}
procedure cgSetCamera(cam: TCGCamera);
begin
// Set the OpenGL viewpoint using gluLookAt().
with cam do gluLookAt(Pos.x, Pos.y, Pos.z,
Target.x, Target.y, Target.z,
Up.x, Up.y, Up.z);
end;
procedure cgSetPerspective(vol: TCGViewingVolume);
begin
// Set the OpenGL viewing volume.
with vol do gluPerspective(FOV, Aspect, zNear, zFar);
end;
{******************************************************************************}
{ TCGFOG }
{******************************************************************************}
constructor TCGFog.Create;
begin
// Create TCGFog with OpenGL's default parameters.
inherited Create;
Mode := GL_EXP;
Density := 1;
FogStart := 0;
FogEnd := 1;
// I'm not really sure if this is the default fog color?
Color := cgColorF(1, 1, 1, 1);
end;
procedure TCGFog.Enable;
begin
// Enable fogging.
glEnable(GL_FOG);
end;
procedure TCGFog.Disable;
begin
// Disable fogging.
glDisable(GL_FOG);
end;
procedure TCGFog.SetMode(m: GLenum);
begin
// Change fog mode.
FMode := m;
glFogi(GL_FOG_MODE, m);
end;
procedure TCGFog.SetDensity(d: Single);
begin
// Change fog density.
FDensity := d;
glFogf(GL_FOG_DENSITY, d);
end;
procedure TCGFog.SetStart(s: Single);
begin
// Change distance to fog start.
FStart := s;
glFogf(GL_FOG_START, s);
end;
procedure TCGFog.SetEnd(e: Single);
begin
// Change distance to fog end.
FEnd := e;
glFogf(GL_FOG_END, e);
end;
procedure TCGFog.SetColor(c: TCGColorF);
begin
// Change fog color.
FColor := c;
glFogfv(GL_FOG_COLOR, @FColor);
end;
{******************************************************************************}
{ TCGLIGHTMODEL }
{******************************************************************************}
procedure TCGLightModel.SetAmbient(c: TCGColorF);
begin
// Set scene's global ambient light.
FAmbient := c;
glLightModelfv(GL_LIGHT_MODEL_AMBIENT, @FAmbient);
end;
procedure TCGLightModel.SetLocalViewer(l: Boolean);
begin
// Toggle use of a local or infinite viewpoint.
FLocalViewer := l;
glLightModeli(GL_LIGHT_MODEL_LOCAL_VIEWER, Integer(l));
end;
procedure TCGLightModel.SetTwoSided(t: Boolean);
begin
// Toggle two-sided lighting calculations.
FTwoSided := t;
glLightModeli(GL_LIGHT_MODEL_TWO_SIDE, Integer(t));
end;
{******************************************************************************}
{ TCGOBJECTARRAY }
{******************************************************************************}
constructor TCGObjectArray.Create;
begin
inherited Create;
FItemSize := SizeOf(TCGObject);
end;
function TCGObjectArray.GetObject(i: Integer): TCGObject;
begin
GetItem(i, Result);
end;
procedure TCGObjectArray.SetObject(i: Integer; o: TCGObject);
begin
SetItem(i, o);
end;
function TCGObjectArray.CopyObject(i: Integer): TCGObject;
begin
{ Return a copy of one of the objects. Retreiving one of the objects directly
returns the pointer to the object, which can be a problem in some situations. }
Result := TCGObject.Create;
with Result do
begin
Name := Items[i].Name;
Origin := Items[i].Origin;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -