Debian Codegraph @

src/scene/castleinternalrenderer_materials.inc

raw
1 {%MainUnit castleinternalrenderer.pas}
2 {
3 Copyright 2003-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 var
18 Lighting: Boolean;
19
20 procedure RenderMaterialsBegin;
21
22 { Is this a Phong material that has only emissiveColor non-zero. }
23 function PureEmissivePhong(const Material: TMaterialInfo): Boolean;
24 begin
25 Result :=
26 (Material is TPhongMaterialInfo) and
27 TPhongMaterialInfo(Material).PureEmissive and
28 { Existence of Color/ColorRGBA nodes means that we cannot rely on simple
29 "only emissive" lighting calculation, because Color/ColorRGBA
30 nodes replace the diffuse factor, in case of Phong Material. }
31 (Shape.Geometry.InternalColor = nil) and
32 (Shape.Geometry.InternalColorRGBA = nil);
33 end;
34
35 { Apply material for shader and fixed-function pipelines.
36 Assumes Lighting is already calculated.
37 For fixed-function pipeline, this sets full glMaterial and glColor. }
38 procedure RenderMaterial(const M: TMaterialInfo);
39 {$ifndef OpenGLES}
40 var
41 PM: TPhongMaterialInfo;
42 // UM: TUnlitMaterialInfo;
43 {$endif}
44 begin
45 Shader.Material := M;
46
47 if RenderOptions.Mode <> rmFull then Exit;
48
49 if GLFeatures.EnableFixedFunction then
50 begin
51 {$ifndef OpenGLES}
52 if Lighting then
53 begin
54 Assert(M <> nil); // Lighting is always false when M = nil
55
56 if M is TPhongMaterialInfo then
57 begin
58 PM := TPhongMaterialInfo(M);
59 glMaterialv(GL_FRONT_AND_BACK, GL_AMBIENT, Vector4(PM.AmbientColor, M.Opacity));
60 glMaterialv(GL_FRONT_AND_BACK, GL_DIFFUSE, Vector4(PM.DiffuseColor, M.Opacity));
61 glMaterialv(GL_FRONT_AND_BACK, GL_SPECULAR, Vector4(PM.SpecularColor, M.Opacity));
62 glMaterialv(GL_FRONT_AND_BACK, GL_EMISSION, Vector4(PM.EmissiveColor, M.Opacity));
63 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, PM.ShininessExp);
64 end else
65 if M is TUnlitMaterialInfo then
66 begin
67 Assert(false, 'UnlitMaterial should not have Lighting=true');
68 // UM := TUnlitMaterialInfo(M);
69 // glMaterialv(GL_FRONT_AND_BACK, GL_AMBIENT, Vector4(TVector3.Zero, M.Opacity));
70 // glMaterialv(GL_FRONT_AND_BACK, GL_DIFFUSE, Vector4(TVector3.Zero, M.Opacity));
71 // glMaterialv(GL_FRONT_AND_BACK, GL_SPECULAR, Vector4(TVector3.Zero, M.Opacity));
72 // glMaterialv(GL_FRONT_AND_BACK, GL_EMISSION, Vector4(UM.EmissiveColor, M.Opacity));
73 // glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0);
74 end else
75 begin
76 // approximate PhysicalMaterial in fixed-function using Phong with diffuseColor = baseColor
77 glMaterialv(GL_FRONT_AND_BACK, GL_AMBIENT, Vector4(TVector3.Zero, M.Opacity));
78 glMaterialv(GL_FRONT_AND_BACK, GL_DIFFUSE, Vector4(M.MainColor, M.Opacity));
79 glMaterialv(GL_FRONT_AND_BACK, GL_SPECULAR, Vector4(TVector3.Zero, M.Opacity));
80 glMaterialv(GL_FRONT_AND_BACK, GL_EMISSION, Vector4(TVector3.Zero, M.Opacity));
81 glMaterialf(GL_FRONT_AND_BACK, GL_SHININESS, 0.0);
82 end;
83
84 { If Lighting, we know that GL_LIGHTING is enabled
85 (for fixed-function, or shader). So no point in setting glColor. }
86 end else
87 if M <> nil then
88 begin
89 { UnlitMaterial is implemented here, as it causes Lighting = false always.
90 We will also use this for fixed-function pipeline when
91 - user set RenderOptions.Lighting = false and we have Phong Material or PhysicalMaterial.
92 - or we have Phong Material that is PureEmissive. }
93 if PureEmissivePhong(M) then
94 glColorv(Vector4(TPhongMaterialInfo(M).EmissiveColor, M.Opacity))
95 else
96 glColorv(M.MainColor);
97 end else
98 begin
99 { In case of material = nil, behave like UnlitMaterial with emissiveColor = white. }
100 glColorv(WhiteRGB);
101 end;
102 {$endif}
103 end;
104 end;
105
106 var
107 Material: TMaterialInfo;
108 MaterialLit, MaterialAndShapeLit: Boolean;
109 begin
110 // calculate Material
111 if Shape.Geometry is TAbstractGeometryNode_1 then
112 begin
113 { VRML 1.0 }
114 Material := Shape.State.VRML1State.Material.MaterialInfo(0);
115 end else
116 begin
117 { VRML >= 2.0, X3D }
118 Material := Shape.State.MaterialInfo;
119 end;
120
121 // calculate MaterialLit ("whether Material requires lighting calculation")
122 MaterialLit :=
123 { Material = nil indicates "white unlit material". }
124 (Material <> nil) and
125 { Optimize materials in case of PureEmissive. }
126 (not PureEmissivePhong(Material)) and
127 { UnlitMaterial explicitly says that lighting calculation is not done. }
128 (not (Material is TUnlitMaterialInfo));
129
130 // calculate MaterialAndShapeLit ("whether Material or geometry requires lighting calculation")
131 MaterialAndShapeLit := MaterialLit and Shape.Geometry.Lit(Shape.State);
132
133 // calculate Lighting ("whether anything requires lighting calculation")
134 Lighting := RenderOptions.Lighting and MaterialAndShapeLit and
135 (RenderOptions.Mode = rmFull);
136 RenderContext.FixedFunctionLighting := Lighting;
137 if Lighting then
138 Shader.EnableLighting;
139
140 RenderMaterial(Material);
141 end;