forked from CelestiaProject/Celestia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodelgeometry.cpp
More file actions
239 lines (200 loc) · 7.35 KB
/
modelgeometry.cpp
File metadata and controls
239 lines (200 loc) · 7.35 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// modelgeometry.cpp
//
// Copyright (C) 2004-2010, Celestia Development Team
// Original version by Chris Laurel <claurel@gmail.com>
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
#include "modelgeometry.h"
#include "rendcontext.h"
#include "texmanager.h"
#include <Eigen/Core>
#include <functional>
#include <algorithm>
#include <cassert>
using namespace cmod;
using namespace Eigen;
using namespace std;
using namespace celmath;
// Vertex buffer object support
// VBO optimization is only worthwhile for large enough vertex lists
static const unsigned int MinVBOSize = 4096;
class ModelOpenGLData
{
public:
ModelOpenGLData() = default;
~ModelOpenGLData()
{
for (auto vboId : vbos)
{
if (vboId.second)
{
glDeleteBuffers(1, &vboId.second);
}
}
}
std::map<const void*, GLuint> vbos; // vertex buffer objects
};
/** Create a new ModelGeometry wrapping the specified model.
* The ModelGeoemtry takes ownership of the model.
*/
ModelGeometry::ModelGeometry(unique_ptr<cmod::Model>&& model) :
m_model(move(model)),
m_glData(unique_ptr<ModelOpenGLData>(new ModelOpenGLData()))
{
}
bool
ModelGeometry::pick(const Ray3d& r, double& distance) const
{
return m_model->pick(r.origin, r.direction, distance);
}
/*! Render the model; the time parameter is ignored right now
* since this class doesn't currently support animation.
*/
void
ModelGeometry::render(RenderContext& rc, double /* t */)
{
// The first time the mesh is rendered, we will try and place the
// vertex data in a vertex buffer object and potentially get a huge
// rendering performance boost. This can consume a great deal of
// memory, since we're duplicating the vertex data. TODO: investigate
// the possibility of deleting the original data. We can always map
// read-only later on for things like picking, but this could be a low
// performance path.
if (!m_vbInitialized)
{
m_vbInitialized = true;
for (unsigned int i = 0; i < m_model->getMeshCount(); ++i)
{
Mesh* mesh = m_model->getMesh(i);
const Mesh::VertexDescription& vertexDesc = mesh->getVertexDescription();
GLuint vboId = 0;
if (mesh->getVertexCount() * vertexDesc.stride > MinVBOSize)
{
glGenBuffers(1, &vboId);
if (vboId != 0)
{
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER,
mesh->getVertexCount() * vertexDesc.stride,
mesh->getVertexData(),
GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
m_glData->vbos[mesh->getVertexData()] = vboId;
}
}
for (unsigned int groupIndex = 0; groupIndex < mesh->getGroupCount(); ++groupIndex)
{
const Mesh::PrimitiveGroup* group = mesh->getGroup(groupIndex);
if (group->vertexOverride != nullptr && group->vertexCountOverride * group->vertexDescriptionOverride.stride > MinVBOSize)
{
glGenBuffers(1, &vboId);
if (vboId != 0)
{
glBindBuffer(GL_ARRAY_BUFFER, vboId);
glBufferData(GL_ARRAY_BUFFER,
group->vertexCountOverride * group->vertexDescriptionOverride.stride,
group->vertexOverride, GL_STATIC_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
m_glData->vbos[group->vertexOverride] = vboId;
}
}
}
}
}
unsigned int lastMaterial = ~0u;
unsigned int materialCount = m_model->getMaterialCount();
// Iterate over all meshes in the model
for (unsigned int meshIndex = 0; meshIndex < m_model->getMeshCount(); ++meshIndex)
{
Mesh* mesh = m_model->getMesh(meshIndex);
const void* currentData = nullptr;
GLuint currentVboId = 0;
// Iterate over all primitive groups in the mesh
for (unsigned int groupIndex = 0; groupIndex < mesh->getGroupCount(); ++groupIndex)
{
const Mesh::PrimitiveGroup* group = mesh->getGroup(groupIndex);
bool useOverrideValue = group->vertexOverride != nullptr && rc.shouldDrawLineAsTriangles();
const void* data = useOverrideValue ? group->vertexOverride : mesh->getVertexData();
auto vertexDescription = useOverrideValue ? group->vertexDescriptionOverride : mesh->getVertexDescription();
if (currentData != data)
{
GLuint vboId = m_glData->vbos[data];
if (vboId != 0)
{
// Bind the vertex buffer object.
glBindBuffer(GL_ARRAY_BUFFER, vboId);
rc.setVertexArrays(vertexDescription, nullptr);
}
else
{
if (currentVboId != 0)
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
// No vertex buffer object; just use normal vertex arrays
rc.setVertexArrays(vertexDescription, data);
}
currentData = data;
currentVboId = vboId;
}
rc.updateShader(vertexDescription, group->prim);
// Set up the material
const Material* material = nullptr;
unsigned int materialIndex = group->materialIndex;
if (materialIndex != lastMaterial && materialIndex < materialCount)
{
material = m_model->getMaterial(materialIndex);
}
rc.setMaterial(material);
rc.drawGroup(*group, useOverrideValue);
}
// If we set a VBO, unbind it.
if (currentVboId != 0)
{
glBindBuffer(GL_ARRAY_BUFFER, 0);
}
}
}
bool
ModelGeometry::isOpaque() const
{
return m_model->isOpaque();
}
bool
ModelGeometry::isNormalized() const
{
return m_model->isNormalized();
}
bool
ModelGeometry::usesTextureType(Material::TextureSemantic t) const
{
return m_model->usesTextureType(t);
}
void
ModelGeometry::loadTextures()
{
#if 0
for (const auto m : materials)
{
if (m->maps[Mesh::DiffuseMap] != InvalidResource)
GetTextureManager()->find(m->maps[Mesh::DiffuseMap]);
if (m->maps[Mesh::NormalMap] != InvalidResource)
GetTextureManager()->find(m->maps[Mesh::NormalMap]);
if (m->maps[Mesh::SpecularMap] != InvalidResource)
GetTextureManager()->find(m->maps[Mesh::SpecularMap]);
if (m->maps[Mesh::EmissiveMap] != InvalidResource)
GetTextureManager()->find(m->maps[Mesh::EmissiveMap]);
}
#endif
}
fs::path
CelestiaTextureResource::source() const
{
if (m_textureHandle == InvalidResource)
return fs::path();
const TextureInfo* t = GetTextureManager()->getResourceInfo(textureHandle());
return t ? t->source : fs::path();
}