aboutsummaryrefslogtreecommitdiff
path: root/src/client
diff options
context:
space:
mode:
authorDanila Shutov <dcbrwn2@gmail.com>2020-02-16 22:37:28 +0300
committerGitHub <noreply@github.com>2020-02-16 20:37:28 +0100
commit6958071f497c8f059429343339ad3818ed3cfed1 (patch)
tree9e38865425f8c0589b695935e13009070778719b /src/client
parent478e7532985320c5ddacc0280f77b67956715a65 (diff)
downloadminetest-6958071f497c8f059429343339ad3818ed3cfed1.tar.gz
minetest-6958071f497c8f059429343339ad3818ed3cfed1.tar.bz2
minetest-6958071f497c8f059429343339ad3818ed3cfed1.zip
Basic model shading (#9374)
Diffstat (limited to 'src/client')
-rw-r--r--src/client/client.cpp2
-rw-r--r--src/client/client.h2
-rw-r--r--src/client/clientenvironment.cpp57
-rw-r--r--src/client/content_cao.cpp129
-rw-r--r--src/client/content_cao.h4
-rw-r--r--src/client/shader.cpp8
-rw-r--r--src/client/shader.h2
7 files changed, 163 insertions, 41 deletions
diff --git a/src/client/client.cpp b/src/client/client.cpp
index 6016f940e..c6d28ce80 100644
--- a/src/client/client.cpp
+++ b/src/client/client.cpp
@@ -1860,7 +1860,7 @@ ITextureSource* Client::getTextureSource()
{
return m_tsrc;
}
-IShaderSource* Client::getShaderSource()
+IWritableShaderSource* Client::getShaderSource()
{
return m_shsrc;
}
diff --git a/src/client/client.h b/src/client/client.h
index b9807a11e..2933a46e0 100644
--- a/src/client/client.h
+++ b/src/client/client.h
@@ -367,7 +367,7 @@ public:
const NodeDefManager* getNodeDefManager() override;
ICraftDefManager* getCraftDefManager() override;
ITextureSource* getTextureSource();
- virtual IShaderSource* getShaderSource();
+ virtual IWritableShaderSource* getShaderSource();
u16 allocateUnknownNodeId(const std::string &name) override;
virtual ISoundManager* getSoundManager();
MtEventManager* getEventManager();
diff --git a/src/client/clientenvironment.cpp b/src/client/clientenvironment.cpp
index 5eb033302..52d133781 100644
--- a/src/client/clientenvironment.cpp
+++ b/src/client/clientenvironment.cpp
@@ -32,11 +32,66 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "raycast.h"
#include "voxelalgorithms.h"
#include "settings.h"
+#include "shader.h"
#include "content_cao.h"
#include <algorithm>
#include "client/renderingengine.h"
/*
+ CAOShaderConstantSetter
+*/
+
+//! Shader constant setter for passing material emissive color to the CAO object_shader
+class CAOShaderConstantSetter : public IShaderConstantSetter
+{
+public:
+ CAOShaderConstantSetter():
+ m_emissive_color_setting("emissiveColor")
+ {}
+
+ ~CAOShaderConstantSetter() override = default;
+
+ void onSetConstants(video::IMaterialRendererServices *services,
+ bool is_highlevel) override
+ {
+ if (!is_highlevel)
+ return;
+
+ // Ambient color
+ video::SColorf emissive_color(m_emissive_color);
+
+ float as_array[4] = {
+ emissive_color.r,
+ emissive_color.g,
+ emissive_color.b,
+ emissive_color.a,
+ };
+ m_emissive_color_setting.set(as_array, services);
+ }
+
+ void onSetMaterial(const video::SMaterial& material) override
+ {
+ m_emissive_color = material.EmissiveColor;
+ }
+
+private:
+ video::SColor m_emissive_color;
+ CachedPixelShaderSetting<float, 4> m_emissive_color_setting;
+};
+
+class CAOShaderConstantSetterFactory : public IShaderConstantSetterFactory
+{
+public:
+ CAOShaderConstantSetterFactory()
+ {}
+
+ virtual IShaderConstantSetter* create()
+ {
+ return new CAOShaderConstantSetter();
+ }
+};
+
+/*
ClientEnvironment
*/
@@ -47,6 +102,8 @@ ClientEnvironment::ClientEnvironment(ClientMap *map,
m_texturesource(texturesource),
m_client(client)
{
+ auto *shdrsrc = m_client->getShaderSource();
+ shdrsrc->addShaderConstantSetterFactory(new CAOShaderConstantSetterFactory());
}
ClientEnvironment::~ClientEnvironment()
diff --git a/src/client/content_cao.cpp b/src/client/content_cao.cpp
index c76f05bbb..3b203bc07 100644
--- a/src/client/content_cao.cpp
+++ b/src/client/content_cao.cpp
@@ -46,6 +46,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "wieldmesh.h"
#include <algorithm>
#include <cmath>
+#include "client/shader.h"
class Settings;
struct ToolCapabilities;
@@ -352,6 +353,8 @@ void GenericCAO::initialize(const std::string &data)
player->setCAO(this);
}
}
+
+ m_enable_shaders = g_settings->getBool("enable_shaders");
}
void GenericCAO::processInitData(const std::string &data)
@@ -577,8 +580,17 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
return;
}
- video::E_MATERIAL_TYPE material_type = (m_prop.use_texture_alpha) ?
- video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+ if (m_enable_shaders) {
+ IShaderSource *shader_source = m_client->getShaderSource();
+ u32 shader_id = shader_source->getShader(
+ "object_shader",
+ TILE_MATERIAL_BASIC,
+ NDT_NORMAL);
+ m_material_type = shader_source->getShaderInfo(shader_id).material;
+ } else {
+ m_material_type = (m_prop.use_texture_alpha) ?
+ video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+ }
auto grabMatrixNode = [this] {
infostream << "GenericCAO::addToScene(): " << m_prop.visual << std::endl;
@@ -587,6 +599,18 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
m_matrixnode->grab();
};
+ auto setSceneNodeMaterial = [this] (scene::ISceneNode *node) {
+ node->setMaterialFlag(video::EMF_LIGHTING, false);
+ node->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
+ node->setMaterialFlag(video::EMF_FOG_ENABLE, true);
+ node->setMaterialType(m_material_type);
+
+ if (m_enable_shaders) {
+ node->setMaterialFlag(video::EMF_GOURAUD_SHADING, false);
+ node->setMaterialFlag(video::EMF_NORMALIZE_NORMALS, true);
+ }
+ };
+
if (m_prop.visual == "sprite") {
grabMatrixNode();
m_spritenode = RenderingEngine::get_scene_manager()->addBillboardSceneNode(
@@ -594,10 +618,9 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
m_spritenode->grab();
m_spritenode->setMaterialTexture(0,
tsrc->getTextureForMesh("unknown_node.png"));
- m_spritenode->setMaterialFlag(video::EMF_LIGHTING, false);
- m_spritenode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
- m_spritenode->setMaterialType(material_type);
- m_spritenode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
+
+ setSceneNodeMaterial(m_spritenode);
+
u8 li = m_last_light;
m_spritenode->setColor(video::SColor(255,li,li,li));
m_spritenode->setSize(v2f(m_prop.visual_size.X,
@@ -619,10 +642,10 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
{ // Front
scene::IMeshBuffer *buf = new scene::SMeshBuffer();
video::S3DVertex vertices[4] = {
- video::S3DVertex(-dx, -dy, 0, 0,0,0, c, 1,1),
- video::S3DVertex( dx, -dy, 0, 0,0,0, c, 0,1),
- video::S3DVertex( dx, dy, 0, 0,0,0, c, 0,0),
- video::S3DVertex(-dx, dy, 0, 0,0,0, c, 1,0),
+ video::S3DVertex(-dx, -dy, 0, 0,0,1, c, 1,1),
+ video::S3DVertex( dx, -dy, 0, 0,0,1, c, 0,1),
+ video::S3DVertex( dx, dy, 0, 0,0,1, c, 0,0),
+ video::S3DVertex(-dx, dy, 0, 0,0,1, c, 1,0),
};
if (m_is_player) {
// Move minimal Y position to 0 (feet position)
@@ -635,7 +658,14 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
- buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL;
+ buf->getMaterial().MaterialType = m_material_type;
+
+ if (m_enable_shaders) {
+ buf->getMaterial().EmissiveColor = c;
+ buf->getMaterial().setFlag(video::EMF_GOURAUD_SHADING, false);
+ buf->getMaterial().setFlag(video::EMF_NORMALIZE_NORMALS, true);
+ }
+
// Add to mesh
mesh->addMeshBuffer(buf);
buf->drop();
@@ -643,10 +673,10 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
{ // Back
scene::IMeshBuffer *buf = new scene::SMeshBuffer();
video::S3DVertex vertices[4] = {
- video::S3DVertex( dx,-dy, 0, 0,0,0, c, 1,1),
- video::S3DVertex(-dx,-dy, 0, 0,0,0, c, 0,1),
- video::S3DVertex(-dx, dy, 0, 0,0,0, c, 0,0),
- video::S3DVertex( dx, dy, 0, 0,0,0, c, 1,0),
+ video::S3DVertex( dx,-dy, 0, 0,0,-1, c, 1,1),
+ video::S3DVertex(-dx,-dy, 0, 0,0,-1, c, 0,1),
+ video::S3DVertex(-dx, dy, 0, 0,0,-1, c, 0,0),
+ video::S3DVertex( dx, dy, 0, 0,0,-1, c, 1,0),
};
if (m_is_player) {
// Move minimal Y position to 0 (feet position)
@@ -659,7 +689,14 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
buf->getMaterial().setFlag(video::EMF_LIGHTING, false);
buf->getMaterial().setFlag(video::EMF_BILINEAR_FILTER, false);
buf->getMaterial().setFlag(video::EMF_FOG_ENABLE, true);
- buf->getMaterial().MaterialType = video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
+ buf->getMaterial().MaterialType = m_material_type;
+
+ if (m_enable_shaders) {
+ buf->getMaterial().EmissiveColor = c;
+ buf->getMaterial().setFlag(video::EMF_GOURAUD_SHADING, false);
+ buf->getMaterial().setFlag(video::EMF_NORMALIZE_NORMALS, true);
+ }
+
// Add to mesh
mesh->addMeshBuffer(buf);
buf->drop();
@@ -683,10 +720,7 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
u8 li = m_last_light;
setMeshColor(m_meshnode->getMesh(), video::SColor(255,li,li,li));
- m_meshnode->setMaterialFlag(video::EMF_LIGHTING, false);
- m_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
- m_meshnode->setMaterialType(material_type);
- m_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
+ setSceneNodeMaterial(m_meshnode);
} else if (m_prop.visual == "mesh") {
grabMatrixNode();
scene::IAnimatedMesh *mesh = m_client->getMesh(m_prop.mesh, true);
@@ -704,10 +738,8 @@ void GenericCAO::addToScene(ITextureSource *tsrc)
setAnimatedMeshColor(m_animated_meshnode, video::SColor(255,li,li,li));
- m_animated_meshnode->setMaterialFlag(video::EMF_LIGHTING, true);
- m_animated_meshnode->setMaterialFlag(video::EMF_BILINEAR_FILTER, false);
- m_animated_meshnode->setMaterialType(material_type);
- m_animated_meshnode->setMaterialFlag(video::EMF_FOG_ENABLE, true);
+ setSceneNodeMaterial(m_animated_meshnode);
+
m_animated_meshnode->setMaterialFlag(video::EMF_BACK_FACE_CULLING,
m_prop.backface_culling);
} else
@@ -789,17 +821,41 @@ void GenericCAO::updateLightNoCheck(u8 light_at_pos)
return;
u8 li = decode_light(light_at_pos + m_glow);
+
if (li != m_last_light) {
m_last_light = li;
video::SColor color(255,li,li,li);
- if (m_meshnode) {
- setMeshColor(m_meshnode->getMesh(), color);
- } else if (m_animated_meshnode) {
- setAnimatedMeshColor(m_animated_meshnode, color);
- } else if (m_wield_meshnode) {
- m_wield_meshnode->setColor(color);
- } else if (m_spritenode) {
- m_spritenode->setColor(color);
+
+ if (m_enable_shaders) {
+ scene::ISceneNode *node = getSceneNode();
+
+ if (node == nullptr) {
+ return;
+ }
+
+ if (m_prop.visual == "upright_sprite") {
+ scene::IMesh *mesh = m_meshnode->getMesh();
+ for (u32 i = 0; i < mesh->getMeshBufferCount(); ++i) {
+ scene::IMeshBuffer* buf = mesh->getMeshBuffer(i);
+ video::SMaterial& material = buf->getMaterial();
+ material.EmissiveColor = color;
+ }
+ } else {
+ for (u32 i = 0; i < node->getMaterialCount(); ++i) {
+ video::SMaterial& material = node->getMaterial(i);
+ material.EmissiveColor = color;
+ }
+ }
+ } else {
+ if (m_meshnode) {
+ setMeshColor(m_meshnode->getMesh(), color);
+ } else if (m_animated_meshnode) {
+ setAnimatedMeshColor(m_animated_meshnode, color);
+ } else if (m_wield_meshnode) {
+ m_wield_meshnode->setColor(color);
+ } else if (m_spritenode) {
+ m_spritenode->setColor(color);
+ }
}
}
}
@@ -1101,16 +1157,13 @@ void GenericCAO::updateTextures(std::string mod)
m_current_texture_modifier = mod;
m_glow = m_prop.glow;
- video::E_MATERIAL_TYPE material_type = (m_prop.use_texture_alpha) ?
- video::EMT_TRANSPARENT_ALPHA_CHANNEL : video::EMT_TRANSPARENT_ALPHA_CHANNEL_REF;
-
if (m_spritenode) {
if (m_prop.visual == "sprite") {
std::string texturestring = "unknown_node.png";
if (!m_prop.textures.empty())
texturestring = m_prop.textures[0];
texturestring += mod;
- m_spritenode->getMaterial(0).MaterialType = material_type;
+ m_spritenode->getMaterial(0).MaterialType = m_material_type;
m_spritenode->getMaterial(0).MaterialTypeParam = 0.5f;
m_spritenode->setMaterialTexture(0,
tsrc->getTextureForMesh(texturestring));
@@ -1146,7 +1199,7 @@ void GenericCAO::updateTextures(std::string mod)
// Set material flags and texture
video::SMaterial& material = m_animated_meshnode->getMaterial(i);
- material.MaterialType = material_type;
+ material.MaterialType = m_material_type;
material.MaterialTypeParam = 0.5f;
material.TextureLayer[0].Texture = texture;
material.setFlag(video::EMF_LIGHTING, true);
@@ -1193,7 +1246,7 @@ void GenericCAO::updateTextures(std::string mod)
// Set material flags and texture
video::SMaterial& material = m_meshnode->getMaterial(i);
- material.MaterialType = material_type;
+ material.MaterialType = m_material_type;
material.MaterialTypeParam = 0.5f;
material.setFlag(video::EMF_LIGHTING, false);
material.setFlag(video::EMF_BILINEAR_FILTER, false);
diff --git a/src/client/content_cao.h b/src/client/content_cao.h
index 6f3b2f06b..a1098305a 100644
--- a/src/client/content_cao.h
+++ b/src/client/content_cao.h
@@ -125,6 +125,10 @@ private:
u8 m_last_light = 255;
bool m_is_visible = false;
s8 m_glow = 0;
+ // Material
+ video::E_MATERIAL_TYPE m_material_type;
+ // Settings
+ bool m_enable_shaders = false;
public:
GenericCAO(Client *client, ClientEnvironment *env);
diff --git a/src/client/shader.cpp b/src/client/shader.cpp
index f36ff3d85..eda415ce6 100644
--- a/src/client/shader.cpp
+++ b/src/client/shader.cpp
@@ -188,7 +188,7 @@ public:
delete setter;
}
- virtual void OnSetConstants(video::IMaterialRendererServices *services, s32 userData)
+ virtual void OnSetConstants(video::IMaterialRendererServices *services, s32 userData) override
{
video::IVideoDriver *driver = services->getVideoDriver();
sanity_check(driver != NULL);
@@ -198,6 +198,12 @@ public:
for (IShaderConstantSetter *setter : m_setters)
setter->onSetConstants(services, is_highlevel);
}
+
+ virtual void OnSetMaterial(const video::SMaterial& material) override
+ {
+ for (IShaderConstantSetter *setter : m_setters)
+ setter->onSetMaterial(material);
+ }
};
diff --git a/src/client/shader.h b/src/client/shader.h
index 583c776f4..109d39336 100644
--- a/src/client/shader.h
+++ b/src/client/shader.h
@@ -67,6 +67,8 @@ public:
virtual ~IShaderConstantSetter() = default;
virtual void onSetConstants(video::IMaterialRendererServices *services,
bool is_highlevel) = 0;
+ virtual void onSetMaterial(const video::SMaterial& material)
+ { }
};