UNIT a3dge.Skybox;
(*<Defines a model that renders a skybox. *)
(*
Copyright (c) 2012, 2025 Guillermo Martínez J.
This software is provided 'as-is', without any express or implied
warranty. In no event will the authors be held liable for any damages
arising from the use of this software.
Permission is granted to anyone to use this software for any purpose,
including commercial applications, and to alter it and redistribute it
freely, subject to the following restrictions:
1. The origin of this software must not be misrepresented; you must not
claim that you wrote the original software. If you use this software
in a product, an acknowledgment in the product documentation would be
appreciated but is not required.
2. Altered source versions must be plainly marked as such, and must not be
misrepresented as being the original software.
3. This notice may not be removed or altered from any source
distribution.
*)
{$Include a3dge.cfg}
INTERFACE
USES
{$IfDef FPC}
GL,
{$Else}
OpenGL,
{$EndIf}
a3dge.World3D, Allegro5;
TYPE
(* Defines a skybox.
The texture is a bitmap with the 6 sides compacted as follow: @preformatted(
+--------+-------+------+
| front | right | back |
+--------+-------+------+
| bottom | left | top |
+--------+-------+------+
)
Look at the image "map.png" at the envmap directory for more info. *)
TSkybox = CLASS (TBaseModel3D)
PRIVATE
fOwnsTexture: Boolean;
fMap: GLuint;
fTexture: ALLEGRO_BITMAPptr;
procedure SetTexture (aTexture: ALLEGRO_BITMAPptr);
PUBLIC
(* Constructor. *)
constructor Create; override; overload;
(* Creates the skybox loading the texture map from the image file. *)
CONSTRUCTOR Create (FileName: STRING); OVERLOAD;
(* Releases resources. *)
DESTRUCTOR Destroy; OVERRIDE;
(* Renders the skybox. Call it after
@link(TBaseCamera3D.ApplyRotationmatrix).
The size is 20 x 20 x 20 so the "far" plane should be more than 50 to be
sure it won't cut it. *)
PROCEDURE Render (CONST aObject: TObject3D); OVERRIDE;
(* Should free the @link(Texture) when removed. *)
property OwnsTexture: Boolean read fOwnsTexture write fOwnsTexture;
(* Access to texture. *)
property Texture: ALLEGRO_BITMAPptr read fTexture write SetTexture;
END;
IMPLEMENTATION
USES
al5opengl, sysutils,
a3dge, a3dge.Classes, a3dge.Material;
procedure TSkybox.SetTexture (aTexture: ALLEGRO_BITMAPptr);
begin
if aTexture = fTexture then Exit;
if fOwnsTexture and Assigned (fTexture) then
al_destroy_bitmap (fTexture);
fTexture := aTexture;
fMap := al_get_opengl_texture (fTexture)
{$IfDef DEBUG}
;
a3dge.Application.Log (etInfo,'Skybox'' texture Id: %d', [fMap])
{$EndIf}
end;
constructor TSkybox.Create;
begin
inherited Create;
fOwnsTexture := True
end;
CONSTRUCTOR TSkybox.Create (FileName: STRING);
var
lBitmapFlags: LongInt;
BEGIN
INHERITED Create;
fOwnsTexture := True;
a3dge.Application.Log (
etDebug, 'Loading skybox "%s"...', [ExtractFileName (FileName)]
);
IF NOT FileExists (FileName) THEN
RAISE A3DGEException.CreateFmt ('Can''t find file "%s".', [FileName]);
{ Use of mip-mapping or other filtering with the bitmap used in the skybox
isn't a good idea, as it blurs the seams creating some artifacts.
}
lBitmapFlags := al_get_new_bitmap_flags;
al_set_new_bitmap_flags (ALLEGRO_CONVERT_BITMAP);
Self.SetTexture (al_load_bitmap (FileName));
al_set_new_bitmap_flags (lBitmapFlags);
IF fTexture = NIL THEN
RAISE A3DGEException.CreateFmt ('Can''t load file "%s".', [FileName])
END;
DESTRUCTOR TSkybox.Destroy;
BEGIN
IF fOwnsTexture and Assigned (fTexture) THEN
{ It "unloads" it from graphic memory when destroying. }
al_destroy_bitmap (fTexture);
INHERITED Destroy;
END;
PROCEDURE TSkybox.Render (CONST aObject: TObject3D);
var
lLightEnabled, lFogEnabled, lDepthEnabled, lTextureDisabled: Boolean;
BEGIN
lLightEnabled := glIsEnabled (GL_LIGHTING) = GL_TRUE;
lFogEnabled :=glIsEnabled (GL_FOG) = GL_TRUE;
lDepthEnabled := glIsEnabled (GL_DEPTH_TEST) = GL_TRUE;
lTextureDisabled := glIsEnabled (GL_TEXTURE_2D) = GL_FALSE;
{ Sky is always visible and not lighted. }
glDisable (GL_DEPTH_TEST);
glDisable (GL_FOG);
glDisable (GL_LIGHTING);
if lTextureDisabled then glEnable (GL_TEXTURE_2D);
glColor4fv (@clrWhite);
glBindTexture (GL_TEXTURE_2D, fMap);
{ Render. }
glBegin (GL_QUADS);
{ Front }
glTexCoord2f (0.334, 0.5); glVertex3i ( 10, -10, -10);
glTexCoord2f (0.334, 1 ); glVertex3i ( 10, 10, -10);
glTexCoord2f (0 , 1 ); glVertex3i (-10, 10, -10);
glTexCoord2f (0 , 0.5); glVertex3i (-10, -10, -10);
{ Right }
glTexCoord2f (0.667, 0.5); glVertex3i ( 10, -10, 10);
glTexCoord2f (0.667, 1 ); glVertex3i ( 10, 10, 10);
glTexCoord2f (0.334, 1 ); glVertex3i ( 10, 10, -10);
glTexCoord2f (0.334, 0.5); glVertex3i ( 10, -10, -10);
{ Back }
glTexCoord2f (1 , 0.5); glVertex3i (-10, -10, 10);
glTexCoord2f (1 , 1 ); glVertex3i (-10, 10, 10);
glTexCoord2f (0.667, 1 ); glVertex3i ( 10, 10, 10);
glTexCoord2f (0.667, 0.5); glVertex3i ( 10, -10, 10);
{ Left }
glTexCoord2f (0.667, 0.5); glVertex3i (-10, 10, 10);
glTexCoord2f (0.334, 0.5); glVertex3i (-10, -10, 10);
glTexCoord2f (0.334, 0 ); glVertex3i (-10, -10, -10);
glTexCoord2f (0.667, 0 ); glVertex3i (-10, 10, -10);
{ Top }
glTexCoord2f (0.667, 0 ); glVertex3i (-10, 10, -10);
glTexCoord2f (1 , 0 ); glVertex3i ( 10, 10, -10);
glTexCoord2f (1 , 0.5); glVertex3i ( 10, 10, 10);
glTexCoord2f (0.667, 0.5); glVertex3i (-10, 10, 10);
{ Bottom }
glTexCoord2f (0.334, 0.5); glVertex3i (-10, -10, 10);
glTexCoord2f (0 , 0.5); glVertex3i ( 10, -10, 10);
glTexCoord2f (0 , 0 ); glVertex3i ( 10, -10, -10);
glTexCoord2f (0.334, 0 ); glVertex3i (-10, -10, -10);
glEnd;
{ Enable OpenGL state again. }
if lLightEnabled then glEnable (GL_LIGHTING);
if lFogEnabled then glEnable (GL_FOG);
if lDepthEnabled then glEnable (GL_DEPTH_TEST);
if lTextureDisabled then glDisable (GL_TEXTURE_2D)
END;
END.