Navigation Menu

Skip to content

Commit

Permalink
Add minimap feature
Browse files Browse the repository at this point in the history
  • Loading branch information
RealBadAngel authored and est31 committed Jun 27, 2015
1 parent 3376d2e commit ffd16e3
Show file tree
Hide file tree
Showing 23 changed files with 883 additions and 30 deletions.
1 change: 1 addition & 0 deletions build/android/jni/Android.mk
Expand Up @@ -183,6 +183,7 @@ LOCAL_SRC_FILES := \
jni/src/mg_decoration.cpp \
jni/src/mg_ore.cpp \
jni/src/mg_schematic.cpp \
jni/src/minimap.cpp \
jni/src/mods.cpp \
jni/src/nameidmapping.cpp \
jni/src/nodedef.cpp \
Expand Down
32 changes: 32 additions & 0 deletions client/shaders/minimap_shader/opengl_fragment.glsl
@@ -0,0 +1,32 @@
uniform sampler2D baseTexture;
uniform sampler2D normalTexture;
uniform vec3 yawVec;

void main (void)
{
vec2 uv = gl_TexCoord[0].st;

//texture sampling rate
const float step = 1.0 / 256.0;
float tl = texture2D(normalTexture, vec2(uv.x - step, uv.y + step)).r;
float t = texture2D(normalTexture, vec2(uv.x - step, uv.y - step)).r;
float tr = texture2D(normalTexture, vec2(uv.x + step, uv.y + step)).r;
float r = texture2D(normalTexture, vec2(uv.x + step, uv.y)).r;
float br = texture2D(normalTexture, vec2(uv.x + step, uv.y - step)).r;
float b = texture2D(normalTexture, vec2(uv.x, uv.y - step)).r;
float bl = texture2D(normalTexture, vec2(uv.x - step, uv.y - step)).r;
float l = texture2D(normalTexture, vec2(uv.x - step, uv.y)).r;
float dX = (tr + 2.0 * r + br) - (tl + 2.0 * l + bl);
float dY = (bl + 2.0 * b + br) - (tl + 2.0 * t + tr);
vec4 bump = vec4 (normalize(vec3 (dX, dY, 0.1)),1.0);
float height = 2.0 * texture2D(normalTexture, vec2(uv.x, uv.y)).r - 1.0;
vec4 base = texture2D(baseTexture, uv).rgba;
vec3 L = normalize(vec3(0.0, 0.75, 1.0));
float specular = pow(clamp(dot(reflect(L, bump.xyz), yawVec), 0.0, 1.0), 1.0);
float diffuse = dot(yawVec, bump.xyz);

vec3 color = (1.1 * diffuse + 0.05 * height + 0.5 * specular) * base.rgb;
vec4 col = vec4(color.rgb, base.a);
col *= gl_Color;
gl_FragColor = vec4(col.rgb, base.a);
}
11 changes: 11 additions & 0 deletions client/shaders/minimap_shader/opengl_vertex.glsl
@@ -0,0 +1,11 @@
uniform mat4 mWorldViewProj;
uniform mat4 mInvWorld;
uniform mat4 mTransWorld;
uniform mat4 mWorld;

void main(void)
{
gl_TexCoord[0] = gl_MultiTexCoord0;
gl_Position = mWorldViewProj * gl_Vertex;
gl_FrontColor = gl_BackColor = gl_Color;
}
1 change: 1 addition & 0 deletions src/CMakeLists.txt
Expand Up @@ -423,6 +423,7 @@ set(client_SRCS
main.cpp
mapblock_mesh.cpp
mesh.cpp
minimap.cpp
particles.cpp
shader.cpp
sky.cpp
Expand Down
35 changes: 21 additions & 14 deletions src/client.cpp
Expand Up @@ -34,6 +34,7 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#include "porting.h"
#include "mapblock_mesh.h"
#include "mapblock.h"
#include "minimap.h"
#include "settings.h"
#include "profiler.h"
#include "gettext.h"
Expand Down Expand Up @@ -185,11 +186,6 @@ void * MeshUpdateThread::Thread()
ScopeProfiler sp(g_profiler, "Client: Mesh making");

MapBlockMesh *mesh_new = new MapBlockMesh(q->data, m_camera_offset);
if(mesh_new->getMesh()->getMeshBufferCount() == 0)
{
delete mesh_new;
mesh_new = NULL;
}

MeshUpdateResult r;
r.p = q->p;
Expand Down Expand Up @@ -274,6 +270,7 @@ Client::Client(
// Add local player
m_env.addPlayer(new LocalPlayer(this, playername));

m_mapper = new Mapper(device, this);
m_cache_save_interval = g_settings->getU16("server_map_save_interval");

m_cache_smooth_lighting = g_settings->getBool("smooth_lighting");
Expand Down Expand Up @@ -537,27 +534,37 @@ void Client::step(float dtime)
*/
{
int num_processed_meshes = 0;
while(!m_mesh_update_thread.m_queue_out.empty())
while (!m_mesh_update_thread.m_queue_out.empty())
{
num_processed_meshes++;
MeshUpdateResult r = m_mesh_update_thread.m_queue_out.pop_frontNoEx();
MapBlock *block = m_env.getMap().getBlockNoCreateNoEx(r.p);
if(block) {
MinimapMapblock *minimap_mapblock = NULL;
if (block) {
// Delete the old mesh
if(block->mesh != NULL)
{
// TODO: Remove hardware buffers of meshbuffers of block->mesh
if (block->mesh != NULL) {
delete block->mesh;
block->mesh = NULL;
}

// Replace with the new mesh
block->mesh = r.mesh;
if (r.mesh)
minimap_mapblock = r.mesh->getMinimapMapblock();

if (r.mesh && r.mesh->getMesh()->getMeshBufferCount() == 0) {
delete r.mesh;
block->mesh = NULL;
} else {
// Replace with the new mesh
block->mesh = r.mesh;
}
} else {
delete r.mesh;
minimap_mapblock = NULL;
}

if(r.ack_block_to_server) {
m_mapper->addBlock(r.p, minimap_mapblock);

if (r.ack_block_to_server) {
/*
Acknowledge block
[0] u8 count
Expand All @@ -567,7 +574,7 @@ void Client::step(float dtime)
}
}

if(num_processed_meshes > 0)
if (num_processed_meshes > 0)
g_profiler->graphAdd("num_processed_meshes", num_processed_meshes);
}

Expand Down
6 changes: 6 additions & 0 deletions src/client.h
Expand Up @@ -48,6 +48,8 @@ struct MapDrawControl;
class MtEventManager;
struct PointedThing;
class Database;
class Mapper;
struct MinimapMapblock;

struct QueuedMeshUpdate
{
Expand Down Expand Up @@ -504,6 +506,9 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
float getCurRate(void);
float getAvgRate(void);

Mapper* getMapper ()
{ return m_mapper; }

// IGameDef interface
virtual IItemDefManager* getItemDefManager();
virtual INodeDefManager* getNodeDefManager();
Expand Down Expand Up @@ -583,6 +588,7 @@ class Client : public con::PeerHandler, public InventoryManager, public IGameDef
ParticleManager m_particle_manager;
con::Connection m_con;
IrrlichtDevice *m_device;
Mapper *m_mapper;
// Server serialization version
u8 m_server_ser_ver;
// Used version of the protocol with server
Expand Down
40 changes: 40 additions & 0 deletions src/client/tile.cpp
Expand Up @@ -382,6 +382,8 @@ class TextureSource : public IWritableTextureSource
video::IImage* generateImage(const std::string &name);

video::ITexture* getNormalTexture(const std::string &name);
video::SColor getTextureAverageColor(const std::string &name);

private:

// The id of the thread that is allowed to use irrlicht directly
Expand Down Expand Up @@ -2008,3 +2010,41 @@ video::ITexture* TextureSource::getNormalTexture(const std::string &name)
}
return NULL;
}

video::SColor TextureSource::getTextureAverageColor(const std::string &name)
{
video::IVideoDriver *driver = m_device->getVideoDriver();
video::SColor c(0, 0, 0, 0);
u32 id;
video::ITexture *texture = getTexture(name, &id);
video::IImage *image = driver->createImage(texture,
core::position2d<s32>(0, 0),
texture->getOriginalSize());
u32 total = 0;
u32 tR = 0;
u32 tG = 0;
u32 tB = 0;
core::dimension2d<u32> dim = image->getDimension();
u16 step = 1;
if (dim.Width > 16)
step = dim.Width / 16;
for (u16 x = 0; x < dim.Width; x += step) {
for (u16 y = 0; y < dim.Width; y += step) {
c = image->getPixel(x,y);
if (c.getAlpha() > 0) {
total++;
tR += c.getRed();
tG += c.getGreen();
tB += c.getBlue();
}
}
}
image->drop();
if (total > 0) {
c.setRed(tR / total);
c.setGreen(tG / total);
c.setBlue(tB / total);
}
c.setAlpha(255);
return c;
}
2 changes: 2 additions & 0 deletions src/client/tile.h
Expand Up @@ -110,6 +110,7 @@ class ITextureSource : public ISimpleTextureSource
virtual video::ITexture* generateTextureFromMesh(
const TextureFromMeshParams &params)=0;
virtual video::ITexture* getNormalTexture(const std::string &name)=0;
virtual video::SColor getTextureAverageColor(const std::string &name)=0;
};

class IWritableTextureSource : public ITextureSource
Expand All @@ -131,6 +132,7 @@ class IWritableTextureSource : public ITextureSource
virtual void insertSourceImage(const std::string &name, video::IImage *img)=0;
virtual void rebuildImagesAndTextures()=0;
virtual video::ITexture* getNormalTexture(const std::string &name)=0;
virtual video::SColor getTextureAverageColor(const std::string &name)=0;
};

IWritableTextureSource* createTextureSource(IrrlichtDevice *device);
Expand Down
4 changes: 4 additions & 0 deletions src/defaultsettings.cpp
Expand Up @@ -43,6 +43,7 @@ void set_default_settings(Settings *settings)
settings->setDefault("keymap_special1", "KEY_KEY_E");
settings->setDefault("keymap_chat", "KEY_KEY_T");
settings->setDefault("keymap_cmd", "/");
settings->setDefault("keymap_minimap", "KEY_F9");
settings->setDefault("keymap_console", "KEY_F10");
settings->setDefault("keymap_rangeselect", "KEY_KEY_R");
settings->setDefault("keymap_freemove", "KEY_KEY_K");
Expand Down Expand Up @@ -176,6 +177,9 @@ void set_default_settings(Settings *settings)
settings->setDefault("enable_particles", "true");
settings->setDefault("enable_mesh_cache", "true");

settings->setDefault("enable_minimap", "true");
settings->setDefault("minimap_shape_round", "true");

settings->setDefault("curl_timeout", "5000");
settings->setDefault("curl_parallel_limit", "8");
settings->setDefault("curl_file_download_timeout", "300000");
Expand Down
11 changes: 7 additions & 4 deletions src/drawscene.cpp
Expand Up @@ -416,10 +416,11 @@ void draw_plain(Camera& camera, bool show_hud, Hud& hud,
camera.drawWieldedTool();
}

void draw_scene(video::IVideoDriver* driver, scene::ISceneManager* smgr,
Camera& camera, Client& client, LocalPlayer* player, Hud& hud,
gui::IGUIEnvironment* guienv, std::vector<aabb3f> hilightboxes,
const v2u32& screensize, video::SColor skycolor, bool show_hud)
void draw_scene(video::IVideoDriver *driver, scene::ISceneManager *smgr,
Camera &camera, Client& client, LocalPlayer *player, Hud &hud,
Mapper &mapper, gui::IGUIEnvironment *guienv,
std::vector<aabb3f> hilightboxes, const v2u32 &screensize,
video::SColor skycolor, bool show_hud, bool show_minimap)
{
TimeTaker timer("smgr");

Expand Down Expand Up @@ -484,6 +485,8 @@ void draw_scene(video::IVideoDriver* driver, scene::ISceneManager* smgr,
hud.drawCrosshair();
hud.drawHotbar(client.getPlayerItem());
hud.drawLuaElements(camera.getOffset());
if (show_minimap)
mapper.drawMinimap();
}

guienv->drawAll();
Expand Down
16 changes: 9 additions & 7 deletions src/drawscene.h
Expand Up @@ -22,16 +22,18 @@ with this program; if not, write to the Free Software Foundation, Inc.,

#include "camera.h"
#include "hud.h"
#include "minimap.h"
#include "irrlichttypes_extrabloated.h"


void draw_load_screen(const std::wstring &text, IrrlichtDevice* device,
gui::IGUIEnvironment* guienv, float dtime=0, int percent=0,
bool clouds=true);
void draw_load_screen(const std::wstring &text, IrrlichtDevice *device,
gui::IGUIEnvironment *guienv, float dtime = 0, int percent = 0,
bool clouds = true);

void draw_scene(video::IVideoDriver* driver, scene::ISceneManager* smgr,
Camera& camera, Client& client, LocalPlayer* player, Hud& hud,
gui::IGUIEnvironment* guienv, std::vector<aabb3f> hilightboxes,
const v2u32& screensize, video::SColor skycolor, bool show_hud);
void draw_scene(video::IVideoDriver *driver, scene::ISceneManager *smgr,
Camera &camera, Client &client, LocalPlayer *player, Hud &hud,
Mapper &mapper, gui::IGUIEnvironment *guienv,
std::vector<aabb3f> hilightboxes, const v2u32 &screensize,
video::SColor skycolor, bool show_hud, bool show_minimap);

#endif /* DRAWSCENE_H_ */

0 comments on commit ffd16e3

Please sign in to comment.