This file is indexed.

/usr/src/castle-game-engine-4.1.1/opengl/castleglcubemaps.pas is in castle-game-engine-src 4.1.1-1.

This file is owned by root:root, with mode 0o644.

The actual contents of the file can be viewed below.

  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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
{
  Copyright 2008-2013 Michalis Kamburelis.

  This file is part of "Castle Game Engine".

  "Castle Game Engine" is free software; see the file COPYING.txt,
  included in this distribution, for details about the copyright.

  "Castle Game Engine" is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

  ----------------------------------------------------------------------------
}

{ OpenGL utilities for cube (environment) maps. }
unit CastleGLCubeMaps;

interface

uses CastleVectors, CastleCubeMaps, CastleImages, CastleDDS, GL, GLU, CastleGLUtils,
  CastleRenderingCamera, CastleGLImages, Castle3D;

type
  TCubeMapRenderSimpleFunction = procedure (ForCubeMap: boolean);

{ Calculate spherical harmonics basis describing environment rendered
  by OpenGL. Environment is rendered by
  Render(true) callback, from the
  CapturePoint. It's rendered to color buffer, and captured as grayscale.
  Captured pixel value is just assumed to be the value of spherical function
  at this direction. It's also scaled by ScaleColor (since rendering
  to OpenGL catches values in 0..1 range, but SH vector can express
  values from any range).

  This changes glViewport, so be sure to reset glViewport to something
  normal after calling this.

  The maps will be drawn in the color buffer (from positions MapScreenX, Y),
  so will actually be visible (call this before glClear or such if you
  want to hide them). }
procedure SHVectorGLCapture(
  var SHVector: array of Single;
  const CapturePoint: TVector3Single;
  const Render: TCubeMapRenderSimpleFunction;
  const MapScreenX, MapScreenY: Integer;
  const ScaleColor: Single);

type
  TCubeMapImages = array [TCubeMapSide] of TCastleImage;

{ Capture cube map by rendering environment from CapturePoint.

  Environment is rendered by Render callback
  that must honour camera described in RenderingCamera object.
  RenderingCamera.Target will be set to rtCubeMapEnvironment.
  RenderingCamera camera will be set to appropriate views
  from the CapturePoint. You should at least load RenderingCamera.Matrix
  to OpenGL modelview matrix before rendering your 3D scene.

  Scene is rendered to color buffer, captured as appropriate
  for Images classes (e.g. TGrayscaleImage, or TRGBImage).

  Cube map is recorded in six images provided in Images parameter.
  These must be already created TCastleImage instances with the exact same size.
  (But we do not require here the images to be square, or have power-of-two
  size, or honor GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB limit,
  as we do not initialize actual OpenGL cube map here.
  You can use generated images for any purpose.)

  This changes glViewport, so be sure to reset glViewport to something
  normal after calling this.

  ProjectionNear, ProjectionFar parameters will be used to set GL
  projection matrix. ProjectionFar may be equal to ZFarInfinity,
  as always.

  @param(MapsOverlap

    If @false then the six maps will be drawn on non-overlapping
    locations in the color buffer, and will be shifted by MapScreenX, Y.
    This has two benefits: you can actually show the generated maps then
    (useful for debug purposes). And, more important, you can sometimes
    get away then with only one glClear before GLCaptureCubeMapImages
    (avoiding doing glClear in each Render).

    Otherwise (when NiceMapsLayout = @false),
    then MapScreenX, Y will be ignored, and all images will be simply
    drawn in screen lower-left corner starting from (0, 0).
    The benefit is that this has the most chance of fitting into
    your window size.) }

procedure GLCaptureCubeMapImages(
  const Images: TCubeMapImages;
  const CapturePoint: TVector3Single;
  const Render: TRenderFromViewFunction;
  const ProjectionNear, ProjectionFar: Single;
  const MapsOverlap: boolean;
  const MapScreenX, MapScreenY: Integer);

{ Capture cube map to DDS image by rendering environment from CapturePoint.

  See GLCaptureCubeMapImages for documentation, this works the same,
  but it creates TDDSImage instance containing all six images (oriented
  as appropriate for DDS). }
function GLCaptureCubeMapDDS(
  const Size: Cardinal;
  const CapturePoint: TVector3Single;
  const Render: TRenderFromViewFunction;
  const ProjectionNear, ProjectionFar: Single;
  const MapsOverlap: boolean;
  const MapScreenX, MapScreenY: Integer): TDDSImage;

{ Capture cube map to OpenGL cube map texture by rendering environment
  from CapturePoint.

  See GLCaptureCubeMapImages for documentation, this works the same,
  but it captures images to given OpenGL texture name Tex.
  Tex must already be created cube map texture (with OpenGL
  size and internal formats set), with square images of Size.
  This also means that Size must be a valid OpenGL cube map texture size,
  you can check it by GLImages.IsCubeMapTextureSized.

  This captures the cube map images to "zero" texture level.
  If you use mipmaps, it's your problem how to generate other texture levels
  --- in the simplest case, call GenerateMipmap(GL_TEXTURE_CUBE_MAP).

  It uses RenderToTexture to render to the texture, so it will use
  framebuffer if available, and it's fast. }
procedure GLCaptureCubeMapTexture(
  const Tex: TGLuint;
  const Size: Cardinal;
  const CapturePoint: TVector3Single;
  const Render: TRenderFromViewFunction;
  const ProjectionNear, ProjectionFar: Single;
  RenderToTexture: TGLRenderToTexture);

implementation

uses SysUtils, CastleSphericalHarmonics, GLExt;

procedure SHVectorGLCapture(
  var SHVector: array of Single;
  const CapturePoint: TVector3Single;
  const Render: TCubeMapRenderSimpleFunction;
  const MapScreenX, MapScreenY: Integer;
  const ScaleColor: Single);

  procedure DrawMap(Side: TCubeMapSide);
  var
    Map: TGrayscaleImage;
    I, SHBasis, ScreenX, ScreenY: Integer;
  begin
    ScreenX := CubeMapInfo[Side].ScreenX * CubeMapSize + MapScreenX;
    ScreenY := CubeMapInfo[Side].ScreenY * CubeMapSize + MapScreenY;

    { We have to clear the buffer first. Clearing with glClear is fast,
      but it must be clipped with scissor (glViewport does not clip glClear).
      Later: actually, clearing is not needed now, since we call DrawLightMap
      at the beginning, right after clearing the whole screen.

    glScissor(ScreenX, ScreenY, CubeMapSize, CubeMapSize);
    glEnable(GL_SCISSOR_TEST);
      glClear(GL_COLOR_BUFFER_BIT);
    glDisable(GL_SCISSOR_TEST);
    }

    glViewport(ScreenX, ScreenY, CubeMapSize, CubeMapSize);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix;
      glLoadIdentity;
      glMultMatrix(PerspectiveProjMatrixDeg(90, 1, 0.01, 100));
      glMatrixMode(GL_MODELVIEW);

      glPushMatrix;

        glLoadMatrix(LookDirMatrix(CapturePoint, CubeMapInfo[Side].Dir, CubeMapInfo[Side].Up));
        Render(true);

      glPopMatrix;

      glMatrixMode(GL_PROJECTION);
    glPopMatrix;
    glMatrixMode(GL_MODELVIEW);

    Map := TGrayscaleImage(SaveScreen_noflush(TGrayscaleImage,
      ScreenX, ScreenY, CubeMapSize, CubeMapSize, GL_BACK));
    try
      { Use the Map to calculate SHVector[SHBasis] (this is the actual
        purpose of drawing the Render). SHVector[SHBasis] is just a dot product
        for all directions (for all pixels, in this case) of
        light intensity values * SH basis values. }

      for I := 0 to Sqr(CubeMapSize) - 1 do
        for SHBasis := 0 to High(SHVector) do
          SHVector[SHBasis] += (Map.GrayscalePixels[I]/255) *
            ScaleColor *
            SHBasisMap[SHBasis, Side, I];
    finally FreeAndNil(Map) end;
  end;

var
  SHBasis: Integer;
  Side: TCubeMapSide;
begin
  InitializeSHBasisMap;

  { Call all DrawMap. This wil draw maps, get them,
    and calculate SHVector describing them. }

  for SHBasis := 0 to High(SHVector) do
    SHVector[SHBasis] := 0;

  for Side := Low(TCubeMapSide) to High(TCubeMapSide) do
    DrawMap(Side);

  for SHBasis := 0 to High(SHVector) do
  begin
    { Each SHVector[SHBasis] is now calculated for all sphere points.
      We want this to be integral over a sphere, so normalize now.

      Since SHBasisMap contains result of SH function * solid angle of pixel
      (on cube map, pixels have different solid angles),
      so below we divide by 4*Pi (sphere area, sum of solid angles for every
      pixel). }
    SHVector[SHBasis] /= 4 * Pi;
  end;
end;

procedure SetRenderingCamera(
  const CapturePoint: TVector3Single;
  const Side: TCubeMapSide;
  const ProjectionMatrix: TMatrix4Single);
begin
  RenderingCamera.FromMatrix(
    LookDirMatrix(CapturePoint, CubeMapInfo[Side].Dir, CubeMapInfo[Side].Up),
    FastLookDirMatrix(CubeMapInfo[Side].Dir, CubeMapInfo[Side].Up),
    ProjectionMatrix, nil);
end;

procedure GLCaptureCubeMapImages(
  const Images: TCubeMapImages;
  const CapturePoint: TVector3Single;
  const Render: TRenderFromViewFunction;
  const ProjectionNear, ProjectionFar: Single;
  const MapsOverlap: boolean;
  const MapScreenX, MapScreenY: Integer);
var
  Width, Height: Cardinal;

  procedure DrawMap(Side: TCubeMapSide);
  var
    ScreenX, ScreenY: Integer;
    ProjectionMatrix: TMatrix4Single;
  begin
    if MapsOverlap then
    begin
      ScreenX := 0;
      ScreenY := 0;
    end else
    begin
      ScreenX := CubeMapInfo[Side].ScreenX * Integer(Width ) + MapScreenX;
      ScreenY := CubeMapInfo[Side].ScreenY * Integer(Height) + MapScreenY;
    end;

    glViewport(ScreenX, ScreenY, Width, Height);

    glMatrixMode(GL_PROJECTION);
    glPushMatrix;
      ProjectionMatrix := PerspectiveProjMatrixDeg(90, 1, ProjectionNear, ProjectionFar);
      glLoadMatrix(ProjectionMatrix);
      glMatrixMode(GL_MODELVIEW);

        RenderingCamera.Target := rtCubeMapEnvironment;
        SetRenderingCamera(CapturePoint, Side, ProjectionMatrix);
        Render;

      glMatrixMode(GL_PROJECTION);
    glPopMatrix;
    glMatrixMode(GL_MODELVIEW);

    SaveScreen_noflush(Images[Side], ScreenX, ScreenY, GL_BACK);
  end;

var
  Side: TCubeMapSide;
begin
  Width  := Images[csPositiveX].Width ;
  Height := Images[csPositiveX].Height;

  for Side := Low(TCubeMapSide) to High(TCubeMapSide) do
    DrawMap(Side);
end;

function GLCaptureCubeMapDDS(
  const Size: Cardinal;
  const CapturePoint: TVector3Single;
  const Render: TRenderFromViewFunction;
  const ProjectionNear, ProjectionFar: Single;
  const MapsOverlap: boolean;
  const MapScreenX, MapScreenY: Integer): TDDSImage;
var
  Images: TCubeMapImages;
  Side: TCubeMapSide;
begin
  for Side := Low(Side) to High(Side) do
    Images[Side] := TRGBImage.Create(Size, Size);

  GLCaptureCubeMapImages(Images, CapturePoint, Render,
    ProjectionNear, ProjectionFar,
    MapsOverlap, MapScreenX, MapScreenY);

  Result := TDDSImage.Create;
  Result.Width := Size;
  Result.Height := Size;
  Result.DDSType := dtCubeMap;
  Result.CubeMapSides := AllDDSCubeMapSides;
  Result.Mipmaps := false;
  Result.MipmapsCount := 1;
  Result.Images.Count := 6;
  Result.Images[Ord(dcsPositiveX)] := Images[csPositiveX];
  Result.Images[Ord(dcsNegativeX)] := Images[csNegativeX];
  { For DDS positive/negative Y must be swapped (Direct X has left-handed
    coord system). }
  Result.Images[Ord(dcsNegativeY)] := Images[csPositiveY];
  Result.Images[Ord(dcsPositiveY)] := Images[csNegativeY];
  Result.Images[Ord(dcsPositiveZ)] := Images[csPositiveZ];
  Result.Images[Ord(dcsNegativeZ)] := Images[csNegativeZ];
end;

procedure GLCaptureCubeMapTexture(
  const Tex: TGLuint;
  const Size: Cardinal;
  const CapturePoint: TVector3Single;
  const Render: TRenderFromViewFunction;
  const ProjectionNear, ProjectionFar: Single;
  RenderToTexture: TGLRenderToTexture);

  procedure DrawMap(Side: TCubeMapSide);
  var
    ProjectionMatrix: TMatrix4Single;
  begin
    { I used to implement here MapsOverlap, MapScreenX, MapScreenY,
      but removed (since inherently conflicting with framebuffer possibility
      inside TGLRenderToTexture).

    if MapsOverlap then
    begin
      ScreenX := 0;
      ScreenY := 0;
    end else
    begin
      ScreenX := CubeMapInfo[Side].ScreenX * Integer(Size) + MapScreenX;
      ScreenY := CubeMapInfo[Side].ScreenY * Integer(Size) + MapScreenY;
    end;
    }

    RenderToTexture.RenderBegin;
    RenderToTexture.SetTexture(Tex, GL_TEXTURE_CUBE_MAP_POSITIVE_X_ARB + Ord(Side));

      glViewport(0, 0, Size, Size);

      glMatrixMode(GL_PROJECTION);
      glPushMatrix;
        ProjectionMatrix := PerspectiveProjMatrixDeg(90, 1, ProjectionNear, ProjectionFar);
        glLoadMatrix(ProjectionMatrix);
        glMatrixMode(GL_MODELVIEW);

          RenderingCamera.Target := rtCubeMapEnvironment;
          SetRenderingCamera(CapturePoint, Side, ProjectionMatrix);
          Render;

        glMatrixMode(GL_PROJECTION);
      glPopMatrix;
      glMatrixMode(GL_MODELVIEW);

    RenderToTexture.RenderEnd(Side < High(Side));
  end;

var
  Side: TCubeMapSide;
begin
  RenderToTexture.CompleteTextureTarget := GL_TEXTURE_CUBE_MAP_ARB;
  for Side := Low(TCubeMapSide) to High(TCubeMapSide) do
    DrawMap(Side);
end;

end.