source: ps/trunk/source/renderer/ParticleRenderer.cpp@ 25269

Last change on this file since 25269 was 25269, checked in by Vladislav Belov, 3 years ago

Removes low-level GL calls from graphics and geometrics primitives and adds DebugRenderer.

Tested By: Freagarach

Differential Revision: https://code.wildfiregames.com/D3857

  • Property svn:eol-style set to native
File size: 4.7 KB
Line 
1/* Copyright (C) 2021 Wildfire Games.
2 * This file is part of 0 A.D.
3 *
4 * 0 A.D. is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * 0 A.D. is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "precompiled.h"
19
20#include "ParticleRenderer.h"
21
22#include "graphics/ParticleEmitter.h"
23#include "graphics/ShaderDefines.h"
24#include "graphics/ShaderManager.h"
25#include "graphics/TextureManager.h"
26#include "ps/Profile.h"
27#include "renderer/DebugRenderer.h"
28#include "renderer/Renderer.h"
29
30struct ParticleRendererInternals
31{
32 int frameNumber;
33 CShaderTechniquePtr shader;
34 CShaderTechniquePtr shaderSolid;
35 std::vector<CParticleEmitter*> emitters[CRenderer::CULL_MAX];
36};
37
38ParticleRenderer::ParticleRenderer()
39{
40 m = new ParticleRendererInternals();
41 m->frameNumber = 0;
42}
43
44ParticleRenderer::~ParticleRenderer()
45{
46 delete m;
47}
48
49void ParticleRenderer::Submit(int cullGroup, CParticleEmitter* emitter)
50{
51 m->emitters[cullGroup].push_back(emitter);
52}
53
54void ParticleRenderer::EndFrame()
55{
56 for (int cullGroup = 0; cullGroup < CRenderer::CULL_MAX; ++cullGroup)
57 m->emitters[cullGroup].clear();
58 // this should leave the capacity unchanged, which is okay since it
59 // won't be very large or very variable
60}
61
62struct SortEmitterDistance
63{
64 SortEmitterDistance(const CMatrix3D& m) : worldToCam(m) { }
65
66 // TODO: if this is slow, we should pre-compute the distance for each emitter
67
68 bool operator()(CParticleEmitter* const& a, CParticleEmitter* const& b)
69 {
70 CVector3D posa = a->GetPosition();
71 CVector3D posb = b->GetPosition();
72 if (posa == posb)
73 return false;
74 float dista = worldToCam.Transform(posa).LengthSquared();
75 float distb = worldToCam.Transform(posb).LengthSquared();
76 return distb < dista;
77 }
78
79 CMatrix3D worldToCam;
80};
81
82void ParticleRenderer::PrepareForRendering(const CShaderDefines& context)
83{
84 PROFILE3("prepare particles");
85
86 // Can't load the shader in the constructor because it's called before the
87 // renderer initialisation is complete, so load it the first time through here
88 if (!m->shader)
89 {
90 m->shader = g_Renderer.GetShaderManager().LoadEffect(str_particle, context, CShaderDefines());
91 m->shaderSolid = g_Renderer.GetShaderManager().LoadEffect(str_particle_solid, context, CShaderDefines());
92 }
93
94 ++m->frameNumber;
95
96 for (int cullGroup = 0; cullGroup < CRenderer::CULL_MAX; ++cullGroup)
97 {
98 PROFILE("update emitters");
99 for (size_t i = 0; i < m->emitters[cullGroup].size(); ++i)
100 {
101 CParticleEmitter* emitter = m->emitters[cullGroup][i];
102 emitter->UpdateArrayData(m->frameNumber);
103 emitter->PrepareForRendering();
104 }
105 }
106
107 for (int cullGroup = 0; cullGroup < CRenderer::CULL_MAX; ++cullGroup)
108 {
109 // Sort back-to-front by distance from camera
110 PROFILE("sort emitters");
111 CMatrix3D worldToCam;
112 g_Renderer.GetViewCamera().GetOrientation().GetInverse(worldToCam);
113 std::stable_sort(m->emitters[cullGroup].begin(), m->emitters[cullGroup].end(), SortEmitterDistance(worldToCam));
114 }
115
116 // TODO: should batch by texture here when possible, maybe
117}
118
119void ParticleRenderer::RenderParticles(int cullGroup, bool solidColor)
120{
121 CShaderTechniquePtr shader = solidColor ? m->shaderSolid : m->shader;
122
123 std::vector<CParticleEmitter*>& emitters = m->emitters[cullGroup];
124
125 shader->BeginPass();
126
127 shader->GetShader()->Uniform(str_transform, g_Renderer.GetViewCamera().GetViewProjection());
128 shader->GetShader()->Uniform(str_modelViewMatrix, g_Renderer.GetViewCamera().GetOrientation().GetInverse());
129
130 if (!solidColor)
131 glEnable(GL_BLEND);
132 glDepthMask(0);
133
134 for (size_t i = 0; i < emitters.size(); ++i)
135 {
136 CParticleEmitter* emitter = emitters[i];
137
138 emitter->Bind(shader->GetShader());
139 emitter->RenderArray(shader->GetShader());
140 }
141
142 CVertexBuffer::Unbind();
143
144 pglBlendEquationEXT(GL_FUNC_ADD);
145 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
146
147 glDisable(GL_BLEND);
148 glDepthMask(1);
149
150 shader->EndPass();
151}
152
153void ParticleRenderer::RenderBounds(int cullGroup, CShaderProgramPtr& shader)
154{
155 std::vector<CParticleEmitter*>& emitters = m->emitters[cullGroup];
156
157 for (size_t i = 0; i < emitters.size(); ++i)
158 {
159 CParticleEmitter* emitter = emitters[i];
160
161 CBoundingBoxAligned bounds = emitter->m_Type->CalculateBounds(emitter->GetPosition(), emitter->GetParticleBounds());
162 g_Renderer.GetDebugRenderer().DrawBoundingBox(bounds, shader);
163 }
164}
Note: See TracBrowser for help on using the repository browser.