1
0
forked from 0ad/0ad

Move selection ring OverlayTexture code from CCmpSelectable::UpdateTexturedLineOverlay to the SOverlayTexturedLine struct in graphics/Overlay.cpp and SimRender in simulation2/helpers/Render.cpp.

This abstraction allows calling that code again from other simulation
components, like the RangeOverlayRenderer in D555.

Differential Revision: https://code.wildfiregames.com/D1139
Refs #3915, D555
Comments By: Vladislav, echotangoecho
This was SVN commit r20621.
This commit is contained in:
elexis 2017-12-10 00:19:51 +00:00
parent 470e9fc8f1
commit 13ad114dd6
5 changed files with 81 additions and 56 deletions

View File

@ -1,4 +1,4 @@
/* Copyright (C) 2011 Wildfire Games.
/* Copyright (C) 2017 Wildfire Games.
* This file is part of 0 A.D.
*
* 0 A.D. is free software: you can redistribute it and/or modify
@ -19,7 +19,9 @@
#include "Overlay.h"
#include "graphics/TextureManager.h"
#include "ps/CStr.h"
#include "renderer/Renderer.h"
SOverlayTexturedLine::LineCapType SOverlayTexturedLine::StrToLineCapType(const std::wstring& str)
{
@ -37,3 +39,21 @@ SOverlayTexturedLine::LineCapType SOverlayTexturedLine::StrToLineCapType(const s
}
}
void SOverlayTexturedLine::CreateOverlayTexture(const SOverlayDescriptor* overlayDescriptor)
{
CTextureProperties texturePropsBase(overlayDescriptor->m_LineTexture.c_str());
texturePropsBase.SetWrap(GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE);
texturePropsBase.SetMaxAnisotropy(4.f);
CTextureProperties texturePropsMask(overlayDescriptor->m_LineTextureMask.c_str());
texturePropsMask.SetWrap(GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE);
texturePropsMask.SetMaxAnisotropy(4.f);
m_AlwaysVisible = false;
m_Closed = true;
m_Thickness = overlayDescriptor->m_LineThickness;
m_TextureBase = g_Renderer.GetTextureManager().CreateTexture(texturePropsBase);
m_TextureMask = g_Renderer.GetTextureManager().CreateTexture(texturePropsMask);
ENSURE(m_TextureBase);
}

View File

@ -28,6 +28,7 @@
class CTerrain;
class CSimContext;
class CTexturedLineRData;
struct SOverlayDescriptor;
/**
* Line-based overlay, with world-space coordinates, rendered in the world
@ -121,6 +122,11 @@ struct SOverlayTexturedLine
*/
static LineCapType StrToLineCapType(const std::wstring& str);
/**
* Creates the texture specified by the given overlay descriptor and assigns it to this overlay.
*/
void CreateOverlayTexture(const SOverlayDescriptor* overlayDescriptor);
void PushCoords(const float x, const float z) { m_Coords.push_back(x); m_Coords.push_back(z); }
void PushCoords(const CVector2D& v) { PushCoords(v.X, v.Y); }
void PushCoords(const std::vector<CVector2D>& points)

View File

@ -494,60 +494,15 @@ void CCmpSelectable::UpdateTexturedLineOverlay(const SOverlayDescriptor* overlay
float rotY;
CVector2D origin;
cmpPosition->GetInterpolatedPosition2D(frameOffset, origin.X, origin.Y, rotY);
CFixedVector3D rotation = cmpPosition->GetRotation();
CTextureProperties texturePropsBase(overlayDescriptor->m_LineTexture.c_str());
texturePropsBase.SetWrap(GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE);
texturePropsBase.SetMaxAnisotropy(4.f);
CTextureProperties texturePropsMask(overlayDescriptor->m_LineTextureMask.c_str());
texturePropsMask.SetWrap(GL_CLAMP_TO_BORDER, GL_CLAMP_TO_EDGE);
texturePropsMask.SetMaxAnisotropy(4.f);
overlay.m_AlwaysVisible = false;
overlay.m_Closed = true;
overlay.m_SimContext = &GetSimContext();
overlay.m_Thickness = overlayDescriptor->m_LineThickness;
overlay.m_TextureBase = g_Renderer.GetTextureManager().CreateTexture(texturePropsBase);
overlay.m_TextureMask = g_Renderer.GetTextureManager().CreateTexture(texturePropsMask);
overlay.m_Color = m_Color;
overlay.CreateOverlayTexture(overlayDescriptor);
if (buildingOverlay && fpShape == ICmpFootprint::SQUARE)
{
float s = sinf(-rotation.Y.ToFloat());
float c = cosf(-rotation.Y.ToFloat());
CVector2D unitX(c, s);
CVector2D unitZ(-s, c);
// Add half the line thickness to the radius so that we get an 'outside' stroke of the footprint shape
const float halfSizeX = fpSize0_fixed.ToFloat() / 2.f + overlay.m_Thickness / 2.f;
const float halfSizeZ = fpSize1_fixed.ToFloat() / 2.f + overlay.m_Thickness / 2.f;
std::vector<CVector2D> points;
points.push_back(CVector2D(origin + unitX * halfSizeX + unitZ * (-halfSizeZ)));
points.push_back(CVector2D(origin + unitX * (-halfSizeX) + unitZ * (-halfSizeZ)));
points.push_back(CVector2D(origin + unitX * (-halfSizeX) + unitZ * halfSizeZ));
points.push_back(CVector2D(origin + unitX * halfSizeX + unitZ * halfSizeZ));
SimRender::SubdividePoints(points, TERRAIN_TILE_SIZE / 3.f, overlay.m_Closed);
overlay.PushCoords(points);
}
SimRender::ConstructTexturedLineBox(overlay, origin, cmpPosition->GetRotation(), fpSize0_fixed.ToFloat(), fpSize1_fixed.ToFloat());
else
{
const float radius = (buildingOverlay ? fpSize0_fixed.ToFloat() : overlayDescriptor->m_Radius) + overlay.m_Thickness / 3.f;
u32 numSteps = ceilf(float(2 * M_PI) * radius / (TERRAIN_TILE_SIZE / 3.f));
for (u32 i = 0; i < numSteps; ++i)
{
float angle = i * float(2 * M_PI) / numSteps;
float px = origin.X + radius * sinf(angle);
float pz = origin.Y + radius * cosf(angle);
overlay.PushCoords(px, pz);
}
}
ENSURE(overlay.m_TextureBase);
SimRender::ConstructTexturedLineCircle(overlay, origin, buildingOverlay ? fpSize0_fixed.ToFloat() : overlayDescriptor->m_Radius);
}
void CCmpSelectable::UpdateDynamicOverlay(float frameOffset)

View File

@ -600,3 +600,41 @@ void SimRender::SubdividePoints(std::vector<CVector2D>& points, float maxSegment
points.swap(newPoints);
}
void SimRender::ConstructTexturedLineBox(SOverlayTexturedLine& overlay, const CVector2D& origin,
const CFixedVector3D& rotation, const float sizeX, const float sizeZ)
{
float s = sinf(-rotation.Y.ToFloat());
float c = cosf(-rotation.Y.ToFloat());
CVector2D unitX(c, s);
CVector2D unitZ(-s, c);
// Add half the line thickness to the radius so that we get an 'outside' stroke of the footprint shape
const float halfSizeX = sizeX / 2.f + overlay.m_Thickness / 2.f;
const float halfSizeZ = sizeZ / 2.f + overlay.m_Thickness / 2.f;
std::vector<CVector2D> points;
points.push_back(CVector2D(origin + unitX * halfSizeX + unitZ * (-halfSizeZ)));
points.push_back(CVector2D(origin + unitX * (-halfSizeX) + unitZ * (-halfSizeZ)));
points.push_back(CVector2D(origin + unitX * (-halfSizeX) + unitZ * halfSizeZ));
points.push_back(CVector2D(origin + unitX * halfSizeX + unitZ * halfSizeZ));
SimRender::SubdividePoints(points, TERRAIN_TILE_SIZE / 3.f, overlay.m_Closed);
overlay.PushCoords(points);
}
void SimRender::ConstructTexturedLineCircle(SOverlayTexturedLine& overlay, const CVector2D& origin, const float overlay_radius)
{
const float radius = overlay_radius + overlay.m_Thickness / 3.f;
size_t numSteps = ceilf(float(2 * M_PI) * radius / (TERRAIN_TILE_SIZE / 3.f));
for (size_t i = 0; i < numSteps; ++i)
{
float angle = i * float(2 * M_PI) / numSteps;
float px = origin.X + radius * sinf(angle);
float pz = origin.Y + radius * cosf(angle);
overlay.PushCoords(px, pz);
}
}

View File

@ -18,20 +18,16 @@
#ifndef INCLUDED_HELPER_RENDER
#define INCLUDED_HELPER_RENDER
/**
* @file
* Helper functions related to rendering
*/
#include "maths/Vector2D.h"
class CSimContext;
class CVector2D;
class CVector3D;
class CFixedVector3D;
class CMatrix3D;
class CBoundingBoxAligned;
class CBoundingBoxOriented;
struct SOverlayLine;
struct SOverlayTexturedLine;
struct SDashedLine
{
@ -190,6 +186,16 @@ void ConstructDashedLine(const std::vector<CVector2D>& linePoints, SDashedLine&
*/
void SubdividePoints(std::vector<CVector2D>& points, float maxSegmentLength, bool closed);
/**
* Sets the coordinates of a rectangular textured overlay, for example used by selection rings of structures.
*/
void ConstructTexturedLineBox(SOverlayTexturedLine& overlay, const CVector2D& origin, const CFixedVector3D& rotation, const float sizeX, const float sizeZ);
/**
* Sets the coordinates of a circular textured overlay, for example by selection rings of units or attack range visualization.
*/
void ConstructTexturedLineCircle(SOverlayTexturedLine& overlay, const CVector2D& origin, const float overlay_radius);
} // namespace
#endif // INCLUDED_HELPER_RENDER