Improve ship nickname rendering
This commit is contained in:
parent
e18484ea62
commit
e050d7d0ec
@ -1,11 +1,14 @@
|
|||||||
#version 330 core
|
#version 330 core
|
||||||
in vec2 TexCoord;
|
in vec2 TexCoord;
|
||||||
out vec4 FragColor;
|
out vec4 FragColor;
|
||||||
|
|
||||||
uniform sampler2D uText;
|
uniform sampler2D uText;
|
||||||
uniform vec3 uColor;
|
uniform vec4 uColor;
|
||||||
|
uniform vec4 uOutlineColor;
|
||||||
|
uniform float uOutlineWidth = 0.7;
|
||||||
void main() {
|
void main() {
|
||||||
float a = texture(uText, TexCoord).r; // glyph alpha in RED channel
|
float dist = texture(uText, TexCoord).r;
|
||||||
FragColor = vec4(uColor, a);
|
float outline = smoothstep(0.5 - uOutlineWidth, 0.5 + uOutlineWidth, dist);
|
||||||
|
float text = smoothstep(0.5, 0.5 + 0.1, dist);
|
||||||
|
float glow = exp(-pow(dist - 0.5, 2.0) / 0.02);
|
||||||
|
FragColor = vec4(uColor.rgb * (text + glow * 0.6), uColor.a * outline);
|
||||||
}
|
}
|
||||||
24
src/Game.cpp
24
src/Game.cpp
@ -756,26 +756,28 @@ namespace ZL
|
|||||||
// Позиция корабля в мире
|
// Позиция корабля в мире
|
||||||
Vector3f shipWorld = st.position;
|
Vector3f shipWorld = st.position;
|
||||||
|
|
||||||
// Значение положения лейбла
|
float distSq = (Environment::shipState.position - shipWorld).squaredNorm();
|
||||||
Vector3f labelWorld = shipWorld + Vector3f{ 0.0f, -5.0f, 0.0f };
|
if (distSq > MAX_DIST_SQ)
|
||||||
|
continue;
|
||||||
|
float dist = sqrt(distSq);
|
||||||
|
float alpha = std::clamp(1.f - (dist - FADE_START) / FADE_RANGE, 0.f, 1.f);
|
||||||
|
if (alpha < 0.01f)
|
||||||
|
continue;
|
||||||
|
Vector3f labelWorld = shipWorld + Vector3f{ 0.f, -4.f, 0.f }; // регулировка высоты
|
||||||
float sx, sy, depth;
|
float sx, sy, depth;
|
||||||
if (!worldToScreen(labelWorld, sx, sy, depth))
|
if (!worldToScreen(labelWorld, sx, sy, depth))
|
||||||
continue;
|
continue;
|
||||||
|
|
||||||
float uiX = sx;
|
float uiX = sx, uiY = sy;
|
||||||
float uiY = sy;
|
float scale = std::clamp(BASE_SCALE / (dist * PERSPECTIVE_K + 1.f), MIN_SCALE, MAX_SCALE);
|
||||||
|
|
||||||
// Масштаб по расстоянию
|
|
||||||
float dist = (Environment::shipState.position - shipWorld).norm();
|
|
||||||
float scale = std::clamp(140.0f / (dist + 1.0f), 0.6f, 1.2f);
|
|
||||||
|
|
||||||
// Дефолтный лейбл
|
// Дефолтный лейбл
|
||||||
std::string label = "Player (" + std::to_string(st.id) + ")";
|
std::string label = "Player (" + std::to_string(st.id) + ") " + std::to_string((int)dist) + "m";
|
||||||
|
|
||||||
// TODO: nickname sync
|
// TODO: nickname sync
|
||||||
|
|
||||||
textRenderer->drawText(label, uiX, uiY, scale, true);
|
textRenderer->drawText(label, uiX + 1.f, uiY + 1.f, scale, true, {0.f, 0.f, 0.f, alpha}); // color param
|
||||||
|
textRenderer->drawText(label, uiX, uiY, scale, true, { 1.f, 1.f, 1.f, alpha });
|
||||||
}
|
}
|
||||||
|
|
||||||
glDisable(GL_BLEND);
|
glDisable(GL_BLEND);
|
||||||
|
|||||||
@ -115,6 +115,13 @@ namespace ZL {
|
|||||||
uint64_t lastExplosionTime = 0;
|
uint64_t lastExplosionTime = 0;
|
||||||
const uint64_t explosionDurationMs = 500;
|
const uint64_t explosionDurationMs = 500;
|
||||||
|
|
||||||
|
static constexpr float MAX_DIST_SQ = 10000.f * 10000.f;
|
||||||
|
static constexpr float FADE_START = 6000.f;
|
||||||
|
static constexpr float FADE_RANGE = 4000.f;
|
||||||
|
static constexpr float BASE_SCALE = 140.f;
|
||||||
|
static constexpr float PERSPECTIVE_K = 0.05f; // Tune
|
||||||
|
static constexpr float MIN_SCALE = 0.4f;
|
||||||
|
static constexpr float MAX_SCALE = 1.5f;
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
@ -835,6 +835,15 @@ namespace ZL {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Renderer::RenderUniform4fv(const std::string& uniformName, const float* value)
|
||||||
|
{
|
||||||
|
auto shader = shaderManager.GetCurrentShader();
|
||||||
|
auto uniform = shader->uniformList.find(uniformName);
|
||||||
|
if (uniform != shader->uniformList.end()) {
|
||||||
|
glUniform4fv(uniform->second, 1, value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void Renderer::RenderUniform1i(const std::string& uniformName, const int value)
|
void Renderer::RenderUniform1i(const std::string& uniformName, const int value)
|
||||||
{
|
{
|
||||||
auto shader = shaderManager.GetCurrentShader();
|
auto shader = shaderManager.GetCurrentShader();
|
||||||
|
|||||||
@ -138,6 +138,7 @@ namespace ZL {
|
|||||||
void RenderUniformMatrix4fv(const std::string& uniformName, bool transpose, const float* value);
|
void RenderUniformMatrix4fv(const std::string& uniformName, bool transpose, const float* value);
|
||||||
void RenderUniform1i(const std::string& uniformName, const int value);
|
void RenderUniform1i(const std::string& uniformName, const int value);
|
||||||
void RenderUniform3fv(const std::string& uniformName, const float* value);
|
void RenderUniform3fv(const std::string& uniformName, const float* value);
|
||||||
|
void RenderUniform4fv(const std::string& uniformName, const float* value);
|
||||||
void RenderUniform1f(const std::string& uniformName, float value);
|
void RenderUniform1f(const std::string& uniformName, float value);
|
||||||
|
|
||||||
void VertexAttribPointer2fv(const std::string& attribName, int stride, const char* pointer);
|
void VertexAttribPointer2fv(const std::string& attribName, int stride, const char* pointer);
|
||||||
|
|||||||
@ -3,8 +3,9 @@
|
|||||||
#include FT_FREETYPE_H
|
#include FT_FREETYPE_H
|
||||||
|
|
||||||
#include "Environment.h"
|
#include "Environment.h"
|
||||||
#include "render/OpenGlExtensions.h" // если у тебя там gl* указатели
|
#include "render/OpenGlExtensions.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
#include <array>
|
||||||
|
|
||||||
namespace ZL {
|
namespace ZL {
|
||||||
|
|
||||||
@ -25,8 +26,6 @@ bool TextRenderer::init(Renderer& renderer, const std::string& ttfPath, int pixe
|
|||||||
{
|
{
|
||||||
r = &renderer;
|
r = &renderer;
|
||||||
|
|
||||||
// Добавим шейдер (если у тебя shaderManager работает так же, как с default)
|
|
||||||
// Подстрой под твой AddShaderFromFiles: тут без zip.
|
|
||||||
r->shaderManager.AddShaderFromFiles(shaderName,
|
r->shaderManager.AddShaderFromFiles(shaderName,
|
||||||
"resources/shaders/text2d.vertex",
|
"resources/shaders/text2d.vertex",
|
||||||
"resources/shaders/text2d.fragment",
|
"resources/shaders/text2d.fragment",
|
||||||
@ -109,7 +108,7 @@ bool TextRenderer::loadGlyphs(const std::string& ttfPath, int pixelSize)
|
|||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void TextRenderer::drawText(const std::string& text, float x, float y, float scale, bool centered)
|
void TextRenderer::drawText(const std::string& text, float x, float y, float scale, bool centered, std::array<float, 4> color)
|
||||||
{
|
{
|
||||||
if (!r) return;
|
if (!r) return;
|
||||||
|
|
||||||
@ -127,8 +126,6 @@ void TextRenderer::drawText(const std::string& text, float x, float y, float sca
|
|||||||
r->shaderManager.PushShader(shaderName);
|
r->shaderManager.PushShader(shaderName);
|
||||||
|
|
||||||
// Орто-проекция в пикселях: (0..W, 0..H)
|
// Орто-проекция в пикселях: (0..W, 0..H)
|
||||||
// ВАЖНО: мы рисуем в координатах Game::drawBoxesLabels() как uiX/uiY
|
|
||||||
// Здесь предположим, что (0,0) внизу слева.
|
|
||||||
float W = (float)Environment::width;
|
float W = (float)Environment::width;
|
||||||
float H = (float)Environment::height;
|
float H = (float)Environment::height;
|
||||||
|
|
||||||
@ -142,9 +139,7 @@ void TextRenderer::drawText(const std::string& text, float x, float y, float sca
|
|||||||
r->RenderUniformMatrix4fv("uProjection", false, proj.data());
|
r->RenderUniformMatrix4fv("uProjection", false, proj.data());
|
||||||
r->RenderUniform1i("uText", 0);
|
r->RenderUniform1i("uText", 0);
|
||||||
|
|
||||||
// белый цвет
|
r->RenderUniform4fv("uColor", color.data());
|
||||||
float color[3] = {1.0f, 1.0f, 1.0f};
|
|
||||||
r->RenderUniform3fv("uColor", color);
|
|
||||||
|
|
||||||
glActiveTexture(GL_TEXTURE0);
|
glActiveTexture(GL_TEXTURE0);
|
||||||
// glBindVertexArray(vao);
|
// glBindVertexArray(vao);
|
||||||
@ -184,9 +179,7 @@ void TextRenderer::drawText(const std::string& text, float x, float y, float sca
|
|||||||
};
|
};
|
||||||
|
|
||||||
glBindTexture(GL_TEXTURE_2D, g.texID);
|
glBindTexture(GL_TEXTURE_2D, g.texID);
|
||||||
|
|
||||||
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts);
|
glBufferSubData(GL_ARRAY_BUFFER, 0, sizeof(verts), verts);
|
||||||
|
|
||||||
glDrawArrays(GL_TRIANGLES, 0, 6);
|
glDrawArrays(GL_TRIANGLES, 0, 6);
|
||||||
|
|
||||||
penX += (g.advance >> 6) * scale;
|
penX += (g.advance >> 6) * scale;
|
||||||
|
|||||||
@ -5,6 +5,7 @@
|
|||||||
#include <unordered_map>
|
#include <unordered_map>
|
||||||
#include <Eigen/Dense>
|
#include <Eigen/Dense>
|
||||||
#include "render/Renderer.h"
|
#include "render/Renderer.h"
|
||||||
|
#include <array>
|
||||||
|
|
||||||
|
|
||||||
namespace ZL {
|
namespace ZL {
|
||||||
@ -22,7 +23,7 @@ public:
|
|||||||
~TextRenderer();
|
~TextRenderer();
|
||||||
|
|
||||||
bool init(Renderer& renderer, const std::string& ttfPath, int pixelSize);
|
bool init(Renderer& renderer, const std::string& ttfPath, int pixelSize);
|
||||||
void drawText(const std::string& text, float x, float y, float scale, bool centered);
|
void drawText(const std::string& text, float x, float y, float scale, bool centered, std::array<float, 4> color = { 1.f,1.f,1.f,1.f });
|
||||||
|
|
||||||
private:
|
private:
|
||||||
bool loadGlyphs(const std::string& ttfPath, int pixelSize);
|
bool loadGlyphs(const std::string& ttfPath, int pixelSize);
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user