| 1 |
/****************************************************************************** |
| 2 |
This source file is part of the Avogadro project. |
| 3 |
This source code is released under the 3-Clause BSD License, (see "LICENSE"). |
| 4 |
******************************************************************************/ |
| 5 |
|
| 6 |
#include "meshgeometry.h" |
| 7 |
|
| 8 |
#include "avogadrogl.h" |
| 9 |
#include "bufferobject.h" |
| 10 |
#include "camera.h" |
| 11 |
#include "scene.h" |
| 12 |
#include "shader.h" |
| 13 |
#include "shaderprogram.h" |
| 14 |
#include "vertexarrayobject.h" |
| 15 |
#include "visitor.h" |
| 16 |
|
| 17 |
#include <avogadro/core/matrix.h> |
| 18 |
#include <avogadro/core/vector.h> |
| 19 |
|
| 20 |
#include <iostream> |
| 21 |
#include <iterator> |
| 22 |
#include <limits> |
| 23 |
|
| 24 |
namespace { |
| 25 |
#include "mesh_fs.h" |
| 26 |
#include "mesh_vs.h" |
| 27 |
} // namespace |
| 28 |
|
| 29 |
using Avogadro::Vector3f; |
| 30 |
using Avogadro::Vector3ub; |
| 31 |
using Avogadro::Vector4ub; |
| 32 |
|
| 33 |
using std::cout; |
| 34 |
using std::endl; |
| 35 |
|
| 36 |
namespace Avogadro::Rendering { |
| 37 |
|
| 38 |
const unsigned int MeshGeometry::InvalidIndex = |
| 39 |
std::numeric_limits<unsigned int>::max(); |
| 40 |
|
| 41 |
class MeshGeometry::Private |
| 42 |
{ |
| 43 |
public: |
| 44 |
Private() {} |
| 45 |
|
| 46 |
BufferObject vbo; |
| 47 |
BufferObject ibo; |
| 48 |
VertexArrayObject vao; |
| 49 |
|
| 50 |
inline static Shader* vertexShader = nullptr; |
| 51 |
inline static Shader* fragmentShader = nullptr; |
| 52 |
inline static ShaderProgram* program = nullptr; |
| 53 |
|
| 54 |
size_t numberOfVertices; |
| 55 |
size_t numberOfIndices; |
| 56 |
}; |
| 57 |
|
| 58 |
MeshGeometry::MeshGeometry() |
| 59 |
: m_color(255, 0, 0), m_opacity(255), m_dirty(false), d(new Private) |
| 60 |
{ |
| 61 |
} |
| 62 |
|
| 63 |
MeshGeometry::MeshGeometry(const MeshGeometry& other) |
| 64 |
: Drawable(other), m_vertices(other.m_vertices), m_indices(other.m_indices), |
| 65 |
m_color(other.m_color), m_opacity(other.m_opacity), |
| 66 |
m_dirty(true), // Force rendering internals to be rebuilt |
| 67 |
d(new Private) |
| 68 |
{ |
| 69 |
} |
| 70 |
|
| 71 |
MeshGeometry::~MeshGeometry() |
| 72 |
{ |
| 73 |
delete d; |
| 74 |
} |
| 75 |
|
| 76 |
void MeshGeometry::accept(Visitor& visitor) |
| 77 |
{ |
| 78 |
visitor.visit(*this); |
| 79 |
} |
| 80 |
|
| 81 |
void MeshGeometry::update() |
| 82 |
{ |
| 83 |
if (m_vertices.empty() || m_indices.empty()) |
| 84 |
return; |
| 85 |
|
| 86 |
// Build and link the shader if it has not been used yet. |
| 87 |
// Must be done before VAO setup since we need the program for attribute |
| 88 |
// setup. |
| 89 |
if (d->vertexShader == nullptr) { |
| 90 |
d->vertexShader = new Shader; |
| 91 |
d->vertexShader->setType(Shader::Vertex); |
| 92 |
d->vertexShader->setSource(mesh_vs); |
| 93 |
|
| 94 |
d->fragmentShader = new Shader; |
| 95 |
d->fragmentShader->setType(Shader::Fragment); |
| 96 |
d->fragmentShader->setSource(mesh_fs); |
| 97 |
|
| 98 |
if (!d->vertexShader->compile()) |
| 99 |
cout << d->vertexShader->error() << endl; |
| 100 |
if (!d->fragmentShader->compile()) |
| 101 |
cout << d->fragmentShader->error() << endl; |
| 102 |
|
| 103 |
if (d->program == nullptr) |
| 104 |
d->program = new ShaderProgram; |
| 105 |
d->program->attachShader(*d->vertexShader); |
| 106 |
d->program->attachShader(*d->fragmentShader); |
| 107 |
if (!d->program->link()) |
| 108 |
cout << d->program->error() << endl; |
| 109 |
} |
| 110 |
|
| 111 |
// Check if the VBOs are ready, if not get them ready. |
| 112 |
if (!d->vbo.ready() || m_dirty) { |
| 113 |
d->vbo.upload(m_vertices, BufferObject::ArrayBuffer); |
| 114 |
d->ibo.upload(m_indices, BufferObject::ElementArrayBuffer); |
| 115 |
|
| 116 |
// Set up VAO with vertex attribute bindings (OpenGL 4.0 core profile) |
| 117 |
d->vao.bind(); |
| 118 |
d->vbo.bind(); |
| 119 |
d->ibo.bind(); |
| 120 |
|
| 121 |
ShaderProgram* program = d->program; |
| 122 |
if (!program->enableAttributeArray("vertex")) |
| 123 |
cout << program->error() << endl; |
| 124 |
if (!program->useAttributeArray("vertex", PackedVertex::vertexOffset(), |
| 125 |
sizeof(PackedVertex), FloatType, 3, |
| 126 |
ShaderProgram::NoNormalize)) { |
| 127 |
cout << program->error() << endl; |
| 128 |
} |
| 129 |
if (!program->enableAttributeArray("color")) |
| 130 |
cout << program->error() << endl; |
| 131 |
if (!program->useAttributeArray("color", PackedVertex::colorOffset(), |
| 132 |
sizeof(PackedVertex), UCharType, 4, |
| 133 |
ShaderProgram::Normalize)) { |
| 134 |
cout << program->error() << endl; |
| 135 |
} |
| 136 |
if (!program->enableAttributeArray("normal")) |
| 137 |
cout << program->error() << endl; |
| 138 |
if (!program->useAttributeArray("normal", PackedVertex::normalOffset(), |
| 139 |
sizeof(PackedVertex), FloatType, 3, |
| 140 |
ShaderProgram::NoNormalize)) { |
| 141 |
cout << program->error() << endl; |
| 142 |
} |
| 143 |
|
| 144 |
d->vao.release(); |
| 145 |
|
| 146 |
d->numberOfVertices = m_vertices.size(); |
| 147 |
d->numberOfIndices = m_indices.size(); |
| 148 |
m_dirty = false; |
| 149 |
} |
| 150 |
} |
| 151 |
|
| 152 |
void MeshGeometry::render(const Camera& camera) |
| 153 |
{ |
| 154 |
if (m_indices.empty() || m_vertices.empty()) |
| 155 |
return; |
| 156 |
|
| 157 |
// Prepare the VBOs, IBOs, VAO, and shader program if necessary. |
| 158 |
update(); |
| 159 |
|
| 160 |
ShaderProgram* program = d->program; |
| 161 |
|
| 162 |
if (!program->bind()) |
| 163 |
cout << program->error() << endl; |
| 164 |
|
| 165 |
// Bind the VAO (captures all vertex attribute state) |
| 166 |
d->vao.bind(); |
| 167 |
|
| 168 |
// Set up our uniforms (model-view and projection matrices right now). |
| 169 |
if (!program->setUniformValue("modelView", camera.modelView().matrix())) { |
| 170 |
cout << program->error() << endl; |
| 171 |
} |
| 172 |
if (!program->setUniformValue("projection", camera.projection().matrix())) { |
| 173 |
cout << program->error() << endl; |
| 174 |
} |
| 175 |
Matrix3f normalMatrix = camera.modelView().linear().inverse().transpose(); |
| 176 |
if (!program->setUniformValue("normalMatrix", normalMatrix)) |
| 177 |
std::cout << program->error() << std::endl; |
| 178 |
|
| 179 |
// Render the mesh using the shader and VAO. |
| 180 |
glDrawRangeElements(GL_TRIANGLES, 0, |
| 181 |
static_cast<GLuint>(d->numberOfVertices - 1), |
| 182 |
static_cast<GLsizei>(d->numberOfIndices), GL_UNSIGNED_INT, |
| 183 |
reinterpret_cast<const GLvoid*>(0)); |
| 184 |
|
| 185 |
d->vao.release(); |
| 186 |
program->release(); |
| 187 |
} |
| 188 |
|
| 189 |
unsigned int MeshGeometry::addVertices(const Core::Array<Vector3f>& v, |
| 190 |
const Core::Array<Vector3f>& n, |
| 191 |
const Core::Array<Vector4ub>& c) |
| 192 |
{ |
| 193 |
if (v.size() != n.size() || n.size() != c.size()) |
| 194 |
return InvalidIndex; |
| 195 |
|
| 196 |
size_t result = m_vertices.size(); |
| 197 |
|
| 198 |
auto vIter = v.begin(); |
| 199 |
auto vEnd = v.end(); |
| 200 |
auto nIter = n.begin(); |
| 201 |
auto cIter = c.begin(); |
| 202 |
|
| 203 |
while (vIter != vEnd) |
| 204 |
m_vertices.push_back(PackedVertex(*(cIter++), *(nIter++), *(vIter++))); |
| 205 |
|
| 206 |
m_dirty = true; |
| 207 |
|
| 208 |
return static_cast<unsigned int>(result); |
| 209 |
} |
| 210 |
|
| 211 |
unsigned int MeshGeometry::addVertices(const Core::Array<Vector3f>& v, |
| 212 |
const Core::Array<Vector3f>& n, |
| 213 |
const Core::Array<Vector3ub>& c) |
| 214 |
{ |
| 215 |
if (v.size() != n.size() || n.size() != c.size()) |
| 216 |
return InvalidIndex; |
| 217 |
|
| 218 |
size_t result = m_vertices.size(); |
| 219 |
|
| 220 |
auto vIter = v.begin(); |
| 221 |
auto vEnd = v.end(); |
| 222 |
auto nIter = n.begin(); |
| 223 |
auto cIter = c.begin(); |
| 224 |
|
| 225 |
Vector4ub tmpColor(0, 0, 0, m_opacity); |
| 226 |
while (vIter != vEnd) { |
| 227 |
tmpColor.head<3>() = *(cIter++); |
| 228 |
m_vertices.push_back(PackedVertex(tmpColor, *(nIter++), *(vIter++))); |
| 229 |
} |
| 230 |
|
| 231 |
m_dirty = true; |
| 232 |
|
| 233 |
return static_cast<unsigned int>(result); |
| 234 |
} |
| 235 |
|
| 236 |
unsigned int MeshGeometry::addVertices(const Core::Array<Vector3f>& v, |
| 237 |
const Core::Array<Vector3f>& n) |
| 238 |
{ |
| 239 |
if (v.size() != n.size()) |
| 240 |
return InvalidIndex; |
| 241 |
|
| 242 |
size_t result = m_vertices.size(); |
| 243 |
|
| 244 |
auto vIter = v.begin(); |
| 245 |
auto vEnd = v.end(); |
| 246 |
auto nIter = n.begin(); |
| 247 |
|
| 248 |
const Vector4ub tmpColor(m_color[0], m_color[1], m_color[2], m_opacity); |
| 249 |
while (vIter != vEnd) |
| 250 |
m_vertices.push_back(PackedVertex(tmpColor, *(nIter++), *(vIter++))); |
| 251 |
|
| 252 |
m_dirty = true; |
| 253 |
|
| 254 |
return static_cast<unsigned int>(result); |
| 255 |
} |
| 256 |
|
| 257 |
void MeshGeometry::addTriangle(size_t index1, size_t index2, size_t index3) |
| 258 |
{ |
| 259 |
m_indices.push_back(index1); |
| 260 |
m_indices.push_back(index2); |
| 261 |
m_indices.push_back(index3); |
| 262 |
m_dirty = true; |
| 263 |
} |
| 264 |
|
| 265 |
void MeshGeometry::addTriangles(const Core::Array<unsigned int>& indiceArray) |
| 266 |
{ |
| 267 |
m_indices.reserve(m_indices.size() + indiceArray.size()); |
| 268 |
std::copy(indiceArray.begin(), indiceArray.end(), |
| 269 |
std::back_inserter(m_indices)); |
| 270 |
m_dirty = true; |
| 271 |
} |
| 272 |
|
| 273 |
void MeshGeometry::clear() |
| 274 |
{ |
| 275 |
m_vertices.clear(); |
| 276 |
m_indices.clear(); |
| 277 |
m_dirty = true; |
| 278 |
} |
| 279 |
|
| 280 |
} // namespace Avogadro::Rendering |