]> git.lizzy.rs Git - irrlicht.git/blob - source/Irrlicht/OpenGL/FixedPipelineRenderer.cpp
OpenGL3: Drop unused material property uniforms
[irrlicht.git] / source / Irrlicht / OpenGL / FixedPipelineRenderer.cpp
1 // Copyright (C) 2023 Vitaliy Lobachevskiy
2 // Copyright (C) 2014 Patryk Nadrowski
3 // This file is part of the "Irrlicht Engine".
4 // For conditions of distribution and use, see copyright notice in Irrlicht.h
5
6 #include "FixedPipelineRenderer.h"
7
8 #include "IVideoDriver.h"
9
10 namespace irr
11 {
12 namespace video
13 {
14
15 // Base callback
16
17 COpenGL3MaterialBaseCB::COpenGL3MaterialBaseCB() :
18         FirstUpdateBase(true), WVPMatrixID(-1), WVMatrixID(-1), NMatrixID(-1),
19         FogEnableID(-1), FogTypeID(-1), FogColorID(-1), FogStartID(-1),
20         FogEndID(-1), FogDensityID(-1), ThicknessID(-1), LightEnable(false), MaterialAmbient(SColorf(0.f, 0.f, 0.f)), MaterialDiffuse(SColorf(0.f, 0.f, 0.f)), MaterialEmissive(SColorf(0.f, 0.f, 0.f)), MaterialSpecular(SColorf(0.f, 0.f, 0.f)),
21         MaterialShininess(0.f), FogEnable(0), FogType(1), FogColor(SColorf(0.f, 0.f, 0.f, 1.f)), FogStart(0.f), FogEnd(0.f), FogDensity(0.f), Thickness(1.f)
22 {
23 }
24
25 void COpenGL3MaterialBaseCB::OnSetMaterial(const SMaterial& material)
26 {
27         LightEnable = material.Lighting;
28         MaterialAmbient = SColorf(material.AmbientColor);
29         MaterialDiffuse = SColorf(material.DiffuseColor);
30         MaterialEmissive = SColorf(material.EmissiveColor);
31         MaterialSpecular = SColorf(material.SpecularColor);
32         MaterialShininess = material.Shininess;
33
34         FogEnable = material.FogEnable ? 1 : 0;
35
36         Thickness = (material.Thickness > 0.f) ? material.Thickness : 1.f;
37 }
38
39 void COpenGL3MaterialBaseCB::OnSetConstants(IMaterialRendererServices* services, s32 userData)
40 {
41         IVideoDriver* driver = services->getVideoDriver();
42
43         if (FirstUpdateBase)
44         {
45                 WVPMatrixID = services->getVertexShaderConstantID("uWVPMatrix");
46                 WVMatrixID = services->getVertexShaderConstantID("uWVMatrix");
47                 NMatrixID = services->getVertexShaderConstantID("uNMatrix");
48
49                 FogEnableID = services->getVertexShaderConstantID("uFogEnable");
50                 FogTypeID = services->getVertexShaderConstantID("uFogType");
51                 FogColorID = services->getVertexShaderConstantID("uFogColor");
52                 FogStartID = services->getVertexShaderConstantID("uFogStart");
53                 FogEndID = services->getVertexShaderConstantID("uFogEnd");
54                 FogDensityID = services->getVertexShaderConstantID("uFogDensity");
55                 ThicknessID = services->getVertexShaderConstantID("uThickness");
56
57                 FirstUpdateBase = false;
58         }
59
60         const core::matrix4 W = driver->getTransform(ETS_WORLD);
61         const core::matrix4 V = driver->getTransform(ETS_VIEW);
62         const core::matrix4 P = driver->getTransform(ETS_PROJECTION);
63
64         core::matrix4 Matrix = P * V * W;
65         services->setPixelShaderConstant(WVPMatrixID, Matrix.pointer(), 16);
66
67         Matrix = V * W;
68         services->setPixelShaderConstant(WVMatrixID, Matrix.pointer(), 16);
69
70         Matrix.makeInverse();
71         services->setPixelShaderConstant(NMatrixID, Matrix.getTransposed().pointer(), 16);
72
73         services->setPixelShaderConstant(FogEnableID, &FogEnable, 1);
74
75         if (FogEnable)
76         {
77                 SColor TempColor(0);
78                 E_FOG_TYPE TempType = EFT_FOG_LINEAR;
79                 bool TempPerFragment = false;
80                 bool TempRange = false;
81
82                 driver->getFog(TempColor, TempType, FogStart, FogEnd, FogDensity, TempPerFragment, TempRange);
83
84                 FogType = (s32)TempType;
85                 FogColor = SColorf(TempColor);
86
87                 services->setPixelShaderConstant(FogTypeID, &FogType, 1);
88                 services->setPixelShaderConstant(FogColorID, reinterpret_cast<f32*>(&FogColor), 4);
89                 services->setPixelShaderConstant(FogStartID, &FogStart, 1);
90                 services->setPixelShaderConstant(FogEndID, &FogEnd, 1);
91                 services->setPixelShaderConstant(FogDensityID, &FogDensity, 1);
92         }
93
94         services->setPixelShaderConstant(ThicknessID, &Thickness, 1);
95 }
96
97 // EMT_SOLID + EMT_TRANSPARENT_ADD_COLOR + EMT_TRANSPARENT_ALPHA_CHANNEL + EMT_TRANSPARENT_VERTEX_ALPHA
98
99 COpenGL3MaterialSolidCB::COpenGL3MaterialSolidCB() :
100         FirstUpdate(true), TMatrix0ID(-1), AlphaRefID(-1), TextureUsage0ID(-1), TextureUnit0ID(-1), AlphaRef(0.5f), TextureUsage0(0), TextureUnit0(0)
101 {
102 }
103
104 void COpenGL3MaterialSolidCB::OnSetMaterial(const SMaterial& material)
105 {
106         COpenGL3MaterialBaseCB::OnSetMaterial(material);
107
108         AlphaRef = material.MaterialTypeParam;
109         TextureUsage0 = (material.TextureLayer[0].Texture) ? 1 : 0;
110 }
111
112 void COpenGL3MaterialSolidCB::OnSetConstants(IMaterialRendererServices* services, s32 userData)
113 {
114         COpenGL3MaterialBaseCB::OnSetConstants(services, userData);
115
116         IVideoDriver* driver = services->getVideoDriver();
117
118         if (FirstUpdate)
119         {
120                 TMatrix0ID = services->getVertexShaderConstantID("uTMatrix0");
121                 AlphaRefID = services->getVertexShaderConstantID("uAlphaRef");
122                 TextureUsage0ID = services->getVertexShaderConstantID("uTextureUsage0");
123                 TextureUnit0ID = services->getVertexShaderConstantID("uTextureUnit0");
124
125                 FirstUpdate = false;
126         }
127
128         core::matrix4 Matrix = driver->getTransform(ETS_TEXTURE_0);
129         services->setPixelShaderConstant(TMatrix0ID, Matrix.pointer(), 16);
130
131         services->setPixelShaderConstant(AlphaRefID, &AlphaRef, 1);
132         services->setPixelShaderConstant(TextureUsage0ID, &TextureUsage0, 1);
133         services->setPixelShaderConstant(TextureUnit0ID, &TextureUnit0, 1);
134 }
135
136 // EMT_ONETEXTURE_BLEND
137
138 COpenGL3MaterialOneTextureBlendCB::COpenGL3MaterialOneTextureBlendCB() :
139         FirstUpdate(true), TMatrix0ID(-1), BlendTypeID(-1), TextureUsage0ID(-1), TextureUnit0ID(-1), BlendType(0), TextureUsage0(0), TextureUnit0(0)
140 {
141 }
142
143 void COpenGL3MaterialOneTextureBlendCB::OnSetMaterial(const SMaterial& material)
144 {
145         COpenGL3MaterialBaseCB::OnSetMaterial(material);
146
147         BlendType = 0;
148
149         E_BLEND_FACTOR srcRGBFact,dstRGBFact,srcAlphaFact,dstAlphaFact;
150         E_MODULATE_FUNC modulate;
151         u32 alphaSource;
152         unpack_textureBlendFuncSeparate(srcRGBFact, dstRGBFact, srcAlphaFact, dstAlphaFact, modulate, alphaSource, material.MaterialTypeParam);
153
154         if (textureBlendFunc_hasAlpha(srcRGBFact) || textureBlendFunc_hasAlpha(dstRGBFact) || textureBlendFunc_hasAlpha(srcAlphaFact) || textureBlendFunc_hasAlpha(dstAlphaFact))
155         {
156                 if (alphaSource == EAS_VERTEX_COLOR)
157                 {
158                         BlendType = 1;
159                 }
160                 else if (alphaSource == EAS_TEXTURE)
161                 {
162                         BlendType = 2;
163                 }
164         }
165
166         TextureUsage0 = (material.TextureLayer[0].Texture) ? 1 : 0;
167 }
168
169 void COpenGL3MaterialOneTextureBlendCB::OnSetConstants(IMaterialRendererServices* services, s32 userData)
170 {
171         COpenGL3MaterialBaseCB::OnSetConstants(services, userData);
172
173         IVideoDriver* driver = services->getVideoDriver();
174
175         if (FirstUpdate)
176         {
177                 TMatrix0ID = services->getVertexShaderConstantID("uTMatrix0");
178                 BlendTypeID = services->getVertexShaderConstantID("uBlendType");
179                 TextureUsage0ID = services->getVertexShaderConstantID("uTextureUsage0");
180                 TextureUnit0ID = services->getVertexShaderConstantID("uTextureUnit0");
181
182                 FirstUpdate = false;
183         }
184
185         core::matrix4 Matrix = driver->getTransform(ETS_TEXTURE_0);
186         services->setPixelShaderConstant(TMatrix0ID, Matrix.pointer(), 16);
187
188         services->setPixelShaderConstant(BlendTypeID, &BlendType, 1);
189         services->setPixelShaderConstant(TextureUsage0ID, &TextureUsage0, 1);
190         services->setPixelShaderConstant(TextureUnit0ID, &TextureUnit0, 1);
191 }
192
193 }
194 }