fix crosshair circle rendering issues and added round dot option

This commit is contained in:
pierow 2024-04-29 17:24:47 -04:00
parent fb214971d5
commit 3b8049d226
4 changed files with 98 additions and 32 deletions

View file

@ -31,6 +31,7 @@ int CHudCrosshairs::Init()
cl_cross_dot_alpha = CVAR_CREATE("cl_cross_dot_alpha", "", FCVAR_ARCHIVE);
cl_cross_dot_outline = CVAR_CREATE("cl_cross_dot_outline", "0", FCVAR_ARCHIVE);
cl_cross_dot_outline_alpha = CVAR_CREATE("cl_cross_dot_outline_alpha", "", FCVAR_ARCHIVE);
cl_cross_dot_round = CVAR_CREATE("cl_cross_dot_round", "0", FCVAR_ARCHIVE);
cl_cross_line_top = CVAR_CREATE("cl_cross_line_top", "1", FCVAR_ARCHIVE);
cl_cross_line_bottom = CVAR_CREATE("cl_cross_line_bottom", "1", FCVAR_ARCHIVE);
cl_cross_line_left = CVAR_CREATE("cl_cross_line_left", "1", FCVAR_ARCHIVE);
@ -77,7 +78,7 @@ int CHudCrosshairs::Draw(float time)
if (cl_cross_scaling->value != 0)
{
// Scale from 1080 Y res, as it's what the default crosshairs are designed for, but don't scale when resolution is close to 1080, as it looks bad.
const float screenscale = ScreenHeight() / 1080.0f; //ScreenHeight() * 0.000926f
const float screenscale = ScreenHeight() / 1080; //ScreenHeight() * 0.000926f
// Lowering scale looks like junk, don't bother.
//if (screenscale < 0.75f)
//{
@ -197,11 +198,28 @@ int CHudCrosshairs::Draw(float time)
gl.color(0, 0, 0, dotoutalpha);
float size = min(cl_cross_dot_size->value * scalar, ScreenHeight() * 0.2f) + (dotout * 2.0f);
Vector2D offset = Vector2D(size / 2.0f, size / 2.0f);
float size = min(cl_cross_dot_size->value * scalar, ScreenHeight() * 0.2f);
if (cl_cross_dot_round->value)
{
size *= 0.5f;
if (old_round_dot_outline_radius != size || old_round_dot_outline_thickness != dotout) {
// Recompute the circle points.
round_dot_outline_points = HudGL::compute_circle(size + (dotout * 0.5f), dotout);
old_round_dot_outline_radius = size;
old_round_dot_outline_thickness = dotout;
}
gl.circle(center, round_dot_outline_points);
}
else
{
//TOFIX - Not a true outline, will be seen behind transparent dots
size += dotout * 2.0f;
Vector2D offset = Vector2D(size / 2.0f, size / 2.0f);
gl.rectangle(center - offset, center + offset);
}
}
float circleout;
if (cl_cross_circle_outline->string[0] == 0)
@ -225,22 +243,26 @@ int CHudCrosshairs::Draw(float time)
auto radius = cl_cross_circle_radius->value * scalar;
float thickness;
if (!circleoutinner)
{
radius += cl_cross_circle_thickness->value * scalar * 0.5f;
gl.line_width(circleout);
thickness = circleout;
}
else
{
gl.line_width(cl_cross_circle_thickness->value * scalar + (circleout * 2.0f));
//TOFIX - Not a true outline, will be seen behind transparent circles
thickness = cl_cross_circle_thickness->value * scalar + (circleout * 2.0f);
}
if (old_circle_radius != radius) {
if (old_circle_outline_radius != radius || old_circle_outline_thickness != thickness) {
// Recompute the circle points.
circle_points = HudGL::compute_circle(radius);
old_circle_radius = radius;
circle_outline_points = HudGL::compute_circle(radius, thickness);
old_circle_outline_radius = radius;
old_circle_outline_thickness = thickness;
}
gl.circle(center, circle_points);
gl.circle(center, circle_outline_points);
}
gl.color(r, g, b, alpha);
@ -299,13 +321,14 @@ int CHudCrosshairs::Draw(float time)
gl.color(r, g, b, circlealpha);
}
gl.line_width(cl_cross_circle_thickness->value * scalar);
float radius = cl_cross_circle_radius->value * scalar;
if (old_circle_radius != radius) {
float thickness = cl_cross_circle_thickness->value * scalar;
if (old_circle_radius != radius || old_circle_thickness != thickness) {
// Recompute the circle points.
circle_points = HudGL::compute_circle(radius);
circle_points = HudGL::compute_circle(radius, thickness);
old_circle_radius = radius;
old_circle_thickness = thickness;
gEngfuncs.Con_Printf("radius:%f thickness:%f\n", radius, thickness);
}
gl.circle(center, circle_points);
@ -332,8 +355,22 @@ int CHudCrosshairs::Draw(float time)
float size = min(cl_cross_dot_size->value * scalar, ScreenHeight() * 0.2f);
Vector2D offset = Vector2D(size / 2.0f, size / 2.0f);
if (cl_cross_dot_round->value)
{
size *= 0.5f;
if (old_round_dot_radius != size) {
// Recompute the circle points.
round_dot_points = HudGL::compute_circle(size);
old_round_dot_radius = size;
}
gl.round_dot(center, round_dot_points);
}
else
{
gl.rectangle(center - offset, center + offset);
}
}
return 0;
}

View file

@ -27,6 +27,7 @@ class CHudCrosshairs : public CHudBase
cvar_t* cl_cross_dot_alpha;
cvar_t* cl_cross_dot_outline;
cvar_t* cl_cross_dot_outline_alpha;
cvar_t* cl_cross_dot_round;
cvar_t* cl_cross_line_top;
cvar_t* cl_cross_line_bottom;
cvar_t* cl_cross_line_left;
@ -34,7 +35,16 @@ class CHudCrosshairs : public CHudBase
cvar_t* cl_cross_scaling;
float old_circle_radius;
float old_circle_thickness;
std::vector<Vector2D> circle_points;
float old_circle_outline_radius;
float old_circle_outline_thickness;
std::vector<Vector2D> circle_outline_points;
float old_round_dot_radius;
std::vector<Vector2D> round_dot_points;
float old_round_dot_outline_radius;
float old_round_dot_outline_thickness;
std::vector<Vector2D> round_dot_outline_points;
public:
virtual int Init();

View file

@ -52,11 +52,21 @@ void HudGL::line(const Vector2D& start, const Vector2D& end) const {
glEnd();
}
//#ifdef __APPLE__
////Remove when OSX builds with c++11
//#else
void HudGL::circle(const Vector2D& center, const std::vector<Vector2D>& points) const {
glBegin(GL_LINE_STRIP);
glBegin(GL_TRIANGLE_STRIP);
for (const Vector2D& point : points)
glVertex2f(center.x + point.x, center.y + point.y);
glVertex2f(center.x + points[0].x, center.y + points[0].y);
glVertex2f(center.x + points[1].x, center.y + points[1].y);
glEnd();
}
void HudGL::round_dot(const Vector2D& center, const std::vector<Vector2D>& points) const {
glBegin(GL_TRIANGLE_FAN);
glVertex2f(center.x, center.y);
for (const Vector2D& point : points)
glVertex2f(center.x + point.x, center.y + point.y);
@ -64,7 +74,6 @@ void HudGL::circle(const Vector2D& center, const std::vector<Vector2D>& points)
glVertex2f(center.x + points[0].x, center.y + points[0].y);
glEnd();
}
//#endif
void HudGL::rectangle(const Vector2D& corner_a, const Vector2D& corner_b) const {
glBegin(GL_QUADS);
@ -75,9 +84,6 @@ void HudGL::rectangle(const Vector2D& corner_a, const Vector2D& corner_b) const
glEnd();
}
//#ifdef __APPLE__
////Remove when OSX builds with c++11
//#else
std::vector<Vector2D> HudGL::compute_circle(float radius) {
// Maximum allowed distance between the circle and the rendered line segment.
const float MAX_ERROR = 0.1f;
@ -94,4 +100,23 @@ std::vector<Vector2D> HudGL::compute_circle(float radius) {
return points;
}
//#endif
std::vector<Vector2D> HudGL::compute_circle(float radius, float thickness) {
// Maximum allowed distance between the circle and the rendered line segment.
const float MAX_ERROR = 0.1f;
const unsigned segment_count =
static_cast<unsigned>(std::ceil(M_PI / std::acos((radius - MAX_ERROR) / radius)));
const unsigned point_count = segment_count * 2;
float half_thickness = thickness * 0.5f;
std::vector<Vector2D> points;
points.reserve(point_count);
for (unsigned i = 0; i < segment_count; ++i) {
float angle = static_cast<float>(M_PI * 2 * i / segment_count);
points.emplace_back((radius + half_thickness) * std::cos(angle), (radius + half_thickness) * std::sin(angle));
points.emplace_back((radius - half_thickness) * std::cos(angle), (radius - half_thickness) * std::sin(angle));
}
return points;
}

View file

@ -9,16 +9,10 @@ public:
void color(unsigned char r, unsigned char g, unsigned char b, unsigned char a) const;
void line_width(float width) const;
void line(const Vector2D& start, const Vector2D& end) const;
//#ifdef __APPLE__
// //Remove when OSX builds with c++11
//#else
void circle(const Vector2D& center, const std::vector<Vector2D>& points) const;
//#endif
void round_dot(const Vector2D& center, const std::vector<Vector2D>& points) const;
void rectangle(const Vector2D& corner_a, const Vector2D& corner_b) const;
//#ifdef __APPLE__
// //Remove when OSX builds with c++11
//#else
static std::vector<Vector2D> compute_circle(float radius);
//#endif
static std::vector<Vector2D> compute_circle(float radius, float thickness);
};