4 Prefix: glc - OpenGL Core
5 Beschreibung: diese Unit enthält eine Klassen-Kapselung der OpenGL Licht- und Material-Objekte }
12 Classes, SysUtils, {$IFNDEF OPENGL_ES}dglOpenGl{$ELSE}dglOpenGLES{$ENDIF}, ugluVector, uglcTypes;
15 TglcMaterialRec = packed record
16 Ambient: TgluVector4f;
17 Diffuse: TgluVector4f;
18 Specular: TgluVector4f;
19 Emission: TgluVector4f;
22 PglcMaterialRec = ^TglcMaterialRec;
28 TglcLightRec = packed record
29 Ambient: TgluVector4f;
30 Diffuse: TgluVector4f;
31 Specular: TgluVector4f;
32 Position: TgluVector4f;
33 SpotDirection: TgluVector3f;
34 SpotExponent: GLfloat;
38 QuadraticAtt: GLfloat;
40 PglcLightRec = ^TglcLightRec;
43 MAT_DEFAULT_AMBIENT: TgluVector4f = (0.2, 0.2, 0.2, 1.0);
44 MAT_DEFAULT_DIFFUSE: TgluVector4f = (0.8, 0.8, 0.8, 1.0);
45 MAT_DEFAULT_SPECULAR: TgluVector4f = (0.5, 0.5, 0.5, 1.0);
46 MAT_DEFAULT_EMISSION: TgluVector4f = (0.0, 0.0, 0.0, 1.0);
47 MAT_DEFAULT_SHININESS: GLfloat = 50.0;
49 LIGHT_DEFAULT_AMBIENT: TgluVector4f = (0.4, 0.4, 0.4, 1.0);
50 LIGHT_DEFAULT_DIFFUSE: TgluVector4f = (0.7, 0.7, 0.7, 1.0);
51 LIGHT_DEFAULT_SPECULAR: TgluVector4f = (0.9, 0.9, 0.9, 1.0);
52 LIGHT_DEFAULT_POSITION: TgluVector4f = (0.0, 0.0, 1.0, 0.0);
53 LIGHT_DEFAULT_SPOT_DIRECTION: TgluVector3f = (0.0, 0.0, -1.0);
54 LIGHT_DEFAULT_SPOT_EXPONENT: GLfloat = 0.0;
55 LIGHT_DEFAULT_SPOT_CUTOFF: GLfloat = 180.0;
56 LIGHT_DEFAULT_CONSTANT_ATT: GLfloat = 1.0;
57 LIGHT_DEFAULT_LINEAR_ATT: GLfloat = 0.0;
58 LIGHT_DEFAULT_QUADRATIC_ATT: GLfloat = 0.0;
61 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
62 TglcMaterial = class(TObject)
64 fData: TglcMaterialRec;
66 property Diffuse: TgluVector4f read fData.Diffuse write fData.Diffuse;
67 property Ambient: TgluVector4f read fData.Ambient write fData.Ambient;
68 property Specular: TgluVector4f read fData.Specular write fData.Specular;
69 property Emission: TgluVector4f read fData.Emission write fData.Emission;
70 property Shininess: GLfloat read fData.Shininess write fData.Shininess;
71 property Data: TglcMaterialRec read fData write fData;
73 procedure Bind(const aFace: TglcFace);
75 class procedure Bind(const aFace: TglcFace; const aMaterial: TglcMaterialRec);
76 class function DefaultValues: TglcMaterialRec;
81 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
82 EglcLight = class(Exception);
83 TglcLight = class(TObject)
85 function GetDataPtr: PglcLightRec;
89 procedure SetAmbient (const aValue: TgluVector4f); virtual;
90 procedure SetDiffuse (const aValue: TgluVector4f); virtual;
91 procedure SetSpecular (const aValue: TgluVector4f); virtual;
92 procedure SetPosition4f (const aValue: TgluVector4f); virtual;
93 procedure SetSpotDirection(const aValue: TgluVector3f); virtual;
94 procedure SetSpotExponent (const aValue: GLfloat); virtual;
95 procedure SetSpotCutoff (const aValue: GLfloat); virtual;
96 procedure SetConstantAtt (const aValue: GLfloat); virtual;
97 procedure SetLinearAtt (const aValue: GLfloat); virtual;
98 procedure SetQuadraticAtt (const aValue: GLfloat); virtual;
99 procedure SetData (const aValue: TglcLightRec); virtual;
101 property Ambient: TgluVector4f read fData.Ambient write SetAmbient;
102 property Diffuse: TgluVector4f read fData.Diffuse write SetDiffuse;
103 property Specular: TgluVector4f read fData.Specular write SetSpecular;
104 property Position4f: TgluVector4f read fData.Position write SetPosition4f;
105 property SpotDirection: TgluVector3f read fData.SpotDirection write SetSpotDirection;
106 property SpotExponent: GLfloat read fData.SpotExponent write SetSpotExponent;
107 property SpotCutoff: GLfloat read fData.SpotCutoff write SetSpotCutoff;
108 property ConstantAtt: GLfloat read fData.ConstantAtt write SetConstantAtt;
109 property LinearAtt: GLfloat read fData.LinearAtt write SetLinearAtt;
110 property QuadraticAtt: GLfloat read fData.QuadraticAtt write SetQuadraticAtt;
112 property Data: TglcLightRec read fData write SetData;
113 property DataPtr: PglcLightRec read GetDataPtr;
115 procedure Bind(const aLightID: GLenum; const aEnableLighting: Boolean = false); virtual; abstract;
117 class procedure Bind(const aLightID: GLenum; const aLight: TglcLightRec;
118 const aEnableLighting: Boolean; const aLightType: TglcLightType);
119 class procedure Unbind(const aLightID: GLenum; const aDisableLighting: Boolean = true);
120 class function DefaultValues: TglcLightRec; virtual;
125 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
126 TglcLightGlobal = class(TglcLight)
128 function GetDirection: TgluVector3f;
129 procedure SetDirection(aValue: TgluVector3f);
134 property Direction: TgluVector3f read GetDirection write SetDirection;
136 procedure Bind(const aLightID: GLenum; const aEnableLighting: Boolean = false); override;
139 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
140 TglcLightPoint = class(TglcLight)
144 function GetPosition: TgluVector3f;
145 procedure SetPosition(const aValue: TgluVector3f);
147 procedure SetMaxSize (const aValue: Single); virtual;
148 procedure SetSizeFactor(const aValue: Single); virtual;
153 property ConstantAtt;
155 property QuadraticAtt;
156 property MaxSize: Single read fMaxSize write SetMaxSize;
157 property SizeFactor: Single read fSizeFactor write SetSizeFactor;
158 property Position: TgluVector3f read GetPosition write SetPosition;
160 procedure Bind(const aLightID: GLenum; const aEnableLighting: Boolean = false); override;
165 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
166 TglcLightSpot = class(TglcLightPoint)
169 property SpotDirection;
170 property SpotExponent;
172 procedure Bind(const aLightID: GLenum; const aEnableLighting: Boolean = false); override;
177 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
178 //TglcMaterial//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
179 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
180 procedure TglcMaterial.Bind(const aFace: TglcFace);
185 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
186 class procedure TglcMaterial.Bind(const aFace: TglcFace; const aMaterial: TglcMaterialRec);
188 glMaterialfv(GLenum(aFace), GL_AMBIENT, @aMaterial.Ambient[0]);
189 glMaterialfv(GLenum(aFace), GL_DIFFUSE, @aMaterial.Diffuse[0]);
190 glMaterialfv(GLenum(aFace), GL_EMISSION, @aMaterial.Emission[0]);
191 glMaterialfv(GLenum(aFace), GL_SPECULAR, @aMaterial.Specular[0]);
192 glMaterialfv(GLenum(aFace), GL_SHININESS, @aMaterial.Shininess);
195 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
196 class function TglcMaterial.DefaultValues: TglcMaterialRec;
198 result.Ambient := MAT_DEFAULT_AMBIENT;
199 result.Diffuse := MAT_DEFAULT_DIFFUSE;
200 result.Specular := MAT_DEFAULT_SPECULAR;
201 result.Emission := MAT_DEFAULT_EMISSION;
202 result.Shininess := MAT_DEFAULT_SHININESS;
205 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
206 constructor TglcMaterial.Create;
209 fData := DefaultValues;
212 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
213 //TglcLight/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
214 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
215 function TglcLight.GetDataPtr: PglcLightRec;
220 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
221 procedure TglcLight.SetAmbient(const aValue: TgluVector4f);
223 fData.Ambient := aValue;
226 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
227 procedure TglcLight.SetDiffuse(const aValue: TgluVector4f);
229 fData.Diffuse := aValue;
232 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
233 procedure TglcLight.SetSpecular(const aValue: TgluVector4f);
235 fData.Specular := aValue;
238 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
239 procedure TglcLight.SetPosition4f(const aValue: TgluVector4f);
241 fData.Position := aValue;
244 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
245 procedure TglcLight.SetConstantAtt(const aValue: GLfloat);
247 fData.ConstantAtt := aValue;
250 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
251 procedure TglcLight.SetLinearAtt(const aValue: GLfloat);
253 fData.LinearAtt := aValue;
256 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
257 procedure TglcLight.SetQuadraticAtt(const aValue: GLfloat);
259 fData.QuadraticAtt := aValue;
262 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
263 procedure TglcLight.SetSpotDirection(const aValue: TgluVector3f);
265 fData.SpotDirection := aValue;
268 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
269 procedure TglcLight.SetSpotExponent(const aValue: GLfloat);
271 fData.SpotExponent := aValue;
274 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
275 procedure TglcLight.SetSpotCutoff(const aValue: GLfloat);
277 fData.SpotCutoff := aValue;
280 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
281 procedure TglcLight.SetData(const aValue: TglcLightRec);
286 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
287 class procedure TglcLight.Bind(const aLightID: GLenum; const aLight: TglcLightRec;
288 const aEnableLighting: Boolean; const aLightType: TglcLightType);
291 if (aEnableLighting) then
292 glEnable(GL_LIGHTING);
294 if (aLightType in [ltGlobal, ltPoint, ltSpot]) then begin
295 glLightfv(aLightID, GL_AMBIENT, @aLight.Ambient[0]);
296 glLightfv(aLightID, GL_DIFFUSE, @aLight.Diffuse[0]);
297 glLightfv(aLightID, GL_SPECULAR, @aLight.Specular[0]);
298 glLightfv(aLightID, GL_POSITION, @aLight.Position[0]);
300 glLightfv(aLightID, GL_AMBIENT, @LIGHT_DEFAULT_AMBIENT[0]);
301 glLightfv(aLightID, GL_DIFFUSE, @LIGHT_DEFAULT_DIFFUSE[0]);
302 glLightfv(aLightID, GL_SPECULAR, @LIGHT_DEFAULT_SPECULAR[0]);
303 glLightfv(aLightID, GL_POSITION, @LIGHT_DEFAULT_POSITION[0]);
306 if (aLightType in [ltPoint, ltSpot]) then begin
307 glLightfv(aLightID, GL_CONSTANT_ATTENUATION, @aLight.ConstantAtt);
308 glLightfv(aLightID, GL_LINEAR_ATTENUATION, @aLight.LinearAtt);
309 glLightfv(aLightID, GL_QUADRATIC_ATTENUATION, @aLight.QuadraticAtt);
311 glLightfv(aLightID, GL_CONSTANT_ATTENUATION, @LIGHT_DEFAULT_CONSTANT_ATT);
312 glLightfv(aLightID, GL_LINEAR_ATTENUATION, @LIGHT_DEFAULT_LINEAR_ATT);
313 glLightfv(aLightID, GL_QUADRATIC_ATTENUATION, @LIGHT_DEFAULT_QUADRATIC_ATT);
316 if (aLightType in [ltSpot]) then begin
317 glLightfv(aLightID, GL_SPOT_DIRECTION, @aLight.SpotDirection[0]);
318 glLightfv(aLightID, GL_SPOT_EXPONENT, @aLight.SpotExponent);
319 glLightfv(aLightID, GL_SPOT_CUTOFF, @aLight.SpotCutoff);
321 glLightfv(aLightID, GL_SPOT_DIRECTION, @LIGHT_DEFAULT_SPOT_DIRECTION[0]);
322 glLightfv(aLightID, GL_SPOT_EXPONENT, @LIGHT_DEFAULT_SPOT_EXPONENT);
323 glLightfv(aLightID, GL_SPOT_CUTOFF, @LIGHT_DEFAULT_SPOT_CUTOFF);
327 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
328 class procedure TglcLight.Unbind(const aLightID: GLenum; const aDisableLighting: Boolean);
331 if aDisableLighting then
332 glDisable(GL_LIGHTING);
335 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
336 class function TglcLight.DefaultValues: TglcLightRec;
338 result.Ambient := LIGHT_DEFAULT_AMBIENT;
339 result.Diffuse := LIGHT_DEFAULT_DIFFUSE;
340 result.Specular := LIGHT_DEFAULT_SPECULAR;
341 result.Position := LIGHT_DEFAULT_POSITION;
342 result.SpotDirection := LIGHT_DEFAULT_SPOT_DIRECTION;
343 result.SpotExponent := LIGHT_DEFAULT_SPOT_EXPONENT;
344 result.SpotCutoff := LIGHT_DEFAULT_SPOT_CUTOFF;
345 result.ConstantAtt := LIGHT_DEFAULT_CONSTANT_ATT;
346 result.LinearAtt := LIGHT_DEFAULT_LINEAR_ATT;
347 result.QuadraticAtt := LIGHT_DEFAULT_QUADRATIC_ATT;
350 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
351 constructor TglcLight.Create;
354 fData := DefaultValues;
357 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
358 //TglcLightGlobal///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
359 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
360 function TglcLightGlobal.GetDirection: TgluVector3f;
362 result := gluVector3f(Position4f);
365 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
366 procedure TglcLightGlobal.SetDirection(aValue: TgluVector3f);
368 Position4f := gluVector4f(aValue, 0.0);
371 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
372 procedure TglcLightGlobal.Bind(const aLightID: GLenum; const aEnableLighting: Boolean);
374 TglcLight.Bind(aLightID, fData, aEnableLighting, ltGlobal);
377 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
378 //TglcLightPoint////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
379 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
380 function TglcLightPoint.GetPosition: TgluVector3f;
382 result := gluVector3f(fData.Position);
385 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
386 procedure TglcLightPoint.SetPosition(const aValue: TgluVector3f);
388 SetPosition4f(gluVector4f(aValue, 1.0));
391 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
392 procedure TglcLightPoint.SetMaxSize(const aValue: Single);
397 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
398 procedure TglcLightPoint.SetSizeFactor(const aValue: Single);
400 fSizeFactor := aValue;
403 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
404 procedure TglcLightPoint.Bind(const aLightID: GLenum; const aEnableLighting: Boolean);
406 TglcLight.Bind(aLightID, fData, aEnableLighting, ltPoint);
409 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
410 constructor TglcLightPoint.Create;
413 Position := gluVector3f(0.0, 0.0, 0.0);
418 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
419 //TglcLightSpot/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
420 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
421 procedure TglcLightSpot.Bind(const aLightID: GLenum; const aEnableLighting: Boolean);
423 TglcLight.Bind(aLightID, fData, aEnableLighting, ltSpot);