#include "stdafx.h" #include "BannerRenderer.h" #include "BannerModel.h" #include "ModelPart.h" #include "TileEntityRenderDispatcher.h" #include "Textures.h" #include "BufferedImage.h" #include #include "../Minecraft.World/net.minecraft.world.level.tile.entity.h" #include "../Minecraft.World/net.minecraft.world.level.tile.h" #include "../Minecraft.World/net.minecraft.world.level.h" #include "../Minecraft.World/DyePowderItem.h" #include "../Minecraft.World/Entity.h" #include "../Minecraft.World/Level.h" #include "../Minecraft.World/ItemInstance.h" #include "../Minecraft.World/com.mojang.nbt.h" #include "../Minecraft.World/BannerTileEntity.h" #include "Lighting.h" #include #include #ifdef _WINDOWS64 #include "DynamicLighting.h" #endif BannerRenderer *BannerRenderer::instance = nullptr; ResourceLocation BannerRenderer::BANNER_LOCATION = ResourceLocation(TN_MOB_BANNER_BANNER_BASE); BannerRenderer::BannerRenderer() { bannerModel = new BannerModel(); } void BannerRenderer::init(TileEntityRenderDispatcher *dispatcher) { TileEntityRenderer::init(dispatcher); instance = this; } static const int BANNER_COLOR_RGB[16] = { 0x1D1D21, 0xB02E26, 0x5E7C16, 0x835432, 0x3C44AA, 0x8932B8, 0x169C9C, 0x9D9D97, 0x474F52, 0xF38BAA, 0x80C71F, 0xFED83D, 0x3AB3DA, 0xC74EBD, 0xF9801D, 0xF9FFFE }; static int getBannerColorRGB(int baseColor) { ColourTable *table = Minecraft::GetInstance()->getColourTable(); if (table != nullptr) { unsigned int rgb = table->getColour(static_cast(eMinecraftColour_Banner_White + (15 - baseColor))); if (rgb != 0) return static_cast(rgb & 0xFFFFFF); } return BANNER_COLOR_RGB[baseColor]; } static void applyBannerColor(int baseColor, float alpha, float brightness = 1.0f) { if (baseColor >= 0 && baseColor < 16) { int rgb = getBannerColorRGB(baseColor); float r = ((rgb >> 16) & 0xFF) / 255.0f * brightness; float g = ((rgb >> 8) & 0xFF) / 255.0f * brightness; float b = ( rgb & 0xFF) / 255.0f * brightness; glColor4f(r, g, b, alpha); } else { glColor4f(brightness, brightness, brightness, alpha); } } const wchar_t *BannerRenderer::patternTextureName(const std::wstring &id) { static const struct { const wchar_t *id; const wchar_t *tex; } MAP[] = { { L"bl", L"square_bottom_left" }, { L"br", L"square_bottom_right" }, { L"tl", L"square_top_left" }, { L"tr", L"square_top_right" }, { L"bs", L"stripe_bottom" }, { L"ts", L"stripe_top" }, { L"ls", L"stripe_left" }, { L"rs", L"stripe_right" }, { L"cs", L"stripe_center" }, { L"ms", L"stripe_middle" }, { L"drs", L"stripe_downright" }, { L"dls", L"stripe_downleft" }, { L"ss", L"small_stripes" }, { L"cr", L"cross" }, { L"sc", L"straight_cross" }, { L"bt", L"triangle_bottom" }, { L"tt", L"triangle_top" }, { L"bts", L"triangles_bottom" }, { L"tts", L"triangles_top" }, { L"ld", L"diagonal_left" }, { L"rd", L"diagonal_up_right" }, { L"lud", L"diagonal_up_left" }, { L"rud", L"diagonal_right" }, { L"mc", L"circle" }, { L"mr", L"rhombus" }, { L"vh", L"half_vertical" }, { L"hh", L"half_horizontal" }, { L"vhr", L"half_vertical_right" }, { L"hhb", L"half_horizontal_bottom" }, { L"bo", L"border" }, { L"cbo", L"curly_border" }, { L"cre", L"creeper" }, { L"gra", L"gradient" }, { L"gru", L"gradient_up" }, { L"bri", L"bricks" }, { L"sku", L"skull" }, { L"flo", L"flower" }, { L"moj", L"mojang" }, }; for (const auto &e : MAP) { if (id == e.id) return e.tex; } return nullptr; } std::vector BannerRenderer::patternsFromItem(shared_ptr bannerItem) { std::vector out; if (bannerItem == nullptr || !bannerItem->hasTag()) return out; CompoundTag *tag = bannerItem->getTag(); if (!tag->contains(L"BlockEntityTag", Tag::TAG_Compound)) return out; CompoundTag *bet = tag->getCompound(L"BlockEntityTag"); if (!bet->contains(L"Patterns")) return out; ListTag *list = static_cast *>( static_cast(bet->getList(L"Patterns"))); if (!list) return out; for (int i = 0; i < list->size() && i < 6; i++) { CompoundTag *entry = list->get(i); BannerPattern bp; bp.pattern = entry->getString(L"Pattern"); bp.color = entry->getInt(L"Color"); out.push_back(bp); } return out; } void BannerRenderer::renderPatterns(const std::vector &patterns, float alpha, float lightU, float lightV) { if (patterns.empty()) return; Textures *textures = tileEntityRenderDispatcher ? tileEntityRenderDispatcher->textures : nullptr; if (textures == nullptr) return; float restoreU, restoreV; glGetLightmapUV(restoreU, restoreV); if (lightU >= 0.0f) { restoreU = lightU; restoreV = lightV; } glEnable(GL_BLEND); glDepthFunc(GL_LEQUAL); for (const BannerPattern &bp : patterns) { const wchar_t *tex = patternTextureName(bp.pattern); if (tex == nullptr) continue; ResourceLocation loc(std::wstring(L"mob/banner/") + tex + L".png"); textures->bindTexture(&loc); glMultiTexCoord2f(GL_TEXTURE1, 240.0f, 240.0f); glColorMask(true, true, true, false); glBlendFunc(GL_ZERO, GL_ONE_MINUS_SRC_COLOR); glColor4f(alpha, alpha, alpha, 1.0f); bannerModel->bannerSlate->render(1.0f / 16.0f, false); glMultiTexCoord2f(GL_TEXTURE1, restoreU, restoreV); glBlendFunc(GL_SRC_ALPHA, GL_ONE); applyBannerColor(bp.color, alpha); bannerModel->bannerSlate->render(1.0f / 16.0f, false); glColorMask(true, true, true, true); } glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } static BufferedImage *getBannerMaskImage(Textures *textures, const wchar_t *name) { static std::unordered_map s_images; auto it = s_images.find(name); if (it != s_images.end()) return it->second; BufferedImage *img = textures->readImage(TN_COUNT, std::wstring(L"mob/banner/") + name + L".png"); if (img != nullptr && (img->getWidth() != 64 || img->getHeight() != 64)) img = nullptr; s_images[name] = img; return img; } static int getCompositeBannerTexture(Textures *textures, int baseColor, const std::vector &patterns, const wchar_t *(*nameFor)(const std::wstring &)) { if (textures == nullptr || baseColor < 0 || baseColor > 15) return -1; std::wstring key = std::to_wstring(baseColor); for (const BannerPattern &bp : patterns) key += L";" + bp.pattern + L":" + std::to_wstring(bp.color); static std::unordered_map s_cache; auto cached = s_cache.find(key); if (cached != s_cache.end()) return cached->second; BufferedImage *baseImage = getBannerMaskImage(textures, L"banner_base"); BufferedImage *baseMask = getBannerMaskImage(textures, L"base"); if (baseImage == nullptr || baseMask == nullptr) { s_cache[key] = -1; return -1; } BufferedImage *out = new BufferedImage(64, 64, BufferedImage::TYPE_INT_ARGB); int *dst = out->getData(); int *src = baseImage->getData(); const int pixels = 64 * 64; float r[64 * 64], g[64 * 64], b[64 * 64]; for (int i = 0; i < pixels; i++) { r[i] = g[i] = b[i] = 0.0f; } std::vector > layers; layers.push_back(std::make_pair(baseMask, baseColor)); for (const BannerPattern &bp : patterns) { const wchar_t *tex = nameFor(bp.pattern); if (tex == nullptr) continue; BufferedImage *mask = getBannerMaskImage(textures, tex); if (mask != nullptr) layers.push_back(std::make_pair(mask, bp.color & 15)); } for (const auto &layer : layers) { int rgb = getBannerColorRGB(layer.second); float cr = ((rgb >> 16) & 0xFF) / 255.0f; float cg = ((rgb >> 8) & 0xFF) / 255.0f; float cb = (rgb & 0xFF) / 255.0f; int *mask = layer.first->getData(); for (int i = 0; i < pixels; i++) { int m = mask[i]; float a = (((m >> 24) & 0xFF) / 255.0f) * ((m & 0xFF) / 255.0f); if (a <= 0.0f) continue; int s = src[i]; float lr = ((s >> 16) & 0xFF) / 255.0f * cr; float lg = ((s >> 8) & 0xFF) / 255.0f * cg; float lb = (s & 0xFF) / 255.0f * cb; r[i] += (lr - r[i]) * a; g[i] += (lg - g[i]) * a; b[i] += (lb - b[i]) * a; } } for (int i = 0; i < pixels; i++) { int a = (src[i] >> 24) & 0xFF; int ir = static_cast(r[i] * 255.0f + 0.5f); int ig = static_cast(g[i] * 255.0f + 0.5f); int ib = static_cast(b[i] * 255.0f + 0.5f); dst[i] = (a << 24) | (ir << 16) | (ig << 8) | ib; } int id = textures->getTexture(out, C4JRender::TEXTURE_FORMAT_RxGyBzAw, false); s_cache[key] = id; return id; } void BannerRenderer::renderModelColoured(int baseColor, const std::vector &patterns, float alpha, bool useCompiled, float lightU, float lightV) { glEnable(GL_BLEND); glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); Textures *textures = tileEntityRenderDispatcher ? tileEntityRenderDispatcher->textures : nullptr; int composite = getCompositeBannerTexture(textures, baseColor, patterns, &BannerRenderer::patternTextureName); if (composite >= 0) { textures->bind(composite); glColor4f(1.0f, 1.0f, 1.0f, alpha); bannerModel->bannerSlate->render(1.0f / 16.0f, false); } else { bindTexture(&BANNER_LOCATION); applyBannerColor(baseColor, alpha); bannerModel->bannerSlate->render(1.0f / 16.0f, false); renderPatterns(patterns, alpha, lightU, lightV); } bindTexture(&BANNER_LOCATION); glColor4f(1.0f, 1.0f, 1.0f, alpha); bannerModel->bannerStand->render(1.0f / 16.0f, useCompiled); bannerModel->bannerTop->render(1.0f / 16.0f, useCompiled); glDisable(GL_BLEND); } void BannerRenderer::render(shared_ptr _banner, double x, double y, double z, float a, bool setColor, float alpha, bool useCompiled) { shared_ptr banner = dynamic_pointer_cast(_banner); if (!banner) return; Tile *tile = banner->getTile(); if (tile != Tile::standing_banner) return; const float f = 0.6666667f; glDisable(GL_LIGHTING); glPushMatrix(); if (!banner->isWall()) { glTranslatef((float)x + 0.5f, (float)y + 0.75f * f, (float)z + 0.5f); float rot = banner->getData() * 360.0f / 16.0f; glRotatef(-rot, 0.0f, 1.0f, 0.0f); bannerModel->bannerStand->visible = true; } else { glTranslatef((float)x + 0.5f, (float)y - 0.25f * f, (float)z + 0.5f); int face = banner->getData(); float rot = 0.0f; if (face == 2) rot = 180.0f; if (face == 4) rot = 90.0f; if (face == 5) rot = -90.0f; glRotatef(-rot, 0.0f, 1.0f, 0.0f); glTranslatef(0.0f, -0.3125f, -0.4375f); bannerModel->bannerStand->visible = false; } bannerModel->bannerSlate->y = -32.0f; float f3 = (float)(banner->x * 7 + banner->y * 9 + banner->z * 13) + (float)banner->level->getGameTime() + a; bannerModel->bannerSlate->xRot = (-0.0125f + 0.01f * cosf(f3 * 3.14159265f * 0.02f)) * 3.14159265f; glScalef(f, -f, -f); int lightCol = banner->level->getLightColor(banner->x, banner->y, banner->z, 0); #ifdef _WINDOWS64 lightCol = DynamicLighting::getLightmapWithDynamicLight(banner->x, banner->y, banner->z, lightCol); #endif float lightU = (float)(lightCol & 0xFFFF); float lightV = (float)((lightCol >> 16) & 0xFFFF); renderModelColoured(banner->getBaseColor(), banner->getPatterns(), alpha, useCompiled, lightU, lightV); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glMultiTexCoord2f(GL_TEXTURE1, 240.0f, 240.0f); glPopMatrix(); glEnable(GL_LIGHTING); } void BannerRenderer::renderBannerForGui(float x, float y, float scaleX, float scaleY, float alpha, int baseColor, shared_ptr bannerItem) { std::vector patterns = patternsFromItem(bannerItem); glPushMatrix(); glTranslatef(x + 8.0f * scaleX, y + 8.0f * scaleY, 0.0f); glScalef(16.0f * scaleX, -16.0f * scaleY, 1.0f); glTranslatef(0.0f, -3.25f / 16.0f, 0.0f); glRotatef(30.0f, 1.0f, 0.0f, 0.0f); glRotatef(20.0f, 0.0f, 1.0f, 0.0f); glScalef(0.5325f, 0.5325f, 0.5325f); const float f = 0.6666667f; glScalef(f, -f, -f); bannerModel->bannerSlate->y = -32.0f; bannerModel->bannerSlate->xRot = (-0.0125f + 0.01f) * 3.14159265f; bannerModel->bannerStand->visible = true; renderModelColoured(baseColor, patterns, alpha, false); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glPopMatrix(); } void BannerRenderer::renderBannerForHand(float alpha, int baseColor, shared_ptr bannerItem) { std::vector patterns = patternsFromItem(bannerItem); glDisable(GL_LIGHTING); glPushMatrix(); bannerModel->bannerSlate->y = -32.0f; bannerModel->bannerSlate->xRot = 0.0f; bannerModel->bannerStand->visible = true; renderModelColoured(baseColor, patterns, alpha, false); glColor4f(1.0f, 1.0f, 1.0f, 1.0f); glPopMatrix(); glEnable(GL_LIGHTING); }