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
6 #include "FixedPipelineRenderer.h"
8 #include "IVideoDriver.h"
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)
25 void COpenGL3MaterialBaseCB::OnSetMaterial(const SMaterial& material)
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;
34 FogEnable = material.FogEnable ? 1 : 0;
36 Thickness = (material.Thickness > 0.f) ? material.Thickness : 1.f;
39 void COpenGL3MaterialBaseCB::OnSetConstants(IMaterialRendererServices* services, s32 userData)
41 IVideoDriver* driver = services->getVideoDriver();
45 WVPMatrixID = services->getVertexShaderConstantID("uWVPMatrix");
46 WVMatrixID = services->getVertexShaderConstantID("uWVMatrix");
47 NMatrixID = services->getVertexShaderConstantID("uNMatrix");
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");
57 FirstUpdateBase = false;
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);
64 core::matrix4 Matrix = P * V * W;
65 services->setPixelShaderConstant(WVPMatrixID, Matrix.pointer(), 16);
68 services->setPixelShaderConstant(WVMatrixID, Matrix.pointer(), 16);
71 services->setPixelShaderConstant(NMatrixID, Matrix.getTransposed().pointer(), 16);
73 services->setPixelShaderConstant(FogEnableID, &FogEnable, 1);
78 E_FOG_TYPE TempType = EFT_FOG_LINEAR;
79 bool TempPerFragment = false;
80 bool TempRange = false;
82 driver->getFog(TempColor, TempType, FogStart, FogEnd, FogDensity, TempPerFragment, TempRange);
84 FogType = (s32)TempType;
85 FogColor = SColorf(TempColor);
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);
94 services->setPixelShaderConstant(ThicknessID, &Thickness, 1);
97 // EMT_SOLID + EMT_TRANSPARENT_ADD_COLOR + EMT_TRANSPARENT_ALPHA_CHANNEL + EMT_TRANSPARENT_VERTEX_ALPHA
99 COpenGL3MaterialSolidCB::COpenGL3MaterialSolidCB() :
100 FirstUpdate(true), TMatrix0ID(-1), AlphaRefID(-1), TextureUsage0ID(-1), TextureUnit0ID(-1), AlphaRef(0.5f), TextureUsage0(0), TextureUnit0(0)
104 void COpenGL3MaterialSolidCB::OnSetMaterial(const SMaterial& material)
106 COpenGL3MaterialBaseCB::OnSetMaterial(material);
108 AlphaRef = material.MaterialTypeParam;
109 TextureUsage0 = (material.TextureLayer[0].Texture) ? 1 : 0;
112 void COpenGL3MaterialSolidCB::OnSetConstants(IMaterialRendererServices* services, s32 userData)
114 COpenGL3MaterialBaseCB::OnSetConstants(services, userData);
116 IVideoDriver* driver = services->getVideoDriver();
120 TMatrix0ID = services->getVertexShaderConstantID("uTMatrix0");
121 AlphaRefID = services->getVertexShaderConstantID("uAlphaRef");
122 TextureUsage0ID = services->getVertexShaderConstantID("uTextureUsage0");
123 TextureUnit0ID = services->getVertexShaderConstantID("uTextureUnit0");
128 core::matrix4 Matrix = driver->getTransform(ETS_TEXTURE_0);
129 services->setPixelShaderConstant(TMatrix0ID, Matrix.pointer(), 16);
131 services->setPixelShaderConstant(AlphaRefID, &AlphaRef, 1);
132 services->setPixelShaderConstant(TextureUsage0ID, &TextureUsage0, 1);
133 services->setPixelShaderConstant(TextureUnit0ID, &TextureUnit0, 1);
136 // EMT_ONETEXTURE_BLEND
138 COpenGL3MaterialOneTextureBlendCB::COpenGL3MaterialOneTextureBlendCB() :
139 FirstUpdate(true), TMatrix0ID(-1), BlendTypeID(-1), TextureUsage0ID(-1), TextureUnit0ID(-1), BlendType(0), TextureUsage0(0), TextureUnit0(0)
143 void COpenGL3MaterialOneTextureBlendCB::OnSetMaterial(const SMaterial& material)
145 COpenGL3MaterialBaseCB::OnSetMaterial(material);
149 E_BLEND_FACTOR srcRGBFact,dstRGBFact,srcAlphaFact,dstAlphaFact;
150 E_MODULATE_FUNC modulate;
152 unpack_textureBlendFuncSeparate(srcRGBFact, dstRGBFact, srcAlphaFact, dstAlphaFact, modulate, alphaSource, material.MaterialTypeParam);
154 if (textureBlendFunc_hasAlpha(srcRGBFact) || textureBlendFunc_hasAlpha(dstRGBFact) || textureBlendFunc_hasAlpha(srcAlphaFact) || textureBlendFunc_hasAlpha(dstAlphaFact))
156 if (alphaSource == EAS_VERTEX_COLOR)
160 else if (alphaSource == EAS_TEXTURE)
166 TextureUsage0 = (material.TextureLayer[0].Texture) ? 1 : 0;
169 void COpenGL3MaterialOneTextureBlendCB::OnSetConstants(IMaterialRendererServices* services, s32 userData)
171 COpenGL3MaterialBaseCB::OnSetConstants(services, userData);
173 IVideoDriver* driver = services->getVideoDriver();
177 TMatrix0ID = services->getVertexShaderConstantID("uTMatrix0");
178 BlendTypeID = services->getVertexShaderConstantID("uBlendType");
179 TextureUsage0ID = services->getVertexShaderConstantID("uTextureUsage0");
180 TextureUnit0ID = services->getVertexShaderConstantID("uTextureUnit0");
185 core::matrix4 Matrix = driver->getTransform(ETS_TEXTURE_0);
186 services->setPixelShaderConstant(TMatrix0ID, Matrix.pointer(), 16);
188 services->setPixelShaderConstant(BlendTypeID, &BlendType, 1);
189 services->setPixelShaderConstant(TextureUsage0ID, &TextureUsage0, 1);
190 services->setPixelShaderConstant(TextureUnit0ID, &TextureUnit0, 1);