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

Last change on this file since 24652 was 24652, checked in by Vladislav Belov, 4 years ago

Removes usages of GetRenderPath to checking for support of shaders.

  • Property svn:eol-style set to native
File size: 4.6 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/Renderer.h"
28
29struct ParticleRendererInternals
30{
31 int frameNumber;
32 CShaderTechniquePtr shader;
33 CShaderTechniquePtr shaderSolid;
34 std::vector<CParticleEmitter*> emitters[CRenderer::CULL_MAX];
35};
36
37ParticleRenderer::ParticleRenderer()
38{
39 m = new ParticleRendererInternals();
40 m->frameNumber = 0;
41}
42
43ParticleRenderer::~ParticleRenderer()
44{
45 delete m;
46}
47
48void ParticleRenderer::Submit(int cullGroup, CParticleEmitter* emitter)
49{
50 m->emitters[cullGroup].push_back(emitter);
51}
52
53void ParticleRenderer::EndFrame()
54{
55 for (int cullGroup = 0; cullGroup < CRenderer::CULL_MAX; ++cullGroup)
56 m->emitters[cullGroup].clear();
57 // this should leave the capacity unchanged, which is okay since it
58 // won't be very large or very variable
59}
60
61struct SortEmitterDistance
62{
63 SortEmitterDistance(const CMatrix3D& m) : worldToCam(m) { }
64
65 // TODO: if this is slow, we should pre-compute the distance for each emitter
66
67 bool operator()(CParticleEmitter* const& a, CParticleEmitter* const& b)
68 {
69 CVector3D posa = a->GetPosition();
70 CVector3D posb = b->GetPosition();
71 if (posa == posb)
72 return false;
73 float dista = worldToCam.Transform(posa).LengthSquared();
74 float distb = worldToCam.Transform(posb).LengthSquared();
75 return distb < dista;
76 }
77
78 CMatrix3D worldToCam;
79};
80
81void ParticleRenderer::PrepareForRendering(const CShaderDefines& context)
82{
83 PROFILE3("prepare particles");
84
85 // Can't load the shader in the constructor because it's called before the
86 // renderer initialisation is complete, so load it the first time through here
87 if (!m->shader)
88 {
89 m->shader = g_Renderer.GetShaderManager().LoadEffect(str_particle, context, CShaderDefines());
90 m->shaderSolid = g_Renderer.GetShaderManager().LoadEffect(str_particle_solid, context, CShaderDefines());
91 }
92
93 ++m->frameNumber;
94
95 for (int cullGroup = 0; cullGroup < CRenderer::CULL_MAX; ++cullGroup)
96 {
97 PROFILE("update emitters");
98 for (size_t i = 0; i < m->emitters[cullGroup].size(); ++i)
99 {
100 CParticleEmitter* emitter = m->emitters[cullGroup][i];
101 emitter->UpdateArrayData(m->frameNumber);
102 emitter->PrepareForRendering();
103 }
104 }
105
106 for (int cullGroup = 0; cullGroup < CRenderer::CULL_MAX; ++cullGroup)
107 {
108 // Sort back-to-front by distance from camera
109 PROFILE("sort emitters");
110 CMatrix3D worldToCam;
111 g_Renderer.GetViewCamera().GetOrientation().GetInverse(worldToCam);
112 std::stable_sort(m->emitters[cullGroup].begin(), m->emitters[cullGroup].end(), SortEmitterDistance(worldToCam));
113 }
114
115 // TODO: should batch by texture here when possible, maybe
116}
117
118void ParticleRenderer::RenderParticles(int cullGroup, bool solidColor)
119{
120 CShaderTechniquePtr shader = solidColor ? m->shaderSolid : m->shader;
121
122 std::vector<CParticleEmitter*>& emitters = m->emitters[cullGroup];
123
124 shader->BeginPass();
125
126 shader->GetShader()->Uniform(str_transform, g_Renderer.GetViewCamera().GetViewProjection());
127 shader->GetShader()->Uniform(str_modelViewMatrix, g_Renderer.GetViewCamera().GetOrientation().GetInverse());
128
129 if (!solidColor)
130 glEnable(GL_BLEND);
131 glDepthMask(0);
132
133 for (size_t i = 0; i < emitters.size(); ++i)
134 {
135 CParticleEmitter* emitter = emitters[i];
136
137 emitter->Bind(shader->GetShader());
138 emitter->RenderArray(shader->GetShader());
139 }
140
141 CVertexBuffer::Unbind();
142
143 pglBlendEquationEXT(GL_FUNC_ADD);
144 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
145
146 glDisable(GL_BLEND);
147 glDepthMask(1);
148
149 shader->EndPass();
150}
151
152void ParticleRenderer::RenderBounds(int cullGroup, CShaderProgramPtr& shader)
153{
154 std::vector<CParticleEmitter*>& emitters = m->emitters[cullGroup];
155
156 for (size_t i = 0; i < emitters.size(); ++i)
157 {
158 CParticleEmitter* emitter = emitters[i];
159
160 CBoundingBoxAligned bounds = emitter->m_Type->CalculateBounds(emitter->GetPosition(), emitter->GetParticleBounds());
161 bounds.Render(shader);
162 }
163}
Note: See TracBrowser for help on using the repository browser.