Changeset 25314

Timestamp:
Apr 25, 2021, 10:48:44 PM (3 years ago)
Author:
Vladislav Belov
Message:

Adds line drawing to DebugRenderer.

Location:
ps/trunk/source
Files:
11 edited

Legend:

Unmodified
Added
Removed
  • ps/trunk/source/graphics/Overlay.h

    r24352 r25314  
    1 /* Copyright (C) 2020 Wildfire Games.
     1/* Copyright (C) 202 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
     
    3636/**
    3737 * Line-based overlay, with world-space coordinates, rendered in the world
    38  * potentially behind other objects. Designed for selection circles and debug info.
     38 * potentially behind other objects. Designed for debug info.
    3939 */
    4040struct SOverlayLine
    4141{
    42     SOverlayLine() : m_Thickness(1) { }
    43 
    44     CColor m_Color;
    45     std::vector<float> m_Coords; // (x, y, z) vertex coordinate triples; shape is not automatically closed
    46     u8 m_Thickness; // in pixels
    47 
    48     void PushCoords(const CVector3D& v) { PushCoords(v.X, v.Y, v.Z); }
     42    SOverlayLine() : m_Thickness(0.1f) { }
     43
     44    CColor m_Color;
     45    // Shape is not automatically closed.
     46    std::vector<CVector3D> m_Coords;
     47    // Half-width of the line, in world-space units.
     48    float m_Thickness;
     49
     50    void PushCoords(const CVector3D& v) { m_Coords.emplace_back(v); }
     51
    4952    void PushCoords(const float x, const float y, const float z)
    5053    {
    51         m_Coords.push_back(x);
    52         m_Coords.push_back(y);
    53         m_Coords.push_back(z);
     54        m_Coords.emplace_back(x, y, z);
    5455    }
    5556};
  • ps/trunk/source/renderer/DebugRenderer.cpp

    r25271 r25314  
    3737        return;
    3838
     39
     40
     41
     42
     43
    3944#if CONFIG2_GLES
    4045    #warning TODO: implement drawing line for GLES
     
    4954    debugLineShader->Uniform(str_color, color);
    5055
    51     // Basis to set a line with the width in R^3 space.
    52     const CVector3D direction = (to - from).Normalized();
    53     const CVector3D upCandidate = direction.Dot(CVector3D(0.0f, 1.0f, 0.0f)) > 0.9f ?
    54         CVector3D(1.0f, 0.0f, 0.0f) :
    55         CVector3D(0.0f, 1.0f, 0.0f);
    56     const CVector3D right = direction.Cross(upCandidate).Normalized();
    57     const CVector3D up = direction.Cross(right);
     56    const CVector3D cameraIn = g_Renderer.GetViewCamera().GetOrientation().GetIn();
    5857
    5958    std::vector<float> vertices;
    60     const size_t splits = 3;
    61     float angle = 0.0f;
    62     const float step = static_cast<float>(M_PI * 2.0f / splits);
    63     for (size_t idx = 0; idx <= splits; ++idx, angle += step)
    64     {
    65         const CVector3D offset = (right * std::cos(angle) + up * std::sin(angle)) * width;
    66         const CVector3D a = from + offset;
    67         const CVector3D b = to + offset;
    68         vertices.emplace_back(a.X);
    69         vertices.emplace_back(a.Y);
    70         vertices.emplace_back(a.Z);
    71         vertices.emplace_back(b.X);
    72         vertices.emplace_back(b.Y);
    73         vertices.emplace_back(b.Z);
    74     }
    75     debugLineShader->VertexPointer(3, GL_FLOAT, sizeof(float) * 3, vertices.data());
     59#define ADD(position) \
     60    vertices.emplace_back((position).X); \
     61    vertices.emplace_back((position).Y); \
     62    vertices.emplace_back((position).Z);
     63
     64    for (size_t idx = 1; idx < line.size(); ++idx)
     65    {
     66        const CVector3D from = line[idx - 1];
     67        const CVector3D to = line[idx];
     68        const CVector3D direction = (to - from).Normalized();
     69        const CVector3D view = direction.Dot(cameraIn) > 0.9f ?
     70            CVector3D(0.0f, 1.0f, 0.0f) :
     71            cameraIn;
     72        const CVector3D offset = view.Cross(direction).Normalized() * width;
     73
     74        ADD(from + offset)
     75        ADD(to - offset)
     76        ADD(to + offset)
     77        ADD(from + offset)
     78        ADD(from - offset)
     79        ADD(to - offset)
     80    }
     81
     82#undef ADD
     83
     84    debugLineShader->VertexPointer(3, GL_FLOAT, 0, vertices.data());
    7685    debugLineShader->AssertPointersBound();
    77     glDrawArrays(GL_TRIANGLE_STRIP, 0, vertices.size() / 3);
     86    glDrawArrays(GL_TRIANGLE, 0, vertices.size() / 3);
    7887
    7988    debugLineShader->Unbind();
  • ps/trunk/source/renderer/DebugRenderer.h

    r25271 r25314  
    2121#include "graphics/ShaderProgramPtr.h"
    2222
     23
     24
    2325class CBoundingBoxAligned;
    2426class CBrush;
     
    3739     */
    3840    void DrawLine(const CVector3D& from, const CVector3D& to, const CColor& color, const float width);
     41
    3942
    4043    /**
  • ps/trunk/source/renderer/OverlayRenderer.cpp

    r25285 r25314  
    3131#include "ps/Game.h"
    3232#include "ps/Profile.h"
     33
    3334#include "renderer/Renderer.h"
    3435#include "renderer/TexturedLineRData.h"
     
    225226void OverlayRenderer::Submit(SOverlayLine* line)
    226227{
    227     ENSURE(line->m_Coords.size() % 3 == 0);
    228 
    229228    m->lines.push_back(line);
    230229}
     
    382381#warning TODO: implement OverlayRenderer::RenderOverlaysBeforeWater for GLES
    383382#else
    384     if (g_Renderer.GetOverlayRenderMode() == WIREFRAME)
    385         glPolygonMode(GL_FRONT_AND_BACK, GL_LINE);
    386 
    387     pglActiveTextureARB(GL_TEXTURE0);
    388     glDisable(GL_TEXTURE_2D);
    389383    glEnable(GL_BLEND);
    390 
    391384    // Ignore z so that we draw behind terrain (but don't disable GL_DEPTH_TEST
    392385    // since we still want to write to the z buffer)
    393386    glDepthFunc(GL_ALWAYS);
    394387
    395     for (size_t i = 0; i < m->lines.size(); ++i)
    396     {
    397         SOverlayLine* line = m->lines[i];
     388    for (SOverlayLine* line : m->lines)
     389    {
    398390        if (line->m_Coords.empty())
    399391            continue;
    400392
    401         ENSURE(line->m_Coords.size() % 3 == 0);
    402 
    403         glColor4fv(line->m_Color.FloatArray());
    404         glLineWidth((float)line->m_Thickness);
    405 
    406         glInterleavedArrays(GL_V3F, sizeof(float)*3, &line->m_Coords[0]);
    407         glDrawArrays(GL_LINE_STRIP, 0, (GLsizei)line->m_Coords.size()/3);
    408     }
    409 
    410     glDisableClientState(GL_VERTEX_ARRAY);
    411 
    412     glLineWidth(1.f);
     393        g_Renderer.GetDebugRenderer().DrawLine(line->m_Coords, line->m_Color, static_cast<float>(line->m_Thickness));
     394    }
     395
    413396    glDepthFunc(GL_LEQUAL);
    414397    glDisable(GL_BLEND);
    415 
    416     if (g_Renderer.GetOverlayRenderMode() == WIREFRAME)
    417         glPolygonMode(GL_FRONT_AND_BACK, GL_FILL);
    418398#endif
    419399}
  • ps/trunk/source/renderer/PatchRData.cpp

    r24870 r25314  
    3838#include "ps/World.h"
    3939#include "renderer/AlphaMapCalculator.h"
     40
    4041#include "renderer/Renderer.h"
    4142#include "renderer/TerrainRenderer.h"
     
    11221123    }
    11231124
    1124 #if CONFIG2_GLES
    1125 #warning TODO: implement CPatchRData::RenderOutlines for GLES
    1126 #else
    1127     glVertexPointer(3, GL_FLOAT, sizeof(CVector3D), &line[0]);
    1128     glDrawArrays(GL_LINE_STRIP, 0, line.size());
    1129 #endif
     1125    g_Renderer.GetDebugRenderer().DrawLine(line, CColor(0.0f, 0.0f, 1.0f, 1.0f), 0.1f);
    11301126}
    11311127
  • ps/trunk/source/renderer/TerrainRenderer.cpp

    r25114 r25314  
    354354        return;
    355355
    356 #if CONFIG2_GLES
    357 #warning TODO: implement TerrainRenderer::RenderOutlines for GLES
    358 #else
    359     glEnableClientState(GL_VERTEX_ARRAY);
    360356    for (size_t i = 0; i < visiblePatches.size(); ++i)
    361357        visiblePatches[i]->RenderOutline();
    362     glDisableClientState(GL_VERTEX_ARRAY);
    363 #endif
    364358}
    365359
  • ps/trunk/source/simulation2/components/CCmpRallyPointRenderer.cpp

    r25228 r25314  
    442442            SOverlayLine overlayLine;
    443443            overlayLine.m_Color = CColor(1.0f, 0.0f, 0.0f, 1.0f);
    444             overlayLine.m_Thickness = 2;
     444            overlayLine.m_Thickness = ;
    445445            SimRender::ConstructSquareOnGround(GetSimContext(), point.X, point.Y, 0.2f, 0.2f, 1.0f, overlayLine, true);
    446446            m_DebugNodeOverlays[index].push_back(overlayLine);
  • ps/trunk/source/simulation2/components/CCmpSelectable.cpp

    r25228 r25314  
    671671            {
    672672                SimRender::ConstructBoxOutline(cmpVisual->GetBounds(), *m_DebugBoundingBoxOverlay);
    673                 m_DebugBoundingBoxOverlay->m_Thickness = 2;
     673                m_DebugBoundingBoxOverlay->m_Thickness = ;
    674674                m_DebugBoundingBoxOverlay->m_Color = CColor(1.f, 0.f, 0.f, 1.f);
    675675
    676676                SimRender::ConstructBoxOutline(cmpVisual->GetSelectionBox(), *m_DebugSelectionBoxOverlay);
    677                 m_DebugSelectionBoxOverlay->m_Thickness = 2;
     677                m_DebugSelectionBoxOverlay->m_Thickness = ;
    678678                m_DebugSelectionBoxOverlay->m_Color = CColor(0.f, 1.f, 0.f, 1.f);
    679679
  • ps/trunk/source/simulation2/components/CCmpTerritoryManager.cpp

    r24487 r25314  
    1 /* Copyright (C) 2020 Wildfire Games.
     1/* Copyright (C) 202 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
     
    661661                    overlayNode.m_Color = CColor(1.0f, 1.0f, 1.0f, 1.0f);
    662662
    663                 overlayNode.m_Thickness = 1;
     663                overlayNode.m_Thickness = ;
    664664                SimRender::ConstructCircleOnGround(GetSimContext(), boundaries[i].points[j].X, boundaries[i].points[j].Y, 0.1f, overlayNode, true);
    665665                m_DebugBoundaryLineNodes.push_back(overlayNode);
  • ps/trunk/source/simulation2/helpers/Render.cpp

    r24227 r25314  
    1 /* Copyright (C) 2019 Wildfire Games.
     1/* Copyright (C) 20 Wildfire Games.
    22 * This file is part of 0 A.D.
    33 *
     
    5858    for (size_t i = 0; i < xz.size(); i += 2)
    5959    {
    60         float px = xz[i];
    61         float pz = xz[i+1];
    62         float py = std::max(water, cmpTerrain->GetExactGroundLevel(px, pz)) + heightOffset;
    63         overlay.m_Coords.push_back(px);
    64         overlay.m_Coords.push_back(py);
    65         overlay.m_Coords.push_back(pz);
     60        const float px = xz[i];
     61        const float pz = xz[i+1];
     62        const float py = std::max(water, cmpTerrain->GetExactGroundLevel(px, pz)) + heightOffset;
     63        overlay.PushCoords(px, py, pz);
    6664    }
    6765}
     
    10098        // Start at the center point
    10199        cy = std::max(water, cmpTerrain->GetExactGroundLevel(x, z)) + heightOffset;
    102         overlay.m_Coords.push_back(x);
    103         overlay.m_Coords.push_back(cy);
    104         overlay.m_Coords.push_back(z);
     100        overlay.PushCoords(x, cy, z);
    105101    }
    106102
     
    111107        float pz = z + radius * sinf(a);
    112108        float py = std::max(water, cmpTerrain->GetExactGroundLevel(px, pz)) + heightOffset;
    113         overlay.m_Coords.push_back(px);
    114         overlay.m_Coords.push_back(py);
    115         overlay.m_Coords.push_back(pz);
     109        overlay.PushCoords(px, py, pz);
    116110    }
    117111
     
    119113    {
    120114        // Return to the center point
    121         overlay.m_Coords.push_back(x);
    122         overlay.m_Coords.push_back(cy);
    123         overlay.m_Coords.push_back(z);
     115        overlay.PushCoords(x, cy, z);
    124116    }
    125117}
     
    196188        float pz = coords[i].second;
    197189        float py = std::max(water, cmpTerrain->GetExactGroundLevel(px, pz)) + heightOffset;
    198         overlay.m_Coords.push_back(px);
    199         overlay.m_Coords.push_back(py);
    200         overlay.m_Coords.push_back(pz);
     190        overlay.PushCoords(px, py, pz);
    201191    }
    202192}
     
    327317    outZ.m_Color = CColor(0, 0, 1, .5f); // Z axis; blue
    328318
    329     outX.m_Thickness = 2;
    330     outY.m_Thickness = 2;
    331     outZ.m_Thickness = 2;
     319    outX.m_Thickness = ;
     320    outY.m_Thickness = ;
     321    outZ.m_Thickness = ;
    332322
    333323    CVector3D origin = coordSystem.GetTranslation();
  • ps/trunk/source/tools/atlas/GameInterface/ActorViewer.cpp

    r25222 r25314  
    131131            {
    132132                SelectionBoxOverlay.m_Color = CColor(35/255.f, 86/255.f, 188/255.f, .75f); // pretty blue
    133                 SelectionBoxOverlay.m_Thickness = 2;
     133                SelectionBoxOverlay.m_Thickness = ;
    134134
    135135                SimRender::ConstructBoxOutline(cmpVisual->GetSelectionBox(), SelectionBoxOverlay);
Note: See TracChangeset for help on using the changeset viewer.