1
0
forked from 0ad/0ad

Adds circle drawing to DebugRenderer.

This was SVN commit r25315.
This commit is contained in:
Vladislav Belov 2021-04-25 21:55:19 +00:00
parent e90aaf6348
commit 2086a66340
4 changed files with 88 additions and 82 deletions

View File

@ -44,6 +44,7 @@
#include "simulation2/MessageTypes.h"
#include "simulation2/system/ComponentManager.h"
#include "simulation2/Simulation2.h"
#include "renderer/DebugRenderer.h"
#include "renderer/Renderer.h"
@ -74,111 +75,63 @@ void CCinemaManager::DrawPaths() const
if (!cmpCinemaManager)
return;
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
for (const std::pair<const CStrW, CCinemaPath>& p : cmpCinemaManager->GetPaths())
{
DrawSpline(p.second, CColor(0.2f, 0.2f, 1.f, 0.9f), 128, true);
DrawSpline(p.second, CColor(0.2f, 0.2f, 1.f, 0.9f), 128);
DrawNodes(p.second, CColor(0.1f, 1.f, 0.f, 1.f));
if (p.second.GetTargetSpline().GetAllNodes().empty())
continue;
DrawSpline(p.second.GetTargetSpline(), CColor(1.f, 0.3f, 0.4f, 0.9f), 128, true);
DrawSpline(p.second.GetTargetSpline(), CColor(1.f, 0.3f, 0.4f, 0.9f), 128);
DrawNodes(p.second.GetTargetSpline(), CColor(1.f, 0.1f, 0.f, 1.f));
}
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
}
void CCinemaManager::DrawSpline(const RNSpline& spline, const CColor& splineColor, int smoothness, bool lines) const
void CCinemaManager::DrawSpline(const RNSpline& spline, const CColor& splineColor, int smoothness) const
{
if (spline.GetAllNodes().size() < 2)
return;
if (spline.GetAllNodes().size() == 2 && lines)
if (spline.GetAllNodes().size() == 2)
smoothness = 2;
float start = spline.MaxDistance.ToFloat() / smoothness;
float time = 0;
const float start = spline.MaxDistance.ToFloat() / smoothness;
#if CONFIG2_GLES
#warning TODO : implement CCinemaPath on GLES
#else
glDisable(GL_DEPTH_TEST);
glEnable(GL_BLEND);
glColor4f(splineColor.r, splineColor.g, splineColor.b, splineColor.a);
if (lines)
std::vector<CVector3D> line;
for (int i = 0; i <= smoothness; ++i)
{
const float time = start * i / spline.MaxDistance.ToFloat();
line.emplace_back(spline.GetPosition(time));
}
g_Renderer.GetDebugRenderer().DrawLine(line, splineColor, 0.2f);
// Height indicator
if (g_Game && g_Game->GetWorld() && g_Game->GetWorld()->GetTerrain())
{
glLineWidth(1.8f);
glEnable(GL_LINE_SMOOTH);
glBegin(GL_LINE_STRIP);
for (int i = 0; i <= smoothness; ++i)
{
time = start * i / spline.MaxDistance.ToFloat();
CVector3D tmp = spline.GetPosition(time);
glVertex3f(tmp.X, tmp.Y, tmp.Z);
const float time = start * i / spline.MaxDistance.ToFloat();
const CVector3D tmp = spline.GetPosition(time);
const float groundY = g_Game->GetWorld()->GetTerrain()->GetExactGroundLevel(tmp.X, tmp.Z);
g_Renderer.GetDebugRenderer().DrawLine(tmp, CVector3D(tmp.X, groundY, tmp.Z), splineColor, 0.1f);
}
glEnd();
// Height indicator
if (g_Game && g_Game->GetWorld() && g_Game->GetWorld()->GetTerrain())
{
glLineWidth(1.1f);
glBegin(GL_LINES);
for (int i = 0; i <= smoothness; ++i)
{
time = start * i / spline.MaxDistance.ToFloat();
CVector3D tmp = spline.GetPosition(time);
float groundY = g_Game->GetWorld()->GetTerrain()->GetExactGroundLevel(tmp.X, tmp.Z);
glVertex3f(tmp.X, tmp.Y, tmp.Z);
glVertex3f(tmp.X, groundY, tmp.Z);
}
glEnd();
}
glDisable(GL_LINE_SMOOTH);
glLineWidth(1.0f);
}
else
{
smoothness /= 2;
start = spline.MaxDistance.ToFloat() / smoothness;
glEnable(GL_POINT_SMOOTH);
glPointSize(3.0f);
glBegin(GL_POINTS);
for (int i = 0; i <= smoothness; ++i)
{
time = start * i / spline.MaxDistance.ToFloat();
CVector3D tmp = spline.GetPosition(time);
glVertex3f(tmp.X, tmp.Y, tmp.Z);
}
glEnd();
glPointSize(1.0f);
glDisable(GL_POINT_SMOOTH);
}
glDisable(GL_BLEND);
glEnable(GL_DEPTH_TEST);
#endif
}
void CCinemaManager::DrawNodes(const RNSpline& spline, const CColor& nodeColor) const
{
#if CONFIG2_GLES
#warning TODO : implement CCinemaPath on GLES
#else
glDisable(GL_DEPTH_TEST);
glEnable(GL_POINT_SMOOTH);
glPointSize(7.0f);
glColor4f(nodeColor.r, nodeColor.g, nodeColor.b, nodeColor.a);
glBegin(GL_POINTS);
for (const SplineData& node : spline.GetAllNodes())
glVertex3f(node.Position.X.ToFloat(), node.Position.Y.ToFloat(), node.Position.Z.ToFloat());
glEnd();
glPointSize(1.0f);
glDisable(GL_POINT_SMOOTH);
glEnable(GL_DEPTH_TEST);
#endif
{
g_Renderer.GetDebugRenderer().DrawCircle(
CVector3D(node.Position.X.ToFloat(), node.Position.Y.ToFloat(), node.Position.Z.ToFloat()),
0.5f, nodeColor);
}
}
bool CCinemaManager::IsEnabled() const

View File

@ -34,12 +34,9 @@ public:
~CCinemaManager() {}
/**
* Renders black bars and paths (if enabled)
* Renders paths and their nodes (if enabled).
*/
void Render() const;
void DrawPaths() const;
void DrawSpline(const RNSpline& spline, const CColor& splineColor, int smoothness, bool lines) const;
void DrawNodes(const RNSpline& spline, const CColor& nodesColor) const;
bool IsPlaying() const;
bool IsEnabled() const;
@ -54,6 +51,10 @@ public:
void SetPathsDrawing(const bool drawPath);
private:
void DrawPaths() const;
void DrawSpline(const RNSpline& spline, const CColor& splineColor, int smoothness) const;
void DrawNodes(const RNSpline& spline, const CColor& nodesColor) const;
bool m_DrawPaths;
};

View File

@ -56,6 +56,7 @@ void CDebugRenderer::DrawLine(const std::vector<CVector3D>& line, const CColor&
const CVector3D cameraIn = g_Renderer.GetViewCamera().GetOrientation().GetIn();
std::vector<float> vertices;
vertices.reserve(line.size() * 6 * 3);
#define ADD(position) \
vertices.emplace_back((position).X); \
vertices.emplace_back((position).Y); \
@ -90,6 +91,52 @@ void CDebugRenderer::DrawLine(const std::vector<CVector3D>& line, const CColor&
#endif
}
void CDebugRenderer::DrawCircle(const CVector3D& origin, const float radius, const CColor& color)
{
#if CONFIG2_GLES
#warning TODO: implement drawing circle for GLES
#else
CShaderTechniquePtr debugCircleTech =
g_Renderer.GetShaderManager().LoadEffect(str_debug_line);
debugCircleTech->BeginPass();
CShaderProgramPtr debugCircleShader = debugCircleTech->GetShader();
const CCamera& camera = g_Renderer.GetViewCamera();
debugCircleShader->Bind();
debugCircleShader->Uniform(str_transform, camera.GetViewProjection());
debugCircleShader->Uniform(str_color, color);
const CVector3D cameraUp = camera.GetOrientation().GetUp();
const CVector3D cameraLeft = camera.GetOrientation().GetLeft();
std::vector<float> vertices;
#define ADD(position) \
vertices.emplace_back((position).X); \
vertices.emplace_back((position).Y); \
vertices.emplace_back((position).Z);
ADD(origin)
constexpr size_t segments = 16;
for (size_t idx = 0; idx <= segments; ++idx)
{
const float angle = M_PI * 2.0f * idx / segments;
const CVector3D offset = cameraUp * sin(angle) - cameraLeft * cos(angle);
ADD(origin + offset * radius)
}
#undef ADD
debugCircleShader->VertexPointer(3, GL_FLOAT, 0, vertices.data());
debugCircleShader->AssertPointersBound();
glDrawArrays(GL_TRIANGLE_FAN, 0, vertices.size() / 3);
debugCircleShader->Unbind();
debugCircleTech->EndPass();
#endif
}
void CDebugRenderer::DrawCameraFrustum(const CCamera& camera, int intermediates) const
{
#if CONFIG2_GLES

View File

@ -40,6 +40,11 @@ public:
void DrawLine(const CVector3D& from, const CVector3D& to, const CColor& color, const float width);
void DrawLine(const std::vector<CVector3D>& line, const CColor& color, const float width);
/**
* Render the circle in world space oriented to the view camera.
*/
void DrawCircle(const CVector3D& origin, const float radius, const CColor& color);
/**
* Render: Renders the camera's frustum in world space.
* The caller should set the color using glColorXy before calling Render.