| 1 |
{%MainUnit castleinternalrenderer.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2024 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
{ } |
| 20 |
TTextureResource = class; |
| 21 |
|
| 22 |
TTextureResourceClass = class of TTextureResource; |
| 23 |
|
| 24 |
{ OpenGL resource of a texture node. } |
| 25 |
TTextureResource = class(TRendererResource) |
| 26 |
protected |
| 27 |
{ Calculate things from TextureProperties node. |
| 28 |
If TextureProperties = @nil, they are taken from defaults |
| 29 |
(possibly in RenderOptions) and BoundaryModeS/-T are taken |
| 30 |
from repeatS and repeatT fields. |
| 31 |
@groupBegin } |
| 32 |
procedure HandleTextureProperties( |
| 33 |
const TextureProperties: TTexturePropertiesNode; |
| 34 |
const RenderOptions: TCastleRenderOptions; |
| 35 |
const RepeatS, RepeatT, RepeatR: TGLenum; |
| 36 |
out Filter: TTextureFilter; |
| 37 |
out BoundaryModeS, BoundaryModeT, BoundaryModeR: TGLenum; |
| 38 |
out Anisotropy: TGLfloat; |
| 39 |
out GUITexture: boolean); overload; |
| 40 |
|
| 41 |
procedure HandleTextureProperties( |
| 42 |
const TextureProperties: TTexturePropertiesNode; |
| 43 |
const RenderOptions: TCastleRenderOptions; |
| 44 |
const RepeatS, RepeatT: TGLenum; |
| 45 |
out Filter: TTextureFilter; |
| 46 |
out BoundaryModeS, BoundaryModeT: TGLenum; |
| 47 |
out Anisotropy: TGLfloat; |
| 48 |
out GUITexture: boolean); overload; |
| 49 |
|
| 50 |
procedure HandleTextureProperties( |
| 51 |
const TextureProperties: TTexturePropertiesNode; |
| 52 |
const RenderOptions: TCastleRenderOptions; |
| 53 |
out Filter: TTextureFilter; |
| 54 |
out Anisotropy: TGLfloat; |
| 55 |
out GUITexture: boolean); overload; |
| 56 |
{ @groupEnd } |
| 57 |
|
| 58 |
{ Decide if this class can handle given texture Node. } |
| 59 |
class function IsClassForTextureNode( |
| 60 |
ANode: TAbstractTextureNode): boolean; virtual; abstract; |
| 61 |
public |
| 62 |
{ ANode must be TAbstractTextureNode } |
| 63 |
constructor Create(const ANode: TX3DNode); override; |
| 64 |
|
| 65 |
{ Reference to handled texture node. |
| 66 |
Never @nil. |
| 67 |
It's guaranteed to satisfy IsClassForTextureNode method of this class. } |
| 68 |
function TextureNode: TAbstractTextureNode; |
| 69 |
|
| 70 |
{ Find suitable TTextureResource class that can best handle given Node. |
| 71 |
Returns @nil if not found. |
| 72 |
|
| 73 |
@italic(Descedants implementors): override IsClassForTextureNode |
| 74 |
to be correctly recognized by this. } |
| 75 |
class function ClassForTextureNode( |
| 76 |
ANode: TAbstractTextureNode): TTextureResourceClass; |
| 77 |
public |
| 78 |
{ Bind texture for OpenGL (without enabling it). |
| 79 |
|
| 80 |
Just like Enable, returns @false when texture node was not successfully |
| 81 |
prepared for OpenGL. Returns @true when it was successfully bound |
| 82 |
(caller can be sure then that given texture unit is currently active). } |
| 83 |
function Bind(const TextureUnit: Cardinal): boolean; virtual; abstract; |
| 84 |
|
| 85 |
{ Enables texture for OpenGL. This has to bind texture identifier |
| 86 |
and enable proper texture state (for example: |
| 87 |
2D, and not 3D, and not cube). |
| 88 |
|
| 89 |
When returns @false, it means that texture node was not successfully |
| 90 |
prepared for OpenGL, which means (we assume that you called Prepare |
| 91 |
before Enable) that texture failed to load, required not available |
| 92 |
OpenGL version / extension etc. Caller will then disable |
| 93 |
the texture unit, and you don't have to generate tex coords for it. |
| 94 |
|
| 95 |
When returns @true (success) caller can be sure that the specified |
| 96 |
TextureUnit is currently bound (if OpenGL multitexturing |
| 97 |
extensions are available at all). This is useful, if you want |
| 98 |
to later adjust texture unit parameters, like |
| 99 |
glTexEnvi(GL_TEXTURE_ENV, ...). |
| 100 |
|
| 101 |
It's also already enabled (by glEnable(GL_TEXTURE_2D / |
| 102 |
GL_TEXTURE_CUBE_MAP / GL_TEXTURE_3D) ). } |
| 103 |
function Enable(const TextureUnit: Cardinal; |
| 104 |
Shader: TShader; const Env: TTextureEnv): boolean; virtual; abstract; |
| 105 |
|
| 106 |
{ Sets texture state for many texture units, based on this node. |
| 107 |
On every texture unit where something is enabled, |
| 108 |
proper texture identifier must be bound. |
| 109 |
Also, has to set complete glTexEnv on every enabled texture unit. |
| 110 |
|
| 111 |
TextureUnitsCount says how many texture units can be enabled/disabled. |
| 112 |
|
| 113 |
TextureUnitsCount does *not* take into account whether multitexturing |
| 114 |
OpenGL extensions are available at all. |
| 115 |
Look at GLFeatures.UseMultiTexturing for this. |
| 116 |
Think of GLFeatures.UseMultiTexturing as capping TextureUnitsCount to 1 |
| 117 |
(still, remember to honour TextureUnitsCount = 0 case in your implementation, |
| 118 |
even when GLFeatures.UseMultiTexturing = @true). |
| 119 |
|
| 120 |
You have to update TextureSlotsUsed, this is the count of texture units |
| 121 |
where some texture coordinates should be generated. |
| 122 |
It's initial value may be > 0, in case some texture slots are already taken. |
| 123 |
This means that all texture units above TextureSlotsUsed |
| 124 |
(to TextureUnitsCount - 1) should be disabled by the caller (no need to |
| 125 |
do this in EnableAll), |
| 126 |
and there's no need to generated texture coords for them. |
| 127 |
|
| 128 |
( |
| 129 |
Yes, there is some small optimization missed in the definition |
| 130 |
of TextureSlotsUsed: if some textures in the middle of |
| 131 |
multitexture children list failed to load, but some following children |
| 132 |
succeded, we'll generate tex coords even for the useless texture units |
| 133 |
in the middle. We could avoid generating texture coords for them, |
| 134 |
by changing TextureSlotsUsed into bool array. |
| 135 |
This optimization is not considered worthy implementing for now. |
| 136 |
) |
| 137 |
|
| 138 |
You have to set texture state of all texture units < TextureSlotsUsed, |
| 139 |
and only on them. } |
| 140 |
procedure EnableAll( |
| 141 |
const Renderer: TRenderer; |
| 142 |
const TextureUnitsCount: Cardinal; |
| 143 |
var TextureSlotsUsed: Cardinal; |
| 144 |
const Shader: TShader; |
| 145 |
const HasPreviousMainTexture: Boolean); virtual; abstract; |
| 146 |
end; |
| 147 |
|
| 148 |
{ Manage TTextureResource connected to X3D nodes. |
| 149 |
|
| 150 |
Non-instantiable class (has only class methods). |
| 151 |
|
| 152 |
This should be the only code accessing |
| 153 |
TAbstractTextureNode.InternalRendererResource field, |
| 154 |
everything else should go through this class. } |
| 155 |
TTextureResources = class abstract |
| 156 |
public |
| 157 |
{ Renderer resource for given ANode. |
| 158 |
Allows ANode = @nil. |
| 159 |
Returns @nil if not found or ANode is @nil. } |
| 160 |
class function Get(const ANode: TAbstractTextureNode): TTextureResource; |
| 161 |
|
| 162 |
{ Prepare renderer resources for given texture node, |
| 163 |
uploading texture to OpenGL if necessary. |
| 164 |
Accepts all texture nodes, multi texture or not-multi texture nodes. |
| 165 |
Accepts also (and ignores) @nil as ANode. } |
| 166 |
class procedure Prepare( |
| 167 |
const RenderOptions: TCastleRenderOptions; |
| 168 |
const ANode: TAbstractTextureNode); |
| 169 |
|
| 170 |
{ Release OpenGL resources for given texture node. |
| 171 |
Consistently with @link(Prepare), accepts and ignores @nil as ANode. } |
| 172 |
class procedure Unprepare(const ANode: TAbstractTextureNode); |
| 173 |
|
| 174 |
{ Bind texture node. Calls TTextureResource.Bind method. |
| 175 |
If no texture renderer is prepared for this node, returns @false. } |
| 176 |
class function Bind(ANode: TAbstractTextureNode; |
| 177 |
const TextureUnit: Cardinal): boolean; |
| 178 |
|
| 179 |
{ Enable and bind texture node. Calls TTextureResource.Enable method. |
| 180 |
If no texture renderer is prepared for this node, returns @false. } |
| 181 |
class function Enable(ANode: TAbstractTextureNode; |
| 182 |
const TextureUnit: Cardinal; |
| 183 |
Shader: TShader; const Env: TTextureEnv): boolean; |
| 184 |
end; |
| 185 |
|
| 186 |
{ Common class for all single (not multi-texture) texture nodes. |
| 187 |
|
| 188 |
Implements EnableAll method, by calling @link(Enable) call. |
| 189 |
Override only @link(Enable) in descendants. } |
| 190 |
TSingleTextureResource = class(TTextureResource) |
| 191 |
public |
| 192 |
procedure EnableAll(const Renderer: TRenderer; |
| 193 |
const TextureUnitsCount: Cardinal; |
| 194 |
var TextureSlotsUsed: Cardinal; |
| 195 |
const Shader: TShader; |
| 196 |
const HasPreviousMainTexture: Boolean); override; |
| 197 |
end; |
| 198 |
|
| 199 |
{ Renderer resource for TMultiTextureNode. |
| 200 |
|
| 201 |
In EnableAll, this uses other TTextureResource (of children) |
| 202 |
to enable them too. You don't need to worry about it from the outside, |
| 203 |
just use EnableAll. } |
| 204 |
TMultiTextureResource = class(TTextureResource) |
| 205 |
protected |
| 206 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 207 |
procedure PrepareCore( |
| 208 |
const RenderOptions: TCastleRenderOptions); override; |
| 209 |
procedure UnprepareCore; override; |
| 210 |
public |
| 211 |
{ ANode must be TMultiTextureNode } |
| 212 |
constructor Create(const ANode: TX3DNode); override; |
| 213 |
|
| 214 |
function TextureNode: TMultiTextureNode; |
| 215 |
|
| 216 |
function Bind(const TextureUnit: Cardinal): boolean; override; |
| 217 |
function Enable(const TextureUnit: Cardinal; |
| 218 |
Shader: TShader; const Env: TTextureEnv): boolean; override; |
| 219 |
procedure EnableAll(const Renderer: TRenderer; |
| 220 |
const TextureUnitsCount: Cardinal; |
| 221 |
var TextureSlotsUsed: Cardinal; |
| 222 |
const Shader: TShader; |
| 223 |
const HasPreviousMainTexture: Boolean); override; |
| 224 |
end; |
| 225 |
|
| 226 |
{ Renderer resource for a 2D texture. } |
| 227 |
T2DTextureResource = class(TSingleTextureResource) |
| 228 |
public |
| 229 |
{ OpenGL texture identifier. |
| 230 |
It may be 0 if the texture was initialized successfully. } |
| 231 |
function GLName: TGLTextureId; virtual; abstract; |
| 232 |
end; |
| 233 |
|
| 234 |
{ Renderer resource for 2D texture with static image (not a video). } |
| 235 |
TImageTextureResource = class(T2DTextureResource) |
| 236 |
strict private |
| 237 |
FGLName: TGLuint; |
| 238 |
protected |
| 239 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 240 |
procedure PrepareCore( |
| 241 |
const RenderOptions: TCastleRenderOptions); override; |
| 242 |
procedure UnprepareCore; override; |
| 243 |
public |
| 244 |
NormalMap, HeightMap: TGLuint; |
| 245 |
HeightMapScale: Single; |
| 246 |
|
| 247 |
function TextureNode: TAbstractTexture2DNode; |
| 248 |
|
| 249 |
function Bind(const TextureUnit: Cardinal): boolean; override; |
| 250 |
function Enable(const TextureUnit: Cardinal; |
| 251 |
Shader: TShader; const Env: TTextureEnv): boolean; override; |
| 252 |
function GLName: TGLTextureId; override; |
| 253 |
end; |
| 254 |
|
| 255 |
{ Renderer resource for TMovieTextureNode. } |
| 256 |
TMovieTextureResource = class(T2DTextureResource) |
| 257 |
protected |
| 258 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 259 |
procedure PrepareCore( |
| 260 |
const RenderOptions: TCastleRenderOptions); override; |
| 261 |
procedure UnprepareCore; override; |
| 262 |
public |
| 263 |
GLVideo: TGLVideo3D; |
| 264 |
|
| 265 |
function TextureNode: TMovieTextureNode; |
| 266 |
|
| 267 |
function Bind(const TextureUnit: Cardinal): boolean; override; |
| 268 |
function Enable(const TextureUnit: Cardinal; |
| 269 |
Shader: TShader; const Env: TTextureEnv): boolean; override; |
| 270 |
function GLName: TGLTextureId; override; |
| 271 |
end; |
| 272 |
|
| 273 |
{ Renderer resource for TRenderedTextureNode. } |
| 274 |
TRenderedTextureResource = class(T2DTextureResource) |
| 275 |
strict private |
| 276 |
FGLName: TGLuint; |
| 277 |
RenderToTexture: TGLRenderToTexture; |
| 278 |
protected |
| 279 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 280 |
procedure PrepareCore( |
| 281 |
const RenderOptions: TCastleRenderOptions); override; |
| 282 |
procedure UnprepareCore; override; |
| 283 |
public |
| 284 |
{ The actual decided image size and mipmap status. } |
| 285 |
Width, Height: Cardinal; |
| 286 |
NeedsMipmaps: boolean; |
| 287 |
|
| 288 |
function TextureNode: TRenderedTextureNode; |
| 289 |
|
| 290 |
function Bind(const TextureUnit: Cardinal): boolean; override; |
| 291 |
function Enable(const TextureUnit: Cardinal; |
| 292 |
Shader: TShader; const Env: TTextureEnv): boolean; override; |
| 293 |
function GLName: TGLTextureId; override; |
| 294 |
|
| 295 |
procedure Update( |
| 296 |
const Render: TRenderFromViewFunction; |
| 297 |
const ProjectionNear, ProjectionFar: Single; |
| 298 |
const CurrentViewpoint: TAbstractViewpointNode; |
| 299 |
const CameraViewKnown: boolean; |
| 300 |
const CameraView: TViewVectors; |
| 301 |
const ShapeForViewpointMirror: TX3DRendererShape); |
| 302 |
end; |
| 303 |
|
| 304 |
{ Abstract renderer resource for TAbstractEnvironmentTextureNode. } |
| 305 |
TCubeMapTextureResource = class(TSingleTextureResource) |
| 306 |
protected |
| 307 |
{ Releases GLName by TextureCubeMap_DecReference. |
| 308 |
Suitable for descendants tht initialize GLName by |
| 309 |
TextureCubeMap_IncReference. } |
| 310 |
procedure UnprepareCore; override; |
| 311 |
public |
| 312 |
GLName: TGLuint; |
| 313 |
|
| 314 |
function TextureNode: TAbstractEnvironmentTextureNode; |
| 315 |
|
| 316 |
function Bind(const TextureUnit: Cardinal): boolean; override; |
| 317 |
function Enable(const TextureUnit: Cardinal; |
| 318 |
Shader: TShader; const Env: TTextureEnv): boolean; override; |
| 319 |
end; |
| 320 |
|
| 321 |
{ Renderer resource for TComposedCubeMapTextureNode. } |
| 322 |
TComposedCubeMapTextureResource = class(TCubeMapTextureResource) |
| 323 |
protected |
| 324 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 325 |
procedure PrepareCore( |
| 326 |
const RenderOptions: TCastleRenderOptions); override; |
| 327 |
public |
| 328 |
function TextureNode: TComposedCubeMapTextureNode; |
| 329 |
end; |
| 330 |
|
| 331 |
{ Renderer resource for TImageCubeMapTextureNode. } |
| 332 |
TImageCubeMapTextureResource = class(TCubeMapTextureResource) |
| 333 |
protected |
| 334 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 335 |
procedure PrepareCore( |
| 336 |
const RenderOptions: TCastleRenderOptions); override; |
| 337 |
public |
| 338 |
function TextureNode: TImageCubeMapTextureNode; |
| 339 |
end; |
| 340 |
|
| 341 |
{ Renderer resource for TGeneratedCubeMapTextureNode. } |
| 342 |
TGeneratedCubeMapTextureResource = class(TCubeMapTextureResource) |
| 343 |
private |
| 344 |
RenderToTexture: TGLRenderToTexture; |
| 345 |
protected |
| 346 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 347 |
procedure PrepareCore( |
| 348 |
const RenderOptions: TCastleRenderOptions); override; |
| 349 |
procedure UnprepareCore; override; |
| 350 |
public |
| 351 |
{ The right size of the texture, |
| 352 |
that satisfies all OpenGL cube map sizes requirements |
| 353 |
(IsCubeMapTextureSized). } |
| 354 |
Size: Cardinal; |
| 355 |
|
| 356 |
{ Does Filter need mipmaps. } |
| 357 |
NeedsMipmaps: boolean; |
| 358 |
|
| 359 |
function TextureNode: TGeneratedCubeMapTextureNode; |
| 360 |
|
| 361 |
procedure Update( |
| 362 |
const Render: TRenderFromViewFunction; |
| 363 |
const ProjectionNear, ProjectionFar: Single; |
| 364 |
const CubeMiddle: TVector3); |
| 365 |
end; |
| 366 |
|
| 367 |
{ Renderer resource for TAbstractTexture3DNode. } |
| 368 |
T3DTextureResource = class(TSingleTextureResource) |
| 369 |
protected |
| 370 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 371 |
procedure PrepareCore( |
| 372 |
const RenderOptions: TCastleRenderOptions); override; |
| 373 |
procedure UnprepareCore; override; |
| 374 |
public |
| 375 |
GLName: TGLuint; |
| 376 |
|
| 377 |
function TextureNode: TAbstractTexture3DNode; |
| 378 |
|
| 379 |
function Bind(const TextureUnit: Cardinal): boolean; override; |
| 380 |
function Enable(const TextureUnit: Cardinal; |
| 381 |
Shader: TShader; const Env: TTextureEnv): boolean; override; |
| 382 |
end; |
| 383 |
|
| 384 |
{ Renderer resource for TGeneratedShadowMapNode. } |
| 385 |
TGeneratedShadowMapResource = class(T2DTextureResource) |
| 386 |
strict private |
| 387 |
RenderToTexture: TGLRenderToTexture; |
| 388 |
{ VarianceShadowMaps calculated at the PrepareCore time. } |
| 389 |
VarianceShadowMaps: boolean; |
| 390 |
NeedsMipmaps: boolean; |
| 391 |
FGLName: TGLuint; |
| 392 |
WarninigDoneCannotEnable: Boolean; |
| 393 |
protected |
| 394 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 395 |
procedure PrepareCore( |
| 396 |
const RenderOptions: TCastleRenderOptions); override; |
| 397 |
procedure UnprepareCore; override; |
| 398 |
public |
| 399 |
{ The right size of the texture, |
| 400 |
that satisfies all OpenGL sizes requirements. } |
| 401 |
Size: Cardinal; |
| 402 |
|
| 403 |
function TextureNode: TGeneratedShadowMapNode; |
| 404 |
|
| 405 |
function Bind(const TextureUnit: Cardinal): boolean; override; |
| 406 |
function Enable(const TextureUnit: Cardinal; |
| 407 |
Shader: TShader; const Env: TTextureEnv): boolean; override; |
| 408 |
function GLName: TGLTextureId; override; |
| 409 |
|
| 410 |
procedure Update( |
| 411 |
const Render: TRenderFromViewFunction; |
| 412 |
const ProjectionNear, ProjectionFar: Single; |
| 413 |
const Light: TAbstractPunctualLightNode); |
| 414 |
|
| 415 |
{ Check would we use Variance Shadow Maps with current ARenderer |
| 416 |
attributes and OpenGL version/extensions. } |
| 417 |
class function ClassVarianceShadowMaps(RenderOptions: TCastleRenderOptions): boolean; |
| 418 |
end; |
| 419 |
|
| 420 |
{ Renderer resource for TShaderTextureNode. } |
| 421 |
TShaderTextureResource = class(TSingleTextureResource) |
| 422 |
protected |
| 423 |
class function IsClassForTextureNode(ANode: TAbstractTextureNode): boolean; override; |
| 424 |
procedure PrepareCore( |
| 425 |
const RenderOptions: TCastleRenderOptions); override; |
| 426 |
procedure UnprepareCore; override; |
| 427 |
public |
| 428 |
function TextureNode: TShaderTextureNode; |
| 429 |
function Bind(const TextureUnit: Cardinal): boolean; override; |
| 430 |
function Enable(const TextureUnit: Cardinal; |
| 431 |
Shader: TShader; const Env: TTextureEnv): boolean; override; |
| 432 |
end; |
| 433 |
|
| 434 |
{$endif read_interface} |
| 435 |
|
| 436 |
{$ifdef read_implementation} |
| 437 |
|
| 438 |
{$ifndef OpenGLES} |
| 439 |
const |
| 440 |
CombineGL: array [TCombine] of TGLint = ( |
| 441 |
GL_MODULATE, GL_REPLACE, GL_ADD_SIGNED_EXT, GL_ADD, GL_SUBTRACT, |
| 442 |
GL_INTERPOLATE_EXT, GL_DOT3_RGB_ARB, GL_DOT3_RGBA_ARB |
| 443 |
); |
| 444 |
{$endif} |
| 445 |
|
| 446 |
function TextureRepeatToGL(const IsRepeat: boolean): TGLenum; |
| 447 |
begin |
| 448 |
if IsRepeat then |
| 449 |
Result := GL_REPEAT |
| 450 |
else |
| 451 |
Result := GLFeatures.CLAMP_TO_EDGE; |
| 452 |
end; |
| 453 |
|
| 454 |
{ TTextureResource ------------------------------------------------------------- } |
| 455 |
|
| 456 |
constructor TTextureResource.Create(const ANode: TX3DNode); |
| 457 |
begin |
| 458 |
Assert(ANode is TAbstractTextureNode, 'TTextureResource.Create acceps as Node only TAbstractTextureNode'); |
| 459 |
inherited; |
| 460 |
end; |
| 461 |
|
| 462 |
procedure TTextureResource.HandleTextureProperties( |
| 463 |
const TextureProperties: TTexturePropertiesNode; |
| 464 |
const RenderOptions: TCastleRenderOptions; |
| 465 |
const RepeatS, RepeatT, RepeatR: TGLenum; |
| 466 |
out Filter: TTextureFilter; |
| 467 |
out BoundaryModeS, BoundaryModeT, BoundaryModeR: TGLenum; |
| 468 |
out Anisotropy: TGLfloat; |
| 469 |
out GUITexture: boolean); |
| 470 |
|
| 471 |
function BoundaryModeGL(const Value: TBoundaryMode): TGLenum; |
| 472 |
const |
| 473 |
Map: array [TBoundaryMode] of TGLenum = ( |
| 474 |
GL_CLAMP_TO_EDGE, // bmClamp: Undefined in X3D, we prefer to make it equal to bmClampToEdge as we don't support border. |
| 475 |
GL_CLAMP_TO_EDGE, |
| 476 |
GL_CLAMP_TO_EDGE, //< bmClampToBoundaryUnsupported: Border not supported, hence behave like bmClampToEdge. |
| 477 |
GL_MIRRORED_REPEAT, |
| 478 |
GL_REPEAT |
| 479 |
); |
| 480 |
begin |
| 481 |
Result := Map[Value]; |
| 482 |
|
| 483 |
{ Use GLFeatures.CLAMP_TO_EDGE instead of GL_CLAMP_TO_EDGE, to support even |
| 484 |
ancient OpenGL versions that don't support GL_CLAMP_TO_EDGE. } |
| 485 |
if Result = GL_CLAMP_TO_EDGE then |
| 486 |
Result := GLFeatures.CLAMP_TO_EDGE; |
| 487 |
end; |
| 488 |
|
| 489 |
{ Convert TAutoMinificationFilter to TMinificationFilter, |
| 490 |
resolving the "default"/"fastest"/"nicest" aliases to specific values. } |
| 491 |
function FinalMinificationFilter(const FilterAuto: TAutoMinificationFilter): TMinificationFilter; |
| 492 |
begin |
| 493 |
case FilterAuto of |
| 494 |
minDefault: |
| 495 |
case RenderOptions.MinificationFilter of |
| 496 |
minDefault: Result := TCastleRenderOptions.DefaultMinificationFilter; |
| 497 |
minFastest: Result := minNearest; |
| 498 |
minNicest : Result := minLinearMipmapLinear; |
| 499 |
else Result := RenderOptions.MinificationFilter; |
| 500 |
end; |
| 501 |
minFastest: Result := minNearest; |
| 502 |
minNicest : Result := minLinearMipmapLinear; |
| 503 |
else Result := FilterAuto; |
| 504 |
end; |
| 505 |
end; |
| 506 |
|
| 507 |
{ Convert TAutoMagnificationFilter to TMagnificationFilter, |
| 508 |
resolving the "default"/"fastest"/"nicest" aliases to specific values. } |
| 509 |
function FinalMagnificationFilter(const FilterAuto: TAutoMagnificationFilter): TMagnificationFilter; |
| 510 |
begin |
| 511 |
case FilterAuto of |
| 512 |
magDefault: |
| 513 |
case RenderOptions.MagnificationFilter of |
| 514 |
magDefault: Result := TCastleRenderOptions.DefaultMagnificationFilter; |
| 515 |
magFastest: Result := magNearest; |
| 516 |
magNicest : Result := magLinear; |
| 517 |
else Result := RenderOptions.MagnificationFilter; |
| 518 |
end; |
| 519 |
magFastest: Result := magNearest; |
| 520 |
magNicest : Result := magLinear; |
| 521 |
else Result := FilterAuto; |
| 522 |
end; |
| 523 |
end; |
| 524 |
|
| 525 |
function TextureFilter(const RenderOptions: TCastleRenderOptions): TTextureFilter; |
| 526 |
begin |
| 527 |
case RenderOptions.MinificationFilter of |
| 528 |
minDefault: Result.Minification := TCastleRenderOptions.DefaultMinificationFilter; |
| 529 |
minFastest: Result.Minification := minNearest; |
| 530 |
minNicest : Result.Minification := minLinearMipmapLinear; |
| 531 |
else Result.Minification := RenderOptions.MinificationFilter; |
| 532 |
end; |
| 533 |
|
| 534 |
case RenderOptions.MagnificationFilter of |
| 535 |
magDefault: Result.Magnification := TCastleRenderOptions.DefaultMagnificationFilter; |
| 536 |
magFastest: Result.Magnification := magNearest; |
| 537 |
magNicest : Result.Magnification := magLinear; |
| 538 |
else Result.Magnification := RenderOptions.MagnificationFilter; |
| 539 |
end; |
| 540 |
end; |
| 541 |
|
| 542 |
begin |
| 543 |
if TextureProperties <> nil then |
| 544 |
begin |
| 545 |
Filter.Minification := FinalMinificationFilter(TextureProperties.MinificationFilter); |
| 546 |
Filter.Magnification := FinalMagnificationFilter(TextureProperties.MagnificationFilter); |
| 547 |
Anisotropy := TextureProperties.AnisotropicDegree; |
| 548 |
GUITexture := TextureProperties.GUITexture; |
| 549 |
|
| 550 |
if (TextureProperties.BoundaryModeS = bmClampToBoundaryUnsupported) or |
| 551 |
(TextureProperties.BoundaryModeT = bmClampToBoundaryUnsupported) or |
| 552 |
(TextureProperties.BoundaryModeR = bmClampToBoundaryUnsupported) then |
| 553 |
WritelnWarning('VRML/X3D', 'Boundary mode CLAMP_TO_BOUNDARY is not supported. Behaves like CLAMP_TO_EDGE.'); |
| 554 |
|
| 555 |
BoundaryModeS := BoundaryModeGL(TextureProperties.BoundaryModeS); |
| 556 |
BoundaryModeT := BoundaryModeGL(TextureProperties.BoundaryModeT); |
| 557 |
BoundaryModeR := BoundaryModeGL(TextureProperties.BoundaryModeR); |
| 558 |
end else |
| 559 |
begin |
| 560 |
Filter := TextureFilter(RenderOptions); |
| 561 |
Anisotropy := 1; |
| 562 |
GUITexture := false; |
| 563 |
|
| 564 |
{ Use repeatS/T/R |
| 565 |
as no texture property node is provided. [see 18.2.3] } |
| 566 |
BoundaryModeS := RepeatS; |
| 567 |
BoundaryModeT := RepeatT; |
| 568 |
BoundaryModeR := RepeatR; |
| 569 |
end; |
| 570 |
end; |
| 571 |
|
| 572 |
procedure TTextureResource.HandleTextureProperties( |
| 573 |
const TextureProperties: TTexturePropertiesNode; |
| 574 |
const RenderOptions: TCastleRenderOptions; |
| 575 |
const RepeatS, RepeatT: TGLenum; |
| 576 |
out Filter: TTextureFilter; |
| 577 |
out BoundaryModeS, BoundaryModeT: TGLenum; |
| 578 |
out Anisotropy: TGLfloat; |
| 579 |
out GUITexture: boolean); |
| 580 |
var |
| 581 |
Dummy: TGLenum; |
| 582 |
const |
| 583 |
DummyRepeatMode = GL_REPEAT; |
| 584 |
begin |
| 585 |
HandleTextureProperties(TextureProperties, RenderOptions, |
| 586 |
RepeatS, RepeatT, DummyRepeatMode, |
| 587 |
Filter, BoundaryModeS, BoundaryModeT, Dummy, Anisotropy, GUITexture); |
| 588 |
end; |
| 589 |
|
| 590 |
procedure TTextureResource.HandleTextureProperties( |
| 591 |
const TextureProperties: TTexturePropertiesNode; |
| 592 |
const RenderOptions: TCastleRenderOptions; |
| 593 |
out Filter: TTextureFilter; |
| 594 |
out Anisotropy: TGLfloat; |
| 595 |
out GUITexture: boolean); |
| 596 |
var |
| 597 |
Dummy: array[0..2] of TGLenum; |
| 598 |
const |
| 599 |
DummyRepeatMode = GL_REPEAT; |
| 600 |
begin |
| 601 |
HandleTextureProperties( |
| 602 |
TextureProperties, RenderOptions, |
| 603 |
DummyRepeatMode, DummyRepeatMode, DummyRepeatMode, |
| 604 |
Filter, Dummy[0], Dummy[1], Dummy[2], Anisotropy, GUITexture); |
| 605 |
end; |
| 606 |
|
| 607 |
class function TTextureResource.ClassForTextureNode( |
| 608 |
ANode: TAbstractTextureNode): TTextureResourceClass; |
| 609 |
|
| 610 |
function TryResult(C: TTextureResourceClass): boolean; |
| 611 |
begin |
| 612 |
Result := C.IsClassForTextureNode(ANode); |
| 613 |
if Result then |
| 614 |
ClassForTextureNode := C; |
| 615 |
end; |
| 616 |
|
| 617 |
begin |
| 618 |
{ TODO: in the future, some way of registering class for this will |
| 619 |
be done. For now, just try known final TTextureResource descendants. } |
| 620 |
if not (TryResult(TMultiTextureResource) or |
| 621 |
TryResult(TImageTextureResource) or |
| 622 |
TryResult(TMovieTextureResource) or |
| 623 |
TryResult(TRenderedTextureResource) or |
| 624 |
TryResult(TComposedCubeMapTextureResource) or |
| 625 |
TryResult(TImageCubeMapTextureResource) or |
| 626 |
TryResult(TGeneratedCubeMapTextureResource) or |
| 627 |
TryResult(T3DTextureResource) or |
| 628 |
TryResult(TGeneratedShadowMapResource) or |
| 629 |
TryResult(TShaderTextureResource) ) then |
| 630 |
Result := nil; |
| 631 |
end; |
| 632 |
|
| 633 |
function TTextureResource.TextureNode: TAbstractTextureNode; |
| 634 |
begin |
| 635 |
Result := TAbstractTextureNode(inherited Node); |
| 636 |
end; |
| 637 |
|
| 638 |
{ TTextureResources ------------------------------------------------------------ } |
| 639 |
|
| 640 |
class function TTextureResources.Get(const ANode: TAbstractTextureNode): TTextureResource; |
| 641 |
begin |
| 642 |
if ANode <> nil then |
| 643 |
Result := TTextureResource(ANode.InternalRendererResource) |
| 644 |
else |
| 645 |
Result := nil; |
| 646 |
end; |
| 647 |
|
| 648 |
class procedure TTextureResources.Prepare( |
| 649 |
const RenderOptions: TCastleRenderOptions; |
| 650 |
const ANode: TAbstractTextureNode); |
| 651 |
var |
| 652 |
TextureRes: TTextureResource; |
| 653 |
TextureResClass: TTextureResourceClass; |
| 654 |
begin |
| 655 |
if (ANode <> nil) and |
| 656 |
(ANode.InternalRendererResource = nil) then |
| 657 |
begin |
| 658 |
TextureResClass := TTextureResourceClass.ClassForTextureNode(ANode); |
| 659 |
if TextureResClass <> nil { Ignore if not handled node. } then |
| 660 |
begin |
| 661 |
TextureRes := TextureResClass.Create(ANode); |
| 662 |
TextureRes.Prepare(RenderOptions); |
| 663 |
ANode.InternalRendererResource := TextureRes; |
| 664 |
end; |
| 665 |
end; |
| 666 |
end; |
| 667 |
|
| 668 |
class procedure TTextureResources.Unprepare(const ANode: TAbstractTextureNode); |
| 669 |
begin |
| 670 |
if ANode <> nil then |
| 671 |
ANode.InternalRendererResourceFree; |
| 672 |
end; |
| 673 |
|
| 674 |
class function TTextureResources.Bind(ANode: TAbstractTextureNode; |
| 675 |
const TextureUnit: Cardinal): boolean; |
| 676 |
var |
| 677 |
TextureRes: TTextureResource; |
| 678 |
begin |
| 679 |
TextureRes := Get(ANode); |
| 680 |
Result := TextureRes <> nil; |
| 681 |
if Result then |
| 682 |
Result := TextureRes.Bind(TextureUnit); |
| 683 |
end; |
| 684 |
|
| 685 |
class function TTextureResources.Enable(ANode: TAbstractTextureNode; |
| 686 |
const TextureUnit: Cardinal; Shader: TShader; const Env: TTextureEnv): boolean; |
| 687 |
var |
| 688 |
TextureRes: TTextureResource; |
| 689 |
begin |
| 690 |
TextureRes := Get(ANode); |
| 691 |
Result := TextureRes <> nil; |
| 692 |
if Result then |
| 693 |
Result := TextureRes.Enable(TextureUnit, Shader, Env); |
| 694 |
end; |
| 695 |
|
| 696 |
{ TSingleTextureResource ------------------------------------------------------- } |
| 697 |
|
| 698 |
procedure TSingleTextureResource.EnableAll(const Renderer: TRenderer; |
| 699 |
const TextureUnitsCount: Cardinal; |
| 700 |
var TextureSlotsUsed: Cardinal; |
| 701 |
const Shader: TShader; |
| 702 |
const HasPreviousMainTexture: Boolean); |
| 703 |
var |
| 704 |
Env: TTextureEnv; |
| 705 |
begin |
| 706 |
{ coModulate is default texture mode for single texturing (not MultiTexture). |
| 707 |
|
| 708 |
Note: VRML 2 and X3D < 4.0 specifications say that the texture color should |
| 709 |
replace material color (see "Lighting model" spec) *in case of RGB |
| 710 |
(not grayscale) textures*. We contradict that specs, instead modulating |
| 711 |
with material color because of GL_MODULATE below. See |
| 712 |
https://castle-engine.io/x3d_multi_texturing.php#section_default_texture_mode , |
| 713 |
https://github.com/michaliskambi/x3d-tests/wiki/Make-RGB-and-grayscale-textures-treatment-consistent . |
| 714 |
|
| 715 |
What we do follows X3D 4.0 spec. |
| 716 |
As explained above, we actually changed the spec, because "always doing |
| 717 |
coModulate" is better. |
| 718 |
|
| 719 |
For alpha channel: specification says clearly that texture alpha |
| 720 |
should replace (never modulate) alpha channel, if only texture |
| 721 |
has any alpha channel. Again, we contradict it, instead multiplying alpha. |
| 722 |
|
| 723 |
Note that using GL_REPLACE below would not really make us conforming |
| 724 |
to the spec: it would mean that texture color replaces the whole resulting |
| 725 |
lighting calculation (not just material color, which is input to |
| 726 |
lighting calculation). So it would not be conforming either. } |
| 727 |
|
| 728 |
Env.Init(coModulate); |
| 729 |
|
| 730 |
{ HasPreviousMainTexture is passed when enabling Appearance.texture |
| 731 |
on Text node (geometry with FontTextureNode set), |
| 732 |
IOW when we have textured Text node. |
| 733 |
|
| 734 |
In this case, we already called EnableAll with FontTextureNode, |
| 735 |
and it has determined the initial color (and alpha) of the text. |
| 736 |
So our Source should indicate to mix with previous texture (FontTextureNode), |
| 737 |
and not with the material color (as is default, and would override |
| 738 |
FontTextureNode alpha). |
| 739 |
|
| 740 |
Testcases: |
| 741 |
demo-models/shadow_maps/primitives.x3dv |
| 742 |
demo-models/text/text_textured.x3dv |
| 743 |
Without this "if HasPreviousMainTexture then" check, |
| 744 |
the textured text displays every letter as a rectangle, as alpha |
| 745 |
of FontTextureNode is ignored. } |
| 746 |
if HasPreviousMainTexture then |
| 747 |
begin |
| 748 |
Env.Source[cRGB ] := csPreviousTexture; |
| 749 |
Env.Source[cAlpha] := csPreviousTexture; |
| 750 |
end; |
| 751 |
|
| 752 |
if (TextureUnitsCount > 0) and |
| 753 |
Enable(TextureSlotsUsed, Shader, Env) then |
| 754 |
begin |
| 755 |
if GLFeatures.EnableFixedFunction then |
| 756 |
begin |
| 757 |
{$ifndef OpenGLES} |
| 758 |
{ Here we know that Env was initialized above. |
| 759 |
So we know that mode is simple, the same for rgb and alpha, |
| 760 |
and can be set using GL_TEXTURE_ENV_MODE |
| 761 |
(doesn't require using GL_COMBINE). } |
| 762 |
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, CombineGL[Env.Combine[cRGB]]); |
| 763 |
{$endif} |
| 764 |
end; |
| 765 |
Inc(TextureSlotsUsed); |
| 766 |
end; |
| 767 |
end; |
| 768 |
|
| 769 |
{ TMultiTextureResource -------------------------------------------------------- } |
| 770 |
|
| 771 |
constructor TMultiTextureResource.Create(const ANode: TX3DNode); |
| 772 |
begin |
| 773 |
Assert(ANode is TMultiTextureNode, 'TMultiTextureResource.Create acceps as Node only TMultiTextureNode'); |
| 774 |
inherited; |
| 775 |
end; |
| 776 |
|
| 777 |
class function TMultiTextureResource.IsClassForTextureNode( |
| 778 |
ANode: TAbstractTextureNode): boolean; |
| 779 |
begin |
| 780 |
Result := ANode is TMultiTextureNode; |
| 781 |
end; |
| 782 |
|
| 783 |
function TMultiTextureResource.TextureNode: TMultiTextureNode; |
| 784 |
begin |
| 785 |
Result := TMultiTextureNode(inherited TextureNode); |
| 786 |
end; |
| 787 |
|
| 788 |
procedure TMultiTextureResource.PrepareCore( |
| 789 |
const RenderOptions: TCastleRenderOptions); |
| 790 |
var |
| 791 |
ChildTex: TX3DNode; |
| 792 |
I: Integer; |
| 793 |
begin |
| 794 |
for I := 0 to TextureNode.FdTexture.Count - 1 do |
| 795 |
begin |
| 796 |
ChildTex := TextureNode.FdTexture[I]; |
| 797 |
if ChildTex is TAbstractTextureNode then // also checks ChildTex <> nil |
| 798 |
begin |
| 799 |
if ChildTex is TMultiTextureNode then |
| 800 |
WritelnWarning('VRML/X3D', 'Child of MultiTexture node cannot be another MultiTexture node') else |
| 801 |
begin |
| 802 |
TTextureResources.Prepare(RenderOptions, TAbstractTextureNode(ChildTex)); |
| 803 |
end; |
| 804 |
end; |
| 805 |
end; |
| 806 |
end; |
| 807 |
|
| 808 |
procedure TMultiTextureResource.UnprepareCore; |
| 809 |
var |
| 810 |
ChildTex: TX3DNode; |
| 811 |
I: Integer; |
| 812 |
begin |
| 813 |
for I := 0 to TextureNode.FdTexture.Count - 1 do |
| 814 |
begin |
| 815 |
ChildTex := TextureNode.FdTexture[I]; |
| 816 |
if (ChildTex is TAbstractTextureNode) and // also checks ChildTex <> nil |
| 817 |
(not (ChildTex is TMultiTextureNode)) then |
| 818 |
TTextureResources.Unprepare(TAbstractTextureNode(ChildTex)); |
| 819 |
end; |
| 820 |
end; |
| 821 |
|
| 822 |
function TMultiTextureResource.Bind(const TextureUnit: Cardinal): boolean; |
| 823 |
begin |
| 824 |
{ TMultiTextureResource cannot set only one texture unit. |
| 825 |
This may be called from GLSL shader, when someone will use MultiTexture |
| 826 |
node for a shader uniform field. I don't know how this should be handled, |
| 827 |
I guess returning failure is Ok for now. } |
| 828 |
Result := false; |
| 829 |
end; |
| 830 |
|
| 831 |
function TMultiTextureResource.Enable(const TextureUnit: Cardinal; |
| 832 |
Shader: TShader; const Env: TTextureEnv): boolean; |
| 833 |
begin |
| 834 |
{ This should never be called. TMultiTextureResource cannot set only one |
| 835 |
texture unit. } |
| 836 |
Result := false; |
| 837 |
end; |
| 838 |
|
| 839 |
procedure TMultiTextureResource.EnableAll(const Renderer: TRenderer; |
| 840 |
const TextureUnitsCount: Cardinal; |
| 841 |
var TextureSlotsUsed: Cardinal; |
| 842 |
const Shader: TShader; |
| 843 |
const HasPreviousMainTexture: Boolean); |
| 844 |
{$ifndef OpenGLES} |
| 845 |
const |
| 846 |
ColorSourceGL: array [TColorSource] of TGLint = ( |
| 847 |
GL_PRIMARY_COLOR, GL_TEXTURE, GL_CONSTANT, GL_PREVIOUS |
| 848 |
); |
| 849 |
Argument: array [ta0..ta1] of Integer = ( 0, 1 ); |
| 850 |
{$endif} |
| 851 |
var |
| 852 |
InitialTextureSlotsUsed: Cardinal; |
| 853 |
ChildTex: TX3DNode; |
| 854 |
I: Integer; |
| 855 |
Success: boolean; |
| 856 |
Env: TTextureEnv; |
| 857 |
ModeStr, SourceStr, FunctionStr: string; |
| 858 |
begin |
| 859 |
{ calculate TextureSlotsUsed } |
| 860 |
InitialTextureSlotsUsed := TextureSlotsUsed; |
| 861 |
TextureSlotsUsed := TextureSlotsUsed + TextureNode.FdTexture.Count; |
| 862 |
MinVar(TextureSlotsUsed, TextureUnitsCount); |
| 863 |
if not GLFeatures.UseMultiTexturing then |
| 864 |
MinVar(TextureSlotsUsed, 1); |
| 865 |
|
| 866 |
{ TODO: Passing it to shader this way precludes ability to later change it |
| 867 |
(e.g. in case some ROUTE animates MultiTexture.color/alpha, or Pascal code changes it) |
| 868 |
without recompiling the shader. |
| 869 |
|
| 870 |
Right now changes to MultiTexture.color/alpha will recompile the shader. |
| 871 |
|
| 872 |
To make it efficient and corrent, this should be a uniform passed to shader. |
| 873 |
This is low-priority due to low MultiTexture usage. } |
| 874 |
Shader.MultiTextureColor := Vector4(TextureNode.Color, TextureNode.Alpha); |
| 875 |
|
| 876 |
for I := 0 to Integer(TextureSlotsUsed) - Integer(InitialTextureSlotsUsed) - 1 do |
| 877 |
begin |
| 878 |
ChildTex := TextureNode.FdTexture[I]; |
| 879 |
Success := false; |
| 880 |
|
| 881 |
if (ChildTex <> nil) and |
| 882 |
(ChildTex is TAbstractTextureNode) then |
| 883 |
begin |
| 884 |
if I < TextureNode.FdMode.Count then |
| 885 |
ModeStr := TextureNode.FdMode.Items[I] else |
| 886 |
ModeStr := ''; |
| 887 |
if I < TextureNode.FdSource.Count then |
| 888 |
SourceStr := TextureNode.FdSource.Items[I] else |
| 889 |
SourceStr := ''; |
| 890 |
if I < TextureNode.FdFunction.Count then |
| 891 |
FunctionStr := TextureNode.FdFunction.Items[I] else |
| 892 |
FunctionStr := ''; |
| 893 |
Env.Init(ModeStr, SourceStr, FunctionStr); |
| 894 |
|
| 895 |
if ChildTex is TMultiTextureNode then |
| 896 |
WritelnWarning('VRML/X3D', 'Child of MultiTexture node cannot be another MultiTexture node') |
| 897 |
else |
| 898 |
Success := TTextureResources.Enable( |
| 899 |
TAbstractTextureNode(ChildTex), |
| 900 |
InitialTextureSlotsUsed + I, Shader, Env); |
| 901 |
|
| 902 |
{ Apply Env for fixed-function pipeline. } |
| 903 |
if Success and GLFeatures.UseMultiTexturing { needed OpenGL exts available } then |
| 904 |
begin |
| 905 |
{ Set all the multitexture mode-related stuff. |
| 906 |
Below we handle TextureNode.mode, source, color, alpha, |
| 907 |
function fields. } |
| 908 |
|
| 909 |
if Env.Disabled then |
| 910 |
begin |
| 911 |
{ When mode=OFF, turn off the texture unit. } |
| 912 |
Renderer.DisableCurrentTexture; |
| 913 |
end else |
| 914 |
begin |
| 915 |
if GLFeatures.EnableFixedFunction then |
| 916 |
begin |
| 917 |
{$ifndef OpenGLES} |
| 918 |
glTexEnvi(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, GL_COMBINE); |
| 919 |
|
| 920 |
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_RGB, CombineGL[Env.Combine[cRGB]]); |
| 921 |
glTexEnvi(GL_TEXTURE_ENV, GL_COMBINE_ALPHA, CombineGL[Env.Combine[cAlpha]]); |
| 922 |
|
| 923 |
glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE, Env.Scale[cRGB]); |
| 924 |
glTexEnvf(GL_TEXTURE_ENV, GL_ALPHA_SCALE, Env.Scale[cAlpha]); |
| 925 |
|
| 926 |
if Env.CurrentTextureArgument[cRGB] <> taNone then |
| 927 |
begin |
| 928 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB + Argument[Env.CurrentTextureArgument[cRGB]], GL_TEXTURE); |
| 929 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB + Argument[Env.CurrentTextureArgument[cRGB]], GL_SRC_COLOR); |
| 930 |
end; |
| 931 |
|
| 932 |
if Env.CurrentTextureArgument[cAlpha] <> taNone then |
| 933 |
begin |
| 934 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA + Argument[Env.CurrentTextureArgument[cAlpha]], GL_TEXTURE); |
| 935 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA + Argument[Env.CurrentTextureArgument[cAlpha]], GL_SRC_ALPHA); |
| 936 |
end; |
| 937 |
|
| 938 |
if Env.SourceArgument[cRGB] <> taNone then |
| 939 |
begin |
| 940 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_RGB + Argument[Env.SourceArgument[cRGB]], ColorSourceGL[Env.Source[cRGB]]); |
| 941 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_RGB + Argument[Env.SourceArgument[cRGB]], GL_SRC_COLOR); |
| 942 |
end; |
| 943 |
|
| 944 |
if Env.SourceArgument[cAlpha] <> taNone then |
| 945 |
begin |
| 946 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE0_ALPHA + Argument[Env.SourceArgument[cAlpha]], ColorSourceGL[Env.Source[cAlpha]]); |
| 947 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND0_ALPHA + Argument[Env.SourceArgument[cAlpha]], GL_SRC_ALPHA); |
| 948 |
end; |
| 949 |
|
| 950 |
if (Env.Combine[cRGB] = coBlend) or |
| 951 |
(Env.Combine[cAlpha] = coBlend) then |
| 952 |
begin |
| 953 |
{ Whole source2 (both RGB and alpha) is filled by alpha from color |
| 954 |
specified by BlendAlphaSource. } |
| 955 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_RGB, ColorSourceGL[Env.BlendAlphaSource]); |
| 956 |
glTexEnvi(GL_TEXTURE_ENV, GL_SOURCE2_ALPHA, ColorSourceGL[Env.BlendAlphaSource]); |
| 957 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_RGB, GL_SRC_ALPHA); |
| 958 |
glTexEnvi(GL_TEXTURE_ENV, GL_OPERAND2_ALPHA, GL_SRC_ALPHA); |
| 959 |
end; |
| 960 |
|
| 961 |
if Env.NeedsConstantColor then |
| 962 |
begin |
| 963 |
{ Assign constant color now, when we know it should be used. } |
| 964 |
glTexEnvv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_COLOR, Vector4( |
| 965 |
TextureNode.FdColor.Value, |
| 966 |
TextureNode.FdAlpha.Value)); |
| 967 |
end; |
| 968 |
{$endif} |
| 969 |
end; |
| 970 |
end; |
| 971 |
end; |
| 972 |
end; |
| 973 |
|
| 974 |
if not Success then |
| 975 |
Renderer.DisableTexture(I); |
| 976 |
end; |
| 977 |
end; |
| 978 |
|
| 979 |
{ TImageTextureResource -------------------------------------------------------- } |
| 980 |
|
| 981 |
class function TImageTextureResource.IsClassForTextureNode( |
| 982 |
ANode: TAbstractTextureNode): boolean; |
| 983 |
begin |
| 984 |
Result := (ANode is TAbstractTexture2DNode) and |
| 985 |
TAbstractTexture2DNode(ANode).IsTextureImage; |
| 986 |
end; |
| 987 |
|
| 988 |
function TImageTextureResource.TextureNode: TAbstractTexture2DNode; |
| 989 |
begin |
| 990 |
Result := TAbstractTexture2DNode(inherited TextureNode); |
| 991 |
end; |
| 992 |
|
| 993 |
procedure TImageTextureResource.PrepareCore( |
| 994 |
const RenderOptions: TCastleRenderOptions); |
| 995 |
var |
| 996 |
Filter: TTextureFilter; |
| 997 |
Anisotropy: TGLfloat; |
| 998 |
BoundaryModeS: TGLenum; |
| 999 |
BoundaryModeT: TGLenum; |
| 1000 |
TextureWrap: TTextureWrap2D; |
| 1001 |
GUITexture, FlipVertically: boolean; |
| 1002 |
begin |
| 1003 |
{ HandledNode already made sure IsTextureImage = @true } |
| 1004 |
|
| 1005 |
HandleTextureProperties(TextureNode.TextureProperties, RenderOptions, |
| 1006 |
TextureRepeatToGL(TextureNode.RepeatS), TextureRepeatToGL(TextureNode.RepeatT), |
| 1007 |
Filter, BoundaryModeS, BoundaryModeT, Anisotropy, GUITexture); |
| 1008 |
|
| 1009 |
TextureWrap.Data[0] := BoundaryModeS; |
| 1010 |
TextureWrap.Data[1] := BoundaryModeT; |
| 1011 |
|
| 1012 |
FlipVertically := |
| 1013 |
(TextureNode is TImageTextureNode) and |
| 1014 |
TImageTextureNode(TextureNode).FlipVertically; |
| 1015 |
|
| 1016 |
FGLName := RendererCache.TextureImage_IncReference( |
| 1017 |
TextureNode.TextureImage, |
| 1018 |
TextureNode.TextureUsedFullUrl, |
| 1019 |
Filter, |
| 1020 |
Anisotropy, |
| 1021 |
TextureWrap, |
| 1022 |
TextureNode.TextureComposite, |
| 1023 |
GUITexture, |
| 1024 |
FlipVertically); |
| 1025 |
end; |
| 1026 |
|
| 1027 |
procedure TImageTextureResource.UnprepareCore; |
| 1028 |
begin |
| 1029 |
if FGLName <> 0 then |
| 1030 |
begin |
| 1031 |
RendererCache.TextureImage_DecReference(FGLName); |
| 1032 |
FGLName := 0; |
| 1033 |
end; |
| 1034 |
end; |
| 1035 |
|
| 1036 |
function TImageTextureResource.Bind(const TextureUnit: Cardinal): boolean; |
| 1037 |
begin |
| 1038 |
Result := FGLName <> 0; |
| 1039 |
if not Result then Exit; |
| 1040 |
|
| 1041 |
TRenderer.ActiveTexture(TextureUnit); |
| 1042 |
glBindTexture(GL_TEXTURE_2D, FGLName); |
| 1043 |
end; |
| 1044 |
|
| 1045 |
function TImageTextureResource.Enable(const TextureUnit: Cardinal; |
| 1046 |
Shader: TShader; const Env: TTextureEnv): boolean; |
| 1047 |
begin |
| 1048 |
Result := Bind(TextureUnit); |
| 1049 |
if not Result then Exit; |
| 1050 |
|
| 1051 |
Shader.EnableTexture(TextureUnit, tt2D, TextureNode, Env); |
| 1052 |
end; |
| 1053 |
|
| 1054 |
function TImageTextureResource.GLName: TGLTextureId; |
| 1055 |
begin |
| 1056 |
Result := FGLName; |
| 1057 |
end; |
| 1058 |
|
| 1059 |
{ TMovieTextureResource -------------------------------------------------------- } |
| 1060 |
|
| 1061 |
class function TMovieTextureResource.IsClassForTextureNode( |
| 1062 |
ANode: TAbstractTextureNode): boolean; |
| 1063 |
begin |
| 1064 |
{ Although for most code TMovieTextureResource, it would be enought |
| 1065 |
to have any TAbstractTexture2DNode with IsTextureVideo = @true. |
| 1066 |
For when rendering, we'll need some TMovieTextureNode properties |
| 1067 |
to choose video frame. |
| 1068 |
|
| 1069 |
Anyway, TMovieTextureNode is for now the only texture node possible |
| 1070 |
that may have IsTextureVideo = @true, so it's not a real problem for now. } |
| 1071 |
|
| 1072 |
Result := (ANode is TMovieTextureNode) and |
| 1073 |
TMovieTextureNode(ANode).IsTextureVideo; |
| 1074 |
end; |
| 1075 |
|
| 1076 |
function TMovieTextureResource.TextureNode: TMovieTextureNode; |
| 1077 |
begin |
| 1078 |
Result := TMovieTextureNode(inherited TextureNode); |
| 1079 |
end; |
| 1080 |
|
| 1081 |
procedure TMovieTextureResource.PrepareCore( |
| 1082 |
const RenderOptions: TCastleRenderOptions); |
| 1083 |
var |
| 1084 |
Filter: TTextureFilter; |
| 1085 |
Anisotropy: TGLfloat; |
| 1086 |
BoundaryModeS: TGLenum; |
| 1087 |
BoundaryModeT: TGLenum; |
| 1088 |
TextureWrap: TTextureWrap2D; |
| 1089 |
GUITexture: boolean; |
| 1090 |
begin |
| 1091 |
{ HandledNode already made sure IsTextureVideo = @true } |
| 1092 |
|
| 1093 |
HandleTextureProperties(TextureNode.TextureProperties, RenderOptions, |
| 1094 |
TextureRepeatToGL(TextureNode.RepeatS), TextureRepeatToGL(TextureNode.RepeatT), |
| 1095 |
Filter, BoundaryModeS, BoundaryModeT, Anisotropy, GUITexture); |
| 1096 |
|
| 1097 |
TextureWrap.Data[0] := BoundaryModeS; |
| 1098 |
TextureWrap.Data[1] := BoundaryModeT; |
| 1099 |
|
| 1100 |
GLVideo := RendererCache.TextureVideo_IncReference( |
| 1101 |
TextureNode.TextureVideo, |
| 1102 |
TextureNode.TextureUsedFullUrl, |
| 1103 |
TextureNode.FlipVertically, |
| 1104 |
Filter, |
| 1105 |
Anisotropy, |
| 1106 |
TextureWrap, |
| 1107 |
GUITexture); |
| 1108 |
end; |
| 1109 |
|
| 1110 |
procedure TMovieTextureResource.UnprepareCore; |
| 1111 |
begin |
| 1112 |
if GLVideo <> nil then |
| 1113 |
RendererCache.TextureVideo_DecReference(GLVideo); |
| 1114 |
end; |
| 1115 |
|
| 1116 |
function TMovieTextureResource.GLName: TGLTextureId; |
| 1117 |
var |
| 1118 |
VideoUnscaledTime: TFloatTime; |
| 1119 |
begin |
| 1120 |
{ Note: don't call IsTextureImage, IsTextureVideo here --- this |
| 1121 |
would cause reloading images/videos, nullifying |
| 1122 |
TCastleSceneCore.FreeResources([frTextureDataInNodes]) purpose. |
| 1123 |
|
| 1124 |
Actually, it would be safe to call this for non-MovieTexture nodes, |
| 1125 |
as they should be prepared to GL resources before doing |
| 1126 |
FreeResources. But for MovieTexture nodes it's forbidden, |
| 1127 |
as it's called at every frame render. } |
| 1128 |
|
| 1129 |
if GLVideo <> nil then |
| 1130 |
begin |
| 1131 |
VideoUnscaledTime := |
| 1132 |
TextureNode.TimeFunctionality.ElapsedTimeInCycle * |
| 1133 |
TextureNode.Speed; |
| 1134 |
if TextureNode.Speed < 0 then |
| 1135 |
VideoUnscaledTime := TextureNode.Duration + VideoUnscaledTime; |
| 1136 |
Result := GLVideo.GLTextureFromTime(VideoUnscaledTime); |
| 1137 |
end else |
| 1138 |
Result := 0; |
| 1139 |
end; |
| 1140 |
|
| 1141 |
function TMovieTextureResource.Bind(const TextureUnit: Cardinal): boolean; |
| 1142 |
var |
| 1143 |
N: TGLTextureId; |
| 1144 |
begin |
| 1145 |
N := GLName; |
| 1146 |
Result := N <> 0; |
| 1147 |
if Result then |
| 1148 |
begin |
| 1149 |
TRenderer.ActiveTexture(TextureUnit); |
| 1150 |
glBindTexture(GL_TEXTURE_2D, N); |
| 1151 |
end; |
| 1152 |
end; |
| 1153 |
|
| 1154 |
function TMovieTextureResource.Enable(const TextureUnit: Cardinal; |
| 1155 |
Shader: TShader; const Env: TTextureEnv): boolean; |
| 1156 |
begin |
| 1157 |
Result := Bind(TextureUnit); |
| 1158 |
if not Result then Exit; |
| 1159 |
|
| 1160 |
Shader.EnableTexture(TextureUnit, tt2D, TextureNode, Env); |
| 1161 |
end; |
| 1162 |
|
| 1163 |
{ TRenderedTextureResource ----------------------------------------------------- } |
| 1164 |
|
| 1165 |
class function TRenderedTextureResource.IsClassForTextureNode( |
| 1166 |
ANode: TAbstractTextureNode): boolean; |
| 1167 |
begin |
| 1168 |
Result := ANode is TRenderedTextureNode; |
| 1169 |
end; |
| 1170 |
|
| 1171 |
function TRenderedTextureResource.TextureNode: TRenderedTextureNode; |
| 1172 |
begin |
| 1173 |
Result := TRenderedTextureNode(inherited TextureNode); |
| 1174 |
end; |
| 1175 |
|
| 1176 |
procedure TRenderedTextureResource.PrepareCore( |
| 1177 |
const RenderOptions: TCastleRenderOptions); |
| 1178 |
var |
| 1179 |
InitialImage: TCastleImage; |
| 1180 |
Filter: TTextureFilter; |
| 1181 |
Anisotropy: TGLfloat; |
| 1182 |
BoundaryModeS: TGLenum; |
| 1183 |
BoundaryModeT: TGLenum; |
| 1184 |
TextureWrap: TTextureWrap2D; |
| 1185 |
NodeWidth, NodeHeight: Cardinal; |
| 1186 |
GUITexture: boolean; |
| 1187 |
Sizing: TTextureSizing; |
| 1188 |
begin |
| 1189 |
HandleTextureProperties(TextureNode.TextureProperties, RenderOptions, |
| 1190 |
TextureRepeatToGL(TextureNode.FdRepeatS.Value), TextureRepeatToGL(TextureNode.FdRepeatT.Value), |
| 1191 |
Filter, BoundaryModeS, BoundaryModeT, Anisotropy, GUITexture); |
| 1192 |
|
| 1193 |
{ calculate Filter, Anisotropy, NeedsMipmaps } |
| 1194 |
NeedsMipmaps := Filter.NeedsMipmaps; |
| 1195 |
if NeedsMipmaps and not HasGenerateMipmap then |
| 1196 |
begin |
| 1197 |
WritelnWarning('VRML/X3D' { This may be caused by OpenGL implementation |
| 1198 |
limits, so it may be impossible to predict by VRML author, |
| 1199 |
so it's "ignorable" warning. }, |
| 1200 |
'OpenGL(ES) doesn''t have glGenerateMipmap, so you cannot use mipmaps for RenderedTexture'); |
| 1201 |
Filter.Minification := minLinear; |
| 1202 |
NeedsMipmaps := false; |
| 1203 |
end; |
| 1204 |
|
| 1205 |
TextureWrap.Data[0] := BoundaryModeS; |
| 1206 |
TextureWrap.Data[1] := BoundaryModeT; |
| 1207 |
|
| 1208 |
{ calculate Width, Height } |
| 1209 |
if TextureNode.FdDimensions.Items.Count - 1 >= 0 then |
| 1210 |
NodeWidth := Max(TextureNode.FdDimensions.Items[0], 0) else |
| 1211 |
NodeWidth := DefaultRenderedTextureWidth; |
| 1212 |
if TextureNode.FdDimensions.Items.Count - 1 >= 1 then |
| 1213 |
NodeHeight := Max(TextureNode.FdDimensions.Items[1], 0) else |
| 1214 |
NodeHeight := DefaultRenderedTextureHeight; |
| 1215 |
Width := NodeWidth ; |
| 1216 |
Height := NodeHeight; |
| 1217 |
if GUITexture then |
| 1218 |
Sizing := tsAny else |
| 1219 |
Sizing := tsScalablePowerOf2; |
| 1220 |
if not IsTextureSized(Width, Height, Sizing) then |
| 1221 |
begin |
| 1222 |
Width := ResizeToTextureSize(Width , Sizing); |
| 1223 |
Height := ResizeToTextureSize(Height, Sizing); |
| 1224 |
WritelnWarning('VRML/X3D' { This may be caused by OpenGL implementation |
| 1225 |
limits, so it may be impossible to predict by VRML author, |
| 1226 |
so it's "ignorable" warning. }, |
| 1227 |
Format('Rendered texture size %d x %d is incorrect (texture size must be a power of two, > 0 and <= GL_MAX_TEXTURE_SIZE = %d), corrected to %d x %d', |
| 1228 |
[ NodeWidth, NodeHeight, |
| 1229 |
GLFeatures.MaxTextureSize, |
| 1230 |
Width, Height])); |
| 1231 |
end; |
| 1232 |
|
| 1233 |
if (TextureNode.FdDepthMap.Count > 0) and |
| 1234 |
TextureNode.FdDepthMap.Items[0] then |
| 1235 |
begin |
| 1236 |
WritelnWarning('VRML/X3D', 'RenderedTexture with depthMap = TRUE is no longer supported. For shadow maps, use dedicated CGE nodes.'); |
| 1237 |
Exit; |
| 1238 |
end; |
| 1239 |
|
| 1240 |
InitialImage := TRGBImage.Create(Width, Height); |
| 1241 |
try |
| 1242 |
InitialImage.URL := 'generated:/' + TextureNode.NiceName; |
| 1243 |
|
| 1244 |
{ Fill with deliberately stupid (but constant) color, |
| 1245 |
to recognize easily RenderedTexture which don't have textures |
| 1246 |
updated. } |
| 1247 |
InitialImage.Clear(Vector4Byte(255, 0, 255, 255)); |
| 1248 |
|
| 1249 |
FGLName := RendererCache.TextureImage_IncReference( |
| 1250 |
InitialImage, |
| 1251 |
'' { generated texture contents means empty URL }, |
| 1252 |
Filter, |
| 1253 |
Anisotropy, |
| 1254 |
TextureWrap, |
| 1255 |
nil, |
| 1256 |
GUITexture, |
| 1257 |
false); |
| 1258 |
|
| 1259 |
{ RenderedTexture never has any normal / height map |
| 1260 |
(Hm, although it would be possible to generate some in theory |
| 1261 |
--- after all, we generate it from 3D data. Idea for the future.) |
| 1262 |
NormalMap := 0; |
| 1263 |
HeightMap := 0; |
| 1264 |
} |
| 1265 |
finally FreeAndNil(InitialImage) end; |
| 1266 |
|
| 1267 |
RenderToTexture := TGLRenderToTexture.Create(Width, Height); |
| 1268 |
RenderToTexture.SetTexture(FGLName, GL_TEXTURE_2D); |
| 1269 |
RenderToTexture.Buffer := tbColor; |
| 1270 |
RenderToTexture.Stencil := true; |
| 1271 |
RenderToTexture.GLContextOpen; |
| 1272 |
end; |
| 1273 |
|
| 1274 |
procedure TRenderedTextureResource.UnprepareCore; |
| 1275 |
begin |
| 1276 |
FreeAndNil(RenderToTexture); |
| 1277 |
|
| 1278 |
if FGLName <> 0 then |
| 1279 |
begin |
| 1280 |
RendererCache.TextureImage_DecReference(FGLName); |
| 1281 |
FGLName := 0; |
| 1282 |
end; |
| 1283 |
end; |
| 1284 |
|
| 1285 |
function TRenderedTextureResource.Bind(const TextureUnit: Cardinal): boolean; |
| 1286 |
begin |
| 1287 |
Result := FGLName <> 0; |
| 1288 |
if not Result then Exit; |
| 1289 |
|
| 1290 |
TRenderer.ActiveTexture(TextureUnit); |
| 1291 |
glBindTexture(GL_TEXTURE_2D, FGLName); |
| 1292 |
end; |
| 1293 |
|
| 1294 |
function TRenderedTextureResource.Enable(const TextureUnit: Cardinal; |
| 1295 |
Shader: TShader; const Env: TTextureEnv): boolean; |
| 1296 |
begin |
| 1297 |
Result := Bind(TextureUnit); |
| 1298 |
if not Result then Exit; |
| 1299 |
|
| 1300 |
Shader.EnableTexture(TextureUnit, tt2D, TextureNode, Env); |
| 1301 |
end; |
| 1302 |
|
| 1303 |
procedure TRenderedTextureResource.Update( |
| 1304 |
const Render: TRenderFromViewFunction; |
| 1305 |
const ProjectionNear, ProjectionFar: Single; |
| 1306 |
const CurrentViewpoint: TAbstractViewpointNode; |
| 1307 |
const CameraViewKnown: boolean; |
| 1308 |
const CameraView: TViewVectors; |
| 1309 |
const ShapeForViewpointMirror: TX3DRendererShape); |
| 1310 |
|
| 1311 |
function GetProjectionMatrix(const Viewpoint: TAbstractViewpointNode): TMatrix4; |
| 1312 |
{ We have to calculate projection, given Viewpoint node. |
| 1313 |
Similar to TCastleSceneCore.InternalUpdateCamera. |
| 1314 |
|
| 1315 |
We have to calculate things a little differently, e.g. we have |
| 1316 |
no NavigationInfo (it could be undesirable to use scene bound NavigationInfo, |
| 1317 |
since X3D author cannot change it by any RenderedTexture field, |
| 1318 |
and for now we have no field RenderedTexture.navigationInfo...), |
| 1319 |
no knowledge of scene box (although we do have ProjectionNear / Far |
| 1320 |
already). |
| 1321 |
} |
| 1322 |
|
| 1323 |
procedure DoPerspective; |
| 1324 |
var |
| 1325 |
FieldOfView, AngleOfViewY: Single; |
| 1326 |
FieldOfViewAxis: TFieldOfViewAxis; |
| 1327 |
Angles: TVector2; |
| 1328 |
begin |
| 1329 |
if (Viewpoint <> nil) and |
| 1330 |
(Viewpoint is TViewpointNode) then |
| 1331 |
begin |
| 1332 |
FieldOfView := TViewpointNode(Viewpoint).FieldOfView; |
| 1333 |
if TViewpointNode(Viewpoint).FieldOfViewForceVertical then |
| 1334 |
FieldOfViewAxis := faVertical |
| 1335 |
else |
| 1336 |
FieldOfViewAxis := faSmallest; |
| 1337 |
end else |
| 1338 |
if (Viewpoint <> nil) and |
| 1339 |
(Viewpoint is TPerspectiveCameraNode_1) then |
| 1340 |
begin |
| 1341 |
FieldOfView := TPerspectiveCameraNode_1(Viewpoint).FdHeightAngle.Value; |
| 1342 |
FieldOfViewAxis := faSmallest; |
| 1343 |
end else |
| 1344 |
begin |
| 1345 |
FieldOfView := DefaultViewpointFieldOfView; |
| 1346 |
FieldOfViewAxis := faSmallest; |
| 1347 |
end; |
| 1348 |
|
| 1349 |
Angles := TViewpointNode.InternalFieldOfView(FieldOfView, FieldOfViewAxis, Width, Height); |
| 1350 |
AngleOfViewY := RadToDeg(Angles[1]); |
| 1351 |
|
| 1352 |
Result := PerspectiveProjectionMatrixDeg(AngleOfViewY, Width / Height, |
| 1353 |
ProjectionNear, ProjectionFar); |
| 1354 |
end; |
| 1355 |
|
| 1356 |
procedure DoOrthographic; |
| 1357 |
var |
| 1358 |
FieldOfView: TSingleList; |
| 1359 |
FinalFieldOfView: TFloatRectangle; |
| 1360 |
begin |
| 1361 |
{ default VRML/X3D fov } |
| 1362 |
FinalFieldOfView.Left := -1; |
| 1363 |
FinalFieldOfView.Bottom := -1; |
| 1364 |
FinalFieldOfView.Width := 2; |
| 1365 |
FinalFieldOfView.Height := 2; |
| 1366 |
|
| 1367 |
{ update left / right / bottom / top using OrthoViewpoint.fieldOfView } |
| 1368 |
if (Viewpoint <> nil) and |
| 1369 |
(Viewpoint is TOrthoViewpointNode) then |
| 1370 |
begin |
| 1371 |
FieldOfView := TOrthoViewpointNode(Viewpoint).FdFieldOfView.Items; |
| 1372 |
if FieldOfView.Count > 0 then FinalFieldOfView.Left := FieldOfView.Items[0]; |
| 1373 |
if FieldOfView.Count > 1 then FinalFieldOfView.Bottom := FieldOfView.Items[1]; |
| 1374 |
if FieldOfView.Count > 2 then FinalFieldOfView.Width := FieldOfView.Items[2] - FinalFieldOfView.Left; |
| 1375 |
if FieldOfView.Count > 3 then FinalFieldOfView.Height := FieldOfView.Items[3] - FinalFieldOfView.Bottom; |
| 1376 |
end else |
| 1377 |
if (Viewpoint <> nil) and |
| 1378 |
(Viewpoint is TOrthographicCameraNode_1) then |
| 1379 |
begin |
| 1380 |
FinalFieldOfView.Left := -TOrthographicCameraNode_1(Viewpoint).FdHeight.Value / 2; |
| 1381 |
FinalFieldOfView.Bottom := -TOrthographicCameraNode_1(Viewpoint).FdHeight.Value / 2; |
| 1382 |
FinalFieldOfView.Width := TOrthographicCameraNode_1(Viewpoint).FdHeight.Value; |
| 1383 |
FinalFieldOfView.Height := TOrthographicCameraNode_1(Viewpoint).FdHeight.Value; |
| 1384 |
end; |
| 1385 |
|
| 1386 |
FinalFieldOfView := TOrthoViewpointNode.InternalFieldOfView( |
| 1387 |
FinalFieldOfView, Width, Height); |
| 1388 |
|
| 1389 |
Result := OrthoProjectionMatrix(FinalFieldOfView, ProjectionNear, ProjectionFar); |
| 1390 |
end; |
| 1391 |
|
| 1392 |
var |
| 1393 |
ProjectionType: TProjectionType; |
| 1394 |
begin |
| 1395 |
if Viewpoint <> nil then |
| 1396 |
ProjectionType := Viewpoint.ProjectionType |
| 1397 |
else |
| 1398 |
ProjectionType := ptPerspective; |
| 1399 |
|
| 1400 |
case ProjectionType of |
| 1401 |
ptPerspective: DoPerspective; |
| 1402 |
ptOrthographic: DoOrthographic; |
| 1403 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 1404 |
else raise EInternalError.Create('TRenderedTextureResource.Update-ProjectionType?'); |
| 1405 |
{$endif} |
| 1406 |
end; |
| 1407 |
end; |
| 1408 |
|
| 1409 |
procedure GetRenderedTextureCamera(const Viewpoint: TAbstractViewpointNode; |
| 1410 |
out CameraRender: TViewVectors); |
| 1411 |
|
| 1412 |
procedure GetFromCurrent; |
| 1413 |
begin |
| 1414 |
if CameraViewKnown then |
| 1415 |
begin |
| 1416 |
CameraRender := CameraView; |
| 1417 |
end else |
| 1418 |
if CurrentViewpoint <> nil then |
| 1419 |
CurrentViewpoint.GetView(CameraRender) |
| 1420 |
else |
| 1421 |
begin |
| 1422 |
{ If all else fails (no viewpoint node bound, not known current |
| 1423 |
camera settings) then use defaults. } |
| 1424 |
CameraRender := DefaultX3DCameraView; |
| 1425 |
end; |
| 1426 |
end; |
| 1427 |
|
| 1428 |
begin |
| 1429 |
if Viewpoint = CurrentViewpoint then |
| 1430 |
GetFromCurrent else |
| 1431 |
begin |
| 1432 |
{ Viewpoint gets assigned something different than CurrentViewpoint |
| 1433 |
only when it's non-nil. } |
| 1434 |
Assert(Viewpoint <> nil); |
| 1435 |
Viewpoint.GetView(CameraRender); |
| 1436 |
end; |
| 1437 |
end; |
| 1438 |
|
| 1439 |
{ Best plane for a set of points. |
| 1440 |
The plane direction (first 3 components) is guaranteed to be normalized. } |
| 1441 |
function PointsPlane(const Points: TVector3List; out Plane: TVector4): Boolean; |
| 1442 |
var |
| 1443 |
Normal: TVector3 absolute Plane; |
| 1444 |
Tri: TTriangle3; |
| 1445 |
I: Integer; |
| 1446 |
PlaneShift: Single; |
| 1447 |
Point: TVector3; |
| 1448 |
begin |
| 1449 |
if Points.Count < 3 then Exit(false); |
| 1450 |
|
| 1451 |
{ Calculate plane Normal, averaging normals of all valid triangles. |
| 1452 |
Assumes polygon is convex. } |
| 1453 |
|
| 1454 |
Normal := TVector3.Zero; |
| 1455 |
|
| 1456 |
Tri[0] := Points.L[0]; |
| 1457 |
Tri[1] := Points.L[1]; |
| 1458 |
Tri[2] := Points.L[2]; |
| 1459 |
if Tri.IsValid then |
| 1460 |
Normal := Tri.Normal; |
| 1461 |
|
| 1462 |
for I := 3 to Points.Count - 1 do |
| 1463 |
begin |
| 1464 |
Tri[1] := Tri[2]; |
| 1465 |
Tri[2] := Points.L[I]; |
| 1466 |
if Tri.IsValid then |
| 1467 |
Normal := Normal + Tri.Normal; |
| 1468 |
end; |
| 1469 |
|
| 1470 |
if Normal.IsPerfectlyZero then |
| 1471 |
Exit(false); // no valid triangle |
| 1472 |
|
| 1473 |
{ calculate Plane[3] by averaging possible shift for all Points } |
| 1474 |
PlaneShift := 0; |
| 1475 |
for Point in Points do |
| 1476 |
begin |
| 1477 |
PlaneShift := PlaneShift + |
| 1478 |
-Normal.X * Point.X |
| 1479 |
-Normal.Y * Point.Y |
| 1480 |
-Normal.Z * Point.Z; |
| 1481 |
end; |
| 1482 |
PlaneShift := PlaneShift / Points.Count; |
| 1483 |
|
| 1484 |
Plane.W := PlaneShift; |
| 1485 |
Result := true; |
| 1486 |
end; |
| 1487 |
|
| 1488 |
procedure GetMatricesForViewpoint(const Viewpoint: TAbstractViewpointNode; |
| 1489 |
out CameraRender: TViewVectors; |
| 1490 |
out ProjectionMatrix: TMatrix4); |
| 1491 |
begin |
| 1492 |
ProjectionMatrix := GetProjectionMatrix(Viewpoint); |
| 1493 |
GetRenderedTextureCamera(Viewpoint, CameraRender); |
| 1494 |
end; |
| 1495 |
|
| 1496 |
procedure GetMatricesForViewpointMirror(const ViewpointMirror: TViewpointMirrorNode; |
| 1497 |
out CameraRender: TViewVectors; |
| 1498 |
out ProjectionMatrix: TMatrix4); |
| 1499 |
var |
| 1500 |
PosOnPlane, Side, Coord, PlaneCoord: TVector3; |
| 1501 |
PlaneCoordProjected: TVector2; |
| 1502 |
Plane: TVector4; |
| 1503 |
ZNear: Single; |
| 1504 |
FrustumDimensions: TFloatRectangle; |
| 1505 |
ShapeBox: TBox3d; |
| 1506 |
ShapeCorners: TBoxCorners; |
| 1507 |
GeometryCoordsField: TMFVec3f; |
| 1508 |
GeometryCoords: TVector3List; |
| 1509 |
begin |
| 1510 |
if (ShapeForViewpointMirror.ParentScene = nil) or |
| 1511 |
(not ShapeForViewpointMirror.ParentScene.HasWorldTransform) then |
| 1512 |
begin |
| 1513 |
WritelnWarning('ViewpointMirror can only work in TCastleScene that is part of viewport exactly once (without TCastleTransformReference)'); |
| 1514 |
GetMatricesForViewpoint(CurrentViewpoint, CameraRender, ProjectionMatrix); |
| 1515 |
Exit; |
| 1516 |
end; |
| 1517 |
|
| 1518 |
{ calculate camera CameraRender (in world space) } |
| 1519 |
if not CameraViewKnown then |
| 1520 |
begin |
| 1521 |
WritelnWarning('ViewpointMirror will not work correctly until camera vectors are known'); |
| 1522 |
GetMatricesForViewpoint(CurrentViewpoint, CameraRender, ProjectionMatrix); |
| 1523 |
Exit; |
| 1524 |
end; |
| 1525 |
GetRenderedTextureCamera(CurrentViewpoint, CameraRender); |
| 1526 |
|
| 1527 |
{ calculate GeometryCoords } |
| 1528 |
GeometryCoords := nil; |
| 1529 |
if ShapeForViewpointMirror.Geometry.InternalCoord(ShapeForViewpointMirror.State, GeometryCoordsField) and |
| 1530 |
(GeometryCoordsField <> nil) then |
| 1531 |
GeometryCoords := GeometryCoordsField.Items; |
| 1532 |
if GeometryCoords = nil then |
| 1533 |
begin |
| 1534 |
WritelnWarning('ViewpointMirror can only be used on node with coordinates'); |
| 1535 |
GetMatricesForViewpoint(CurrentViewpoint, CameraRender, ProjectionMatrix); |
| 1536 |
Exit; |
| 1537 |
end; |
| 1538 |
|
| 1539 |
{ calculate Plane of the mirror from GeometryCoords (in local shape coords) } |
| 1540 |
if not PointsPlane(GeometryCoords, Plane) then |
| 1541 |
begin |
| 1542 |
WritelnWarning('ViewpointMirror plane cannot be determined, the shape does not define a plane'); |
| 1543 |
GetMatricesForViewpoint(CurrentViewpoint, CameraRender, ProjectionMatrix); |
| 1544 |
Exit; |
| 1545 |
end; |
| 1546 |
|
| 1547 |
{ convert Plane to world coords } |
| 1548 |
Plane := PlaneTransform(Plane, |
| 1549 |
ShapeForViewpointMirror.ParentScene.WorldTransform * |
| 1550 |
ShapeForViewpointMirror.State.Transformation.Transform); |
| 1551 |
|
| 1552 |
{ reflect Pos, Dir, Up for mirror view } |
| 1553 |
PosOnPlane := PointOnPlaneClosestToPoint(Plane, CameraRender.Translation); |
| 1554 |
CameraRender.Translation := 2 * PosOnPlane - CameraRender.Translation; // mirror Pos versus Plane |
| 1555 |
CameraRender.Direction := PosOnPlane - CameraRender.Translation; |
| 1556 |
//CameraRender.Up := no need to change |
| 1557 |
|
| 1558 |
{ calculate Side, and make Dir, Up normalized and orthogonal } |
| 1559 |
CameraRender.Direction := CameraRender.Direction.Normalize; |
| 1560 |
//CameraRender.Up := CameraRender.Up.Normalize; // no need to |
| 1561 |
MakeVectorsOrthoOnTheirPlane(CameraRender.Up, CameraRender.Direction); |
| 1562 |
Side := TVector3.CrossProduct(CameraRender.Direction, CameraRender.Up); |
| 1563 |
|
| 1564 |
{ At this point CameraRender is ready. } |
| 1565 |
|
| 1566 |
{ calculate FrustumDimensions, |
| 1567 |
to include ShapeForViewpointMirror bounding box projected on the Plane. } |
| 1568 |
ShapeBox := ShapeForViewpointMirror.BoundingBox.Transform( |
| 1569 |
ShapeForViewpointMirror.ParentScene.WorldTransform); |
| 1570 |
ShapeBox.Corners(ShapeCorners); |
| 1571 |
FrustumDimensions := TFloatRectangle.Empty; |
| 1572 |
for Coord in ShapeCorners do |
| 1573 |
begin |
| 1574 |
PlaneCoord := PointOnPlaneClosestToPoint(Plane, Coord) - PosOnPlane; |
| 1575 |
PlaneCoordProjected := Vector2( |
| 1576 |
TVector3.DotProduct(PlaneCoord, Side), |
| 1577 |
TVector3.DotProduct(PlaneCoord, CameraRender.Up) |
| 1578 |
); |
| 1579 |
FrustumDimensions := FrustumDimensions.Include(PlaneCoordProjected); |
| 1580 |
end; |
| 1581 |
|
| 1582 |
ZNear := PointsDistance(CameraRender.Translation, PosOnPlane) + |
| 1583 |
ViewpointMirror.DistanceFromShape; |
| 1584 |
ProjectionMatrix := FrustumProjectionMatrix(FrustumDimensions, |
| 1585 |
ZNear, ProjectionFar); |
| 1586 |
|
| 1587 |
if ShapeForViewpointMirror.MirrorPlaneUniforms = nil then |
| 1588 |
ShapeForViewpointMirror.MirrorPlaneUniforms := TMirrorPlaneUniforms.Create; |
| 1589 |
ShapeForViewpointMirror.MirrorPlaneUniforms.NormalizedPlane := Plane; |
| 1590 |
ShapeForViewpointMirror.MirrorPlaneUniforms.CameraPositionOnPlane := PosOnPlane; |
| 1591 |
ShapeForViewpointMirror.MirrorPlaneUniforms.CameraSide := Side; |
| 1592 |
ShapeForViewpointMirror.MirrorPlaneUniforms.CameraUp := CameraRender.Up; |
| 1593 |
ShapeForViewpointMirror.MirrorPlaneUniforms.FrustumDimensions := FrustumDimensions; |
| 1594 |
end; |
| 1595 |
|
| 1596 |
var |
| 1597 |
CameraRender: TViewVectors; |
| 1598 |
SavedProjectionMatrix, NewProjectionMatrix: TMatrix4; |
| 1599 |
Camera: TRenderingCamera; |
| 1600 |
SavedViewport: TRectangle; |
| 1601 |
begin |
| 1602 |
if FGLName = 0 then Exit; |
| 1603 |
|
| 1604 |
{ calculate matrices to use when rendering, |
| 1605 |
using algorithm based on TextureNode.Viewpoint } |
| 1606 |
if TextureNode.FdViewpoint.Value is TViewpointMirrorNode then |
| 1607 |
GetMatricesForViewpointMirror( |
| 1608 |
TViewpointMirrorNode(TextureNode.FdViewpoint.Value), |
| 1609 |
CameraRender, NewProjectionMatrix) |
| 1610 |
else |
| 1611 |
if TextureNode.FdViewpoint.Value is TAbstractViewpointNode then |
| 1612 |
GetMatricesForViewpoint( |
| 1613 |
TAbstractViewpointNode(TextureNode.FdViewpoint.Value), |
| 1614 |
CameraRender, NewProjectionMatrix) |
| 1615 |
else |
| 1616 |
GetMatricesForViewpoint( |
| 1617 |
CurrentViewpoint, |
| 1618 |
CameraRender, NewProjectionMatrix); |
| 1619 |
|
| 1620 |
TextureNode.EventProjection.Send(NewProjectionMatrix); |
| 1621 |
TextureNode.EventViewing.Send(CameraRender.Matrix); |
| 1622 |
TextureNode.EventRendering.Send(true); |
| 1623 |
|
| 1624 |
Camera := TRenderingCamera.Create; |
| 1625 |
try |
| 1626 |
Camera.Target := rfRenderedTexture; |
| 1627 |
Camera.FromViewVectors(CameraRender, NewProjectionMatrix); |
| 1628 |
|
| 1629 |
RenderToTexture.RenderBegin; |
| 1630 |
|
| 1631 |
SavedViewport := RenderContext.Viewport; |
| 1632 |
RenderContext.Viewport := Rectangle(0, 0, Width, Height); |
| 1633 |
|
| 1634 |
SavedProjectionMatrix := RenderContext.ProjectionMatrix; |
| 1635 |
RenderContext.ProjectionMatrix := NewProjectionMatrix; |
| 1636 |
|
| 1637 |
Render(Camera); |
| 1638 |
|
| 1639 |
RenderContext.ProjectionMatrix := SavedProjectionMatrix; |
| 1640 |
RenderContext.Viewport := SavedViewport; |
| 1641 |
|
| 1642 |
RenderToTexture.RenderEnd; |
| 1643 |
finally FreeAndNil(Camera) end; |
| 1644 |
|
| 1645 |
if NeedsMipmaps then |
| 1646 |
RenderToTexture.GenerateMipmap; |
| 1647 |
|
| 1648 |
TextureNode.EventRendering.Send(false); |
| 1649 |
end; |
| 1650 |
|
| 1651 |
function TRenderedTextureResource.GLName: TGLTextureId; |
| 1652 |
begin |
| 1653 |
Result := FGLName; |
| 1654 |
end; |
| 1655 |
|
| 1656 |
{ TCubeMapTextureResource ------------------------------------------------------ } |
| 1657 |
|
| 1658 |
function TCubeMapTextureResource.TextureNode: TAbstractEnvironmentTextureNode; |
| 1659 |
begin |
| 1660 |
Result := TAbstractEnvironmentTextureNode(inherited TextureNode); |
| 1661 |
end; |
| 1662 |
|
| 1663 |
procedure TCubeMapTextureResource.UnprepareCore; |
| 1664 |
begin |
| 1665 |
if GLName <> 0 then |
| 1666 |
begin |
| 1667 |
RendererCache.TextureCubeMap_DecReference(GLName); |
| 1668 |
GLName := 0; |
| 1669 |
end; |
| 1670 |
end; |
| 1671 |
|
| 1672 |
function TCubeMapTextureResource.Bind(const TextureUnit: Cardinal): boolean; |
| 1673 |
begin |
| 1674 |
Result := GLName <> 0; |
| 1675 |
if not Result then Exit; |
| 1676 |
|
| 1677 |
TRenderer.ActiveTexture(TextureUnit); |
| 1678 |
glBindTexture(GL_TEXTURE_CUBE_MAP, GLName); |
| 1679 |
end; |
| 1680 |
|
| 1681 |
function TCubeMapTextureResource.Enable(const TextureUnit: Cardinal; |
| 1682 |
Shader: TShader; const Env: TTextureEnv): boolean; |
| 1683 |
begin |
| 1684 |
Result := Bind(TextureUnit); |
| 1685 |
if not Result then Exit; |
| 1686 |
|
| 1687 |
Shader.EnableTexture(TextureUnit, ttCubeMap, TextureNode, Env); |
| 1688 |
end; |
| 1689 |
|
| 1690 |
{ TComposedCubeMapTextureResource ---------------------------------------------- } |
| 1691 |
|
| 1692 |
class function TComposedCubeMapTextureResource.IsClassForTextureNode( |
| 1693 |
ANode: TAbstractTextureNode): boolean; |
| 1694 |
begin |
| 1695 |
Result := ANode is TComposedCubeMapTextureNode; |
| 1696 |
end; |
| 1697 |
|
| 1698 |
function TComposedCubeMapTextureResource.TextureNode: TComposedCubeMapTextureNode; |
| 1699 |
begin |
| 1700 |
Result := TComposedCubeMapTextureNode(inherited TextureNode); |
| 1701 |
end; |
| 1702 |
|
| 1703 |
procedure TComposedCubeMapTextureResource.PrepareCore( |
| 1704 |
const RenderOptions: TCastleRenderOptions); |
| 1705 |
var |
| 1706 |
Filter: TTextureFilter; |
| 1707 |
Anisotropy: TGLfloat; |
| 1708 |
BackRot, FrontRot, LeftRot, RightRot: TCastleImage; |
| 1709 |
GUITexture: boolean; |
| 1710 |
begin |
| 1711 |
if not GLFeatures.TextureCubeMap then |
| 1712 |
begin |
| 1713 |
WritelnWarning('VRML/X3D', 'Rendering context doesn''t support cube maps, cannot use ComposedCubeMapTexture'); |
| 1714 |
Exit; |
| 1715 |
end; |
| 1716 |
|
| 1717 |
if not TextureNode.LoadSides then |
| 1718 |
begin |
| 1719 |
WritelnWarning('VRML/X3D', 'Not all sides of a CubeMapTexture are correctly set and loaded, cannot use cube map'); |
| 1720 |
Exit; |
| 1721 |
end; |
| 1722 |
|
| 1723 |
HandleTextureProperties(TextureNode.TextureProperties, RenderOptions, |
| 1724 |
Filter, Anisotropy, GUITexture); |
| 1725 |
|
| 1726 |
try |
| 1727 |
{ To match expected orientation for OpenGL, we have to rotate images. |
| 1728 |
(source images are oriented as for VRML Background.) |
| 1729 |
We safely cast them to TCastleImage below, SideLoaded above checked |
| 1730 |
that they are indeed of TCastleImage class. } |
| 1731 |
BackRot := (TAbstractTexture2DNode(TextureNode.FdBack .Value).TextureImage as TCastleImage).MakeRotated(2); |
| 1732 |
FrontRot := (TAbstractTexture2DNode(TextureNode.FdFront.Value).TextureImage as TCastleImage).MakeRotated(2); |
| 1733 |
LeftRot := (TAbstractTexture2DNode(TextureNode.FdLeft .Value).TextureImage as TCastleImage).MakeRotated(2); |
| 1734 |
RightRot := (TAbstractTexture2DNode(TextureNode.FdRight.Value).TextureImage as TCastleImage).MakeRotated(2); |
| 1735 |
|
| 1736 |
GLName := RendererCache.TextureCubeMap_IncReference( |
| 1737 |
{ TODO: we could implement TComposedCubeMapTextureNode.TextureUsedFullUrl |
| 1738 |
by glueing 6 URLs for 6 sides, and pass it below. } |
| 1739 |
'', |
| 1740 |
Filter, Anisotropy, |
| 1741 |
{ positive x } RightRot, |
| 1742 |
{ negative x } LeftRot, |
| 1743 |
{ positive y } TAbstractTexture2DNode(TextureNode.FdTop .Value).TextureImage as TCastleImage, |
| 1744 |
{ negative y } TAbstractTexture2DNode(TextureNode.FdBottom.Value).TextureImage as TCastleImage, |
| 1745 |
{ positive z } BackRot, |
| 1746 |
{ negative z } FrontRot, |
| 1747 |
nil); |
| 1748 |
finally |
| 1749 |
FreeAndNil(BackRot); |
| 1750 |
FreeAndNil(FrontRot); |
| 1751 |
FreeAndNil(LeftRot); |
| 1752 |
FreeAndNil(RightRot); |
| 1753 |
end; |
| 1754 |
end; |
| 1755 |
|
| 1756 |
{ TImageCubeMapTextureResource ------------------------------------------------- } |
| 1757 |
|
| 1758 |
class function TImageCubeMapTextureResource.IsClassForTextureNode( |
| 1759 |
ANode: TAbstractTextureNode): boolean; |
| 1760 |
begin |
| 1761 |
Result := ANode is TImageCubeMapTextureNode; |
| 1762 |
end; |
| 1763 |
|
| 1764 |
function TImageCubeMapTextureResource.TextureNode: TImageCubeMapTextureNode; |
| 1765 |
begin |
| 1766 |
Result := TImageCubeMapTextureNode(inherited TextureNode); |
| 1767 |
end; |
| 1768 |
|
| 1769 |
procedure TImageCubeMapTextureResource.PrepareCore( |
| 1770 |
const RenderOptions: TCastleRenderOptions); |
| 1771 |
var |
| 1772 |
Filter: TTextureFilter; |
| 1773 |
Anisotropy: TGLfloat; |
| 1774 |
GUITexture: boolean; |
| 1775 |
Composite: TCompositeImage; |
| 1776 |
begin |
| 1777 |
if not GLFeatures.TextureCubeMap then |
| 1778 |
begin |
| 1779 |
WritelnWarning('VRML/X3D', 'Rendering context doesn''t support cube maps, cannot use ImageCubeMapTexture'); |
| 1780 |
Exit; |
| 1781 |
end; |
| 1782 |
|
| 1783 |
Composite := TextureNode.LoadImage; |
| 1784 |
{ If TextureNode doesn't contain anything useful, just exit. |
| 1785 |
TextureNode.LoadImage already did necessary WritelnWarnings. } |
| 1786 |
if Composite = nil then Exit; |
| 1787 |
|
| 1788 |
try |
| 1789 |
|
| 1790 |
HandleTextureProperties(TextureNode.TextureProperties, RenderOptions, |
| 1791 |
Filter, Anisotropy, GUITexture); |
| 1792 |
|
| 1793 |
{ TODO: this is a quick and dirty method: |
| 1794 |
- We call LoadImage each time, while load calls should |
| 1795 |
be minimized (to avoid loading image many times, but also |
| 1796 |
to avoid making repeated warnings in case image fails). |
| 1797 |
Should be cached, like for 2D texture nodes. |
| 1798 |
- We do not use cube map mipmaps stored inside Composite file. |
| 1799 |
} |
| 1800 |
|
| 1801 |
GLName := RendererCache.TextureCubeMap_IncReference( |
| 1802 |
{ TODO: we could implement TImageCubeMapTextureNode.TextureUsedFullUrl and pass it below. } |
| 1803 |
'', |
| 1804 |
Filter, Anisotropy, |
| 1805 |
Composite.CubeMapImage(csPositiveX), |
| 1806 |
Composite.CubeMapImage(csNegativeX), |
| 1807 |
Composite.CubeMapImage(csPositiveY), |
| 1808 |
Composite.CubeMapImage(csNegativeY), |
| 1809 |
Composite.CubeMapImage(csPositiveZ), |
| 1810 |
Composite.CubeMapImage(csNegativeZ), |
| 1811 |
Composite); |
| 1812 |
finally FreeAndNil(Composite); end; |
| 1813 |
end; |
| 1814 |
|
| 1815 |
{ TGeneratedCubeMapTextureResource --------------------------------------------- } |
| 1816 |
|
| 1817 |
class function TGeneratedCubeMapTextureResource.IsClassForTextureNode( |
| 1818 |
ANode: TAbstractTextureNode): boolean; |
| 1819 |
begin |
| 1820 |
Result := ANode is TGeneratedCubeMapTextureNode; |
| 1821 |
end; |
| 1822 |
|
| 1823 |
function TGeneratedCubeMapTextureResource.TextureNode: TGeneratedCubeMapTextureNode; |
| 1824 |
begin |
| 1825 |
Result := TGeneratedCubeMapTextureNode(inherited TextureNode); |
| 1826 |
end; |
| 1827 |
|
| 1828 |
procedure TGeneratedCubeMapTextureResource.PrepareCore( |
| 1829 |
const RenderOptions: TCastleRenderOptions); |
| 1830 |
var |
| 1831 |
Filter: TTextureFilter; |
| 1832 |
Anisotropy: TGLfloat; |
| 1833 |
GUITexture: boolean; |
| 1834 |
InitialImage: TCastleImage; |
| 1835 |
begin |
| 1836 |
if not GLFeatures.TextureCubeMap then |
| 1837 |
begin |
| 1838 |
WritelnWarning('VRML/X3D', 'Rendering context doesn''t support cube maps, cannot use GeneratedCubeMapTexture'); |
| 1839 |
Exit; |
| 1840 |
end; |
| 1841 |
|
| 1842 |
HandleTextureProperties(TextureNode.TextureProperties, RenderOptions, |
| 1843 |
Filter, Anisotropy, GUITexture); |
| 1844 |
|
| 1845 |
{ calculate Filter, Anisotropy, NeedsMipmaps } |
| 1846 |
NeedsMipmaps := Filter.NeedsMipmaps; |
| 1847 |
if NeedsMipmaps and not HasGenerateMipmap then |
| 1848 |
begin |
| 1849 |
WritelnWarning('VRML/X3D' { This may be caused by OpenGL implementation |
| 1850 |
limits, so it may be impossible to predict by VRML author, |
| 1851 |
so it's "ignorable" warning. }, |
| 1852 |
'OpenGL(ES) doesn''t have glGenerateMipmap, so you cannot use mipmaps for GeneratedCubeMapTexture'); |
| 1853 |
Filter.Minification := minLinear; |
| 1854 |
NeedsMipmaps := false; |
| 1855 |
end; |
| 1856 |
|
| 1857 |
{ calculate Size } |
| 1858 |
Size := Max(TextureNode.FdSize.Value, 0); |
| 1859 |
if not IsCubeMapTextureSized(Size) then |
| 1860 |
begin |
| 1861 |
Size := ResizeToCubeMapTextureSize(Size); |
| 1862 |
WritelnWarning('VRML/X3D' { This may be caused by OpenGL implementation |
| 1863 |
limits, so it may be impossible to predict by VRML author, |
| 1864 |
so it's "ignorable" warning. }, |
| 1865 |
Format('Cube map texture size %d is incorrect (cube map texture size must be a power of two, > 0 and <= GL_MAX_CUBE_MAP_TEXTURE_SIZE_ARB = %d), corrected to %d', |
| 1866 |
[ TextureNode.FdSize.Value, GLFeatures.MaxCubeMapTextureSize, Size])); |
| 1867 |
end; |
| 1868 |
|
| 1869 |
InitialImage := TRGBImage.Create(Size, Size); |
| 1870 |
try |
| 1871 |
InitialImage.URL := 'generated:/' + TextureNode.NiceName; |
| 1872 |
|
| 1873 |
{ Fill with deliberately stupid (but constant) color, |
| 1874 |
to recognize easily GeneratedCubeMapTexture which don't have textures |
| 1875 |
updated. } |
| 1876 |
if not GLVersion.BuggyGenerateCubeMap then |
| 1877 |
InitialImage.Clear(Vector4Byte(255, 0, 255, 255)) |
| 1878 |
else |
| 1879 |
InitialImage.Clear(Vector4Byte(237, 237, 237, 255)); { when buggy, use some reasonable color } |
| 1880 |
|
| 1881 |
GLName := RendererCache.TextureCubeMap_IncReference( |
| 1882 |
'' { generated texture contents means empty URL; this also prevents sharing textures by TextureCubeMap_IncReference }, |
| 1883 |
Filter, Anisotropy, |
| 1884 |
InitialImage, InitialImage, |
| 1885 |
InitialImage, InitialImage, |
| 1886 |
InitialImage, InitialImage, |
| 1887 |
nil); |
| 1888 |
finally FreeAndNil(InitialImage) end; |
| 1889 |
|
| 1890 |
RenderToTexture := TGLRenderToTexture.Create(Size, Size); |
| 1891 |
RenderToTexture.SetTexture(GLName, GL_TEXTURE_CUBE_MAP_POSITIVE_X); |
| 1892 |
RenderToTexture.GLContextOpen; |
| 1893 |
|
| 1894 |
{ Workaround for NVidia GeForce FX 5200 bug: |
| 1895 |
(Confirmed it's needed on Linux, both 32 and 64bit, Kambi's "kocury". |
| 1896 |
Confirmed it's *not* needed on Radeon (Linux 32bit, fglrx, Kambi's chantal).) |
| 1897 |
|
| 1898 |
Although TextureCubeMap_IncReference (glTextureCubeMap inside) already |
| 1899 |
called initial GenerateMipmap (for our InitialImage), it's not enough. |
| 1900 |
It seems that assigning texture to FBO destroys it's mipmaps (at least |
| 1901 |
their contents). |
| 1902 |
So you have to call GenerateMipmap *after* RenderToTexture.GLContextInit |
| 1903 |
before showing this texture, to recreate mipmaps. |
| 1904 |
|
| 1905 |
To see bug, comment below, and run view3scene on |
| 1906 |
demo_models/x3d/cubemap_generated_recursive.x3dv |
| 1907 |
Before pressing [space] (which triggers Update method), teapots |
| 1908 |
(or any other objects, reproducible with various IndexedFaceSets) |
| 1909 |
will have seemingly random mipmaps (base level 0 is Ok, RGB(255,0,255), |
| 1910 |
but mipmaps are seemingly filled with random garbage). |
| 1911 |
Line below fixes it at negligible cost (we'll generate mipmaps at loading |
| 1912 |
one more time than necessary). Other fix that works is to move |
| 1913 |
RenderToTexture.GLContextOpen to first Update call, but this obfuscates code. } |
| 1914 |
if (GLVersion.VendorType = gvNvidia) and NeedsMipmaps then |
| 1915 |
begin |
| 1916 |
RenderToTexture.CompleteTextureTarget := GL_TEXTURE_CUBE_MAP; |
| 1917 |
RenderToTexture.GenerateMipmap; |
| 1918 |
end; |
| 1919 |
end; |
| 1920 |
|
| 1921 |
procedure TGeneratedCubeMapTextureResource.UnprepareCore; |
| 1922 |
begin |
| 1923 |
FreeAndNil(RenderToTexture); |
| 1924 |
inherited; |
| 1925 |
end; |
| 1926 |
|
| 1927 |
procedure TGeneratedCubeMapTextureResource.Update( |
| 1928 |
const Render: TRenderFromViewFunction; |
| 1929 |
const ProjectionNear, ProjectionFar: Single; |
| 1930 |
const CubeMiddle: TVector3); |
| 1931 |
begin |
| 1932 |
if GLName = 0 then Exit; |
| 1933 |
|
| 1934 |
if GLVersion.BuggyGenerateCubeMap then Exit; |
| 1935 |
|
| 1936 |
GLCaptureCubeMapTexture(GLName, Size, |
| 1937 |
CubeMiddle, |
| 1938 |
Render, ProjectionNear, ProjectionFar, |
| 1939 |
RenderToTexture); |
| 1940 |
|
| 1941 |
if NeedsMipmaps then |
| 1942 |
RenderToTexture.GenerateMipmap; |
| 1943 |
end; |
| 1944 |
|
| 1945 |
{ T3DTextureResource ----------------------------------------------------------- } |
| 1946 |
|
| 1947 |
class function T3DTextureResource.IsClassForTextureNode( |
| 1948 |
ANode: TAbstractTextureNode): boolean; |
| 1949 |
begin |
| 1950 |
Result := ANode is TAbstractTexture3DNode; |
| 1951 |
end; |
| 1952 |
|
| 1953 |
function T3DTextureResource.TextureNode: TAbstractTexture3DNode; |
| 1954 |
begin |
| 1955 |
Result := TAbstractTexture3DNode(inherited TextureNode); |
| 1956 |
end; |
| 1957 |
|
| 1958 |
procedure T3DTextureResource.PrepareCore( |
| 1959 |
const RenderOptions: TCastleRenderOptions); |
| 1960 |
var |
| 1961 |
Filter: TTextureFilter; |
| 1962 |
Anisotropy: TGLfloat; |
| 1963 |
BoundaryModeS: TGLenum; |
| 1964 |
BoundaryModeT: TGLenum; |
| 1965 |
BoundaryModeR: TGLenum; |
| 1966 |
GUITexture: boolean; |
| 1967 |
TextureWrap: TTextureWrap3D; |
| 1968 |
begin |
| 1969 |
if not GLFeatures.Texture3D then |
| 1970 |
begin |
| 1971 |
WritelnWarning('VRML/X3D', 'Rendering context doesn''t support 3D textures, cannot use Texture3D nodes'); |
| 1972 |
Exit; |
| 1973 |
end; |
| 1974 |
|
| 1975 |
TextureNode.TextureLoaded := true; |
| 1976 |
|
| 1977 |
{ If TextureImage doesn't contain anything useful, just exit. |
| 1978 |
Setting TextureLoaded already did necessary WritelnWarnings. } |
| 1979 |
if TextureNode.TextureImage = nil then Exit; |
| 1980 |
|
| 1981 |
HandleTextureProperties(TextureNode.TextureProperties, RenderOptions, |
| 1982 |
TextureRepeatToGL(TextureNode.FdRepeatS.Value), TextureRepeatToGL(TextureNode.FdRepeatT.Value), |
| 1983 |
TextureRepeatToGL(TextureNode.FdRepeatR.Value), Filter, BoundaryModeS, BoundaryModeT, BoundaryModeR, Anisotropy, GUITexture); |
| 1984 |
|
| 1985 |
{ calculate TextureWrap } |
| 1986 |
TextureWrap.Data[0] := BoundaryModeS; |
| 1987 |
TextureWrap.Data[1] := BoundaryModeT; |
| 1988 |
TextureWrap.Data[2] := BoundaryModeR; |
| 1989 |
|
| 1990 |
GLName := RendererCache.Texture3D_IncReference( |
| 1991 |
{ TODO: We could implement TAbstractTexture3DNode.TextureUsedFullUrl } |
| 1992 |
'', |
| 1993 |
Filter, Anisotropy, |
| 1994 |
TextureWrap, TextureNode.TextureImage, TextureNode.TextureComposite); |
| 1995 |
end; |
| 1996 |
|
| 1997 |
procedure T3DTextureResource.UnprepareCore; |
| 1998 |
begin |
| 1999 |
if GLName <> 0 then |
| 2000 |
begin |
| 2001 |
RendererCache.Texture3D_DecReference(GLName); |
| 2002 |
GLName := 0; |
| 2003 |
end; |
| 2004 |
end; |
| 2005 |
|
| 2006 |
function T3DTextureResource.Bind(const TextureUnit: Cardinal): boolean; |
| 2007 |
begin |
| 2008 |
Result := GLName <> 0; |
| 2009 |
if not Result then Exit; |
| 2010 |
|
| 2011 |
TRenderer.ActiveTexture(TextureUnit); |
| 2012 |
glBindTexture(GL_TEXTURE_3D, GLName); |
| 2013 |
end; |
| 2014 |
|
| 2015 |
function T3DTextureResource.Enable(const TextureUnit: Cardinal; |
| 2016 |
Shader: TShader; const Env: TTextureEnv): boolean; |
| 2017 |
begin |
| 2018 |
Result := Bind(TextureUnit); |
| 2019 |
if not Result then Exit; |
| 2020 |
|
| 2021 |
Shader.EnableTexture(TextureUnit, tt3D, TextureNode, Env); |
| 2022 |
end; |
| 2023 |
|
| 2024 |
{ TGeneratedShadowMapResource ------------------------------------------------------ } |
| 2025 |
|
| 2026 |
class function TGeneratedShadowMapResource.IsClassForTextureNode( |
| 2027 |
ANode: TAbstractTextureNode): boolean; |
| 2028 |
begin |
| 2029 |
Result := ANode is TGeneratedShadowMapNode; |
| 2030 |
end; |
| 2031 |
|
| 2032 |
function TGeneratedShadowMapResource.TextureNode: TGeneratedShadowMapNode; |
| 2033 |
begin |
| 2034 |
Result := TGeneratedShadowMapNode(inherited TextureNode); |
| 2035 |
end; |
| 2036 |
|
| 2037 |
class function TGeneratedShadowMapResource.ClassVarianceShadowMaps( |
| 2038 |
RenderOptions: TCastleRenderOptions): boolean; |
| 2039 |
begin |
| 2040 |
Result := |
| 2041 |
(RenderOptions.ShadowSampling = ssVarianceShadowMaps) and |
| 2042 |
GLFeatures.Shaders and |
| 2043 |
GLFeatures.TextureFloat; |
| 2044 |
end; |
| 2045 |
|
| 2046 |
procedure TGeneratedShadowMapResource.PrepareCore( |
| 2047 |
const RenderOptions: TCastleRenderOptions); |
| 2048 |
var |
| 2049 |
TextureWrap: TTextureWrap2D; |
| 2050 |
Filter: TTextureFilter; |
| 2051 |
begin |
| 2052 |
// in fixed-function, do not create shadow maps, do not render to shadow maps |
| 2053 |
if GLFeatures.EnableFixedFunction then Exit; |
| 2054 |
|
| 2055 |
{ Float texture extensions: there is also NV_float_buffer, |
| 2056 |
which is the only way for a float texture on old nvidia GPUs. |
| 2057 |
But it's only for NV_texture_rectangle, without bilinear filtering |
| 2058 |
or mipmapping, so it's not useful for VSM at all. } |
| 2059 |
|
| 2060 |
VarianceShadowMaps := ClassVarianceShadowMaps(RenderOptions); |
| 2061 |
|
| 2062 |
WritelnLog('Shadows', Format('Variance Shadow Maps used: %s (Reasons: RenderOptions.Shadow sampling = %s, GLSL support = %s, texture_float support = %s)', [ |
| 2063 |
BoolToStr(VarianceShadowMaps, true), |
| 2064 |
ShadowSamplingNames[RenderOptions.ShadowSampling], |
| 2065 |
BoolToStr(GLFeatures.Shaders, true), |
| 2066 |
BoolToStr(GLFeatures.TextureFloat, true) |
| 2067 |
])); |
| 2068 |
|
| 2069 |
{ TODO: fix TextureNode.FdSize.Value if needed } |
| 2070 |
Size := TextureNode.FdSize.Value; |
| 2071 |
|
| 2072 |
{ At least in case of float textures for VSM, CLAMP_TO_EDGE is needed |
| 2073 |
(instead of standard clamp to border). Didn't see any difference |
| 2074 |
for depth textures. } |
| 2075 |
TextureWrap.Data[0] := GLFeatures.CLAMP_TO_EDGE; |
| 2076 |
TextureWrap.Data[1] := GLFeatures.CLAMP_TO_EDGE; |
| 2077 |
|
| 2078 |
{ calculate Filter, NeedsMipmaps } |
| 2079 |
Filter.Magnification := magLinear; |
| 2080 |
if VarianceShadowMaps and HasGenerateMipmap then |
| 2081 |
begin |
| 2082 |
NeedsMipmaps := true; |
| 2083 |
Filter.Minification := minLinearMipmapLinear; |
| 2084 |
end else |
| 2085 |
begin |
| 2086 |
NeedsMipmaps := false; |
| 2087 |
Filter.Minification := minLinear; |
| 2088 |
end; |
| 2089 |
|
| 2090 |
if TextureNode.CompareMode <> smCompareRLEqual then |
| 2091 |
WritelnWarning('VRML/X3D', 'Only the standard GeneratedShadowMap.compareMode value, "COMPARE_R_LEQUAL" (in Pascal: smCompareRLEqual), is supported now. This is the only value sensible for shadow maps.'); |
| 2092 |
|
| 2093 |
if VarianceShadowMaps then |
| 2094 |
begin |
| 2095 |
FGLName := RendererCache.TextureFloat_IncReference( |
| 2096 |
'' { generated texture contents means empty URL }, |
| 2097 |
Filter, TextureWrap, Size, Size, true); |
| 2098 |
end else |
| 2099 |
begin |
| 2100 |
if not GLFeatures.TextureDepthCompare then |
| 2101 |
begin |
| 2102 |
WritelnWarning('VRML/X3D', 'Shadow maps not supported. OpenGL(ES) doesn''t support shadow samplers with comparison (TextureDepthCompare), cannot use GeneratedShadowMap nodes.'); |
| 2103 |
Exit; |
| 2104 |
end; |
| 2105 |
|
| 2106 |
FGLName := RendererCache.TextureDepth_IncReference( |
| 2107 |
'' { generated texture contents means empty URL }, |
| 2108 |
TextureWrap, Size, Size); |
| 2109 |
end; |
| 2110 |
|
| 2111 |
RenderToTexture := TGLRenderToTexture.Create(Size, Size); |
| 2112 |
RenderToTexture.SetTexture(FGLName, GL_TEXTURE_2D); |
| 2113 |
if VarianceShadowMaps then |
| 2114 |
RenderToTexture.Buffer := tbColor |
| 2115 |
else |
| 2116 |
RenderToTexture.Buffer := tbDepth; |
| 2117 |
RenderToTexture.Stencil := false; |
| 2118 |
RenderToTexture.GLContextOpen; |
| 2119 |
end; |
| 2120 |
|
| 2121 |
procedure TGeneratedShadowMapResource.UnprepareCore; |
| 2122 |
begin |
| 2123 |
FreeAndNil(RenderToTexture); |
| 2124 |
|
| 2125 |
if FGLName <> 0 then |
| 2126 |
begin |
| 2127 |
if VarianceShadowMaps then |
| 2128 |
RendererCache.TextureFloat_DecReference(FGLName) |
| 2129 |
else |
| 2130 |
RendererCache.TextureDepth_DecReference(FGLName); |
| 2131 |
FGLName := 0; |
| 2132 |
end; |
| 2133 |
end; |
| 2134 |
|
| 2135 |
function TGeneratedShadowMapResource.Bind(const TextureUnit: Cardinal): boolean; |
| 2136 |
begin |
| 2137 |
Result := FGLName <> 0; |
| 2138 |
if not Result then Exit; |
| 2139 |
|
| 2140 |
TRenderer.ActiveTexture(TextureUnit); |
| 2141 |
glBindTexture(GL_TEXTURE_2D, FGLName); |
| 2142 |
end; |
| 2143 |
|
| 2144 |
function TGeneratedShadowMapResource.Enable(const TextureUnit: Cardinal; |
| 2145 |
Shader: TShader; const Env: TTextureEnv): boolean; |
| 2146 |
begin |
| 2147 |
Result := Bind(TextureUnit); |
| 2148 |
if not Result then Exit; |
| 2149 |
|
| 2150 |
if not WarninigDoneCannotEnable then |
| 2151 |
begin |
| 2152 |
WarninigDoneCannotEnable := true; |
| 2153 |
WritelnWarning('Not supported anymore: GeneratedShadowMap placed directly within Appearance.textures of a shadow receiver. Use instead Appearance.receiveShadows or light.shadows to activate shadow maps.'); |
| 2154 |
end; |
| 2155 |
end; |
| 2156 |
|
| 2157 |
procedure TGeneratedShadowMapResource.Update( |
| 2158 |
const Render: TRenderFromViewFunction; |
| 2159 |
const ProjectionNear, ProjectionFar: Single; |
| 2160 |
const Light: TAbstractPunctualLightNode); |
| 2161 |
var |
| 2162 |
NewProjectionMatrix, SavedProjectionMatrix: TMatrix4; |
| 2163 |
SavedViewport: TRectangle; |
| 2164 |
Camera: TRenderingCamera; |
| 2165 |
SavedPolygonOffset: TPolygonOffset; |
| 2166 |
CameraRender: TViewVectors; |
| 2167 |
begin |
| 2168 |
if FGLName = 0 then Exit; |
| 2169 |
|
| 2170 |
{ Render view for shadow map } |
| 2171 |
NewProjectionMatrix := Light.ProjectionMatrix; |
| 2172 |
|
| 2173 |
Camera := TRenderingCamera.Create; |
| 2174 |
try |
| 2175 |
if VarianceShadowMaps then |
| 2176 |
Camera.Target := rtVarianceShadowMap |
| 2177 |
else |
| 2178 |
Camera.Target := rtShadowMap; |
| 2179 |
|
| 2180 |
Light.GetView(CameraRender); |
| 2181 |
Camera.FromViewVectors(CameraRender, NewProjectionMatrix); |
| 2182 |
|
| 2183 |
RenderToTexture.RenderBegin; |
| 2184 |
|
| 2185 |
SavedViewport := RenderContext.Viewport; |
| 2186 |
RenderContext.Viewport := Rectangle(0, 0, Size, Size); |
| 2187 |
|
| 2188 |
SavedProjectionMatrix := RenderContext.ProjectionMatrix; |
| 2189 |
RenderContext.ProjectionMatrix := NewProjectionMatrix; |
| 2190 |
|
| 2191 |
{ Enable polygon offset for everything (whole scene). |
| 2192 |
|
| 2193 |
VarianceShadowMaps notes: can offset be avoided in this case? |
| 2194 |
Practice (on "ATI Mobility Radeon HD 4300 Series") shows that |
| 2195 |
offset is still needed for VSM. Without offset, VSM has some noise, |
| 2196 |
and sunny_street demos have noise on walls. With offset, VSM is perfect. } |
| 2197 |
SavedPolygonOffset := RenderContext.PolygonOffset; |
| 2198 |
RenderContext.PolygonOffsetEnable(TextureNode.FdScale.Value, TextureNode.FdBias.Value); |
| 2199 |
|
| 2200 |
Render(Camera); |
| 2201 |
|
| 2202 |
RenderContext.PolygonOffset := SavedPolygonOffset; |
| 2203 |
RenderContext.ProjectionMatrix := SavedProjectionMatrix; |
| 2204 |
RenderContext.Viewport := SavedViewport; |
| 2205 |
|
| 2206 |
RenderToTexture.RenderEnd; |
| 2207 |
|
| 2208 |
finally FreeAndNil(Camera) end; |
| 2209 |
|
| 2210 |
if NeedsMipmaps then |
| 2211 |
RenderToTexture.GenerateMipmap; |
| 2212 |
end; |
| 2213 |
|
| 2214 |
function TGeneratedShadowMapResource.GLName: TGLTextureId; |
| 2215 |
begin |
| 2216 |
Result := FGLName; |
| 2217 |
end; |
| 2218 |
|
| 2219 |
{ TShaderTextureResource ------------------------------------------------------ } |
| 2220 |
|
| 2221 |
class function TShaderTextureResource.IsClassForTextureNode( |
| 2222 |
ANode: TAbstractTextureNode): boolean; |
| 2223 |
begin |
| 2224 |
Result := ANode is TShaderTextureNode; |
| 2225 |
end; |
| 2226 |
|
| 2227 |
procedure TShaderTextureResource.PrepareCore( |
| 2228 |
const RenderOptions: TCastleRenderOptions); |
| 2229 |
begin |
| 2230 |
{ no need to do anything } |
| 2231 |
end; |
| 2232 |
|
| 2233 |
procedure TShaderTextureResource.UnprepareCore; |
| 2234 |
begin |
| 2235 |
{ no need to do anything } |
| 2236 |
end; |
| 2237 |
|
| 2238 |
function TShaderTextureResource.Bind(const TextureUnit: Cardinal): boolean; |
| 2239 |
begin |
| 2240 |
Result := true; |
| 2241 |
{ no need to actually do anything } |
| 2242 |
|
| 2243 |
{ TODO: ShaderTexture should not need to increase the TextureUnit. |
| 2244 |
It's not loaded to OpenGL, so it doesn't take the multitexture slot. |
| 2245 |
For now we increase it, only to pass texture coords to it. } |
| 2246 |
end; |
| 2247 |
|
| 2248 |
function TShaderTextureResource.Enable(const TextureUnit: Cardinal; |
| 2249 |
Shader: TShader; const Env: TTextureEnv): boolean; |
| 2250 |
begin |
| 2251 |
Result := Bind(TextureUnit); |
| 2252 |
if not Result then Exit; |
| 2253 |
|
| 2254 |
Shader.EnableTexture(TextureUnit, ttShader, TextureNode, Env); |
| 2255 |
end; |
| 2256 |
|
| 2257 |
function TShaderTextureResource.TextureNode: TShaderTextureNode; |
| 2258 |
begin |
| 2259 |
Result := TShaderTextureNode(inherited TextureNode); |
| 2260 |
end; |
| 2261 |
|
| 2262 |
{$endif read_implementation} |