xoreos  0.0.5
renderqueue.cpp
Go to the documentation of this file.
1 /* xoreos - A reimplementation of BioWare's Aurora engine
2  *
3  * xoreos is the legal property of its developers, whose names
4  * can be found in the AUTHORS file distributed with this source
5  * distribution.
6  *
7  * xoreos is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 3
10  * of the License, or (at your option) any later version.
11  *
12  * xoreos is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with xoreos. If not, see <http://www.gnu.org/licenses/>.
19  */
20 
25 #include <cassert>
26 
27 #include "glm/gtc/type_ptr.hpp"
28 
30 #include "src/common/util.h"
31 
32 #include <algorithm>
33 
34 namespace Graphics {
35 
36 namespace Render {
37 
39 {
40  if (a.program < b.program)
41  return false;
42  else if (a.program > b.program)
43  return true;
44  if (a.material < b.material) // node_program values are equal.
45  return false;
46  else if (a.material > b.material)
47  return true;
48  else // node material values are equal, compare mesh values next.
49  return (a.mesh <= b.mesh ? false : true);
50  // No, surfaces are not compared here.
51 }
52 
54  return (a.reference <= b.reference);
55  //return (a.reference.lengthSquared() <= b.reference.lengthSquared());
56 }
57 
58 RenderQueue::RenderQueue(uint32 precache) : _nodeArray(precache), _cameraReference(0.0f, 0.0f, 0.0f) {
59 // _nodeArray.reserve(1000);
60 }
61 
63 {
64  _nodeArray.clear();
65 }
66 
67 void RenderQueue::setCameraReference(const glm::vec3 &reference) {
68  _cameraReference = reference;
69 }
70 
71 void RenderQueue::queueItem(Shader::ShaderProgram *program, Shader::ShaderSurface *surface, Shader::ShaderMaterial *material, Mesh::Mesh *mesh, const glm::mat4 *transform, float alpha) {
72  if (program->glid == 0) {
73  return;
74  }
75  glm::vec3 ref((*transform)[3]);
76  ref += mesh->getCentre();
77  ref -= _cameraReference;
78  // Length squared of ref serves as a suitable depth sorting value.
79  _nodeArray.push_back(RenderQueueNode(program, surface, material, mesh, transform, alpha, glm::dot(ref, ref)));
80 }
81 
82 void RenderQueue::queueItem(Shader::ShaderRenderable *renderable, const glm::mat4 *transform, float alpha) {
83  if (renderable->getProgram()->glid == 0) {
84  return;
85  }
86  glm::vec3 ref((*transform)[3]);
87  ref -= _cameraReference;
88  // Length squared of ref serves as a suitable depth sorting value.
89  _nodeArray.push_back(RenderQueueNode(renderable->getProgram(), renderable->getSurface(), renderable->getMaterial(), renderable->getMesh(), transform, alpha, glm::dot(ref, ref)));
90 }
91 
93  if (_nodeArray.size() > 1) {
94  std::sort(_nodeArray.begin(), _nodeArray.end(), compareShader);
95  }
96 }
97 
99  if (_nodeArray.size() > 1) {
100  std::sort(_nodeArray.begin(), _nodeArray.end(), compareDepth);
101  }
102 }
103 
105  if (_nodeArray.size() == 0) {
106  return;
107  }
108 
109  Shader::ShaderProgram *currentProgram = 0;
110  Shader::ShaderMaterial *currentMaterial = 0;
111  Shader::ShaderSurface *currentSurface = 0;
112  Mesh::Mesh *currentMesh = 0;
113 
114  uint32 i = 0;
115  uint32 limit = _nodeArray.size();
116  while (i < limit) {
117  assert(_nodeArray[i].program);
118  if (currentProgram != _nodeArray[i].program) {
119  currentProgram = _nodeArray[i].program;
120  glUseProgram(currentProgram->glid);
121 
122  if (currentSurface != 0) {
123  currentSurface->unbindGLState();
124  }
125  if (currentMaterial != 0) {
126  currentMaterial->unbindGLState();
127  }
128  currentMaterial = 0;
129  currentSurface = 0;
130  }
131 
132  assert(_nodeArray[i].material);
133  if (currentMaterial != _nodeArray[i].material) {
134  if (currentMaterial != 0) {
135  currentMaterial->unbindGLState();
136  }
137  currentMaterial = _nodeArray[i].material;
138  currentMaterial->bindProgramNoFade(currentProgram);
139  currentMaterial->bindGLState();
140  }
141 
142  assert(_nodeArray[i].surface);
143  assert(_nodeArray[i].mesh);
144 
145  if (currentSurface != _nodeArray[i].surface) {
146  if (currentSurface != 0) {
147  currentSurface->unbindGLState();
148  }
149  currentSurface = _nodeArray[i].surface;
150  currentSurface->bindGLState();
151  }
152 
153  currentSurface = _nodeArray[i].surface;
154  currentMesh = _nodeArray[i].mesh;
155  currentMesh->renderBind(); // Binds VAO ready for rendering.
156 
157  // There's at least one mesh to be rendering here.
158  assert(_nodeArray[i].transform);
159  assert(currentSurface);
160  assert(currentMaterial);
161 
162  currentSurface->bindProgram(currentProgram, _nodeArray[i].transform);
163  //currentSurface->bindObjectModelview(currentProgram, _nodeArray[i].transform);
164  currentMaterial->bindFade(currentProgram, _nodeArray[i].alpha);
165  currentMesh->render();
166 
167  ++i; // Move to next object.
168  while ((i < limit) && (_nodeArray[i].mesh == currentMesh) && (_nodeArray[i].material == currentMaterial) && (_nodeArray[i].surface == currentSurface)) {
169  // Next object is basically the same, but will have a different object modelview transform. So rebind that, and render again.
170  assert(_nodeArray[i].transform);
171  currentSurface->bindObjectModelview(currentProgram, _nodeArray[i].transform);
172  currentMaterial->bindFade(currentProgram, _nodeArray[i].alpha);
173  currentMesh->render();
174  ++i;
175  }
176  // Done rendering, unbind the mesh, and onwards into the queue.
177  currentMesh->renderUnbind();
178  }
179  if (currentMaterial) {
180  currentMaterial->unbindGLState();
181  }
182  if (currentSurface) {
183  currentSurface->unbindGLState();
184  }
185 
186  glUseProgram(0);
187  glActiveTexture(GL_TEXTURE0);
188 }
189 
191  _nodeArray.clear();
192 }
193 
194 } // namespace Render
195 
196 } // namespace Graphics
void clear()
Clear the queue of all items.
void bindFade(Shader::ShaderProgram *program, float fade)
float reference
Reference point to the camera location, primarily used for depth sorting.
Definition: renderqueue.h:48
void bindProgram(Shader::ShaderProgram *program)
Utility templates and functions.
void renderUnbind()
Definition: mesh.cpp:227
void sortShader()
Sort queue elements by shader program.
Definition: renderqueue.cpp:92
Render queue, for efficient OpenGL render calls.
RenderQueue(uint32 precache=1000)
Definition: renderqueue.cpp:58
void queueItem(Shader::ShaderProgram *program, Shader::ShaderSurface *surface, Shader::ShaderMaterial *material, Mesh::Mesh *mesh, const glm::mat4 *transform, float alpha)
Definition: renderqueue.cpp:71
void renderBind()
Follows the steps of renderImmediate, but broken into different functions.
Definition: mesh.cpp:177
const glm::vec3 & getCentre() const
Definition: mesh.cpp:268
void sortDepth()
Sort queue elements by depth.
Definition: renderqueue.cpp:98
uint32_t uint32
Definition: types.h:204
void bindProgramNoFade(Shader::ShaderProgram *program)
std::vector< RenderQueueNode > _nodeArray
Definition: renderqueue.h:75
void render()
Render all queued items.
void setCameraReference(const glm::vec3 &reference)
Definition: renderqueue.cpp:67
bool compareShader(const RenderQueue::RenderQueueNode &a, const RenderQueue::RenderQueueNode &b)
Definition: renderqueue.cpp:38
bool compareDepth(const RenderQueue::RenderQueueNode &a, const RenderQueue::RenderQueueNode &b)
Definition: renderqueue.cpp:53
void bindObjectModelview(Shader::ShaderProgram *program, const glm::mat4 *t)