Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Add wielded (and CAOs) shader
  • Loading branch information
RealBadAngel committed Jul 21, 2015
1 parent 5b0c719 commit 6035069
Show file tree
Hide file tree
Showing 11 changed files with 224 additions and 40 deletions.
114 changes: 114 additions & 0 deletions client/shaders/wielded_shader/opengl_fragment.glsl
@@ -0,0 +1,114 @@
uniform sampler2D baseTexture;
uniform sampler2D normalTexture;
uniform sampler2D textureFlags;

uniform vec4 skyBgColor;
uniform float fogDistance;
uniform vec3 eyePosition;

varying vec3 vPosition;
varying vec3 worldPosition;

varying vec3 eyeVec;
varying vec3 lightVec;

bool normalTexturePresent = false;
bool texTileableHorizontal = false;
bool texTileableVertical = false;
bool texSeamless = false;

const float e = 2.718281828459;
const float BS = 10.0;

void get_texture_flags()
{
vec4 flags = texture2D(textureFlags, vec2(0.0, 0.0));
if (flags.r > 0.5) {
normalTexturePresent = true;
}
if (flags.g > 0.5) {
texTileableHorizontal = true;
}
if (flags.b > 0.5) {
texTileableVertical = true;
}
if (texTileableHorizontal && texTileableVertical) {
texSeamless = true;
}
}

float intensity(vec3 color)
{
return (color.r + color.g + color.b) / 3.0;
}

float get_rgb_height(vec2 uv)
{
if (texSeamless) {
return intensity(texture2D(baseTexture, uv).rgb);
} else {
return intensity(texture2D(baseTexture, clamp(uv, 0.0, 0.999)).rgb);
}
}

vec4 get_normal_map(vec2 uv)
{
vec4 bump = texture2D(normalTexture, uv).rgba;
bump.xyz = normalize(bump.xyz * 2.0 - 1.0);
return bump;
}

void main(void)
{
vec3 color;
vec4 bump;
vec2 uv = gl_TexCoord[0].st;
bool use_normalmap = false;
get_texture_flags();

#if USE_NORMALMAPS == 1
if (normalTexturePresent) {
bump = get_normal_map(uv);
use_normalmap = true;
}
#endif

if (GENERATE_NORMALMAPS == 1 && normalTexturePresent == false) {
float tl = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y + SAMPLE_STEP));
float t = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y - SAMPLE_STEP));
float tr = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y + SAMPLE_STEP));
float r = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y));
float br = get_rgb_height(vec2(uv.x + SAMPLE_STEP, uv.y - SAMPLE_STEP));
float b = get_rgb_height(vec2(uv.x, uv.y - SAMPLE_STEP));
float bl = get_rgb_height(vec2(uv.x -SAMPLE_STEP, uv.y - SAMPLE_STEP));
float l = get_rgb_height(vec2(uv.x - SAMPLE_STEP, uv.y));
float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
bump = vec4(normalize(vec3 (dX, dY, NORMALMAPS_STRENGTH)), 1.0);
use_normalmap = true;
}

vec4 base = texture2D(baseTexture, uv).rgba;

#ifdef ENABLE_BUMPMAPPING
if (use_normalmap) {
vec3 L = normalize(lightVec);
vec3 E = normalize(eyeVec);
float specular = pow(clamp(dot(reflect(L, bump.xyz), E), 0.0, 1.0), 1.0);
float diffuse = dot(-E,bump.xyz);
color = (diffuse + 0.1 * specular) * base.rgb;
} else {
color = base.rgb;
}
#else
color = base.rgb;
#endif

vec4 col = vec4(color.rgb, base.a);
col *= gl_Color;
if (fogDistance != 0.0) {
float d = max(0.0, min(vPosition.z / fogDistance * 1.5 - 0.6, 1.0));
col = mix(col, skyBgColor, d);
}
gl_FragColor = vec4(col.rgb, base.a);
}
35 changes: 35 additions & 0 deletions client/shaders/wielded_shader/opengl_vertex.glsl
@@ -0,0 +1,35 @@
uniform mat4 mWorldViewProj;
uniform mat4 mInvWorld;
uniform mat4 mTransWorld;
uniform mat4 mWorld;

uniform float dayNightRatio;
uniform vec3 eyePosition;
uniform float animationTimer;

varying vec3 vPosition;
varying vec3 worldPosition;

varying vec3 eyeVec;
varying vec3 lightVec;
varying vec3 tsEyeVec;
varying vec3 tsLightVec;

const float e = 2.718281828459;
const float BS = 10.0;

void main(void)
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = mWorldViewProj * gl_Vertex;

vPosition = gl_Position.xyz;
worldPosition = (mWorld * gl_Vertex).xyz;

vec3 sunPosition = vec3 (0.0, eyePosition.y * BS + 900.0, 0.0);

lightVec = sunPosition - worldPosition;
eyeVec = -(gl_ModelViewMatrix * gl_Vertex).xyz;

gl_FrontColor = gl_BackColor = gl_Color;
}
9 changes: 2 additions & 7 deletions src/camera.cpp
Expand Up @@ -92,10 +92,9 @@ Camera::Camera(scene::ISceneManager* smgr, MapDrawControl& draw_control,
// all other 3D scene nodes and before the GUI.
m_wieldmgr = smgr->createNewSceneManager();
m_wieldmgr->addCameraSceneNode();
m_wieldnode = new WieldMeshSceneNode(m_wieldmgr->getRootSceneNode(), m_wieldmgr, -1, true);
m_wieldnode = new WieldMeshSceneNode(m_wieldmgr->getRootSceneNode(), m_wieldmgr, -1, false);
m_wieldnode->setItem(ItemStack(), m_gamedef);
m_wieldnode->drop(); // m_wieldmgr grabbed it
m_wieldlightnode = m_wieldmgr->addLightSceneNode(NULL, v3f(0.0, 50.0, 0.0));

/* TODO: Add a callback function so these can be updated when a setting
* changes. At this point in time it doesn't matter (e.g. /set
Expand Down Expand Up @@ -451,11 +450,7 @@ void Camera::update(LocalPlayer* player, f32 frametime, f32 busytime,
m_wieldnode->setPosition(wield_position);
m_wieldnode->setRotation(wield_rotation);

// Shine light upon the wield mesh
video::SColor black(255,0,0,0);
m_wieldmgr->setAmbientLight(player->light_color.getInterpolated(black, 0.7));
m_wieldlightnode->getLightData().DiffuseColor = player->light_color.getInterpolated(black, 0.3);
m_wieldlightnode->setPosition(v3f(30+5*sin(2*player->getYaw()*M_PI/180), -50, 0));
m_wieldnode->setColor(player->light_color);

// Render distance feedback loop
updateViewingRange(frametime, busytime);
Expand Down
1 change: 0 additions & 1 deletion src/camera.h
Expand Up @@ -159,7 +159,6 @@ class Camera

scene::ISceneManager* m_wieldmgr;
WieldMeshSceneNode* m_wieldnode;
scene::ILightSceneNode* m_wieldlightnode;

// draw control
MapDrawControl& m_draw_control;
Expand Down
17 changes: 9 additions & 8 deletions src/client/tile.cpp
Expand Up @@ -385,7 +385,8 @@ class TextureSource : public IWritableTextureSource

video::ITexture* getNormalTexture(const std::string &name);
video::SColor getTextureAverageColor(const std::string &name);
video::ITexture *getShaderFlagsTexture(TileDef *tiledef, TileSpec *tile);
video::ITexture *getShaderFlagsTexture(
bool normamap_present, bool tileable_vertical, bool tileable_horizontal);

private:

Expand Down Expand Up @@ -2050,14 +2051,14 @@ video::SColor TextureSource::getTextureAverageColor(const std::string &name)
return c;
}

video::ITexture *TextureSource::getShaderFlagsTexture(TileDef *tiledef, TileSpec *tile)

video::ITexture *TextureSource::getShaderFlagsTexture(
bool normalmap_present, bool tileable_vertical, bool tileable_horizontal)
{
std::string tname = "__shaderFlagsTexture";

bool normalmap_present = tile->normal_texture ? true : false;
tname += normalmap_present ? "1" : "0";
tname += tiledef->tileable_horizontal ? "1" : "0";
tname += tiledef->tileable_vertical ? "1" : "0";
tname += tileable_horizontal ? "1" : "0";
tname += tileable_vertical ? "1" : "0";

if (isKnownSourceImage(tname)) {
return getTexture(tname);
Expand All @@ -2069,8 +2070,8 @@ video::ITexture *TextureSource::getShaderFlagsTexture(TileDef *tiledef, TileSpec
video::SColor c(
255,
normalmap_present ? 255 : 0,
tiledef->tileable_horizontal ? 255 : 0,
tiledef->tileable_vertical ? 255 : 0);
tileable_horizontal ? 255 : 0,
tileable_vertical ? 255 : 0);
flags_image->setPixel(0, 0, c);
insertSourceImage(tname, flags_image);
flags_image->drop();
Expand Down
6 changes: 4 additions & 2 deletions src/client/tile.h
Expand Up @@ -113,7 +113,8 @@ class ITextureSource : public ISimpleTextureSource
const TextureFromMeshParams &params)=0;
virtual video::ITexture* getNormalTexture(const std::string &name)=0;
virtual video::SColor getTextureAverageColor(const std::string &name)=0;
virtual video::ITexture *getShaderFlagsTexture(TileDef *tiledef, TileSpec *tile)=0;
virtual video::ITexture *getShaderFlagsTexture(bool normamap_present,
bool tileable_vertical, bool tileable_horizontal)=0;
};

class IWritableTextureSource : public ITextureSource
Expand All @@ -136,7 +137,8 @@ class IWritableTextureSource : public ITextureSource
virtual void rebuildImagesAndTextures()=0;
virtual video::ITexture* getNormalTexture(const std::string &name)=0;
virtual video::SColor getTextureAverageColor(const std::string &name)=0;
virtual video::ITexture *getShaderFlagsTexture(TileDef *tiledef, TileSpec *tile)=0;
virtual video::ITexture *getShaderFlagsTexture(bool normamap_present,
bool tileable_vertical, bool tileable_horizontal)=0;
};

IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
Expand Down
36 changes: 36 additions & 0 deletions src/mesh.cpp
Expand Up @@ -33,6 +33,13 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define MY_ETLM_READ_ONLY video::ETLM_READ_ONLY
#endif

static void applyFacesShading(video::SColor& color, float factor)
{
color.setRed(core::clamp(core::round32(color.getRed()*factor), 0, 255));
color.setGreen(core::clamp(core::round32(color.getGreen()*factor), 0, 255));
color.setBlue(core::clamp(core::round32(color.getBlue()*factor), 0, 255));
}

scene::IAnimatedMesh* createCubeMesh(v3f scale)
{
video::SColor c(255,255,255,255);
Expand Down Expand Up @@ -165,6 +172,35 @@ void setMeshColor(scene::IMesh *mesh, const video::SColor &color)
}
}

void shadeMeshFaces(scene::IMesh *mesh)
{
if (mesh == NULL)
return;

u32 mc = mesh->getMeshBufferCount();
for (u32 j = 0; j < mc; j++) {
scene::IMeshBuffer *buf = mesh->getMeshBuffer(j);
const u32 stride = getVertexPitchFromType(buf->getVertexType());
u32 vertex_count = buf->getVertexCount();
u8 *vertices = (u8 *)buf->getVertices();
for (u32 i = 0; i < vertex_count; i++) {
video::S3DVertex *vertex = (video::S3DVertex *)(vertices + i * stride);
video::SColor &vc = vertex->Color;
if (vertex->Normal.Y < -0.5) {
applyFacesShading (vc, 0.447213);
} else if (vertex->Normal.Z > 0.5) {
applyFacesShading (vc, 0.670820);
} else if (vertex->Normal.Z < -0.5) {
applyFacesShading (vc, 0.670820);
} else if (vertex->Normal.X > 0.5) {
applyFacesShading (vc, 0.836660);
} else if (vertex->Normal.X < -0.5) {
applyFacesShading (vc, 0.836660);
}
}
}
}

void setMeshColorByNormalXYZ(scene::IMesh *mesh,
const video::SColor &colorX,
const video::SColor &colorY,
Expand Down
6 changes: 6 additions & 0 deletions src/mesh.h
Expand Up @@ -48,6 +48,12 @@ void translateMesh(scene::IMesh *mesh, v3f vec);
*/
void setMeshColor(scene::IMesh *mesh, const video::SColor &color);

/*
Shade mesh faces according to their normals
*/

void shadeMeshFaces(scene::IMesh *mesh);

/*
Set the color of all vertices in the mesh.
For each vertex, determine the largest absolute entry in
Expand Down
4 changes: 3 additions & 1 deletion src/nodedef.cpp
Expand Up @@ -1012,7 +1012,9 @@ void CNodeDefManager::fillTileAttribs(ITextureSource *tsrc, TileSpec *tile,
if (use_normal_texture) {
tile->normal_texture = tsrc->getNormalTexture(tiledef->name);
}
tile->flags_texture = tsrc->getShaderFlagsTexture(tiledef, tile);
tile->flags_texture = tsrc->getShaderFlagsTexture(
tile->normal_texture ? true : false,
tiledef->tileable_horizontal, tiledef->tileable_vertical);

// Material flags
tile->material_flags = 0;
Expand Down

0 comments on commit 6035069

Please sign in to comment.