Menu

[r96]: / trunk / src / engine / a3dge.skybox.pas  Maximize  Restore  History

Download this file

197 lines (165 with data), 6.5 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
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.
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.