| 1 |
{%MainUnit castleinternalrenderer.pas} |
| 2 |
{ |
| 3 |
Copyright 2002-2023 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 |
{ Mesh rendering. |
| 18 |
This is internal for CastleInternalRenderer unit and |
| 19 |
used by TRenderer. |
| 20 |
|
| 21 |
- This was split from TRenderer, first of all to make |
| 22 |
TRenderer smaller. |
| 23 |
TRenderer class is already large and complicated. |
| 24 |
|
| 25 |
- TMeshRenderer and descendants are created |
| 26 |
only for the time of one RenderShape call. |
| 27 |
The allows to reason about them easier. |
| 28 |
|
| 29 |
- Note: Avoid abusing the fact that this is in the same unit |
| 30 |
as TRenderer, avoid accessing TRenderer private stuff. |
| 31 |
The rendering classes are already complicated, |
| 32 |
keeping them cleanly separated makes it easier to reason about them. |
| 33 |
} |
| 34 |
|
| 35 |
type |
| 36 |
{ Base abstract class for rendering using VBO, shaders --- the normal |
| 37 |
OpenGL stuff. } |
| 38 |
TMeshRenderer = class |
| 39 |
private |
| 40 |
FRenderer: TRenderer; |
| 41 |
FShape: TX3DRendererShape; |
| 42 |
FGeometry: TAbstractGeometryNode; |
| 43 |
FState: TX3DGraphTraverseState; |
| 44 |
protected |
| 45 |
{ Current rendering properties, constant for the whole |
| 46 |
lifetime of the renderer, set in constructor. } |
| 47 |
property Renderer: TRenderer read FRenderer; |
| 48 |
property Shape: TX3DRendererShape read FShape; |
| 49 |
property Geometry: TAbstractGeometryNode read FGeometry; |
| 50 |
property State: TX3DGraphTraverseState read FState; |
| 51 |
public |
| 52 |
PrepareRenderShape: Boolean; |
| 53 |
|
| 54 |
CurrentMeshShader: TX3DShaderProgramBase; |
| 55 |
|
| 56 |
{ Complete transformation from shape local coordinates to camera shape: |
| 57 |
Camera transformation * scene transformation * shape transformation. } |
| 58 |
ShapeModelView: TMatrix4; |
| 59 |
|
| 60 |
RenderOptions: TCastleRenderOptions; |
| 61 |
|
| 62 |
{ Custom shader to use, if non-nil: |
| 63 |
Overrides whatever shaders have been determined |
| 64 |
for the rendering (built-in CGE or from X3D file). |
| 65 |
In practice, used now to set shaders when generating shadow maps. } |
| 66 |
CustomShaders: TCustomShaders; |
| 67 |
|
| 68 |
constructor Create(ARenderer: TRenderer; AShape: TX3DRendererShape); |
| 69 |
|
| 70 |
{ Render the whole geometry. |
| 71 |
|
| 72 |
For some details what is, and what is not yet set in OpenGL |
| 73 |
before this, you have to see TVRMLOpenGLRender.RenderShape |
| 74 |
implementation. |
| 75 |
|
| 76 |
Material for VRML 1.0: currently bound material is zero. |
| 77 |
You can set Arrays.Color inside for VRML 1.0 material changes inside. |
| 78 |
|
| 79 |
Texture: you only have to generate texture coordinates in descendants. |
| 80 |
For all texture units < TexCoordsNeeded. (For other tex units, |
| 81 |
do not generate texture coords, |
| 82 |
they may not be even declared in shader code.) |
| 83 |
Do not change here the bound texture or operate on texture matrix |
| 84 |
or texture enabled state. } |
| 85 |
procedure Render; virtual; abstract; |
| 86 |
end; |
| 87 |
|
| 88 |
{ Base abstract class for rendering nodes with explicit "coord" field. |
| 89 |
(Both IndexedXxx and not indexed). Uses TArraysGenerator |
| 90 |
instance to generate TGeometryArrays contents, then renders them. } |
| 91 |
TBaseCoordinateRenderer = class(TMeshRenderer) |
| 92 |
private |
| 93 |
{ Available inside RenderCoordinate* } |
| 94 |
Arrays: TGeometryArrays; |
| 95 |
function PerVertexColor: Boolean; |
| 96 |
protected |
| 97 |
{ Override these to render contents of Arrays. |
| 98 |
These are all called only when Coord is assigned. |
| 99 |
RenderCoordinateBegin, RenderCoordinateEnd are called around actual |
| 100 |
vertex arrays rendering. |
| 101 |
@groupBegin } |
| 102 |
procedure RenderCoordinateBegin; virtual; |
| 103 |
procedure RenderCoordinateEnd; virtual; |
| 104 |
{ @groupEnd } |
| 105 |
public |
| 106 |
{ Set this to non-zero to use this VBO during rendering, |
| 107 |
assuming Arrays contents are already loaded there. |
| 108 |
In this case, Arrays data must be already freed |
| 109 |
(by TGeometryArrays.FreeData). } |
| 110 |
Vbo: TVboArrays; |
| 111 |
|
| 112 |
Vao: TVertexArrayObject; |
| 113 |
|
| 114 |
{ You have to set this before calling Render, to enable proper shading. } |
| 115 |
Shader: TShader; |
| 116 |
|
| 117 |
{ Used (and updated) to the number of OpenGL texture units reserved. } |
| 118 |
BoundTextureUnits: Cardinal; |
| 119 |
|
| 120 |
Lighting: boolean; |
| 121 |
|
| 122 |
{ Don't override this, it is already overridden here |
| 123 |
to do everything necessary. } |
| 124 |
procedure Render; override; |
| 125 |
end; |
| 126 |
|
| 127 |
TTextureCoordinateRenderer = class(TBaseCoordinateRenderer) |
| 128 |
{$ifndef OpenGLES} |
| 129 |
private |
| 130 |
RestoreDefaultTexCoord: boolean; |
| 131 |
{$endif} |
| 132 |
protected |
| 133 |
procedure RenderCoordinateBegin; override; |
| 134 |
procedure RenderCoordinateEnd; override; |
| 135 |
end; |
| 136 |
|
| 137 |
TFogCoordinateRenderer = class(TTextureCoordinateRenderer) |
| 138 |
protected |
| 139 |
procedure RenderCoordinateBegin; override; |
| 140 |
procedure RenderCoordinateEnd; override; |
| 141 |
end; |
| 142 |
|
| 143 |
TColorPerVertexCoordinateRenderer = class(TFogCoordinateRenderer) |
| 144 |
{$ifndef OpenGLES} |
| 145 |
private |
| 146 |
MaterialFromColorEnabled: boolean; |
| 147 |
{$endif} |
| 148 |
protected |
| 149 |
procedure RenderCoordinateBegin; override; |
| 150 |
procedure RenderCoordinateEnd; override; |
| 151 |
end; |
| 152 |
|
| 153 |
TShaderCoordinateRenderer = class(TColorPerVertexCoordinateRenderer) |
| 154 |
protected |
| 155 |
procedure RenderCoordinateBegin; override; |
| 156 |
end; |
| 157 |
|
| 158 |
TCompleteCoordinateRenderer = TShaderCoordinateRenderer; |
| 159 |
|
| 160 |
{ TMeshRenderer ---------------------------------------------------------- } |
| 161 |
|
| 162 |
constructor TMeshRenderer.Create(ARenderer: TRenderer; |
| 163 |
AShape: TX3DRendererShape); |
| 164 |
begin |
| 165 |
inherited Create; |
| 166 |
|
| 167 |
FRenderer := ARenderer; |
| 168 |
FShape := AShape; |
| 169 |
FGeometry := AShape.Geometry; |
| 170 |
FState := AShape.State; |
| 171 |
end; |
| 172 |
|
| 173 |
{ TBaseCoordinateRenderer ------------------------------------------------ } |
| 174 |
|
| 175 |
procedure TBaseCoordinateRenderer.Render; |
| 176 |
var |
| 177 |
Attribs: TGLSLAttributeList; |
| 178 |
|
| 179 |
procedure EnableVertexAttrib(GLSLProgram: TGLSLProgram); |
| 180 |
var |
| 181 |
I: Integer; |
| 182 |
A: TGLSLAttribute; |
| 183 |
begin |
| 184 |
if Attribs = nil then |
| 185 |
Attribs := TGLSLAttributeList.Create; |
| 186 |
for I := 0 to Arrays.Attribs.Count - 1 do |
| 187 |
begin |
| 188 |
A := GLSLProgram.AttributeOptional(Arrays.Attribs[I].Name); |
| 189 |
if A.Location = -1 then |
| 190 |
begin |
| 191 |
{ Do not make warnings about missing internal attributes |
| 192 |
(for the same reason as uniform values, see GLRendererShader). } |
| 193 |
if not Arrays.Attribs[I].Internal then |
| 194 |
WritelnWarning('VRML/X3D', 'Shader attribute "' + Arrays.Attribs[I].Name + '" not found (or not used)'); |
| 195 |
Continue; |
| 196 |
end; |
| 197 |
case Arrays.Attribs[I].AType of |
| 198 |
atFloat : A.EnableArraySingle (Vao, Arrays.AttributeSize, Arrays.GLSLAttribute(Arrays.Attribs[I])); |
| 199 |
|
| 200 |
atVector2: A.EnableArrayVector2(Vao, Arrays.AttributeSize, Arrays.GLSLAttribute(Arrays.Attribs[I])); |
| 201 |
atVector3: A.EnableArrayVector3(Vao, Arrays.AttributeSize, Arrays.GLSLAttribute(Arrays.Attribs[I])); |
| 202 |
atVector4: A.EnableArrayVector4(Vao, Arrays.AttributeSize, Arrays.GLSLAttribute(Arrays.Attribs[I])); |
| 203 |
|
| 204 |
atMatrix3: A.EnableArrayMatrix3(Vao, Arrays.AttributeSize, Arrays.GLSLAttribute(Arrays.Attribs[I])); |
| 205 |
atMatrix4: A.EnableArrayMatrix4(Vao, Arrays.AttributeSize, Arrays.GLSLAttribute(Arrays.Attribs[I])); |
| 206 |
|
| 207 |
{$ifndef COMPILER_CASE_ANALYSIS} |
| 208 |
else raise EInternalError.Create('Arrays.Attribs[I].AType?'); |
| 209 |
{$endif} |
| 210 |
end; |
| 211 |
Attribs.Add(A); |
| 212 |
end; |
| 213 |
end; |
| 214 |
|
| 215 |
procedure DisableArrays; |
| 216 |
var |
| 217 |
I: Integer; |
| 218 |
begin |
| 219 |
if CurrentMeshShader = nil then |
| 220 |
begin |
| 221 |
{$ifndef OpenGLES} |
| 222 |
{ disable fixed-function parameters } |
| 223 |
glDisableClientState(GL_VERTEX_ARRAY); |
| 224 |
glDisableClientState(GL_NORMAL_ARRAY); |
| 225 |
|
| 226 |
if RenderOptions.Mode = rmFull then |
| 227 |
glDisableClientState(GL_COLOR_ARRAY); |
| 228 |
|
| 229 |
{ Note: For simplicity, we no longer support fog coordinate in fixed-function. |
| 230 |
We avoid extra alternative code path for GL_EXT_fog_coord or GL_version_1_4 that made it core. |
| 231 |
if Arrays.HasFogCoord and GLFeatures.EXT_fog_coord then |
| 232 |
glDisableClientState(GL_FOG_COORDINATE_ARRAY_EXT); |
| 233 |
} |
| 234 |
|
| 235 |
if GLFeatures.UseMultiTexturing then |
| 236 |
begin |
| 237 |
for I := 0 to Arrays.TexCoords.Count - 1 do |
| 238 |
if (Arrays.TexCoords[I] <> nil) and |
| 239 |
(Arrays.TexCoords[I].Generation = tgExplicit) then |
| 240 |
begin |
| 241 |
glClientActiveTexture(GL_TEXTURE0 + I); |
| 242 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 243 |
end; |
| 244 |
end else |
| 245 |
if (Arrays.TexCoords.Count <> 0) and |
| 246 |
(Arrays.TexCoords[0] <> nil) and |
| 247 |
(Arrays.TexCoords[0].Generation = tgExplicit) then |
| 248 |
begin |
| 249 |
glDisableClientState(GL_TEXTURE_COORD_ARRAY); |
| 250 |
end; |
| 251 |
{$endif} |
| 252 |
end; |
| 253 |
|
| 254 |
if Attribs <> nil then |
| 255 |
begin |
| 256 |
for I := 0 to Attribs.Count - 1 do |
| 257 |
Attribs[I].DisableArray; |
| 258 |
FreeAndNil(Attribs); |
| 259 |
end; |
| 260 |
end; |
| 261 |
|
| 262 |
{ Call one of MaterialXxx, to pass shader uniforms suitable |
| 263 |
for the current lighting model. } |
| 264 |
procedure FinalizeLightingModel; |
| 265 |
|
| 266 |
{ Pass shader uniforms for Phong calculation. } |
| 267 |
procedure MaterialPhong(const M: TPhongMaterialInfo); |
| 268 |
begin |
| 269 |
Assert(Shader.LightingModel = lmPhong); // should be set by InitializeLightingModel |
| 270 |
|
| 271 |
CurrentMeshShader.UniformCastle_MaterialDiffuseAlpha.SetValue(Vector4(M.DiffuseColor, M.Opacity)); |
| 272 |
CurrentMeshShader.UniformCastle_MaterialShininess.SetValue(M.ShininessExp); |
| 273 |
CurrentMeshShader.UniformCastle_MaterialAmbient.SetValue(M.AmbientColor); |
| 274 |
CurrentMeshShader.UniformCastle_MaterialSpecular.SetValue(M.SpecularColor); |
| 275 |
CurrentMeshShader.UniformCastle_MaterialEmissive.SetValue(M.EmissiveColor); |
| 276 |
CurrentMeshShader.UniformCastle_GlobalAmbient.SetValue(RenderContext.GlobalAmbient); |
| 277 |
|
| 278 |
{ castle_UnlitColor is used when we calculated Lighting = false |
| 279 |
(in castlerenderer_materials.inc) and material is TPhongMaterialInfo. |
| 280 |
This means that TPhongMaterialInfo has PureEmissive. |
| 281 |
(Note: the case when Lighting = false because of |
| 282 |
RenderOptions.Lighting = false was covered earlier, |
| 283 |
instead of calling MaterialPhongInitialize). } |
| 284 |
CurrentMeshShader.UniformCastle_UnlitColor.SetValue(Vector4(M.EmissiveColor, M.Opacity)); |
| 285 |
end; |
| 286 |
|
| 287 |
{ Pass shader uniforms for Physical (PBR) lighting calculation. } |
| 288 |
procedure MaterialPhysical(const M: TPhysicalMaterialInfo); |
| 289 |
begin |
| 290 |
Assert(Shader.LightingModel = lmPhysical); // should be set by InitializeLightingModel |
| 291 |
CurrentMeshShader.UniformCastle_MaterialBaseAlpha.SetValue(Vector4(M.BaseColor, M.Opacity)); |
| 292 |
CurrentMeshShader.UniformCastle_MaterialEmissive.SetValue(M.EmissiveColor); |
| 293 |
CurrentMeshShader.UniformCastle_MaterialMetallic.SetValue(M.Metallic); |
| 294 |
CurrentMeshShader.UniformCastle_MaterialRoughness.SetValue(M.Roughness); |
| 295 |
end; |
| 296 |
|
| 297 |
{ Pass shader uniforms for unlit lighting calculation. } |
| 298 |
procedure MaterialUnlit(const M: TUnlitMaterialInfo); |
| 299 |
begin |
| 300 |
Assert(Shader.LightingModel = lmUnlit); // should be set by InitializeLightingModel |
| 301 |
CurrentMeshShader.UniformCastle_MaterialEmissiveAlpha.SetValue(Vector4(M.EmissiveColor, M.Opacity)); |
| 302 |
end; |
| 303 |
|
| 304 |
{ Pass shader uniforms for unlit (with explicit color) lighting calculation. } |
| 305 |
procedure MaterialUnlitExplicit(const Color: TCastleColor); |
| 306 |
begin |
| 307 |
Assert(Shader.LightingModel = lmUnlit); // should be set by InitializeLightingModel |
| 308 |
CurrentMeshShader.UniformCastle_MaterialEmissiveAlpha.SetValue(Color); |
| 309 |
end; |
| 310 |
|
| 311 |
begin |
| 312 |
if CustomShaders <> nil then |
| 313 |
// right now CustomShaders <> nil means "rendering to shadow map", so no need for any colors |
| 314 |
MaterialUnlitExplicit(White) |
| 315 |
else |
| 316 |
if RenderOptions.Mode = rmSolidColor then |
| 317 |
{ In case of rmSolidColor, force using unlit with RenderOptions.SolidColor. |
| 318 |
Note that castlerenderer_materials.inc made sure that Lightning = false |
| 319 |
in case of rmSolidColor, so we know it is rendered with LIT undefined. } |
| 320 |
MaterialUnlitExplicit(Vector4(RenderOptions.SolidColor, 1)) |
| 321 |
else |
| 322 |
if (not RenderOptions.Lighting) and (Shader.Material <> nil) then |
| 323 |
{ In case we have a material, but user requested RenderOptions.Lighting = false, |
| 324 |
use MainColor. Otherwise we would e.g. use EmissiveColor in case of Phong, |
| 325 |
which would usually be black. |
| 326 |
RenderOptions.Lighting = false is a debug rendering anyway, not covered |
| 327 |
by X3D spec equations. } |
| 328 |
MaterialUnlitExplicit(Vector4(Shader.Material.MainColor, Shader.Material.Opacity)) |
| 329 |
else |
| 330 |
if Arrays.ForceUnlit then |
| 331 |
MaterialUnlitExplicit(Arrays.ForcedUnlitColor) |
| 332 |
else |
| 333 |
if Shader.Material is TPhongMaterialInfo then |
| 334 |
MaterialPhong(TPhongMaterialInfo(Shader.Material)) |
| 335 |
else |
| 336 |
if Shader.Material is TPhysicalMaterialInfo then |
| 337 |
MaterialPhysical(TPhysicalMaterialInfo(Shader.Material)) |
| 338 |
else |
| 339 |
if Shader.Material is TUnlitMaterialInfo then |
| 340 |
MaterialUnlit(TUnlitMaterialInfo(Shader.Material)) |
| 341 |
else |
| 342 |
{ Unlit white is used when material = nil. } |
| 343 |
MaterialUnlitExplicit(White); |
| 344 |
end; |
| 345 |
|
| 346 |
const |
| 347 |
ToMode: array [TGeometryPrimitive] of TGLenum = ( |
| 348 |
GL_TRIANGLES, |
| 349 |
GL_TRIANGLE_FAN, |
| 350 |
GL_TRIANGLE_STRIP, |
| 351 |
GL_LINE_STRIP, |
| 352 |
GL_LINE_LOOP, |
| 353 |
GL_LINES, |
| 354 |
GL_POINTS |
| 355 |
); |
| 356 |
var |
| 357 |
I: Integer; |
| 358 |
First: Cardinal; |
| 359 |
Mode: TGLenum; |
| 360 |
TexCoordAttrib: TGLSLAttribute; |
| 361 |
begin |
| 362 |
Attribs := nil; |
| 363 |
|
| 364 |
RenderCoordinateBegin; |
| 365 |
try |
| 366 |
if not PrepareRenderShape then |
| 367 |
begin |
| 368 |
{ Initialize vertex arrays that we we will use with indexed nodes. } |
| 369 |
if Vbo[vtCoordinate] <> 0 then |
| 370 |
RenderContext.BindBuffer[btArray] := Vbo[vtCoordinate]; |
| 371 |
|
| 372 |
{$ifdef OpenGLES} |
| 373 |
Assert(CurrentMeshShader <> nil, 'OpenGL ES 2.0 rendering must go through shaders'); |
| 374 |
{$else} |
| 375 |
if not GLFeatures.EnableFixedFunction then |
| 376 |
Assert(CurrentMeshShader <> nil, 'All rendering must go through shaders when GLFeatures.EnableFixedFunction = false'); |
| 377 |
{$endif} |
| 378 |
|
| 379 |
if CurrentMeshShader <> nil then |
| 380 |
begin |
| 381 |
CurrentMeshShader.UniformCastle_ModelViewMatrix.SetValue(ShapeModelView); |
| 382 |
CurrentMeshShader.UniformCastle_ProjectionMatrix.SetValue(RenderContext.ProjectionMatrix); |
| 383 |
if Shader.NeedsNormals then |
| 384 |
CurrentMeshShader.UniformCastle_NormalMatrix.SetValue(ModelViewToNormalMatrix(ShapeModelView)); |
| 385 |
|
| 386 |
FinalizeLightingModel; |
| 387 |
|
| 388 |
Attribs := TGLSLAttributeList.Create; |
| 389 |
|
| 390 |
CurrentMeshShader.AttributeCastle_Vertex.EnableArrayVector3(Vao, Arrays.CoordinateSize, PtrUInt(Arrays.Position)); |
| 391 |
Attribs.Add(CurrentMeshShader.AttributeCastle_Vertex); |
| 392 |
|
| 393 |
if Shader.NeedsNormals then |
| 394 |
begin |
| 395 |
CurrentMeshShader.AttributeCastle_Normal.EnableArrayVector3(Vao, Arrays.CoordinateSize, PtrUInt(Arrays.Normal)); |
| 396 |
Attribs.Add(CurrentMeshShader.AttributeCastle_Normal); |
| 397 |
end; |
| 398 |
|
| 399 |
if Arrays.HasTangent then |
| 400 |
begin |
| 401 |
CurrentMeshShader.AttributeCastle_Tangent.EnableArrayVector3(Vao, Arrays.CoordinateSize, PtrUInt(Arrays.Tangent)); |
| 402 |
Attribs.Add(CurrentMeshShader.AttributeCastle_Tangent); |
| 403 |
end; |
| 404 |
end else |
| 405 |
begin |
| 406 |
{ set fixed-function parameters } |
| 407 |
{$ifndef OpenGLES} |
| 408 |
glEnableClientState(GL_VERTEX_ARRAY); |
| 409 |
glVertexPointer(3, GL_FLOAT, Arrays.CoordinateSize, Arrays.Position); |
| 410 |
|
| 411 |
if Shader.NeedsNormals then |
| 412 |
begin |
| 413 |
glEnableClientState(GL_NORMAL_ARRAY); |
| 414 |
glNormalPointer(GL_FLOAT, Arrays.CoordinateSize, Arrays.Normal); |
| 415 |
end; |
| 416 |
{$endif not OpenGLES} |
| 417 |
end; |
| 418 |
|
| 419 |
if Vbo[vtAttribute] <> 0 then |
| 420 |
RenderContext.BindBuffer[btArray] := Vbo[vtAttribute]; |
| 421 |
|
| 422 |
if CurrentMeshShader <> nil then |
| 423 |
begin |
| 424 |
if PerVertexColor then |
| 425 |
begin |
| 426 |
Assert(Arrays.ColorType <> ctNone); |
| 427 |
case Arrays.ColorType of |
| 428 |
ctRgb : CurrentMeshShader.AttributeCastle_ColorPerVertex.EnableArrayVector3(Vao, Arrays.AttributeSize, PtrUInt(Arrays.ColorRgb)); |
| 429 |
ctRgbAlpha: CurrentMeshShader.AttributeCastle_ColorPerVertex.EnableArrayVector4(Vao, Arrays.AttributeSize, PtrUInt(Arrays.ColorRgbAlpha)); |
| 430 |
else ; |
| 431 |
end; |
| 432 |
Attribs.Add(CurrentMeshShader.AttributeCastle_ColorPerVertex); |
| 433 |
end; |
| 434 |
|
| 435 |
if Arrays.HasFogCoord then |
| 436 |
begin |
| 437 |
CurrentMeshShader.AttributeCastle_FogCoord.EnableArraySingle(Vao, Arrays.AttributeSize, PtrUInt(Arrays.FogCoord)); |
| 438 |
Attribs.Add(CurrentMeshShader.AttributeCastle_FogCoord); |
| 439 |
end; |
| 440 |
|
| 441 |
for I := 0 to Arrays.TexCoords.Count - 1 do |
| 442 |
if (Arrays.TexCoords[I] <> nil) and |
| 443 |
(Arrays.TexCoords[I].Generation = tgExplicit) then |
| 444 |
begin |
| 445 |
// TODO: would be faster to not query GLSLProgram.Attribute every frame, |
| 446 |
// but save it in GLSLProgram. |
| 447 |
TexCoordAttrib := CurrentMeshShader.AttributeOptional('castle_MultiTexCoord' + IntToStr(I)); |
| 448 |
if TexCoordAttrib.Location = -1 then Continue; |
| 449 |
TexCoordAttrib.EnableArray(Vao, 0, Arrays.TexCoords[I].Dimensions, GL_FLOAT, GL_FALSE, |
| 450 |
Arrays.AttributeSize, PtrUInt(Arrays.TexCoord(I, 0))); |
| 451 |
Attribs.Add(TexCoordAttrib); |
| 452 |
end; |
| 453 |
end else |
| 454 |
begin |
| 455 |
{ set fixed-function parameters } |
| 456 |
{$ifndef OpenGLES} |
| 457 |
if PerVertexColor then |
| 458 |
begin |
| 459 |
Assert(Arrays.ColorType <> ctNone); |
| 460 |
glEnableClientState(GL_COLOR_ARRAY); |
| 461 |
case Arrays.ColorType of |
| 462 |
ctRgb : glColorPointer(3, GL_FLOAT, Arrays.AttributeSize, Arrays.ColorRgb); |
| 463 |
ctRgbAlpha: glColorPointer(4, GL_FLOAT, Arrays.AttributeSize, Arrays.ColorRgbAlpha); |
| 464 |
else ; |
| 465 |
end; |
| 466 |
end else |
| 467 |
if (RenderOptions.Mode = rmFull) and Arrays.ForceUnlit then |
| 468 |
glColorv(Arrays.ForcedUnlitColor); |
| 469 |
|
| 470 |
{ Note: For simplicity, we no longer support fog coordinate in fixed-function. |
| 471 |
We avoid extra alternative code path for GL_EXT_fog_coord or GL_version_1_4 that made it core. |
| 472 |
if Arrays.HasFogCoord and GLFeatures.EXT_fog_coord then |
| 473 |
begin |
| 474 |
glEnableClientState(GL_FOG_COORDINATE_ARRAY_EXT); |
| 475 |
glFogCoordPointerEXT(GL_FLOAT, Arrays.AttributeSize, Arrays.FogCoord); |
| 476 |
end; |
| 477 |
} |
| 478 |
|
| 479 |
if GLFeatures.UseMultiTexturing then |
| 480 |
begin |
| 481 |
for I := 0 to Arrays.TexCoords.Count - 1 do |
| 482 |
if (Arrays.TexCoords[I] <> nil) and |
| 483 |
(Arrays.TexCoords[I].Generation = tgExplicit) then |
| 484 |
begin |
| 485 |
glClientActiveTexture(GL_TEXTURE0 + I); |
| 486 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 487 |
glTexCoordPointer(Arrays.TexCoords[I].Dimensions, GL_FLOAT, |
| 488 |
Arrays.AttributeSize, Arrays.TexCoord(I, 0)); |
| 489 |
end; |
| 490 |
end else |
| 491 |
if (Arrays.TexCoords.Count <> 0) and |
| 492 |
(Arrays.TexCoords[0] <> nil) and |
| 493 |
(Arrays.TexCoords[0].Generation = tgExplicit) then |
| 494 |
begin |
| 495 |
glEnableClientState(GL_TEXTURE_COORD_ARRAY); |
| 496 |
glTexCoordPointer(Arrays.TexCoords[0].Dimensions, GL_FLOAT, |
| 497 |
Arrays.AttributeSize, Arrays.TexCoord(0, 0)); |
| 498 |
end; |
| 499 |
{$endif not OpenGLES} |
| 500 |
end; |
| 501 |
|
| 502 |
{ We know now that CurrentMeshShader is the program set by |
| 503 |
TShaderCoordinateRenderer.RenderCoordinateBegin } |
| 504 |
if (Arrays.Attribs.Count <> 0) and (CurrentMeshShader <> nil) then |
| 505 |
EnableVertexAttrib(CurrentMeshShader); |
| 506 |
|
| 507 |
Mode := ToMode[Arrays.Primitive]; |
| 508 |
First := 0; |
| 509 |
if Arrays.HasIndexes then |
| 510 |
begin |
| 511 |
if Vbo[vtIndex] <> 0 then |
| 512 |
begin |
| 513 |
RenderContext.CurrentVao := Vao; |
| 514 |
RenderContext.BindBuffer[btElementArray] := Vbo[vtIndex]; |
| 515 |
end; |
| 516 |
|
| 517 |
if Arrays.Counts = nil then |
| 518 |
glDrawElements(Mode, Arrays.IndexesCount, |
| 519 |
GLIndexConst, Pointer(Arrays.IndexesPtr(First))) |
| 520 |
else |
| 521 |
for I := 0 to Arrays.Counts.Count - 1 do |
| 522 |
begin |
| 523 |
glDrawElements(Mode, Arrays.Counts[I], |
| 524 |
GLIndexConst, Pointer(Arrays.IndexesPtr(First))); |
| 525 |
First := First + Arrays.Counts[I]; |
| 526 |
end; |
| 527 |
end else |
| 528 |
begin |
| 529 |
if Arrays.Counts = nil then |
| 530 |
glDrawArrays(Mode, First, Arrays.Count) |
| 531 |
else |
| 532 |
for I := 0 to Arrays.Counts.Count - 1 do |
| 533 |
begin |
| 534 |
glDrawArrays(Mode, First, Arrays.Counts[I]); |
| 535 |
First := First + Arrays.Counts[I]; |
| 536 |
end; |
| 537 |
end; |
| 538 |
|
| 539 |
{ update Renderer.Statistics.DrawCalls } |
| 540 |
if Arrays.Counts = nil then |
| 541 |
Inc(Renderer.Statistics^.DrawCalls) |
| 542 |
else |
| 543 |
Inc(Renderer.Statistics^.DrawCalls, Arrays.Counts.Count); |
| 544 |
|
| 545 |
{ We disable arrays explicitly. We could alternatively try |
| 546 |
glPushClientAttrib(GL_CLIENT_VERTEX_ARRAY_BIT), but |
| 547 |
1. it makes Mesa 7.2 segfault |
| 548 |
2. I don't know if it really works for GLSL vertex attribs arrays. |
| 549 |
3. It is not available on OpenGLES. } |
| 550 |
DisableArrays; |
| 551 |
end; |
| 552 |
finally RenderCoordinateEnd end; |
| 553 |
end; |
| 554 |
|
| 555 |
procedure TBaseCoordinateRenderer.RenderCoordinateBegin; |
| 556 |
begin |
| 557 |
RenderContext.CullFace := Arrays.CullFace; |
| 558 |
{ Following glTF on what to do with negative scale transformations: |
| 559 |
|
| 560 |
""" |
| 561 |
If the determinant of the transform is a negative value, |
| 562 |
the winding order of the mesh triangle faces should be reversed. |
| 563 |
This supports negative scales for mirroring geometry. |
| 564 |
""" |
| 565 |
|
| 566 |
This makes backface culling remove proper faces when negative |
| 567 |
scale is used. |
| 568 |
See https://github.com/castle-engine/castle-engine/issues/336 . } |
| 569 |
if RenderOptions.RobustNegativeScale and |
| 570 |
(ShapeModelView.Determinant < 0) then |
| 571 |
RenderContext.FrontFaceCcw := not Arrays.FrontFaceCcw |
| 572 |
else |
| 573 |
RenderContext.FrontFaceCcw := Arrays.FrontFaceCcw; |
| 574 |
end; |
| 575 |
|
| 576 |
procedure TBaseCoordinateRenderer.RenderCoordinateEnd; |
| 577 |
begin |
| 578 |
end; |
| 579 |
|
| 580 |
function TBaseCoordinateRenderer.PerVertexColor: Boolean; |
| 581 |
begin |
| 582 |
Result := (RenderOptions.Mode = rmFull) and (Arrays.ColorType <> ctNone); |
| 583 |
end; |
| 584 |
|
| 585 |
{ TTextureCoordinateRenderer ----------------------------------------- } |
| 586 |
|
| 587 |
{$ifndef OpenGLES} |
| 588 |
{ If multitexturing available, sets fixed-function texture coordinate for texture |
| 589 |
unit TextureUnit (by appropriate glMultiTexCoord). |
| 590 |
Otherwise (when no multitexturing), sets texture coordinate for |
| 591 |
the only texture unit (glTexCoord). |
| 592 |
|
| 593 |
The only thing that you have to care about is to specify TextureUnit < |
| 594 |
GLFeatures.MaxTextureUnits. Everything else (whether multitexturing |
| 595 |
available, and shifting TextureUnit by GL_TEXTURE0) is taken care of inside here. } |
| 596 |
procedure MultiTexCoord(const TextureUnit: Cardinal; const TexCoord: TVector4); |
| 597 |
begin |
| 598 |
if GLFeatures.UseMultiTexturing then |
| 599 |
glMultiTexCoord4fv(GL_TEXTURE0 + TextureUnit, @TexCoord) |
| 600 |
else |
| 601 |
glTexCoord4fv(@TexCoord); |
| 602 |
end; |
| 603 |
{$endif} |
| 604 |
|
| 605 |
procedure TTextureCoordinateRenderer.RenderCoordinateBegin; |
| 606 |
|
| 607 |
{ Enable and set parameters of texture generation, |
| 608 |
for texture units where TexCoords[].Generation[] <> tgExplicit. } |
| 609 |
procedure EnableTextureGen(const TexUnit: Cardinal; |
| 610 |
TexCoord: TGeometryTexCoord); |
| 611 |
|
| 612 |
{$ifndef OpenGLES} |
| 613 |
{ Do the necessary things for WorldSpaceNormal and |
| 614 |
WorldSpaceReflectionVector in fixed-function pipeline. |
| 615 |
Note that the shader pipeline counterpart |
| 616 |
of this is entirely inside EnableTexGen. } |
| 617 |
procedure TextureTransformToWorldSpace(const SuccessName, FailName: string); |
| 618 |
begin |
| 619 |
if not GLFeatures.EnableFixedFunction then |
| 620 |
Exit; |
| 621 |
|
| 622 |
{ Set the 4th texture coord component (Q) to 0.0. |
| 623 |
Reason: We use matrix 4x4, so it will transform whole texture |
| 624 |
coordinate (4-valued vector, since we're in homogeneous |
| 625 |
coordinates). We want the generated STR to be transformed |
| 626 |
like a direction, so we want to set Q = always 0.0 |
| 627 |
(in homogeneous coords, this means that vector represents |
| 628 |
a direction and will be accordingly transformed). } |
| 629 |
MultiTexCoord(TexUnit, TVector4.Zero); |
| 630 |
|
| 631 |
{ Remember to set default tex coord 4th component back to 1 |
| 632 |
at the end. Otherwise rendering normal textures (not cube maps) |
| 633 |
could be incorrect --- as the 4th texture component is 0, |
| 634 |
GPU doesn't know how to sample 2D or 3D texture data. |
| 635 |
See Barna29.x3dv from Victor Amat bugreport. |
| 636 |
|
| 637 |
Note that we don't remember that we need to reset it only |
| 638 |
on TexUnit. So RenderCoordinateEnd will just have to reset |
| 639 |
it on all units possibly touched by EnableTextureGen. |
| 640 |
It's a little unoptimal. OTOH, setting up an array to remember |
| 641 |
a list of TexUnit in this case would be an overkill, as in 99% cases |
| 642 |
you only have 1 or 2 texture units where you generate tex coords. } |
| 643 |
RestoreDefaultTexCoord := true; |
| 644 |
|
| 645 |
glMatrixMode(GL_TEXTURE); |
| 646 |
Renderer.PushTextureUnit(TexUnit); |
| 647 |
{ CameraMatrix transforms from world space to camera space, |
| 648 |
we want to transform directions from camera space to world |
| 649 |
space below. So inverted CameraMatrix is exactly what we need. } |
| 650 |
Renderer.RenderingCamera.InverseMatrixNeeded; |
| 651 |
glMultMatrix(Renderer.RenderingCamera.InverseMatrix); |
| 652 |
glMatrixMode(GL_MODELVIEW); |
| 653 |
|
| 654 |
(* |
| 655 |
Later: remove this log, it spams a lot on some scenes, |
| 656 |
and possibly is incorrect on newer Mesa versions. |
| 657 |
|
| 658 |
if GLVersion.Mesa and Log then |
| 659 |
{ Mesa 7.0.4 makes textures strange (like each cube face was |
| 660 |
1-pixel wide?, although I checked --- they are loaded with |
| 661 |
correct sizes). Output this on log, since it's done by renderer, |
| 662 |
potentially many times for a frame. |
| 663 |
Nothing more intelligent now than warning about this is done. } |
| 664 |
WritelnWarning('TexCoord', Format('Transforming tex coords to WORLDSPACE* under Mesa3D may mess up static cube maps (ImageCubeMapTexture and ComposedCubeMapTexture, but not GeneratedCubeMapTexure)', |
| 665 |
[SuccessName, FailName])); |
| 666 |
*) |
| 667 |
end; |
| 668 |
{$endif} |
| 669 |
|
| 670 |
const |
| 671 |
GenerationVectorsIdentity: TTextureGenerationVectors = |
| 672 |
( (X:1; Y: 0; Z: 0; W: 0), |
| 673 |
(X:0; Y: 1; Z: 0; W: 0), |
| 674 |
(X:0; Y: 0; Z: 1; W: 0) ); |
| 675 |
ProjectorScalingMatrix: TMatrix4 = (Data: |
| 676 |
( (0.5, 0, 0, 0), |
| 677 |
( 0, 0.5, 0, 0), |
| 678 |
( 0, 0, 0.5, 0), |
| 679 |
(0.5, 0.5, 0.5, 1) )); |
| 680 |
var |
| 681 |
TransformToWorldSpace: boolean; |
| 682 |
ProjectorMatrix: TMatrix4; |
| 683 |
begin |
| 684 |
{ No point in generating for more texture units. |
| 685 |
They are not even declared in GLSL code, and would cause GLSL errors |
| 686 |
if your textures use e.g. only 1 texture unit, |
| 687 |
but you use MultiTextureCoordinate with 2 children and 2nd children |
| 688 |
is TextureCoordinateGenerator. |
| 689 |
(testcase: x3d-tests/pbr/physical_material.x3dv) } |
| 690 |
if TexUnit >= Renderer.TexCoordsNeeded then |
| 691 |
Exit; |
| 692 |
|
| 693 |
TransformToWorldSpace := TexCoord.Generation in |
| 694 |
[tgWorldSpaceReflectionVector, tgWorldSpaceNormal]; |
| 695 |
|
| 696 |
TRenderer.ActiveTexture(TexUnit); |
| 697 |
case TexCoord.Generation of |
| 698 |
tgSphereMap: |
| 699 |
Shader.EnableTexGen(TexUnit, tgSphere); |
| 700 |
tgCoord: |
| 701 |
begin |
| 702 |
if GLFeatures.EnableFixedFunction then |
| 703 |
begin |
| 704 |
{$ifndef OpenGLES} |
| 705 |
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 706 |
glTexGenv(GL_S, GL_OBJECT_PLANE, GenerationVectorsIdentity[0]); |
| 707 |
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 708 |
glTexGenv(GL_T, GL_OBJECT_PLANE, GenerationVectorsIdentity[1]); |
| 709 |
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 710 |
glTexGenv(GL_R, GL_OBJECT_PLANE, GenerationVectorsIdentity[2]); |
| 711 |
{$endif} |
| 712 |
end; |
| 713 |
Shader.EnableTexGen(TexUnit, tgObject, 0, GenerationVectorsIdentity[0]); |
| 714 |
Shader.EnableTexGen(TexUnit, tgObject, 1, GenerationVectorsIdentity[1]); |
| 715 |
Shader.EnableTexGen(TexUnit, tgObject, 2, GenerationVectorsIdentity[2]); |
| 716 |
end; |
| 717 |
tgCoordEye: |
| 718 |
begin |
| 719 |
if GLFeatures.EnableFixedFunction then |
| 720 |
begin |
| 721 |
{$ifndef OpenGLES} |
| 722 |
{ Vectors given to EYE_PLANE are transformed by inverted |
| 723 |
modelview matrix (at the moment of glTexGen call). |
| 724 |
We don't want to transform them, so just temporarily setting |
| 725 |
modelview matrix to identity makes things work Ok. } |
| 726 |
glPushMatrix; |
| 727 |
glLoadIdentity; |
| 728 |
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); |
| 729 |
glTexGenv(GL_S, GL_EYE_PLANE, GenerationVectorsIdentity[0]); |
| 730 |
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); |
| 731 |
glTexGenv(GL_T, GL_EYE_PLANE, GenerationVectorsIdentity[1]); |
| 732 |
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); |
| 733 |
glTexGenv(GL_R, GL_EYE_PLANE, GenerationVectorsIdentity[2]); |
| 734 |
glPopMatrix; |
| 735 |
{$endif} |
| 736 |
end; |
| 737 |
{ TODO: the shader version of this is quite unoptimal, |
| 738 |
we pass 3 uniform vec4 vectors, make 3 dot operations, |
| 739 |
just to simply copy vertex_eye to texture coordinate. |
| 740 |
Same for tgCoord. So far, this is not used often enough |
| 741 |
to bother with optimization. } |
| 742 |
Shader.EnableTexGen(TexUnit, tgEye, 0, GenerationVectorsIdentity[0]); |
| 743 |
Shader.EnableTexGen(TexUnit, tgEye, 1, GenerationVectorsIdentity[1]); |
| 744 |
Shader.EnableTexGen(TexUnit, tgEye, 2, GenerationVectorsIdentity[2]); |
| 745 |
end; |
| 746 |
tgCameraSpaceNormal, |
| 747 |
tgWorldSpaceNormal: |
| 748 |
begin |
| 749 |
{$ifdef OpenGLES} |
| 750 |
Assert(GLFeatures.TextureCubeMap); // on OpenGLES, cubemaps are always supported |
| 751 |
{$else} |
| 752 |
if not GLFeatures.TextureCubeMap then |
| 753 |
begin |
| 754 |
WritelnWarning('VRML/X3D', 'Rendering context doesn''t support cube maps, cannot generate texture coordinates with mode = "CAMERASPACENORMAL", will do "COORD" instead'); |
| 755 |
|
| 756 |
if GLFeatures.EnableFixedFunction then |
| 757 |
begin |
| 758 |
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 759 |
glTexGenv(GL_S, GL_OBJECT_PLANE, GenerationVectorsIdentity[0]); |
| 760 |
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 761 |
glTexGenv(GL_T, GL_OBJECT_PLANE, GenerationVectorsIdentity[1]); |
| 762 |
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 763 |
glTexGenv(GL_R, GL_OBJECT_PLANE, GenerationVectorsIdentity[2]); |
| 764 |
end; |
| 765 |
|
| 766 |
Shader.EnableTexGen(TexUnit, tgObject, 0, GenerationVectorsIdentity[0]); |
| 767 |
Shader.EnableTexGen(TexUnit, tgObject, 1, GenerationVectorsIdentity[1]); |
| 768 |
Shader.EnableTexGen(TexUnit, tgObject, 2, GenerationVectorsIdentity[2]); |
| 769 |
end else |
| 770 |
{$endif} |
| 771 |
begin |
| 772 |
{$ifndef OpenGLES} |
| 773 |
if TransformToWorldSpace then |
| 774 |
TextureTransformToWorldSpace('WORLDSPACENORMAL', 'CAMERASPACENORMAL'); |
| 775 |
{$endif} |
| 776 |
Shader.EnableTexGen(TexUnit, tgNormal, TransformToWorldSpace); |
| 777 |
end; |
| 778 |
end; |
| 779 |
tgCameraSpaceReflectionVector, |
| 780 |
tgWorldSpaceReflectionVector: |
| 781 |
begin |
| 782 |
{$ifdef OpenGLES} |
| 783 |
Assert(GLFeatures.TextureCubeMap); // on OpenGLES, cubemaps are always supported |
| 784 |
{$else} |
| 785 |
if not GLFeatures.TextureCubeMap then |
| 786 |
begin |
| 787 |
WritelnWarning('VRML/X3D', 'Rendering context doesn''t support cube maps, cannot generate texture coordinates with mode = "CAMERASPACEREFLECTIONVECTOR", will do "COORD" instead'); |
| 788 |
|
| 789 |
if GLFeatures.EnableFixedFunction then |
| 790 |
begin |
| 791 |
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 792 |
glTexGenv(GL_S, GL_OBJECT_PLANE, GenerationVectorsIdentity[0]); |
| 793 |
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 794 |
glTexGenv(GL_T, GL_OBJECT_PLANE, GenerationVectorsIdentity[1]); |
| 795 |
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 796 |
glTexGenv(GL_R, GL_OBJECT_PLANE, GenerationVectorsIdentity[2]); |
| 797 |
end; |
| 798 |
|
| 799 |
Shader.EnableTexGen(TexUnit, tgObject, 0, GenerationVectorsIdentity[0]); |
| 800 |
Shader.EnableTexGen(TexUnit, tgObject, 1, GenerationVectorsIdentity[1]); |
| 801 |
Shader.EnableTexGen(TexUnit, tgObject, 2, GenerationVectorsIdentity[2]); |
| 802 |
end else |
| 803 |
{$endif} |
| 804 |
begin |
| 805 |
{$ifndef OpenGLES} |
| 806 |
if TransformToWorldSpace then |
| 807 |
TextureTransformToWorldSpace('WORLDSPACEREFLECTIONVECTOR', 'CAMERASPACEREFLECTIONVECTOR'); |
| 808 |
{$endif} |
| 809 |
Shader.EnableTexGen(TexUnit, tgReflection, TransformToWorldSpace); |
| 810 |
end; |
| 811 |
end; |
| 812 |
tgProjection: |
| 813 |
begin |
| 814 |
if Assigned(TexCoord.GenerationProjectorMatrix) then |
| 815 |
begin |
| 816 |
ProjectorMatrix := ProjectorScalingMatrix * TexCoord.GenerationProjectorMatrix(); |
| 817 |
|
| 818 |
if GLFeatures.EnableFixedFunction then |
| 819 |
begin |
| 820 |
{$ifndef OpenGLES} |
| 821 |
glPushMatrix; |
| 822 |
glLoadMatrix(Renderer.RenderingCamera.Matrix); |
| 823 |
|
| 824 |
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); |
| 825 |
glTexGenv(GL_S, GL_EYE_PLANE, ProjectorMatrix.Rows[0]); |
| 826 |
|
| 827 |
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); |
| 828 |
glTexGenv(GL_T, GL_EYE_PLANE, ProjectorMatrix.Rows[1]); |
| 829 |
|
| 830 |
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); |
| 831 |
glTexGenv(GL_R, GL_EYE_PLANE, ProjectorMatrix.Rows[2]); |
| 832 |
|
| 833 |
glTexGeni(GL_Q, GL_TEXTURE_GEN_MODE, GL_EYE_LINEAR); |
| 834 |
glTexGenv(GL_Q, GL_EYE_PLANE, ProjectorMatrix.Rows[3]); |
| 835 |
glPopMatrix; |
| 836 |
{$endif} |
| 837 |
end; |
| 838 |
|
| 839 |
Renderer.RenderingCamera.InverseMatrixNeeded; |
| 840 |
ProjectorMatrix := ProjectorMatrix * Renderer.RenderingCamera.InverseMatrix; |
| 841 |
|
| 842 |
Shader.EnableTexGen(TexUnit, tgEye, 0, ProjectorMatrix.Rows[0]); |
| 843 |
Shader.EnableTexGen(TexUnit, tgEye, 1, ProjectorMatrix.Rows[1]); |
| 844 |
Shader.EnableTexGen(TexUnit, tgEye, 2, ProjectorMatrix.Rows[2]); |
| 845 |
Shader.EnableTexGen(TexUnit, tgEye, 3, ProjectorMatrix.Rows[3]); |
| 846 |
end; |
| 847 |
end; |
| 848 |
tgBounds2d: |
| 849 |
begin |
| 850 |
if GLFeatures.EnableFixedFunction then |
| 851 |
begin |
| 852 |
{$ifndef OpenGLES} |
| 853 |
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 854 |
glTexGenv(GL_S, GL_OBJECT_PLANE, TexCoord.GenerationBoundsVector[0]); |
| 855 |
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 856 |
glTexGenv(GL_T, GL_OBJECT_PLANE, TexCoord.GenerationBoundsVector[1]); |
| 857 |
{$endif} |
| 858 |
end; |
| 859 |
Shader.EnableTexGen(TexUnit, tgObject, 0, TexCoord.GenerationBoundsVector[0]); |
| 860 |
Shader.EnableTexGen(TexUnit, tgObject, 1, TexCoord.GenerationBoundsVector[1]); |
| 861 |
end; |
| 862 |
tgBounds3d: |
| 863 |
begin |
| 864 |
if GLFeatures.EnableFixedFunction then |
| 865 |
begin |
| 866 |
{$ifndef OpenGLES} |
| 867 |
glTexGeni(GL_S, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 868 |
glTexGenv(GL_S, GL_OBJECT_PLANE, TexCoord.GenerationBoundsVector[0]); |
| 869 |
glTexGeni(GL_T, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 870 |
glTexGenv(GL_T, GL_OBJECT_PLANE, TexCoord.GenerationBoundsVector[1]); |
| 871 |
glTexGeni(GL_R, GL_TEXTURE_GEN_MODE, GL_OBJECT_LINEAR); |
| 872 |
glTexGenv(GL_R, GL_OBJECT_PLANE, TexCoord.GenerationBoundsVector[2]); |
| 873 |
{$endif} |
| 874 |
end; |
| 875 |
Shader.EnableTexGen(TexUnit, tgObject, 0, TexCoord.GenerationBoundsVector[0]); |
| 876 |
Shader.EnableTexGen(TexUnit, tgObject, 1, TexCoord.GenerationBoundsVector[1]); |
| 877 |
Shader.EnableTexGen(TexUnit, tgObject, 2, TexCoord.GenerationBoundsVector[2]); |
| 878 |
end; |
| 879 |
tgMirrorPlane: |
| 880 |
begin |
| 881 |
Shader.EnableTexGen(TexUnit, tgcMirrorPlane, false); |
| 882 |
Shader.MirrorPlaneUniforms := Shape.MirrorPlaneUniforms; |
| 883 |
end; |
| 884 |
else raise EInternalError.Create('EnableTextureGen - Gen?'); |
| 885 |
end; |
| 886 |
end; |
| 887 |
|
| 888 |
var |
| 889 |
I: Integer; |
| 890 |
begin |
| 891 |
inherited; |
| 892 |
for I := 0 to Arrays.TexCoords.Count - 1 do |
| 893 |
if Arrays.TexCoords[I].Generation <> tgExplicit then |
| 894 |
EnableTextureGen(I, Arrays.TexCoords[I]); |
| 895 |
end; |
| 896 |
|
| 897 |
procedure TTextureCoordinateRenderer.RenderCoordinateEnd; |
| 898 |
{$ifndef OpenGLES} |
| 899 |
const |
| 900 |
DefaultTexCoord: TVector4 = (X:0; Y: 0; Z: 0; W: 1); |
| 901 |
{$endif} |
| 902 |
var |
| 903 |
I: Integer; |
| 904 |
begin |
| 905 |
for I := 0 to Arrays.TexCoords.Count - 1 do |
| 906 |
if Arrays.TexCoords[I].Generation <> tgExplicit then |
| 907 |
begin |
| 908 |
{ Disable OpenGL GL_TEXTURE_GEN_x, if it was (possibly) |
| 909 |
enabled by RenderCoordinateBegin. } |
| 910 |
Shader.DisableTexGen(I); |
| 911 |
if GLFeatures.EnableFixedFunction then |
| 912 |
begin |
| 913 |
{$ifndef OpenGLES} |
| 914 |
{ DisableTexGen already set glActiveTexture } |
| 915 |
if RestoreDefaultTexCoord then |
| 916 |
MultiTexCoord(I, DefaultTexCoord); |
| 917 |
{$endif} |
| 918 |
end; |
| 919 |
end; |
| 920 |
|
| 921 |
inherited; |
| 922 |
end; |
| 923 |
|
| 924 |
{ TFogCoordinateRenderer ------------------------------------------------------- } |
| 925 |
|
| 926 |
procedure TFogCoordinateRenderer.RenderCoordinateBegin; |
| 927 |
begin |
| 928 |
inherited; |
| 929 |
|
| 930 |
if Arrays.HasFogCoord and |
| 931 |
Arrays.FogDirectValues then |
| 932 |
begin |
| 933 |
{ We potentially override the fog already set by TRenderer.RenderShapeFog here. } |
| 934 |
Shader.ModifyFog(ftLinear, fcPassedCoordinate, 1); |
| 935 |
end; |
| 936 |
end; |
| 937 |
|
| 938 |
procedure TFogCoordinateRenderer.RenderCoordinateEnd; |
| 939 |
begin |
| 940 |
inherited; |
| 941 |
end; |
| 942 |
|
| 943 |
{ TColorPerVertexCoordinateRenderer --------------------------------------- } |
| 944 |
|
| 945 |
procedure TColorPerVertexCoordinateRenderer.RenderCoordinateBegin; |
| 946 |
begin |
| 947 |
inherited; |
| 948 |
|
| 949 |
if PerVertexColor then |
| 950 |
begin |
| 951 |
{ Note that the shader implementation of color-per-vertex is enabled regardless |
| 952 |
if we have lighting or not, as in fact this activates COLOR_PER_VERTEX |
| 953 |
feature, which works regardless if we're lit or unlit. } |
| 954 |
Assert(Arrays.ColorType <> ctNone); // PerVertexColor = true should guarantee this |
| 955 |
Shader.EnableColorPerVertex(Arrays.ColorMode, Arrays.ColorType); |
| 956 |
|
| 957 |
{$ifndef OpenGLES} |
| 958 |
{ For fixed-function pipeline, GL_COLOR_MATERIAL is needed only if Lighting. } |
| 959 |
MaterialFromColorEnabled := GLFeatures.EnableFixedFunction and Lighting; |
| 960 |
if MaterialFromColorEnabled then |
| 961 |
{ Note: glColorMaterial is already set by TRenderer.ClassRenderBegin } |
| 962 |
glEnable(GL_COLOR_MATERIAL); |
| 963 |
{$endif} |
| 964 |
end else |
| 965 |
begin |
| 966 |
{$ifndef OpenGLES} |
| 967 |
MaterialFromColorEnabled := false; |
| 968 |
{$endif} |
| 969 |
end; |
| 970 |
end; |
| 971 |
|
| 972 |
procedure TColorPerVertexCoordinateRenderer.RenderCoordinateEnd; |
| 973 |
begin |
| 974 |
{$ifndef OpenGLES} |
| 975 |
if MaterialFromColorEnabled then |
| 976 |
glDisable(GL_COLOR_MATERIAL); |
| 977 |
{$endif} |
| 978 |
|
| 979 |
inherited; |
| 980 |
end; |
| 981 |
|
| 982 |
{ TShaderCoordinateRenderer -------------------------------------------------- } |
| 983 |
|
| 984 |
procedure TShaderCoordinateRenderer.RenderCoordinateBegin; |
| 985 |
|
| 986 |
{ Initialize Shader.LightingModel. |
| 987 |
Must be done before linking the shader. |
| 988 |
Logic must follow *exactly* FinalizeLightingModel (which must be done after |
| 989 |
linking the shader). } |
| 990 |
procedure InitializeLightingModel; |
| 991 |
begin |
| 992 |
if CustomShaders <> nil then |
| 993 |
Shader.LightingModel := lmUnlit |
| 994 |
else |
| 995 |
if RenderOptions.Mode = rmSolidColor then |
| 996 |
Shader.LightingModel := lmUnlit |
| 997 |
else |
| 998 |
if (not RenderOptions.Lighting) and (Shader.Material <> nil) then |
| 999 |
Shader.LightingModel := lmUnlit |
| 1000 |
else |
| 1001 |
if Arrays.ForceUnlit then |
| 1002 |
Shader.LightingModel := lmUnlit |
| 1003 |
else |
| 1004 |
if Shader.Material is TPhongMaterialInfo then |
| 1005 |
Shader.LightingModel := lmPhong |
| 1006 |
else |
| 1007 |
if Shader.Material is TPhysicalMaterialInfo then |
| 1008 |
Shader.LightingModel := lmPhysical |
| 1009 |
else |
| 1010 |
if Shader.Material is TUnlitMaterialInfo then |
| 1011 |
Shader.LightingModel := lmUnlit |
| 1012 |
else |
| 1013 |
Shader.LightingModel := lmUnlit |
| 1014 |
end; |
| 1015 |
|
| 1016 |
var |
| 1017 |
CurrentX3DProgram: TX3DShaderProgram; |
| 1018 |
Pass: TTotalRenderingPass; |
| 1019 |
begin |
| 1020 |
inherited; |
| 1021 |
|
| 1022 |
InitializeLightingModel; |
| 1023 |
|
| 1024 |
if CustomShaders <> nil then |
| 1025 |
begin |
| 1026 |
CurrentMeshShader := CustomShaders.Shader[Shader.AlphaTest]; |
| 1027 |
end else |
| 1028 |
begin |
| 1029 |
{ Finalize Shader settings, before calculating Shader.CodeHash } |
| 1030 |
if (Shape.Node <> nil) and |
| 1031 |
(Shape.Node.Appearance <> nil) then |
| 1032 |
Shader.EnableAppearanceEffects(Shape.Node.Appearance.FdEffects); |
| 1033 |
if Shape.State.Effects <> nil then |
| 1034 |
Shader.EnableGroupEffects(Shape.State.Effects); |
| 1035 |
(* |
| 1036 |
{ For now we decided to not implement per-scene RenderOptions.ColorSpace, |
| 1037 |
as use-case would be low. |
| 1038 |
In all practical cases, adjusting global ColorSpace seems like a better idea, |
| 1039 |
as this should really apply to all your rendering. } |
| 1040 |
if RenderOptions.ColorSpace <> csDefault then |
| 1041 |
FinalColorSpace := RenderOptions.ColorSpace // use per-scene value |
| 1042 |
else |
| 1043 |
FinalColorSpace := CastleRenderOptions.ColorSpace; // use global variable |
| 1044 |
*) |
| 1045 |
Shader.ColorSpaceLinear := |
| 1046 |
(ColorSpace = csLinear) or |
| 1047 |
((ColorSpace = csLinearWhenPhysicalMaterial) and |
| 1048 |
(Shader.Material is TPhysicalMaterialInfo)); |
| 1049 |
|
| 1050 |
if not GLFeatures.EnableFixedFunction then |
| 1051 |
begin |
| 1052 |
Pass := Renderer.FPass; |
| 1053 |
|
| 1054 |
{ calculate and use Shape.ProgramCache[Pass].ShaderProgram } |
| 1055 |
if (Shape.ProgramCache[Pass] = nil) or |
| 1056 |
(not(Shape.ProgramCache[Pass].Hash = Shader.CodeHash)) then |
| 1057 |
begin |
| 1058 |
if LogRenderer then |
| 1059 |
WritelnLog('GLSL', 'Creating shaders for shape "%s" (new: %s, pass: %d)', [ |
| 1060 |
Shape.NiceName, |
| 1061 |
BoolToStr((Shape.ProgramCache[Pass] = nil) or |
| 1062 |
(Shape.ProgramCache[Pass].ShaderProgram = nil), true), |
| 1063 |
Pass |
| 1064 |
]); |
| 1065 |
|
| 1066 |
if Shape.ProgramCache[Pass] <> nil then |
| 1067 |
RendererCache.Program_DecReference(Shape.ProgramCache[Pass]); |
| 1068 |
|
| 1069 |
Shape.ProgramCache[Pass] := RendererCache.Program_IncReference( |
| 1070 |
Shader, Shape.NiceName); |
| 1071 |
end; |
| 1072 |
|
| 1073 |
Assert(Shape.ProgramCache[Pass] <> nil); |
| 1074 |
CurrentX3DProgram := Shape.ProgramCache[Pass].ShaderProgram; |
| 1075 |
CurrentMeshShader := CurrentX3DProgram; |
| 1076 |
if not GLFeatures.EnableFixedFunction then |
| 1077 |
Assert(CurrentX3DProgram <> nil, 'Shader is not initialized, but it has to be (creation of fallback shaders failed?)'); |
| 1078 |
if CurrentX3DProgram <> nil then // may be nil on desktops |
| 1079 |
Shader.SetDynamicUniforms(CurrentX3DProgram); |
| 1080 |
end else |
| 1081 |
CurrentMeshShader := nil; |
| 1082 |
end; |
| 1083 |
|
| 1084 |
if CurrentMeshShader <> nil then |
| 1085 |
CurrentMeshShader.SetupUniforms(BoundTextureUnits); |
| 1086 |
|
| 1087 |
RenderContext.CurrentProgram := CurrentMeshShader; // enable the CurrentMeshShader for OpenGL |
| 1088 |
end; |