xoreos  0.0.5
shaderrenderable.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 "glm/gtc/type_ptr.hpp"
26 
28 
29 namespace Graphics {
30 
31 namespace Shader {
32 
33 ShaderRenderable::ShaderRenderable() : _surface(0), _material(0), _program(0), _mesh(0) {
34 }
35 
36 ShaderRenderable::ShaderRenderable(Shader::ShaderSurface *surface, Shader::ShaderMaterial *material, Mesh::Mesh *mesh) : _surface(surface), _material(material), _program(0), _mesh(mesh) {
37  //_program = ShaderMan.getShaderProgram(_surface->getVertexShader(), _material->getFragmentShader());
41  updateProgram();
42 }
43 
44 ShaderRenderable::ShaderRenderable(Shader::ShaderRenderable *src) : _surface(0), _material(0), _program(0), _mesh(0) {
45  if (src) {
46  setSurface(src->_surface, false);
47  setMaterial(src->_material, false);
48  setMesh(src->_mesh);
49  updateProgram();
50  }
51 }
52 
53 ShaderRenderable::ShaderRenderable(const Shader::ShaderRenderable &src) : _surface(src._surface), _material(src._material), _program(src._program), _mesh(src._mesh) {
54  if (_surface) {
56  }
57  if (_material) {
59  }
60  if (_mesh) {
62  }
63 }
64 
66  setSurface(src._surface, false);
67  setMaterial(src._material, false);
68  setMesh(src._mesh);
69  _program = src._program;
70  return *this;
71 }
72 
74  if (_surface) {
76  }
77  if (_material) {
79  }
80  if (_mesh) {
82  }
83 }
84 
86  return _surface;
87 }
88 
90  return _material;
91 }
92 
94  return _program;
95 }
96 
98  return _mesh;
99 }
100 
101 void ShaderRenderable::setSurface(Shader::ShaderSurface *surface, bool rebuildProgram) {
102  // TODO: check old surface for usage count decrement.
103  if (_surface) {
105  }
106  _surface = surface;
107  if (_surface) {
109  }
110  if (rebuildProgram) {
111  updateProgram();
112  }
113 }
114 
115 void ShaderRenderable::setMaterial(Shader::ShaderMaterial *material, bool rebuildProgram) {
116  if (_material) {
118  }
119  _material = material;
120  if (_material) {
122  }
123  if (rebuildProgram) {
124  updateProgram();
125  }
126 }
127 
129  if (_mesh) {
130  _mesh->useDecrement();
131  }
132  _mesh = mesh;
133  if (_mesh) {
134  _mesh->useIncrement();
135  }
136 }
137 
139  if (src) {
140  setSurface(src->getSurface(), false);
141  setMaterial(src->getMaterial(), false);
142  setMesh(src->getMesh());
143  } else {
144  setSurface(0, false);
145  setMaterial(0, false);
146  setMesh(0);
147  }
148  updateProgram();
149 }
150 
151 void ShaderRenderable::renderImmediate(const glm::mat4 &tform, float alpha) {
152  if (!_program->glid)
153  return;
154  glUseProgram(_program->glid);
155  _material->bindProgram(_program, alpha);
157  _surface->bindProgram(_program, &tform);
159 
161 
164  glUseProgram(0);
165 }
166 
168  if ((_surface != 0) && (_material != 0)) {
169  ShaderMan.registerShaderProgram(_surface->getVertexShader(), _material->getFragmentShader());
171  } else {
172  _program = 0;
173  }
174 }
175 
176 } // End of namespace Shader
177 
178 } // End of namespace Graphics
Shader renderable, a class for easier managing of a collection of items (surface, material...
Shader::ShaderObject * getFragmentShader() const
void setSurface(Shader::ShaderSurface *surface, bool rebuildProgram=true)
void bindProgram(Shader::ShaderProgram *program)
void copyRenderable(Shader::ShaderRenderable *src)
void bindProgram(Shader::ShaderProgram *program)
void renderImmediate()
Definition: mesh.cpp:171
#define ShaderMan
Shortcut for accessing the shader manager.
Definition: shader.h:293
Shader::ShaderObject * getVertexShader() const
const ShaderRenderable & operator=(const ShaderRenderable &src)
void useIncrement()
Definition: mesh.cpp:254
void renderImmediate(const glm::mat4 &tform, float alpha=1.0f)
void setMaterial(Shader::ShaderMaterial *material, bool rebuildProgram=true)
void useDecrement()
Definition: mesh.cpp:258