Skip to content

Commit

Permalink
some small things
Browse files Browse the repository at this point in the history
  • Loading branch information
Desour committed Aug 27, 2024
1 parent 94ebaef commit 6078e4a
Show file tree
Hide file tree
Showing 6 changed files with 50 additions and 45 deletions.
3 changes: 1 addition & 2 deletions client/shaders/nodes_shader/opengl_vertex.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -171,8 +171,7 @@ void main(void)
// Generate waves with Perlin-type noise.
// The constants are calibrated such that they roughly
// correspond to the old sine waves.
// Note: The same function is implemented in clientmap.cpp.
// Keep them consistent!
// Note: The same thing is implemented in clientmap.cpp. Keep them consistent!
vec3 wavePos = (mWorld * pos).xyz + cameraOffset;
// The waves are slightly compressed along the z-axis to get
// wave-fronts along the x-axis.
Expand Down
2 changes: 1 addition & 1 deletion client/shaders/object_shader/opengl_fragment.glsl
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ void main(void)

// Apply posteffect color for the wieldhand, by blending it above this
// fragment.
// The alpha channel is not blended. There is posteffect geometry behind the
// The alpha channel is not blended. The posteffect geometry behind the
// wieldhand already makes the image less transparent.
// wield_posteffect_color.rgb is premultiplied.
col.rgb = col.rgb * (1.0 - wield_posteffect_color.a) + wield_posteffect_color.rgb;
Expand Down
64 changes: 34 additions & 30 deletions src/client/clientmap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "util/basic_macros.h"
#include "util/convex_polygon.h"
#include "client/renderingengine.h"
#include <SViewFrustum.h>
#include <queue>

namespace {
Expand Down Expand Up @@ -1058,7 +1057,8 @@ int ClientMap::getBackgroundBrightness(float max_d, u32 daylight_factor,
return ret;
}

std::vector<std::pair<std::vector<v2f>, video::SColor>> ClientMap::getPostFxPolygons()
std::vector<std::pair<std::vector<v2f>, video::SColor>>
ClientMap::getPostFxPolygons()
{
using mat4 = core::matrix4;
using v4f = std::array<f32, 4>;
Expand Down Expand Up @@ -1100,7 +1100,7 @@ std::vector<std::pair<std::vector<v2f>, video::SColor>> ClientMap::getPostFxPoly
video::SColor post_color = features.post_effect_color;

if (features.post_effect_color_shaded) {
auto apply_light = [] (u32 color, u32 light) {
auto apply_light = [](u32 color, u32 light) {
return core::clamp(core::round32(color * light / 255.0f), 0, 255);
};
post_color.setRed(apply_light(post_color.getRed(), m_camera_light_color.getRed()));
Expand All @@ -1115,7 +1115,7 @@ std::vector<std::pair<std::vector<v2f>, video::SColor>> ClientMap::getPostFxPoly
return post_color;
};

std::optional<LiquidWaveParams> liquid_wave_params =
const std::optional<LiquidWaveParams> liquid_wave_params =
g_settings->getBool("enable_shaders")
&& g_settings->getBool("enable_waving_water") ?
LiquidWaveParams{
Expand All @@ -1126,7 +1126,7 @@ std::vector<std::pair<std::vector<v2f>, video::SColor>> ClientMap::getPostFxPoly
}
: std::optional<LiquidWaveParams>(std::nullopt);

bool smooth_lighting = g_settings->getBool("smooth_lighting");
const bool smooth_lighting = g_settings->getBool("smooth_lighting");

// transposed inversed view-(quasi)projection matrix
// This matrix moves the near plane from camera-offset-local world-space to
Expand All @@ -1135,7 +1135,7 @@ std::vector<std::pair<std::vector<v2f>, video::SColor>> ClientMap::getPostFxPoly
// (Only the near plane is transformed correctly (we don't care about the
// rest of the space), therefore "(quasi)projection".)
// (We also don't care for positive scalar multiples, so the adjugate would
// also work.)
// work too.)
const mat4 vp_mat_ti = [&]() {
auto camera_node = m_client->getCamera()->getCameraNode();

Expand All @@ -1161,27 +1161,25 @@ std::vector<std::pair<std::vector<v2f>, video::SColor>> ClientMap::getPostFxPoly
// Go through all 8 nodes that the near plane possibly instersects with

// the one in the (-1,-1,-1) corner
v3f minus_corner(
const v3s16 minus_corner(
std::floor(m_camera_position.X/BS),
std::floor(m_camera_position.Y/BS),
std::floor(m_camera_position.Z/BS)
);

for (const v3f &corner_dir : {
v3f(0.0f, 0.0f, 0.0f),
v3f(1.0f, 0.0f, 0.0f),
v3f(0.0f, 1.0f, 0.0f),
v3f(1.0f, 1.0f, 0.0f),
v3f(0.0f, 0.0f, 1.0f),
v3f(1.0f, 0.0f, 1.0f),
v3f(0.0f, 1.0f, 1.0f),
v3f(1.0f, 1.0f, 1.0f),
for (const v3s16 &corner_dir : {
v3s16(0, 0, 0),
v3s16(1, 0, 0),
v3s16(0, 1, 0),
v3s16(1, 1, 0),
v3s16(0, 0, 1),
v3s16(1, 0, 1),
v3s16(0, 1, 1),
v3s16(1, 1, 1),
}) {
v3f node_pos_no_BS = minus_corner + corner_dir;
v3f node_pos = node_pos_no_BS * BS;
v3s16 npos = floatToInt(node_pos, BS);
const v3s16 npos = minus_corner + corner_dir;

MapNode node = getNode(npos);
const MapNode node = getNode(npos);

const ContentFeatures &features = m_nodedef->get(node);
const video::SColor post_color = get_post_color(features);
Expand All @@ -1202,9 +1200,9 @@ std::vector<std::pair<std::vector<v2f>, video::SColor>> ClientMap::getPostFxPoly
mat4 world_mat_i = mat4::EM4CONST_IDENTITY;
world_mat_i.setTranslation(intToFloat(-npos + m_camera_offset, 1.0f));
world_mat_i.setScale(1.0f/BS);
mat4 world_mat_ti(world_mat_i, mat4::EM4CONST_TRANSPOSED);
const mat4 world_mat_ti(world_mat_i, mat4::EM4CONST_TRANSPOSED);

mat4 wvp_mat_ti = vp_mat_ti * world_mat_ti;
const mat4 wvp_mat_ti = vp_mat_ti * world_mat_ti;

auto clip_polygon_by_objsp_plane = [&](std::vector<v2f> &polyg,
const v4f &clip_plane) -> bool {
Expand Down Expand Up @@ -1341,24 +1339,29 @@ video::SColorf ClientMap::renderPostFx()

// Color for wield item
// We take the weighted (by area and alpha) sum of all polygons.
// The result is premultiplied.
// The result is alpha-premultiplied.
video::SColorf color_sum(0.0f, 0.0f, 0.0f, 0.0f);

for (const auto &[polygon, post_color] : polygons) {
video::SColorf color = post_color;

color.a *= get_convex_polygon_area(polygon);
// (screen size and aspect ratio don't matter for relative screen area)
f32 area = get_convex_polygon_area(polygon);
color.a *= area;

color_sum.r += color.a * color.r;
color_sum.g += color.a * color.g;
color_sum.b += color.a * color.b;
color_sum.a = color_sum.a + color.a;
}

color_sum.a = rangelim(color_sum.a, 0.0f, 1.0f);
color_sum.r = rangelim(color_sum.r, 0.0f, color_sum.a);
color_sum.g = rangelim(color_sum.g, 0.0f, color_sum.a);
color_sum.b = rangelim(color_sum.b, 0.0f, color_sum.a);
if (color_sum.a > 1.0f) {
f32 reci_a = 1.0f / color_sum.a;
color_sum.a *= reci_a;
color_sum.r *= reci_a;
color_sum.g *= reci_a;
color_sum.b *= reci_a;
}

return color_sum;
}
Expand Down Expand Up @@ -1608,7 +1611,8 @@ void ClientMap::updateTransparentMeshBuffers()
m_needs_update_transparent_meshes = false;
}

std::optional<std::array<f32, 4>> ClientMap::getLiquidTopFaceHeights(v3s16 pos,
std::optional<std::array<f32, 4>>
ClientMap::getLiquidTopFaceHeights(v3s16 pos,
const std::optional<LiquidWaveParams> &wave_params)
{
std::optional<std::array<f32, 4>> heights_opt = std::nullopt;
Expand Down Expand Up @@ -1652,7 +1656,7 @@ std::optional<std::array<f32, 4>> ClientMap::getLiquidTopFaceHeights(v3s16 pos,
return heights_opt;

// do the wave
// Note: The same function is implemented in the nodes vertex shader. Keep
// Note: The same thing is implemented in the nodes vertex shader. Keep
// them consistent!
auto wave_func = [&wave_params](v3f wavePos) {
using v4f = std::array<f32, 4>;
Expand Down
10 changes: 6 additions & 4 deletions src/client/clientmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,8 +124,8 @@ class ClientMap : public Map, public scene::ISceneNode
* Finds the heights of the corners of the top face of a liquid node at the
* given pos.
*
* Note: Liquid top faces are not quads, but two triangles, split by a plane
* through the -x-y and +x+y vertices.
* Note: Liquid top faces are not quads, but two triangles, split by a
* diagonal.
*
* @param pos The position of the node.
* @param wave_params If given, the corner heights are waved like in the node
Expand All @@ -134,7 +134,8 @@ class ClientMap : public Map, public scene::ISceneNode
* following order: {-x-z, +x-z, -x+z, +x+z}
* Or nullopt if there is no lquid top face.
*/
std::optional<std::array<f32, 4>> getLiquidTopFaceHeights(v3s16 pos,
std::optional<std::array<f32, 4>>
getLiquidTopFaceHeights(v3s16 pos,
const std::optional<LiquidWaveParams> &wave_params);

void onSettingChanged(const std::string &name);
Expand All @@ -152,7 +153,8 @@ class ClientMap : public Map, public scene::ISceneNode
void updateTransparentMeshBuffers();

// helper for renderPostFx
std::vector<std::pair<std::vector<v2f>, video::SColor>> getPostFxPolygons();
std::vector<std::pair<std::vector<v2f>, video::SColor>>
getPostFxPolygons();


// Orders blocks by distance to the camera
Expand Down
12 changes: 6 additions & 6 deletions src/util/convex_polygon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@ with this program; if not, write to the Free Software Foundation, Inc.,
*/

#include "convex_polygon.h"
#include <IVideoDriver.h>
#include <algorithm>

#ifndef SERVER

#include <IVideoDriver.h>

void draw_convex_polygon(video::IVideoDriver *driver, const std::vector<v2f> &polygon,
const video::SColor &color)
{
Expand Down Expand Up @@ -66,7 +67,6 @@ f32 get_convex_polygon_area(const std::vector<v2f> &polygon)
if (polygon.size() < 3)
return 0.0f;
// sum up the areas of all triangles
// (screen size and aspect ratio don't matter)
f32 area = 0.0f;
const v2f &v1 = polygon[0];
for (size_t i = 2; i < polygon.size(); ++i) {
Expand Down Expand Up @@ -102,7 +102,7 @@ std::vector<v2f> clip_convex_polygon(const std::vector<v2f> &polygon, v3f clip_l
// * no vertices are clipped away
// * clip_line intersects the polygon at two points.
// There is hence one (possibly over the list end) contiguous sub-list of
// vertices are all in (not clipped)
// vertices that are all in (not clipped)
//
// is_in applied on the polygon vertices looks like this:
// {in, in, in, out, out, in, in}
Expand All @@ -119,7 +119,7 @@ std::vector<v2f> clip_convex_polygon(const std::vector<v2f> &polygon, v3f clip_l
first_out = std::find_if(polygon.begin() + 1, polygon.end(), is_out);
if (first_out == polygon.end()) {
// all are in
polygon_clipped = std::move(polygon);
polygon_clipped = polygon;
return polygon_clipped;
}
last_in = first_out - 1;
Expand Down Expand Up @@ -152,8 +152,8 @@ std::vector<v2f> clip_convex_polygon(const std::vector<v2f> &polygon, v3f clip_l

auto split_edge = [&](const v2f &p1, const v2f &p2) {
auto homogenize = [](const v2f &p) { return v3f(p.X, p.Y, 1.0f); };
// find a pos that is on the splitter-line and between p1 and p2
// compute: meet(line, join(p1, p2))
// find a pos that is on clip_line and between p1 and p2
// compute: meet(clip_line, join(p1, p2))
v3f pos_homo = clip_line.crossProduct(homogenize(p1).crossProduct(homogenize(p2)));
// dehomogenize
return v2f(pos_homo.X / pos_homo.Z, pos_homo.Y / pos_homo.Z);
Expand Down
4 changes: 2 additions & 2 deletions src/util/convex_polygon.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,10 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "irrlichttypes_bloated.h"
#include <vector>

namespace irr::video { class IVideoDriver; }

#ifndef SERVER

namespace irr::video { class IVideoDriver; }

/** Draws a convex polygon over the whole screen.
* @param polygon Ordered list of corner vertices.
* X coordinates go from 0 (left) to 1 (right), Y from 0 (up) to
Expand Down

0 comments on commit 6078e4a

Please sign in to comment.